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