OSDN Git Service

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