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