1 /* Web construction code for GNU compiler.
2 Contributed by Jan Hubicka.
3 Copyright (C) 2001, 2002, 2004, 2006
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 /* Simple optimization pass that splits independent uses of each pseudo,
24 increasing effectiveness of other optimizations. The optimization can
25 serve as an example of use for the dataflow module.
27 We don't split registers with REG_USERVAR set unless -fmessy-debugging
28 is specified, because debugging information about such split variables
32 - Add code to keep debugging up-to-date after splitting user variable
33 pseudos. This can be done by keeping track of all the pseudos used
34 for the variable and using life analysis information before reload
35 to determine which one is live and, in case more than one are live,
36 choose the one with the latest definition.
38 Other optimization passes can benefit from the infrastructure too.
40 - We may use profile information and ignore infrequent use for the
41 purpose of web unifying, inserting the compensation code later to
42 implement full induction variable expansion for loops (currently
43 we expand only if the induction variable is dead afterward, which
44 is often the case). */
48 #include "coretypes.h"
53 #include "hard-reg-set.h"
56 #include "basic-block.h"
61 #include "tree-pass.h"
64 /* This entry is allocated for each reference in the insn stream. */
67 /* Pointer to the parent in the union/find tree. */
68 struct web_entry
*pred
;
69 /* Newly assigned register to the entry. Set only for roots. */
73 static struct web_entry
*unionfind_root (struct web_entry
*);
74 static void unionfind_union (struct web_entry
*, struct web_entry
*);
75 static void union_defs (struct df
*, struct df_ref
*, struct web_entry
*,
77 static rtx
entry_register (struct web_entry
*, struct df_ref
*, char *);
78 static void replace_ref (struct df_ref
*, rtx
);
80 /* Find the root of unionfind tree (the representative of set). */
82 static struct web_entry
*
83 unionfind_root (struct web_entry
*element
)
85 struct web_entry
*element1
= element
, *element2
;
88 element
= element
->pred
;
89 while (element1
->pred
)
91 element2
= element1
->pred
;
92 element1
->pred
= element
;
101 unionfind_union (struct web_entry
*first
, struct web_entry
*second
)
103 first
= unionfind_root (first
);
104 second
= unionfind_root (second
);
107 second
->pred
= first
;
110 /* For each use, all possible defs reaching it must come in the same
111 register, union them. */
114 union_defs (struct df
*df
, struct df_ref
*use
, struct web_entry
*def_entry
,
115 struct web_entry
*use_entry
)
117 rtx insn
= DF_REF_INSN (use
);
118 struct df_link
*link
= DF_REF_CHAIN (use
);
119 struct df_ref
*use_link
= DF_INSN_USES (df
, insn
);
120 struct df_ref
*def_link
= DF_INSN_DEFS (df
, insn
);
121 rtx set
= single_set (insn
);
123 /* Some instructions may use match_dup for their operands. In case the
124 operands are dead, we will assign them different pseudos, creating
125 invalid instructions, so union all uses of the same operand for each
131 && DF_REF_REAL_REG (use
) == DF_REF_REAL_REG (use_link
))
132 unionfind_union (use_entry
+ DF_REF_ID (use
),
133 use_entry
+ DF_REF_ID (use_link
));
134 use_link
= use_link
->next_ref
;
137 /* Recognize trivial noop moves and attempt to keep them as noop.
138 While most of noop moves should be removed, we still keep some
139 of them at libcall boundaries and such. */
142 && SET_SRC (set
) == DF_REF_REG (use
)
143 && SET_SRC (set
) == SET_DEST (set
))
147 if (DF_REF_REAL_REG (use
) == DF_REF_REAL_REG (def_link
))
148 unionfind_union (use_entry
+ DF_REF_ID (use
),
149 def_entry
+ DF_REF_ID (def_link
));
150 def_link
= def_link
->next_ref
;
155 unionfind_union (use_entry
+ DF_REF_ID (use
),
156 def_entry
+ DF_REF_ID (link
->ref
));
160 /* A READ_WRITE use requires the corresponding def to be in the same
161 register. Find it and union. */
162 if (use
->flags
& DF_REF_READ_WRITE
)
164 struct df_ref
*link
= DF_INSN_DEFS (df
, DF_REF_INSN (use
));
168 if (DF_REF_REAL_REG (link
) == DF_REF_REAL_REG (use
))
169 unionfind_union (use_entry
+ DF_REF_ID (use
),
170 def_entry
+ DF_REF_ID (link
));
171 link
= link
->next_ref
;
176 /* Find the corresponding register for the given entry. */
179 entry_register (struct web_entry
*entry
, struct df_ref
*ref
, char *used
)
181 struct web_entry
*root
;
184 /* Find the corresponding web and see if it has been visited. */
185 root
= unionfind_root (entry
);
189 /* We are seeing this web for the first time, do the assignment. */
190 reg
= DF_REF_REAL_REG (ref
);
192 /* In case the original register is already assigned, generate new one. */
193 if (!used
[REGNO (reg
)])
194 newreg
= reg
, used
[REGNO (reg
)] = 1;
195 else if (REG_USERVAR_P (reg
) && 0/*&& !flag_messy_debugging*/)
200 "New web forced to keep reg=%i (user variable)\n",
205 newreg
= gen_reg_rtx (GET_MODE (reg
));
206 REG_USERVAR_P (newreg
) = REG_USERVAR_P (reg
);
207 REG_POINTER (newreg
) = REG_POINTER (reg
);
208 REG_ATTRS (newreg
) = REG_ATTRS (reg
);
210 fprintf (dump_file
, "Web oldreg=%i newreg=%i\n", REGNO (reg
),
218 /* Replace the reference by REG. */
221 replace_ref (struct df_ref
*ref
, rtx reg
)
223 rtx oldreg
= DF_REF_REAL_REG (ref
);
224 rtx
*loc
= DF_REF_REAL_LOC (ref
);
229 fprintf (dump_file
, "Updating insn %i (%i->%i)\n",
230 INSN_UID (DF_REF_INSN (ref
)), REGNO (oldreg
), REGNO (reg
));
234 /* Main entry point. */
240 struct web_entry
*def_entry
;
241 struct web_entry
*use_entry
;
243 int max
= max_reg_num ();
246 df
= df_init (DF_EQUIV_NOTES
);
247 df_chain_add_problem (df
, DF_UD_CHAIN
);
249 df_reorganize_refs (&df
->def_info
);
250 df_reorganize_refs (&df
->use_info
);
252 def_entry
= XCNEWVEC (struct web_entry
, DF_DEFS_SIZE (df
));
253 use_entry
= XCNEWVEC (struct web_entry
, DF_USES_SIZE (df
));
254 used
= XCNEWVEC (char, max
);
257 df_dump (df
, dump_file
);
259 /* Produce the web. */
260 for (i
= 0; i
< DF_USES_SIZE (df
); i
++)
261 union_defs (df
, DF_USES_GET (df
, i
), def_entry
, use_entry
);
263 /* Update the instruction stream, allocating new registers for split pseudos
265 for (i
= 0; i
< DF_USES_SIZE (df
); i
++)
266 replace_ref (DF_USES_GET (df
, i
),
267 entry_register (use_entry
+ i
, DF_USES_GET (df
, i
), used
));
268 for (i
= 0; i
< DF_DEFS_SIZE (df
); i
++)
269 replace_ref (DF_DEFS_GET (df
, i
),
270 entry_register (def_entry
+ i
, DF_DEFS_GET (df
, i
), used
));
272 /* Dataflow information is corrupt here, but it can be easily updated
273 by creating new entries for new registers and updates or calling
283 gate_handle_web (void)
285 return (optimize
> 0 && flag_web
);
289 rest_of_handle_web (void)
292 delete_trivially_dead_insns (get_insns (), max_reg_num ());
293 cleanup_cfg (CLEANUP_EXPENSIVE
);
294 reg_scan (get_insns (), max_reg_num ());
298 struct tree_opt_pass pass_web
=
301 gate_handle_web
, /* gate */
302 rest_of_handle_web
, /* execute */
305 0, /* static_pass_number */
307 0, /* properties_required */
308 0, /* properties_provided */
309 0, /* properties_destroyed */
310 0, /* todo_flags_start */
311 TODO_dump_func
, /* todo_flags_finish */