OSDN Git Service

* cse.c (CHEAP_REGNO): Redefine using REGNO_PTR_FRAME_P and
[pf3gnuchains/gcc-fork.git] / gcc / web.c
1 /* Web construction code for GNU compiler.
2    Contributed by Jan Hubicka.
3    Copyright (C) 2001, 2002, 2004 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
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.
11
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.
16
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.  */
21
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.
25
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.
29
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.
36
37       Other optimization passes can benefit from the infrastructure too.
38
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).  */
44
45 #include "config.h"
46 #include "system.h"
47 #include "coretypes.h"
48 #include "tm.h"
49 #include "toplev.h"
50
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"
58
59
60 /* This entry is allocated for each reference in the insn stream.  */
61 struct web_entry
62 {
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;
67 };
68
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 *);
74 static void replace_ref (struct ref *, rtx);
75
76 /* Find the root of unionfind tree (the representative of set).  */
77
78 static struct web_entry *
79 unionfind_root (struct web_entry *element)
80 {
81   struct web_entry *element1 = element, *element2;
82
83   while (element->pred)
84     element = element->pred;
85   while (element1->pred)
86     {
87       element2 = element1->pred;
88       element1->pred = element;
89       element1 = element2;
90     }
91   return element;
92 }
93
94 /* Union sets.  */
95
96 static void
97 unionfind_union (struct web_entry *first, struct web_entry *second)
98 {
99   first = unionfind_root (first);
100   second = unionfind_root (second);
101   if (first == second)
102     return;
103   second->pred = first;
104 }
105
106 /* For each use, all possible defs reaching it must come in the same
107    register, union them.  */
108
109 static void
110 union_defs (struct df *df, struct ref *use, struct web_entry *def_entry,
111             struct web_entry *use_entry)
112 {
113   rtx insn = DF_REF_INSN (use);
114   struct df_link *link = DF_REF_CHAIN (use);
115   struct df_link *use_link = DF_INSN_USES (df, insn);
116   struct df_link *def_link = DF_INSN_DEFS (df, insn);
117   rtx set = single_set (insn);
118
119   /* Some instructions may use match_dup for their operands.  In case the
120      operands are dead, we will assign them different pseudos, creating
121      invalid instructions, so union all uses of the same operand for each
122      insn.  */
123
124   while (use_link)
125     {
126       if (use != use_link->ref
127           && DF_REF_REAL_REG (use) == DF_REF_REAL_REG (use_link->ref))
128         unionfind_union (use_entry + DF_REF_ID (use),
129                          use_entry + DF_REF_ID (use_link->ref));
130       use_link = use_link->next;
131     }
132
133   /* Recognize trivial noop moves and attempt to keep them as noop.
134      While most of noop moves should be removed, we still keep some
135      of them at libcall boundaries and such.  */
136
137   if (set
138       && SET_SRC (set) == DF_REF_REG (use)
139       && SET_SRC (set) == SET_DEST (set))
140     {
141       while (def_link)
142         {
143           if (DF_REF_REAL_REG (use) == DF_REF_REAL_REG (def_link->ref))
144             unionfind_union (use_entry + DF_REF_ID (use),
145                              def_entry + DF_REF_ID (def_link->ref));
146           def_link = def_link->next;
147         }
148     }
149   while (link)
150     {
151       unionfind_union (use_entry + DF_REF_ID (use),
152                        def_entry + DF_REF_ID (link->ref));
153       link = link->next;
154     }
155
156   /* A READ_WRITE use requires the corresponding def to be in the same
157      register.  Find it and union.  */
158   if (use->flags & DF_REF_READ_WRITE)
159     {
160       struct df_link *link = DF_INSN_DEFS (df, DF_REF_INSN (use));
161
162       while (link)
163         {
164           if (DF_REF_REAL_REG (link->ref) == DF_REF_REAL_REG (use))
165             unionfind_union (use_entry + DF_REF_ID (use),
166                              def_entry + DF_REF_ID (link->ref));
167           link = link->next;
168         }
169     }
170 }
171
172 /* Find the corresponding register for the given entry.  */
173
174 static rtx
175 entry_register (struct web_entry *entry, struct ref *ref, char *used)
176 {
177   struct web_entry *root;
178   rtx reg, newreg;
179
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;
184
185   /* We are seeing this web for the first time, do the assignment.  */
186   reg = DF_REF_REAL_REG (ref);
187
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*/)
192     {
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));
198     }
199   else
200     {
201       newreg = gen_reg_rtx (GET_MODE (reg));
202       REG_USERVAR_P (newreg) = REG_USERVAR_P (reg);
203       REG_POINTER (newreg) = REG_POINTER (reg);
204       REG_ATTRS (newreg) = REG_ATTRS (reg);
205       if (dump_file)
206         fprintf (dump_file, "Web oldreg=%i newreg=%i\n", REGNO (reg),
207                  REGNO (newreg));
208     }
209
210   root->reg = newreg;
211   return newreg;
212 }
213
214 /* Replace the reference by REG.  */
215
216 static void
217 replace_ref (struct ref *ref, rtx reg)
218 {
219   rtx oldreg = DF_REF_REAL_REG (ref);
220   rtx *loc = DF_REF_REAL_LOC (ref);
221
222   if (oldreg == reg)
223     return;
224   if (dump_file)
225     fprintf (dump_file, "Updating insn %i (%i->%i)\n",
226              INSN_UID (DF_REF_INSN (ref)), REGNO (oldreg), REGNO (reg)); 
227   *loc = reg;
228 }
229
230 /* Main entry point.  */
231
232 void
233 web_main (void)
234 {
235   struct df *df;
236   struct web_entry *def_entry;
237   struct web_entry *use_entry;
238   unsigned int i;
239   int max = max_reg_num ();
240   char *used;
241
242   df = df_init ();
243   df_analyze (df, 0, DF_UD_CHAIN | DF_EQUIV_NOTES);
244
245   def_entry = xcalloc (df->n_defs, sizeof (struct web_entry));
246   use_entry = xcalloc (df->n_uses, sizeof (struct web_entry));
247   used = xcalloc (max, sizeof (char));
248
249   if (dump_file)
250     df_dump (df, DF_UD_CHAIN | DF_DU_CHAIN, dump_file);
251
252   /* Produce the web.  */
253   for (i = 0; i < df->n_uses; i++)
254     union_defs (df, df->uses[i], def_entry, use_entry);
255
256   /* Update the instruction stream, allocating new registers for split pseudos
257      in progress.  */
258   for (i = 0; i < df->n_uses; i++)
259     replace_ref (df->uses[i], entry_register (use_entry + i, df->uses[i],
260                                               used));
261   for (i = 0; i < df->n_defs; i++)
262     replace_ref (df->defs[i], entry_register (def_entry + i, df->defs[i],
263                                               used));
264
265   /* Dataflow information is corrupt here, but it can be easily updated
266      by creating new entries for new registers and updates or calling
267      df_insns_modify.  */
268   free (def_entry);
269   free (use_entry);
270   free (used);
271   df_finish (df);
272 }