OSDN Git Service

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