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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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
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). */
47 #include "coretypes.h"
52 #include "hard-reg-set.h"
55 #include "basic-block.h"
60 #include "tree-pass.h"
63 static rtx
entry_register (struct web_entry
*, df_ref
, char *);
64 static void replace_ref (df_ref
, rtx
);
66 /* Find the root of unionfind tree (the representative of set). */
69 unionfind_root (struct web_entry
*element
)
71 struct web_entry
*element1
= element
, *element2
;
74 element
= element
->pred
;
75 while (element1
->pred
)
77 element2
= element1
->pred
;
78 element1
->pred
= element
;
85 Return true if FIRST and SECOND points to the same web entry structure and
86 nothing is done. Otherwise, return false. */
89 unionfind_union (struct web_entry
*first
, struct web_entry
*second
)
91 first
= unionfind_root (first
);
92 second
= unionfind_root (second
);
99 /* For each use, all possible defs reaching it must come in the same
100 register, union them.
101 FUN is the function that does the union. */
104 union_defs (df_ref use
, struct web_entry
*def_entry
,
105 struct web_entry
*use_entry
,
106 bool (*fun
) (struct web_entry
*, struct web_entry
*))
108 struct df_insn_info
*insn_info
= DF_REF_INSN_INFO (use
);
109 struct df_link
*link
= DF_REF_CHAIN (use
);
117 rtx insn
= insn_info
->insn
;
118 use_link
= DF_INSN_INFO_USES (insn_info
);
119 eq_use_link
= DF_INSN_INFO_EQ_USES (insn_info
);
120 def_link
= DF_INSN_INFO_DEFS (insn_info
);
121 set
= single_set (insn
);
125 /* An artificial use. It links up with nothing. */
132 /* Some instructions may use match_dup for their operands. In case the
133 operands are dead, we will assign them different pseudos, creating
134 invalid instructions, so union all uses of the same operand for each
141 && DF_REF_REAL_REG (use
) == DF_REF_REAL_REG (*use_link
))
142 (*fun
) (use_entry
+ DF_REF_ID (use
),
143 use_entry
+ DF_REF_ID (*use_link
));
150 if (use
!= *eq_use_link
151 && DF_REF_REAL_REG (use
) == DF_REF_REAL_REG (*eq_use_link
))
152 (*fun
) (use_entry
+ DF_REF_ID (use
),
153 use_entry
+ DF_REF_ID (*eq_use_link
));
157 /* Recognize trivial noop moves and attempt to keep them as noop. */
160 && SET_SRC (set
) == DF_REF_REG (use
)
161 && SET_SRC (set
) == SET_DEST (set
))
166 if (DF_REF_REAL_REG (use
) == DF_REF_REAL_REG (*def_link
))
167 (*fun
) (use_entry
+ DF_REF_ID (use
),
168 def_entry
+ DF_REF_ID (*def_link
));
174 (*fun
) (use_entry
+ DF_REF_ID (use
),
175 def_entry
+ DF_REF_ID (link
->ref
));
179 /* A READ_WRITE use requires the corresponding def to be in the same
180 register. Find it and union. */
181 if (DF_REF_FLAGS (use
) & DF_REF_READ_WRITE
)
186 link
= DF_INSN_INFO_DEFS (insn_info
);
193 if (DF_REF_REAL_REG (*link
) == DF_REF_REAL_REG (use
))
194 (*fun
) (use_entry
+ DF_REF_ID (use
),
195 def_entry
+ DF_REF_ID (*link
));
201 /* Find the corresponding register for the given entry. */
204 entry_register (struct web_entry
*entry
, df_ref ref
, char *used
)
206 struct web_entry
*root
;
209 /* Find the corresponding web and see if it has been visited. */
210 root
= unionfind_root (entry
);
214 /* We are seeing this web for the first time, do the assignment. */
215 reg
= DF_REF_REAL_REG (ref
);
217 /* In case the original register is already assigned, generate new one. */
218 if (!used
[REGNO (reg
)])
219 newreg
= reg
, used
[REGNO (reg
)] = 1;
220 else if (REG_USERVAR_P (reg
) && 0/*&& !flag_messy_debugging*/)
225 "New web forced to keep reg=%i (user variable)\n",
230 newreg
= gen_reg_rtx (GET_MODE (reg
));
231 REG_USERVAR_P (newreg
) = REG_USERVAR_P (reg
);
232 REG_POINTER (newreg
) = REG_POINTER (reg
);
233 REG_ATTRS (newreg
) = REG_ATTRS (reg
);
235 fprintf (dump_file
, "Web oldreg=%i newreg=%i\n", REGNO (reg
),
243 /* Replace the reference by REG. */
246 replace_ref (df_ref ref
, rtx reg
)
248 rtx oldreg
= DF_REF_REAL_REG (ref
);
249 rtx
*loc
= DF_REF_REAL_LOC (ref
);
250 unsigned int uid
= DF_REF_INSN_UID (ref
);
255 fprintf (dump_file
, "Updating insn %i (%i->%i)\n",
256 uid
, REGNO (oldreg
), REGNO (reg
));
258 df_insn_rescan (DF_REF_INSN (ref
));
263 gate_handle_web (void)
265 return (optimize
> 0 && flag_web
);
268 /* Main entry point. */
273 struct web_entry
*def_entry
;
274 struct web_entry
*use_entry
;
275 unsigned int max
= max_reg_num ();
278 unsigned int uses_num
= 0;
281 df_set_flags (DF_NO_HARD_REGS
+ DF_EQ_NOTES
);
282 df_chain_add_problem (DF_UD_CHAIN
);
284 df_set_flags (DF_DEFER_INSN_RESCAN
);
286 /* Assign ids to the uses. */
288 FOR_BB_INSNS (bb
, insn
)
290 unsigned int uid
= INSN_UID (insn
);
294 for (use_rec
= DF_INSN_UID_USES (uid
); *use_rec
; use_rec
++)
296 df_ref use
= *use_rec
;
297 if (DF_REF_REGNO (use
) >= FIRST_PSEUDO_REGISTER
)
298 DF_REF_ID (use
) = uses_num
++;
300 for (use_rec
= DF_INSN_UID_EQ_USES (uid
); *use_rec
; use_rec
++)
302 df_ref use
= *use_rec
;
303 if (DF_REF_REGNO (use
) >= FIRST_PSEUDO_REGISTER
)
304 DF_REF_ID (use
) = uses_num
++;
309 /* Record the number of uses and defs at the beginning of the optimization. */
310 def_entry
= XCNEWVEC (struct web_entry
, DF_DEFS_TABLE_SIZE());
311 used
= XCNEWVEC (char, max
);
312 use_entry
= XCNEWVEC (struct web_entry
, uses_num
);
314 /* Produce the web. */
316 FOR_BB_INSNS (bb
, insn
)
318 unsigned int uid
= INSN_UID (insn
);
322 for (use_rec
= DF_INSN_UID_USES (uid
); *use_rec
; use_rec
++)
324 df_ref use
= *use_rec
;
325 if (DF_REF_REGNO (use
) >= FIRST_PSEUDO_REGISTER
)
326 union_defs (use
, def_entry
, use_entry
, unionfind_union
);
328 for (use_rec
= DF_INSN_UID_EQ_USES (uid
); *use_rec
; use_rec
++)
330 df_ref use
= *use_rec
;
331 if (DF_REF_REGNO (use
) >= FIRST_PSEUDO_REGISTER
)
332 union_defs (use
, def_entry
, use_entry
, unionfind_union
);
337 /* Update the instruction stream, allocating new registers for split pseudos
340 FOR_BB_INSNS (bb
, insn
)
342 unsigned int uid
= INSN_UID (insn
);
347 for (use_rec
= DF_INSN_UID_USES (uid
); *use_rec
; use_rec
++)
349 df_ref use
= *use_rec
;
350 if (DF_REF_REGNO (use
) >= FIRST_PSEUDO_REGISTER
)
351 replace_ref (use
, entry_register (use_entry
+ DF_REF_ID (use
), use
, used
));
353 for (use_rec
= DF_INSN_UID_EQ_USES (uid
); *use_rec
; use_rec
++)
355 df_ref use
= *use_rec
;
356 if (DF_REF_REGNO (use
) >= FIRST_PSEUDO_REGISTER
)
357 replace_ref (use
, entry_register (use_entry
+ DF_REF_ID (use
), use
, used
));
359 for (def_rec
= DF_INSN_UID_DEFS (uid
); *def_rec
; def_rec
++)
361 df_ref def
= *def_rec
;
362 if (DF_REF_REGNO (def
) >= FIRST_PSEUDO_REGISTER
)
363 replace_ref (def
, entry_register (def_entry
+ DF_REF_ID (def
), def
, used
));
374 struct rtl_opt_pass pass_web
=
379 gate_handle_web
, /* gate */
380 web_main
, /* execute */
383 0, /* static_pass_number */
385 0, /* properties_required */
386 0, /* properties_provided */
387 0, /* properties_destroyed */
388 0, /* todo_flags_start */
389 TODO_df_finish
| TODO_verify_rtl_sharing
|
390 TODO_dump_func
/* todo_flags_finish */