2005-01-20 Michael Koch <konqueror@gmx.de>
[official-gcc.git] / gcc / web.c
blob11f2caa6cd2e4983e57c4495fff06b8d334fc428
1 /* Web construction code for GNU compiler.
2 Contributed by Jan Hubicka.
3 Copyright (C) 2001, 2002, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* Simple optimization pass that splits independent uses of each pseudo,
23 increasing effectiveness of other optimizations. The optimization can
24 serve as an example of use for the dataflow module.
26 We don't split registers with REG_USERVAR set unless -fmessy-debugging
27 is specified, because debugging information about such split variables
28 is almost unusable.
30 TODO
31 - Add code to keep debugging up-to-date after splitting user variable
32 pseudos. This can be done by keeping track of all the pseudos used
33 for the variable and using life analysis information before reload
34 to determine which one is live and, in case more than one are live,
35 choose the one with the latest definition.
37 Other optimization passes can benefit from the infrastructure too.
39 - We may use profile information and ignore infrequent use for the
40 purpose of web unifying, inserting the compensation code later to
41 implement full induction variable expansion for loops (currently
42 we expand only if the induction variable is dead afterward, which
43 is often the case). */
45 #include "config.h"
46 #include "system.h"
47 #include "coretypes.h"
48 #include "tm.h"
49 #include "toplev.h"
51 #include "rtl.h"
52 #include "hard-reg-set.h"
53 #include "flags.h"
54 #include "basic-block.h"
55 #include "output.h"
56 #include "df.h"
57 #include "function.h"
60 /* This entry is allocated for each reference in the insn stream. */
61 struct web_entry
63 /* Pointer to the parent in the union/find tree. */
64 struct web_entry *pred;
65 /* Newly assigned register to the entry. Set only for roots. */
66 rtx reg;
69 static struct web_entry *unionfind_root (struct web_entry *);
70 static void unionfind_union (struct web_entry *, struct web_entry *);
71 static void union_defs (struct df *, struct ref *, struct web_entry *,
72 struct web_entry *);
73 static rtx entry_register (struct web_entry *, struct ref *, char *, char *);
74 static void replace_ref (struct ref *, rtx);
75 static int mark_addressof (rtx *, void *);
77 /* Find the root of unionfind tree (the representative of set). */
79 static struct web_entry *
80 unionfind_root (struct web_entry *element)
82 struct web_entry *element1 = element, *element2;
84 while (element->pred)
85 element = element->pred;
86 while (element1->pred)
88 element2 = element1->pred;
89 element1->pred = element;
90 element1 = element2;
92 return element;
95 /* Union sets. */
97 static void
98 unionfind_union (struct web_entry *first, struct web_entry *second)
100 first = unionfind_root (first);
101 second = unionfind_root (second);
102 if (first == second)
103 return;
104 second->pred = first;
107 /* For each use, all possible defs reaching it must come in the same
108 register, union them. */
110 static void
111 union_defs (struct df *df, struct ref *use, struct web_entry *def_entry,
112 struct web_entry *use_entry)
114 rtx insn = DF_REF_INSN (use);
115 struct df_link *link = DF_REF_CHAIN (use);
116 struct df_link *use_link = DF_INSN_USES (df, insn);
117 struct df_link *def_link = DF_INSN_DEFS (df, insn);
118 rtx set = single_set (insn);
120 /* Some instructions may use match_dup for their operands. In case the
121 operands are dead, we will assign them different pseudos, creating
122 invalid instructions, so union all uses of the same operand for each
123 insn. */
125 while (use_link)
127 if (use != use_link->ref
128 && DF_REF_REAL_REG (use) == DF_REF_REAL_REG (use_link->ref))
129 unionfind_union (use_entry + DF_REF_ID (use),
130 use_entry + DF_REF_ID (use_link->ref));
131 use_link = use_link->next;
134 /* Recognize trivial noop moves and attempt to keep them as noop.
135 While most of noop moves should be removed, we still keep some
136 of them at libcall boundaries and such. */
138 if (set
139 && SET_SRC (set) == DF_REF_REG (use)
140 && SET_SRC (set) == SET_DEST (set))
142 while (def_link)
144 if (DF_REF_REAL_REG (use) == DF_REF_REAL_REG (def_link->ref))
145 unionfind_union (use_entry + DF_REF_ID (use),
146 def_entry + DF_REF_ID (def_link->ref));
147 def_link = def_link->next;
150 while (link)
152 unionfind_union (use_entry + DF_REF_ID (use),
153 def_entry + DF_REF_ID (link->ref));
154 link = link->next;
157 /* A READ_WRITE use requires the corresponding def to be in the same
158 register. Find it and union. */
159 if (use->flags & DF_REF_READ_WRITE)
161 struct df_link *link = DF_INSN_DEFS (df, DF_REF_INSN (use));
163 while (DF_REF_REAL_REG (link->ref) != DF_REF_REAL_REG (use))
164 link = link->next;
166 unionfind_union (use_entry + DF_REF_ID (use),
167 def_entry + DF_REF_ID (link->ref));
171 /* Find the corresponding register for the given entry. */
173 static rtx
174 entry_register (struct web_entry *entry, struct ref *ref, char *used,
175 char *use_addressof)
177 struct web_entry *root;
178 rtx reg, newreg;
180 /* Find the corresponding web and see if it has been visited. */
181 root = unionfind_root (entry);
182 if (root->reg)
183 return root->reg;
185 /* We are seeing this web for the first time, do the assignment. */
186 reg = DF_REF_REAL_REG (ref);
188 /* In case the original register is already assigned, generate new one. */
189 if (!used[REGNO (reg)])
190 newreg = reg, used[REGNO (reg)] = 1;
191 else if (REG_USERVAR_P (reg) && 0/*&& !flag_messy_debugging*/)
193 newreg = reg;
194 if (dump_file)
195 fprintf (dump_file,
196 "New web forced to keep reg=%i (user variable)\n",
197 REGNO (reg));
199 else if (use_addressof [REGNO (reg)])
201 newreg = reg;
202 if (dump_file)
203 fprintf (dump_file,
204 "New web forced to keep reg=%i (address taken)\n",
205 REGNO (reg));
207 else
209 newreg = gen_reg_rtx (GET_MODE (reg));
210 REG_USERVAR_P (newreg) = REG_USERVAR_P (reg);
211 REG_POINTER (newreg) = REG_POINTER (reg);
212 REG_LOOP_TEST_P (newreg) = REG_LOOP_TEST_P (reg);
213 RTX_UNCHANGING_P (newreg) = RTX_UNCHANGING_P (reg);
214 REG_ATTRS (newreg) = REG_ATTRS (reg);
215 if (dump_file)
216 fprintf (dump_file, "Web oldreg=%i newreg=%i\n", REGNO (reg),
217 REGNO (newreg));
220 root->reg = newreg;
221 return newreg;
224 /* Replace the reference by REG. */
226 static void
227 replace_ref (struct ref *ref, rtx reg)
229 rtx oldreg = DF_REF_REAL_REG (ref);
230 rtx *loc = DF_REF_REAL_LOC (ref);
232 if (oldreg == reg)
233 return;
234 if (dump_file)
235 fprintf (dump_file, "Updating insn %i (%i->%i)\n",
236 INSN_UID (DF_REF_INSN (ref)), REGNO (oldreg), REGNO (reg));
237 *loc = reg;
240 /* Mark each pseudo whose address is taken. */
242 static int
243 mark_addressof (rtx *rtl, void *data)
245 if (!*rtl)
246 return 0;
247 if (GET_CODE (*rtl) == ADDRESSOF
248 && REG_P (XEXP (*rtl, 0)))
249 ((char *)data)[REGNO (XEXP (*rtl, 0))] = 1;
250 return 0;
253 /* Main entry point. */
255 void
256 web_main (void)
258 struct df *df;
259 struct web_entry *def_entry;
260 struct web_entry *use_entry;
261 unsigned int i;
262 int max = max_reg_num ();
263 char *used;
264 char *use_addressof;
265 basic_block bb;
266 rtx insn;
268 df = df_init ();
269 df_analyze (df, 0, DF_UD_CHAIN | DF_EQUIV_NOTES);
271 def_entry = xcalloc (df->n_defs, sizeof (struct web_entry));
272 use_entry = xcalloc (df->n_uses, sizeof (struct web_entry));
273 used = xcalloc (max, sizeof (char));
274 use_addressof = xcalloc (max, sizeof (char));
276 if (dump_file)
277 df_dump (df, DF_UD_CHAIN | DF_DU_CHAIN, dump_file);
279 /* Produce the web. */
280 for (i = 0; i < df->n_uses; i++)
281 union_defs (df, df->uses[i], def_entry, use_entry);
283 /* We can not safely rename registers whose address is taken. */
284 FOR_EACH_BB (bb)
285 FOR_BB_INSNS (bb, insn)
287 if (INSN_P (insn))
288 for_each_rtx (&PATTERN (insn), mark_addressof, use_addressof);
291 /* Update the instruction stream, allocating new registers for split pseudos
292 in progress. */
293 for (i = 0; i < df->n_uses; i++)
294 replace_ref (df->uses[i], entry_register (use_entry + i, df->uses[i],
295 used, use_addressof));
296 for (i = 0; i < df->n_defs; i++)
297 replace_ref (df->defs[i], entry_register (def_entry + i, df->defs[i],
298 used, use_addressof));
300 /* Dataflow information is corrupt here, but it can be easily updated
301 by creating new entries for new registers and updates or calling
302 df_insns_modify. */
303 free (def_entry);
304 free (use_entry);
305 free (used);
306 free (use_addressof);
307 df_finish (df);