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