OSDN Git Service

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