1*e4b17023SJohn Marino /* Save and restore call-clobbered registers which are live across a call.
2*e4b17023SJohn Marino Copyright (C) 1989, 1992, 1994, 1995, 1997, 1998, 1999, 2000,
3*e4b17023SJohn Marino 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4*e4b17023SJohn Marino Free Software Foundation, Inc.
5*e4b17023SJohn Marino
6*e4b17023SJohn Marino This file is part of GCC.
7*e4b17023SJohn Marino
8*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
9*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
10*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
11*e4b17023SJohn Marino version.
12*e4b17023SJohn Marino
13*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
15*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16*e4b17023SJohn Marino for more details.
17*e4b17023SJohn Marino
18*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
19*e4b17023SJohn Marino along with GCC; see the file COPYING3. If not see
20*e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */
21*e4b17023SJohn Marino
22*e4b17023SJohn Marino #include "config.h"
23*e4b17023SJohn Marino #include "system.h"
24*e4b17023SJohn Marino #include "coretypes.h"
25*e4b17023SJohn Marino #include "tm.h"
26*e4b17023SJohn Marino #include "rtl.h"
27*e4b17023SJohn Marino #include "regs.h"
28*e4b17023SJohn Marino #include "insn-config.h"
29*e4b17023SJohn Marino #include "flags.h"
30*e4b17023SJohn Marino #include "hard-reg-set.h"
31*e4b17023SJohn Marino #include "recog.h"
32*e4b17023SJohn Marino #include "basic-block.h"
33*e4b17023SJohn Marino #include "df.h"
34*e4b17023SJohn Marino #include "reload.h"
35*e4b17023SJohn Marino #include "function.h"
36*e4b17023SJohn Marino #include "expr.h"
37*e4b17023SJohn Marino #include "diagnostic-core.h"
38*e4b17023SJohn Marino #include "tm_p.h"
39*e4b17023SJohn Marino #include "addresses.h"
40*e4b17023SJohn Marino #include "output.h"
41*e4b17023SJohn Marino #include "ggc.h"
42*e4b17023SJohn Marino
43*e4b17023SJohn Marino #define MOVE_MAX_WORDS (MOVE_MAX / UNITS_PER_WORD)
44*e4b17023SJohn Marino
45*e4b17023SJohn Marino #define regno_save_mode \
46*e4b17023SJohn Marino (this_target_reload->x_regno_save_mode)
47*e4b17023SJohn Marino #define cached_reg_save_code \
48*e4b17023SJohn Marino (this_target_reload->x_cached_reg_save_code)
49*e4b17023SJohn Marino #define cached_reg_restore_code \
50*e4b17023SJohn Marino (this_target_reload->x_cached_reg_restore_code)
51*e4b17023SJohn Marino
52*e4b17023SJohn Marino /* For each hard register, a place on the stack where it can be saved,
53*e4b17023SJohn Marino if needed. */
54*e4b17023SJohn Marino
55*e4b17023SJohn Marino static rtx
56*e4b17023SJohn Marino regno_save_mem[FIRST_PSEUDO_REGISTER][MAX_MOVE_MAX / MIN_UNITS_PER_WORD + 1];
57*e4b17023SJohn Marino
58*e4b17023SJohn Marino /* The number of elements in the subsequent array. */
59*e4b17023SJohn Marino static int save_slots_num;
60*e4b17023SJohn Marino
61*e4b17023SJohn Marino /* Allocated slots so far. */
62*e4b17023SJohn Marino static rtx save_slots[FIRST_PSEUDO_REGISTER];
63*e4b17023SJohn Marino
64*e4b17023SJohn Marino /* Set of hard regs currently residing in save area (during insn scan). */
65*e4b17023SJohn Marino
66*e4b17023SJohn Marino static HARD_REG_SET hard_regs_saved;
67*e4b17023SJohn Marino
68*e4b17023SJohn Marino /* Number of registers currently in hard_regs_saved. */
69*e4b17023SJohn Marino
70*e4b17023SJohn Marino static int n_regs_saved;
71*e4b17023SJohn Marino
72*e4b17023SJohn Marino /* Computed by mark_referenced_regs, all regs referenced in a given
73*e4b17023SJohn Marino insn. */
74*e4b17023SJohn Marino static HARD_REG_SET referenced_regs;
75*e4b17023SJohn Marino
76*e4b17023SJohn Marino
77*e4b17023SJohn Marino typedef void refmarker_fn (rtx *loc, enum machine_mode mode, int hardregno,
78*e4b17023SJohn Marino void *mark_arg);
79*e4b17023SJohn Marino
80*e4b17023SJohn Marino static int reg_save_code (int, enum machine_mode);
81*e4b17023SJohn Marino static int reg_restore_code (int, enum machine_mode);
82*e4b17023SJohn Marino
83*e4b17023SJohn Marino struct saved_hard_reg;
84*e4b17023SJohn Marino static void initiate_saved_hard_regs (void);
85*e4b17023SJohn Marino static void new_saved_hard_reg (int, int);
86*e4b17023SJohn Marino static void finish_saved_hard_regs (void);
87*e4b17023SJohn Marino static int saved_hard_reg_compare_func (const void *, const void *);
88*e4b17023SJohn Marino
89*e4b17023SJohn Marino static void mark_set_regs (rtx, const_rtx, void *);
90*e4b17023SJohn Marino static void mark_referenced_regs (rtx *, refmarker_fn *mark, void *mark_arg);
91*e4b17023SJohn Marino static refmarker_fn mark_reg_as_referenced;
92*e4b17023SJohn Marino static refmarker_fn replace_reg_with_saved_mem;
93*e4b17023SJohn Marino static int insert_save (struct insn_chain *, int, int, HARD_REG_SET *,
94*e4b17023SJohn Marino enum machine_mode *);
95*e4b17023SJohn Marino static int insert_restore (struct insn_chain *, int, int, int,
96*e4b17023SJohn Marino enum machine_mode *);
97*e4b17023SJohn Marino static struct insn_chain *insert_one_insn (struct insn_chain *, int, int,
98*e4b17023SJohn Marino rtx);
99*e4b17023SJohn Marino static void add_stored_regs (rtx, const_rtx, void *);
100*e4b17023SJohn Marino
101*e4b17023SJohn Marino
102*e4b17023SJohn Marino
103*e4b17023SJohn Marino static GTY(()) rtx savepat;
104*e4b17023SJohn Marino static GTY(()) rtx restpat;
105*e4b17023SJohn Marino static GTY(()) rtx test_reg;
106*e4b17023SJohn Marino static GTY(()) rtx test_mem;
107*e4b17023SJohn Marino static GTY(()) rtx saveinsn;
108*e4b17023SJohn Marino static GTY(()) rtx restinsn;
109*e4b17023SJohn Marino
110*e4b17023SJohn Marino /* Return the INSN_CODE used to save register REG in mode MODE. */
111*e4b17023SJohn Marino static int
reg_save_code(int reg,enum machine_mode mode)112*e4b17023SJohn Marino reg_save_code (int reg, enum machine_mode mode)
113*e4b17023SJohn Marino {
114*e4b17023SJohn Marino bool ok;
115*e4b17023SJohn Marino if (cached_reg_save_code[reg][mode])
116*e4b17023SJohn Marino return cached_reg_save_code[reg][mode];
117*e4b17023SJohn Marino if (!HARD_REGNO_MODE_OK (reg, mode))
118*e4b17023SJohn Marino {
119*e4b17023SJohn Marino /* Depending on how HARD_REGNO_MODE_OK is defined, range propagation
120*e4b17023SJohn Marino might deduce here that reg >= FIRST_PSEUDO_REGISTER. So the assert
121*e4b17023SJohn Marino below silences a warning. */
122*e4b17023SJohn Marino gcc_assert (reg < FIRST_PSEUDO_REGISTER);
123*e4b17023SJohn Marino cached_reg_save_code[reg][mode] = -1;
124*e4b17023SJohn Marino cached_reg_restore_code[reg][mode] = -1;
125*e4b17023SJohn Marino return -1;
126*e4b17023SJohn Marino }
127*e4b17023SJohn Marino
128*e4b17023SJohn Marino /* Update the register number and modes of the register
129*e4b17023SJohn Marino and memory operand. */
130*e4b17023SJohn Marino SET_REGNO_RAW (test_reg, reg);
131*e4b17023SJohn Marino PUT_MODE (test_reg, mode);
132*e4b17023SJohn Marino PUT_MODE (test_mem, mode);
133*e4b17023SJohn Marino
134*e4b17023SJohn Marino /* Force re-recognition of the modified insns. */
135*e4b17023SJohn Marino INSN_CODE (saveinsn) = -1;
136*e4b17023SJohn Marino INSN_CODE (restinsn) = -1;
137*e4b17023SJohn Marino
138*e4b17023SJohn Marino cached_reg_save_code[reg][mode] = recog_memoized (saveinsn);
139*e4b17023SJohn Marino cached_reg_restore_code[reg][mode] = recog_memoized (restinsn);
140*e4b17023SJohn Marino
141*e4b17023SJohn Marino /* Now extract both insns and see if we can meet their
142*e4b17023SJohn Marino constraints. */
143*e4b17023SJohn Marino ok = (cached_reg_save_code[reg][mode] != -1
144*e4b17023SJohn Marino && cached_reg_restore_code[reg][mode] != -1);
145*e4b17023SJohn Marino if (ok)
146*e4b17023SJohn Marino {
147*e4b17023SJohn Marino extract_insn (saveinsn);
148*e4b17023SJohn Marino ok = constrain_operands (1);
149*e4b17023SJohn Marino extract_insn (restinsn);
150*e4b17023SJohn Marino ok &= constrain_operands (1);
151*e4b17023SJohn Marino }
152*e4b17023SJohn Marino
153*e4b17023SJohn Marino if (! ok)
154*e4b17023SJohn Marino {
155*e4b17023SJohn Marino cached_reg_save_code[reg][mode] = -1;
156*e4b17023SJohn Marino cached_reg_restore_code[reg][mode] = -1;
157*e4b17023SJohn Marino }
158*e4b17023SJohn Marino gcc_assert (cached_reg_save_code[reg][mode]);
159*e4b17023SJohn Marino return cached_reg_save_code[reg][mode];
160*e4b17023SJohn Marino }
161*e4b17023SJohn Marino
162*e4b17023SJohn Marino /* Return the INSN_CODE used to restore register REG in mode MODE. */
163*e4b17023SJohn Marino static int
reg_restore_code(int reg,enum machine_mode mode)164*e4b17023SJohn Marino reg_restore_code (int reg, enum machine_mode mode)
165*e4b17023SJohn Marino {
166*e4b17023SJohn Marino if (cached_reg_restore_code[reg][mode])
167*e4b17023SJohn Marino return cached_reg_restore_code[reg][mode];
168*e4b17023SJohn Marino /* Populate our cache. */
169*e4b17023SJohn Marino reg_save_code (reg, mode);
170*e4b17023SJohn Marino return cached_reg_restore_code[reg][mode];
171*e4b17023SJohn Marino }
172*e4b17023SJohn Marino
173*e4b17023SJohn Marino /* Initialize for caller-save.
174*e4b17023SJohn Marino
175*e4b17023SJohn Marino Look at all the hard registers that are used by a call and for which
176*e4b17023SJohn Marino reginfo.c has not already excluded from being used across a call.
177*e4b17023SJohn Marino
178*e4b17023SJohn Marino Ensure that we can find a mode to save the register and that there is a
179*e4b17023SJohn Marino simple insn to save and restore the register. This latter check avoids
180*e4b17023SJohn Marino problems that would occur if we tried to save the MQ register of some
181*e4b17023SJohn Marino machines directly into memory. */
182*e4b17023SJohn Marino
183*e4b17023SJohn Marino void
init_caller_save(void)184*e4b17023SJohn Marino init_caller_save (void)
185*e4b17023SJohn Marino {
186*e4b17023SJohn Marino rtx addr_reg;
187*e4b17023SJohn Marino int offset;
188*e4b17023SJohn Marino rtx address;
189*e4b17023SJohn Marino int i, j;
190*e4b17023SJohn Marino
191*e4b17023SJohn Marino if (caller_save_initialized_p)
192*e4b17023SJohn Marino return;
193*e4b17023SJohn Marino
194*e4b17023SJohn Marino caller_save_initialized_p = true;
195*e4b17023SJohn Marino
196*e4b17023SJohn Marino CLEAR_HARD_REG_SET (no_caller_save_reg_set);
197*e4b17023SJohn Marino /* First find all the registers that we need to deal with and all
198*e4b17023SJohn Marino the modes that they can have. If we can't find a mode to use,
199*e4b17023SJohn Marino we can't have the register live over calls. */
200*e4b17023SJohn Marino
201*e4b17023SJohn Marino for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
202*e4b17023SJohn Marino {
203*e4b17023SJohn Marino if (call_used_regs[i]
204*e4b17023SJohn Marino && !TEST_HARD_REG_BIT (call_fixed_reg_set, i))
205*e4b17023SJohn Marino {
206*e4b17023SJohn Marino for (j = 1; j <= MOVE_MAX_WORDS; j++)
207*e4b17023SJohn Marino {
208*e4b17023SJohn Marino regno_save_mode[i][j] = HARD_REGNO_CALLER_SAVE_MODE (i, j,
209*e4b17023SJohn Marino VOIDmode);
210*e4b17023SJohn Marino if (regno_save_mode[i][j] == VOIDmode && j == 1)
211*e4b17023SJohn Marino {
212*e4b17023SJohn Marino SET_HARD_REG_BIT (call_fixed_reg_set, i);
213*e4b17023SJohn Marino }
214*e4b17023SJohn Marino }
215*e4b17023SJohn Marino }
216*e4b17023SJohn Marino else
217*e4b17023SJohn Marino regno_save_mode[i][1] = VOIDmode;
218*e4b17023SJohn Marino }
219*e4b17023SJohn Marino
220*e4b17023SJohn Marino /* The following code tries to approximate the conditions under which
221*e4b17023SJohn Marino we can easily save and restore a register without scratch registers or
222*e4b17023SJohn Marino other complexities. It will usually work, except under conditions where
223*e4b17023SJohn Marino the validity of an insn operand is dependent on the address offset.
224*e4b17023SJohn Marino No such cases are currently known.
225*e4b17023SJohn Marino
226*e4b17023SJohn Marino We first find a typical offset from some BASE_REG_CLASS register.
227*e4b17023SJohn Marino This address is chosen by finding the first register in the class
228*e4b17023SJohn Marino and by finding the smallest power of two that is a valid offset from
229*e4b17023SJohn Marino that register in every mode we will use to save registers. */
230*e4b17023SJohn Marino
231*e4b17023SJohn Marino for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
232*e4b17023SJohn Marino if (TEST_HARD_REG_BIT
233*e4b17023SJohn Marino (reg_class_contents
234*e4b17023SJohn Marino [(int) base_reg_class (regno_save_mode[i][1], ADDR_SPACE_GENERIC,
235*e4b17023SJohn Marino PLUS, CONST_INT)], i))
236*e4b17023SJohn Marino break;
237*e4b17023SJohn Marino
238*e4b17023SJohn Marino gcc_assert (i < FIRST_PSEUDO_REGISTER);
239*e4b17023SJohn Marino
240*e4b17023SJohn Marino addr_reg = gen_rtx_REG (Pmode, i);
241*e4b17023SJohn Marino
242*e4b17023SJohn Marino for (offset = 1 << (HOST_BITS_PER_INT / 2); offset; offset >>= 1)
243*e4b17023SJohn Marino {
244*e4b17023SJohn Marino address = gen_rtx_PLUS (Pmode, addr_reg, GEN_INT (offset));
245*e4b17023SJohn Marino
246*e4b17023SJohn Marino for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
247*e4b17023SJohn Marino if (regno_save_mode[i][1] != VOIDmode
248*e4b17023SJohn Marino && ! strict_memory_address_p (regno_save_mode[i][1], address))
249*e4b17023SJohn Marino break;
250*e4b17023SJohn Marino
251*e4b17023SJohn Marino if (i == FIRST_PSEUDO_REGISTER)
252*e4b17023SJohn Marino break;
253*e4b17023SJohn Marino }
254*e4b17023SJohn Marino
255*e4b17023SJohn Marino /* If we didn't find a valid address, we must use register indirect. */
256*e4b17023SJohn Marino if (offset == 0)
257*e4b17023SJohn Marino address = addr_reg;
258*e4b17023SJohn Marino
259*e4b17023SJohn Marino /* Next we try to form an insn to save and restore the register. We
260*e4b17023SJohn Marino see if such an insn is recognized and meets its constraints.
261*e4b17023SJohn Marino
262*e4b17023SJohn Marino To avoid lots of unnecessary RTL allocation, we construct all the RTL
263*e4b17023SJohn Marino once, then modify the memory and register operands in-place. */
264*e4b17023SJohn Marino
265*e4b17023SJohn Marino test_reg = gen_rtx_REG (VOIDmode, 0);
266*e4b17023SJohn Marino test_mem = gen_rtx_MEM (VOIDmode, address);
267*e4b17023SJohn Marino savepat = gen_rtx_SET (VOIDmode, test_mem, test_reg);
268*e4b17023SJohn Marino restpat = gen_rtx_SET (VOIDmode, test_reg, test_mem);
269*e4b17023SJohn Marino
270*e4b17023SJohn Marino saveinsn = gen_rtx_INSN (VOIDmode, 0, 0, 0, 0, savepat, 0, -1, 0);
271*e4b17023SJohn Marino restinsn = gen_rtx_INSN (VOIDmode, 0, 0, 0, 0, restpat, 0, -1, 0);
272*e4b17023SJohn Marino
273*e4b17023SJohn Marino for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
274*e4b17023SJohn Marino for (j = 1; j <= MOVE_MAX_WORDS; j++)
275*e4b17023SJohn Marino if (reg_save_code (i,regno_save_mode[i][j]) == -1)
276*e4b17023SJohn Marino {
277*e4b17023SJohn Marino regno_save_mode[i][j] = VOIDmode;
278*e4b17023SJohn Marino if (j == 1)
279*e4b17023SJohn Marino {
280*e4b17023SJohn Marino SET_HARD_REG_BIT (call_fixed_reg_set, i);
281*e4b17023SJohn Marino if (call_used_regs[i])
282*e4b17023SJohn Marino SET_HARD_REG_BIT (no_caller_save_reg_set, i);
283*e4b17023SJohn Marino }
284*e4b17023SJohn Marino }
285*e4b17023SJohn Marino }
286*e4b17023SJohn Marino
287*e4b17023SJohn Marino
288*e4b17023SJohn Marino
289*e4b17023SJohn Marino /* Initialize save areas by showing that we haven't allocated any yet. */
290*e4b17023SJohn Marino
291*e4b17023SJohn Marino void
init_save_areas(void)292*e4b17023SJohn Marino init_save_areas (void)
293*e4b17023SJohn Marino {
294*e4b17023SJohn Marino int i, j;
295*e4b17023SJohn Marino
296*e4b17023SJohn Marino for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
297*e4b17023SJohn Marino for (j = 1; j <= MOVE_MAX_WORDS; j++)
298*e4b17023SJohn Marino regno_save_mem[i][j] = 0;
299*e4b17023SJohn Marino save_slots_num = 0;
300*e4b17023SJohn Marino
301*e4b17023SJohn Marino }
302*e4b17023SJohn Marino
303*e4b17023SJohn Marino /* The structure represents a hard register which should be saved
304*e4b17023SJohn Marino through the call. It is used when the integrated register
305*e4b17023SJohn Marino allocator (IRA) is used and sharing save slots is on. */
306*e4b17023SJohn Marino struct saved_hard_reg
307*e4b17023SJohn Marino {
308*e4b17023SJohn Marino /* Order number starting with 0. */
309*e4b17023SJohn Marino int num;
310*e4b17023SJohn Marino /* The hard regno. */
311*e4b17023SJohn Marino int hard_regno;
312*e4b17023SJohn Marino /* Execution frequency of all calls through which given hard
313*e4b17023SJohn Marino register should be saved. */
314*e4b17023SJohn Marino int call_freq;
315*e4b17023SJohn Marino /* Stack slot reserved to save the hard register through calls. */
316*e4b17023SJohn Marino rtx slot;
317*e4b17023SJohn Marino /* True if it is first hard register in the chain of hard registers
318*e4b17023SJohn Marino sharing the same stack slot. */
319*e4b17023SJohn Marino int first_p;
320*e4b17023SJohn Marino /* Order number of the next hard register structure with the same
321*e4b17023SJohn Marino slot in the chain. -1 represents end of the chain. */
322*e4b17023SJohn Marino int next;
323*e4b17023SJohn Marino };
324*e4b17023SJohn Marino
325*e4b17023SJohn Marino /* Map: hard register number to the corresponding structure. */
326*e4b17023SJohn Marino static struct saved_hard_reg *hard_reg_map[FIRST_PSEUDO_REGISTER];
327*e4b17023SJohn Marino
328*e4b17023SJohn Marino /* The number of all structures representing hard registers should be
329*e4b17023SJohn Marino saved, in order words, the number of used elements in the following
330*e4b17023SJohn Marino array. */
331*e4b17023SJohn Marino static int saved_regs_num;
332*e4b17023SJohn Marino
333*e4b17023SJohn Marino /* Pointers to all the structures. Index is the order number of the
334*e4b17023SJohn Marino corresponding structure. */
335*e4b17023SJohn Marino static struct saved_hard_reg *all_saved_regs[FIRST_PSEUDO_REGISTER];
336*e4b17023SJohn Marino
337*e4b17023SJohn Marino /* First called function for work with saved hard registers. */
338*e4b17023SJohn Marino static void
initiate_saved_hard_regs(void)339*e4b17023SJohn Marino initiate_saved_hard_regs (void)
340*e4b17023SJohn Marino {
341*e4b17023SJohn Marino int i;
342*e4b17023SJohn Marino
343*e4b17023SJohn Marino saved_regs_num = 0;
344*e4b17023SJohn Marino for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
345*e4b17023SJohn Marino hard_reg_map[i] = NULL;
346*e4b17023SJohn Marino }
347*e4b17023SJohn Marino
348*e4b17023SJohn Marino /* Allocate and return new saved hard register with given REGNO and
349*e4b17023SJohn Marino CALL_FREQ. */
350*e4b17023SJohn Marino static void
new_saved_hard_reg(int regno,int call_freq)351*e4b17023SJohn Marino new_saved_hard_reg (int regno, int call_freq)
352*e4b17023SJohn Marino {
353*e4b17023SJohn Marino struct saved_hard_reg *saved_reg;
354*e4b17023SJohn Marino
355*e4b17023SJohn Marino saved_reg
356*e4b17023SJohn Marino = (struct saved_hard_reg *) xmalloc (sizeof (struct saved_hard_reg));
357*e4b17023SJohn Marino hard_reg_map[regno] = all_saved_regs[saved_regs_num] = saved_reg;
358*e4b17023SJohn Marino saved_reg->num = saved_regs_num++;
359*e4b17023SJohn Marino saved_reg->hard_regno = regno;
360*e4b17023SJohn Marino saved_reg->call_freq = call_freq;
361*e4b17023SJohn Marino saved_reg->first_p = FALSE;
362*e4b17023SJohn Marino saved_reg->next = -1;
363*e4b17023SJohn Marino }
364*e4b17023SJohn Marino
365*e4b17023SJohn Marino /* Free memory allocated for the saved hard registers. */
366*e4b17023SJohn Marino static void
finish_saved_hard_regs(void)367*e4b17023SJohn Marino finish_saved_hard_regs (void)
368*e4b17023SJohn Marino {
369*e4b17023SJohn Marino int i;
370*e4b17023SJohn Marino
371*e4b17023SJohn Marino for (i = 0; i < saved_regs_num; i++)
372*e4b17023SJohn Marino free (all_saved_regs[i]);
373*e4b17023SJohn Marino }
374*e4b17023SJohn Marino
375*e4b17023SJohn Marino /* The function is used to sort the saved hard register structures
376*e4b17023SJohn Marino according their frequency. */
377*e4b17023SJohn Marino static int
saved_hard_reg_compare_func(const void * v1p,const void * v2p)378*e4b17023SJohn Marino saved_hard_reg_compare_func (const void *v1p, const void *v2p)
379*e4b17023SJohn Marino {
380*e4b17023SJohn Marino const struct saved_hard_reg *p1 = *(struct saved_hard_reg * const *) v1p;
381*e4b17023SJohn Marino const struct saved_hard_reg *p2 = *(struct saved_hard_reg * const *) v2p;
382*e4b17023SJohn Marino
383*e4b17023SJohn Marino if (flag_omit_frame_pointer)
384*e4b17023SJohn Marino {
385*e4b17023SJohn Marino if (p1->call_freq - p2->call_freq != 0)
386*e4b17023SJohn Marino return p1->call_freq - p2->call_freq;
387*e4b17023SJohn Marino }
388*e4b17023SJohn Marino else if (p2->call_freq - p1->call_freq != 0)
389*e4b17023SJohn Marino return p2->call_freq - p1->call_freq;
390*e4b17023SJohn Marino
391*e4b17023SJohn Marino return p1->num - p2->num;
392*e4b17023SJohn Marino }
393*e4b17023SJohn Marino
394*e4b17023SJohn Marino /* Allocate save areas for any hard registers that might need saving.
395*e4b17023SJohn Marino We take a conservative approach here and look for call-clobbered hard
396*e4b17023SJohn Marino registers that are assigned to pseudos that cross calls. This may
397*e4b17023SJohn Marino overestimate slightly (especially if some of these registers are later
398*e4b17023SJohn Marino used as spill registers), but it should not be significant.
399*e4b17023SJohn Marino
400*e4b17023SJohn Marino For IRA we use priority coloring to decrease stack slots needed for
401*e4b17023SJohn Marino saving hard registers through calls. We build conflicts for them
402*e4b17023SJohn Marino to do coloring.
403*e4b17023SJohn Marino
404*e4b17023SJohn Marino Future work:
405*e4b17023SJohn Marino
406*e4b17023SJohn Marino In the fallback case we should iterate backwards across all possible
407*e4b17023SJohn Marino modes for the save, choosing the largest available one instead of
408*e4b17023SJohn Marino falling back to the smallest mode immediately. (eg TF -> DF -> SF).
409*e4b17023SJohn Marino
410*e4b17023SJohn Marino We do not try to use "move multiple" instructions that exist
411*e4b17023SJohn Marino on some machines (such as the 68k moveml). It could be a win to try
412*e4b17023SJohn Marino and use them when possible. The hard part is doing it in a way that is
413*e4b17023SJohn Marino machine independent since they might be saving non-consecutive
414*e4b17023SJohn Marino registers. (imagine caller-saving d0,d1,a0,a1 on the 68k) */
415*e4b17023SJohn Marino
416*e4b17023SJohn Marino void
setup_save_areas(void)417*e4b17023SJohn Marino setup_save_areas (void)
418*e4b17023SJohn Marino {
419*e4b17023SJohn Marino int i, j, k, freq;
420*e4b17023SJohn Marino HARD_REG_SET hard_regs_used;
421*e4b17023SJohn Marino struct saved_hard_reg *saved_reg;
422*e4b17023SJohn Marino rtx insn;
423*e4b17023SJohn Marino struct insn_chain *chain, *next;
424*e4b17023SJohn Marino unsigned int regno;
425*e4b17023SJohn Marino HARD_REG_SET hard_regs_to_save, used_regs, this_insn_sets;
426*e4b17023SJohn Marino reg_set_iterator rsi;
427*e4b17023SJohn Marino
428*e4b17023SJohn Marino CLEAR_HARD_REG_SET (hard_regs_used);
429*e4b17023SJohn Marino
430*e4b17023SJohn Marino /* Find every CALL_INSN and record which hard regs are live across the
431*e4b17023SJohn Marino call into HARD_REG_MAP and HARD_REGS_USED. */
432*e4b17023SJohn Marino initiate_saved_hard_regs ();
433*e4b17023SJohn Marino /* Create hard reg saved regs. */
434*e4b17023SJohn Marino for (chain = reload_insn_chain; chain != 0; chain = next)
435*e4b17023SJohn Marino {
436*e4b17023SJohn Marino insn = chain->insn;
437*e4b17023SJohn Marino next = chain->next;
438*e4b17023SJohn Marino if (!CALL_P (insn)
439*e4b17023SJohn Marino || find_reg_note (insn, REG_NORETURN, NULL))
440*e4b17023SJohn Marino continue;
441*e4b17023SJohn Marino freq = REG_FREQ_FROM_BB (BLOCK_FOR_INSN (insn));
442*e4b17023SJohn Marino REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
443*e4b17023SJohn Marino &chain->live_throughout);
444*e4b17023SJohn Marino COPY_HARD_REG_SET (used_regs, call_used_reg_set);
445*e4b17023SJohn Marino
446*e4b17023SJohn Marino /* Record all registers set in this call insn. These don't
447*e4b17023SJohn Marino need to be saved. N.B. the call insn might set a subreg
448*e4b17023SJohn Marino of a multi-hard-reg pseudo; then the pseudo is considered
449*e4b17023SJohn Marino live during the call, but the subreg that is set
450*e4b17023SJohn Marino isn't. */
451*e4b17023SJohn Marino CLEAR_HARD_REG_SET (this_insn_sets);
452*e4b17023SJohn Marino note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
453*e4b17023SJohn Marino /* Sibcalls are considered to set the return value. */
454*e4b17023SJohn Marino if (SIBLING_CALL_P (insn) && crtl->return_rtx)
455*e4b17023SJohn Marino mark_set_regs (crtl->return_rtx, NULL_RTX, &this_insn_sets);
456*e4b17023SJohn Marino
457*e4b17023SJohn Marino AND_COMPL_HARD_REG_SET (used_regs, call_fixed_reg_set);
458*e4b17023SJohn Marino AND_COMPL_HARD_REG_SET (used_regs, this_insn_sets);
459*e4b17023SJohn Marino AND_HARD_REG_SET (hard_regs_to_save, used_regs);
460*e4b17023SJohn Marino for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
461*e4b17023SJohn Marino if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
462*e4b17023SJohn Marino {
463*e4b17023SJohn Marino if (hard_reg_map[regno] != NULL)
464*e4b17023SJohn Marino hard_reg_map[regno]->call_freq += freq;
465*e4b17023SJohn Marino else
466*e4b17023SJohn Marino new_saved_hard_reg (regno, freq);
467*e4b17023SJohn Marino SET_HARD_REG_BIT (hard_regs_used, regno);
468*e4b17023SJohn Marino }
469*e4b17023SJohn Marino /* Look through all live pseudos, mark their hard registers. */
470*e4b17023SJohn Marino EXECUTE_IF_SET_IN_REG_SET
471*e4b17023SJohn Marino (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
472*e4b17023SJohn Marino {
473*e4b17023SJohn Marino int r = reg_renumber[regno];
474*e4b17023SJohn Marino int bound;
475*e4b17023SJohn Marino
476*e4b17023SJohn Marino if (r < 0)
477*e4b17023SJohn Marino continue;
478*e4b17023SJohn Marino
479*e4b17023SJohn Marino bound = r + hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
480*e4b17023SJohn Marino for (; r < bound; r++)
481*e4b17023SJohn Marino if (TEST_HARD_REG_BIT (used_regs, r))
482*e4b17023SJohn Marino {
483*e4b17023SJohn Marino if (hard_reg_map[r] != NULL)
484*e4b17023SJohn Marino hard_reg_map[r]->call_freq += freq;
485*e4b17023SJohn Marino else
486*e4b17023SJohn Marino new_saved_hard_reg (r, freq);
487*e4b17023SJohn Marino SET_HARD_REG_BIT (hard_regs_to_save, r);
488*e4b17023SJohn Marino SET_HARD_REG_BIT (hard_regs_used, r);
489*e4b17023SJohn Marino }
490*e4b17023SJohn Marino }
491*e4b17023SJohn Marino }
492*e4b17023SJohn Marino
493*e4b17023SJohn Marino /* If requested, figure out which hard regs can share save slots. */
494*e4b17023SJohn Marino if (optimize && flag_ira_share_save_slots)
495*e4b17023SJohn Marino {
496*e4b17023SJohn Marino rtx slot;
497*e4b17023SJohn Marino char *saved_reg_conflicts;
498*e4b17023SJohn Marino int next_k;
499*e4b17023SJohn Marino struct saved_hard_reg *saved_reg2, *saved_reg3;
500*e4b17023SJohn Marino int call_saved_regs_num;
501*e4b17023SJohn Marino struct saved_hard_reg *call_saved_regs[FIRST_PSEUDO_REGISTER];
502*e4b17023SJohn Marino int best_slot_num;
503*e4b17023SJohn Marino int prev_save_slots_num;
504*e4b17023SJohn Marino rtx prev_save_slots[FIRST_PSEUDO_REGISTER];
505*e4b17023SJohn Marino
506*e4b17023SJohn Marino /* Find saved hard register conflicts. */
507*e4b17023SJohn Marino saved_reg_conflicts = (char *) xmalloc (saved_regs_num * saved_regs_num);
508*e4b17023SJohn Marino memset (saved_reg_conflicts, 0, saved_regs_num * saved_regs_num);
509*e4b17023SJohn Marino for (chain = reload_insn_chain; chain != 0; chain = next)
510*e4b17023SJohn Marino {
511*e4b17023SJohn Marino call_saved_regs_num = 0;
512*e4b17023SJohn Marino insn = chain->insn;
513*e4b17023SJohn Marino next = chain->next;
514*e4b17023SJohn Marino if (!CALL_P (insn)
515*e4b17023SJohn Marino || find_reg_note (insn, REG_NORETURN, NULL))
516*e4b17023SJohn Marino continue;
517*e4b17023SJohn Marino REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
518*e4b17023SJohn Marino &chain->live_throughout);
519*e4b17023SJohn Marino COPY_HARD_REG_SET (used_regs, call_used_reg_set);
520*e4b17023SJohn Marino
521*e4b17023SJohn Marino /* Record all registers set in this call insn. These don't
522*e4b17023SJohn Marino need to be saved. N.B. the call insn might set a subreg
523*e4b17023SJohn Marino of a multi-hard-reg pseudo; then the pseudo is considered
524*e4b17023SJohn Marino live during the call, but the subreg that is set
525*e4b17023SJohn Marino isn't. */
526*e4b17023SJohn Marino CLEAR_HARD_REG_SET (this_insn_sets);
527*e4b17023SJohn Marino note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
528*e4b17023SJohn Marino /* Sibcalls are considered to set the return value,
529*e4b17023SJohn Marino compare df-scan.c:df_get_call_refs. */
530*e4b17023SJohn Marino if (SIBLING_CALL_P (insn) && crtl->return_rtx)
531*e4b17023SJohn Marino mark_set_regs (crtl->return_rtx, NULL_RTX, &this_insn_sets);
532*e4b17023SJohn Marino
533*e4b17023SJohn Marino AND_COMPL_HARD_REG_SET (used_regs, call_fixed_reg_set);
534*e4b17023SJohn Marino AND_COMPL_HARD_REG_SET (used_regs, this_insn_sets);
535*e4b17023SJohn Marino AND_HARD_REG_SET (hard_regs_to_save, used_regs);
536*e4b17023SJohn Marino for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
537*e4b17023SJohn Marino if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
538*e4b17023SJohn Marino {
539*e4b17023SJohn Marino gcc_assert (hard_reg_map[regno] != NULL);
540*e4b17023SJohn Marino call_saved_regs[call_saved_regs_num++] = hard_reg_map[regno];
541*e4b17023SJohn Marino }
542*e4b17023SJohn Marino /* Look through all live pseudos, mark their hard registers. */
543*e4b17023SJohn Marino EXECUTE_IF_SET_IN_REG_SET
544*e4b17023SJohn Marino (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
545*e4b17023SJohn Marino {
546*e4b17023SJohn Marino int r = reg_renumber[regno];
547*e4b17023SJohn Marino int bound;
548*e4b17023SJohn Marino
549*e4b17023SJohn Marino if (r < 0)
550*e4b17023SJohn Marino continue;
551*e4b17023SJohn Marino
552*e4b17023SJohn Marino bound = r + hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
553*e4b17023SJohn Marino for (; r < bound; r++)
554*e4b17023SJohn Marino if (TEST_HARD_REG_BIT (used_regs, r))
555*e4b17023SJohn Marino call_saved_regs[call_saved_regs_num++] = hard_reg_map[r];
556*e4b17023SJohn Marino }
557*e4b17023SJohn Marino for (i = 0; i < call_saved_regs_num; i++)
558*e4b17023SJohn Marino {
559*e4b17023SJohn Marino saved_reg = call_saved_regs[i];
560*e4b17023SJohn Marino for (j = 0; j < call_saved_regs_num; j++)
561*e4b17023SJohn Marino if (i != j)
562*e4b17023SJohn Marino {
563*e4b17023SJohn Marino saved_reg2 = call_saved_regs[j];
564*e4b17023SJohn Marino saved_reg_conflicts[saved_reg->num * saved_regs_num
565*e4b17023SJohn Marino + saved_reg2->num]
566*e4b17023SJohn Marino = saved_reg_conflicts[saved_reg2->num * saved_regs_num
567*e4b17023SJohn Marino + saved_reg->num]
568*e4b17023SJohn Marino = TRUE;
569*e4b17023SJohn Marino }
570*e4b17023SJohn Marino }
571*e4b17023SJohn Marino }
572*e4b17023SJohn Marino /* Sort saved hard regs. */
573*e4b17023SJohn Marino qsort (all_saved_regs, saved_regs_num, sizeof (struct saved_hard_reg *),
574*e4b17023SJohn Marino saved_hard_reg_compare_func);
575*e4b17023SJohn Marino /* Initiate slots available from the previous reload
576*e4b17023SJohn Marino iteration. */
577*e4b17023SJohn Marino prev_save_slots_num = save_slots_num;
578*e4b17023SJohn Marino memcpy (prev_save_slots, save_slots, save_slots_num * sizeof (rtx));
579*e4b17023SJohn Marino save_slots_num = 0;
580*e4b17023SJohn Marino /* Allocate stack slots for the saved hard registers. */
581*e4b17023SJohn Marino for (i = 0; i < saved_regs_num; i++)
582*e4b17023SJohn Marino {
583*e4b17023SJohn Marino saved_reg = all_saved_regs[i];
584*e4b17023SJohn Marino regno = saved_reg->hard_regno;
585*e4b17023SJohn Marino for (j = 0; j < i; j++)
586*e4b17023SJohn Marino {
587*e4b17023SJohn Marino saved_reg2 = all_saved_regs[j];
588*e4b17023SJohn Marino if (! saved_reg2->first_p)
589*e4b17023SJohn Marino continue;
590*e4b17023SJohn Marino slot = saved_reg2->slot;
591*e4b17023SJohn Marino for (k = j; k >= 0; k = next_k)
592*e4b17023SJohn Marino {
593*e4b17023SJohn Marino saved_reg3 = all_saved_regs[k];
594*e4b17023SJohn Marino next_k = saved_reg3->next;
595*e4b17023SJohn Marino if (saved_reg_conflicts[saved_reg->num * saved_regs_num
596*e4b17023SJohn Marino + saved_reg3->num])
597*e4b17023SJohn Marino break;
598*e4b17023SJohn Marino }
599*e4b17023SJohn Marino if (k < 0
600*e4b17023SJohn Marino && (GET_MODE_SIZE (regno_save_mode[regno][1])
601*e4b17023SJohn Marino <= GET_MODE_SIZE (regno_save_mode
602*e4b17023SJohn Marino [saved_reg2->hard_regno][1])))
603*e4b17023SJohn Marino {
604*e4b17023SJohn Marino saved_reg->slot
605*e4b17023SJohn Marino = adjust_address_nv
606*e4b17023SJohn Marino (slot, regno_save_mode[saved_reg->hard_regno][1], 0);
607*e4b17023SJohn Marino regno_save_mem[regno][1] = saved_reg->slot;
608*e4b17023SJohn Marino saved_reg->next = saved_reg2->next;
609*e4b17023SJohn Marino saved_reg2->next = i;
610*e4b17023SJohn Marino if (dump_file != NULL)
611*e4b17023SJohn Marino fprintf (dump_file, "%d uses slot of %d\n",
612*e4b17023SJohn Marino regno, saved_reg2->hard_regno);
613*e4b17023SJohn Marino break;
614*e4b17023SJohn Marino }
615*e4b17023SJohn Marino }
616*e4b17023SJohn Marino if (j == i)
617*e4b17023SJohn Marino {
618*e4b17023SJohn Marino saved_reg->first_p = TRUE;
619*e4b17023SJohn Marino for (best_slot_num = -1, j = 0; j < prev_save_slots_num; j++)
620*e4b17023SJohn Marino {
621*e4b17023SJohn Marino slot = prev_save_slots[j];
622*e4b17023SJohn Marino if (slot == NULL_RTX)
623*e4b17023SJohn Marino continue;
624*e4b17023SJohn Marino if (GET_MODE_SIZE (regno_save_mode[regno][1])
625*e4b17023SJohn Marino <= GET_MODE_SIZE (GET_MODE (slot))
626*e4b17023SJohn Marino && best_slot_num < 0)
627*e4b17023SJohn Marino best_slot_num = j;
628*e4b17023SJohn Marino if (GET_MODE (slot) == regno_save_mode[regno][1])
629*e4b17023SJohn Marino break;
630*e4b17023SJohn Marino }
631*e4b17023SJohn Marino if (best_slot_num >= 0)
632*e4b17023SJohn Marino {
633*e4b17023SJohn Marino saved_reg->slot = prev_save_slots[best_slot_num];
634*e4b17023SJohn Marino saved_reg->slot
635*e4b17023SJohn Marino = adjust_address_nv
636*e4b17023SJohn Marino (saved_reg->slot,
637*e4b17023SJohn Marino regno_save_mode[saved_reg->hard_regno][1], 0);
638*e4b17023SJohn Marino if (dump_file != NULL)
639*e4b17023SJohn Marino fprintf (dump_file,
640*e4b17023SJohn Marino "%d uses a slot from prev iteration\n", regno);
641*e4b17023SJohn Marino prev_save_slots[best_slot_num] = NULL_RTX;
642*e4b17023SJohn Marino if (best_slot_num + 1 == prev_save_slots_num)
643*e4b17023SJohn Marino prev_save_slots_num--;
644*e4b17023SJohn Marino }
645*e4b17023SJohn Marino else
646*e4b17023SJohn Marino {
647*e4b17023SJohn Marino saved_reg->slot
648*e4b17023SJohn Marino = assign_stack_local_1
649*e4b17023SJohn Marino (regno_save_mode[regno][1],
650*e4b17023SJohn Marino GET_MODE_SIZE (regno_save_mode[regno][1]), 0,
651*e4b17023SJohn Marino ASLK_REDUCE_ALIGN);
652*e4b17023SJohn Marino if (dump_file != NULL)
653*e4b17023SJohn Marino fprintf (dump_file, "%d uses a new slot\n", regno);
654*e4b17023SJohn Marino }
655*e4b17023SJohn Marino regno_save_mem[regno][1] = saved_reg->slot;
656*e4b17023SJohn Marino save_slots[save_slots_num++] = saved_reg->slot;
657*e4b17023SJohn Marino }
658*e4b17023SJohn Marino }
659*e4b17023SJohn Marino free (saved_reg_conflicts);
660*e4b17023SJohn Marino finish_saved_hard_regs ();
661*e4b17023SJohn Marino }
662*e4b17023SJohn Marino else
663*e4b17023SJohn Marino {
664*e4b17023SJohn Marino /* We are not sharing slots.
665*e4b17023SJohn Marino
666*e4b17023SJohn Marino Run through all the call-used hard-registers and allocate
667*e4b17023SJohn Marino space for each in the caller-save area. Try to allocate space
668*e4b17023SJohn Marino in a manner which allows multi-register saves/restores to be done. */
669*e4b17023SJohn Marino
670*e4b17023SJohn Marino for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
671*e4b17023SJohn Marino for (j = MOVE_MAX_WORDS; j > 0; j--)
672*e4b17023SJohn Marino {
673*e4b17023SJohn Marino int do_save = 1;
674*e4b17023SJohn Marino
675*e4b17023SJohn Marino /* If no mode exists for this size, try another. Also break out
676*e4b17023SJohn Marino if we have already saved this hard register. */
677*e4b17023SJohn Marino if (regno_save_mode[i][j] == VOIDmode || regno_save_mem[i][1] != 0)
678*e4b17023SJohn Marino continue;
679*e4b17023SJohn Marino
680*e4b17023SJohn Marino /* See if any register in this group has been saved. */
681*e4b17023SJohn Marino for (k = 0; k < j; k++)
682*e4b17023SJohn Marino if (regno_save_mem[i + k][1])
683*e4b17023SJohn Marino {
684*e4b17023SJohn Marino do_save = 0;
685*e4b17023SJohn Marino break;
686*e4b17023SJohn Marino }
687*e4b17023SJohn Marino if (! do_save)
688*e4b17023SJohn Marino continue;
689*e4b17023SJohn Marino
690*e4b17023SJohn Marino for (k = 0; k < j; k++)
691*e4b17023SJohn Marino if (! TEST_HARD_REG_BIT (hard_regs_used, i + k))
692*e4b17023SJohn Marino {
693*e4b17023SJohn Marino do_save = 0;
694*e4b17023SJohn Marino break;
695*e4b17023SJohn Marino }
696*e4b17023SJohn Marino if (! do_save)
697*e4b17023SJohn Marino continue;
698*e4b17023SJohn Marino
699*e4b17023SJohn Marino /* We have found an acceptable mode to store in. Since
700*e4b17023SJohn Marino hard register is always saved in the widest mode
701*e4b17023SJohn Marino available, the mode may be wider than necessary, it is
702*e4b17023SJohn Marino OK to reduce the alignment of spill space. We will
703*e4b17023SJohn Marino verify that it is equal to or greater than required
704*e4b17023SJohn Marino when we restore and save the hard register in
705*e4b17023SJohn Marino insert_restore and insert_save. */
706*e4b17023SJohn Marino regno_save_mem[i][j]
707*e4b17023SJohn Marino = assign_stack_local_1 (regno_save_mode[i][j],
708*e4b17023SJohn Marino GET_MODE_SIZE (regno_save_mode[i][j]),
709*e4b17023SJohn Marino 0, ASLK_REDUCE_ALIGN);
710*e4b17023SJohn Marino
711*e4b17023SJohn Marino /* Setup single word save area just in case... */
712*e4b17023SJohn Marino for (k = 0; k < j; k++)
713*e4b17023SJohn Marino /* This should not depend on WORDS_BIG_ENDIAN.
714*e4b17023SJohn Marino The order of words in regs is the same as in memory. */
715*e4b17023SJohn Marino regno_save_mem[i + k][1]
716*e4b17023SJohn Marino = adjust_address_nv (regno_save_mem[i][j],
717*e4b17023SJohn Marino regno_save_mode[i + k][1],
718*e4b17023SJohn Marino k * UNITS_PER_WORD);
719*e4b17023SJohn Marino }
720*e4b17023SJohn Marino }
721*e4b17023SJohn Marino
722*e4b17023SJohn Marino /* Now loop again and set the alias set of any save areas we made to
723*e4b17023SJohn Marino the alias set used to represent frame objects. */
724*e4b17023SJohn Marino for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
725*e4b17023SJohn Marino for (j = MOVE_MAX_WORDS; j > 0; j--)
726*e4b17023SJohn Marino if (regno_save_mem[i][j] != 0)
727*e4b17023SJohn Marino set_mem_alias_set (regno_save_mem[i][j], get_frame_alias_set ());
728*e4b17023SJohn Marino }
729*e4b17023SJohn Marino
730*e4b17023SJohn Marino
731*e4b17023SJohn Marino
732*e4b17023SJohn Marino /* Find the places where hard regs are live across calls and save them. */
733*e4b17023SJohn Marino
734*e4b17023SJohn Marino void
save_call_clobbered_regs(void)735*e4b17023SJohn Marino save_call_clobbered_regs (void)
736*e4b17023SJohn Marino {
737*e4b17023SJohn Marino struct insn_chain *chain, *next, *last = NULL;
738*e4b17023SJohn Marino enum machine_mode save_mode [FIRST_PSEUDO_REGISTER];
739*e4b17023SJohn Marino
740*e4b17023SJohn Marino /* Computed in mark_set_regs, holds all registers set by the current
741*e4b17023SJohn Marino instruction. */
742*e4b17023SJohn Marino HARD_REG_SET this_insn_sets;
743*e4b17023SJohn Marino
744*e4b17023SJohn Marino CLEAR_HARD_REG_SET (hard_regs_saved);
745*e4b17023SJohn Marino n_regs_saved = 0;
746*e4b17023SJohn Marino
747*e4b17023SJohn Marino for (chain = reload_insn_chain; chain != 0; chain = next)
748*e4b17023SJohn Marino {
749*e4b17023SJohn Marino rtx insn = chain->insn;
750*e4b17023SJohn Marino enum rtx_code code = GET_CODE (insn);
751*e4b17023SJohn Marino
752*e4b17023SJohn Marino next = chain->next;
753*e4b17023SJohn Marino
754*e4b17023SJohn Marino gcc_assert (!chain->is_caller_save_insn);
755*e4b17023SJohn Marino
756*e4b17023SJohn Marino if (NONDEBUG_INSN_P (insn))
757*e4b17023SJohn Marino {
758*e4b17023SJohn Marino /* If some registers have been saved, see if INSN references
759*e4b17023SJohn Marino any of them. We must restore them before the insn if so. */
760*e4b17023SJohn Marino
761*e4b17023SJohn Marino if (n_regs_saved)
762*e4b17023SJohn Marino {
763*e4b17023SJohn Marino int regno;
764*e4b17023SJohn Marino HARD_REG_SET this_insn_sets;
765*e4b17023SJohn Marino
766*e4b17023SJohn Marino if (code == JUMP_INSN)
767*e4b17023SJohn Marino /* Restore all registers if this is a JUMP_INSN. */
768*e4b17023SJohn Marino COPY_HARD_REG_SET (referenced_regs, hard_regs_saved);
769*e4b17023SJohn Marino else
770*e4b17023SJohn Marino {
771*e4b17023SJohn Marino CLEAR_HARD_REG_SET (referenced_regs);
772*e4b17023SJohn Marino mark_referenced_regs (&PATTERN (insn),
773*e4b17023SJohn Marino mark_reg_as_referenced, NULL);
774*e4b17023SJohn Marino AND_HARD_REG_SET (referenced_regs, hard_regs_saved);
775*e4b17023SJohn Marino }
776*e4b17023SJohn Marino
777*e4b17023SJohn Marino for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
778*e4b17023SJohn Marino if (TEST_HARD_REG_BIT (referenced_regs, regno))
779*e4b17023SJohn Marino regno += insert_restore (chain, 1, regno, MOVE_MAX_WORDS,
780*e4b17023SJohn Marino save_mode);
781*e4b17023SJohn Marino /* If a saved register is set after the call, this means we no
782*e4b17023SJohn Marino longer should restore it. This can happen when parts of a
783*e4b17023SJohn Marino multi-word pseudo do not conflict with other pseudos, so
784*e4b17023SJohn Marino IRA may allocate the same hard register for both. One may
785*e4b17023SJohn Marino be live across the call, while the other is set
786*e4b17023SJohn Marino afterwards. */
787*e4b17023SJohn Marino CLEAR_HARD_REG_SET (this_insn_sets);
788*e4b17023SJohn Marino note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
789*e4b17023SJohn Marino AND_COMPL_HARD_REG_SET (hard_regs_saved, this_insn_sets);
790*e4b17023SJohn Marino }
791*e4b17023SJohn Marino
792*e4b17023SJohn Marino if (code == CALL_INSN
793*e4b17023SJohn Marino && ! SIBLING_CALL_P (insn)
794*e4b17023SJohn Marino && ! find_reg_note (insn, REG_NORETURN, NULL))
795*e4b17023SJohn Marino {
796*e4b17023SJohn Marino unsigned regno;
797*e4b17023SJohn Marino HARD_REG_SET hard_regs_to_save;
798*e4b17023SJohn Marino reg_set_iterator rsi;
799*e4b17023SJohn Marino
800*e4b17023SJohn Marino /* Use the register life information in CHAIN to compute which
801*e4b17023SJohn Marino regs are live during the call. */
802*e4b17023SJohn Marino REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
803*e4b17023SJohn Marino &chain->live_throughout);
804*e4b17023SJohn Marino /* Save hard registers always in the widest mode available. */
805*e4b17023SJohn Marino for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
806*e4b17023SJohn Marino if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
807*e4b17023SJohn Marino save_mode [regno] = regno_save_mode [regno][1];
808*e4b17023SJohn Marino else
809*e4b17023SJohn Marino save_mode [regno] = VOIDmode;
810*e4b17023SJohn Marino
811*e4b17023SJohn Marino /* Look through all live pseudos, mark their hard registers
812*e4b17023SJohn Marino and choose proper mode for saving. */
813*e4b17023SJohn Marino EXECUTE_IF_SET_IN_REG_SET
814*e4b17023SJohn Marino (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
815*e4b17023SJohn Marino {
816*e4b17023SJohn Marino int r = reg_renumber[regno];
817*e4b17023SJohn Marino int nregs;
818*e4b17023SJohn Marino enum machine_mode mode;
819*e4b17023SJohn Marino
820*e4b17023SJohn Marino if (r < 0)
821*e4b17023SJohn Marino continue;
822*e4b17023SJohn Marino nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
823*e4b17023SJohn Marino mode = HARD_REGNO_CALLER_SAVE_MODE
824*e4b17023SJohn Marino (r, nregs, PSEUDO_REGNO_MODE (regno));
825*e4b17023SJohn Marino if (GET_MODE_BITSIZE (mode)
826*e4b17023SJohn Marino > GET_MODE_BITSIZE (save_mode[r]))
827*e4b17023SJohn Marino save_mode[r] = mode;
828*e4b17023SJohn Marino while (nregs-- > 0)
829*e4b17023SJohn Marino SET_HARD_REG_BIT (hard_regs_to_save, r + nregs);
830*e4b17023SJohn Marino }
831*e4b17023SJohn Marino
832*e4b17023SJohn Marino /* Record all registers set in this call insn. These don't need
833*e4b17023SJohn Marino to be saved. N.B. the call insn might set a subreg of a
834*e4b17023SJohn Marino multi-hard-reg pseudo; then the pseudo is considered live
835*e4b17023SJohn Marino during the call, but the subreg that is set isn't. */
836*e4b17023SJohn Marino CLEAR_HARD_REG_SET (this_insn_sets);
837*e4b17023SJohn Marino note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
838*e4b17023SJohn Marino
839*e4b17023SJohn Marino /* Compute which hard regs must be saved before this call. */
840*e4b17023SJohn Marino AND_COMPL_HARD_REG_SET (hard_regs_to_save, call_fixed_reg_set);
841*e4b17023SJohn Marino AND_COMPL_HARD_REG_SET (hard_regs_to_save, this_insn_sets);
842*e4b17023SJohn Marino AND_COMPL_HARD_REG_SET (hard_regs_to_save, hard_regs_saved);
843*e4b17023SJohn Marino AND_HARD_REG_SET (hard_regs_to_save, call_used_reg_set);
844*e4b17023SJohn Marino
845*e4b17023SJohn Marino for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
846*e4b17023SJohn Marino if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
847*e4b17023SJohn Marino regno += insert_save (chain, 1, regno, &hard_regs_to_save, save_mode);
848*e4b17023SJohn Marino
849*e4b17023SJohn Marino /* Must recompute n_regs_saved. */
850*e4b17023SJohn Marino n_regs_saved = 0;
851*e4b17023SJohn Marino for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
852*e4b17023SJohn Marino if (TEST_HARD_REG_BIT (hard_regs_saved, regno))
853*e4b17023SJohn Marino n_regs_saved++;
854*e4b17023SJohn Marino }
855*e4b17023SJohn Marino last = chain;
856*e4b17023SJohn Marino }
857*e4b17023SJohn Marino else if (DEBUG_INSN_P (insn) && n_regs_saved)
858*e4b17023SJohn Marino mark_referenced_regs (&PATTERN (insn),
859*e4b17023SJohn Marino replace_reg_with_saved_mem,
860*e4b17023SJohn Marino save_mode);
861*e4b17023SJohn Marino
862*e4b17023SJohn Marino if (chain->next == 0 || chain->next->block != chain->block)
863*e4b17023SJohn Marino {
864*e4b17023SJohn Marino int regno;
865*e4b17023SJohn Marino /* At the end of the basic block, we must restore any registers that
866*e4b17023SJohn Marino remain saved. If the last insn in the block is a JUMP_INSN, put
867*e4b17023SJohn Marino the restore before the insn, otherwise, put it after the insn. */
868*e4b17023SJohn Marino
869*e4b17023SJohn Marino if (n_regs_saved
870*e4b17023SJohn Marino && DEBUG_INSN_P (insn)
871*e4b17023SJohn Marino && last
872*e4b17023SJohn Marino && last->block == chain->block)
873*e4b17023SJohn Marino {
874*e4b17023SJohn Marino rtx ins, prev;
875*e4b17023SJohn Marino basic_block bb = BLOCK_FOR_INSN (insn);
876*e4b17023SJohn Marino
877*e4b17023SJohn Marino /* When adding hard reg restores after a DEBUG_INSN, move
878*e4b17023SJohn Marino all notes between last real insn and this DEBUG_INSN after
879*e4b17023SJohn Marino the DEBUG_INSN, otherwise we could get code
880*e4b17023SJohn Marino -g/-g0 differences. */
881*e4b17023SJohn Marino for (ins = PREV_INSN (insn); ins != last->insn; ins = prev)
882*e4b17023SJohn Marino {
883*e4b17023SJohn Marino prev = PREV_INSN (ins);
884*e4b17023SJohn Marino if (NOTE_P (ins))
885*e4b17023SJohn Marino {
886*e4b17023SJohn Marino NEXT_INSN (prev) = NEXT_INSN (ins);
887*e4b17023SJohn Marino PREV_INSN (NEXT_INSN (ins)) = prev;
888*e4b17023SJohn Marino PREV_INSN (ins) = insn;
889*e4b17023SJohn Marino NEXT_INSN (ins) = NEXT_INSN (insn);
890*e4b17023SJohn Marino NEXT_INSN (insn) = ins;
891*e4b17023SJohn Marino if (NEXT_INSN (ins))
892*e4b17023SJohn Marino PREV_INSN (NEXT_INSN (ins)) = ins;
893*e4b17023SJohn Marino if (BB_END (bb) == insn)
894*e4b17023SJohn Marino BB_END (bb) = ins;
895*e4b17023SJohn Marino }
896*e4b17023SJohn Marino else
897*e4b17023SJohn Marino gcc_assert (DEBUG_INSN_P (ins));
898*e4b17023SJohn Marino }
899*e4b17023SJohn Marino }
900*e4b17023SJohn Marino last = NULL;
901*e4b17023SJohn Marino
902*e4b17023SJohn Marino if (n_regs_saved)
903*e4b17023SJohn Marino for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
904*e4b17023SJohn Marino if (TEST_HARD_REG_BIT (hard_regs_saved, regno))
905*e4b17023SJohn Marino regno += insert_restore (chain, JUMP_P (insn),
906*e4b17023SJohn Marino regno, MOVE_MAX_WORDS, save_mode);
907*e4b17023SJohn Marino }
908*e4b17023SJohn Marino }
909*e4b17023SJohn Marino }
910*e4b17023SJohn Marino
911*e4b17023SJohn Marino /* Here from note_stores, or directly from save_call_clobbered_regs, when
912*e4b17023SJohn Marino an insn stores a value in a register.
913*e4b17023SJohn Marino Set the proper bit or bits in this_insn_sets. All pseudos that have
914*e4b17023SJohn Marino been assigned hard regs have had their register number changed already,
915*e4b17023SJohn Marino so we can ignore pseudos. */
916*e4b17023SJohn Marino static void
mark_set_regs(rtx reg,const_rtx setter ATTRIBUTE_UNUSED,void * data)917*e4b17023SJohn Marino mark_set_regs (rtx reg, const_rtx setter ATTRIBUTE_UNUSED, void *data)
918*e4b17023SJohn Marino {
919*e4b17023SJohn Marino int regno, endregno, i;
920*e4b17023SJohn Marino HARD_REG_SET *this_insn_sets = (HARD_REG_SET *) data;
921*e4b17023SJohn Marino
922*e4b17023SJohn Marino if (GET_CODE (reg) == SUBREG)
923*e4b17023SJohn Marino {
924*e4b17023SJohn Marino rtx inner = SUBREG_REG (reg);
925*e4b17023SJohn Marino if (!REG_P (inner) || REGNO (inner) >= FIRST_PSEUDO_REGISTER)
926*e4b17023SJohn Marino return;
927*e4b17023SJohn Marino regno = subreg_regno (reg);
928*e4b17023SJohn Marino endregno = regno + subreg_nregs (reg);
929*e4b17023SJohn Marino }
930*e4b17023SJohn Marino else if (REG_P (reg)
931*e4b17023SJohn Marino && REGNO (reg) < FIRST_PSEUDO_REGISTER)
932*e4b17023SJohn Marino {
933*e4b17023SJohn Marino regno = REGNO (reg);
934*e4b17023SJohn Marino endregno = END_HARD_REGNO (reg);
935*e4b17023SJohn Marino }
936*e4b17023SJohn Marino else
937*e4b17023SJohn Marino return;
938*e4b17023SJohn Marino
939*e4b17023SJohn Marino for (i = regno; i < endregno; i++)
940*e4b17023SJohn Marino SET_HARD_REG_BIT (*this_insn_sets, i);
941*e4b17023SJohn Marino }
942*e4b17023SJohn Marino
943*e4b17023SJohn Marino /* Here from note_stores when an insn stores a value in a register.
944*e4b17023SJohn Marino Set the proper bit or bits in the passed regset. All pseudos that have
945*e4b17023SJohn Marino been assigned hard regs have had their register number changed already,
946*e4b17023SJohn Marino so we can ignore pseudos. */
947*e4b17023SJohn Marino static void
add_stored_regs(rtx reg,const_rtx setter,void * data)948*e4b17023SJohn Marino add_stored_regs (rtx reg, const_rtx setter, void *data)
949*e4b17023SJohn Marino {
950*e4b17023SJohn Marino int regno, endregno, i;
951*e4b17023SJohn Marino enum machine_mode mode = GET_MODE (reg);
952*e4b17023SJohn Marino int offset = 0;
953*e4b17023SJohn Marino
954*e4b17023SJohn Marino if (GET_CODE (setter) == CLOBBER)
955*e4b17023SJohn Marino return;
956*e4b17023SJohn Marino
957*e4b17023SJohn Marino if (GET_CODE (reg) == SUBREG
958*e4b17023SJohn Marino && REG_P (SUBREG_REG (reg))
959*e4b17023SJohn Marino && REGNO (SUBREG_REG (reg)) < FIRST_PSEUDO_REGISTER)
960*e4b17023SJohn Marino {
961*e4b17023SJohn Marino offset = subreg_regno_offset (REGNO (SUBREG_REG (reg)),
962*e4b17023SJohn Marino GET_MODE (SUBREG_REG (reg)),
963*e4b17023SJohn Marino SUBREG_BYTE (reg),
964*e4b17023SJohn Marino GET_MODE (reg));
965*e4b17023SJohn Marino regno = REGNO (SUBREG_REG (reg)) + offset;
966*e4b17023SJohn Marino endregno = regno + subreg_nregs (reg);
967*e4b17023SJohn Marino }
968*e4b17023SJohn Marino else
969*e4b17023SJohn Marino {
970*e4b17023SJohn Marino if (!REG_P (reg) || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
971*e4b17023SJohn Marino return;
972*e4b17023SJohn Marino
973*e4b17023SJohn Marino regno = REGNO (reg) + offset;
974*e4b17023SJohn Marino endregno = end_hard_regno (mode, regno);
975*e4b17023SJohn Marino }
976*e4b17023SJohn Marino
977*e4b17023SJohn Marino for (i = regno; i < endregno; i++)
978*e4b17023SJohn Marino SET_REGNO_REG_SET ((regset) data, i);
979*e4b17023SJohn Marino }
980*e4b17023SJohn Marino
981*e4b17023SJohn Marino /* Walk X and record all referenced registers in REFERENCED_REGS. */
982*e4b17023SJohn Marino static void
mark_referenced_regs(rtx * loc,refmarker_fn * mark,void * arg)983*e4b17023SJohn Marino mark_referenced_regs (rtx *loc, refmarker_fn *mark, void *arg)
984*e4b17023SJohn Marino {
985*e4b17023SJohn Marino enum rtx_code code = GET_CODE (*loc);
986*e4b17023SJohn Marino const char *fmt;
987*e4b17023SJohn Marino int i, j;
988*e4b17023SJohn Marino
989*e4b17023SJohn Marino if (code == SET)
990*e4b17023SJohn Marino mark_referenced_regs (&SET_SRC (*loc), mark, arg);
991*e4b17023SJohn Marino if (code == SET || code == CLOBBER)
992*e4b17023SJohn Marino {
993*e4b17023SJohn Marino loc = &SET_DEST (*loc);
994*e4b17023SJohn Marino code = GET_CODE (*loc);
995*e4b17023SJohn Marino if ((code == REG && REGNO (*loc) < FIRST_PSEUDO_REGISTER)
996*e4b17023SJohn Marino || code == PC || code == CC0
997*e4b17023SJohn Marino || (code == SUBREG && REG_P (SUBREG_REG (*loc))
998*e4b17023SJohn Marino && REGNO (SUBREG_REG (*loc)) < FIRST_PSEUDO_REGISTER
999*e4b17023SJohn Marino /* If we're setting only part of a multi-word register,
1000*e4b17023SJohn Marino we shall mark it as referenced, because the words
1001*e4b17023SJohn Marino that are not being set should be restored. */
1002*e4b17023SJohn Marino && ((GET_MODE_SIZE (GET_MODE (*loc))
1003*e4b17023SJohn Marino >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (*loc))))
1004*e4b17023SJohn Marino || (GET_MODE_SIZE (GET_MODE (SUBREG_REG (*loc)))
1005*e4b17023SJohn Marino <= UNITS_PER_WORD))))
1006*e4b17023SJohn Marino return;
1007*e4b17023SJohn Marino }
1008*e4b17023SJohn Marino if (code == MEM || code == SUBREG)
1009*e4b17023SJohn Marino {
1010*e4b17023SJohn Marino loc = &XEXP (*loc, 0);
1011*e4b17023SJohn Marino code = GET_CODE (*loc);
1012*e4b17023SJohn Marino }
1013*e4b17023SJohn Marino
1014*e4b17023SJohn Marino if (code == REG)
1015*e4b17023SJohn Marino {
1016*e4b17023SJohn Marino int regno = REGNO (*loc);
1017*e4b17023SJohn Marino int hardregno = (regno < FIRST_PSEUDO_REGISTER ? regno
1018*e4b17023SJohn Marino : reg_renumber[regno]);
1019*e4b17023SJohn Marino
1020*e4b17023SJohn Marino if (hardregno >= 0)
1021*e4b17023SJohn Marino mark (loc, GET_MODE (*loc), hardregno, arg);
1022*e4b17023SJohn Marino else if (arg)
1023*e4b17023SJohn Marino /* ??? Will we ever end up with an equiv expression in a debug
1024*e4b17023SJohn Marino insn, that would have required restoring a reg, or will
1025*e4b17023SJohn Marino reload take care of it for us? */
1026*e4b17023SJohn Marino return;
1027*e4b17023SJohn Marino /* If this is a pseudo that did not get a hard register, scan its
1028*e4b17023SJohn Marino memory location, since it might involve the use of another
1029*e4b17023SJohn Marino register, which might be saved. */
1030*e4b17023SJohn Marino else if (reg_equiv_mem (regno) != 0)
1031*e4b17023SJohn Marino mark_referenced_regs (&XEXP (reg_equiv_mem (regno), 0), mark, arg);
1032*e4b17023SJohn Marino else if (reg_equiv_address (regno) != 0)
1033*e4b17023SJohn Marino mark_referenced_regs (®_equiv_address (regno), mark, arg);
1034*e4b17023SJohn Marino return;
1035*e4b17023SJohn Marino }
1036*e4b17023SJohn Marino
1037*e4b17023SJohn Marino fmt = GET_RTX_FORMAT (code);
1038*e4b17023SJohn Marino for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1039*e4b17023SJohn Marino {
1040*e4b17023SJohn Marino if (fmt[i] == 'e')
1041*e4b17023SJohn Marino mark_referenced_regs (&XEXP (*loc, i), mark, arg);
1042*e4b17023SJohn Marino else if (fmt[i] == 'E')
1043*e4b17023SJohn Marino for (j = XVECLEN (*loc, i) - 1; j >= 0; j--)
1044*e4b17023SJohn Marino mark_referenced_regs (&XVECEXP (*loc, i, j), mark, arg);
1045*e4b17023SJohn Marino }
1046*e4b17023SJohn Marino }
1047*e4b17023SJohn Marino
1048*e4b17023SJohn Marino /* Parameter function for mark_referenced_regs() that adds registers
1049*e4b17023SJohn Marino present in the insn and in equivalent mems and addresses to
1050*e4b17023SJohn Marino referenced_regs. */
1051*e4b17023SJohn Marino
1052*e4b17023SJohn Marino static void
mark_reg_as_referenced(rtx * loc ATTRIBUTE_UNUSED,enum machine_mode mode,int hardregno,void * arg ATTRIBUTE_UNUSED)1053*e4b17023SJohn Marino mark_reg_as_referenced (rtx *loc ATTRIBUTE_UNUSED,
1054*e4b17023SJohn Marino enum machine_mode mode,
1055*e4b17023SJohn Marino int hardregno,
1056*e4b17023SJohn Marino void *arg ATTRIBUTE_UNUSED)
1057*e4b17023SJohn Marino {
1058*e4b17023SJohn Marino add_to_hard_reg_set (&referenced_regs, mode, hardregno);
1059*e4b17023SJohn Marino }
1060*e4b17023SJohn Marino
1061*e4b17023SJohn Marino /* Parameter function for mark_referenced_regs() that replaces
1062*e4b17023SJohn Marino registers referenced in a debug_insn that would have been restored,
1063*e4b17023SJohn Marino should it be a non-debug_insn, with their save locations. */
1064*e4b17023SJohn Marino
1065*e4b17023SJohn Marino static void
replace_reg_with_saved_mem(rtx * loc,enum machine_mode mode,int regno,void * arg)1066*e4b17023SJohn Marino replace_reg_with_saved_mem (rtx *loc,
1067*e4b17023SJohn Marino enum machine_mode mode,
1068*e4b17023SJohn Marino int regno,
1069*e4b17023SJohn Marino void *arg)
1070*e4b17023SJohn Marino {
1071*e4b17023SJohn Marino unsigned int i, nregs = hard_regno_nregs [regno][mode];
1072*e4b17023SJohn Marino rtx mem;
1073*e4b17023SJohn Marino enum machine_mode *save_mode = (enum machine_mode *)arg;
1074*e4b17023SJohn Marino
1075*e4b17023SJohn Marino for (i = 0; i < nregs; i++)
1076*e4b17023SJohn Marino if (TEST_HARD_REG_BIT (hard_regs_saved, regno + i))
1077*e4b17023SJohn Marino break;
1078*e4b17023SJohn Marino
1079*e4b17023SJohn Marino /* If none of the registers in the range would need restoring, we're
1080*e4b17023SJohn Marino all set. */
1081*e4b17023SJohn Marino if (i == nregs)
1082*e4b17023SJohn Marino return;
1083*e4b17023SJohn Marino
1084*e4b17023SJohn Marino while (++i < nregs)
1085*e4b17023SJohn Marino if (!TEST_HARD_REG_BIT (hard_regs_saved, regno + i))
1086*e4b17023SJohn Marino break;
1087*e4b17023SJohn Marino
1088*e4b17023SJohn Marino if (i == nregs
1089*e4b17023SJohn Marino && regno_save_mem[regno][nregs])
1090*e4b17023SJohn Marino {
1091*e4b17023SJohn Marino mem = copy_rtx (regno_save_mem[regno][nregs]);
1092*e4b17023SJohn Marino
1093*e4b17023SJohn Marino if (nregs == (unsigned int) hard_regno_nregs[regno][save_mode[regno]])
1094*e4b17023SJohn Marino mem = adjust_address_nv (mem, save_mode[regno], 0);
1095*e4b17023SJohn Marino
1096*e4b17023SJohn Marino if (GET_MODE (mem) != mode)
1097*e4b17023SJohn Marino {
1098*e4b17023SJohn Marino /* This is gen_lowpart_if_possible(), but without validating
1099*e4b17023SJohn Marino the newly-formed address. */
1100*e4b17023SJohn Marino int offset = 0;
1101*e4b17023SJohn Marino
1102*e4b17023SJohn Marino if (WORDS_BIG_ENDIAN)
1103*e4b17023SJohn Marino offset = (MAX (GET_MODE_SIZE (GET_MODE (mem)), UNITS_PER_WORD)
1104*e4b17023SJohn Marino - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
1105*e4b17023SJohn Marino if (BYTES_BIG_ENDIAN)
1106*e4b17023SJohn Marino /* Adjust the address so that the address-after-the-data is
1107*e4b17023SJohn Marino unchanged. */
1108*e4b17023SJohn Marino offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
1109*e4b17023SJohn Marino - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (mem))));
1110*e4b17023SJohn Marino
1111*e4b17023SJohn Marino mem = adjust_address_nv (mem, mode, offset);
1112*e4b17023SJohn Marino }
1113*e4b17023SJohn Marino }
1114*e4b17023SJohn Marino else
1115*e4b17023SJohn Marino {
1116*e4b17023SJohn Marino mem = gen_rtx_CONCATN (mode, rtvec_alloc (nregs));
1117*e4b17023SJohn Marino for (i = 0; i < nregs; i++)
1118*e4b17023SJohn Marino if (TEST_HARD_REG_BIT (hard_regs_saved, regno + i))
1119*e4b17023SJohn Marino {
1120*e4b17023SJohn Marino gcc_assert (regno_save_mem[regno + i][1]);
1121*e4b17023SJohn Marino XVECEXP (mem, 0, i) = copy_rtx (regno_save_mem[regno + i][1]);
1122*e4b17023SJohn Marino }
1123*e4b17023SJohn Marino else
1124*e4b17023SJohn Marino {
1125*e4b17023SJohn Marino gcc_assert (save_mode[regno] != VOIDmode);
1126*e4b17023SJohn Marino XVECEXP (mem, 0, i) = gen_rtx_REG (save_mode [regno],
1127*e4b17023SJohn Marino regno + i);
1128*e4b17023SJohn Marino }
1129*e4b17023SJohn Marino }
1130*e4b17023SJohn Marino
1131*e4b17023SJohn Marino gcc_assert (GET_MODE (mem) == mode);
1132*e4b17023SJohn Marino *loc = mem;
1133*e4b17023SJohn Marino }
1134*e4b17023SJohn Marino
1135*e4b17023SJohn Marino
1136*e4b17023SJohn Marino /* Insert a sequence of insns to restore. Place these insns in front of
1137*e4b17023SJohn Marino CHAIN if BEFORE_P is nonzero, behind the insn otherwise. MAXRESTORE is
1138*e4b17023SJohn Marino the maximum number of registers which should be restored during this call.
1139*e4b17023SJohn Marino It should never be less than 1 since we only work with entire registers.
1140*e4b17023SJohn Marino
1141*e4b17023SJohn Marino Note that we have verified in init_caller_save that we can do this
1142*e4b17023SJohn Marino with a simple SET, so use it. Set INSN_CODE to what we save there
1143*e4b17023SJohn Marino since the address might not be valid so the insn might not be recognized.
1144*e4b17023SJohn Marino These insns will be reloaded and have register elimination done by
1145*e4b17023SJohn Marino find_reload, so we need not worry about that here.
1146*e4b17023SJohn Marino
1147*e4b17023SJohn Marino Return the extra number of registers saved. */
1148*e4b17023SJohn Marino
1149*e4b17023SJohn Marino static int
insert_restore(struct insn_chain * chain,int before_p,int regno,int maxrestore,enum machine_mode * save_mode)1150*e4b17023SJohn Marino insert_restore (struct insn_chain *chain, int before_p, int regno,
1151*e4b17023SJohn Marino int maxrestore, enum machine_mode *save_mode)
1152*e4b17023SJohn Marino {
1153*e4b17023SJohn Marino int i, k;
1154*e4b17023SJohn Marino rtx pat = NULL_RTX;
1155*e4b17023SJohn Marino int code;
1156*e4b17023SJohn Marino unsigned int numregs = 0;
1157*e4b17023SJohn Marino struct insn_chain *new_chain;
1158*e4b17023SJohn Marino rtx mem;
1159*e4b17023SJohn Marino
1160*e4b17023SJohn Marino /* A common failure mode if register status is not correct in the
1161*e4b17023SJohn Marino RTL is for this routine to be called with a REGNO we didn't
1162*e4b17023SJohn Marino expect to save. That will cause us to write an insn with a (nil)
1163*e4b17023SJohn Marino SET_DEST or SET_SRC. Instead of doing so and causing a crash
1164*e4b17023SJohn Marino later, check for this common case here instead. This will remove
1165*e4b17023SJohn Marino one step in debugging such problems. */
1166*e4b17023SJohn Marino gcc_assert (regno_save_mem[regno][1]);
1167*e4b17023SJohn Marino
1168*e4b17023SJohn Marino /* Get the pattern to emit and update our status.
1169*e4b17023SJohn Marino
1170*e4b17023SJohn Marino See if we can restore `maxrestore' registers at once. Work
1171*e4b17023SJohn Marino backwards to the single register case. */
1172*e4b17023SJohn Marino for (i = maxrestore; i > 0; i--)
1173*e4b17023SJohn Marino {
1174*e4b17023SJohn Marino int j;
1175*e4b17023SJohn Marino int ok = 1;
1176*e4b17023SJohn Marino
1177*e4b17023SJohn Marino if (regno_save_mem[regno][i] == 0)
1178*e4b17023SJohn Marino continue;
1179*e4b17023SJohn Marino
1180*e4b17023SJohn Marino for (j = 0; j < i; j++)
1181*e4b17023SJohn Marino if (! TEST_HARD_REG_BIT (hard_regs_saved, regno + j))
1182*e4b17023SJohn Marino {
1183*e4b17023SJohn Marino ok = 0;
1184*e4b17023SJohn Marino break;
1185*e4b17023SJohn Marino }
1186*e4b17023SJohn Marino /* Must do this one restore at a time. */
1187*e4b17023SJohn Marino if (! ok)
1188*e4b17023SJohn Marino continue;
1189*e4b17023SJohn Marino
1190*e4b17023SJohn Marino numregs = i;
1191*e4b17023SJohn Marino break;
1192*e4b17023SJohn Marino }
1193*e4b17023SJohn Marino
1194*e4b17023SJohn Marino mem = regno_save_mem [regno][numregs];
1195*e4b17023SJohn Marino if (save_mode [regno] != VOIDmode
1196*e4b17023SJohn Marino && save_mode [regno] != GET_MODE (mem)
1197*e4b17023SJohn Marino && numregs == (unsigned int) hard_regno_nregs[regno][save_mode [regno]]
1198*e4b17023SJohn Marino /* Check that insn to restore REGNO in save_mode[regno] is
1199*e4b17023SJohn Marino correct. */
1200*e4b17023SJohn Marino && reg_save_code (regno, save_mode[regno]) >= 0)
1201*e4b17023SJohn Marino mem = adjust_address_nv (mem, save_mode[regno], 0);
1202*e4b17023SJohn Marino else
1203*e4b17023SJohn Marino mem = copy_rtx (mem);
1204*e4b17023SJohn Marino
1205*e4b17023SJohn Marino /* Verify that the alignment of spill space is equal to or greater
1206*e4b17023SJohn Marino than required. */
1207*e4b17023SJohn Marino gcc_assert (MIN (MAX_SUPPORTED_STACK_ALIGNMENT,
1208*e4b17023SJohn Marino GET_MODE_ALIGNMENT (GET_MODE (mem))) <= MEM_ALIGN (mem));
1209*e4b17023SJohn Marino
1210*e4b17023SJohn Marino pat = gen_rtx_SET (VOIDmode,
1211*e4b17023SJohn Marino gen_rtx_REG (GET_MODE (mem),
1212*e4b17023SJohn Marino regno), mem);
1213*e4b17023SJohn Marino code = reg_restore_code (regno, GET_MODE (mem));
1214*e4b17023SJohn Marino new_chain = insert_one_insn (chain, before_p, code, pat);
1215*e4b17023SJohn Marino
1216*e4b17023SJohn Marino /* Clear status for all registers we restored. */
1217*e4b17023SJohn Marino for (k = 0; k < i; k++)
1218*e4b17023SJohn Marino {
1219*e4b17023SJohn Marino CLEAR_HARD_REG_BIT (hard_regs_saved, regno + k);
1220*e4b17023SJohn Marino SET_REGNO_REG_SET (&new_chain->dead_or_set, regno + k);
1221*e4b17023SJohn Marino n_regs_saved--;
1222*e4b17023SJohn Marino }
1223*e4b17023SJohn Marino
1224*e4b17023SJohn Marino /* Tell our callers how many extra registers we saved/restored. */
1225*e4b17023SJohn Marino return numregs - 1;
1226*e4b17023SJohn Marino }
1227*e4b17023SJohn Marino
1228*e4b17023SJohn Marino /* Like insert_restore above, but save registers instead. */
1229*e4b17023SJohn Marino
1230*e4b17023SJohn Marino static int
insert_save(struct insn_chain * chain,int before_p,int regno,HARD_REG_SET (* to_save),enum machine_mode * save_mode)1231*e4b17023SJohn Marino insert_save (struct insn_chain *chain, int before_p, int regno,
1232*e4b17023SJohn Marino HARD_REG_SET (*to_save), enum machine_mode *save_mode)
1233*e4b17023SJohn Marino {
1234*e4b17023SJohn Marino int i;
1235*e4b17023SJohn Marino unsigned int k;
1236*e4b17023SJohn Marino rtx pat = NULL_RTX;
1237*e4b17023SJohn Marino int code;
1238*e4b17023SJohn Marino unsigned int numregs = 0;
1239*e4b17023SJohn Marino struct insn_chain *new_chain;
1240*e4b17023SJohn Marino rtx mem;
1241*e4b17023SJohn Marino
1242*e4b17023SJohn Marino /* A common failure mode if register status is not correct in the
1243*e4b17023SJohn Marino RTL is for this routine to be called with a REGNO we didn't
1244*e4b17023SJohn Marino expect to save. That will cause us to write an insn with a (nil)
1245*e4b17023SJohn Marino SET_DEST or SET_SRC. Instead of doing so and causing a crash
1246*e4b17023SJohn Marino later, check for this common case here. This will remove one
1247*e4b17023SJohn Marino step in debugging such problems. */
1248*e4b17023SJohn Marino gcc_assert (regno_save_mem[regno][1]);
1249*e4b17023SJohn Marino
1250*e4b17023SJohn Marino /* Get the pattern to emit and update our status.
1251*e4b17023SJohn Marino
1252*e4b17023SJohn Marino See if we can save several registers with a single instruction.
1253*e4b17023SJohn Marino Work backwards to the single register case. */
1254*e4b17023SJohn Marino for (i = MOVE_MAX_WORDS; i > 0; i--)
1255*e4b17023SJohn Marino {
1256*e4b17023SJohn Marino int j;
1257*e4b17023SJohn Marino int ok = 1;
1258*e4b17023SJohn Marino if (regno_save_mem[regno][i] == 0)
1259*e4b17023SJohn Marino continue;
1260*e4b17023SJohn Marino
1261*e4b17023SJohn Marino for (j = 0; j < i; j++)
1262*e4b17023SJohn Marino if (! TEST_HARD_REG_BIT (*to_save, regno + j))
1263*e4b17023SJohn Marino {
1264*e4b17023SJohn Marino ok = 0;
1265*e4b17023SJohn Marino break;
1266*e4b17023SJohn Marino }
1267*e4b17023SJohn Marino /* Must do this one save at a time. */
1268*e4b17023SJohn Marino if (! ok)
1269*e4b17023SJohn Marino continue;
1270*e4b17023SJohn Marino
1271*e4b17023SJohn Marino numregs = i;
1272*e4b17023SJohn Marino break;
1273*e4b17023SJohn Marino }
1274*e4b17023SJohn Marino
1275*e4b17023SJohn Marino mem = regno_save_mem [regno][numregs];
1276*e4b17023SJohn Marino if (save_mode [regno] != VOIDmode
1277*e4b17023SJohn Marino && save_mode [regno] != GET_MODE (mem)
1278*e4b17023SJohn Marino && numregs == (unsigned int) hard_regno_nregs[regno][save_mode [regno]]
1279*e4b17023SJohn Marino /* Check that insn to save REGNO in save_mode[regno] is
1280*e4b17023SJohn Marino correct. */
1281*e4b17023SJohn Marino && reg_save_code (regno, save_mode[regno]) >= 0)
1282*e4b17023SJohn Marino mem = adjust_address_nv (mem, save_mode[regno], 0);
1283*e4b17023SJohn Marino else
1284*e4b17023SJohn Marino mem = copy_rtx (mem);
1285*e4b17023SJohn Marino
1286*e4b17023SJohn Marino /* Verify that the alignment of spill space is equal to or greater
1287*e4b17023SJohn Marino than required. */
1288*e4b17023SJohn Marino gcc_assert (MIN (MAX_SUPPORTED_STACK_ALIGNMENT,
1289*e4b17023SJohn Marino GET_MODE_ALIGNMENT (GET_MODE (mem))) <= MEM_ALIGN (mem));
1290*e4b17023SJohn Marino
1291*e4b17023SJohn Marino pat = gen_rtx_SET (VOIDmode, mem,
1292*e4b17023SJohn Marino gen_rtx_REG (GET_MODE (mem),
1293*e4b17023SJohn Marino regno));
1294*e4b17023SJohn Marino code = reg_save_code (regno, GET_MODE (mem));
1295*e4b17023SJohn Marino new_chain = insert_one_insn (chain, before_p, code, pat);
1296*e4b17023SJohn Marino
1297*e4b17023SJohn Marino /* Set hard_regs_saved and dead_or_set for all the registers we saved. */
1298*e4b17023SJohn Marino for (k = 0; k < numregs; k++)
1299*e4b17023SJohn Marino {
1300*e4b17023SJohn Marino SET_HARD_REG_BIT (hard_regs_saved, regno + k);
1301*e4b17023SJohn Marino SET_REGNO_REG_SET (&new_chain->dead_or_set, regno + k);
1302*e4b17023SJohn Marino n_regs_saved++;
1303*e4b17023SJohn Marino }
1304*e4b17023SJohn Marino
1305*e4b17023SJohn Marino /* Tell our callers how many extra registers we saved/restored. */
1306*e4b17023SJohn Marino return numregs - 1;
1307*e4b17023SJohn Marino }
1308*e4b17023SJohn Marino
1309*e4b17023SJohn Marino /* A for_each_rtx callback used by add_used_regs. Add the hard-register
1310*e4b17023SJohn Marino equivalent of each REG to regset DATA. */
1311*e4b17023SJohn Marino
1312*e4b17023SJohn Marino static int
add_used_regs_1(rtx * loc,void * data)1313*e4b17023SJohn Marino add_used_regs_1 (rtx *loc, void *data)
1314*e4b17023SJohn Marino {
1315*e4b17023SJohn Marino unsigned int regno;
1316*e4b17023SJohn Marino regset live;
1317*e4b17023SJohn Marino rtx x;
1318*e4b17023SJohn Marino
1319*e4b17023SJohn Marino x = *loc;
1320*e4b17023SJohn Marino live = (regset) data;
1321*e4b17023SJohn Marino if (REG_P (x))
1322*e4b17023SJohn Marino {
1323*e4b17023SJohn Marino regno = REGNO (x);
1324*e4b17023SJohn Marino if (HARD_REGISTER_NUM_P (regno))
1325*e4b17023SJohn Marino bitmap_set_range (live, regno, hard_regno_nregs[regno][GET_MODE (x)]);
1326*e4b17023SJohn Marino else
1327*e4b17023SJohn Marino regno = reg_renumber[regno];
1328*e4b17023SJohn Marino }
1329*e4b17023SJohn Marino return 0;
1330*e4b17023SJohn Marino }
1331*e4b17023SJohn Marino
1332*e4b17023SJohn Marino /* A note_uses callback used by insert_one_insn. Add the hard-register
1333*e4b17023SJohn Marino equivalent of each REG to regset DATA. */
1334*e4b17023SJohn Marino
1335*e4b17023SJohn Marino static void
add_used_regs(rtx * loc,void * data)1336*e4b17023SJohn Marino add_used_regs (rtx *loc, void *data)
1337*e4b17023SJohn Marino {
1338*e4b17023SJohn Marino for_each_rtx (loc, add_used_regs_1, data);
1339*e4b17023SJohn Marino }
1340*e4b17023SJohn Marino
1341*e4b17023SJohn Marino /* Emit a new caller-save insn and set the code. */
1342*e4b17023SJohn Marino static struct insn_chain *
insert_one_insn(struct insn_chain * chain,int before_p,int code,rtx pat)1343*e4b17023SJohn Marino insert_one_insn (struct insn_chain *chain, int before_p, int code, rtx pat)
1344*e4b17023SJohn Marino {
1345*e4b17023SJohn Marino rtx insn = chain->insn;
1346*e4b17023SJohn Marino struct insn_chain *new_chain;
1347*e4b17023SJohn Marino
1348*e4b17023SJohn Marino #ifdef HAVE_cc0
1349*e4b17023SJohn Marino /* If INSN references CC0, put our insns in front of the insn that sets
1350*e4b17023SJohn Marino CC0. This is always safe, since the only way we could be passed an
1351*e4b17023SJohn Marino insn that references CC0 is for a restore, and doing a restore earlier
1352*e4b17023SJohn Marino isn't a problem. We do, however, assume here that CALL_INSNs don't
1353*e4b17023SJohn Marino reference CC0. Guard against non-INSN's like CODE_LABEL. */
1354*e4b17023SJohn Marino
1355*e4b17023SJohn Marino if ((NONJUMP_INSN_P (insn) || JUMP_P (insn))
1356*e4b17023SJohn Marino && before_p
1357*e4b17023SJohn Marino && reg_referenced_p (cc0_rtx, PATTERN (insn)))
1358*e4b17023SJohn Marino chain = chain->prev, insn = chain->insn;
1359*e4b17023SJohn Marino #endif
1360*e4b17023SJohn Marino
1361*e4b17023SJohn Marino new_chain = new_insn_chain ();
1362*e4b17023SJohn Marino if (before_p)
1363*e4b17023SJohn Marino {
1364*e4b17023SJohn Marino rtx link;
1365*e4b17023SJohn Marino
1366*e4b17023SJohn Marino new_chain->prev = chain->prev;
1367*e4b17023SJohn Marino if (new_chain->prev != 0)
1368*e4b17023SJohn Marino new_chain->prev->next = new_chain;
1369*e4b17023SJohn Marino else
1370*e4b17023SJohn Marino reload_insn_chain = new_chain;
1371*e4b17023SJohn Marino
1372*e4b17023SJohn Marino chain->prev = new_chain;
1373*e4b17023SJohn Marino new_chain->next = chain;
1374*e4b17023SJohn Marino new_chain->insn = emit_insn_before (pat, insn);
1375*e4b17023SJohn Marino /* ??? It would be nice if we could exclude the already / still saved
1376*e4b17023SJohn Marino registers from the live sets. */
1377*e4b17023SJohn Marino COPY_REG_SET (&new_chain->live_throughout, &chain->live_throughout);
1378*e4b17023SJohn Marino note_uses (&PATTERN (chain->insn), add_used_regs,
1379*e4b17023SJohn Marino &new_chain->live_throughout);
1380*e4b17023SJohn Marino /* If CHAIN->INSN is a call, then the registers which contain
1381*e4b17023SJohn Marino the arguments to the function are live in the new insn. */
1382*e4b17023SJohn Marino if (CALL_P (chain->insn))
1383*e4b17023SJohn Marino for (link = CALL_INSN_FUNCTION_USAGE (chain->insn);
1384*e4b17023SJohn Marino link != NULL_RTX;
1385*e4b17023SJohn Marino link = XEXP (link, 1))
1386*e4b17023SJohn Marino note_uses (&XEXP (link, 0), add_used_regs,
1387*e4b17023SJohn Marino &new_chain->live_throughout);
1388*e4b17023SJohn Marino
1389*e4b17023SJohn Marino CLEAR_REG_SET (&new_chain->dead_or_set);
1390*e4b17023SJohn Marino if (chain->insn == BB_HEAD (BASIC_BLOCK (chain->block)))
1391*e4b17023SJohn Marino BB_HEAD (BASIC_BLOCK (chain->block)) = new_chain->insn;
1392*e4b17023SJohn Marino }
1393*e4b17023SJohn Marino else
1394*e4b17023SJohn Marino {
1395*e4b17023SJohn Marino new_chain->next = chain->next;
1396*e4b17023SJohn Marino if (new_chain->next != 0)
1397*e4b17023SJohn Marino new_chain->next->prev = new_chain;
1398*e4b17023SJohn Marino chain->next = new_chain;
1399*e4b17023SJohn Marino new_chain->prev = chain;
1400*e4b17023SJohn Marino new_chain->insn = emit_insn_after (pat, insn);
1401*e4b17023SJohn Marino /* ??? It would be nice if we could exclude the already / still saved
1402*e4b17023SJohn Marino registers from the live sets, and observe REG_UNUSED notes. */
1403*e4b17023SJohn Marino COPY_REG_SET (&new_chain->live_throughout, &chain->live_throughout);
1404*e4b17023SJohn Marino /* Registers that are set in CHAIN->INSN live in the new insn.
1405*e4b17023SJohn Marino (Unless there is a REG_UNUSED note for them, but we don't
1406*e4b17023SJohn Marino look for them here.) */
1407*e4b17023SJohn Marino note_stores (PATTERN (chain->insn), add_stored_regs,
1408*e4b17023SJohn Marino &new_chain->live_throughout);
1409*e4b17023SJohn Marino CLEAR_REG_SET (&new_chain->dead_or_set);
1410*e4b17023SJohn Marino if (chain->insn == BB_END (BASIC_BLOCK (chain->block)))
1411*e4b17023SJohn Marino BB_END (BASIC_BLOCK (chain->block)) = new_chain->insn;
1412*e4b17023SJohn Marino }
1413*e4b17023SJohn Marino new_chain->block = chain->block;
1414*e4b17023SJohn Marino new_chain->is_caller_save_insn = 1;
1415*e4b17023SJohn Marino
1416*e4b17023SJohn Marino INSN_CODE (new_chain->insn) = code;
1417*e4b17023SJohn Marino return new_chain;
1418*e4b17023SJohn Marino }
1419*e4b17023SJohn Marino #include "gt-caller-save.h"
1420