2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / tree-ssa-dse.c
bloba7b70f5c09c840d1e856a7d4327c6540a2728a6f
1 /* Dead store elimination
2 Copyright (C) 2004-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "input.h"
25 #include "alias.h"
26 #include "symtab.h"
27 #include "tree.h"
28 #include "fold-const.h"
29 #include "tm_p.h"
30 #include "predict.h"
31 #include "hard-reg-set.h"
32 #include "function.h"
33 #include "dominance.h"
34 #include "cfg.h"
35 #include "basic-block.h"
36 #include "gimple-pretty-print.h"
37 #include "bitmap.h"
38 #include "tree-ssa-alias.h"
39 #include "internal-fn.h"
40 #include "gimple-expr.h"
41 #include "is-a.h"
42 #include "gimple.h"
43 #include "gimple-iterator.h"
44 #include "gimple-ssa.h"
45 #include "tree-cfg.h"
46 #include "tree-phinodes.h"
47 #include "ssa-iterators.h"
48 #include "stringpool.h"
49 #include "tree-ssanames.h"
50 #include "rtl.h"
51 #include "flags.h"
52 #include "insn-config.h"
53 #include "expmed.h"
54 #include "dojump.h"
55 #include "explow.h"
56 #include "calls.h"
57 #include "emit-rtl.h"
58 #include "varasm.h"
59 #include "stmt.h"
60 #include "expr.h"
61 #include "tree-dfa.h"
62 #include "tree-pass.h"
63 #include "domwalk.h"
64 #include "langhooks.h"
65 #include "tree-cfgcleanup.h"
67 /* This file implements dead store elimination.
69 A dead store is a store into a memory location which will later be
70 overwritten by another store without any intervening loads. In this
71 case the earlier store can be deleted.
73 In our SSA + virtual operand world we use immediate uses of virtual
74 operands to detect dead stores. If a store's virtual definition
75 is used precisely once by a later store to the same location which
76 post dominates the first store, then the first store is dead.
78 The single use of the store's virtual definition ensures that
79 there are no intervening aliased loads and the requirement that
80 the second load post dominate the first ensures that if the earlier
81 store executes, then the later stores will execute before the function
82 exits.
84 It may help to think of this as first moving the earlier store to
85 the point immediately before the later store. Again, the single
86 use of the virtual definition and the post-dominance relationship
87 ensure that such movement would be safe. Clearly if there are
88 back to back stores, then the second is redundant.
90 Reviewing section 10.7.2 in Morgan's "Building an Optimizing Compiler"
91 may also help in understanding this code since it discusses the
92 relationship between dead store and redundant load elimination. In
93 fact, they are the same transformation applied to different views of
94 the CFG. */
97 /* Bitmap of blocks that have had EH statements cleaned. We should
98 remove their dead edges eventually. */
99 static bitmap need_eh_cleanup;
102 /* A helper of dse_optimize_stmt.
103 Given a GIMPLE_ASSIGN in STMT that writes to REF, find a candidate
104 statement *USE_STMT that may prove STMT to be dead.
105 Return TRUE if the above conditions are met, otherwise FALSE. */
107 static bool
108 dse_possible_dead_store_p (ao_ref *ref, gimple stmt, gimple *use_stmt)
110 gimple temp;
111 unsigned cnt = 0;
113 *use_stmt = NULL;
115 /* Find the first dominated statement that clobbers (part of) the
116 memory stmt stores to with no intermediate statement that may use
117 part of the memory stmt stores. That is, find a store that may
118 prove stmt to be a dead store. */
119 temp = stmt;
122 gimple use_stmt, defvar_def;
123 imm_use_iterator ui;
124 bool fail = false;
125 tree defvar;
127 /* Limit stmt walking to be linear in the number of possibly
128 dead stores. */
129 if (++cnt > 256)
130 return false;
132 if (gimple_code (temp) == GIMPLE_PHI)
133 defvar = PHI_RESULT (temp);
134 else
135 defvar = gimple_vdef (temp);
136 defvar_def = temp;
137 temp = NULL;
138 FOR_EACH_IMM_USE_STMT (use_stmt, ui, defvar)
140 cnt++;
142 /* If we ever reach our DSE candidate stmt again fail. We
143 cannot handle dead stores in loops. */
144 if (use_stmt == stmt)
146 fail = true;
147 BREAK_FROM_IMM_USE_STMT (ui);
149 /* In simple cases we can look through PHI nodes, but we
150 have to be careful with loops and with memory references
151 containing operands that are also operands of PHI nodes.
152 See gcc.c-torture/execute/20051110-*.c. */
153 else if (gimple_code (use_stmt) == GIMPLE_PHI)
155 if (temp
156 /* Make sure we are not in a loop latch block. */
157 || gimple_bb (stmt) == gimple_bb (use_stmt)
158 || dominated_by_p (CDI_DOMINATORS,
159 gimple_bb (stmt), gimple_bb (use_stmt))
160 /* We can look through PHIs to regions post-dominating
161 the DSE candidate stmt. */
162 || !dominated_by_p (CDI_POST_DOMINATORS,
163 gimple_bb (stmt), gimple_bb (use_stmt)))
165 fail = true;
166 BREAK_FROM_IMM_USE_STMT (ui);
168 /* Do not consider the PHI as use if it dominates the
169 stmt defining the virtual operand we are processing,
170 we have processed it already in this case. */
171 if (gimple_bb (defvar_def) != gimple_bb (use_stmt)
172 && !dominated_by_p (CDI_DOMINATORS,
173 gimple_bb (defvar_def),
174 gimple_bb (use_stmt)))
175 temp = use_stmt;
177 /* If the statement is a use the store is not dead. */
178 else if (ref_maybe_used_by_stmt_p (use_stmt, ref))
180 fail = true;
181 BREAK_FROM_IMM_USE_STMT (ui);
183 /* If this is a store, remember it or bail out if we have
184 multiple ones (the will be in different CFG parts then). */
185 else if (gimple_vdef (use_stmt))
187 if (temp)
189 fail = true;
190 BREAK_FROM_IMM_USE_STMT (ui);
192 temp = use_stmt;
196 if (fail)
197 return false;
199 /* If we didn't find any definition this means the store is dead
200 if it isn't a store to global reachable memory. In this case
201 just pretend the stmt makes itself dead. Otherwise fail. */
202 if (!temp)
204 if (ref_may_alias_global_p (ref))
205 return false;
207 temp = stmt;
208 break;
211 /* Continue walking until we reach a kill. */
212 while (!stmt_kills_ref_p (temp, ref));
214 *use_stmt = temp;
216 return true;
220 /* Attempt to eliminate dead stores in the statement referenced by BSI.
222 A dead store is a store into a memory location which will later be
223 overwritten by another store without any intervening loads. In this
224 case the earlier store can be deleted.
226 In our SSA + virtual operand world we use immediate uses of virtual
227 operands to detect dead stores. If a store's virtual definition
228 is used precisely once by a later store to the same location which
229 post dominates the first store, then the first store is dead. */
231 static void
232 dse_optimize_stmt (gimple_stmt_iterator *gsi)
234 gimple stmt = gsi_stmt (*gsi);
236 /* If this statement has no virtual defs, then there is nothing
237 to do. */
238 if (!gimple_vdef (stmt))
239 return;
241 /* Don't return early on *this_2(D) ={v} {CLOBBER}. */
242 if (gimple_has_volatile_ops (stmt)
243 && (!gimple_clobber_p (stmt)
244 || TREE_CODE (gimple_assign_lhs (stmt)) != MEM_REF))
245 return;
247 /* We know we have virtual definitions. We can handle assignments and
248 some builtin calls. */
249 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
251 switch (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)))
253 case BUILT_IN_MEMCPY:
254 case BUILT_IN_MEMMOVE:
255 case BUILT_IN_MEMSET:
257 gimple use_stmt;
258 ao_ref ref;
259 tree size = NULL_TREE;
260 if (gimple_call_num_args (stmt) == 3)
261 size = gimple_call_arg (stmt, 2);
262 tree ptr = gimple_call_arg (stmt, 0);
263 ao_ref_init_from_ptr_and_size (&ref, ptr, size);
264 if (!dse_possible_dead_store_p (&ref, stmt, &use_stmt))
265 return;
267 if (dump_file && (dump_flags & TDF_DETAILS))
269 fprintf (dump_file, " Deleted dead call '");
270 print_gimple_stmt (dump_file, gsi_stmt (*gsi), dump_flags, 0);
271 fprintf (dump_file, "'\n");
274 tree lhs = gimple_call_lhs (stmt);
275 if (lhs)
277 gimple new_stmt = gimple_build_assign (lhs, ptr);
278 unlink_stmt_vdef (stmt);
279 if (gsi_replace (gsi, new_stmt, true))
280 bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
282 else
284 /* Then we need to fix the operand of the consuming stmt. */
285 unlink_stmt_vdef (stmt);
287 /* Remove the dead store. */
288 if (gsi_remove (gsi, true))
289 bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
291 break;
293 default:
294 return;
298 if (is_gimple_assign (stmt))
300 gimple use_stmt;
302 /* Self-assignments are zombies. */
303 if (operand_equal_p (gimple_assign_rhs1 (stmt),
304 gimple_assign_lhs (stmt), 0))
305 use_stmt = stmt;
306 else
308 ao_ref ref;
309 ao_ref_init (&ref, gimple_assign_lhs (stmt));
310 if (!dse_possible_dead_store_p (&ref, stmt, &use_stmt))
311 return;
314 /* Now we know that use_stmt kills the LHS of stmt. */
316 /* But only remove *this_2(D) ={v} {CLOBBER} if killed by
317 another clobber stmt. */
318 if (gimple_clobber_p (stmt)
319 && !gimple_clobber_p (use_stmt))
320 return;
322 if (dump_file && (dump_flags & TDF_DETAILS))
324 fprintf (dump_file, " Deleted dead store '");
325 print_gimple_stmt (dump_file, gsi_stmt (*gsi), dump_flags, 0);
326 fprintf (dump_file, "'\n");
329 /* Then we need to fix the operand of the consuming stmt. */
330 unlink_stmt_vdef (stmt);
332 /* Remove the dead store. */
333 basic_block bb = gimple_bb (stmt);
334 if (gsi_remove (gsi, true))
335 bitmap_set_bit (need_eh_cleanup, bb->index);
337 /* And release any SSA_NAMEs set in this statement back to the
338 SSA_NAME manager. */
339 release_defs (stmt);
343 class dse_dom_walker : public dom_walker
345 public:
346 dse_dom_walker (cdi_direction direction) : dom_walker (direction) {}
348 virtual void before_dom_children (basic_block);
351 void
352 dse_dom_walker::before_dom_children (basic_block bb)
354 gimple_stmt_iterator gsi;
356 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi);)
358 dse_optimize_stmt (&gsi);
359 if (gsi_end_p (gsi))
360 gsi = gsi_last_bb (bb);
361 else
362 gsi_prev (&gsi);
366 namespace {
368 const pass_data pass_data_dse =
370 GIMPLE_PASS, /* type */
371 "dse", /* name */
372 OPTGROUP_NONE, /* optinfo_flags */
373 TV_TREE_DSE, /* tv_id */
374 ( PROP_cfg | PROP_ssa ), /* properties_required */
375 0, /* properties_provided */
376 0, /* properties_destroyed */
377 0, /* todo_flags_start */
378 0, /* todo_flags_finish */
381 class pass_dse : public gimple_opt_pass
383 public:
384 pass_dse (gcc::context *ctxt)
385 : gimple_opt_pass (pass_data_dse, ctxt)
388 /* opt_pass methods: */
389 opt_pass * clone () { return new pass_dse (m_ctxt); }
390 virtual bool gate (function *) { return flag_tree_dse != 0; }
391 virtual unsigned int execute (function *);
393 }; // class pass_dse
395 unsigned int
396 pass_dse::execute (function *fun)
398 need_eh_cleanup = BITMAP_ALLOC (NULL);
400 renumber_gimple_stmt_uids ();
402 /* We might consider making this a property of each pass so that it
403 can be [re]computed on an as-needed basis. Particularly since
404 this pass could be seen as an extension of DCE which needs post
405 dominators. */
406 calculate_dominance_info (CDI_POST_DOMINATORS);
407 calculate_dominance_info (CDI_DOMINATORS);
409 /* Dead store elimination is fundamentally a walk of the post-dominator
410 tree and a backwards walk of statements within each block. */
411 dse_dom_walker (CDI_POST_DOMINATORS).walk (fun->cfg->x_exit_block_ptr);
413 /* Removal of stores may make some EH edges dead. Purge such edges from
414 the CFG as needed. */
415 if (!bitmap_empty_p (need_eh_cleanup))
417 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
418 cleanup_tree_cfg ();
421 BITMAP_FREE (need_eh_cleanup);
423 /* For now, just wipe the post-dominator information. */
424 free_dominance_info (CDI_POST_DOMINATORS);
425 return 0;
428 } // anon namespace
430 gimple_opt_pass *
431 make_pass_dse (gcc::context *ctxt)
433 return new pass_dse (ctxt);