1 /* Web construction code for GNU compiler.
2 Contributed by Jan Hubicka.
3 Copyright (C) 2001, 2002, 2004, 2006, 2007, 2008, 2010
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 - We may use profile information and ignore infrequent use for the
32 purpose of web unifying, inserting the compensation code later to
33 implement full induction variable expansion for loops (currently
34 we expand only if the induction variable is dead afterward, which
35 is often the case). */
39 #include "coretypes.h"
41 #include "diagnostic-core.h"
44 #include "hard-reg-set.h"
47 #include "basic-block.h"
51 #include "insn-config.h"
54 #include "tree-pass.h"
57 /* Find the root of unionfind tree (the representative of set). */
60 unionfind_root (struct web_entry
*element
)
62 struct web_entry
*element1
= element
, *element2
;
65 element
= element
->pred
;
66 while (element1
->pred
)
68 element2
= element1
->pred
;
69 element1
->pred
= element
;
76 Return true if FIRST and SECOND points to the same web entry structure and
77 nothing is done. Otherwise, return false. */
80 unionfind_union (struct web_entry
*first
, struct web_entry
*second
)
82 first
= unionfind_root (first
);
83 second
= unionfind_root (second
);
90 /* For INSN, union all defs and uses that are linked by match_dup.
91 FUN is the function that does the union. */
94 union_match_dups (rtx insn
, struct web_entry
*def_entry
,
95 struct web_entry
*use_entry
,
96 bool (*fun
) (struct web_entry
*, struct web_entry
*))
98 struct df_insn_info
*insn_info
= DF_INSN_INFO_GET (insn
);
99 df_ref
*use_link
= DF_INSN_INFO_USES (insn_info
);
100 df_ref
*def_link
= DF_INSN_INFO_DEFS (insn_info
);
105 for (i
= 0; i
< recog_data
.n_dups
; i
++)
107 int op
= recog_data
.dup_num
[i
];
108 enum op_type type
= recog_data
.operand_type
[op
];
109 df_ref
*ref
, *dupref
;
110 struct web_entry
*entry
;
112 for (dupref
= use_link
; *dupref
; dupref
++)
113 if (DF_REF_LOC (*dupref
) == recog_data
.dup_loc
[i
])
117 || DF_REF_REGNO (*dupref
) < FIRST_PSEUDO_REGISTER
)
120 ref
= type
== OP_IN
? use_link
: def_link
;
121 entry
= type
== OP_IN
? use_entry
: def_entry
;
123 if (DF_REF_LOC (*ref
) == recog_data
.operand_loc
[op
])
126 (*fun
) (use_entry
+ DF_REF_ID (*dupref
), entry
+ DF_REF_ID (*ref
));
130 /* For each use, all possible defs reaching it must come in the same
131 register, union them.
132 FUN is the function that does the union.
134 In USED, we keep the DF_REF_ID of the first uninitialized uses of a
135 register, so that all uninitialized uses of the register can be
136 combined into a single web. We actually offset it by 2, because
137 the values 0 and 1 are reserved for use by entry_register. */
140 union_defs (df_ref use
, struct web_entry
*def_entry
,
141 unsigned int *used
, struct web_entry
*use_entry
,
142 bool (*fun
) (struct web_entry
*, struct web_entry
*))
144 struct df_insn_info
*insn_info
= DF_REF_INSN_INFO (use
);
145 struct df_link
*link
= DF_REF_CHAIN (use
);
152 rtx insn
= insn_info
->insn
;
153 eq_use_link
= DF_INSN_INFO_EQ_USES (insn_info
);
154 def_link
= DF_INSN_INFO_DEFS (insn_info
);
155 set
= single_set (insn
);
159 /* An artificial use. It links up with nothing. */
165 /* Union all occurrences of the same register in reg notes. */
170 if (use
!= *eq_use_link
171 && DF_REF_REAL_REG (use
) == DF_REF_REAL_REG (*eq_use_link
))
172 (*fun
) (use_entry
+ DF_REF_ID (use
),
173 use_entry
+ DF_REF_ID (*eq_use_link
));
177 /* Recognize trivial noop moves and attempt to keep them as noop. */
180 && SET_SRC (set
) == DF_REF_REG (use
)
181 && SET_SRC (set
) == SET_DEST (set
))
186 if (DF_REF_REAL_REG (use
) == DF_REF_REAL_REG (*def_link
))
187 (*fun
) (use_entry
+ DF_REF_ID (use
),
188 def_entry
+ DF_REF_ID (*def_link
));
193 /* UD chains of uninitialized REGs are empty. Keeping all uses of
194 the same uninitialized REG in a single web is not necessary for
195 correctness, since the uses are undefined, but it's wasteful to
196 allocate one register or slot for each reference. Furthermore,
197 creating new pseudos for uninitialized references in debug insns
198 (see PR 42631) causes -fcompare-debug failures. We record the
199 number of the first uninitialized reference we found, and merge
200 with it any other uninitialized references to the same
204 int regno
= REGNO (DF_REF_REAL_REG (use
));
206 (*fun
) (use_entry
+ DF_REF_ID (use
), use_entry
+ used
[regno
] - 2);
208 used
[regno
] = DF_REF_ID (use
) + 2;
213 (*fun
) (use_entry
+ DF_REF_ID (use
),
214 def_entry
+ DF_REF_ID (link
->ref
));
218 /* A READ_WRITE use requires the corresponding def to be in the same
219 register. Find it and union. */
220 if (DF_REF_FLAGS (use
) & DF_REF_READ_WRITE
)
225 link
= DF_INSN_INFO_DEFS (insn_info
);
232 if (DF_REF_REAL_REG (*link
) == DF_REF_REAL_REG (use
))
233 (*fun
) (use_entry
+ DF_REF_ID (use
),
234 def_entry
+ DF_REF_ID (*link
));
240 /* Find the corresponding register for the given entry. */
243 entry_register (struct web_entry
*entry
, df_ref ref
, unsigned int *used
)
245 struct web_entry
*root
;
248 /* Find the corresponding web and see if it has been visited. */
249 root
= unionfind_root (entry
);
253 /* We are seeing this web for the first time, do the assignment. */
254 reg
= DF_REF_REAL_REG (ref
);
256 /* In case the original register is already assigned, generate new
257 one. Since we use USED to merge uninitialized refs into a single
258 web, we might found an element to be nonzero without our having
259 used it. Test for 1, because union_defs saves it for our use,
260 and there won't be any use for the other values when we get to
262 if (used
[REGNO (reg
)] != 1)
263 newreg
= reg
, used
[REGNO (reg
)] = 1;
266 newreg
= gen_reg_rtx (GET_MODE (reg
));
267 REG_USERVAR_P (newreg
) = REG_USERVAR_P (reg
);
268 REG_POINTER (newreg
) = REG_POINTER (reg
);
269 REG_ATTRS (newreg
) = REG_ATTRS (reg
);
271 fprintf (dump_file
, "Web oldreg=%i newreg=%i\n", REGNO (reg
),
279 /* Replace the reference by REG. */
282 replace_ref (df_ref ref
, rtx reg
)
284 rtx oldreg
= DF_REF_REAL_REG (ref
);
285 rtx
*loc
= DF_REF_REAL_LOC (ref
);
286 unsigned int uid
= DF_REF_INSN_UID (ref
);
291 fprintf (dump_file
, "Updating insn %i (%i->%i)\n",
292 uid
, REGNO (oldreg
), REGNO (reg
));
294 df_insn_rescan (DF_REF_INSN (ref
));
299 gate_handle_web (void)
301 return (optimize
> 0 && flag_web
);
304 /* Main entry point. */
309 struct web_entry
*def_entry
;
310 struct web_entry
*use_entry
;
311 unsigned int max
= max_reg_num ();
314 unsigned int uses_num
= 0;
317 df_set_flags (DF_NO_HARD_REGS
+ DF_EQ_NOTES
);
318 df_chain_add_problem (DF_UD_CHAIN
);
320 df_set_flags (DF_DEFER_INSN_RESCAN
);
322 /* Assign ids to the uses. */
324 FOR_BB_INSNS (bb
, insn
)
326 unsigned int uid
= INSN_UID (insn
);
327 if (NONDEBUG_INSN_P (insn
))
330 for (use_rec
= DF_INSN_UID_USES (uid
); *use_rec
; use_rec
++)
332 df_ref use
= *use_rec
;
333 if (DF_REF_REGNO (use
) >= FIRST_PSEUDO_REGISTER
)
334 DF_REF_ID (use
) = uses_num
++;
336 for (use_rec
= DF_INSN_UID_EQ_USES (uid
); *use_rec
; use_rec
++)
338 df_ref use
= *use_rec
;
339 if (DF_REF_REGNO (use
) >= FIRST_PSEUDO_REGISTER
)
340 DF_REF_ID (use
) = uses_num
++;
345 /* Record the number of uses and defs at the beginning of the optimization. */
346 def_entry
= XCNEWVEC (struct web_entry
, DF_DEFS_TABLE_SIZE());
347 used
= XCNEWVEC (unsigned, max
);
348 use_entry
= XCNEWVEC (struct web_entry
, uses_num
);
350 /* Produce the web. */
352 FOR_BB_INSNS (bb
, insn
)
354 unsigned int uid
= INSN_UID (insn
);
355 if (NONDEBUG_INSN_P (insn
))
358 union_match_dups (insn
, def_entry
, use_entry
, unionfind_union
);
359 for (use_rec
= DF_INSN_UID_USES (uid
); *use_rec
; use_rec
++)
361 df_ref use
= *use_rec
;
362 if (DF_REF_REGNO (use
) >= FIRST_PSEUDO_REGISTER
)
363 union_defs (use
, def_entry
, used
, use_entry
, unionfind_union
);
365 for (use_rec
= DF_INSN_UID_EQ_USES (uid
); *use_rec
; use_rec
++)
367 df_ref use
= *use_rec
;
368 if (DF_REF_REGNO (use
) >= FIRST_PSEUDO_REGISTER
)
369 union_defs (use
, def_entry
, used
, use_entry
, unionfind_union
);
374 /* Update the instruction stream, allocating new registers for split pseudos
377 FOR_BB_INSNS (bb
, insn
)
379 unsigned int uid
= INSN_UID (insn
);
381 if (NONDEBUG_INSN_P (insn
)
382 /* Ignore naked clobber. For example, reg 134 in the second insn
383 of the following sequence will not be replaced.
385 (insn (clobber (reg:SI 134)))
387 (insn (set (reg:SI 0 r0) (reg:SI 134)))
389 Thus the later passes can optimize them away. */
390 && GET_CODE (PATTERN (insn
)) != CLOBBER
)
394 for (use_rec
= DF_INSN_UID_USES (uid
); *use_rec
; use_rec
++)
396 df_ref use
= *use_rec
;
397 if (DF_REF_REGNO (use
) >= FIRST_PSEUDO_REGISTER
)
398 replace_ref (use
, entry_register (use_entry
+ DF_REF_ID (use
), use
, used
));
400 for (use_rec
= DF_INSN_UID_EQ_USES (uid
); *use_rec
; use_rec
++)
402 df_ref use
= *use_rec
;
403 if (DF_REF_REGNO (use
) >= FIRST_PSEUDO_REGISTER
)
404 replace_ref (use
, entry_register (use_entry
+ DF_REF_ID (use
), use
, used
));
406 for (def_rec
= DF_INSN_UID_DEFS (uid
); *def_rec
; def_rec
++)
408 df_ref def
= *def_rec
;
409 if (DF_REF_REGNO (def
) >= FIRST_PSEUDO_REGISTER
)
410 replace_ref (def
, entry_register (def_entry
+ DF_REF_ID (def
), def
, used
));
421 struct rtl_opt_pass pass_web
=
426 gate_handle_web
, /* gate */
427 web_main
, /* execute */
430 0, /* static_pass_number */
432 0, /* properties_required */
433 0, /* properties_provided */
434 0, /* properties_destroyed */
435 0, /* todo_flags_start */
436 TODO_df_finish
| TODO_verify_rtl_sharing
/* todo_flags_finish */