OSDN Git Service

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