(delete_insn_for_stacker): Don't set INSN_DELETED_P on
[gcc.git] / gcc / reg-stack.c
1 /* Register to Stack convert for GNU compiler.
2 Copyright (C) 1992, 1993, 1994 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.
34
35 The virtual registers can be manipulated normally by gcc, and their
36 semantics are the same as for normal registers. After the hard
37 register numbers are substituted, the semantics of an insn containing
38 stack-like regs are not the same as for an insn with normal regs: for
39 instance, it is not safe to delete an insn that appears to be a no-op
40 move. In general, no insn containing hard regs should be changed
41 after this pass is done.
42
43 * The form of the output:
44
45 After this pass, hard register numbers represent the distance from
46 the current top of stack to the desired register. A reference to
47 FIRST_STACK_REG references the top of stack, FIRST_STACK_REG + 1,
48 represents the register just below that, and so forth. Also, REG_DEAD
49 notes indicate whether or not a stack register should be popped.
50
51 A "swap" insn looks like a parallel of two patterns, where each
52 pattern is a SET: one sets A to B, the other B to A.
53
54 A "push" or "load" insn is a SET whose SET_DEST is FIRST_STACK_REG
55 and whose SET_DEST is REG or MEM. Any other SET_DEST, such as PLUS,
56 will replace the existing stack top, not push a new value.
57
58 A store insn is a SET whose SET_DEST is FIRST_STACK_REG, and whose
59 SET_SRC is REG or MEM.
60
61 The case where the SET_SRC and SET_DEST are both FIRST_STACK_REG
62 appears ambiguous. As a special case, the presence of a REG_DEAD note
63 for FIRST_STACK_REG differentiates between a load insn and a pop.
64
65 If a REG_DEAD is present, the insn represents a "pop" that discards
66 the top of the register stack. If there is no REG_DEAD note, then the
67 insn represents a "dup" or a push of the current top of stack onto the
68 stack.
69
70 * Methodology:
71
72 Existing REG_DEAD and REG_UNUSED notes for stack registers are
73 deleted and recreated from scratch. REG_DEAD is never created for a
74 SET_DEST, only REG_UNUSED.
75
76 Before life analysis, the mode of each insn is set based on whether
77 or not any stack registers are mentioned within that insn. VOIDmode
78 means that no regs are mentioned anyway, and QImode means that at
79 least one pattern within the insn mentions stack registers. This
80 information is valid until after reg_to_stack returns, and is used
81 from jump_optimize.
82
83 * asm_operands:
84
85 There are several rules on the usage of stack-like regs in
86 asm_operands insns. These rules apply only to the operands that are
87 stack-like regs:
88
89 1. Given a set of input regs that die in an asm_operands, it is
90 necessary to know which are implicitly popped by the asm, and
91 which must be explicitly popped by gcc.
92
93 An input reg that is implicitly popped by the asm must be
94 explicitly clobbered, unless it is constrained to match an
95 output operand.
96
97 2. For any input reg that is implicitly popped by an asm, it is
98 necessary to know how to adjust the stack to compensate for the pop.
99 If any non-popped input is closer to the top of the reg-stack than
100 the implicitly popped reg, it would not be possible to know what the
101 stack looked like - it's not clear how the rest of the stack "slides
102 up".
103
104 All implicitly popped input regs must be closer to the top of
105 the reg-stack than any input that is not implicitly popped.
106
107 3. It is possible that if an input dies in an insn, reload might
108 use the input reg for an output reload. Consider this example:
109
110 asm ("foo" : "=t" (a) : "f" (b));
111
112 This asm says that input B is not popped by the asm, and that
113 the asm pushes a result onto the reg-stack, ie, the stack is one
114 deeper after the asm than it was before. But, it is possible that
115 reload will think that it can use the same reg for both the input and
116 the output, if input B dies in this insn.
117
118 If any input operand uses the "f" constraint, all output reg
119 constraints must use the "&" earlyclobber.
120
121 The asm above would be written as
122
123 asm ("foo" : "=&t" (a) : "f" (b));
124
125 4. Some operands need to be in particular places on the stack. All
126 output operands fall in this category - there is no other way to
127 know which regs the outputs appear in unless the user indicates
128 this in the constraints.
129
130 Output operands must specifically indicate which reg an output
131 appears in after an asm. "=f" is not allowed: the operand
132 constraints must select a class with a single reg.
133
134 5. Output operands may not be "inserted" between existing stack regs.
135 Since no 387 opcode uses a read/write operand, all output operands
136 are dead before the asm_operands, and are pushed by the asm_operands.
137 It makes no sense to push anywhere but the top of the reg-stack.
138
139 Output operands must start at the top of the reg-stack: output
140 operands may not "skip" a reg.
141
142 6. Some asm statements may need extra stack space for internal
143 calculations. This can be guaranteed by clobbering stack registers
144 unrelated to the inputs and outputs.
145
146 Here are a couple of reasonable asms to want to write. This asm
147 takes one input, which is internally popped, and produces two outputs.
148
149 asm ("fsincos" : "=t" (cos), "=u" (sin) : "0" (inp));
150
151 This asm takes two inputs, which are popped by the fyl2xp1 opcode,
152 and replaces them with one output. The user must code the "st(1)"
153 clobber for reg-stack.c to know that fyl2xp1 pops both inputs.
154
155 asm ("fyl2xp1" : "=t" (result) : "0" (x), "u" (y) : "st(1)");
156
157 */
158 \f
159 #include <stdio.h>
160 #include "config.h"
161 #include "tree.h"
162 #include "rtl.h"
163 #include "insn-config.h"
164 #include "regs.h"
165 #include "hard-reg-set.h"
166 #include "flags.h"
167
168 #ifdef STACK_REGS
169
170 #define REG_STACK_SIZE (LAST_STACK_REG - FIRST_STACK_REG + 1)
171
172 /* True if the current function returns a real value. */
173 static int current_function_returns_real;
174
175 /* This is the basic stack record. TOP is an index into REG[] such
176 that REG[TOP] is the top of stack. If TOP is -1 the stack is empty.
177
178 If TOP is -2, REG[] is not yet initialized. Stack initialization
179 consists of placing each live reg in array `reg' and setting `top'
180 appropriately.
181
182 REG_SET indicates which registers are live. */
183
184 typedef struct stack_def
185 {
186 int top; /* index to top stack element */
187 HARD_REG_SET reg_set; /* set of live registers */
188 char reg[REG_STACK_SIZE]; /* register - stack mapping */
189 } *stack;
190
191 /* highest instruction uid */
192 static int max_uid = 0;
193
194 /* Number of basic blocks in the current function. */
195 static int blocks;
196
197 /* Element N is first insn in basic block N.
198 This info lasts until we finish compiling the function. */
199 static rtx *block_begin;
200
201 /* Element N is last insn in basic block N.
202 This info lasts until we finish compiling the function. */
203 static rtx *block_end;
204
205 /* Element N is nonzero if control can drop into basic block N */
206 static char *block_drops_in;
207
208 /* Element N says all about the stack at entry block N */
209 static stack block_stack_in;
210
211 /* Element N says all about the stack life at the end of block N */
212 static HARD_REG_SET *block_out_reg_set;
213
214 /* This is where the BLOCK_NUM values are really stored. This is set
215 up by find_blocks and used there and in life_analysis. It can be used
216 later, but only to look up an insn that is the head or tail of some
217 block. life_analysis and the stack register conversion process can
218 add insns within a block. */
219 static int *block_number;
220
221 /* This is the register file for all register after conversion */
222 static rtx FP_mode_reg[FIRST_PSEUDO_REGISTER][(int) MAX_MACHINE_MODE];
223
224 /* Get the basic block number of an insn. See note at block_number
225 definition are validity of this information. */
226
227 #define BLOCK_NUM(INSN) \
228 (((INSN_UID (INSN) > max_uid) \
229 ? (int *)(abort() , 0) \
230 : block_number)[INSN_UID (INSN)])
231
232 extern rtx forced_labels;
233 extern rtx gen_jump ();
234 extern rtx gen_movdf (), gen_movxf ();
235 extern rtx find_regno_note ();
236 extern rtx emit_jump_insn_before ();
237 extern rtx emit_label_after ();
238
239 /* Forward declarations */
240
241 static void find_blocks ();
242 static uses_reg_or_mem ();
243 static void stack_reg_life_analysis ();
244 static void change_stack ();
245 static void convert_regs ();
246 static void dump_stack_info ();
247 \f
248 /* Return non-zero if any stack register is mentioned somewhere within PAT. */
249
250 int
251 stack_regs_mentioned_p (pat)
252 rtx pat;
253 {
254 register char *fmt;
255 register int i;
256
257 if (STACK_REG_P (pat))
258 return 1;
259
260 fmt = GET_RTX_FORMAT (GET_CODE (pat));
261 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
262 {
263 if (fmt[i] == 'E')
264 {
265 register int j;
266
267 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
268 if (stack_regs_mentioned_p (XVECEXP (pat, i, j)))
269 return 1;
270 }
271 else if (fmt[i] == 'e' && stack_regs_mentioned_p (XEXP (pat, i)))
272 return 1;
273 }
274
275 return 0;
276 }
277 \f
278 /* Convert register usage from "flat" register file usage to a "stack
279 register file. FIRST is the first insn in the function, FILE is the
280 dump file, if used.
281
282 First compute the beginning and end of each basic block. Do a
283 register life analysis on the stack registers, recording the result
284 for the head and tail of each basic block. The convert each insn one
285 by one. Run a last jump_optimize() pass, if optimizing, to eliminate
286 any cross-jumping created when the converter inserts pop insns.*/
287
288 void
289 reg_to_stack (first, file)
290 rtx first;
291 FILE *file;
292 {
293 register rtx insn;
294 register int i;
295 int stack_reg_seen = 0;
296 enum machine_mode mode;
297
298 current_function_returns_real
299 = TREE_CODE (TREE_TYPE (DECL_RESULT (current_function_decl))) == REAL_TYPE;
300
301 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
302 mode = GET_MODE_WIDER_MODE (mode))
303 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
304 FP_mode_reg[i][(int) mode] = gen_rtx (REG, mode, i);
305
306 /* Count the basic blocks. Also find maximum insn uid. */
307 {
308 register RTX_CODE prev_code = BARRIER;
309 register RTX_CODE code;
310
311 max_uid = 0;
312 blocks = 0;
313 for (insn = first; insn; insn = NEXT_INSN (insn))
314 {
315 /* Note that this loop must select the same block boundaries
316 as code in find_blocks. Also note that this code is not the
317 same as that used in flow.c. */
318
319 if (INSN_UID (insn) > max_uid)
320 max_uid = INSN_UID (insn);
321
322 code = GET_CODE (insn);
323
324 if (code == CODE_LABEL
325 || (prev_code != INSN
326 && prev_code != CALL_INSN
327 && prev_code != CODE_LABEL
328 && GET_RTX_CLASS (code) == 'i'))
329 blocks++;
330
331 /* Remember whether or not this insn mentions an FP regs.
332 Check JUMP_INSNs too, in case someone creates a funny PARALLEL. */
333
334 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
335 && stack_regs_mentioned_p (PATTERN (insn)))
336 {
337 stack_reg_seen = 1;
338 PUT_MODE (insn, QImode);
339 }
340 else
341 PUT_MODE (insn, VOIDmode);
342
343 if (code == CODE_LABEL)
344 LABEL_REFS (insn) = insn; /* delete old chain */
345
346 if (code != NOTE)
347 prev_code = code;
348 }
349 }
350
351 /* If no stack register reference exists in this insn, there isn't
352 anything to convert. */
353
354 if (! stack_reg_seen)
355 return;
356
357 /* If there are stack registers, there must be at least one block. */
358
359 if (! blocks)
360 abort ();
361
362 /* Allocate some tables that last till end of compiling this function
363 and some needed only in find_blocks and life_analysis. */
364
365 block_begin = (rtx *) alloca (blocks * sizeof (rtx));
366 block_end = (rtx *) alloca (blocks * sizeof (rtx));
367 block_drops_in = (char *) alloca (blocks);
368
369 block_stack_in = (stack) alloca (blocks * sizeof (struct stack_def));
370 block_out_reg_set = (HARD_REG_SET *) alloca (blocks * sizeof (HARD_REG_SET));
371 bzero (block_stack_in, blocks * sizeof (struct stack_def));
372 bzero (block_out_reg_set, blocks * sizeof (HARD_REG_SET));
373
374 block_number = (int *) alloca ((max_uid + 1) * sizeof (int));
375
376 find_blocks (first);
377 stack_reg_life_analysis (first);
378
379 /* Dump the life analysis debug information before jump
380 optimization, as that will destroy the LABEL_REFS we keep the
381 information in. */
382
383 if (file)
384 dump_stack_info (file);
385
386 convert_regs ();
387
388 if (optimize)
389 jump_optimize (first, 2, 0, 0);
390 }
391 \f
392 /* Check PAT, which is in INSN, for LABEL_REFs. Add INSN to the
393 label's chain of references, and note which insn contains each
394 reference. */
395
396 static void
397 record_label_references (insn, pat)
398 rtx insn, pat;
399 {
400 register enum rtx_code code = GET_CODE (pat);
401 register int i;
402 register char *fmt;
403
404 if (code == LABEL_REF)
405 {
406 register rtx label = XEXP (pat, 0);
407 register rtx ref;
408
409 if (GET_CODE (label) != CODE_LABEL)
410 abort ();
411
412 /* Don't make a duplicate in the code_label's chain. */
413
414 for (ref = LABEL_REFS (label);
415 ref && ref != label;
416 ref = LABEL_NEXTREF (ref))
417 if (CONTAINING_INSN (ref) == insn)
418 return;
419
420 CONTAINING_INSN (pat) = insn;
421 LABEL_NEXTREF (pat) = LABEL_REFS (label);
422 LABEL_REFS (label) = pat;
423
424 return;
425 }
426
427 fmt = GET_RTX_FORMAT (code);
428 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
429 {
430 if (fmt[i] == 'e')
431 record_label_references (insn, XEXP (pat, i));
432 if (fmt[i] == 'E')
433 {
434 register int j;
435 for (j = 0; j < XVECLEN (pat, i); j++)
436 record_label_references (insn, XVECEXP (pat, i, j));
437 }
438 }
439 }
440 \f
441 /* Return a pointer to the REG expression within PAT. If PAT is not a
442 REG, possible enclosed by a conversion rtx, return the inner part of
443 PAT that stopped the search. */
444
445 static rtx *
446 get_true_reg (pat)
447 rtx *pat;
448 {
449 while (GET_CODE (*pat) == SUBREG
450 || GET_CODE (*pat) == FLOAT
451 || GET_CODE (*pat) == FIX
452 || GET_CODE (*pat) == FLOAT_EXTEND)
453 pat = & XEXP (*pat, 0);
454
455 return pat;
456 }
457 \f
458 /* Scan the OPERANDS and OPERAND_CONSTRAINTS of an asm_operands.
459 N_OPERANDS is the total number of operands. Return which alternative
460 matched, or -1 is no alternative matches.
461
462 OPERAND_MATCHES is an array which indicates which operand this
463 operand matches due to the constraints, or -1 if no match is required.
464 If two operands match by coincidence, but are not required to match by
465 the constraints, -1 is returned.
466
467 OPERAND_CLASS is an array which indicates the smallest class
468 required by the constraints. If the alternative that matches calls
469 for some class `class', and the operand matches a subclass of `class',
470 OPERAND_CLASS is set to `class' as required by the constraints, not to
471 the subclass. If an alternative allows more than one class,
472 OPERAND_CLASS is set to the smallest class that is a union of the
473 allowed classes. */
474
475 static int
476 constrain_asm_operands (n_operands, operands, operand_constraints,
477 operand_matches, operand_class)
478 int n_operands;
479 rtx *operands;
480 char **operand_constraints;
481 int *operand_matches;
482 enum reg_class *operand_class;
483 {
484 char **constraints = (char **) alloca (n_operands * sizeof (char *));
485 char *q;
486 int this_alternative, this_operand;
487 int n_alternatives;
488 int j;
489
490 for (j = 0; j < n_operands; j++)
491 constraints[j] = operand_constraints[j];
492
493 /* Compute the number of alternatives in the operands. reload has
494 already guaranteed that all operands have the same number of
495 alternatives. */
496
497 n_alternatives = 1;
498 for (q = constraints[0]; *q; q++)
499 n_alternatives += (*q == ',');
500
501 this_alternative = 0;
502 while (this_alternative < n_alternatives)
503 {
504 int lose = 0;
505 int i;
506
507 /* No operands match, no narrow class requirements yet. */
508 for (i = 0; i < n_operands; i++)
509 {
510 operand_matches[i] = -1;
511 operand_class[i] = NO_REGS;
512 }
513
514 for (this_operand = 0; this_operand < n_operands; this_operand++)
515 {
516 rtx op = operands[this_operand];
517 enum machine_mode mode = GET_MODE (op);
518 char *p = constraints[this_operand];
519 int offset = 0;
520 int win = 0;
521 int c;
522
523 if (GET_CODE (op) == SUBREG)
524 {
525 if (GET_CODE (SUBREG_REG (op)) == REG
526 && REGNO (SUBREG_REG (op)) < FIRST_PSEUDO_REGISTER)
527 offset = SUBREG_WORD (op);
528 op = SUBREG_REG (op);
529 }
530
531 /* An empty constraint or empty alternative
532 allows anything which matched the pattern. */
533 if (*p == 0 || *p == ',')
534 win = 1;
535
536 while (*p && (c = *p++) != ',')
537 switch (c)
538 {
539 case '=':
540 case '+':
541 case '?':
542 case '&':
543 case '!':
544 case '*':
545 case '%':
546 /* Ignore these. */
547 break;
548
549 case '#':
550 /* Ignore rest of this alternative. */
551 while (*p && *p != ',') p++;
552 break;
553
554 case '0':
555 case '1':
556 case '2':
557 case '3':
558 case '4':
559 case '5':
560 /* This operand must be the same as a previous one.
561 This kind of constraint is used for instructions such
562 as add when they take only two operands.
563
564 Note that the lower-numbered operand is passed first. */
565
566 if (operands_match_p (operands[c - '0'],
567 operands[this_operand]))
568 {
569 operand_matches[this_operand] = c - '0';
570 win = 1;
571 }
572 break;
573
574 case 'p':
575 /* p is used for address_operands. Since this is an asm,
576 just to make sure that the operand is valid for Pmode. */
577
578 if (strict_memory_address_p (Pmode, op))
579 win = 1;
580 break;
581
582 case 'g':
583 /* Anything goes unless it is a REG and really has a hard reg
584 but the hard reg is not in the class GENERAL_REGS. */
585 if (GENERAL_REGS == ALL_REGS
586 || GET_CODE (op) != REG
587 || reg_fits_class_p (op, GENERAL_REGS, offset, mode))
588 {
589 if (GET_CODE (op) == REG)
590 operand_class[this_operand]
591 = reg_class_subunion[(int) operand_class[this_operand]][(int) GENERAL_REGS];
592 win = 1;
593 }
594 break;
595
596 case 'r':
597 if (GET_CODE (op) == REG
598 && (GENERAL_REGS == ALL_REGS
599 || reg_fits_class_p (op, GENERAL_REGS, offset, mode)))
600 {
601 operand_class[this_operand]
602 = reg_class_subunion[(int) operand_class[this_operand]][(int) GENERAL_REGS];
603 win = 1;
604 }
605 break;
606
607 case 'X':
608 /* This is used for a MATCH_SCRATCH in the cases when we
609 don't actually need anything. So anything goes any time. */
610 win = 1;
611 break;
612
613 case 'm':
614 if (GET_CODE (op) == MEM)
615 win = 1;
616 break;
617
618 case '<':
619 if (GET_CODE (op) == MEM
620 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
621 || GET_CODE (XEXP (op, 0)) == POST_DEC))
622 win = 1;
623 break;
624
625 case '>':
626 if (GET_CODE (op) == MEM
627 && (GET_CODE (XEXP (op, 0)) == PRE_INC
628 || GET_CODE (XEXP (op, 0)) == POST_INC))
629 win = 1;
630 break;
631
632 case 'E':
633 /* Match any CONST_DOUBLE, but only if
634 we can examine the bits of it reliably. */
635 if ((HOST_FLOAT_FORMAT != TARGET_FLOAT_FORMAT
636 || HOST_BITS_PER_WIDE_INT != BITS_PER_WORD)
637 && GET_CODE (op) != VOIDmode && ! flag_pretend_float)
638 break;
639 if (GET_CODE (op) == CONST_DOUBLE)
640 win = 1;
641 break;
642
643 case 'F':
644 if (GET_CODE (op) == CONST_DOUBLE)
645 win = 1;
646 break;
647
648 case 'G':
649 case 'H':
650 if (GET_CODE (op) == CONST_DOUBLE
651 && CONST_DOUBLE_OK_FOR_LETTER_P (op, c))
652 win = 1;
653 break;
654
655 case 's':
656 if (GET_CODE (op) == CONST_INT
657 || (GET_CODE (op) == CONST_DOUBLE
658 && GET_MODE (op) == VOIDmode))
659 break;
660 /* Fall through */
661 case 'i':
662 if (CONSTANT_P (op))
663 win = 1;
664 break;
665
666 case 'n':
667 if (GET_CODE (op) == CONST_INT
668 || (GET_CODE (op) == CONST_DOUBLE
669 && GET_MODE (op) == VOIDmode))
670 win = 1;
671 break;
672
673 case 'I':
674 case 'J':
675 case 'K':
676 case 'L':
677 case 'M':
678 case 'N':
679 case 'O':
680 case 'P':
681 if (GET_CODE (op) == CONST_INT
682 && CONST_OK_FOR_LETTER_P (INTVAL (op), c))
683 win = 1;
684 break;
685
686 #ifdef EXTRA_CONSTRAINT
687 case 'Q':
688 case 'R':
689 case 'S':
690 case 'T':
691 case 'U':
692 if (EXTRA_CONSTRAINT (op, c))
693 win = 1;
694 break;
695 #endif
696
697 case 'V':
698 if (GET_CODE (op) == MEM && ! offsettable_memref_p (op))
699 win = 1;
700 break;
701
702 case 'o':
703 if (offsettable_memref_p (op))
704 win = 1;
705 break;
706
707 default:
708 if (GET_CODE (op) == REG
709 && reg_fits_class_p (op, REG_CLASS_FROM_LETTER (c),
710 offset, mode))
711 {
712 operand_class[this_operand]
713 = reg_class_subunion[(int)operand_class[this_operand]][(int) REG_CLASS_FROM_LETTER (c)];
714 win = 1;
715 }
716 }
717
718 constraints[this_operand] = p;
719 /* If this operand did not win somehow,
720 this alternative loses. */
721 if (! win)
722 lose = 1;
723 }
724 /* This alternative won; the operands are ok.
725 Change whichever operands this alternative says to change. */
726 if (! lose)
727 break;
728
729 this_alternative++;
730 }
731
732 /* For operands constrained to match another operand, copy the other
733 operand's class to this operand's class. */
734 for (j = 0; j < n_operands; j++)
735 if (operand_matches[j] >= 0)
736 operand_class[j] = operand_class[operand_matches[j]];
737
738 return this_alternative == n_alternatives ? -1 : this_alternative;
739 }
740 \f
741 /* Record the life info of each stack reg in INSN, updating REGSTACK.
742 N_INPUTS is the number of inputs; N_OUTPUTS the outputs. CONSTRAINTS
743 is an array of the constraint strings used in the asm statement.
744 OPERANDS is an array of all operands for the insn, and is assumed to
745 contain all output operands, then all inputs operands.
746
747 There are many rules that an asm statement for stack-like regs must
748 follow. Those rules are explained at the top of this file: the rule
749 numbers below refer to that explanation. */
750
751 static void
752 record_asm_reg_life (insn, regstack, operands, constraints,
753 n_inputs, n_outputs)
754 rtx insn;
755 stack regstack;
756 rtx *operands;
757 char **constraints;
758 int n_inputs, n_outputs;
759 {
760 int i;
761 int n_operands = n_inputs + n_outputs;
762 int first_input = n_outputs;
763 int n_clobbers;
764 int malformed_asm = 0;
765 rtx body = PATTERN (insn);
766
767 int *operand_matches = (int *) alloca (n_operands * sizeof (int *));
768
769 enum reg_class *operand_class
770 = (enum reg_class *) alloca (n_operands * sizeof (enum reg_class *));
771
772 int reg_used_as_output[FIRST_PSEUDO_REGISTER];
773 int implicitly_dies[FIRST_PSEUDO_REGISTER];
774
775 rtx *clobber_reg;
776
777 /* Find out what the constraints require. If no constraint
778 alternative matches, this asm is malformed. */
779 i = constrain_asm_operands (n_operands, operands, constraints,
780 operand_matches, operand_class);
781 if (i < 0)
782 malformed_asm = 1;
783
784 /* Strip SUBREGs here to make the following code simpler. */
785 for (i = 0; i < n_operands; i++)
786 if (GET_CODE (operands[i]) == SUBREG
787 && GET_CODE (SUBREG_REG (operands[i])) == REG)
788 operands[i] = SUBREG_REG (operands[i]);
789
790 /* Set up CLOBBER_REG. */
791
792 n_clobbers = 0;
793
794 if (GET_CODE (body) == PARALLEL)
795 {
796 clobber_reg = (rtx *) alloca (XVECLEN (body, 0) * sizeof (rtx *));
797
798 for (i = 0; i < XVECLEN (body, 0); i++)
799 if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
800 {
801 rtx clobber = XVECEXP (body, 0, i);
802 rtx reg = XEXP (clobber, 0);
803
804 if (GET_CODE (reg) == SUBREG && GET_CODE (SUBREG_REG (reg)) == REG)
805 reg = SUBREG_REG (reg);
806
807 if (STACK_REG_P (reg))
808 {
809 clobber_reg[n_clobbers] = reg;
810 n_clobbers++;
811 }
812 }
813 }
814
815 /* Enforce rule #4: Output operands must specifically indicate which
816 reg an output appears in after an asm. "=f" is not allowed: the
817 operand constraints must select a class with a single reg.
818
819 Also enforce rule #5: Output operands must start at the top of
820 the reg-stack: output operands may not "skip" a reg. */
821
822 bzero (reg_used_as_output, sizeof (reg_used_as_output));
823 for (i = 0; i < n_outputs; i++)
824 if (STACK_REG_P (operands[i]))
825 if (reg_class_size[(int) operand_class[i]] != 1)
826 {
827 error_for_asm
828 (insn, "Output constraint %d must specify a single register", i);
829 malformed_asm = 1;
830 }
831 else
832 reg_used_as_output[REGNO (operands[i])] = 1;
833
834
835 /* Search for first non-popped reg. */
836 for (i = FIRST_STACK_REG; i < LAST_STACK_REG + 1; i++)
837 if (! reg_used_as_output[i])
838 break;
839
840 /* If there are any other popped regs, that's an error. */
841 for (; i < LAST_STACK_REG + 1; i++)
842 if (reg_used_as_output[i])
843 break;
844
845 if (i != LAST_STACK_REG + 1)
846 {
847 error_for_asm (insn, "Output regs must be grouped at top of stack");
848 malformed_asm = 1;
849 }
850
851 /* Enforce rule #2: All implicitly popped input regs must be closer
852 to the top of the reg-stack than any input that is not implicitly
853 popped. */
854
855 bzero (implicitly_dies, sizeof (implicitly_dies));
856 for (i = first_input; i < first_input + n_inputs; i++)
857 if (STACK_REG_P (operands[i]))
858 {
859 /* An input reg is implicitly popped if it is tied to an
860 output, or if there is a CLOBBER for it. */
861 int j;
862
863 for (j = 0; j < n_clobbers; j++)
864 if (operands_match_p (clobber_reg[j], operands[i]))
865 break;
866
867 if (j < n_clobbers || operand_matches[i] >= 0)
868 implicitly_dies[REGNO (operands[i])] = 1;
869 }
870
871 /* Search for first non-popped reg. */
872 for (i = FIRST_STACK_REG; i < LAST_STACK_REG + 1; i++)
873 if (! implicitly_dies[i])
874 break;
875
876 /* If there are any other popped regs, that's an error. */
877 for (; i < LAST_STACK_REG + 1; i++)
878 if (implicitly_dies[i])
879 break;
880
881 if (i != LAST_STACK_REG + 1)
882 {
883 error_for_asm (insn,
884 "Implicitly popped regs must be grouped at top of stack");
885 malformed_asm = 1;
886 }
887
888 /* Enfore rule #3: If any input operand uses the "f" constraint, all
889 output constraints must use the "&" earlyclobber.
890
891 ??? Detect this more deterministically by having constraint_asm_operands
892 record any earlyclobber. */
893
894 for (i = first_input; i < first_input + n_inputs; i++)
895 if (operand_matches[i] == -1)
896 {
897 int j;
898
899 for (j = 0; j < n_outputs; j++)
900 if (operands_match_p (operands[j], operands[i]))
901 {
902 error_for_asm (insn,
903 "Output operand %d must use `&' constraint", j);
904 malformed_asm = 1;
905 }
906 }
907
908 if (malformed_asm)
909 {
910 /* Avoid further trouble with this insn. */
911 PATTERN (insn) = gen_rtx (USE, VOIDmode, const0_rtx);
912 PUT_MODE (insn, VOIDmode);
913 return;
914 }
915
916 /* Process all outputs */
917 for (i = 0; i < n_outputs; i++)
918 {
919 rtx op = operands[i];
920
921 if (! STACK_REG_P (op))
922 if (stack_regs_mentioned_p (op))
923 abort ();
924 else
925 continue;
926
927 /* Each destination is dead before this insn. If the
928 destination is not used after this insn, record this with
929 REG_UNUSED. */
930
931 if (! TEST_HARD_REG_BIT (regstack->reg_set, REGNO (op)))
932 REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_UNUSED, op,
933 REG_NOTES (insn));
934
935 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (op));
936 }
937
938 /* Process all inputs */
939 for (i = first_input; i < first_input + n_inputs; i++)
940 {
941 if (! STACK_REG_P (operands[i]))
942 if (stack_regs_mentioned_p (operands[i]))
943 abort ();
944 else
945 continue;
946
947 /* If an input is dead after the insn, record a death note.
948 But don't record a death note if there is already a death note,
949 or if the input is also an output. */
950
951 if (! TEST_HARD_REG_BIT (regstack->reg_set, REGNO (operands[i]))
952 && operand_matches[i] == -1
953 && find_regno_note (insn, REG_DEAD, REGNO (operands[i])) == NULL_RTX)
954 REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_DEAD, operands[i],
955 REG_NOTES (insn));
956
957 SET_HARD_REG_BIT (regstack->reg_set, REGNO (operands[i]));
958 }
959 }
960
961 /* Scan PAT, which is part of INSN, and record registers appearing in
962 a SET_DEST in DEST, and other registers in SRC.
963
964 This function does not know about SET_DESTs that are both input and
965 output (such as ZERO_EXTRACT) - this cannot happen on a 387. */
966
967 void
968 record_reg_life_pat (pat, src, dest)
969 rtx pat;
970 HARD_REG_SET *src, *dest;
971 {
972 register char *fmt;
973 register int i;
974
975 if (STACK_REG_P (pat))
976 {
977 if (src)
978 SET_HARD_REG_BIT (*src, REGNO (pat));
979
980 if (dest)
981 SET_HARD_REG_BIT (*dest, REGNO (pat));
982
983 return;
984 }
985
986 if (GET_CODE (pat) == SET)
987 {
988 record_reg_life_pat (XEXP (pat, 0), NULL_PTR, dest);
989 record_reg_life_pat (XEXP (pat, 1), src, NULL_PTR);
990 return;
991 }
992
993 /* We don't need to consider either of these cases. */
994 if (GET_CODE (pat) == USE || GET_CODE (pat) == CLOBBER)
995 return;
996
997 fmt = GET_RTX_FORMAT (GET_CODE (pat));
998 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
999 {
1000 if (fmt[i] == 'E')
1001 {
1002 register int j;
1003
1004 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
1005 record_reg_life_pat (XVECEXP (pat, i, j), src, dest);
1006 }
1007 else if (fmt[i] == 'e')
1008 record_reg_life_pat (XEXP (pat, i), src, dest);
1009 }
1010 }
1011 \f
1012 /* Calculate the number of inputs and outputs in BODY, an
1013 asm_operands. N_OPERANDS is the total number of operands, and
1014 N_INPUTS and N_OUTPUTS are pointers to ints into which the results are
1015 placed. */
1016
1017 static void
1018 get_asm_operand_lengths (body, n_operands, n_inputs, n_outputs)
1019 rtx body;
1020 int n_operands;
1021 int *n_inputs, *n_outputs;
1022 {
1023 if (GET_CODE (body) == SET && GET_CODE (SET_SRC (body)) == ASM_OPERANDS)
1024 *n_inputs = ASM_OPERANDS_INPUT_LENGTH (SET_SRC (body));
1025
1026 else if (GET_CODE (body) == ASM_OPERANDS)
1027 *n_inputs = ASM_OPERANDS_INPUT_LENGTH (body);
1028
1029 else if (GET_CODE (body) == PARALLEL
1030 && GET_CODE (XVECEXP (body, 0, 0)) == SET)
1031 *n_inputs = ASM_OPERANDS_INPUT_LENGTH (SET_SRC (XVECEXP (body, 0, 0)));
1032
1033 else if (GET_CODE (body) == PARALLEL
1034 && GET_CODE (XVECEXP (body, 0, 0)) == ASM_OPERANDS)
1035 *n_inputs = ASM_OPERANDS_INPUT_LENGTH (XVECEXP (body, 0, 0));
1036 else
1037 abort ();
1038
1039 *n_outputs = n_operands - *n_inputs;
1040 }
1041 \f
1042 /* Scan INSN, which is in BLOCK, and record the life & death of stack
1043 registers in REGSTACK. This function is called to process insns from
1044 the last insn in a block to the first. The actual scanning is done in
1045 record_reg_life_pat.
1046
1047 If a register is live after a CALL_INSN, but is not a value return
1048 register for that CALL_INSN, then code is emitted to initialize that
1049 register. The block_end[] data is kept accurate.
1050
1051 Existing death and unset notes for stack registers are deleted
1052 before processing the insn. */
1053
1054 static void
1055 record_reg_life (insn, block, regstack)
1056 rtx insn;
1057 int block;
1058 stack regstack;
1059 {
1060 rtx note, *note_link;
1061 int n_operands;
1062
1063 if ((GET_CODE (insn) != INSN && GET_CODE (insn) != CALL_INSN)
1064 || INSN_DELETED_P (insn))
1065 return;
1066
1067 /* Strip death notes for stack regs from this insn */
1068
1069 note_link = &REG_NOTES(insn);
1070 for (note = *note_link; note; note = XEXP (note, 1))
1071 if (STACK_REG_P (XEXP (note, 0))
1072 && (REG_NOTE_KIND (note) == REG_DEAD
1073 || REG_NOTE_KIND (note) == REG_UNUSED))
1074 *note_link = XEXP (note, 1);
1075 else
1076 note_link = &XEXP (note, 1);
1077
1078 /* Process all patterns in the insn. */
1079
1080 n_operands = asm_noperands (PATTERN (insn));
1081 if (n_operands >= 0)
1082 {
1083 /* This insn is an `asm' with operands. Decode the operands,
1084 decide how many are inputs, and record the life information. */
1085
1086 rtx operands[MAX_RECOG_OPERANDS];
1087 rtx body = PATTERN (insn);
1088 int n_inputs, n_outputs;
1089 char **constraints = (char **) alloca (n_operands * sizeof (char *));
1090
1091 decode_asm_operands (body, operands, NULL_PTR, constraints, NULL_PTR);
1092 get_asm_operand_lengths (body, n_operands, &n_inputs, &n_outputs);
1093 record_asm_reg_life (insn, regstack, operands, constraints,
1094 n_inputs, n_outputs);
1095 return;
1096 }
1097
1098 /* An insn referencing a stack reg has a mode of QImode. */
1099 if (GET_MODE (insn) == QImode)
1100 {
1101 HARD_REG_SET src, dest;
1102 int regno;
1103
1104 CLEAR_HARD_REG_SET (src);
1105 CLEAR_HARD_REG_SET (dest);
1106 record_reg_life_pat (PATTERN (insn), &src, &dest);
1107
1108 for (regno = FIRST_STACK_REG; regno <= LAST_STACK_REG; regno++)
1109 if (! TEST_HARD_REG_BIT (regstack->reg_set, regno))
1110 {
1111 if (TEST_HARD_REG_BIT (src, regno)
1112 && ! TEST_HARD_REG_BIT (dest, regno))
1113 REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_DEAD,
1114 FP_mode_reg[regno][(int) DFmode],
1115 REG_NOTES (insn));
1116 else if (TEST_HARD_REG_BIT (dest, regno))
1117 REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_UNUSED,
1118 FP_mode_reg[regno][(int) DFmode],
1119 REG_NOTES (insn));
1120 }
1121
1122 AND_COMPL_HARD_REG_SET (regstack->reg_set, dest);
1123 IOR_HARD_REG_SET (regstack->reg_set, src);
1124 }
1125
1126 /* There might be a reg that is live after a function call.
1127 Initialize it to zero so that the program does not crash. See comment
1128 towards the end of stack_reg_life_analysis(). */
1129
1130 if (GET_CODE (insn) == CALL_INSN)
1131 {
1132 int reg = FIRST_FLOAT_REG;
1133
1134 /* If a stack reg is mentioned in a CALL_INSN, it must be as the
1135 return value. */
1136
1137 if (stack_regs_mentioned_p (PATTERN (insn)))
1138 reg++;
1139
1140 for (; reg <= LAST_STACK_REG; reg++)
1141 if (TEST_HARD_REG_BIT (regstack->reg_set, reg))
1142 {
1143 rtx init, pat;
1144
1145 /* The insn will use virtual register numbers, and so
1146 convert_regs is expected to process these. But BLOCK_NUM
1147 cannot be used on these insns, because they do not appear in
1148 block_number[]. */
1149
1150 pat = gen_rtx (SET, VOIDmode, FP_mode_reg[reg][(int) DFmode],
1151 CONST0_RTX (DFmode));
1152 init = emit_insn_after (pat, insn);
1153 PUT_MODE (init, QImode);
1154
1155 CLEAR_HARD_REG_BIT (regstack->reg_set, reg);
1156
1157 /* If the CALL_INSN was the end of a block, move the
1158 block_end to point to the new insn. */
1159
1160 if (block_end[block] == insn)
1161 block_end[block] = init;
1162 }
1163
1164 /* Some regs do not survive a CALL */
1165
1166 AND_COMPL_HARD_REG_SET (regstack->reg_set, call_used_reg_set);
1167 }
1168 }
1169 \f
1170 /* Find all basic blocks of the function, which starts with FIRST.
1171 For each JUMP_INSN, build the chain of LABEL_REFS on each CODE_LABEL. */
1172
1173 static void
1174 find_blocks (first)
1175 rtx first;
1176 {
1177 register rtx insn;
1178 register int block;
1179 register RTX_CODE prev_code = BARRIER;
1180 register RTX_CODE code;
1181 rtx label_value_list = 0;
1182
1183 /* Record where all the blocks start and end.
1184 Record which basic blocks control can drop in to. */
1185
1186 block = -1;
1187 for (insn = first; insn; insn = NEXT_INSN (insn))
1188 {
1189 /* Note that this loop must select the same block boundaries
1190 as code in reg_to_stack, but that these are not the same
1191 as those selected in flow.c. */
1192
1193 code = GET_CODE (insn);
1194
1195 if (code == CODE_LABEL
1196 || (prev_code != INSN
1197 && prev_code != CALL_INSN
1198 && prev_code != CODE_LABEL
1199 && GET_RTX_CLASS (code) == 'i'))
1200 {
1201 block_begin[++block] = insn;
1202 block_end[block] = insn;
1203 block_drops_in[block] = prev_code != BARRIER;
1204 }
1205 else if (GET_RTX_CLASS (code) == 'i')
1206 block_end[block] = insn;
1207
1208 if (GET_RTX_CLASS (code) == 'i')
1209 {
1210 rtx note;
1211
1212 /* Make a list of all labels referred to other than by jumps. */
1213 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1214 if (REG_NOTE_KIND (note) == REG_LABEL)
1215 label_value_list = gen_rtx (EXPR_LIST, VOIDmode, XEXP (note, 0),
1216 label_value_list);
1217 }
1218
1219 BLOCK_NUM (insn) = block;
1220
1221 if (code != NOTE)
1222 prev_code = code;
1223 }
1224
1225 if (block + 1 != blocks)
1226 abort ();
1227
1228 /* generate all label references to the corresponding jump insn */
1229 for (block = 0; block < blocks; block++)
1230 {
1231 insn = block_end[block];
1232
1233 if (GET_CODE (insn) == JUMP_INSN)
1234 {
1235 rtx pat = PATTERN (insn);
1236 int computed_jump = 0;
1237 rtx x;
1238
1239 if (GET_CODE (pat) == PARALLEL)
1240 {
1241 int len = XVECLEN (pat, 0);
1242 int has_use_labelref = 0;
1243 int i;
1244
1245 for (i = len - 1; i >= 0; i--)
1246 if (GET_CODE (XVECEXP (pat, 0, i)) == USE
1247 && GET_CODE (XEXP (XVECEXP (pat, 0, i), 0)) == LABEL_REF)
1248 has_use_labelref = 1;
1249
1250 if (! has_use_labelref)
1251 for (i = len - 1; i >= 0; i--)
1252 if (GET_CODE (XVECEXP (pat, 0, i)) == SET
1253 && SET_DEST (XVECEXP (pat, 0, i)) == pc_rtx
1254 && uses_reg_or_mem (SET_SRC (XVECEXP (pat, 0, i))))
1255 computed_jump = 1;
1256 }
1257 else if (GET_CODE (pat) == SET
1258 && SET_DEST (pat) == pc_rtx
1259 && uses_reg_or_mem (SET_SRC (pat)))
1260 computed_jump = 1;
1261
1262 if (computed_jump)
1263 {
1264 for (x = label_value_list; x; x = XEXP (x, 1))
1265 record_label_references (insn,
1266 gen_rtx (LABEL_REF, VOIDmode,
1267 XEXP (x, 0)));
1268
1269 for (x = forced_labels; x; x = XEXP (x, 1))
1270 record_label_references (insn,
1271 gen_rtx (LABEL_REF, VOIDmode,
1272 XEXP (x, 0)));
1273 }
1274
1275 record_label_references (insn, pat);
1276 }
1277 }
1278 }
1279
1280 /* Return 1 if X contain a REG or MEM that is not in the constant pool. */
1281
1282 static int
1283 uses_reg_or_mem (x)
1284 rtx x;
1285 {
1286 enum rtx_code code = GET_CODE (x);
1287 int i, j;
1288 char *fmt;
1289
1290 if (code == REG
1291 || (code == MEM
1292 && ! (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1293 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))))
1294 return 1;
1295
1296 fmt = GET_RTX_FORMAT (code);
1297 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1298 {
1299 if (fmt[i] == 'e'
1300 && uses_reg_or_mem (XEXP (x, i)))
1301 return 1;
1302
1303 if (fmt[i] == 'E')
1304 for (j = 0; j < XVECLEN (x, i); j++)
1305 if (uses_reg_or_mem (XVECEXP (x, i, j)))
1306 return 1;
1307 }
1308
1309 return 0;
1310 }
1311
1312 /* If current function returns its result in an fp stack register,
1313 return the register number. Otherwise return -1. */
1314
1315 static int
1316 stack_result_p (decl)
1317 tree decl;
1318 {
1319 rtx result = DECL_RTL (DECL_RESULT (decl));
1320
1321 if (result != 0
1322 && !(GET_CODE (result) == REG
1323 && REGNO (result) < FIRST_PSEUDO_REGISTER))
1324 {
1325 #ifdef FUNCTION_OUTGOING_VALUE
1326 result
1327 = FUNCTION_OUTGOING_VALUE (TREE_TYPE (DECL_RESULT (decl)), decl);
1328 #else
1329 result = FUNCTION_VALUE (TREE_TYPE (DECL_RESULT (decl)), decl);
1330 #endif
1331 }
1332
1333 return STACK_REG_P (result) ? REGNO (result) : -1;
1334 }
1335 \f
1336 /* Determine the which registers are live at the start of each basic
1337 block of the function whose first insn is FIRST.
1338
1339 First, if the function returns a real_type, mark the function
1340 return type as live at each return point, as the RTL may not give any
1341 hint that the register is live.
1342
1343 Then, start with the last block and work back to the first block.
1344 Similarly, work backwards within each block, insn by insn, recording
1345 which regs are die and which are used (and therefore live) in the
1346 hard reg set of block_stack_in[].
1347
1348 After processing each basic block, if there is a label at the start
1349 of the block, propagate the live registers to all jumps to this block.
1350
1351 As a special case, if there are regs live in this block, that are
1352 not live in a block containing a jump to this label, and the block
1353 containing the jump has already been processed, we must propagate this
1354 block's entry register life back to the block containing the jump, and
1355 restart life analysis from there.
1356
1357 In the worst case, this function may traverse the insns
1358 REG_STACK_SIZE times. This is necessary, since a jump towards the end
1359 of the insns may not know that a reg is live at a target that is early
1360 in the insns. So we back up and start over with the new reg live.
1361
1362 If there are registers that are live at the start of the function,
1363 insns are emitted to initialize these registers. Something similar is
1364 done after CALL_INSNs in record_reg_life. */
1365
1366 static void
1367 stack_reg_life_analysis (first)
1368 rtx first;
1369 {
1370 int reg, block;
1371 struct stack_def regstack;
1372
1373 if (current_function_returns_real
1374 && stack_result_p (current_function_decl) >= 0)
1375 {
1376 /* Find all RETURN insns and mark them. */
1377
1378 int value_regno = stack_result_p (current_function_decl);
1379
1380 for (block = blocks - 1; block >= 0; block--)
1381 if (GET_CODE (block_end[block]) == JUMP_INSN
1382 && GET_CODE (PATTERN (block_end[block])) == RETURN)
1383 SET_HARD_REG_BIT (block_out_reg_set[block], value_regno);
1384
1385 /* Mark of the end of last block if we "fall off" the end of the
1386 function into the epilogue. */
1387
1388 if (GET_CODE (block_end[blocks-1]) != JUMP_INSN
1389 || GET_CODE (PATTERN (block_end[blocks-1])) == RETURN)
1390 SET_HARD_REG_BIT (block_out_reg_set[blocks-1], value_regno);
1391 }
1392
1393 /* now scan all blocks backward for stack register use */
1394
1395 block = blocks - 1;
1396 while (block >= 0)
1397 {
1398 register rtx insn, prev;
1399
1400 /* current register status at last instruction */
1401
1402 COPY_HARD_REG_SET (regstack.reg_set, block_out_reg_set[block]);
1403
1404 prev = block_end[block];
1405 do
1406 {
1407 insn = prev;
1408 prev = PREV_INSN (insn);
1409
1410 /* If the insn is a CALL_INSN, we need to ensure that
1411 everything dies. But otherwise don't process unless there
1412 are some stack regs present. */
1413
1414 if (GET_MODE (insn) == QImode || GET_CODE (insn) == CALL_INSN)
1415 record_reg_life (insn, block, &regstack);
1416
1417 } while (insn != block_begin[block]);
1418
1419 /* Set the state at the start of the block. Mark that no
1420 register mapping information known yet. */
1421
1422 COPY_HARD_REG_SET (block_stack_in[block].reg_set, regstack.reg_set);
1423 block_stack_in[block].top = -2;
1424
1425 /* If there is a label, propagate our register life to all jumps
1426 to this label. */
1427
1428 if (GET_CODE (insn) == CODE_LABEL)
1429 {
1430 register rtx label;
1431 int must_restart = 0;
1432
1433 for (label = LABEL_REFS (insn); label != insn;
1434 label = LABEL_NEXTREF (label))
1435 {
1436 int jump_block = BLOCK_NUM (CONTAINING_INSN (label));
1437
1438 if (jump_block < block)
1439 IOR_HARD_REG_SET (block_out_reg_set[jump_block],
1440 block_stack_in[block].reg_set);
1441 else
1442 {
1443 /* The block containing the jump has already been
1444 processed. If there are registers that were not known
1445 to be live then, but are live now, we must back up
1446 and restart life analysis from that point with the new
1447 life information. */
1448
1449 GO_IF_HARD_REG_SUBSET (block_stack_in[block].reg_set,
1450 block_out_reg_set[jump_block],
1451 win);
1452
1453 IOR_HARD_REG_SET (block_out_reg_set[jump_block],
1454 block_stack_in[block].reg_set);
1455
1456 block = jump_block;
1457 must_restart = 1;
1458
1459 win:
1460 ;
1461 }
1462 }
1463 if (must_restart)
1464 continue;
1465 }
1466
1467 if (block_drops_in[block])
1468 IOR_HARD_REG_SET (block_out_reg_set[block-1],
1469 block_stack_in[block].reg_set);
1470
1471 block -= 1;
1472 }
1473
1474 {
1475 /* If any reg is live at the start of the first block of a
1476 function, then we must guarantee that the reg holds some value by
1477 generating our own "load" of that register. Otherwise a 387 would
1478 fault trying to access an empty register. */
1479
1480 HARD_REG_SET empty_regs;
1481 CLEAR_HARD_REG_SET (empty_regs);
1482 GO_IF_HARD_REG_SUBSET (block_stack_in[0].reg_set, empty_regs,
1483 no_live_regs);
1484 }
1485
1486 /* Load zero into each live register. The fact that a register
1487 appears live at the function start does not necessarily imply an error
1488 in the user program: it merely means that we could not determine that
1489 there wasn't such an error, just as -Wunused sometimes gives
1490 "incorrect" warnings. In those cases, these initializations will do
1491 no harm.
1492
1493 Note that we are inserting virtual register references here:
1494 these insns must be processed by convert_regs later. Also, these
1495 insns will not be in block_number, so BLOCK_NUM() will fail for them. */
1496
1497 for (reg = LAST_STACK_REG; reg >= FIRST_STACK_REG; reg--)
1498 if (TEST_HARD_REG_BIT (block_stack_in[0].reg_set, reg))
1499 {
1500 rtx init_rtx;
1501
1502 init_rtx = gen_rtx (SET, VOIDmode, FP_mode_reg[reg][(int) DFmode],
1503 CONST0_RTX (DFmode));
1504 block_begin[0] = emit_insn_after (init_rtx, first);
1505 PUT_MODE (block_begin[0], QImode);
1506
1507 CLEAR_HARD_REG_BIT (block_stack_in[0].reg_set, reg);
1508 }
1509
1510 no_live_regs:
1511 ;
1512 }
1513 \f
1514 /*****************************************************************************
1515 This section deals with stack register substitution, and forms the second
1516 pass over the RTL.
1517 *****************************************************************************/
1518
1519 /* Replace REG, which is a pointer to a stack reg RTX, with an RTX for
1520 the desired hard REGNO. */
1521
1522 static void
1523 replace_reg (reg, regno)
1524 rtx *reg;
1525 int regno;
1526 {
1527 if (regno < FIRST_STACK_REG || regno > LAST_STACK_REG
1528 || ! STACK_REG_P (*reg))
1529 abort ();
1530
1531 if (GET_MODE_CLASS (GET_MODE (*reg)) != MODE_FLOAT)
1532 abort ();
1533
1534 *reg = FP_mode_reg[regno][(int) GET_MODE (*reg)];
1535 }
1536
1537 /* Remove a note of type NOTE, which must be found, for register
1538 number REGNO from INSN. Remove only one such note. */
1539
1540 static void
1541 remove_regno_note (insn, note, regno)
1542 rtx insn;
1543 enum reg_note note;
1544 int regno;
1545 {
1546 register rtx *note_link, this;
1547
1548 note_link = &REG_NOTES(insn);
1549 for (this = *note_link; this; this = XEXP (this, 1))
1550 if (REG_NOTE_KIND (this) == note
1551 && REG_P (XEXP (this, 0)) && REGNO (XEXP (this, 0)) == regno)
1552 {
1553 *note_link = XEXP (this, 1);
1554 return;
1555 }
1556 else
1557 note_link = &XEXP (this, 1);
1558
1559 abort ();
1560 }
1561
1562 /* Find the hard register number of virtual register REG in REGSTACK.
1563 The hard register number is relative to the top of the stack. -1 is
1564 returned if the register is not found. */
1565
1566 static int
1567 get_hard_regnum (regstack, reg)
1568 stack regstack;
1569 rtx reg;
1570 {
1571 int i;
1572
1573 if (! STACK_REG_P (reg))
1574 abort ();
1575
1576 for (i = regstack->top; i >= 0; i--)
1577 if (regstack->reg[i] == REGNO (reg))
1578 break;
1579
1580 return i >= 0 ? (FIRST_STACK_REG + regstack->top - i) : -1;
1581 }
1582
1583 /* Delete INSN from the RTL. Mark the insn, but don't remove it from
1584 the chain of insns. Doing so could confuse block_begin and block_end
1585 if this were the only insn in the block. */
1586
1587 static void
1588 delete_insn_for_stacker (insn)
1589 rtx insn;
1590 {
1591 PUT_CODE (insn, NOTE);
1592 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1593 NOTE_SOURCE_FILE (insn) = 0;
1594 }
1595 \f
1596 /* Emit an insn to pop virtual register REG before or after INSN.
1597 REGSTACK is the stack state after INSN and is updated to reflect this
1598 pop. WHEN is either emit_insn_before or emit_insn_after. A pop insn
1599 is represented as a SET whose destination is the register to be popped
1600 and source is the top of stack. A death note for the top of stack
1601 cases the movdf pattern to pop. */
1602
1603 static rtx
1604 emit_pop_insn (insn, regstack, reg, when)
1605 rtx insn;
1606 stack regstack;
1607 rtx reg;
1608 rtx (*when)();
1609 {
1610 rtx pop_insn, pop_rtx;
1611 int hard_regno;
1612
1613 hard_regno = get_hard_regnum (regstack, reg);
1614
1615 if (hard_regno < FIRST_STACK_REG)
1616 abort ();
1617
1618 pop_rtx = gen_rtx (SET, VOIDmode, FP_mode_reg[hard_regno][(int) DFmode],
1619 FP_mode_reg[FIRST_STACK_REG][(int) DFmode]);
1620
1621 pop_insn = (*when) (pop_rtx, insn);
1622 /* ??? This used to be VOIDmode, but that seems wrong. */
1623 PUT_MODE (pop_insn, QImode);
1624
1625 REG_NOTES (pop_insn) = gen_rtx (EXPR_LIST, REG_DEAD,
1626 FP_mode_reg[FIRST_STACK_REG][(int) DFmode],
1627 REG_NOTES (pop_insn));
1628
1629 regstack->reg[regstack->top - (hard_regno - FIRST_STACK_REG)]
1630 = regstack->reg[regstack->top];
1631 regstack->top -= 1;
1632 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (reg));
1633
1634 return pop_insn;
1635 }
1636 \f
1637 /* Emit an insn before or after INSN to swap virtual register REG with the
1638 top of stack. WHEN should be `emit_insn_before' or `emit_insn_before'
1639 REGSTACK is the stack state before the swap, and is updated to reflect
1640 the swap. A swap insn is represented as a PARALLEL of two patterns:
1641 each pattern moves one reg to the other.
1642
1643 If REG is already at the top of the stack, no insn is emitted. */
1644
1645 static void
1646 emit_swap_insn (insn, regstack, reg)
1647 rtx insn;
1648 stack regstack;
1649 rtx reg;
1650 {
1651 int hard_regno;
1652 rtx gen_swapdf();
1653 rtx swap_rtx, swap_insn;
1654 int tmp, other_reg; /* swap regno temps */
1655 rtx i1; /* the stack-reg insn prior to INSN */
1656 rtx i1set = NULL_RTX; /* the SET rtx within I1 */
1657
1658 hard_regno = get_hard_regnum (regstack, reg);
1659
1660 if (hard_regno < FIRST_STACK_REG)
1661 abort ();
1662 if (hard_regno == FIRST_STACK_REG)
1663 return;
1664
1665 other_reg = regstack->top - (hard_regno - FIRST_STACK_REG);
1666
1667 tmp = regstack->reg[other_reg];
1668 regstack->reg[other_reg] = regstack->reg[regstack->top];
1669 regstack->reg[regstack->top] = tmp;
1670
1671 /* Find the previous insn involving stack regs, but don't go past
1672 any labels, calls or jumps. */
1673 i1 = prev_nonnote_insn (insn);
1674 while (i1 && GET_CODE (i1) == INSN && GET_MODE (i1) != QImode)
1675 i1 = prev_nonnote_insn (i1);
1676
1677 if (i1)
1678 i1set = single_set (i1);
1679
1680 if (i1set)
1681 {
1682 rtx i2; /* the stack-reg insn prior to I1 */
1683 rtx i1src = *get_true_reg (&SET_SRC (i1set));
1684 rtx i1dest = *get_true_reg (&SET_DEST (i1set));
1685
1686 /* If the previous register stack push was from the reg we are to
1687 swap with, omit the swap. */
1688
1689 if (GET_CODE (i1dest) == REG && REGNO (i1dest) == FIRST_STACK_REG
1690 && GET_CODE (i1src) == REG && REGNO (i1src) == hard_regno - 1
1691 && find_regno_note (i1, REG_DEAD, FIRST_STACK_REG) == NULL_RTX)
1692 return;
1693
1694 /* If the previous insn wrote to the reg we are to swap with,
1695 omit the swap. */
1696
1697 if (GET_CODE (i1dest) == REG && REGNO (i1dest) == hard_regno
1698 && GET_CODE (i1src) == REG && REGNO (i1src) == FIRST_STACK_REG
1699 && find_regno_note (i1, REG_DEAD, FIRST_STACK_REG) == NULL_RTX)
1700 return;
1701 }
1702
1703 if (GET_RTX_CLASS (GET_CODE (i1)) == 'i' && sets_cc0_p (PATTERN (i1)))
1704 {
1705 i1 = next_nonnote_insn (i1);
1706 if (i1 == insn)
1707 abort ();
1708 }
1709
1710 swap_rtx = gen_swapdf (FP_mode_reg[hard_regno][(int) DFmode],
1711 FP_mode_reg[FIRST_STACK_REG][(int) DFmode]);
1712 swap_insn = emit_insn_after (swap_rtx, i1);
1713 /* ??? This used to be VOIDmode, but that seems wrong. */
1714 PUT_MODE (swap_insn, QImode);
1715 }
1716 \f
1717 /* Handle a move to or from a stack register in PAT, which is in INSN.
1718 REGSTACK is the current stack. */
1719
1720 static void
1721 move_for_stack_reg (insn, regstack, pat)
1722 rtx insn;
1723 stack regstack;
1724 rtx pat;
1725 {
1726 rtx *src = get_true_reg (&SET_SRC (pat));
1727 rtx *dest = get_true_reg (&SET_DEST (pat));
1728 rtx note;
1729
1730 if (STACK_REG_P (*src) && STACK_REG_P (*dest))
1731 {
1732 /* Write from one stack reg to another. If SRC dies here, then
1733 just change the register mapping and delete the insn. */
1734
1735 note = find_regno_note (insn, REG_DEAD, REGNO (*src));
1736 if (note)
1737 {
1738 int i;
1739
1740 /* If this is a no-op move, there must not be a REG_DEAD note. */
1741 if (REGNO (*src) == REGNO (*dest))
1742 abort ();
1743
1744 for (i = regstack->top; i >= 0; i--)
1745 if (regstack->reg[i] == REGNO (*src))
1746 break;
1747
1748 /* The source must be live, and the dest must be dead. */
1749 if (i < 0 || get_hard_regnum (regstack, *dest) >= FIRST_STACK_REG)
1750 abort ();
1751
1752 /* It is possible that the dest is unused after this insn.
1753 If so, just pop the src. */
1754
1755 if (find_regno_note (insn, REG_UNUSED, REGNO (*dest)))
1756 {
1757 emit_pop_insn (insn, regstack, *src, emit_insn_after);
1758
1759 delete_insn_for_stacker (insn);
1760 return;
1761 }
1762
1763 regstack->reg[i] = REGNO (*dest);
1764
1765 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1766 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src));
1767
1768 delete_insn_for_stacker (insn);
1769
1770 return;
1771 }
1772
1773 /* The source reg does not die. */
1774
1775 /* If this appears to be a no-op move, delete it, or else it
1776 will confuse the machine description output patterns. But if
1777 it is REG_UNUSED, we must pop the reg now, as per-insn processing
1778 for REG_UNUSED will not work for deleted insns. */
1779
1780 if (REGNO (*src) == REGNO (*dest))
1781 {
1782 if (find_regno_note (insn, REG_UNUSED, REGNO (*dest)))
1783 emit_pop_insn (insn, regstack, *dest, emit_insn_after);
1784
1785 delete_insn_for_stacker (insn);
1786 return;
1787 }
1788
1789 /* The destination ought to be dead */
1790 if (get_hard_regnum (regstack, *dest) >= FIRST_STACK_REG)
1791 abort ();
1792
1793 replace_reg (src, get_hard_regnum (regstack, *src));
1794
1795 regstack->reg[++regstack->top] = REGNO (*dest);
1796 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1797 replace_reg (dest, FIRST_STACK_REG);
1798 }
1799 else if (STACK_REG_P (*src))
1800 {
1801 /* Save from a stack reg to MEM, or possibly integer reg. Since
1802 only top of stack may be saved, emit an exchange first if
1803 needs be. */
1804
1805 emit_swap_insn (insn, regstack, *src);
1806
1807 note = find_regno_note (insn, REG_DEAD, REGNO (*src));
1808 if (note)
1809 {
1810 replace_reg (&XEXP (note, 0), FIRST_STACK_REG);
1811 regstack->top--;
1812 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src));
1813 }
1814 else if (GET_MODE (*src) == XFmode && regstack->top != REG_STACK_SIZE)
1815 {
1816 /* A 387 cannot write an XFmode value to a MEM without
1817 clobbering the source reg. The output code can handle
1818 this by reading back the value from the MEM.
1819 But it is more efficient to use a temp register if one is
1820 available. Push the source value here if the register
1821 stack is not full, and then write the value to memory via
1822 a pop. */
1823 rtx push_rtx, push_insn;
1824 rtx top_stack_reg = FP_mode_reg[FIRST_STACK_REG][(int) XFmode];
1825
1826 push_rtx = gen_movxf (top_stack_reg, top_stack_reg);
1827 push_insn = emit_insn_before (push_rtx, insn);
1828 PUT_MODE (push_insn, QImode);
1829 REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_DEAD, top_stack_reg,
1830 REG_NOTES (insn));
1831 }
1832
1833 replace_reg (src, FIRST_STACK_REG);
1834 }
1835 else if (STACK_REG_P (*dest))
1836 {
1837 /* Load from MEM, or possibly integer REG or constant, into the
1838 stack regs. The actual target is always the top of the
1839 stack. The stack mapping is changed to reflect that DEST is
1840 now at top of stack. */
1841
1842 /* The destination ought to be dead */
1843 if (get_hard_regnum (regstack, *dest) >= FIRST_STACK_REG)
1844 abort ();
1845
1846 if (regstack->top >= REG_STACK_SIZE)
1847 abort ();
1848
1849 regstack->reg[++regstack->top] = REGNO (*dest);
1850 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
1851 replace_reg (dest, FIRST_STACK_REG);
1852 }
1853 else
1854 abort ();
1855 }
1856 \f
1857 void
1858 swap_rtx_condition (pat)
1859 rtx pat;
1860 {
1861 register char *fmt;
1862 register int i;
1863
1864 if (GET_RTX_CLASS (GET_CODE (pat)) == '<')
1865 {
1866 PUT_CODE (pat, swap_condition (GET_CODE (pat)));
1867 return;
1868 }
1869
1870 fmt = GET_RTX_FORMAT (GET_CODE (pat));
1871 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
1872 {
1873 if (fmt[i] == 'E')
1874 {
1875 register int j;
1876
1877 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
1878 swap_rtx_condition (XVECEXP (pat, i, j));
1879 }
1880 else if (fmt[i] == 'e')
1881 swap_rtx_condition (XEXP (pat, i));
1882 }
1883 }
1884
1885 /* Handle a comparison. Special care needs to be taken to avoid
1886 causing comparisons that a 387 cannot do correctly, such as EQ.
1887
1888 Also, a pop insn may need to be emitted. The 387 does have an
1889 `fcompp' insn that can pop two regs, but it is sometimes too expensive
1890 to do this - a `fcomp' followed by a `fstpl %st(0)' may be easier to
1891 set up. */
1892
1893 static void
1894 compare_for_stack_reg (insn, regstack, pat)
1895 rtx insn;
1896 stack regstack;
1897 rtx pat;
1898 {
1899 rtx *src1, *src2;
1900 rtx src1_note, src2_note;
1901
1902 src1 = get_true_reg (&XEXP (SET_SRC (pat), 0));
1903 src2 = get_true_reg (&XEXP (SET_SRC (pat), 1));
1904
1905 /* ??? If fxch turns out to be cheaper than fstp, give priority to
1906 registers that die in this insn - move those to stack top first. */
1907 if (! STACK_REG_P (*src1)
1908 || (STACK_REG_P (*src2)
1909 && get_hard_regnum (regstack, *src2) == FIRST_STACK_REG))
1910 {
1911 rtx temp, next;
1912
1913 temp = XEXP (SET_SRC (pat), 0);
1914 XEXP (SET_SRC (pat), 0) = XEXP (SET_SRC (pat), 1);
1915 XEXP (SET_SRC (pat), 1) = temp;
1916
1917 src1 = get_true_reg (&XEXP (SET_SRC (pat), 0));
1918 src2 = get_true_reg (&XEXP (SET_SRC (pat), 1));
1919
1920 next = next_cc0_user (insn);
1921 if (next == NULL_RTX)
1922 abort ();
1923
1924 swap_rtx_condition (PATTERN (next));
1925 INSN_CODE (next) = -1;
1926 INSN_CODE (insn) = -1;
1927 }
1928
1929 /* We will fix any death note later. */
1930
1931 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
1932
1933 if (STACK_REG_P (*src2))
1934 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
1935 else
1936 src2_note = NULL_RTX;
1937
1938 emit_swap_insn (insn, regstack, *src1);
1939
1940 replace_reg (src1, FIRST_STACK_REG);
1941
1942 if (STACK_REG_P (*src2))
1943 replace_reg (src2, get_hard_regnum (regstack, *src2));
1944
1945 if (src1_note)
1946 {
1947 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (XEXP (src1_note, 0)));
1948 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
1949 regstack->top--;
1950 }
1951
1952 /* If the second operand dies, handle that. But if the operands are
1953 the same stack register, don't bother, because only one death is
1954 needed, and it was just handled. */
1955
1956 if (src2_note
1957 && ! (STACK_REG_P (*src1) && STACK_REG_P (*src2)
1958 && REGNO (*src1) == REGNO (*src2)))
1959 {
1960 /* As a special case, two regs may die in this insn if src2 is
1961 next to top of stack and the top of stack also dies. Since
1962 we have already popped src1, "next to top of stack" is really
1963 at top (FIRST_STACK_REG) now. */
1964
1965 if (get_hard_regnum (regstack, XEXP (src2_note, 0)) == FIRST_STACK_REG
1966 && src1_note)
1967 {
1968 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (XEXP (src2_note, 0)));
1969 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG + 1);
1970 regstack->top--;
1971 }
1972 else
1973 {
1974 /* The 386 can only represent death of the first operand in
1975 the case handled above. In all other cases, emit a separate
1976 pop and remove the death note from here. */
1977
1978 link_cc0_insns (insn);
1979
1980 remove_regno_note (insn, REG_DEAD, REGNO (XEXP (src2_note, 0)));
1981
1982 emit_pop_insn (insn, regstack, XEXP (src2_note, 0),
1983 emit_insn_after);
1984 }
1985 }
1986 }
1987 \f
1988 /* Substitute new registers in PAT, which is part of INSN. REGSTACK
1989 is the current register layout. */
1990
1991 static void
1992 subst_stack_regs_pat (insn, regstack, pat)
1993 rtx insn;
1994 stack regstack;
1995 rtx pat;
1996 {
1997 rtx *dest, *src;
1998 rtx *src1 = (rtx *) NULL_PTR, *src2;
1999 rtx src1_note, src2_note;
2000
2001 if (GET_CODE (pat) != SET)
2002 return;
2003
2004 dest = get_true_reg (&SET_DEST (pat));
2005 src = get_true_reg (&SET_SRC (pat));
2006
2007 /* See if this is a `movM' pattern, and handle elsewhere if so. */
2008
2009 if (*dest != cc0_rtx
2010 && (STACK_REG_P (*src)
2011 || (STACK_REG_P (*dest)
2012 && (GET_CODE (*src) == REG || GET_CODE (*src) == MEM
2013 || GET_CODE (*src) == CONST_DOUBLE))))
2014 move_for_stack_reg (insn, regstack, pat);
2015 else
2016 switch (GET_CODE (SET_SRC (pat)))
2017 {
2018 case COMPARE:
2019 compare_for_stack_reg (insn, regstack, pat);
2020 break;
2021
2022 case CALL:
2023 regstack->reg[++regstack->top] = REGNO (*dest);
2024 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
2025 replace_reg (dest, FIRST_STACK_REG);
2026 break;
2027
2028 case REG:
2029 /* This is a `tstM2' case. */
2030 if (*dest != cc0_rtx)
2031 abort ();
2032
2033 src1 = src;
2034
2035 /* Fall through. */
2036
2037 case FLOAT_TRUNCATE:
2038 case SQRT:
2039 case ABS:
2040 case NEG:
2041 /* These insns only operate on the top of the stack. DEST might
2042 be cc0_rtx if we're processing a tstM pattern. Also, it's
2043 possible that the tstM case results in a REG_DEAD note on the
2044 source. */
2045
2046 if (src1 == 0)
2047 src1 = get_true_reg (&XEXP (SET_SRC (pat), 0));
2048
2049 emit_swap_insn (insn, regstack, *src1);
2050
2051 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
2052
2053 if (STACK_REG_P (*dest))
2054 replace_reg (dest, FIRST_STACK_REG);
2055
2056 if (src1_note)
2057 {
2058 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
2059 regstack->top--;
2060 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src1));
2061 }
2062
2063 replace_reg (src1, FIRST_STACK_REG);
2064
2065 break;
2066
2067 case MINUS:
2068 case DIV:
2069 /* On i386, reversed forms of subM3 and divM3 exist for
2070 MODE_FLOAT, so the same code that works for addM3 and mulM3
2071 can be used. */
2072 case MULT:
2073 case PLUS:
2074 /* These insns can accept the top of stack as a destination
2075 from a stack reg or mem, or can use the top of stack as a
2076 source and some other stack register (possibly top of stack)
2077 as a destination. */
2078
2079 src1 = get_true_reg (&XEXP (SET_SRC (pat), 0));
2080 src2 = get_true_reg (&XEXP (SET_SRC (pat), 1));
2081
2082 /* We will fix any death note later. */
2083
2084 if (STACK_REG_P (*src1))
2085 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
2086 else
2087 src1_note = NULL_RTX;
2088 if (STACK_REG_P (*src2))
2089 src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
2090 else
2091 src2_note = NULL_RTX;
2092
2093 /* If either operand is not a stack register, then the dest
2094 must be top of stack. */
2095
2096 if (! STACK_REG_P (*src1) || ! STACK_REG_P (*src2))
2097 emit_swap_insn (insn, regstack, *dest);
2098 else
2099 {
2100 /* Both operands are REG. If neither operand is already
2101 at the top of stack, choose to make the one that is the dest
2102 the new top of stack. */
2103
2104 int src1_hard_regnum, src2_hard_regnum;
2105
2106 src1_hard_regnum = get_hard_regnum (regstack, *src1);
2107 src2_hard_regnum = get_hard_regnum (regstack, *src2);
2108 if (src1_hard_regnum == -1 || src2_hard_regnum == -1)
2109 abort ();
2110
2111 if (src1_hard_regnum != FIRST_STACK_REG
2112 && src2_hard_regnum != FIRST_STACK_REG)
2113 emit_swap_insn (insn, regstack, *dest);
2114 }
2115
2116 if (STACK_REG_P (*src1))
2117 replace_reg (src1, get_hard_regnum (regstack, *src1));
2118 if (STACK_REG_P (*src2))
2119 replace_reg (src2, get_hard_regnum (regstack, *src2));
2120
2121 if (src1_note)
2122 {
2123 /* If the register that dies is at the top of stack, then
2124 the destination is somewhere else - merely substitute it.
2125 But if the reg that dies is not at top of stack, then
2126 move the top of stack to the dead reg, as though we had
2127 done the insn and then a store-with-pop. */
2128
2129 if (REGNO (XEXP (src1_note, 0)) == regstack->reg[regstack->top])
2130 {
2131 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
2132 replace_reg (dest, get_hard_regnum (regstack, *dest));
2133 }
2134 else
2135 {
2136 int regno = get_hard_regnum (regstack, XEXP (src1_note, 0));
2137
2138 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
2139 replace_reg (dest, regno);
2140
2141 regstack->reg[regstack->top - (regno - FIRST_STACK_REG)]
2142 = regstack->reg[regstack->top];
2143 }
2144
2145 CLEAR_HARD_REG_BIT (regstack->reg_set,
2146 REGNO (XEXP (src1_note, 0)));
2147 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
2148 regstack->top--;
2149 }
2150 else if (src2_note)
2151 {
2152 if (REGNO (XEXP (src2_note, 0)) == regstack->reg[regstack->top])
2153 {
2154 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
2155 replace_reg (dest, get_hard_regnum (regstack, *dest));
2156 }
2157 else
2158 {
2159 int regno = get_hard_regnum (regstack, XEXP (src2_note, 0));
2160
2161 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
2162 replace_reg (dest, regno);
2163
2164 regstack->reg[regstack->top - (regno - FIRST_STACK_REG)]
2165 = regstack->reg[regstack->top];
2166 }
2167
2168 CLEAR_HARD_REG_BIT (regstack->reg_set,
2169 REGNO (XEXP (src2_note, 0)));
2170 replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG);
2171 regstack->top--;
2172 }
2173 else
2174 {
2175 SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
2176 replace_reg (dest, get_hard_regnum (regstack, *dest));
2177 }
2178
2179 break;
2180
2181 case UNSPEC:
2182 switch (XINT (SET_SRC (pat), 1))
2183 {
2184 case 1: /* sin */
2185 case 2: /* cos */
2186 /* These insns only operate on the top of the stack. */
2187
2188 src1 = get_true_reg (&XVECEXP (SET_SRC (pat), 0, 0));
2189
2190 emit_swap_insn (insn, regstack, *src1);
2191
2192 src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
2193
2194 if (STACK_REG_P (*dest))
2195 replace_reg (dest, FIRST_STACK_REG);
2196
2197 if (src1_note)
2198 {
2199 replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
2200 regstack->top--;
2201 CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src1));
2202 }
2203
2204 replace_reg (src1, FIRST_STACK_REG);
2205
2206 break;
2207
2208 default:
2209 abort ();
2210 }
2211 break;
2212
2213 default:
2214 abort ();
2215 }
2216 }
2217 \f
2218 /* Substitute hard regnums for any stack regs in INSN, which has
2219 N_INPUTS inputs and N_OUTPUTS outputs. REGSTACK is the stack info
2220 before the insn, and is updated with changes made here. CONSTRAINTS is
2221 an array of the constraint strings used in the asm statement.
2222
2223 OPERANDS is an array of the operands, and OPERANDS_LOC is a
2224 parallel array of where the operands were found. The output operands
2225 all precede the input operands.
2226
2227 There are several requirements and assumptions about the use of
2228 stack-like regs in asm statements. These rules are enforced by
2229 record_asm_stack_regs; see comments there for details. Any
2230 asm_operands left in the RTL at this point may be assume to meet the
2231 requirements, since record_asm_stack_regs removes any problem asm. */
2232
2233 static void
2234 subst_asm_stack_regs (insn, regstack, operands, operands_loc, constraints,
2235 n_inputs, n_outputs)
2236 rtx insn;
2237 stack regstack;
2238 rtx *operands, **operands_loc;
2239 char **constraints;
2240 int n_inputs, n_outputs;
2241 {
2242 int n_operands = n_inputs + n_outputs;
2243 int first_input = n_outputs;
2244 rtx body = PATTERN (insn);
2245
2246 int *operand_matches = (int *) alloca (n_operands * sizeof (int *));
2247 enum reg_class *operand_class
2248 = (enum reg_class *) alloca (n_operands * sizeof (enum reg_class *));
2249
2250 rtx *note_reg; /* Array of note contents */
2251 rtx **note_loc; /* Address of REG field of each note */
2252 enum reg_note *note_kind; /* The type of each note */
2253
2254 rtx *clobber_reg;
2255 rtx **clobber_loc;
2256
2257 struct stack_def temp_stack;
2258 int n_notes;
2259 int n_clobbers;
2260 rtx note;
2261 int i;
2262
2263 /* Find out what the constraints required. If no constraint
2264 alternative matches, that is a compiler bug: we should have caught
2265 such an insn during the life analysis pass (and reload should have
2266 caught it regardless). */
2267
2268 i = constrain_asm_operands (n_operands, operands, constraints,
2269 operand_matches, operand_class);
2270 if (i < 0)
2271 abort ();
2272
2273 /* Strip SUBREGs here to make the following code simpler. */
2274 for (i = 0; i < n_operands; i++)
2275 if (GET_CODE (operands[i]) == SUBREG
2276 && GET_CODE (SUBREG_REG (operands[i])) == REG)
2277 {
2278 operands_loc[i] = & SUBREG_REG (operands[i]);
2279 operands[i] = SUBREG_REG (operands[i]);
2280 }
2281
2282 /* Set up NOTE_REG, NOTE_LOC and NOTE_KIND. */
2283
2284 for (i = 0, note = REG_NOTES (insn); note; note = XEXP (note, 1))
2285 i++;
2286
2287 note_reg = (rtx *) alloca (i * sizeof (rtx));
2288 note_loc = (rtx **) alloca (i * sizeof (rtx *));
2289 note_kind = (enum reg_note *) alloca (i * sizeof (enum reg_note));
2290
2291 n_notes = 0;
2292 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2293 {
2294 rtx reg = XEXP (note, 0);
2295 rtx *loc = & XEXP (note, 0);
2296
2297 if (GET_CODE (reg) == SUBREG && GET_CODE (SUBREG_REG (reg)) == REG)
2298 {
2299 loc = & SUBREG_REG (reg);
2300 reg = SUBREG_REG (reg);
2301 }
2302
2303 if (STACK_REG_P (reg)
2304 && (REG_NOTE_KIND (note) == REG_DEAD
2305 || REG_NOTE_KIND (note) == REG_UNUSED))
2306 {
2307 note_reg[n_notes] = reg;
2308 note_loc[n_notes] = loc;
2309 note_kind[n_notes] = REG_NOTE_KIND (note);
2310 n_notes++;
2311 }
2312 }
2313
2314 /* Set up CLOBBER_REG and CLOBBER_LOC. */
2315
2316 n_clobbers = 0;
2317
2318 if (GET_CODE (body) == PARALLEL)
2319 {
2320 clobber_reg = (rtx *) alloca (XVECLEN (body, 0) * sizeof (rtx *));
2321 clobber_loc = (rtx **) alloca (XVECLEN (body, 0) * sizeof (rtx **));
2322
2323 for (i = 0; i < XVECLEN (body, 0); i++)
2324 if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
2325 {
2326 rtx clobber = XVECEXP (body, 0, i);
2327 rtx reg = XEXP (clobber, 0);
2328 rtx *loc = & XEXP (clobber, 0);
2329
2330 if (GET_CODE (reg) == SUBREG && GET_CODE (SUBREG_REG (reg)) == REG)
2331 {
2332 loc = & SUBREG_REG (reg);
2333 reg = SUBREG_REG (reg);
2334 }
2335
2336 if (STACK_REG_P (reg))
2337 {
2338 clobber_reg[n_clobbers] = reg;
2339 clobber_loc[n_clobbers] = loc;
2340 n_clobbers++;
2341 }
2342 }
2343 }
2344
2345 bcopy (regstack, &temp_stack, sizeof (temp_stack));
2346
2347 /* Put the input regs into the desired place in TEMP_STACK. */
2348
2349 for (i = first_input; i < first_input + n_inputs; i++)
2350 if (STACK_REG_P (operands[i])
2351 && reg_class_subset_p (operand_class[i], FLOAT_REGS)
2352 && operand_class[i] != FLOAT_REGS)
2353 {
2354 /* If an operand needs to be in a particular reg in
2355 FLOAT_REGS, the constraint was either 't' or 'u'. Since
2356 these constraints are for single register classes, and reload
2357 guaranteed that operand[i] is already in that class, we can
2358 just use REGNO (operands[i]) to know which actual reg this
2359 operand needs to be in. */
2360
2361 int regno = get_hard_regnum (&temp_stack, operands[i]);
2362
2363 if (regno < 0)
2364 abort ();
2365
2366 if (regno != REGNO (operands[i]))
2367 {
2368 /* operands[i] is not in the right place. Find it
2369 and swap it with whatever is already in I's place.
2370 K is where operands[i] is now. J is where it should
2371 be. */
2372 int j, k, temp;
2373
2374 k = temp_stack.top - (regno - FIRST_STACK_REG);
2375 j = (temp_stack.top
2376 - (REGNO (operands[i]) - FIRST_STACK_REG));
2377
2378 temp = temp_stack.reg[k];
2379 temp_stack.reg[k] = temp_stack.reg[j];
2380 temp_stack.reg[j] = temp;
2381 }
2382 }
2383
2384 /* emit insns before INSN to make sure the reg-stack is in the right
2385 order. */
2386
2387 change_stack (insn, regstack, &temp_stack, emit_insn_before);
2388
2389 /* Make the needed input register substitutions. Do death notes and
2390 clobbers too, because these are for inputs, not outputs. */
2391
2392 for (i = first_input; i < first_input + n_inputs; i++)
2393 if (STACK_REG_P (operands[i]))
2394 {
2395 int regnum = get_hard_regnum (regstack, operands[i]);
2396
2397 if (regnum < 0)
2398 abort ();
2399
2400 replace_reg (operands_loc[i], regnum);
2401 }
2402
2403 for (i = 0; i < n_notes; i++)
2404 if (note_kind[i] == REG_DEAD)
2405 {
2406 int regnum = get_hard_regnum (regstack, note_reg[i]);
2407
2408 if (regnum < 0)
2409 abort ();
2410
2411 replace_reg (note_loc[i], regnum);
2412 }
2413
2414 for (i = 0; i < n_clobbers; i++)
2415 {
2416 /* It's OK for a CLOBBER to reference a reg that is not live.
2417 Don't try to replace it in that case. */
2418 int regnum = get_hard_regnum (regstack, clobber_reg[i]);
2419
2420 if (regnum >= 0)
2421 {
2422 /* Sigh - clobbers always have QImode. But replace_reg knows
2423 that these regs can't be MODE_INT and will abort. Just put
2424 the right reg there without calling replace_reg. */
2425
2426 *clobber_loc[i] = FP_mode_reg[regnum][(int) DFmode];
2427 }
2428 }
2429
2430 /* Now remove from REGSTACK any inputs that the asm implicitly popped. */
2431
2432 for (i = first_input; i < first_input + n_inputs; i++)
2433 if (STACK_REG_P (operands[i]))
2434 {
2435 /* An input reg is implicitly popped if it is tied to an
2436 output, or if there is a CLOBBER for it. */
2437 int j;
2438
2439 for (j = 0; j < n_clobbers; j++)
2440 if (operands_match_p (clobber_reg[j], operands[i]))
2441 break;
2442
2443 if (j < n_clobbers || operand_matches[i] >= 0)
2444 {
2445 /* operands[i] might not be at the top of stack. But that's OK,
2446 because all we need to do is pop the right number of regs
2447 off of the top of the reg-stack. record_asm_stack_regs
2448 guaranteed that all implicitly popped regs were grouped
2449 at the top of the reg-stack. */
2450
2451 CLEAR_HARD_REG_BIT (regstack->reg_set,
2452 regstack->reg[regstack->top]);
2453 regstack->top--;
2454 }
2455 }
2456
2457 /* Now add to REGSTACK any outputs that the asm implicitly pushed.
2458 Note that there isn't any need to substitute register numbers.
2459 ??? Explain why this is true. */
2460
2461 for (i = LAST_STACK_REG; i >= FIRST_STACK_REG; i--)
2462 {
2463 /* See if there is an output for this hard reg. */
2464 int j;
2465
2466 for (j = 0; j < n_outputs; j++)
2467 if (STACK_REG_P (operands[j]) && REGNO (operands[j]) == i)
2468 {
2469 regstack->reg[++regstack->top] = i;
2470 SET_HARD_REG_BIT (regstack->reg_set, i);
2471 break;
2472 }
2473 }
2474
2475 /* Now emit a pop insn for any REG_UNUSED output, or any REG_DEAD
2476 input that the asm didn't implicitly pop. If the asm didn't
2477 implicitly pop an input reg, that reg will still be live.
2478
2479 Note that we can't use find_regno_note here: the register numbers
2480 in the death notes have already been substituted. */
2481
2482 for (i = 0; i < n_outputs; i++)
2483 if (STACK_REG_P (operands[i]))
2484 {
2485 int j;
2486
2487 for (j = 0; j < n_notes; j++)
2488 if (REGNO (operands[i]) == REGNO (note_reg[j])
2489 && note_kind[j] == REG_UNUSED)
2490 {
2491 insn = emit_pop_insn (insn, regstack, operands[i],
2492 emit_insn_after);
2493 break;
2494 }
2495 }
2496
2497 for (i = first_input; i < first_input + n_inputs; i++)
2498 if (STACK_REG_P (operands[i]))
2499 {
2500 int j;
2501
2502 for (j = 0; j < n_notes; j++)
2503 if (REGNO (operands[i]) == REGNO (note_reg[j])
2504 && note_kind[j] == REG_DEAD
2505 && TEST_HARD_REG_BIT (regstack->reg_set, REGNO (operands[i])))
2506 {
2507 insn = emit_pop_insn (insn, regstack, operands[i],
2508 emit_insn_after);
2509 break;
2510 }
2511 }
2512 }
2513 \f
2514 /* Substitute stack hard reg numbers for stack virtual registers in
2515 INSN. Non-stack register numbers are not changed. REGSTACK is the
2516 current stack content. Insns may be emitted as needed to arrange the
2517 stack for the 387 based on the contents of the insn. */
2518
2519 static void
2520 subst_stack_regs (insn, regstack)
2521 rtx insn;
2522 stack regstack;
2523 {
2524 register rtx *note_link, note;
2525 register int i;
2526 int n_operands;
2527
2528 if ((GET_CODE (insn) != INSN && GET_CODE (insn) != CALL_INSN)
2529 || INSN_DELETED_P (insn))
2530 return;
2531
2532 /* The stack should be empty at a call. */
2533
2534 if (GET_CODE (insn) == CALL_INSN)
2535 for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
2536 if (TEST_HARD_REG_BIT (regstack->reg_set, i))
2537 abort ();
2538
2539 /* Do the actual substitution if any stack regs are mentioned.
2540 Since we only record whether entire insn mentions stack regs, and
2541 subst_stack_regs_pat only works for patterns that contain stack regs,
2542 we must check each pattern in a parallel here. A call_value_pop could
2543 fail otherwise. */
2544
2545 if (GET_MODE (insn) == QImode)
2546 {
2547 n_operands = asm_noperands (PATTERN (insn));
2548 if (n_operands >= 0)
2549 {
2550 /* This insn is an `asm' with operands. Decode the operands,
2551 decide how many are inputs, and do register substitution.
2552 Any REG_UNUSED notes will be handled by subst_asm_stack_regs. */
2553
2554 rtx operands[MAX_RECOG_OPERANDS];
2555 rtx *operands_loc[MAX_RECOG_OPERANDS];
2556 rtx body = PATTERN (insn);
2557 int n_inputs, n_outputs;
2558 char **constraints
2559 = (char **) alloca (n_operands * sizeof (char *));
2560
2561 decode_asm_operands (body, operands, operands_loc,
2562 constraints, NULL_PTR);
2563 get_asm_operand_lengths (body, n_operands, &n_inputs, &n_outputs);
2564 subst_asm_stack_regs (insn, regstack, operands, operands_loc,
2565 constraints, n_inputs, n_outputs);
2566 return;
2567 }
2568
2569 if (GET_CODE (PATTERN (insn)) == PARALLEL)
2570 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
2571 {
2572 if (stack_regs_mentioned_p (XVECEXP (PATTERN (insn), 0, i)))
2573 subst_stack_regs_pat (insn, regstack,
2574 XVECEXP (PATTERN (insn), 0, i));
2575 }
2576 else
2577 subst_stack_regs_pat (insn, regstack, PATTERN (insn));
2578 }
2579
2580 /* subst_stack_regs_pat may have deleted a no-op insn. If so, any
2581 REG_UNUSED will already have been dealt with, so just return. */
2582
2583 if (INSN_DELETED_P (insn))
2584 return;
2585
2586 /* If there is a REG_UNUSED note on a stack register on this insn,
2587 the indicated reg must be popped. The REG_UNUSED note is removed,
2588 since the form of the newly emitted pop insn references the reg,
2589 making it no longer `unset'. */
2590
2591 note_link = &REG_NOTES(insn);
2592 for (note = *note_link; note; note = XEXP (note, 1))
2593 if (REG_NOTE_KIND (note) == REG_UNUSED && STACK_REG_P (XEXP (note, 0)))
2594 {
2595 *note_link = XEXP (note, 1);
2596 insn = emit_pop_insn (insn, regstack, XEXP (note, 0), emit_insn_after);
2597 }
2598 else
2599 note_link = &XEXP (note, 1);
2600 }
2601 \f
2602 /* Change the organization of the stack so that it fits a new basic
2603 block. Some registers might have to be popped, but there can never be
2604 a register live in the new block that is not now live.
2605
2606 Insert any needed insns before or after INSN. WHEN is emit_insn_before
2607 or emit_insn_after. OLD is the original stack layout, and NEW is
2608 the desired form. OLD is updated to reflect the code emitted, ie, it
2609 will be the same as NEW upon return.
2610
2611 This function will not preserve block_end[]. But that information
2612 is no longer needed once this has executed. */
2613
2614 static void
2615 change_stack (insn, old, new, when)
2616 rtx insn;
2617 stack old;
2618 stack new;
2619 rtx (*when)();
2620 {
2621 int reg;
2622
2623 /* We will be inserting new insns "backwards", by calling emit_insn_before.
2624 If we are to insert after INSN, find the next insn, and insert before
2625 it. */
2626
2627 if (when == emit_insn_after)
2628 insn = NEXT_INSN (insn);
2629
2630 /* Pop any registers that are not needed in the new block. */
2631
2632 for (reg = old->top; reg >= 0; reg--)
2633 if (! TEST_HARD_REG_BIT (new->reg_set, old->reg[reg]))
2634 emit_pop_insn (insn, old, FP_mode_reg[old->reg[reg]][(int) DFmode],
2635 emit_insn_before);
2636
2637 if (new->top == -2)
2638 {
2639 /* If the new block has never been processed, then it can inherit
2640 the old stack order. */
2641
2642 new->top = old->top;
2643 bcopy (old->reg, new->reg, sizeof (new->reg));
2644 }
2645 else
2646 {
2647 /* This block has been entered before, and we must match the
2648 previously selected stack order. */
2649
2650 /* By now, the only difference should be the order of the stack,
2651 not their depth or liveliness. */
2652
2653 GO_IF_HARD_REG_EQUAL (old->reg_set, new->reg_set, win);
2654
2655 abort ();
2656
2657 win:
2658
2659 if (old->top != new->top)
2660 abort ();
2661
2662 /* Loop here emitting swaps until the stack is correct. The
2663 worst case number of swaps emitted is N + 2, where N is the
2664 depth of the stack. In some cases, the reg at the top of
2665 stack may be correct, but swapped anyway in order to fix
2666 other regs. But since we never swap any other reg away from
2667 its correct slot, this algorithm will converge. */
2668
2669 do
2670 {
2671 /* Swap the reg at top of stack into the position it is
2672 supposed to be in, until the correct top of stack appears. */
2673
2674 while (old->reg[old->top] != new->reg[new->top])
2675 {
2676 for (reg = new->top; reg >= 0; reg--)
2677 if (new->reg[reg] == old->reg[old->top])
2678 break;
2679
2680 if (reg == -1)
2681 abort ();
2682
2683 emit_swap_insn (insn, old,
2684 FP_mode_reg[old->reg[reg]][(int) DFmode]);
2685 }
2686
2687 /* See if any regs remain incorrect. If so, bring an
2688 incorrect reg to the top of stack, and let the while loop
2689 above fix it. */
2690
2691 for (reg = new->top; reg >= 0; reg--)
2692 if (new->reg[reg] != old->reg[reg])
2693 {
2694 emit_swap_insn (insn, old,
2695 FP_mode_reg[old->reg[reg]][(int) DFmode]);
2696 break;
2697 }
2698 } while (reg >= 0);
2699
2700 /* At this point there must be no differences. */
2701
2702 for (reg = old->top; reg >= 0; reg--)
2703 if (old->reg[reg] != new->reg[reg])
2704 abort ();
2705 }
2706 }
2707 \f
2708 /* Check PAT, which points to RTL in INSN, for a LABEL_REF. If it is
2709 found, ensure that a jump from INSN to the code_label to which the
2710 label_ref points ends up with the same stack as that at the
2711 code_label. Do this by inserting insns just before the code_label to
2712 pop and rotate the stack until it is in the correct order. REGSTACK
2713 is the order of the register stack in INSN.
2714
2715 Any code that is emitted here must not be later processed as part
2716 of any block, as it will already contain hard register numbers. */
2717
2718 static void
2719 goto_block_pat (insn, regstack, pat)
2720 rtx insn;
2721 stack regstack;
2722 rtx pat;
2723 {
2724 rtx label;
2725 rtx new_jump, new_label, new_barrier;
2726 rtx *ref;
2727 stack label_stack;
2728 struct stack_def temp_stack;
2729 int reg;
2730
2731 if (GET_CODE (pat) != LABEL_REF)
2732 {
2733 int i, j;
2734 char *fmt = GET_RTX_FORMAT (GET_CODE (pat));
2735
2736 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
2737 {
2738 if (fmt[i] == 'e')
2739 goto_block_pat (insn, regstack, XEXP (pat, i));
2740 if (fmt[i] == 'E')
2741 for (j = 0; j < XVECLEN (pat, i); j++)
2742 goto_block_pat (insn, regstack, XVECEXP (pat, i, j));
2743 }
2744 return;
2745 }
2746
2747 label = XEXP (pat, 0);
2748 if (GET_CODE (label) != CODE_LABEL)
2749 abort ();
2750
2751 /* First, see if in fact anything needs to be done to the stack at all. */
2752 if (INSN_UID (label) <= 0)
2753 return;
2754
2755 label_stack = &block_stack_in[BLOCK_NUM (label)];
2756
2757 if (label_stack->top == -2)
2758 {
2759 /* If the target block hasn't had a stack order selected, then
2760 we need merely ensure that no pops are needed. */
2761
2762 for (reg = regstack->top; reg >= 0; reg--)
2763 if (! TEST_HARD_REG_BIT (label_stack->reg_set, regstack->reg[reg]))
2764 break;
2765
2766 if (reg == -1)
2767 {
2768 /* change_stack will not emit any code in this case. */
2769
2770 change_stack (label, regstack, label_stack, emit_insn_after);
2771 return;
2772 }
2773 }
2774 else if (label_stack->top == regstack->top)
2775 {
2776 for (reg = label_stack->top; reg >= 0; reg--)
2777 if (label_stack->reg[reg] != regstack->reg[reg])
2778 break;
2779
2780 if (reg == -1)
2781 return;
2782 }
2783
2784 /* At least one insn will need to be inserted before label. Insert
2785 a jump around the code we are about to emit. Emit a label for the new
2786 code, and point the original insn at this new label. We can't use
2787 redirect_jump here, because we're using fld[4] of the code labels as
2788 LABEL_REF chains, no NUSES counters. */
2789
2790 new_jump = emit_jump_insn_before (gen_jump (label), label);
2791 record_label_references (new_jump, PATTERN (new_jump));
2792 JUMP_LABEL (new_jump) = label;
2793
2794 new_barrier = emit_barrier_after (new_jump);
2795
2796 new_label = gen_label_rtx ();
2797 emit_label_after (new_label, new_barrier);
2798 LABEL_REFS (new_label) = new_label;
2799
2800 /* The old label_ref will no longer point to the code_label if now uses,
2801 so strip the label_ref from the code_label's chain of references. */
2802
2803 for (ref = &LABEL_REFS (label); *ref != label; ref = &LABEL_NEXTREF (*ref))
2804 if (*ref == pat)
2805 break;
2806
2807 if (*ref == label)
2808 abort ();
2809
2810 *ref = LABEL_NEXTREF (*ref);
2811
2812 XEXP (pat, 0) = new_label;
2813 record_label_references (insn, PATTERN (insn));
2814
2815 if (JUMP_LABEL (insn) == label)
2816 JUMP_LABEL (insn) = new_label;
2817
2818 /* Now emit the needed code. */
2819
2820 temp_stack = *regstack;
2821
2822 change_stack (new_label, &temp_stack, label_stack, emit_insn_after);
2823 }
2824 \f
2825 /* Traverse all basic blocks in a function, converting the register
2826 references in each insn from the "flat" register file that gcc uses, to
2827 the stack-like registers the 387 uses. */
2828
2829 static void
2830 convert_regs ()
2831 {
2832 register int block, reg;
2833 register rtx insn, next;
2834 struct stack_def regstack;
2835
2836 for (block = 0; block < blocks; block++)
2837 {
2838 if (block_stack_in[block].top == -2)
2839 {
2840 /* This block has not been previously encountered. Choose a
2841 default mapping for any stack regs live on entry */
2842
2843 block_stack_in[block].top = -1;
2844
2845 for (reg = LAST_STACK_REG; reg >= FIRST_STACK_REG; reg--)
2846 if (TEST_HARD_REG_BIT (block_stack_in[block].reg_set, reg))
2847 block_stack_in[block].reg[++block_stack_in[block].top] = reg;
2848 }
2849
2850 /* Process all insns in this block. Keep track of `next' here,
2851 so that we don't process any insns emitted while making
2852 substitutions in INSN. */
2853
2854 next = block_begin[block];
2855 regstack = block_stack_in[block];
2856 do
2857 {
2858 insn = next;
2859 next = NEXT_INSN (insn);
2860
2861 /* Don't bother processing unless there is a stack reg
2862 mentioned.
2863
2864 ??? For now, process CALL_INSNs too to make sure that the
2865 stack regs are dead after a call. Remove this eventually. */
2866
2867 if (GET_MODE (insn) == QImode || GET_CODE (insn) == CALL_INSN)
2868 subst_stack_regs (insn, &regstack);
2869
2870 } while (insn != block_end[block]);
2871
2872 /* Something failed if the stack life doesn't match. */
2873
2874 GO_IF_HARD_REG_EQUAL (regstack.reg_set, block_out_reg_set[block], win);
2875
2876 abort ();
2877
2878 win:
2879
2880 /* Adjust the stack of this block on exit to match the stack of
2881 the target block, or copy stack information into stack of
2882 jump target if the target block's stack order hasn't been set
2883 yet. */
2884
2885 if (GET_CODE (insn) == JUMP_INSN)
2886 goto_block_pat (insn, &regstack, PATTERN (insn));
2887
2888 /* Likewise handle the case where we fall into the next block. */
2889
2890 if ((block < blocks - 1) && block_drops_in[block+1])
2891 change_stack (insn, &regstack, &block_stack_in[block+1],
2892 emit_insn_after);
2893 }
2894
2895 /* If the last basic block is the end of a loop, and that loop has
2896 regs live at its start, then the last basic block will have regs live
2897 at its end that need to be popped before the function returns. */
2898
2899 for (reg = regstack.top; reg >= 0; reg--)
2900 if (! current_function_returns_real
2901 || regstack.reg[reg] != FIRST_STACK_REG)
2902 insn = emit_pop_insn (insn, &regstack,
2903 FP_mode_reg[regstack.reg[reg]][(int) DFmode],
2904 emit_insn_after);
2905 }
2906 \f
2907 /* Check expression PAT, which is in INSN, for label references. if
2908 one is found, print the block number of destination to FILE. */
2909
2910 static void
2911 print_blocks (file, insn, pat)
2912 FILE *file;
2913 rtx insn, pat;
2914 {
2915 register RTX_CODE code = GET_CODE (pat);
2916 register int i;
2917 register char *fmt;
2918
2919 if (code == LABEL_REF)
2920 {
2921 register rtx label = XEXP (pat, 0);
2922
2923 if (GET_CODE (label) != CODE_LABEL)
2924 abort ();
2925
2926 fprintf (file, " %d", BLOCK_NUM (label));
2927
2928 return;
2929 }
2930
2931 fmt = GET_RTX_FORMAT (code);
2932 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2933 {
2934 if (fmt[i] == 'e')
2935 print_blocks (file, insn, XEXP (pat, i));
2936 if (fmt[i] == 'E')
2937 {
2938 register int j;
2939 for (j = 0; j < XVECLEN (pat, i); j++)
2940 print_blocks (file, insn, XVECEXP (pat, i, j));
2941 }
2942 }
2943 }
2944 \f
2945 /* Write information about stack registers and stack blocks into FILE.
2946 This is part of making a debugging dump. */
2947 static void
2948 dump_stack_info (file)
2949 FILE *file;
2950 {
2951 register int block;
2952
2953 fprintf (file, "\n%d stack blocks.\n", blocks);
2954 for (block = 0; block < blocks; block++)
2955 {
2956 register rtx head, jump, end;
2957 register int regno;
2958
2959 fprintf (file, "\nStack block %d: first insn %d, last %d.\n",
2960 block, INSN_UID (block_begin[block]),
2961 INSN_UID (block_end[block]));
2962
2963 head = block_begin[block];
2964
2965 fprintf (file, "Reached from blocks: ");
2966 if (GET_CODE (head) == CODE_LABEL)
2967 for (jump = LABEL_REFS (head);
2968 jump != head;
2969 jump = LABEL_NEXTREF (jump))
2970 {
2971 register int from_block = BLOCK_NUM (CONTAINING_INSN (jump));
2972 fprintf (file, " %d", from_block);
2973 }
2974 if (block_drops_in[block])
2975 fprintf (file, " previous");
2976
2977 fprintf (file, "\nlive stack registers on block entry: ");
2978 for (regno = FIRST_STACK_REG; regno <= LAST_STACK_REG ; regno++)
2979 {
2980 if (TEST_HARD_REG_BIT (block_stack_in[block].reg_set, regno))
2981 fprintf (file, "%d ", regno);
2982 }
2983
2984 fprintf (file, "\nlive stack registers on block exit: ");
2985 for (regno = FIRST_STACK_REG; regno <= LAST_STACK_REG ; regno++)
2986 {
2987 if (TEST_HARD_REG_BIT (block_out_reg_set[block], regno))
2988 fprintf (file, "%d ", regno);
2989 }
2990
2991 end = block_end[block];
2992
2993 fprintf (file, "\nJumps to blocks: ");
2994 if (GET_CODE (end) == JUMP_INSN)
2995 print_blocks (file, end, PATTERN (end));
2996
2997 if (block + 1 < blocks && block_drops_in[block+1])
2998 fprintf (file, " next");
2999 else if (block + 1 == blocks
3000 || (GET_CODE (end) == JUMP_INSN
3001 && GET_CODE (PATTERN (end)) == RETURN))
3002 fprintf (file, " return");
3003
3004 fprintf (file, "\n");
3005 }
3006 }
3007 #endif /* STACK_REGS */