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