OSDN Git Service

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