1 /* Web construction code for GNU compiler.
2 Contributed by Jan Hubicka.
3 Copyright (C) 2001, 2002, 2004, 2006, 2007
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 static rtx
entry_register (struct web_entry
*, struct df_ref
*, char *);
65 static void replace_ref (struct df_ref
*, rtx
);
67 /* Find the root of unionfind tree (the representative of set). */
70 unionfind_root (struct web_entry
*element
)
72 struct web_entry
*element1
= element
, *element2
;
75 element
= element
->pred
;
76 while (element1
->pred
)
78 element2
= element1
->pred
;
79 element1
->pred
= element
;
86 Return true if FIRST and SECOND points to the same web entry structure and
87 nothing is done. Otherwise, return false. */
90 unionfind_union (struct web_entry
*first
, struct web_entry
*second
)
92 first
= unionfind_root (first
);
93 second
= unionfind_root (second
);
100 /* For each use, all possible defs reaching it must come in the same
101 register, union them.
102 FUN is the function that does the union. */
105 union_defs (struct df_ref
*use
, struct web_entry
*def_entry
,
106 struct web_entry
*use_entry
,
107 bool (*fun
) (struct web_entry
*, struct web_entry
*))
109 rtx insn
= DF_REF_INSN (use
);
110 struct df_link
*link
= DF_REF_CHAIN (use
);
111 struct df_ref
**use_link
;
112 struct df_ref
**eq_use_link
;
113 struct df_ref
**def_link
;
118 use_link
= DF_INSN_USES (insn
);
119 eq_use_link
= DF_INSN_EQ_USES (insn
);
120 def_link
= DF_INSN_DEFS (insn
);
121 set
= single_set (insn
);
131 /* Some instructions may use match_dup for their operands. In case the
132 operands are dead, we will assign them different pseudos, creating
133 invalid instructions, so union all uses of the same operand for each
140 && DF_REF_REAL_REG (use
) == DF_REF_REAL_REG (*use_link
))
141 (*fun
) (use_entry
+ DF_REF_ID (use
),
142 use_entry
+ DF_REF_ID (*use_link
));
149 if (use
!= *eq_use_link
150 && DF_REF_REAL_REG (use
) == DF_REF_REAL_REG (*eq_use_link
))
151 (*fun
) (use_entry
+ DF_REF_ID (use
),
152 use_entry
+ DF_REF_ID (*eq_use_link
));
156 /* Recognize trivial noop moves and attempt to keep them as noop.
157 While most of noop moves should be removed, we still keep some
158 of them at libcall boundaries and such. */
161 && SET_SRC (set
) == DF_REF_REG (use
)
162 && SET_SRC (set
) == SET_DEST (set
))
167 if (DF_REF_REAL_REG (use
) == DF_REF_REAL_REG (*def_link
))
168 (*fun
) (use_entry
+ DF_REF_ID (use
),
169 def_entry
+ DF_REF_ID (*def_link
));
175 (*fun
) (use_entry
+ DF_REF_ID (use
),
176 def_entry
+ DF_REF_ID (link
->ref
));
180 /* A READ_WRITE use requires the corresponding def to be in the same
181 register. Find it and union. */
182 if (use
->flags
& DF_REF_READ_WRITE
)
184 struct df_ref
**link
;
186 if (DF_REF_INSN (use
))
187 link
= DF_INSN_DEFS (DF_REF_INSN (use
));
194 if (DF_REF_REAL_REG (*link
) == DF_REF_REAL_REG (use
))
195 (*fun
) (use_entry
+ DF_REF_ID (use
),
196 def_entry
+ DF_REF_ID (*link
));
202 /* Find the corresponding register for the given entry. */
205 entry_register (struct web_entry
*entry
, struct df_ref
*ref
, char *used
)
207 struct web_entry
*root
;
210 /* Find the corresponding web and see if it has been visited. */
211 root
= unionfind_root (entry
);
215 /* We are seeing this web for the first time, do the assignment. */
216 reg
= DF_REF_REAL_REG (ref
);
218 /* In case the original register is already assigned, generate new one. */
219 if (!used
[REGNO (reg
)])
220 newreg
= reg
, used
[REGNO (reg
)] = 1;
221 else if (REG_USERVAR_P (reg
) && 0/*&& !flag_messy_debugging*/)
226 "New web forced to keep reg=%i (user variable)\n",
231 newreg
= gen_reg_rtx (GET_MODE (reg
));
232 REG_USERVAR_P (newreg
) = REG_USERVAR_P (reg
);
233 REG_POINTER (newreg
) = REG_POINTER (reg
);
234 REG_ATTRS (newreg
) = REG_ATTRS (reg
);
236 fprintf (dump_file
, "Web oldreg=%i newreg=%i\n", REGNO (reg
),
244 /* Replace the reference by REG. */
247 replace_ref (struct df_ref
*ref
, rtx reg
)
249 rtx oldreg
= DF_REF_REAL_REG (ref
);
250 rtx
*loc
= DF_REF_REAL_LOC (ref
);
251 unsigned int uid
= INSN_UID (DF_REF_INSN (ref
));
256 fprintf (dump_file
, "Updating insn %i (%i->%i)\n",
257 uid
, REGNO (oldreg
), REGNO (reg
));
259 df_insn_rescan (DF_REF_INSN (ref
));
264 gate_handle_web (void)
266 return (optimize
> 0 && flag_web
);
269 /* Main entry point. */
274 struct web_entry
*def_entry
;
275 struct web_entry
*use_entry
;
276 unsigned int max
= max_reg_num ();
279 unsigned int uses_num
= 0;
282 df_set_flags (DF_NO_HARD_REGS
+ DF_EQ_NOTES
);
283 df_chain_add_problem (DF_UD_CHAIN
);
285 df_set_flags (DF_DEFER_INSN_RESCAN
);
287 /* Assign ids to the uses. */
289 FOR_BB_INSNS (bb
, insn
)
291 unsigned int uid
= INSN_UID (insn
);
294 struct df_ref
**use_rec
;
295 for (use_rec
= DF_INSN_UID_USES (uid
); *use_rec
; use_rec
++)
297 struct df_ref
*use
= *use_rec
;
298 if (DF_REF_REGNO (use
) >= FIRST_PSEUDO_REGISTER
)
299 DF_REF_ID (use
) = uses_num
++;
301 for (use_rec
= DF_INSN_UID_EQ_USES (uid
); *use_rec
; use_rec
++)
303 struct df_ref
*use
= *use_rec
;
304 if (DF_REF_REGNO (use
) >= FIRST_PSEUDO_REGISTER
)
305 DF_REF_ID (use
) = uses_num
++;
310 /* Record the number of uses and defs at the beginning of the optimization. */
311 def_entry
= XCNEWVEC (struct web_entry
, DF_DEFS_TABLE_SIZE());
312 used
= XCNEWVEC (char, max
);
313 use_entry
= XCNEWVEC (struct web_entry
, uses_num
);
315 /* Produce the web. */
317 FOR_BB_INSNS (bb
, insn
)
319 unsigned int uid
= INSN_UID (insn
);
322 struct df_ref
**use_rec
;
323 for (use_rec
= DF_INSN_UID_USES (uid
); *use_rec
; use_rec
++)
325 struct df_ref
*use
= *use_rec
;
326 if (DF_REF_REGNO (use
) >= FIRST_PSEUDO_REGISTER
)
327 union_defs (use
, def_entry
, use_entry
, unionfind_union
);
329 for (use_rec
= DF_INSN_UID_EQ_USES (uid
); *use_rec
; use_rec
++)
331 struct df_ref
*use
= *use_rec
;
332 if (DF_REF_REGNO (use
) >= FIRST_PSEUDO_REGISTER
)
333 union_defs (use
, def_entry
, use_entry
, unionfind_union
);
338 /* Update the instruction stream, allocating new registers for split pseudos
341 FOR_BB_INSNS (bb
, insn
)
343 unsigned int uid
= INSN_UID (insn
);
346 struct df_ref
**use_rec
;
347 struct df_ref
**def_rec
;
348 for (use_rec
= DF_INSN_UID_USES (uid
); *use_rec
; use_rec
++)
350 struct df_ref
*use
= *use_rec
;
351 if (DF_REF_REGNO (use
) >= FIRST_PSEUDO_REGISTER
)
352 replace_ref (use
, entry_register (use_entry
+ DF_REF_ID (use
), use
, used
));
354 for (use_rec
= DF_INSN_UID_EQ_USES (uid
); *use_rec
; use_rec
++)
356 struct df_ref
*use
= *use_rec
;
357 if (DF_REF_REGNO (use
) >= FIRST_PSEUDO_REGISTER
)
358 replace_ref (use
, entry_register (use_entry
+ DF_REF_ID (use
), use
, used
));
360 for (def_rec
= DF_INSN_UID_DEFS (uid
); *def_rec
; def_rec
++)
362 struct df_ref
*def
= *def_rec
;
363 if (DF_REF_REGNO (def
) >= FIRST_PSEUDO_REGISTER
)
364 replace_ref (def
, entry_register (def_entry
+ DF_REF_ID (def
), def
, used
));
375 struct tree_opt_pass pass_web
=
378 gate_handle_web
, /* gate */
379 web_main
, /* execute */
382 0, /* static_pass_number */
384 0, /* properties_required */
385 0, /* properties_provided */
386 0, /* properties_destroyed */
387 0, /* todo_flags_start */
389 TODO_dump_func
, /* todo_flags_finish */