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