1*c87b03e5Sespie /* Register renaming for the GNU compiler.
2*c87b03e5Sespie Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
3*c87b03e5Sespie
4*c87b03e5Sespie This file is part of GCC.
5*c87b03e5Sespie
6*c87b03e5Sespie GCC is free software; you can redistribute it and/or modify it
7*c87b03e5Sespie under the terms of the GNU General Public License as published by
8*c87b03e5Sespie the Free Software Foundation; either version 2, or (at your option)
9*c87b03e5Sespie any later version.
10*c87b03e5Sespie
11*c87b03e5Sespie GCC is distributed in the hope that it will be useful, but WITHOUT
12*c87b03e5Sespie ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13*c87b03e5Sespie or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14*c87b03e5Sespie License for more details.
15*c87b03e5Sespie
16*c87b03e5Sespie You should have received a copy of the GNU General Public License
17*c87b03e5Sespie along with GCC; see the file COPYING. If not, write to the Free
18*c87b03e5Sespie Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19*c87b03e5Sespie 02111-1307, USA. */
20*c87b03e5Sespie
21*c87b03e5Sespie #define REG_OK_STRICT
22*c87b03e5Sespie
23*c87b03e5Sespie #include "config.h"
24*c87b03e5Sespie #include "system.h"
25*c87b03e5Sespie #include "rtl.h"
26*c87b03e5Sespie #include "tm_p.h"
27*c87b03e5Sespie #include "insn-config.h"
28*c87b03e5Sespie #include "regs.h"
29*c87b03e5Sespie #include "hard-reg-set.h"
30*c87b03e5Sespie #include "basic-block.h"
31*c87b03e5Sespie #include "reload.h"
32*c87b03e5Sespie #include "output.h"
33*c87b03e5Sespie #include "function.h"
34*c87b03e5Sespie #include "recog.h"
35*c87b03e5Sespie #include "flags.h"
36*c87b03e5Sespie #include "toplev.h"
37*c87b03e5Sespie #include "obstack.h"
38*c87b03e5Sespie
39*c87b03e5Sespie #ifndef REG_MODE_OK_FOR_BASE_P
40*c87b03e5Sespie #define REG_MODE_OK_FOR_BASE_P(REGNO, MODE) REG_OK_FOR_BASE_P (REGNO)
41*c87b03e5Sespie #endif
42*c87b03e5Sespie
43*c87b03e5Sespie static const char *const reg_class_names[] = REG_CLASS_NAMES;
44*c87b03e5Sespie
45*c87b03e5Sespie struct du_chain
46*c87b03e5Sespie {
47*c87b03e5Sespie struct du_chain *next_chain;
48*c87b03e5Sespie struct du_chain *next_use;
49*c87b03e5Sespie
50*c87b03e5Sespie rtx insn;
51*c87b03e5Sespie rtx *loc;
52*c87b03e5Sespie enum reg_class class;
53*c87b03e5Sespie unsigned int need_caller_save_reg:1;
54*c87b03e5Sespie unsigned int earlyclobber:1;
55*c87b03e5Sespie };
56*c87b03e5Sespie
57*c87b03e5Sespie enum scan_actions
58*c87b03e5Sespie {
59*c87b03e5Sespie terminate_all_read,
60*c87b03e5Sespie terminate_overlapping_read,
61*c87b03e5Sespie terminate_write,
62*c87b03e5Sespie terminate_dead,
63*c87b03e5Sespie mark_read,
64*c87b03e5Sespie mark_write
65*c87b03e5Sespie };
66*c87b03e5Sespie
67*c87b03e5Sespie static const char * const scan_actions_name[] =
68*c87b03e5Sespie {
69*c87b03e5Sespie "terminate_all_read",
70*c87b03e5Sespie "terminate_overlapping_read",
71*c87b03e5Sespie "terminate_write",
72*c87b03e5Sespie "terminate_dead",
73*c87b03e5Sespie "mark_read",
74*c87b03e5Sespie "mark_write"
75*c87b03e5Sespie };
76*c87b03e5Sespie
77*c87b03e5Sespie static struct obstack rename_obstack;
78*c87b03e5Sespie
79*c87b03e5Sespie static void do_replace PARAMS ((struct du_chain *, int));
80*c87b03e5Sespie static void scan_rtx_reg PARAMS ((rtx, rtx *, enum reg_class,
81*c87b03e5Sespie enum scan_actions, enum op_type, int));
82*c87b03e5Sespie static void scan_rtx_address PARAMS ((rtx, rtx *, enum reg_class,
83*c87b03e5Sespie enum scan_actions, enum machine_mode));
84*c87b03e5Sespie static void scan_rtx PARAMS ((rtx, rtx *, enum reg_class,
85*c87b03e5Sespie enum scan_actions, enum op_type, int));
86*c87b03e5Sespie static struct du_chain *build_def_use PARAMS ((basic_block));
87*c87b03e5Sespie static void dump_def_use_chain PARAMS ((struct du_chain *));
88*c87b03e5Sespie static void note_sets PARAMS ((rtx, rtx, void *));
89*c87b03e5Sespie static void clear_dead_regs PARAMS ((HARD_REG_SET *, enum machine_mode, rtx));
90*c87b03e5Sespie static void merge_overlapping_regs PARAMS ((basic_block, HARD_REG_SET *,
91*c87b03e5Sespie struct du_chain *));
92*c87b03e5Sespie
93*c87b03e5Sespie /* Called through note_stores from update_life. Find sets of registers, and
94*c87b03e5Sespie record them in *DATA (which is actually a HARD_REG_SET *). */
95*c87b03e5Sespie
96*c87b03e5Sespie static void
note_sets(x,set,data)97*c87b03e5Sespie note_sets (x, set, data)
98*c87b03e5Sespie rtx x;
99*c87b03e5Sespie rtx set ATTRIBUTE_UNUSED;
100*c87b03e5Sespie void *data;
101*c87b03e5Sespie {
102*c87b03e5Sespie HARD_REG_SET *pset = (HARD_REG_SET *) data;
103*c87b03e5Sespie unsigned int regno;
104*c87b03e5Sespie int nregs;
105*c87b03e5Sespie if (GET_CODE (x) != REG)
106*c87b03e5Sespie return;
107*c87b03e5Sespie regno = REGNO (x);
108*c87b03e5Sespie nregs = HARD_REGNO_NREGS (regno, GET_MODE (x));
109*c87b03e5Sespie
110*c87b03e5Sespie /* There must not be pseudos at this point. */
111*c87b03e5Sespie if (regno + nregs > FIRST_PSEUDO_REGISTER)
112*c87b03e5Sespie abort ();
113*c87b03e5Sespie
114*c87b03e5Sespie while (nregs-- > 0)
115*c87b03e5Sespie SET_HARD_REG_BIT (*pset, regno + nregs);
116*c87b03e5Sespie }
117*c87b03e5Sespie
118*c87b03e5Sespie /* Clear all registers from *PSET for which a note of kind KIND can be found
119*c87b03e5Sespie in the list NOTES. */
120*c87b03e5Sespie
121*c87b03e5Sespie static void
clear_dead_regs(pset,kind,notes)122*c87b03e5Sespie clear_dead_regs (pset, kind, notes)
123*c87b03e5Sespie HARD_REG_SET *pset;
124*c87b03e5Sespie enum machine_mode kind;
125*c87b03e5Sespie rtx notes;
126*c87b03e5Sespie {
127*c87b03e5Sespie rtx note;
128*c87b03e5Sespie for (note = notes; note; note = XEXP (note, 1))
129*c87b03e5Sespie if (REG_NOTE_KIND (note) == kind && REG_P (XEXP (note, 0)))
130*c87b03e5Sespie {
131*c87b03e5Sespie rtx reg = XEXP (note, 0);
132*c87b03e5Sespie unsigned int regno = REGNO (reg);
133*c87b03e5Sespie int nregs = HARD_REGNO_NREGS (regno, GET_MODE (reg));
134*c87b03e5Sespie
135*c87b03e5Sespie /* There must not be pseudos at this point. */
136*c87b03e5Sespie if (regno + nregs > FIRST_PSEUDO_REGISTER)
137*c87b03e5Sespie abort ();
138*c87b03e5Sespie
139*c87b03e5Sespie while (nregs-- > 0)
140*c87b03e5Sespie CLEAR_HARD_REG_BIT (*pset, regno + nregs);
141*c87b03e5Sespie }
142*c87b03e5Sespie }
143*c87b03e5Sespie
144*c87b03e5Sespie /* For a def-use chain CHAIN in basic block B, find which registers overlap
145*c87b03e5Sespie its lifetime and set the corresponding bits in *PSET. */
146*c87b03e5Sespie
147*c87b03e5Sespie static void
merge_overlapping_regs(b,pset,chain)148*c87b03e5Sespie merge_overlapping_regs (b, pset, chain)
149*c87b03e5Sespie basic_block b;
150*c87b03e5Sespie HARD_REG_SET *pset;
151*c87b03e5Sespie struct du_chain *chain;
152*c87b03e5Sespie {
153*c87b03e5Sespie struct du_chain *t = chain;
154*c87b03e5Sespie rtx insn;
155*c87b03e5Sespie HARD_REG_SET live;
156*c87b03e5Sespie
157*c87b03e5Sespie REG_SET_TO_HARD_REG_SET (live, b->global_live_at_start);
158*c87b03e5Sespie insn = b->head;
159*c87b03e5Sespie while (t)
160*c87b03e5Sespie {
161*c87b03e5Sespie /* Search forward until the next reference to the register to be
162*c87b03e5Sespie renamed. */
163*c87b03e5Sespie while (insn != t->insn)
164*c87b03e5Sespie {
165*c87b03e5Sespie if (INSN_P (insn))
166*c87b03e5Sespie {
167*c87b03e5Sespie clear_dead_regs (&live, REG_DEAD, REG_NOTES (insn));
168*c87b03e5Sespie note_stores (PATTERN (insn), note_sets, (void *) &live);
169*c87b03e5Sespie /* Only record currently live regs if we are inside the
170*c87b03e5Sespie reg's live range. */
171*c87b03e5Sespie if (t != chain)
172*c87b03e5Sespie IOR_HARD_REG_SET (*pset, live);
173*c87b03e5Sespie clear_dead_regs (&live, REG_UNUSED, REG_NOTES (insn));
174*c87b03e5Sespie }
175*c87b03e5Sespie insn = NEXT_INSN (insn);
176*c87b03e5Sespie }
177*c87b03e5Sespie
178*c87b03e5Sespie IOR_HARD_REG_SET (*pset, live);
179*c87b03e5Sespie
180*c87b03e5Sespie /* For the last reference, also merge in all registers set in the
181*c87b03e5Sespie same insn.
182*c87b03e5Sespie @@@ We only have take earlyclobbered sets into account. */
183*c87b03e5Sespie if (! t->next_use)
184*c87b03e5Sespie note_stores (PATTERN (insn), note_sets, (void *) pset);
185*c87b03e5Sespie
186*c87b03e5Sespie t = t->next_use;
187*c87b03e5Sespie }
188*c87b03e5Sespie }
189*c87b03e5Sespie
190*c87b03e5Sespie /* Perform register renaming on the current function. */
191*c87b03e5Sespie
192*c87b03e5Sespie void
regrename_optimize()193*c87b03e5Sespie regrename_optimize ()
194*c87b03e5Sespie {
195*c87b03e5Sespie int tick[FIRST_PSEUDO_REGISTER];
196*c87b03e5Sespie int this_tick = 0;
197*c87b03e5Sespie basic_block bb;
198*c87b03e5Sespie char *first_obj;
199*c87b03e5Sespie
200*c87b03e5Sespie memset (tick, 0, sizeof tick);
201*c87b03e5Sespie
202*c87b03e5Sespie gcc_obstack_init (&rename_obstack);
203*c87b03e5Sespie first_obj = (char *) obstack_alloc (&rename_obstack, 0);
204*c87b03e5Sespie
205*c87b03e5Sespie FOR_EACH_BB (bb)
206*c87b03e5Sespie {
207*c87b03e5Sespie struct du_chain *all_chains = 0;
208*c87b03e5Sespie HARD_REG_SET unavailable;
209*c87b03e5Sespie HARD_REG_SET regs_seen;
210*c87b03e5Sespie
211*c87b03e5Sespie CLEAR_HARD_REG_SET (unavailable);
212*c87b03e5Sespie
213*c87b03e5Sespie if (rtl_dump_file)
214*c87b03e5Sespie fprintf (rtl_dump_file, "\nBasic block %d:\n", bb->index);
215*c87b03e5Sespie
216*c87b03e5Sespie all_chains = build_def_use (bb);
217*c87b03e5Sespie
218*c87b03e5Sespie if (rtl_dump_file)
219*c87b03e5Sespie dump_def_use_chain (all_chains);
220*c87b03e5Sespie
221*c87b03e5Sespie CLEAR_HARD_REG_SET (unavailable);
222*c87b03e5Sespie /* Don't clobber traceback for noreturn functions. */
223*c87b03e5Sespie if (frame_pointer_needed)
224*c87b03e5Sespie {
225*c87b03e5Sespie int i;
226*c87b03e5Sespie
227*c87b03e5Sespie for (i = HARD_REGNO_NREGS (FRAME_POINTER_REGNUM, Pmode); i--;)
228*c87b03e5Sespie SET_HARD_REG_BIT (unavailable, FRAME_POINTER_REGNUM + i);
229*c87b03e5Sespie
230*c87b03e5Sespie #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
231*c87b03e5Sespie for (i = HARD_REGNO_NREGS (HARD_FRAME_POINTER_REGNUM, Pmode); i--;)
232*c87b03e5Sespie SET_HARD_REG_BIT (unavailable, HARD_FRAME_POINTER_REGNUM + i);
233*c87b03e5Sespie #endif
234*c87b03e5Sespie }
235*c87b03e5Sespie
236*c87b03e5Sespie CLEAR_HARD_REG_SET (regs_seen);
237*c87b03e5Sespie while (all_chains)
238*c87b03e5Sespie {
239*c87b03e5Sespie int new_reg, best_new_reg = -1;
240*c87b03e5Sespie int n_uses;
241*c87b03e5Sespie struct du_chain *this = all_chains;
242*c87b03e5Sespie struct du_chain *tmp, *last;
243*c87b03e5Sespie HARD_REG_SET this_unavailable;
244*c87b03e5Sespie int reg = REGNO (*this->loc);
245*c87b03e5Sespie int i;
246*c87b03e5Sespie
247*c87b03e5Sespie all_chains = this->next_chain;
248*c87b03e5Sespie
249*c87b03e5Sespie #if 0 /* This just disables optimization opportunities. */
250*c87b03e5Sespie /* Only rename once we've seen the reg more than once. */
251*c87b03e5Sespie if (! TEST_HARD_REG_BIT (regs_seen, reg))
252*c87b03e5Sespie {
253*c87b03e5Sespie SET_HARD_REG_BIT (regs_seen, reg);
254*c87b03e5Sespie continue;
255*c87b03e5Sespie }
256*c87b03e5Sespie #endif
257*c87b03e5Sespie
258*c87b03e5Sespie if (fixed_regs[reg] || global_regs[reg]
259*c87b03e5Sespie #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
260*c87b03e5Sespie || (frame_pointer_needed && reg == HARD_FRAME_POINTER_REGNUM)
261*c87b03e5Sespie #else
262*c87b03e5Sespie || (frame_pointer_needed && reg == FRAME_POINTER_REGNUM)
263*c87b03e5Sespie #endif
264*c87b03e5Sespie )
265*c87b03e5Sespie continue;
266*c87b03e5Sespie
267*c87b03e5Sespie COPY_HARD_REG_SET (this_unavailable, unavailable);
268*c87b03e5Sespie
269*c87b03e5Sespie /* Find last entry on chain (which has the need_caller_save bit),
270*c87b03e5Sespie count number of uses, and narrow the set of registers we can
271*c87b03e5Sespie use for renaming. */
272*c87b03e5Sespie n_uses = 0;
273*c87b03e5Sespie for (last = this; last->next_use; last = last->next_use)
274*c87b03e5Sespie {
275*c87b03e5Sespie n_uses++;
276*c87b03e5Sespie IOR_COMPL_HARD_REG_SET (this_unavailable,
277*c87b03e5Sespie reg_class_contents[last->class]);
278*c87b03e5Sespie }
279*c87b03e5Sespie if (n_uses < 1)
280*c87b03e5Sespie continue;
281*c87b03e5Sespie
282*c87b03e5Sespie IOR_COMPL_HARD_REG_SET (this_unavailable,
283*c87b03e5Sespie reg_class_contents[last->class]);
284*c87b03e5Sespie
285*c87b03e5Sespie if (this->need_caller_save_reg)
286*c87b03e5Sespie IOR_HARD_REG_SET (this_unavailable, call_used_reg_set);
287*c87b03e5Sespie
288*c87b03e5Sespie merge_overlapping_regs (bb, &this_unavailable, this);
289*c87b03e5Sespie
290*c87b03e5Sespie /* Now potential_regs is a reasonable approximation, let's
291*c87b03e5Sespie have a closer look at each register still in there. */
292*c87b03e5Sespie for (new_reg = 0; new_reg < FIRST_PSEUDO_REGISTER; new_reg++)
293*c87b03e5Sespie {
294*c87b03e5Sespie int nregs = HARD_REGNO_NREGS (new_reg, GET_MODE (*this->loc));
295*c87b03e5Sespie
296*c87b03e5Sespie for (i = nregs - 1; i >= 0; --i)
297*c87b03e5Sespie if (TEST_HARD_REG_BIT (this_unavailable, new_reg + i)
298*c87b03e5Sespie || fixed_regs[new_reg + i]
299*c87b03e5Sespie || global_regs[new_reg + i]
300*c87b03e5Sespie /* Can't use regs which aren't saved by the prologue. */
301*c87b03e5Sespie || (! regs_ever_live[new_reg + i]
302*c87b03e5Sespie && ! call_used_regs[new_reg + i])
303*c87b03e5Sespie #ifdef LEAF_REGISTERS
304*c87b03e5Sespie /* We can't use a non-leaf register if we're in a
305*c87b03e5Sespie leaf function. */
306*c87b03e5Sespie || (current_function_is_leaf
307*c87b03e5Sespie && !LEAF_REGISTERS[new_reg + i])
308*c87b03e5Sespie #endif
309*c87b03e5Sespie #ifdef HARD_REGNO_RENAME_OK
310*c87b03e5Sespie || ! HARD_REGNO_RENAME_OK (reg + i, new_reg + i)
311*c87b03e5Sespie #endif
312*c87b03e5Sespie )
313*c87b03e5Sespie break;
314*c87b03e5Sespie if (i >= 0)
315*c87b03e5Sespie continue;
316*c87b03e5Sespie
317*c87b03e5Sespie /* See whether it accepts all modes that occur in
318*c87b03e5Sespie definition and uses. */
319*c87b03e5Sespie for (tmp = this; tmp; tmp = tmp->next_use)
320*c87b03e5Sespie if (! HARD_REGNO_MODE_OK (new_reg, GET_MODE (*tmp->loc))
321*c87b03e5Sespie || (tmp->need_caller_save_reg
322*c87b03e5Sespie && ! (HARD_REGNO_CALL_PART_CLOBBERED
323*c87b03e5Sespie (reg, GET_MODE (*tmp->loc)))
324*c87b03e5Sespie && (HARD_REGNO_CALL_PART_CLOBBERED
325*c87b03e5Sespie (new_reg, GET_MODE (*tmp->loc)))))
326*c87b03e5Sespie break;
327*c87b03e5Sespie if (! tmp)
328*c87b03e5Sespie {
329*c87b03e5Sespie if (best_new_reg == -1
330*c87b03e5Sespie || tick[best_new_reg] > tick[new_reg])
331*c87b03e5Sespie best_new_reg = new_reg;
332*c87b03e5Sespie }
333*c87b03e5Sespie }
334*c87b03e5Sespie
335*c87b03e5Sespie if (rtl_dump_file)
336*c87b03e5Sespie {
337*c87b03e5Sespie fprintf (rtl_dump_file, "Register %s in insn %d",
338*c87b03e5Sespie reg_names[reg], INSN_UID (last->insn));
339*c87b03e5Sespie if (last->need_caller_save_reg)
340*c87b03e5Sespie fprintf (rtl_dump_file, " crosses a call");
341*c87b03e5Sespie }
342*c87b03e5Sespie
343*c87b03e5Sespie if (best_new_reg == -1)
344*c87b03e5Sespie {
345*c87b03e5Sespie if (rtl_dump_file)
346*c87b03e5Sespie fprintf (rtl_dump_file, "; no available registers\n");
347*c87b03e5Sespie continue;
348*c87b03e5Sespie }
349*c87b03e5Sespie
350*c87b03e5Sespie do_replace (this, best_new_reg);
351*c87b03e5Sespie tick[best_new_reg] = this_tick++;
352*c87b03e5Sespie
353*c87b03e5Sespie if (rtl_dump_file)
354*c87b03e5Sespie fprintf (rtl_dump_file, ", renamed as %s\n", reg_names[best_new_reg]);
355*c87b03e5Sespie }
356*c87b03e5Sespie
357*c87b03e5Sespie obstack_free (&rename_obstack, first_obj);
358*c87b03e5Sespie }
359*c87b03e5Sespie
360*c87b03e5Sespie obstack_free (&rename_obstack, NULL);
361*c87b03e5Sespie
362*c87b03e5Sespie if (rtl_dump_file)
363*c87b03e5Sespie fputc ('\n', rtl_dump_file);
364*c87b03e5Sespie
365*c87b03e5Sespie count_or_remove_death_notes (NULL, 1);
366*c87b03e5Sespie update_life_info (NULL, UPDATE_LIFE_LOCAL,
367*c87b03e5Sespie PROP_REG_INFO | PROP_DEATH_NOTES);
368*c87b03e5Sespie }
369*c87b03e5Sespie
370*c87b03e5Sespie static void
do_replace(chain,reg)371*c87b03e5Sespie do_replace (chain, reg)
372*c87b03e5Sespie struct du_chain *chain;
373*c87b03e5Sespie int reg;
374*c87b03e5Sespie {
375*c87b03e5Sespie while (chain)
376*c87b03e5Sespie {
377*c87b03e5Sespie unsigned int regno = ORIGINAL_REGNO (*chain->loc);
378*c87b03e5Sespie *chain->loc = gen_raw_REG (GET_MODE (*chain->loc), reg);
379*c87b03e5Sespie if (regno >= FIRST_PSEUDO_REGISTER)
380*c87b03e5Sespie ORIGINAL_REGNO (*chain->loc) = regno;
381*c87b03e5Sespie chain = chain->next_use;
382*c87b03e5Sespie }
383*c87b03e5Sespie }
384*c87b03e5Sespie
385*c87b03e5Sespie
386*c87b03e5Sespie static struct du_chain *open_chains;
387*c87b03e5Sespie static struct du_chain *closed_chains;
388*c87b03e5Sespie
389*c87b03e5Sespie static void
scan_rtx_reg(insn,loc,class,action,type,earlyclobber)390*c87b03e5Sespie scan_rtx_reg (insn, loc, class, action, type, earlyclobber)
391*c87b03e5Sespie rtx insn;
392*c87b03e5Sespie rtx *loc;
393*c87b03e5Sespie enum reg_class class;
394*c87b03e5Sespie enum scan_actions action;
395*c87b03e5Sespie enum op_type type;
396*c87b03e5Sespie int earlyclobber;
397*c87b03e5Sespie {
398*c87b03e5Sespie struct du_chain **p;
399*c87b03e5Sespie rtx x = *loc;
400*c87b03e5Sespie enum machine_mode mode = GET_MODE (x);
401*c87b03e5Sespie int this_regno = REGNO (x);
402*c87b03e5Sespie int this_nregs = HARD_REGNO_NREGS (this_regno, mode);
403*c87b03e5Sespie
404*c87b03e5Sespie if (action == mark_write)
405*c87b03e5Sespie {
406*c87b03e5Sespie if (type == OP_OUT)
407*c87b03e5Sespie {
408*c87b03e5Sespie struct du_chain *this = (struct du_chain *)
409*c87b03e5Sespie obstack_alloc (&rename_obstack, sizeof (struct du_chain));
410*c87b03e5Sespie this->next_use = 0;
411*c87b03e5Sespie this->next_chain = open_chains;
412*c87b03e5Sespie this->loc = loc;
413*c87b03e5Sespie this->insn = insn;
414*c87b03e5Sespie this->class = class;
415*c87b03e5Sespie this->need_caller_save_reg = 0;
416*c87b03e5Sespie this->earlyclobber = earlyclobber;
417*c87b03e5Sespie open_chains = this;
418*c87b03e5Sespie }
419*c87b03e5Sespie return;
420*c87b03e5Sespie }
421*c87b03e5Sespie
422*c87b03e5Sespie if ((type == OP_OUT && action != terminate_write)
423*c87b03e5Sespie || (type != OP_OUT && action == terminate_write))
424*c87b03e5Sespie return;
425*c87b03e5Sespie
426*c87b03e5Sespie for (p = &open_chains; *p;)
427*c87b03e5Sespie {
428*c87b03e5Sespie struct du_chain *this = *p;
429*c87b03e5Sespie
430*c87b03e5Sespie /* Check if the chain has been terminated if it has then skip to
431*c87b03e5Sespie the next chain.
432*c87b03e5Sespie
433*c87b03e5Sespie This can happen when we've already appended the location to
434*c87b03e5Sespie the chain in Step 3, but are trying to hide in-out operands
435*c87b03e5Sespie from terminate_write in Step 5. */
436*c87b03e5Sespie
437*c87b03e5Sespie if (*this->loc == cc0_rtx)
438*c87b03e5Sespie p = &this->next_chain;
439*c87b03e5Sespie else
440*c87b03e5Sespie {
441*c87b03e5Sespie int regno = REGNO (*this->loc);
442*c87b03e5Sespie int nregs = HARD_REGNO_NREGS (regno, GET_MODE (*this->loc));
443*c87b03e5Sespie int exact_match = (regno == this_regno && nregs == this_nregs);
444*c87b03e5Sespie
445*c87b03e5Sespie if (regno + nregs <= this_regno
446*c87b03e5Sespie || this_regno + this_nregs <= regno)
447*c87b03e5Sespie {
448*c87b03e5Sespie p = &this->next_chain;
449*c87b03e5Sespie continue;
450*c87b03e5Sespie }
451*c87b03e5Sespie
452*c87b03e5Sespie if (action == mark_read)
453*c87b03e5Sespie {
454*c87b03e5Sespie if (! exact_match)
455*c87b03e5Sespie abort ();
456*c87b03e5Sespie
457*c87b03e5Sespie /* ??? Class NO_REGS can happen if the md file makes use of
458*c87b03e5Sespie EXTRA_CONSTRAINTS to match registers. Which is arguably
459*c87b03e5Sespie wrong, but there we are. Since we know not what this may
460*c87b03e5Sespie be replaced with, terminate the chain. */
461*c87b03e5Sespie if (class != NO_REGS)
462*c87b03e5Sespie {
463*c87b03e5Sespie this = (struct du_chain *)
464*c87b03e5Sespie obstack_alloc (&rename_obstack, sizeof (struct du_chain));
465*c87b03e5Sespie this->next_use = 0;
466*c87b03e5Sespie this->next_chain = (*p)->next_chain;
467*c87b03e5Sespie this->loc = loc;
468*c87b03e5Sespie this->insn = insn;
469*c87b03e5Sespie this->class = class;
470*c87b03e5Sespie this->need_caller_save_reg = 0;
471*c87b03e5Sespie while (*p)
472*c87b03e5Sespie p = &(*p)->next_use;
473*c87b03e5Sespie *p = this;
474*c87b03e5Sespie return;
475*c87b03e5Sespie }
476*c87b03e5Sespie }
477*c87b03e5Sespie
478*c87b03e5Sespie if (action != terminate_overlapping_read || ! exact_match)
479*c87b03e5Sespie {
480*c87b03e5Sespie struct du_chain *next = this->next_chain;
481*c87b03e5Sespie
482*c87b03e5Sespie /* Whether the terminated chain can be used for renaming
483*c87b03e5Sespie depends on the action and this being an exact match.
484*c87b03e5Sespie In either case, we remove this element from open_chains. */
485*c87b03e5Sespie
486*c87b03e5Sespie if ((action == terminate_dead || action == terminate_write)
487*c87b03e5Sespie && exact_match)
488*c87b03e5Sespie {
489*c87b03e5Sespie this->next_chain = closed_chains;
490*c87b03e5Sespie closed_chains = this;
491*c87b03e5Sespie if (rtl_dump_file)
492*c87b03e5Sespie fprintf (rtl_dump_file,
493*c87b03e5Sespie "Closing chain %s at insn %d (%s)\n",
494*c87b03e5Sespie reg_names[REGNO (*this->loc)], INSN_UID (insn),
495*c87b03e5Sespie scan_actions_name[(int) action]);
496*c87b03e5Sespie }
497*c87b03e5Sespie else
498*c87b03e5Sespie {
499*c87b03e5Sespie if (rtl_dump_file)
500*c87b03e5Sespie fprintf (rtl_dump_file,
501*c87b03e5Sespie "Discarding chain %s at insn %d (%s)\n",
502*c87b03e5Sespie reg_names[REGNO (*this->loc)], INSN_UID (insn),
503*c87b03e5Sespie scan_actions_name[(int) action]);
504*c87b03e5Sespie }
505*c87b03e5Sespie *p = next;
506*c87b03e5Sespie }
507*c87b03e5Sespie else
508*c87b03e5Sespie p = &this->next_chain;
509*c87b03e5Sespie }
510*c87b03e5Sespie }
511*c87b03e5Sespie }
512*c87b03e5Sespie
513*c87b03e5Sespie /* Adapted from find_reloads_address_1. CLASS is INDEX_REG_CLASS or
514*c87b03e5Sespie BASE_REG_CLASS depending on how the register is being considered. */
515*c87b03e5Sespie
516*c87b03e5Sespie static void
scan_rtx_address(insn,loc,class,action,mode)517*c87b03e5Sespie scan_rtx_address (insn, loc, class, action, mode)
518*c87b03e5Sespie rtx insn;
519*c87b03e5Sespie rtx *loc;
520*c87b03e5Sespie enum reg_class class;
521*c87b03e5Sespie enum scan_actions action;
522*c87b03e5Sespie enum machine_mode mode;
523*c87b03e5Sespie {
524*c87b03e5Sespie rtx x = *loc;
525*c87b03e5Sespie RTX_CODE code = GET_CODE (x);
526*c87b03e5Sespie const char *fmt;
527*c87b03e5Sespie int i, j;
528*c87b03e5Sespie
529*c87b03e5Sespie if (action == mark_write)
530*c87b03e5Sespie return;
531*c87b03e5Sespie
532*c87b03e5Sespie switch (code)
533*c87b03e5Sespie {
534*c87b03e5Sespie case PLUS:
535*c87b03e5Sespie {
536*c87b03e5Sespie rtx orig_op0 = XEXP (x, 0);
537*c87b03e5Sespie rtx orig_op1 = XEXP (x, 1);
538*c87b03e5Sespie RTX_CODE code0 = GET_CODE (orig_op0);
539*c87b03e5Sespie RTX_CODE code1 = GET_CODE (orig_op1);
540*c87b03e5Sespie rtx op0 = orig_op0;
541*c87b03e5Sespie rtx op1 = orig_op1;
542*c87b03e5Sespie rtx *locI = NULL;
543*c87b03e5Sespie rtx *locB = NULL;
544*c87b03e5Sespie
545*c87b03e5Sespie if (GET_CODE (op0) == SUBREG)
546*c87b03e5Sespie {
547*c87b03e5Sespie op0 = SUBREG_REG (op0);
548*c87b03e5Sespie code0 = GET_CODE (op0);
549*c87b03e5Sespie }
550*c87b03e5Sespie
551*c87b03e5Sespie if (GET_CODE (op1) == SUBREG)
552*c87b03e5Sespie {
553*c87b03e5Sespie op1 = SUBREG_REG (op1);
554*c87b03e5Sespie code1 = GET_CODE (op1);
555*c87b03e5Sespie }
556*c87b03e5Sespie
557*c87b03e5Sespie if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
558*c87b03e5Sespie || code0 == ZERO_EXTEND || code1 == MEM)
559*c87b03e5Sespie {
560*c87b03e5Sespie locI = &XEXP (x, 0);
561*c87b03e5Sespie locB = &XEXP (x, 1);
562*c87b03e5Sespie }
563*c87b03e5Sespie else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
564*c87b03e5Sespie || code1 == ZERO_EXTEND || code0 == MEM)
565*c87b03e5Sespie {
566*c87b03e5Sespie locI = &XEXP (x, 1);
567*c87b03e5Sespie locB = &XEXP (x, 0);
568*c87b03e5Sespie }
569*c87b03e5Sespie else if (code0 == CONST_INT || code0 == CONST
570*c87b03e5Sespie || code0 == SYMBOL_REF || code0 == LABEL_REF)
571*c87b03e5Sespie locB = &XEXP (x, 1);
572*c87b03e5Sespie else if (code1 == CONST_INT || code1 == CONST
573*c87b03e5Sespie || code1 == SYMBOL_REF || code1 == LABEL_REF)
574*c87b03e5Sespie locB = &XEXP (x, 0);
575*c87b03e5Sespie else if (code0 == REG && code1 == REG)
576*c87b03e5Sespie {
577*c87b03e5Sespie int index_op;
578*c87b03e5Sespie
579*c87b03e5Sespie if (REG_OK_FOR_INDEX_P (op0)
580*c87b03e5Sespie && REG_MODE_OK_FOR_BASE_P (op1, mode))
581*c87b03e5Sespie index_op = 0;
582*c87b03e5Sespie else if (REG_OK_FOR_INDEX_P (op1)
583*c87b03e5Sespie && REG_MODE_OK_FOR_BASE_P (op0, mode))
584*c87b03e5Sespie index_op = 1;
585*c87b03e5Sespie else if (REG_MODE_OK_FOR_BASE_P (op1, mode))
586*c87b03e5Sespie index_op = 0;
587*c87b03e5Sespie else if (REG_MODE_OK_FOR_BASE_P (op0, mode))
588*c87b03e5Sespie index_op = 1;
589*c87b03e5Sespie else if (REG_OK_FOR_INDEX_P (op1))
590*c87b03e5Sespie index_op = 1;
591*c87b03e5Sespie else
592*c87b03e5Sespie index_op = 0;
593*c87b03e5Sespie
594*c87b03e5Sespie locI = &XEXP (x, index_op);
595*c87b03e5Sespie locB = &XEXP (x, !index_op);
596*c87b03e5Sespie }
597*c87b03e5Sespie else if (code0 == REG)
598*c87b03e5Sespie {
599*c87b03e5Sespie locI = &XEXP (x, 0);
600*c87b03e5Sespie locB = &XEXP (x, 1);
601*c87b03e5Sespie }
602*c87b03e5Sespie else if (code1 == REG)
603*c87b03e5Sespie {
604*c87b03e5Sespie locI = &XEXP (x, 1);
605*c87b03e5Sespie locB = &XEXP (x, 0);
606*c87b03e5Sespie }
607*c87b03e5Sespie
608*c87b03e5Sespie if (locI)
609*c87b03e5Sespie scan_rtx_address (insn, locI, INDEX_REG_CLASS, action, mode);
610*c87b03e5Sespie if (locB)
611*c87b03e5Sespie scan_rtx_address (insn, locB, MODE_BASE_REG_CLASS (mode), action, mode);
612*c87b03e5Sespie return;
613*c87b03e5Sespie }
614*c87b03e5Sespie
615*c87b03e5Sespie case POST_INC:
616*c87b03e5Sespie case POST_DEC:
617*c87b03e5Sespie case POST_MODIFY:
618*c87b03e5Sespie case PRE_INC:
619*c87b03e5Sespie case PRE_DEC:
620*c87b03e5Sespie case PRE_MODIFY:
621*c87b03e5Sespie #ifndef AUTO_INC_DEC
622*c87b03e5Sespie /* If the target doesn't claim to handle autoinc, this must be
623*c87b03e5Sespie something special, like a stack push. Kill this chain. */
624*c87b03e5Sespie action = terminate_all_read;
625*c87b03e5Sespie #endif
626*c87b03e5Sespie break;
627*c87b03e5Sespie
628*c87b03e5Sespie case MEM:
629*c87b03e5Sespie scan_rtx_address (insn, &XEXP (x, 0),
630*c87b03e5Sespie MODE_BASE_REG_CLASS (GET_MODE (x)), action,
631*c87b03e5Sespie GET_MODE (x));
632*c87b03e5Sespie return;
633*c87b03e5Sespie
634*c87b03e5Sespie case REG:
635*c87b03e5Sespie scan_rtx_reg (insn, loc, class, action, OP_IN, 0);
636*c87b03e5Sespie return;
637*c87b03e5Sespie
638*c87b03e5Sespie default:
639*c87b03e5Sespie break;
640*c87b03e5Sespie }
641*c87b03e5Sespie
642*c87b03e5Sespie fmt = GET_RTX_FORMAT (code);
643*c87b03e5Sespie for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
644*c87b03e5Sespie {
645*c87b03e5Sespie if (fmt[i] == 'e')
646*c87b03e5Sespie scan_rtx_address (insn, &XEXP (x, i), class, action, mode);
647*c87b03e5Sespie else if (fmt[i] == 'E')
648*c87b03e5Sespie for (j = XVECLEN (x, i) - 1; j >= 0; j--)
649*c87b03e5Sespie scan_rtx_address (insn, &XVECEXP (x, i, j), class, action, mode);
650*c87b03e5Sespie }
651*c87b03e5Sespie }
652*c87b03e5Sespie
653*c87b03e5Sespie static void
scan_rtx(insn,loc,class,action,type,earlyclobber)654*c87b03e5Sespie scan_rtx (insn, loc, class, action, type, earlyclobber)
655*c87b03e5Sespie rtx insn;
656*c87b03e5Sespie rtx *loc;
657*c87b03e5Sespie enum reg_class class;
658*c87b03e5Sespie enum scan_actions action;
659*c87b03e5Sespie enum op_type type;
660*c87b03e5Sespie int earlyclobber;
661*c87b03e5Sespie {
662*c87b03e5Sespie const char *fmt;
663*c87b03e5Sespie rtx x = *loc;
664*c87b03e5Sespie enum rtx_code code = GET_CODE (x);
665*c87b03e5Sespie int i, j;
666*c87b03e5Sespie
667*c87b03e5Sespie code = GET_CODE (x);
668*c87b03e5Sespie switch (code)
669*c87b03e5Sespie {
670*c87b03e5Sespie case CONST:
671*c87b03e5Sespie case CONST_INT:
672*c87b03e5Sespie case CONST_DOUBLE:
673*c87b03e5Sespie case CONST_VECTOR:
674*c87b03e5Sespie case SYMBOL_REF:
675*c87b03e5Sespie case LABEL_REF:
676*c87b03e5Sespie case CC0:
677*c87b03e5Sespie case PC:
678*c87b03e5Sespie return;
679*c87b03e5Sespie
680*c87b03e5Sespie case REG:
681*c87b03e5Sespie scan_rtx_reg (insn, loc, class, action, type, earlyclobber);
682*c87b03e5Sespie return;
683*c87b03e5Sespie
684*c87b03e5Sespie case MEM:
685*c87b03e5Sespie scan_rtx_address (insn, &XEXP (x, 0),
686*c87b03e5Sespie MODE_BASE_REG_CLASS (GET_MODE (x)), action,
687*c87b03e5Sespie GET_MODE (x));
688*c87b03e5Sespie return;
689*c87b03e5Sespie
690*c87b03e5Sespie case SET:
691*c87b03e5Sespie scan_rtx (insn, &SET_SRC (x), class, action, OP_IN, 0);
692*c87b03e5Sespie scan_rtx (insn, &SET_DEST (x), class, action, OP_OUT, 0);
693*c87b03e5Sespie return;
694*c87b03e5Sespie
695*c87b03e5Sespie case STRICT_LOW_PART:
696*c87b03e5Sespie scan_rtx (insn, &XEXP (x, 0), class, action, OP_INOUT, earlyclobber);
697*c87b03e5Sespie return;
698*c87b03e5Sespie
699*c87b03e5Sespie case ZERO_EXTRACT:
700*c87b03e5Sespie case SIGN_EXTRACT:
701*c87b03e5Sespie scan_rtx (insn, &XEXP (x, 0), class, action,
702*c87b03e5Sespie type == OP_IN ? OP_IN : OP_INOUT, earlyclobber);
703*c87b03e5Sespie scan_rtx (insn, &XEXP (x, 1), class, action, OP_IN, 0);
704*c87b03e5Sespie scan_rtx (insn, &XEXP (x, 2), class, action, OP_IN, 0);
705*c87b03e5Sespie return;
706*c87b03e5Sespie
707*c87b03e5Sespie case POST_INC:
708*c87b03e5Sespie case PRE_INC:
709*c87b03e5Sespie case POST_DEC:
710*c87b03e5Sespie case PRE_DEC:
711*c87b03e5Sespie case POST_MODIFY:
712*c87b03e5Sespie case PRE_MODIFY:
713*c87b03e5Sespie /* Should only happen inside MEM. */
714*c87b03e5Sespie abort ();
715*c87b03e5Sespie
716*c87b03e5Sespie case CLOBBER:
717*c87b03e5Sespie scan_rtx (insn, &SET_DEST (x), class, action, OP_OUT, 1);
718*c87b03e5Sespie return;
719*c87b03e5Sespie
720*c87b03e5Sespie case EXPR_LIST:
721*c87b03e5Sespie scan_rtx (insn, &XEXP (x, 0), class, action, type, 0);
722*c87b03e5Sespie if (XEXP (x, 1))
723*c87b03e5Sespie scan_rtx (insn, &XEXP (x, 1), class, action, type, 0);
724*c87b03e5Sespie return;
725*c87b03e5Sespie
726*c87b03e5Sespie default:
727*c87b03e5Sespie break;
728*c87b03e5Sespie }
729*c87b03e5Sespie
730*c87b03e5Sespie fmt = GET_RTX_FORMAT (code);
731*c87b03e5Sespie for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
732*c87b03e5Sespie {
733*c87b03e5Sespie if (fmt[i] == 'e')
734*c87b03e5Sespie scan_rtx (insn, &XEXP (x, i), class, action, type, 0);
735*c87b03e5Sespie else if (fmt[i] == 'E')
736*c87b03e5Sespie for (j = XVECLEN (x, i) - 1; j >= 0; j--)
737*c87b03e5Sespie scan_rtx (insn, &XVECEXP (x, i, j), class, action, type, 0);
738*c87b03e5Sespie }
739*c87b03e5Sespie }
740*c87b03e5Sespie
741*c87b03e5Sespie /* Build def/use chain. */
742*c87b03e5Sespie
743*c87b03e5Sespie static struct du_chain *
build_def_use(bb)744*c87b03e5Sespie build_def_use (bb)
745*c87b03e5Sespie basic_block bb;
746*c87b03e5Sespie {
747*c87b03e5Sespie rtx insn;
748*c87b03e5Sespie
749*c87b03e5Sespie open_chains = closed_chains = NULL;
750*c87b03e5Sespie
751*c87b03e5Sespie for (insn = bb->head; ; insn = NEXT_INSN (insn))
752*c87b03e5Sespie {
753*c87b03e5Sespie if (INSN_P (insn))
754*c87b03e5Sespie {
755*c87b03e5Sespie int n_ops;
756*c87b03e5Sespie rtx note;
757*c87b03e5Sespie rtx old_operands[MAX_RECOG_OPERANDS];
758*c87b03e5Sespie rtx old_dups[MAX_DUP_OPERANDS];
759*c87b03e5Sespie int i, icode;
760*c87b03e5Sespie int alt;
761*c87b03e5Sespie int predicated;
762*c87b03e5Sespie
763*c87b03e5Sespie /* Process the insn, determining its effect on the def-use
764*c87b03e5Sespie chains. We perform the following steps with the register
765*c87b03e5Sespie references in the insn:
766*c87b03e5Sespie (1) Any read that overlaps an open chain, but doesn't exactly
767*c87b03e5Sespie match, causes that chain to be closed. We can't deal
768*c87b03e5Sespie with overlaps yet.
769*c87b03e5Sespie (2) Any read outside an operand causes any chain it overlaps
770*c87b03e5Sespie with to be closed, since we can't replace it.
771*c87b03e5Sespie (3) Any read inside an operand is added if there's already
772*c87b03e5Sespie an open chain for it.
773*c87b03e5Sespie (4) For any REG_DEAD note we find, close open chains that
774*c87b03e5Sespie overlap it.
775*c87b03e5Sespie (5) For any write we find, close open chains that overlap it.
776*c87b03e5Sespie (6) For any write we find in an operand, make a new chain.
777*c87b03e5Sespie (7) For any REG_UNUSED, close any chains we just opened. */
778*c87b03e5Sespie
779*c87b03e5Sespie icode = recog_memoized (insn);
780*c87b03e5Sespie extract_insn (insn);
781*c87b03e5Sespie if (! constrain_operands (1))
782*c87b03e5Sespie fatal_insn_not_found (insn);
783*c87b03e5Sespie preprocess_constraints ();
784*c87b03e5Sespie alt = which_alternative;
785*c87b03e5Sespie n_ops = recog_data.n_operands;
786*c87b03e5Sespie
787*c87b03e5Sespie /* Simplify the code below by rewriting things to reflect
788*c87b03e5Sespie matching constraints. Also promote OP_OUT to OP_INOUT
789*c87b03e5Sespie in predicated instructions. */
790*c87b03e5Sespie
791*c87b03e5Sespie predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
792*c87b03e5Sespie for (i = 0; i < n_ops; ++i)
793*c87b03e5Sespie {
794*c87b03e5Sespie int matches = recog_op_alt[i][alt].matches;
795*c87b03e5Sespie if (matches >= 0)
796*c87b03e5Sespie recog_op_alt[i][alt].class = recog_op_alt[matches][alt].class;
797*c87b03e5Sespie if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
798*c87b03e5Sespie || (predicated && recog_data.operand_type[i] == OP_OUT))
799*c87b03e5Sespie recog_data.operand_type[i] = OP_INOUT;
800*c87b03e5Sespie }
801*c87b03e5Sespie
802*c87b03e5Sespie /* Step 1: Close chains for which we have overlapping reads. */
803*c87b03e5Sespie for (i = 0; i < n_ops; i++)
804*c87b03e5Sespie scan_rtx (insn, recog_data.operand_loc[i],
805*c87b03e5Sespie NO_REGS, terminate_overlapping_read,
806*c87b03e5Sespie recog_data.operand_type[i], 0);
807*c87b03e5Sespie
808*c87b03e5Sespie /* Step 2: Close chains for which we have reads outside operands.
809*c87b03e5Sespie We do this by munging all operands into CC0, and closing
810*c87b03e5Sespie everything remaining. */
811*c87b03e5Sespie
812*c87b03e5Sespie for (i = 0; i < n_ops; i++)
813*c87b03e5Sespie {
814*c87b03e5Sespie old_operands[i] = recog_data.operand[i];
815*c87b03e5Sespie /* Don't squash match_operator or match_parallel here, since
816*c87b03e5Sespie we don't know that all of the contained registers are
817*c87b03e5Sespie reachable by proper operands. */
818*c87b03e5Sespie if (recog_data.constraints[i][0] == '\0')
819*c87b03e5Sespie continue;
820*c87b03e5Sespie *recog_data.operand_loc[i] = cc0_rtx;
821*c87b03e5Sespie }
822*c87b03e5Sespie for (i = 0; i < recog_data.n_dups; i++)
823*c87b03e5Sespie {
824*c87b03e5Sespie int dup_num = recog_data.dup_num[i];
825*c87b03e5Sespie
826*c87b03e5Sespie old_dups[i] = *recog_data.dup_loc[i];
827*c87b03e5Sespie *recog_data.dup_loc[i] = cc0_rtx;
828*c87b03e5Sespie
829*c87b03e5Sespie /* For match_dup of match_operator or match_parallel, share
830*c87b03e5Sespie them, so that we don't miss changes in the dup. */
831*c87b03e5Sespie if (icode >= 0
832*c87b03e5Sespie && insn_data[icode].operand[dup_num].eliminable == 0)
833*c87b03e5Sespie old_dups[i] = recog_data.operand[dup_num];
834*c87b03e5Sespie }
835*c87b03e5Sespie
836*c87b03e5Sespie scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_all_read,
837*c87b03e5Sespie OP_IN, 0);
838*c87b03e5Sespie
839*c87b03e5Sespie for (i = 0; i < recog_data.n_dups; i++)
840*c87b03e5Sespie *recog_data.dup_loc[i] = old_dups[i];
841*c87b03e5Sespie for (i = 0; i < n_ops; i++)
842*c87b03e5Sespie *recog_data.operand_loc[i] = old_operands[i];
843*c87b03e5Sespie
844*c87b03e5Sespie /* Step 2B: Can't rename function call argument registers. */
845*c87b03e5Sespie if (GET_CODE (insn) == CALL_INSN && CALL_INSN_FUNCTION_USAGE (insn))
846*c87b03e5Sespie scan_rtx (insn, &CALL_INSN_FUNCTION_USAGE (insn),
847*c87b03e5Sespie NO_REGS, terminate_all_read, OP_IN, 0);
848*c87b03e5Sespie
849*c87b03e5Sespie /* Step 2C: Can't rename asm operands that were originally
850*c87b03e5Sespie hard registers. */
851*c87b03e5Sespie if (asm_noperands (PATTERN (insn)) > 0)
852*c87b03e5Sespie for (i = 0; i < n_ops; i++)
853*c87b03e5Sespie {
854*c87b03e5Sespie rtx *loc = recog_data.operand_loc[i];
855*c87b03e5Sespie rtx op = *loc;
856*c87b03e5Sespie
857*c87b03e5Sespie if (GET_CODE (op) == REG
858*c87b03e5Sespie && REGNO (op) == ORIGINAL_REGNO (op)
859*c87b03e5Sespie && (recog_data.operand_type[i] == OP_IN
860*c87b03e5Sespie || recog_data.operand_type[i] == OP_INOUT))
861*c87b03e5Sespie scan_rtx (insn, loc, NO_REGS, terminate_all_read, OP_IN, 0);
862*c87b03e5Sespie }
863*c87b03e5Sespie
864*c87b03e5Sespie /* Step 3: Append to chains for reads inside operands. */
865*c87b03e5Sespie for (i = 0; i < n_ops + recog_data.n_dups; i++)
866*c87b03e5Sespie {
867*c87b03e5Sespie int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
868*c87b03e5Sespie rtx *loc = (i < n_ops
869*c87b03e5Sespie ? recog_data.operand_loc[opn]
870*c87b03e5Sespie : recog_data.dup_loc[i - n_ops]);
871*c87b03e5Sespie enum reg_class class = recog_op_alt[opn][alt].class;
872*c87b03e5Sespie enum op_type type = recog_data.operand_type[opn];
873*c87b03e5Sespie
874*c87b03e5Sespie /* Don't scan match_operand here, since we've no reg class
875*c87b03e5Sespie information to pass down. Any operands that we could
876*c87b03e5Sespie substitute in will be represented elsewhere. */
877*c87b03e5Sespie if (recog_data.constraints[opn][0] == '\0')
878*c87b03e5Sespie continue;
879*c87b03e5Sespie
880*c87b03e5Sespie if (recog_op_alt[opn][alt].is_address)
881*c87b03e5Sespie scan_rtx_address (insn, loc, class, mark_read, VOIDmode);
882*c87b03e5Sespie else
883*c87b03e5Sespie scan_rtx (insn, loc, class, mark_read, type, 0);
884*c87b03e5Sespie }
885*c87b03e5Sespie
886*c87b03e5Sespie /* Step 4: Close chains for registers that die here.
887*c87b03e5Sespie Also record updates for REG_INC notes. */
888*c87b03e5Sespie for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
889*c87b03e5Sespie {
890*c87b03e5Sespie if (REG_NOTE_KIND (note) == REG_DEAD)
891*c87b03e5Sespie scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
892*c87b03e5Sespie OP_IN, 0);
893*c87b03e5Sespie else if (REG_NOTE_KIND (note) == REG_INC)
894*c87b03e5Sespie scan_rtx (insn, &XEXP (note, 0), ALL_REGS, mark_read,
895*c87b03e5Sespie OP_INOUT, 0);
896*c87b03e5Sespie }
897*c87b03e5Sespie
898*c87b03e5Sespie /* Step 4B: If this is a call, any chain live at this point
899*c87b03e5Sespie requires a caller-saved reg. */
900*c87b03e5Sespie if (GET_CODE (insn) == CALL_INSN)
901*c87b03e5Sespie {
902*c87b03e5Sespie struct du_chain *p;
903*c87b03e5Sespie for (p = open_chains; p; p = p->next_chain)
904*c87b03e5Sespie p->need_caller_save_reg = 1;
905*c87b03e5Sespie }
906*c87b03e5Sespie
907*c87b03e5Sespie /* Step 5: Close open chains that overlap writes. Similar to
908*c87b03e5Sespie step 2, we hide in-out operands, since we do not want to
909*c87b03e5Sespie close these chains. */
910*c87b03e5Sespie
911*c87b03e5Sespie for (i = 0; i < n_ops; i++)
912*c87b03e5Sespie {
913*c87b03e5Sespie old_operands[i] = recog_data.operand[i];
914*c87b03e5Sespie if (recog_data.operand_type[i] == OP_INOUT)
915*c87b03e5Sespie *recog_data.operand_loc[i] = cc0_rtx;
916*c87b03e5Sespie }
917*c87b03e5Sespie for (i = 0; i < recog_data.n_dups; i++)
918*c87b03e5Sespie {
919*c87b03e5Sespie int opn = recog_data.dup_num[i];
920*c87b03e5Sespie old_dups[i] = *recog_data.dup_loc[i];
921*c87b03e5Sespie if (recog_data.operand_type[opn] == OP_INOUT)
922*c87b03e5Sespie *recog_data.dup_loc[i] = cc0_rtx;
923*c87b03e5Sespie }
924*c87b03e5Sespie
925*c87b03e5Sespie scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_write, OP_IN, 0);
926*c87b03e5Sespie
927*c87b03e5Sespie for (i = 0; i < recog_data.n_dups; i++)
928*c87b03e5Sespie *recog_data.dup_loc[i] = old_dups[i];
929*c87b03e5Sespie for (i = 0; i < n_ops; i++)
930*c87b03e5Sespie *recog_data.operand_loc[i] = old_operands[i];
931*c87b03e5Sespie
932*c87b03e5Sespie /* Step 6: Begin new chains for writes inside operands. */
933*c87b03e5Sespie /* ??? Many targets have output constraints on the SET_DEST
934*c87b03e5Sespie of a call insn, which is stupid, since these are certainly
935*c87b03e5Sespie ABI defined hard registers. Don't change calls at all.
936*c87b03e5Sespie Similarly take special care for asm statement that originally
937*c87b03e5Sespie referenced hard registers. */
938*c87b03e5Sespie if (asm_noperands (PATTERN (insn)) > 0)
939*c87b03e5Sespie {
940*c87b03e5Sespie for (i = 0; i < n_ops; i++)
941*c87b03e5Sespie if (recog_data.operand_type[i] == OP_OUT)
942*c87b03e5Sespie {
943*c87b03e5Sespie rtx *loc = recog_data.operand_loc[i];
944*c87b03e5Sespie rtx op = *loc;
945*c87b03e5Sespie enum reg_class class = recog_op_alt[i][alt].class;
946*c87b03e5Sespie
947*c87b03e5Sespie if (GET_CODE (op) == REG
948*c87b03e5Sespie && REGNO (op) == ORIGINAL_REGNO (op))
949*c87b03e5Sespie continue;
950*c87b03e5Sespie
951*c87b03e5Sespie scan_rtx (insn, loc, class, mark_write, OP_OUT,
952*c87b03e5Sespie recog_op_alt[i][alt].earlyclobber);
953*c87b03e5Sespie }
954*c87b03e5Sespie }
955*c87b03e5Sespie else if (GET_CODE (insn) != CALL_INSN)
956*c87b03e5Sespie for (i = 0; i < n_ops + recog_data.n_dups; i++)
957*c87b03e5Sespie {
958*c87b03e5Sespie int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
959*c87b03e5Sespie rtx *loc = (i < n_ops
960*c87b03e5Sespie ? recog_data.operand_loc[opn]
961*c87b03e5Sespie : recog_data.dup_loc[i - n_ops]);
962*c87b03e5Sespie enum reg_class class = recog_op_alt[opn][alt].class;
963*c87b03e5Sespie
964*c87b03e5Sespie if (recog_data.operand_type[opn] == OP_OUT)
965*c87b03e5Sespie scan_rtx (insn, loc, class, mark_write, OP_OUT,
966*c87b03e5Sespie recog_op_alt[opn][alt].earlyclobber);
967*c87b03e5Sespie }
968*c87b03e5Sespie
969*c87b03e5Sespie /* Step 7: Close chains for registers that were never
970*c87b03e5Sespie really used here. */
971*c87b03e5Sespie for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
972*c87b03e5Sespie if (REG_NOTE_KIND (note) == REG_UNUSED)
973*c87b03e5Sespie scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
974*c87b03e5Sespie OP_IN, 0);
975*c87b03e5Sespie }
976*c87b03e5Sespie if (insn == bb->end)
977*c87b03e5Sespie break;
978*c87b03e5Sespie }
979*c87b03e5Sespie
980*c87b03e5Sespie /* Since we close every chain when we find a REG_DEAD note, anything that
981*c87b03e5Sespie is still open lives past the basic block, so it can't be renamed. */
982*c87b03e5Sespie return closed_chains;
983*c87b03e5Sespie }
984*c87b03e5Sespie
985*c87b03e5Sespie /* Dump all def/use chains in CHAINS to RTL_DUMP_FILE. They are
986*c87b03e5Sespie printed in reverse order as that's how we build them. */
987*c87b03e5Sespie
988*c87b03e5Sespie static void
dump_def_use_chain(chains)989*c87b03e5Sespie dump_def_use_chain (chains)
990*c87b03e5Sespie struct du_chain *chains;
991*c87b03e5Sespie {
992*c87b03e5Sespie while (chains)
993*c87b03e5Sespie {
994*c87b03e5Sespie struct du_chain *this = chains;
995*c87b03e5Sespie int r = REGNO (*this->loc);
996*c87b03e5Sespie int nregs = HARD_REGNO_NREGS (r, GET_MODE (*this->loc));
997*c87b03e5Sespie fprintf (rtl_dump_file, "Register %s (%d):", reg_names[r], nregs);
998*c87b03e5Sespie while (this)
999*c87b03e5Sespie {
1000*c87b03e5Sespie fprintf (rtl_dump_file, " %d [%s]", INSN_UID (this->insn),
1001*c87b03e5Sespie reg_class_names[this->class]);
1002*c87b03e5Sespie this = this->next_use;
1003*c87b03e5Sespie }
1004*c87b03e5Sespie fprintf (rtl_dump_file, "\n");
1005*c87b03e5Sespie chains = chains->next_chain;
1006*c87b03e5Sespie }
1007*c87b03e5Sespie }
1008*c87b03e5Sespie
1009*c87b03e5Sespie /* The following code does forward propagation of hard register copies.
1010*c87b03e5Sespie The object is to eliminate as many dependencies as possible, so that
1011*c87b03e5Sespie we have the most scheduling freedom. As a side effect, we also clean
1012*c87b03e5Sespie up some silly register allocation decisions made by reload. This
1013*c87b03e5Sespie code may be obsoleted by a new register allocator. */
1014*c87b03e5Sespie
1015*c87b03e5Sespie /* For each register, we have a list of registers that contain the same
1016*c87b03e5Sespie value. The OLDEST_REGNO field points to the head of the list, and
1017*c87b03e5Sespie the NEXT_REGNO field runs through the list. The MODE field indicates
1018*c87b03e5Sespie what mode the data is known to be in; this field is VOIDmode when the
1019*c87b03e5Sespie register is not known to contain valid data. */
1020*c87b03e5Sespie
1021*c87b03e5Sespie struct value_data_entry
1022*c87b03e5Sespie {
1023*c87b03e5Sespie enum machine_mode mode;
1024*c87b03e5Sespie unsigned int oldest_regno;
1025*c87b03e5Sespie unsigned int next_regno;
1026*c87b03e5Sespie };
1027*c87b03e5Sespie
1028*c87b03e5Sespie struct value_data
1029*c87b03e5Sespie {
1030*c87b03e5Sespie struct value_data_entry e[FIRST_PSEUDO_REGISTER];
1031*c87b03e5Sespie unsigned int max_value_regs;
1032*c87b03e5Sespie };
1033*c87b03e5Sespie
1034*c87b03e5Sespie static void kill_value_regno PARAMS ((unsigned, struct value_data *));
1035*c87b03e5Sespie static void kill_value PARAMS ((rtx, struct value_data *));
1036*c87b03e5Sespie static void set_value_regno PARAMS ((unsigned, enum machine_mode,
1037*c87b03e5Sespie struct value_data *));
1038*c87b03e5Sespie static void init_value_data PARAMS ((struct value_data *));
1039*c87b03e5Sespie static void kill_clobbered_value PARAMS ((rtx, rtx, void *));
1040*c87b03e5Sespie static void kill_set_value PARAMS ((rtx, rtx, void *));
1041*c87b03e5Sespie static int kill_autoinc_value PARAMS ((rtx *, void *));
1042*c87b03e5Sespie static void copy_value PARAMS ((rtx, rtx, struct value_data *));
1043*c87b03e5Sespie static bool mode_change_ok PARAMS ((enum machine_mode, enum machine_mode,
1044*c87b03e5Sespie unsigned int));
1045*c87b03e5Sespie static rtx maybe_mode_change PARAMS ((enum machine_mode, enum machine_mode,
1046*c87b03e5Sespie enum machine_mode, unsigned int,
1047*c87b03e5Sespie unsigned int));
1048*c87b03e5Sespie static rtx find_oldest_value_reg PARAMS ((enum reg_class, rtx,
1049*c87b03e5Sespie struct value_data *));
1050*c87b03e5Sespie static bool replace_oldest_value_reg PARAMS ((rtx *, enum reg_class, rtx,
1051*c87b03e5Sespie struct value_data *));
1052*c87b03e5Sespie static bool replace_oldest_value_addr PARAMS ((rtx *, enum reg_class,
1053*c87b03e5Sespie enum machine_mode, rtx,
1054*c87b03e5Sespie struct value_data *));
1055*c87b03e5Sespie static bool replace_oldest_value_mem PARAMS ((rtx, rtx, struct value_data *));
1056*c87b03e5Sespie static bool copyprop_hardreg_forward_1 PARAMS ((basic_block,
1057*c87b03e5Sespie struct value_data *));
1058*c87b03e5Sespie extern void debug_value_data PARAMS ((struct value_data *));
1059*c87b03e5Sespie #ifdef ENABLE_CHECKING
1060*c87b03e5Sespie static void validate_value_data PARAMS ((struct value_data *));
1061*c87b03e5Sespie #endif
1062*c87b03e5Sespie
1063*c87b03e5Sespie /* Kill register REGNO. This involves removing it from any value lists,
1064*c87b03e5Sespie and resetting the value mode to VOIDmode. */
1065*c87b03e5Sespie
1066*c87b03e5Sespie static void
kill_value_regno(regno,vd)1067*c87b03e5Sespie kill_value_regno (regno, vd)
1068*c87b03e5Sespie unsigned int regno;
1069*c87b03e5Sespie struct value_data *vd;
1070*c87b03e5Sespie {
1071*c87b03e5Sespie unsigned int i, next;
1072*c87b03e5Sespie
1073*c87b03e5Sespie if (vd->e[regno].oldest_regno != regno)
1074*c87b03e5Sespie {
1075*c87b03e5Sespie for (i = vd->e[regno].oldest_regno;
1076*c87b03e5Sespie vd->e[i].next_regno != regno;
1077*c87b03e5Sespie i = vd->e[i].next_regno)
1078*c87b03e5Sespie continue;
1079*c87b03e5Sespie vd->e[i].next_regno = vd->e[regno].next_regno;
1080*c87b03e5Sespie }
1081*c87b03e5Sespie else if ((next = vd->e[regno].next_regno) != INVALID_REGNUM)
1082*c87b03e5Sespie {
1083*c87b03e5Sespie for (i = next; i != INVALID_REGNUM; i = vd->e[i].next_regno)
1084*c87b03e5Sespie vd->e[i].oldest_regno = next;
1085*c87b03e5Sespie }
1086*c87b03e5Sespie
1087*c87b03e5Sespie vd->e[regno].mode = VOIDmode;
1088*c87b03e5Sespie vd->e[regno].oldest_regno = regno;
1089*c87b03e5Sespie vd->e[regno].next_regno = INVALID_REGNUM;
1090*c87b03e5Sespie
1091*c87b03e5Sespie #ifdef ENABLE_CHECKING
1092*c87b03e5Sespie validate_value_data (vd);
1093*c87b03e5Sespie #endif
1094*c87b03e5Sespie }
1095*c87b03e5Sespie
1096*c87b03e5Sespie /* Kill X. This is a convenience function for kill_value_regno
1097*c87b03e5Sespie so that we mind the mode the register is in. */
1098*c87b03e5Sespie
1099*c87b03e5Sespie static void
kill_value(x,vd)1100*c87b03e5Sespie kill_value (x, vd)
1101*c87b03e5Sespie rtx x;
1102*c87b03e5Sespie struct value_data *vd;
1103*c87b03e5Sespie {
1104*c87b03e5Sespie /* SUBREGS are supposed to have been eliminated by now. But some
1105*c87b03e5Sespie ports, e.g. i386 sse, use them to smuggle vector type information
1106*c87b03e5Sespie through to instruction selection. Each such SUBREG should simplify,
1107*c87b03e5Sespie so if we get a NULL we've done something wrong elsewhere. */
1108*c87b03e5Sespie
1109*c87b03e5Sespie if (GET_CODE (x) == SUBREG)
1110*c87b03e5Sespie x = simplify_subreg (GET_MODE (x), SUBREG_REG (x),
1111*c87b03e5Sespie GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
1112*c87b03e5Sespie if (REG_P (x))
1113*c87b03e5Sespie {
1114*c87b03e5Sespie unsigned int regno = REGNO (x);
1115*c87b03e5Sespie unsigned int n = HARD_REGNO_NREGS (regno, GET_MODE (x));
1116*c87b03e5Sespie unsigned int i, j;
1117*c87b03e5Sespie
1118*c87b03e5Sespie /* Kill the value we're told to kill. */
1119*c87b03e5Sespie for (i = 0; i < n; ++i)
1120*c87b03e5Sespie kill_value_regno (regno + i, vd);
1121*c87b03e5Sespie
1122*c87b03e5Sespie /* Kill everything that overlapped what we're told to kill. */
1123*c87b03e5Sespie if (regno < vd->max_value_regs)
1124*c87b03e5Sespie j = 0;
1125*c87b03e5Sespie else
1126*c87b03e5Sespie j = regno - vd->max_value_regs;
1127*c87b03e5Sespie for (; j < regno; ++j)
1128*c87b03e5Sespie {
1129*c87b03e5Sespie if (vd->e[j].mode == VOIDmode)
1130*c87b03e5Sespie continue;
1131*c87b03e5Sespie n = HARD_REGNO_NREGS (j, vd->e[j].mode);
1132*c87b03e5Sespie if (j + n > regno)
1133*c87b03e5Sespie for (i = 0; i < n; ++i)
1134*c87b03e5Sespie kill_value_regno (j + i, vd);
1135*c87b03e5Sespie }
1136*c87b03e5Sespie }
1137*c87b03e5Sespie }
1138*c87b03e5Sespie
1139*c87b03e5Sespie /* Remember that REGNO is valid in MODE. */
1140*c87b03e5Sespie
1141*c87b03e5Sespie static void
set_value_regno(regno,mode,vd)1142*c87b03e5Sespie set_value_regno (regno, mode, vd)
1143*c87b03e5Sespie unsigned int regno;
1144*c87b03e5Sespie enum machine_mode mode;
1145*c87b03e5Sespie struct value_data *vd;
1146*c87b03e5Sespie {
1147*c87b03e5Sespie unsigned int nregs;
1148*c87b03e5Sespie
1149*c87b03e5Sespie vd->e[regno].mode = mode;
1150*c87b03e5Sespie
1151*c87b03e5Sespie nregs = HARD_REGNO_NREGS (regno, mode);
1152*c87b03e5Sespie if (nregs > vd->max_value_regs)
1153*c87b03e5Sespie vd->max_value_regs = nregs;
1154*c87b03e5Sespie }
1155*c87b03e5Sespie
1156*c87b03e5Sespie /* Initialize VD such that there are no known relationships between regs. */
1157*c87b03e5Sespie
1158*c87b03e5Sespie static void
init_value_data(vd)1159*c87b03e5Sespie init_value_data (vd)
1160*c87b03e5Sespie struct value_data *vd;
1161*c87b03e5Sespie {
1162*c87b03e5Sespie int i;
1163*c87b03e5Sespie for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1164*c87b03e5Sespie {
1165*c87b03e5Sespie vd->e[i].mode = VOIDmode;
1166*c87b03e5Sespie vd->e[i].oldest_regno = i;
1167*c87b03e5Sespie vd->e[i].next_regno = INVALID_REGNUM;
1168*c87b03e5Sespie }
1169*c87b03e5Sespie vd->max_value_regs = 0;
1170*c87b03e5Sespie }
1171*c87b03e5Sespie
1172*c87b03e5Sespie /* Called through note_stores. If X is clobbered, kill its value. */
1173*c87b03e5Sespie
1174*c87b03e5Sespie static void
kill_clobbered_value(x,set,data)1175*c87b03e5Sespie kill_clobbered_value (x, set, data)
1176*c87b03e5Sespie rtx x;
1177*c87b03e5Sespie rtx set;
1178*c87b03e5Sespie void *data;
1179*c87b03e5Sespie {
1180*c87b03e5Sespie struct value_data *vd = data;
1181*c87b03e5Sespie if (GET_CODE (set) == CLOBBER)
1182*c87b03e5Sespie kill_value (x, vd);
1183*c87b03e5Sespie }
1184*c87b03e5Sespie
1185*c87b03e5Sespie /* Called through note_stores. If X is set, not clobbered, kill its
1186*c87b03e5Sespie current value and install it as the root of its own value list. */
1187*c87b03e5Sespie
1188*c87b03e5Sespie static void
kill_set_value(x,set,data)1189*c87b03e5Sespie kill_set_value (x, set, data)
1190*c87b03e5Sespie rtx x;
1191*c87b03e5Sespie rtx set;
1192*c87b03e5Sespie void *data;
1193*c87b03e5Sespie {
1194*c87b03e5Sespie struct value_data *vd = data;
1195*c87b03e5Sespie if (GET_CODE (set) != CLOBBER)
1196*c87b03e5Sespie {
1197*c87b03e5Sespie kill_value (x, vd);
1198*c87b03e5Sespie if (REG_P (x))
1199*c87b03e5Sespie set_value_regno (REGNO (x), GET_MODE (x), vd);
1200*c87b03e5Sespie }
1201*c87b03e5Sespie }
1202*c87b03e5Sespie
1203*c87b03e5Sespie /* Called through for_each_rtx. Kill any register used as the base of an
1204*c87b03e5Sespie auto-increment expression, and install that register as the root of its
1205*c87b03e5Sespie own value list. */
1206*c87b03e5Sespie
1207*c87b03e5Sespie static int
kill_autoinc_value(px,data)1208*c87b03e5Sespie kill_autoinc_value (px, data)
1209*c87b03e5Sespie rtx *px;
1210*c87b03e5Sespie void *data;
1211*c87b03e5Sespie {
1212*c87b03e5Sespie rtx x = *px;
1213*c87b03e5Sespie struct value_data *vd = data;
1214*c87b03e5Sespie
1215*c87b03e5Sespie if (GET_RTX_CLASS (GET_CODE (x)) == 'a')
1216*c87b03e5Sespie {
1217*c87b03e5Sespie x = XEXP (x, 0);
1218*c87b03e5Sespie kill_value (x, vd);
1219*c87b03e5Sespie set_value_regno (REGNO (x), Pmode, vd);
1220*c87b03e5Sespie return -1;
1221*c87b03e5Sespie }
1222*c87b03e5Sespie
1223*c87b03e5Sespie return 0;
1224*c87b03e5Sespie }
1225*c87b03e5Sespie
1226*c87b03e5Sespie /* Assert that SRC has been copied to DEST. Adjust the data structures
1227*c87b03e5Sespie to reflect that SRC contains an older copy of the shared value. */
1228*c87b03e5Sespie
1229*c87b03e5Sespie static void
copy_value(dest,src,vd)1230*c87b03e5Sespie copy_value (dest, src, vd)
1231*c87b03e5Sespie rtx dest;
1232*c87b03e5Sespie rtx src;
1233*c87b03e5Sespie struct value_data *vd;
1234*c87b03e5Sespie {
1235*c87b03e5Sespie unsigned int dr = REGNO (dest);
1236*c87b03e5Sespie unsigned int sr = REGNO (src);
1237*c87b03e5Sespie unsigned int dn, sn;
1238*c87b03e5Sespie unsigned int i;
1239*c87b03e5Sespie
1240*c87b03e5Sespie /* ??? At present, it's possible to see noop sets. It'd be nice if
1241*c87b03e5Sespie this were cleaned up beforehand... */
1242*c87b03e5Sespie if (sr == dr)
1243*c87b03e5Sespie return;
1244*c87b03e5Sespie
1245*c87b03e5Sespie /* Do not propagate copies to the stack pointer, as that can leave
1246*c87b03e5Sespie memory accesses with no scheduling dependency on the stack update. */
1247*c87b03e5Sespie if (dr == STACK_POINTER_REGNUM)
1248*c87b03e5Sespie return;
1249*c87b03e5Sespie
1250*c87b03e5Sespie /* Likewise with the frame pointer, if we're using one. */
1251*c87b03e5Sespie if (frame_pointer_needed && dr == HARD_FRAME_POINTER_REGNUM)
1252*c87b03e5Sespie return;
1253*c87b03e5Sespie
1254*c87b03e5Sespie /* If SRC and DEST overlap, don't record anything. */
1255*c87b03e5Sespie dn = HARD_REGNO_NREGS (dr, GET_MODE (dest));
1256*c87b03e5Sespie sn = HARD_REGNO_NREGS (sr, GET_MODE (dest));
1257*c87b03e5Sespie if ((dr > sr && dr < sr + sn)
1258*c87b03e5Sespie || (sr > dr && sr < dr + dn))
1259*c87b03e5Sespie return;
1260*c87b03e5Sespie
1261*c87b03e5Sespie /* If SRC had no assigned mode (i.e. we didn't know it was live)
1262*c87b03e5Sespie assign it now and assume the value came from an input argument
1263*c87b03e5Sespie or somesuch. */
1264*c87b03e5Sespie if (vd->e[sr].mode == VOIDmode)
1265*c87b03e5Sespie set_value_regno (sr, vd->e[dr].mode, vd);
1266*c87b03e5Sespie
1267*c87b03e5Sespie /* If we are narrowing the input to a smaller number of hard regs,
1268*c87b03e5Sespie and it is in big endian, we are really extracting a high part.
1269*c87b03e5Sespie Since we generally associate a low part of a value with the value itself,
1270*c87b03e5Sespie we must not do the same for the high part.
1271*c87b03e5Sespie Note we can still get low parts for the same mode combination through
1272*c87b03e5Sespie a two-step copy involving differently sized hard regs.
1273*c87b03e5Sespie Assume hard regs fr* are 32 bits bits each, while r* are 64 bits each:
1274*c87b03e5Sespie (set (reg:DI r0) (reg:DI fr0))
1275*c87b03e5Sespie (set (reg:SI fr2) (reg:SI r0))
1276*c87b03e5Sespie loads the low part of (reg:DI fr0) - i.e. fr1 - into fr2, while:
1277*c87b03e5Sespie (set (reg:SI fr2) (reg:SI fr0))
1278*c87b03e5Sespie loads the high part of (reg:DI fr0) into fr2.
1279*c87b03e5Sespie
1280*c87b03e5Sespie We can't properly represent the latter case in our tables, so don't
1281*c87b03e5Sespie record anything then. */
1282*c87b03e5Sespie else if (sn < (unsigned int) HARD_REGNO_NREGS (sr, vd->e[sr].mode)
1283*c87b03e5Sespie && (GET_MODE_SIZE (vd->e[sr].mode) > UNITS_PER_WORD
1284*c87b03e5Sespie ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
1285*c87b03e5Sespie return;
1286*c87b03e5Sespie
1287*c87b03e5Sespie /* If SRC had been assigned a mode narrower than the copy, we can't
1288*c87b03e5Sespie link DEST into the chain, because not all of the pieces of the
1289*c87b03e5Sespie copy came from oldest_regno. */
1290*c87b03e5Sespie else if (sn > (unsigned int) HARD_REGNO_NREGS (sr, vd->e[sr].mode))
1291*c87b03e5Sespie return;
1292*c87b03e5Sespie
1293*c87b03e5Sespie /* Link DR at the end of the value chain used by SR. */
1294*c87b03e5Sespie
1295*c87b03e5Sespie vd->e[dr].oldest_regno = vd->e[sr].oldest_regno;
1296*c87b03e5Sespie
1297*c87b03e5Sespie for (i = sr; vd->e[i].next_regno != INVALID_REGNUM; i = vd->e[i].next_regno)
1298*c87b03e5Sespie continue;
1299*c87b03e5Sespie vd->e[i].next_regno = dr;
1300*c87b03e5Sespie
1301*c87b03e5Sespie #ifdef ENABLE_CHECKING
1302*c87b03e5Sespie validate_value_data (vd);
1303*c87b03e5Sespie #endif
1304*c87b03e5Sespie }
1305*c87b03e5Sespie
1306*c87b03e5Sespie /* Return true if a mode change from ORIG to NEW is allowed for REGNO. */
1307*c87b03e5Sespie
1308*c87b03e5Sespie static bool
mode_change_ok(orig_mode,new_mode,regno)1309*c87b03e5Sespie mode_change_ok (orig_mode, new_mode, regno)
1310*c87b03e5Sespie enum machine_mode orig_mode, new_mode;
1311*c87b03e5Sespie unsigned int regno ATTRIBUTE_UNUSED;
1312*c87b03e5Sespie {
1313*c87b03e5Sespie if (GET_MODE_SIZE (orig_mode) < GET_MODE_SIZE (new_mode))
1314*c87b03e5Sespie return false;
1315*c87b03e5Sespie
1316*c87b03e5Sespie #ifdef CANNOT_CHANGE_MODE_CLASS
1317*c87b03e5Sespie return !REG_CANNOT_CHANGE_MODE_P (regno, orig_mode, new_mode);
1318*c87b03e5Sespie #endif
1319*c87b03e5Sespie
1320*c87b03e5Sespie return true;
1321*c87b03e5Sespie }
1322*c87b03e5Sespie
1323*c87b03e5Sespie /* Register REGNO was originally set in ORIG_MODE. It - or a copy of it -
1324*c87b03e5Sespie was copied in COPY_MODE to COPY_REGNO, and then COPY_REGNO was accessed
1325*c87b03e5Sespie in NEW_MODE.
1326*c87b03e5Sespie Return a NEW_MODE rtx for REGNO if that's OK, otherwise return NULL_RTX. */
1327*c87b03e5Sespie
1328*c87b03e5Sespie static rtx
maybe_mode_change(orig_mode,copy_mode,new_mode,regno,copy_regno)1329*c87b03e5Sespie maybe_mode_change (orig_mode, copy_mode, new_mode, regno, copy_regno)
1330*c87b03e5Sespie enum machine_mode orig_mode, copy_mode, new_mode;
1331*c87b03e5Sespie unsigned int regno, copy_regno;
1332*c87b03e5Sespie {
1333*c87b03e5Sespie if (orig_mode == new_mode)
1334*c87b03e5Sespie return gen_rtx_raw_REG (new_mode, regno);
1335*c87b03e5Sespie else if (mode_change_ok (orig_mode, new_mode, regno))
1336*c87b03e5Sespie {
1337*c87b03e5Sespie int copy_nregs = HARD_REGNO_NREGS (copy_regno, copy_mode);
1338*c87b03e5Sespie int use_nregs = HARD_REGNO_NREGS (copy_regno, new_mode);
1339*c87b03e5Sespie int copy_offset
1340*c87b03e5Sespie = GET_MODE_SIZE (copy_mode) / copy_nregs * (copy_nregs - use_nregs);
1341*c87b03e5Sespie int offset
1342*c87b03e5Sespie = GET_MODE_SIZE (orig_mode) - GET_MODE_SIZE (new_mode) - copy_offset;
1343*c87b03e5Sespie int byteoffset = offset % UNITS_PER_WORD;
1344*c87b03e5Sespie int wordoffset = offset - byteoffset;
1345*c87b03e5Sespie
1346*c87b03e5Sespie offset = ((WORDS_BIG_ENDIAN ? wordoffset : 0)
1347*c87b03e5Sespie + (BYTES_BIG_ENDIAN ? byteoffset : 0));
1348*c87b03e5Sespie return gen_rtx_raw_REG (new_mode,
1349*c87b03e5Sespie regno + subreg_regno_offset (regno, orig_mode,
1350*c87b03e5Sespie offset,
1351*c87b03e5Sespie new_mode));
1352*c87b03e5Sespie }
1353*c87b03e5Sespie return NULL_RTX;
1354*c87b03e5Sespie }
1355*c87b03e5Sespie
1356*c87b03e5Sespie /* Find the oldest copy of the value contained in REGNO that is in
1357*c87b03e5Sespie register class CLASS and has mode MODE. If found, return an rtx
1358*c87b03e5Sespie of that oldest register, otherwise return NULL. */
1359*c87b03e5Sespie
1360*c87b03e5Sespie static rtx
find_oldest_value_reg(class,reg,vd)1361*c87b03e5Sespie find_oldest_value_reg (class, reg, vd)
1362*c87b03e5Sespie enum reg_class class;
1363*c87b03e5Sespie rtx reg;
1364*c87b03e5Sespie struct value_data *vd;
1365*c87b03e5Sespie {
1366*c87b03e5Sespie unsigned int regno = REGNO (reg);
1367*c87b03e5Sespie enum machine_mode mode = GET_MODE (reg);
1368*c87b03e5Sespie unsigned int i;
1369*c87b03e5Sespie
1370*c87b03e5Sespie /* If we are accessing REG in some mode other that what we set it in,
1371*c87b03e5Sespie make sure that the replacement is valid. In particular, consider
1372*c87b03e5Sespie (set (reg:DI r11) (...))
1373*c87b03e5Sespie (set (reg:SI r9) (reg:SI r11))
1374*c87b03e5Sespie (set (reg:SI r10) (...))
1375*c87b03e5Sespie (set (...) (reg:DI r9))
1376*c87b03e5Sespie Replacing r9 with r11 is invalid. */
1377*c87b03e5Sespie if (mode != vd->e[regno].mode)
1378*c87b03e5Sespie {
1379*c87b03e5Sespie if (HARD_REGNO_NREGS (regno, mode)
1380*c87b03e5Sespie > HARD_REGNO_NREGS (regno, vd->e[regno].mode))
1381*c87b03e5Sespie return NULL_RTX;
1382*c87b03e5Sespie }
1383*c87b03e5Sespie
1384*c87b03e5Sespie for (i = vd->e[regno].oldest_regno; i != regno; i = vd->e[i].next_regno)
1385*c87b03e5Sespie {
1386*c87b03e5Sespie enum machine_mode oldmode = vd->e[i].mode;
1387*c87b03e5Sespie rtx new;
1388*c87b03e5Sespie
1389*c87b03e5Sespie if (TEST_HARD_REG_BIT (reg_class_contents[class], i)
1390*c87b03e5Sespie && (new = maybe_mode_change (oldmode, vd->e[regno].mode, mode, i,
1391*c87b03e5Sespie regno)))
1392*c87b03e5Sespie {
1393*c87b03e5Sespie ORIGINAL_REGNO (new) = ORIGINAL_REGNO (reg);
1394*c87b03e5Sespie return new;
1395*c87b03e5Sespie }
1396*c87b03e5Sespie }
1397*c87b03e5Sespie
1398*c87b03e5Sespie return NULL_RTX;
1399*c87b03e5Sespie }
1400*c87b03e5Sespie
1401*c87b03e5Sespie /* If possible, replace the register at *LOC with the oldest register
1402*c87b03e5Sespie in register class CLASS. Return true if successfully replaced. */
1403*c87b03e5Sespie
1404*c87b03e5Sespie static bool
replace_oldest_value_reg(loc,class,insn,vd)1405*c87b03e5Sespie replace_oldest_value_reg (loc, class, insn, vd)
1406*c87b03e5Sespie rtx *loc;
1407*c87b03e5Sespie enum reg_class class;
1408*c87b03e5Sespie rtx insn;
1409*c87b03e5Sespie struct value_data *vd;
1410*c87b03e5Sespie {
1411*c87b03e5Sespie rtx new = find_oldest_value_reg (class, *loc, vd);
1412*c87b03e5Sespie if (new)
1413*c87b03e5Sespie {
1414*c87b03e5Sespie if (rtl_dump_file)
1415*c87b03e5Sespie fprintf (rtl_dump_file, "insn %u: replaced reg %u with %u\n",
1416*c87b03e5Sespie INSN_UID (insn), REGNO (*loc), REGNO (new));
1417*c87b03e5Sespie
1418*c87b03e5Sespie *loc = new;
1419*c87b03e5Sespie return true;
1420*c87b03e5Sespie }
1421*c87b03e5Sespie return false;
1422*c87b03e5Sespie }
1423*c87b03e5Sespie
1424*c87b03e5Sespie /* Similar to replace_oldest_value_reg, but *LOC contains an address.
1425*c87b03e5Sespie Adapted from find_reloads_address_1. CLASS is INDEX_REG_CLASS or
1426*c87b03e5Sespie BASE_REG_CLASS depending on how the register is being considered. */
1427*c87b03e5Sespie
1428*c87b03e5Sespie static bool
replace_oldest_value_addr(loc,class,mode,insn,vd)1429*c87b03e5Sespie replace_oldest_value_addr (loc, class, mode, insn, vd)
1430*c87b03e5Sespie rtx *loc;
1431*c87b03e5Sespie enum reg_class class;
1432*c87b03e5Sespie enum machine_mode mode;
1433*c87b03e5Sespie rtx insn;
1434*c87b03e5Sespie struct value_data *vd;
1435*c87b03e5Sespie {
1436*c87b03e5Sespie rtx x = *loc;
1437*c87b03e5Sespie RTX_CODE code = GET_CODE (x);
1438*c87b03e5Sespie const char *fmt;
1439*c87b03e5Sespie int i, j;
1440*c87b03e5Sespie bool changed = false;
1441*c87b03e5Sespie
1442*c87b03e5Sespie switch (code)
1443*c87b03e5Sespie {
1444*c87b03e5Sespie case PLUS:
1445*c87b03e5Sespie {
1446*c87b03e5Sespie rtx orig_op0 = XEXP (x, 0);
1447*c87b03e5Sespie rtx orig_op1 = XEXP (x, 1);
1448*c87b03e5Sespie RTX_CODE code0 = GET_CODE (orig_op0);
1449*c87b03e5Sespie RTX_CODE code1 = GET_CODE (orig_op1);
1450*c87b03e5Sespie rtx op0 = orig_op0;
1451*c87b03e5Sespie rtx op1 = orig_op1;
1452*c87b03e5Sespie rtx *locI = NULL;
1453*c87b03e5Sespie rtx *locB = NULL;
1454*c87b03e5Sespie
1455*c87b03e5Sespie if (GET_CODE (op0) == SUBREG)
1456*c87b03e5Sespie {
1457*c87b03e5Sespie op0 = SUBREG_REG (op0);
1458*c87b03e5Sespie code0 = GET_CODE (op0);
1459*c87b03e5Sespie }
1460*c87b03e5Sespie
1461*c87b03e5Sespie if (GET_CODE (op1) == SUBREG)
1462*c87b03e5Sespie {
1463*c87b03e5Sespie op1 = SUBREG_REG (op1);
1464*c87b03e5Sespie code1 = GET_CODE (op1);
1465*c87b03e5Sespie }
1466*c87b03e5Sespie
1467*c87b03e5Sespie if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
1468*c87b03e5Sespie || code0 == ZERO_EXTEND || code1 == MEM)
1469*c87b03e5Sespie {
1470*c87b03e5Sespie locI = &XEXP (x, 0);
1471*c87b03e5Sespie locB = &XEXP (x, 1);
1472*c87b03e5Sespie }
1473*c87b03e5Sespie else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
1474*c87b03e5Sespie || code1 == ZERO_EXTEND || code0 == MEM)
1475*c87b03e5Sespie {
1476*c87b03e5Sespie locI = &XEXP (x, 1);
1477*c87b03e5Sespie locB = &XEXP (x, 0);
1478*c87b03e5Sespie }
1479*c87b03e5Sespie else if (code0 == CONST_INT || code0 == CONST
1480*c87b03e5Sespie || code0 == SYMBOL_REF || code0 == LABEL_REF)
1481*c87b03e5Sespie locB = &XEXP (x, 1);
1482*c87b03e5Sespie else if (code1 == CONST_INT || code1 == CONST
1483*c87b03e5Sespie || code1 == SYMBOL_REF || code1 == LABEL_REF)
1484*c87b03e5Sespie locB = &XEXP (x, 0);
1485*c87b03e5Sespie else if (code0 == REG && code1 == REG)
1486*c87b03e5Sespie {
1487*c87b03e5Sespie int index_op;
1488*c87b03e5Sespie
1489*c87b03e5Sespie if (REG_OK_FOR_INDEX_P (op0)
1490*c87b03e5Sespie && REG_MODE_OK_FOR_BASE_P (op1, mode))
1491*c87b03e5Sespie index_op = 0;
1492*c87b03e5Sespie else if (REG_OK_FOR_INDEX_P (op1)
1493*c87b03e5Sespie && REG_MODE_OK_FOR_BASE_P (op0, mode))
1494*c87b03e5Sespie index_op = 1;
1495*c87b03e5Sespie else if (REG_MODE_OK_FOR_BASE_P (op1, mode))
1496*c87b03e5Sespie index_op = 0;
1497*c87b03e5Sespie else if (REG_MODE_OK_FOR_BASE_P (op0, mode))
1498*c87b03e5Sespie index_op = 1;
1499*c87b03e5Sespie else if (REG_OK_FOR_INDEX_P (op1))
1500*c87b03e5Sespie index_op = 1;
1501*c87b03e5Sespie else
1502*c87b03e5Sespie index_op = 0;
1503*c87b03e5Sespie
1504*c87b03e5Sespie locI = &XEXP (x, index_op);
1505*c87b03e5Sespie locB = &XEXP (x, !index_op);
1506*c87b03e5Sespie }
1507*c87b03e5Sespie else if (code0 == REG)
1508*c87b03e5Sespie {
1509*c87b03e5Sespie locI = &XEXP (x, 0);
1510*c87b03e5Sespie locB = &XEXP (x, 1);
1511*c87b03e5Sespie }
1512*c87b03e5Sespie else if (code1 == REG)
1513*c87b03e5Sespie {
1514*c87b03e5Sespie locI = &XEXP (x, 1);
1515*c87b03e5Sespie locB = &XEXP (x, 0);
1516*c87b03e5Sespie }
1517*c87b03e5Sespie
1518*c87b03e5Sespie if (locI)
1519*c87b03e5Sespie changed |= replace_oldest_value_addr (locI, INDEX_REG_CLASS, mode,
1520*c87b03e5Sespie insn, vd);
1521*c87b03e5Sespie if (locB)
1522*c87b03e5Sespie changed |= replace_oldest_value_addr (locB,
1523*c87b03e5Sespie MODE_BASE_REG_CLASS (mode),
1524*c87b03e5Sespie mode, insn, vd);
1525*c87b03e5Sespie return changed;
1526*c87b03e5Sespie }
1527*c87b03e5Sespie
1528*c87b03e5Sespie case POST_INC:
1529*c87b03e5Sespie case POST_DEC:
1530*c87b03e5Sespie case POST_MODIFY:
1531*c87b03e5Sespie case PRE_INC:
1532*c87b03e5Sespie case PRE_DEC:
1533*c87b03e5Sespie case PRE_MODIFY:
1534*c87b03e5Sespie return false;
1535*c87b03e5Sespie
1536*c87b03e5Sespie case MEM:
1537*c87b03e5Sespie return replace_oldest_value_mem (x, insn, vd);
1538*c87b03e5Sespie
1539*c87b03e5Sespie case REG:
1540*c87b03e5Sespie return replace_oldest_value_reg (loc, class, insn, vd);
1541*c87b03e5Sespie
1542*c87b03e5Sespie default:
1543*c87b03e5Sespie break;
1544*c87b03e5Sespie }
1545*c87b03e5Sespie
1546*c87b03e5Sespie fmt = GET_RTX_FORMAT (code);
1547*c87b03e5Sespie for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1548*c87b03e5Sespie {
1549*c87b03e5Sespie if (fmt[i] == 'e')
1550*c87b03e5Sespie changed |= replace_oldest_value_addr (&XEXP (x, i), class, mode,
1551*c87b03e5Sespie insn, vd);
1552*c87b03e5Sespie else if (fmt[i] == 'E')
1553*c87b03e5Sespie for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1554*c87b03e5Sespie changed |= replace_oldest_value_addr (&XVECEXP (x, i, j), class,
1555*c87b03e5Sespie mode, insn, vd);
1556*c87b03e5Sespie }
1557*c87b03e5Sespie
1558*c87b03e5Sespie return changed;
1559*c87b03e5Sespie }
1560*c87b03e5Sespie
1561*c87b03e5Sespie /* Similar to replace_oldest_value_reg, but X contains a memory. */
1562*c87b03e5Sespie
1563*c87b03e5Sespie static bool
replace_oldest_value_mem(x,insn,vd)1564*c87b03e5Sespie replace_oldest_value_mem (x, insn, vd)
1565*c87b03e5Sespie rtx x;
1566*c87b03e5Sespie rtx insn;
1567*c87b03e5Sespie struct value_data *vd;
1568*c87b03e5Sespie {
1569*c87b03e5Sespie return replace_oldest_value_addr (&XEXP (x, 0),
1570*c87b03e5Sespie MODE_BASE_REG_CLASS (GET_MODE (x)),
1571*c87b03e5Sespie GET_MODE (x), insn, vd);
1572*c87b03e5Sespie }
1573*c87b03e5Sespie
1574*c87b03e5Sespie /* Perform the forward copy propagation on basic block BB. */
1575*c87b03e5Sespie
1576*c87b03e5Sespie static bool
copyprop_hardreg_forward_1(bb,vd)1577*c87b03e5Sespie copyprop_hardreg_forward_1 (bb, vd)
1578*c87b03e5Sespie basic_block bb;
1579*c87b03e5Sespie struct value_data *vd;
1580*c87b03e5Sespie {
1581*c87b03e5Sespie bool changed = false;
1582*c87b03e5Sespie rtx insn;
1583*c87b03e5Sespie
1584*c87b03e5Sespie for (insn = bb->head; ; insn = NEXT_INSN (insn))
1585*c87b03e5Sespie {
1586*c87b03e5Sespie int n_ops, i, alt, predicated;
1587*c87b03e5Sespie bool is_asm;
1588*c87b03e5Sespie rtx set;
1589*c87b03e5Sespie
1590*c87b03e5Sespie if (! INSN_P (insn))
1591*c87b03e5Sespie {
1592*c87b03e5Sespie if (insn == bb->end)
1593*c87b03e5Sespie break;
1594*c87b03e5Sespie else
1595*c87b03e5Sespie continue;
1596*c87b03e5Sespie }
1597*c87b03e5Sespie
1598*c87b03e5Sespie set = single_set (insn);
1599*c87b03e5Sespie extract_insn (insn);
1600*c87b03e5Sespie if (! constrain_operands (1))
1601*c87b03e5Sespie fatal_insn_not_found (insn);
1602*c87b03e5Sespie preprocess_constraints ();
1603*c87b03e5Sespie alt = which_alternative;
1604*c87b03e5Sespie n_ops = recog_data.n_operands;
1605*c87b03e5Sespie is_asm = asm_noperands (PATTERN (insn)) >= 0;
1606*c87b03e5Sespie
1607*c87b03e5Sespie /* Simplify the code below by rewriting things to reflect
1608*c87b03e5Sespie matching constraints. Also promote OP_OUT to OP_INOUT
1609*c87b03e5Sespie in predicated instructions. */
1610*c87b03e5Sespie
1611*c87b03e5Sespie predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
1612*c87b03e5Sespie for (i = 0; i < n_ops; ++i)
1613*c87b03e5Sespie {
1614*c87b03e5Sespie int matches = recog_op_alt[i][alt].matches;
1615*c87b03e5Sespie if (matches >= 0)
1616*c87b03e5Sespie recog_op_alt[i][alt].class = recog_op_alt[matches][alt].class;
1617*c87b03e5Sespie if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
1618*c87b03e5Sespie || (predicated && recog_data.operand_type[i] == OP_OUT))
1619*c87b03e5Sespie recog_data.operand_type[i] = OP_INOUT;
1620*c87b03e5Sespie }
1621*c87b03e5Sespie
1622*c87b03e5Sespie /* For each earlyclobber operand, zap the value data. */
1623*c87b03e5Sespie for (i = 0; i < n_ops; i++)
1624*c87b03e5Sespie if (recog_op_alt[i][alt].earlyclobber)
1625*c87b03e5Sespie kill_value (recog_data.operand[i], vd);
1626*c87b03e5Sespie
1627*c87b03e5Sespie /* Within asms, a clobber cannot overlap inputs or outputs.
1628*c87b03e5Sespie I wouldn't think this were true for regular insns, but
1629*c87b03e5Sespie scan_rtx treats them like that... */
1630*c87b03e5Sespie note_stores (PATTERN (insn), kill_clobbered_value, vd);
1631*c87b03e5Sespie
1632*c87b03e5Sespie /* Kill all auto-incremented values. */
1633*c87b03e5Sespie /* ??? REG_INC is useless, since stack pushes aren't done that way. */
1634*c87b03e5Sespie for_each_rtx (&PATTERN (insn), kill_autoinc_value, vd);
1635*c87b03e5Sespie
1636*c87b03e5Sespie /* Kill all early-clobbered operands. */
1637*c87b03e5Sespie for (i = 0; i < n_ops; i++)
1638*c87b03e5Sespie if (recog_op_alt[i][alt].earlyclobber)
1639*c87b03e5Sespie kill_value (recog_data.operand[i], vd);
1640*c87b03e5Sespie
1641*c87b03e5Sespie /* Special-case plain move instructions, since we may well
1642*c87b03e5Sespie be able to do the move from a different register class. */
1643*c87b03e5Sespie if (set && REG_P (SET_SRC (set)))
1644*c87b03e5Sespie {
1645*c87b03e5Sespie rtx src = SET_SRC (set);
1646*c87b03e5Sespie unsigned int regno = REGNO (src);
1647*c87b03e5Sespie enum machine_mode mode = GET_MODE (src);
1648*c87b03e5Sespie unsigned int i;
1649*c87b03e5Sespie rtx new;
1650*c87b03e5Sespie
1651*c87b03e5Sespie /* If we are accessing SRC in some mode other that what we
1652*c87b03e5Sespie set it in, make sure that the replacement is valid. */
1653*c87b03e5Sespie if (mode != vd->e[regno].mode)
1654*c87b03e5Sespie {
1655*c87b03e5Sespie if (HARD_REGNO_NREGS (regno, mode)
1656*c87b03e5Sespie > HARD_REGNO_NREGS (regno, vd->e[regno].mode))
1657*c87b03e5Sespie goto no_move_special_case;
1658*c87b03e5Sespie }
1659*c87b03e5Sespie
1660*c87b03e5Sespie /* If the destination is also a register, try to find a source
1661*c87b03e5Sespie register in the same class. */
1662*c87b03e5Sespie if (REG_P (SET_DEST (set)))
1663*c87b03e5Sespie {
1664*c87b03e5Sespie new = find_oldest_value_reg (REGNO_REG_CLASS (regno), src, vd);
1665*c87b03e5Sespie if (new && validate_change (insn, &SET_SRC (set), new, 0))
1666*c87b03e5Sespie {
1667*c87b03e5Sespie if (rtl_dump_file)
1668*c87b03e5Sespie fprintf (rtl_dump_file,
1669*c87b03e5Sespie "insn %u: replaced reg %u with %u\n",
1670*c87b03e5Sespie INSN_UID (insn), regno, REGNO (new));
1671*c87b03e5Sespie changed = true;
1672*c87b03e5Sespie goto did_replacement;
1673*c87b03e5Sespie }
1674*c87b03e5Sespie }
1675*c87b03e5Sespie
1676*c87b03e5Sespie /* Otherwise, try all valid registers and see if its valid. */
1677*c87b03e5Sespie for (i = vd->e[regno].oldest_regno; i != regno;
1678*c87b03e5Sespie i = vd->e[i].next_regno)
1679*c87b03e5Sespie {
1680*c87b03e5Sespie new = maybe_mode_change (vd->e[i].mode, vd->e[regno].mode,
1681*c87b03e5Sespie mode, i, regno);
1682*c87b03e5Sespie if (new != NULL_RTX)
1683*c87b03e5Sespie {
1684*c87b03e5Sespie if (validate_change (insn, &SET_SRC (set), new, 0))
1685*c87b03e5Sespie {
1686*c87b03e5Sespie ORIGINAL_REGNO (new) = ORIGINAL_REGNO (src);
1687*c87b03e5Sespie if (rtl_dump_file)
1688*c87b03e5Sespie fprintf (rtl_dump_file,
1689*c87b03e5Sespie "insn %u: replaced reg %u with %u\n",
1690*c87b03e5Sespie INSN_UID (insn), regno, REGNO (new));
1691*c87b03e5Sespie changed = true;
1692*c87b03e5Sespie goto did_replacement;
1693*c87b03e5Sespie }
1694*c87b03e5Sespie }
1695*c87b03e5Sespie }
1696*c87b03e5Sespie }
1697*c87b03e5Sespie no_move_special_case:
1698*c87b03e5Sespie
1699*c87b03e5Sespie /* For each input operand, replace a hard register with the
1700*c87b03e5Sespie eldest live copy that's in an appropriate register class. */
1701*c87b03e5Sespie for (i = 0; i < n_ops; i++)
1702*c87b03e5Sespie {
1703*c87b03e5Sespie bool replaced = false;
1704*c87b03e5Sespie
1705*c87b03e5Sespie /* Don't scan match_operand here, since we've no reg class
1706*c87b03e5Sespie information to pass down. Any operands that we could
1707*c87b03e5Sespie substitute in will be represented elsewhere. */
1708*c87b03e5Sespie if (recog_data.constraints[i][0] == '\0')
1709*c87b03e5Sespie continue;
1710*c87b03e5Sespie
1711*c87b03e5Sespie /* Don't replace in asms intentionally referencing hard regs. */
1712*c87b03e5Sespie if (is_asm && GET_CODE (recog_data.operand[i]) == REG
1713*c87b03e5Sespie && (REGNO (recog_data.operand[i])
1714*c87b03e5Sespie == ORIGINAL_REGNO (recog_data.operand[i])))
1715*c87b03e5Sespie continue;
1716*c87b03e5Sespie
1717*c87b03e5Sespie if (recog_data.operand_type[i] == OP_IN)
1718*c87b03e5Sespie {
1719*c87b03e5Sespie if (recog_op_alt[i][alt].is_address)
1720*c87b03e5Sespie replaced
1721*c87b03e5Sespie = replace_oldest_value_addr (recog_data.operand_loc[i],
1722*c87b03e5Sespie recog_op_alt[i][alt].class,
1723*c87b03e5Sespie VOIDmode, insn, vd);
1724*c87b03e5Sespie else if (REG_P (recog_data.operand[i]))
1725*c87b03e5Sespie replaced
1726*c87b03e5Sespie = replace_oldest_value_reg (recog_data.operand_loc[i],
1727*c87b03e5Sespie recog_op_alt[i][alt].class,
1728*c87b03e5Sespie insn, vd);
1729*c87b03e5Sespie else if (GET_CODE (recog_data.operand[i]) == MEM)
1730*c87b03e5Sespie replaced = replace_oldest_value_mem (recog_data.operand[i],
1731*c87b03e5Sespie insn, vd);
1732*c87b03e5Sespie }
1733*c87b03e5Sespie else if (GET_CODE (recog_data.operand[i]) == MEM)
1734*c87b03e5Sespie replaced = replace_oldest_value_mem (recog_data.operand[i],
1735*c87b03e5Sespie insn, vd);
1736*c87b03e5Sespie
1737*c87b03e5Sespie /* If we performed any replacement, update match_dups. */
1738*c87b03e5Sespie if (replaced)
1739*c87b03e5Sespie {
1740*c87b03e5Sespie int j;
1741*c87b03e5Sespie rtx new;
1742*c87b03e5Sespie
1743*c87b03e5Sespie changed = true;
1744*c87b03e5Sespie
1745*c87b03e5Sespie new = *recog_data.operand_loc[i];
1746*c87b03e5Sespie recog_data.operand[i] = new;
1747*c87b03e5Sespie for (j = 0; j < recog_data.n_dups; j++)
1748*c87b03e5Sespie if (recog_data.dup_num[j] == i)
1749*c87b03e5Sespie *recog_data.dup_loc[j] = new;
1750*c87b03e5Sespie }
1751*c87b03e5Sespie }
1752*c87b03e5Sespie
1753*c87b03e5Sespie did_replacement:
1754*c87b03e5Sespie /* Clobber call-clobbered registers. */
1755*c87b03e5Sespie if (GET_CODE (insn) == CALL_INSN)
1756*c87b03e5Sespie for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1757*c87b03e5Sespie if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1758*c87b03e5Sespie kill_value_regno (i, vd);
1759*c87b03e5Sespie
1760*c87b03e5Sespie /* Notice stores. */
1761*c87b03e5Sespie note_stores (PATTERN (insn), kill_set_value, vd);
1762*c87b03e5Sespie
1763*c87b03e5Sespie /* Notice copies. */
1764*c87b03e5Sespie if (set && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
1765*c87b03e5Sespie copy_value (SET_DEST (set), SET_SRC (set), vd);
1766*c87b03e5Sespie
1767*c87b03e5Sespie if (insn == bb->end)
1768*c87b03e5Sespie break;
1769*c87b03e5Sespie }
1770*c87b03e5Sespie
1771*c87b03e5Sespie return changed;
1772*c87b03e5Sespie }
1773*c87b03e5Sespie
1774*c87b03e5Sespie /* Main entry point for the forward copy propagation optimization. */
1775*c87b03e5Sespie
1776*c87b03e5Sespie void
copyprop_hardreg_forward()1777*c87b03e5Sespie copyprop_hardreg_forward ()
1778*c87b03e5Sespie {
1779*c87b03e5Sespie struct value_data *all_vd;
1780*c87b03e5Sespie bool need_refresh;
1781*c87b03e5Sespie basic_block bb, bbp = 0;
1782*c87b03e5Sespie
1783*c87b03e5Sespie need_refresh = false;
1784*c87b03e5Sespie
1785*c87b03e5Sespie all_vd = xmalloc (sizeof (struct value_data) * last_basic_block);
1786*c87b03e5Sespie
1787*c87b03e5Sespie FOR_EACH_BB (bb)
1788*c87b03e5Sespie {
1789*c87b03e5Sespie /* If a block has a single predecessor, that we've already
1790*c87b03e5Sespie processed, begin with the value data that was live at
1791*c87b03e5Sespie the end of the predecessor block. */
1792*c87b03e5Sespie /* ??? Ought to use more intelligent queueing of blocks. */
1793*c87b03e5Sespie if (bb->pred)
1794*c87b03e5Sespie for (bbp = bb; bbp && bbp != bb->pred->src; bbp = bbp->prev_bb);
1795*c87b03e5Sespie if (bb->pred
1796*c87b03e5Sespie && ! bb->pred->pred_next
1797*c87b03e5Sespie && ! (bb->pred->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
1798*c87b03e5Sespie && bb->pred->src != ENTRY_BLOCK_PTR
1799*c87b03e5Sespie && bbp)
1800*c87b03e5Sespie all_vd[bb->index] = all_vd[bb->pred->src->index];
1801*c87b03e5Sespie else
1802*c87b03e5Sespie init_value_data (all_vd + bb->index);
1803*c87b03e5Sespie
1804*c87b03e5Sespie if (copyprop_hardreg_forward_1 (bb, all_vd + bb->index))
1805*c87b03e5Sespie need_refresh = true;
1806*c87b03e5Sespie }
1807*c87b03e5Sespie
1808*c87b03e5Sespie if (need_refresh)
1809*c87b03e5Sespie {
1810*c87b03e5Sespie if (rtl_dump_file)
1811*c87b03e5Sespie fputs ("\n\n", rtl_dump_file);
1812*c87b03e5Sespie
1813*c87b03e5Sespie /* ??? Irritatingly, delete_noop_moves does not take a set of blocks
1814*c87b03e5Sespie to scan, so we have to do a life update with no initial set of
1815*c87b03e5Sespie blocks Just In Case. */
1816*c87b03e5Sespie delete_noop_moves (get_insns ());
1817*c87b03e5Sespie update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES,
1818*c87b03e5Sespie PROP_DEATH_NOTES
1819*c87b03e5Sespie | PROP_SCAN_DEAD_CODE
1820*c87b03e5Sespie | PROP_KILL_DEAD_CODE);
1821*c87b03e5Sespie }
1822*c87b03e5Sespie
1823*c87b03e5Sespie free (all_vd);
1824*c87b03e5Sespie }
1825*c87b03e5Sespie
1826*c87b03e5Sespie /* Dump the value chain data to stderr. */
1827*c87b03e5Sespie
1828*c87b03e5Sespie void
debug_value_data(vd)1829*c87b03e5Sespie debug_value_data (vd)
1830*c87b03e5Sespie struct value_data *vd;
1831*c87b03e5Sespie {
1832*c87b03e5Sespie HARD_REG_SET set;
1833*c87b03e5Sespie unsigned int i, j;
1834*c87b03e5Sespie
1835*c87b03e5Sespie CLEAR_HARD_REG_SET (set);
1836*c87b03e5Sespie
1837*c87b03e5Sespie for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1838*c87b03e5Sespie if (vd->e[i].oldest_regno == i)
1839*c87b03e5Sespie {
1840*c87b03e5Sespie if (vd->e[i].mode == VOIDmode)
1841*c87b03e5Sespie {
1842*c87b03e5Sespie if (vd->e[i].next_regno != INVALID_REGNUM)
1843*c87b03e5Sespie fprintf (stderr, "[%u] Bad next_regno for empty chain (%u)\n",
1844*c87b03e5Sespie i, vd->e[i].next_regno);
1845*c87b03e5Sespie continue;
1846*c87b03e5Sespie }
1847*c87b03e5Sespie
1848*c87b03e5Sespie SET_HARD_REG_BIT (set, i);
1849*c87b03e5Sespie fprintf (stderr, "[%u %s] ", i, GET_MODE_NAME (vd->e[i].mode));
1850*c87b03e5Sespie
1851*c87b03e5Sespie for (j = vd->e[i].next_regno;
1852*c87b03e5Sespie j != INVALID_REGNUM;
1853*c87b03e5Sespie j = vd->e[j].next_regno)
1854*c87b03e5Sespie {
1855*c87b03e5Sespie if (TEST_HARD_REG_BIT (set, j))
1856*c87b03e5Sespie {
1857*c87b03e5Sespie fprintf (stderr, "[%u] Loop in regno chain\n", j);
1858*c87b03e5Sespie return;
1859*c87b03e5Sespie }
1860*c87b03e5Sespie
1861*c87b03e5Sespie if (vd->e[j].oldest_regno != i)
1862*c87b03e5Sespie {
1863*c87b03e5Sespie fprintf (stderr, "[%u] Bad oldest_regno (%u)\n",
1864*c87b03e5Sespie j, vd->e[j].oldest_regno);
1865*c87b03e5Sespie return;
1866*c87b03e5Sespie }
1867*c87b03e5Sespie SET_HARD_REG_BIT (set, j);
1868*c87b03e5Sespie fprintf (stderr, "[%u %s] ", j, GET_MODE_NAME (vd->e[j].mode));
1869*c87b03e5Sespie }
1870*c87b03e5Sespie fputc ('\n', stderr);
1871*c87b03e5Sespie }
1872*c87b03e5Sespie
1873*c87b03e5Sespie for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1874*c87b03e5Sespie if (! TEST_HARD_REG_BIT (set, i)
1875*c87b03e5Sespie && (vd->e[i].mode != VOIDmode
1876*c87b03e5Sespie || vd->e[i].oldest_regno != i
1877*c87b03e5Sespie || vd->e[i].next_regno != INVALID_REGNUM))
1878*c87b03e5Sespie fprintf (stderr, "[%u] Non-empty reg in chain (%s %u %i)\n",
1879*c87b03e5Sespie i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1880*c87b03e5Sespie vd->e[i].next_regno);
1881*c87b03e5Sespie }
1882*c87b03e5Sespie
1883*c87b03e5Sespie #ifdef ENABLE_CHECKING
1884*c87b03e5Sespie static void
validate_value_data(vd)1885*c87b03e5Sespie validate_value_data (vd)
1886*c87b03e5Sespie struct value_data *vd;
1887*c87b03e5Sespie {
1888*c87b03e5Sespie HARD_REG_SET set;
1889*c87b03e5Sespie unsigned int i, j;
1890*c87b03e5Sespie
1891*c87b03e5Sespie CLEAR_HARD_REG_SET (set);
1892*c87b03e5Sespie
1893*c87b03e5Sespie for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1894*c87b03e5Sespie if (vd->e[i].oldest_regno == i)
1895*c87b03e5Sespie {
1896*c87b03e5Sespie if (vd->e[i].mode == VOIDmode)
1897*c87b03e5Sespie {
1898*c87b03e5Sespie if (vd->e[i].next_regno != INVALID_REGNUM)
1899*c87b03e5Sespie internal_error ("validate_value_data: [%u] Bad next_regno for empty chain (%u)",
1900*c87b03e5Sespie i, vd->e[i].next_regno);
1901*c87b03e5Sespie continue;
1902*c87b03e5Sespie }
1903*c87b03e5Sespie
1904*c87b03e5Sespie SET_HARD_REG_BIT (set, i);
1905*c87b03e5Sespie
1906*c87b03e5Sespie for (j = vd->e[i].next_regno;
1907*c87b03e5Sespie j != INVALID_REGNUM;
1908*c87b03e5Sespie j = vd->e[j].next_regno)
1909*c87b03e5Sespie {
1910*c87b03e5Sespie if (TEST_HARD_REG_BIT (set, j))
1911*c87b03e5Sespie internal_error ("validate_value_data: Loop in regno chain (%u)",
1912*c87b03e5Sespie j);
1913*c87b03e5Sespie if (vd->e[j].oldest_regno != i)
1914*c87b03e5Sespie internal_error ("validate_value_data: [%u] Bad oldest_regno (%u)",
1915*c87b03e5Sespie j, vd->e[j].oldest_regno);
1916*c87b03e5Sespie
1917*c87b03e5Sespie SET_HARD_REG_BIT (set, j);
1918*c87b03e5Sespie }
1919*c87b03e5Sespie }
1920*c87b03e5Sespie
1921*c87b03e5Sespie for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1922*c87b03e5Sespie if (! TEST_HARD_REG_BIT (set, i)
1923*c87b03e5Sespie && (vd->e[i].mode != VOIDmode
1924*c87b03e5Sespie || vd->e[i].oldest_regno != i
1925*c87b03e5Sespie || vd->e[i].next_regno != INVALID_REGNUM))
1926*c87b03e5Sespie internal_error ("validate_value_data: [%u] Non-empty reg in chain (%s %u %i)",
1927*c87b03e5Sespie i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1928*c87b03e5Sespie vd->e[i].next_regno);
1929*c87b03e5Sespie }
1930*c87b03e5Sespie #endif
1931