1 /* Web construction code for GNU compiler.
2 Contributed by Jan Hubicka.
3 Copyright (C) 2001-2015 Free Software Foundation, Inc.
5 This file is part of GCC.
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 3, or (at your option) any later
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
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* Simple optimization pass that splits independent uses of each pseudo,
22 increasing effectiveness of other optimizations. The optimization can
23 serve as an example of use for the dataflow module.
25 We don't split registers with REG_USERVAR set unless -fmessy-debugging
26 is specified, because debugging information about such split variables
30 - We may use profile information and ignore infrequent use for the
31 purpose of web unifying, inserting the compensation code later to
32 implement full induction variable expansion for loops (currently
33 we expand only if the induction variable is dead afterward, which
34 is often the case). */
38 #include "coretypes.h"
40 #include "diagnostic-core.h"
43 #include "hard-reg-set.h"
53 #include "dominance.h"
55 #include "basic-block.h"
57 #include "insn-config.h"
59 #include "tree-pass.h"
62 /* Find the root of unionfind tree (the representative of set). */
65 web_entry_base::unionfind_root ()
67 web_entry_base
*element
= this, *element1
= this, *element2
;
69 while (element
->pred ())
70 element
= element
->pred ();
71 while (element1
->pred ())
73 element2
= element1
->pred ();
74 element1
->set_pred (element
);
81 Return true if FIRST and SECOND points to the same web entry structure and
82 nothing is done. Otherwise, return false. */
85 unionfind_union (web_entry_base
*first
, web_entry_base
*second
)
87 first
= first
->unionfind_root ();
88 second
= second
->unionfind_root ();
91 second
->set_pred (first
);
95 class web_entry
: public web_entry_base
101 rtx
reg () { return reg_pvt
; }
102 void set_reg (rtx r
) { reg_pvt
= r
; }
105 /* For INSN, union all defs and uses that are linked by match_dup.
106 FUN is the function that does the union. */
109 union_match_dups (rtx_insn
*insn
, web_entry
*def_entry
, web_entry
*use_entry
,
110 bool (*fun
) (web_entry_base
*, web_entry_base
*))
112 struct df_insn_info
*insn_info
= DF_INSN_INFO_GET (insn
);
113 df_ref use_link
= DF_INSN_INFO_USES (insn_info
);
114 df_ref def_link
= DF_INSN_INFO_DEFS (insn_info
);
115 struct web_entry
*dup_entry
;
120 for (i
= 0; i
< recog_data
.n_dups
; i
++)
122 int op
= recog_data
.dup_num
[i
];
123 enum op_type type
= recog_data
.operand_type
[op
];
125 struct web_entry
*entry
;
127 dup_entry
= use_entry
;
128 for (dupref
= use_link
; dupref
; dupref
= DF_REF_NEXT_LOC (dupref
))
129 if (DF_REF_LOC (dupref
) == recog_data
.dup_loc
[i
])
132 if (dupref
== NULL
&& type
== OP_INOUT
)
134 dup_entry
= def_entry
;
135 for (dupref
= def_link
; dupref
; dupref
= DF_REF_NEXT_LOC (dupref
))
136 if (DF_REF_LOC (dupref
) == recog_data
.dup_loc
[i
])
139 /* ??? *DUPREF can still be zero, because when an operand matches
140 a memory, DF_REF_LOC (use_link[n]) points to the register part
141 of the address, whereas recog_data.dup_loc[m] points to the
142 entire memory ref, thus we fail to find the duplicate entry,
143 even though it is there.
144 Example: i686-pc-linux-gnu gcc.c-torture/compile/950607-1.c
145 -O3 -fomit-frame-pointer -funroll-loops */
147 || DF_REF_REGNO (dupref
) < FIRST_PSEUDO_REGISTER
)
150 ref
= type
== OP_IN
? use_link
: def_link
;
151 entry
= type
== OP_IN
? use_entry
: def_entry
;
152 for (; ref
; ref
= DF_REF_NEXT_LOC (ref
))
154 rtx
*l
= DF_REF_LOC (ref
);
155 if (l
== recog_data
.operand_loc
[op
])
157 if (l
&& DF_REF_REAL_LOC (ref
) == recog_data
.operand_loc
[op
])
161 if (!ref
&& type
== OP_INOUT
)
164 for (ref
= use_link
; ref
; ref
= DF_REF_NEXT_LOC (ref
))
166 rtx
*l
= DF_REF_LOC (ref
);
167 if (l
== recog_data
.operand_loc
[op
])
169 if (l
&& DF_REF_REAL_LOC (ref
) == recog_data
.operand_loc
[op
])
175 (*fun
) (dup_entry
+ DF_REF_ID (dupref
), entry
+ DF_REF_ID (ref
));
179 /* For each use, all possible defs reaching it must come in the same
180 register, union them.
181 FUN is the function that does the union.
183 In USED, we keep the DF_REF_ID of the first uninitialized uses of a
184 register, so that all uninitialized uses of the register can be
185 combined into a single web. We actually offset it by 2, because
186 the values 0 and 1 are reserved for use by entry_register. */
189 union_defs (df_ref use
, web_entry
*def_entry
,
190 unsigned int *used
, web_entry
*use_entry
,
191 bool (*fun
) (web_entry_base
*, web_entry_base
*))
193 struct df_insn_info
*insn_info
= DF_REF_INSN_INFO (use
);
194 struct df_link
*link
= DF_REF_CHAIN (use
);
201 set
= single_set (insn_info
->insn
);
202 FOR_EACH_INSN_INFO_EQ_USE (eq_use
, insn_info
)
204 && DF_REF_REAL_REG (use
) == DF_REF_REAL_REG (eq_use
))
205 (*fun
) (use_entry
+ DF_REF_ID (use
), use_entry
+ DF_REF_ID (eq_use
));
210 /* Union all occurrences of the same register in reg notes. */
212 /* Recognize trivial noop moves and attempt to keep them as noop. */
215 && SET_SRC (set
) == DF_REF_REG (use
)
216 && SET_SRC (set
) == SET_DEST (set
))
220 FOR_EACH_INSN_INFO_DEF (def
, insn_info
)
221 if (DF_REF_REAL_REG (use
) == DF_REF_REAL_REG (def
))
222 (*fun
) (use_entry
+ DF_REF_ID (use
), def_entry
+ DF_REF_ID (def
));
225 /* UD chains of uninitialized REGs are empty. Keeping all uses of
226 the same uninitialized REG in a single web is not necessary for
227 correctness, since the uses are undefined, but it's wasteful to
228 allocate one register or slot for each reference. Furthermore,
229 creating new pseudos for uninitialized references in debug insns
230 (see PR 42631) causes -fcompare-debug failures. We record the
231 number of the first uninitialized reference we found, and merge
232 with it any other uninitialized references to the same
236 int regno
= REGNO (DF_REF_REAL_REG (use
));
238 (*fun
) (use_entry
+ DF_REF_ID (use
), use_entry
+ used
[regno
] - 2);
240 used
[regno
] = DF_REF_ID (use
) + 2;
245 (*fun
) (use_entry
+ DF_REF_ID (use
),
246 def_entry
+ DF_REF_ID (link
->ref
));
250 /* A READ_WRITE use requires the corresponding def to be in the same
251 register. Find it and union. */
252 if (DF_REF_FLAGS (use
) & DF_REF_READ_WRITE
)
257 FOR_EACH_INSN_INFO_DEF (def
, insn_info
)
258 if (DF_REF_REAL_REG (use
) == DF_REF_REAL_REG (def
))
259 (*fun
) (use_entry
+ DF_REF_ID (use
), def_entry
+ DF_REF_ID (def
));
263 /* Find the corresponding register for the given entry. */
266 entry_register (web_entry
*entry
, df_ref ref
, unsigned int *used
)
271 /* Find the corresponding web and see if it has been visited. */
272 root
= (web_entry
*)entry
->unionfind_root ();
276 /* We are seeing this web for the first time, do the assignment. */
277 reg
= DF_REF_REAL_REG (ref
);
279 /* In case the original register is already assigned, generate new
280 one. Since we use USED to merge uninitialized refs into a single
281 web, we might found an element to be nonzero without our having
282 used it. Test for 1, because union_defs saves it for our use,
283 and there won't be any use for the other values when we get to
285 if (used
[REGNO (reg
)] != 1)
286 newreg
= reg
, used
[REGNO (reg
)] = 1;
289 newreg
= gen_reg_rtx (GET_MODE (reg
));
290 REG_USERVAR_P (newreg
) = REG_USERVAR_P (reg
);
291 REG_POINTER (newreg
) = REG_POINTER (reg
);
292 REG_ATTRS (newreg
) = REG_ATTRS (reg
);
294 fprintf (dump_file
, "Web oldreg=%i newreg=%i\n", REGNO (reg
),
298 root
->set_reg (newreg
);
302 /* Replace the reference by REG. */
305 replace_ref (df_ref ref
, rtx reg
)
307 rtx oldreg
= DF_REF_REAL_REG (ref
);
308 rtx
*loc
= DF_REF_REAL_LOC (ref
);
309 unsigned int uid
= DF_REF_INSN_UID (ref
);
314 fprintf (dump_file
, "Updating insn %i (%i->%i)\n",
315 uid
, REGNO (oldreg
), REGNO (reg
));
317 df_insn_rescan (DF_REF_INSN (ref
));
323 const pass_data pass_data_web
=
327 OPTGROUP_NONE
, /* optinfo_flags */
329 0, /* properties_required */
330 0, /* properties_provided */
331 0, /* properties_destroyed */
332 0, /* todo_flags_start */
333 TODO_df_finish
, /* todo_flags_finish */
336 class pass_web
: public rtl_opt_pass
339 pass_web (gcc::context
*ctxt
)
340 : rtl_opt_pass (pass_data_web
, ctxt
)
343 /* opt_pass methods: */
344 virtual bool gate (function
*) { return (optimize
> 0 && flag_web
); }
345 virtual unsigned int execute (function
*);
350 pass_web::execute (function
*fun
)
352 web_entry
*def_entry
;
353 web_entry
*use_entry
;
354 unsigned int max
= max_reg_num ();
357 unsigned int uses_num
= 0;
360 df_set_flags (DF_NO_HARD_REGS
+ DF_EQ_NOTES
);
361 df_set_flags (DF_RD_PRUNE_DEAD_DEFS
);
362 df_chain_add_problem (DF_UD_CHAIN
);
364 df_set_flags (DF_DEFER_INSN_RESCAN
);
366 /* Assign ids to the uses. */
367 FOR_ALL_BB_FN (bb
, fun
)
368 FOR_BB_INSNS (bb
, insn
)
370 if (NONDEBUG_INSN_P (insn
))
372 struct df_insn_info
*insn_info
= DF_INSN_INFO_GET (insn
);
374 FOR_EACH_INSN_INFO_USE (use
, insn_info
)
375 if (DF_REF_REGNO (use
) >= FIRST_PSEUDO_REGISTER
)
376 DF_REF_ID (use
) = uses_num
++;
377 FOR_EACH_INSN_INFO_EQ_USE (use
, insn_info
)
378 if (DF_REF_REGNO (use
) >= FIRST_PSEUDO_REGISTER
)
379 DF_REF_ID (use
) = uses_num
++;
383 /* Record the number of uses and defs at the beginning of the optimization. */
384 def_entry
= XCNEWVEC (web_entry
, DF_DEFS_TABLE_SIZE ());
385 used
= XCNEWVEC (unsigned, max
);
386 use_entry
= XCNEWVEC (web_entry
, uses_num
);
388 /* Produce the web. */
389 FOR_ALL_BB_FN (bb
, fun
)
390 FOR_BB_INSNS (bb
, insn
)
391 if (NONDEBUG_INSN_P (insn
))
393 struct df_insn_info
*insn_info
= DF_INSN_INFO_GET (insn
);
395 union_match_dups (insn
, def_entry
, use_entry
, unionfind_union
);
396 FOR_EACH_INSN_INFO_USE (use
, insn_info
)
397 if (DF_REF_REGNO (use
) >= FIRST_PSEUDO_REGISTER
)
398 union_defs (use
, def_entry
, used
, use_entry
, unionfind_union
);
399 FOR_EACH_INSN_INFO_EQ_USE (use
, insn_info
)
400 if (DF_REF_REGNO (use
) >= FIRST_PSEUDO_REGISTER
)
401 union_defs (use
, def_entry
, used
, use_entry
, unionfind_union
);
404 /* Update the instruction stream, allocating new registers for split pseudos
406 FOR_ALL_BB_FN (bb
, fun
)
407 FOR_BB_INSNS (bb
, insn
)
408 if (NONDEBUG_INSN_P (insn
)
409 /* Ignore naked clobber. For example, reg 134 in the second insn
410 of the following sequence will not be replaced.
412 (insn (clobber (reg:SI 134)))
414 (insn (set (reg:SI 0 r0) (reg:SI 134)))
416 Thus the later passes can optimize them away. */
417 && GET_CODE (PATTERN (insn
)) != CLOBBER
)
419 struct df_insn_info
*insn_info
= DF_INSN_INFO_GET (insn
);
421 FOR_EACH_INSN_INFO_USE (use
, insn_info
)
422 if (DF_REF_REGNO (use
) >= FIRST_PSEUDO_REGISTER
)
423 replace_ref (use
, entry_register (use_entry
+ DF_REF_ID (use
),
425 FOR_EACH_INSN_INFO_EQ_USE (use
, insn_info
)
426 if (DF_REF_REGNO (use
) >= FIRST_PSEUDO_REGISTER
)
427 replace_ref (use
, entry_register (use_entry
+ DF_REF_ID (use
),
429 FOR_EACH_INSN_INFO_DEF (def
, insn_info
)
430 if (DF_REF_REGNO (def
) >= FIRST_PSEUDO_REGISTER
)
431 replace_ref (def
, entry_register (def_entry
+ DF_REF_ID (def
),
444 make_pass_web (gcc::context
*ctxt
)
446 return new pass_web (ctxt
);