Daily bump.
[official-gcc.git] / gcc / tree-ssa-dse.c
blob91017ff812745c4467547b985cc5efd730247d6d
1 /* Dead store elimination
2 Copyright (C) 2004-2014 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 "tree.h"
25 #include "tm_p.h"
26 #include "predict.h"
27 #include "vec.h"
28 #include "hashtab.h"
29 #include "hash-set.h"
30 #include "machmode.h"
31 #include "hard-reg-set.h"
32 #include "input.h"
33 #include "function.h"
34 #include "dominance.h"
35 #include "cfg.h"
36 #include "basic-block.h"
37 #include "gimple-pretty-print.h"
38 #include "bitmap.h"
39 #include "tree-ssa-alias.h"
40 #include "internal-fn.h"
41 #include "gimple-expr.h"
42 #include "is-a.h"
43 #include "gimple.h"
44 #include "gimple-iterator.h"
45 #include "gimple-ssa.h"
46 #include "tree-cfg.h"
47 #include "tree-phinodes.h"
48 #include "ssa-iterators.h"
49 #include "stringpool.h"
50 #include "tree-ssanames.h"
51 #include "expr.h"
52 #include "tree-dfa.h"
53 #include "tree-pass.h"
54 #include "domwalk.h"
55 #include "flags.h"
56 #include "langhooks.h"
57 #include "tree-cfgcleanup.h"
59 /* This file implements dead store elimination.
61 A dead store is a store into a memory location which will later be
62 overwritten by another store without any intervening loads. In this
63 case the earlier store can be deleted.
65 In our SSA + virtual operand world we use immediate uses of virtual
66 operands to detect dead stores. If a store's virtual definition
67 is used precisely once by a later store to the same location which
68 post dominates the first store, then the first store is dead.
70 The single use of the store's virtual definition ensures that
71 there are no intervening aliased loads and the requirement that
72 the second load post dominate the first ensures that if the earlier
73 store executes, then the later stores will execute before the function
74 exits.
76 It may help to think of this as first moving the earlier store to
77 the point immediately before the later store. Again, the single
78 use of the virtual definition and the post-dominance relationship
79 ensure that such movement would be safe. Clearly if there are
80 back to back stores, then the second is redundant.
82 Reviewing section 10.7.2 in Morgan's "Building an Optimizing Compiler"
83 may also help in understanding this code since it discusses the
84 relationship between dead store and redundant load elimination. In
85 fact, they are the same transformation applied to different views of
86 the CFG. */
89 /* Bitmap of blocks that have had EH statements cleaned. We should
90 remove their dead edges eventually. */
91 static bitmap need_eh_cleanup;
94 /* A helper of dse_optimize_stmt.
95 Given a GIMPLE_ASSIGN in STMT that writes to REF, find a candidate
96 statement *USE_STMT that may prove STMT to be dead.
97 Return TRUE if the above conditions are met, otherwise FALSE. */
99 static bool
100 dse_possible_dead_store_p (ao_ref *ref, gimple stmt, gimple *use_stmt)
102 gimple temp;
103 unsigned cnt = 0;
105 *use_stmt = NULL;
107 /* Find the first dominated statement that clobbers (part of) the
108 memory stmt stores to with no intermediate statement that may use
109 part of the memory stmt stores. That is, find a store that may
110 prove stmt to be a dead store. */
111 temp = stmt;
114 gimple use_stmt, defvar_def;
115 imm_use_iterator ui;
116 bool fail = false;
117 tree defvar;
119 /* Limit stmt walking to be linear in the number of possibly
120 dead stores. */
121 if (++cnt > 256)
122 return false;
124 if (gimple_code (temp) == GIMPLE_PHI)
125 defvar = PHI_RESULT (temp);
126 else
127 defvar = gimple_vdef (temp);
128 defvar_def = temp;
129 temp = NULL;
130 FOR_EACH_IMM_USE_STMT (use_stmt, ui, defvar)
132 cnt++;
134 /* If we ever reach our DSE candidate stmt again fail. We
135 cannot handle dead stores in loops. */
136 if (use_stmt == stmt)
138 fail = true;
139 BREAK_FROM_IMM_USE_STMT (ui);
141 /* In simple cases we can look through PHI nodes, but we
142 have to be careful with loops and with memory references
143 containing operands that are also operands of PHI nodes.
144 See gcc.c-torture/execute/20051110-*.c. */
145 else if (gimple_code (use_stmt) == GIMPLE_PHI)
147 if (temp
148 /* Make sure we are not in a loop latch block. */
149 || gimple_bb (stmt) == gimple_bb (use_stmt)
150 || dominated_by_p (CDI_DOMINATORS,
151 gimple_bb (stmt), gimple_bb (use_stmt))
152 /* We can look through PHIs to regions post-dominating
153 the DSE candidate stmt. */
154 || !dominated_by_p (CDI_POST_DOMINATORS,
155 gimple_bb (stmt), gimple_bb (use_stmt)))
157 fail = true;
158 BREAK_FROM_IMM_USE_STMT (ui);
160 /* Do not consider the PHI as use if it dominates the
161 stmt defining the virtual operand we are processing,
162 we have processed it already in this case. */
163 if (gimple_bb (defvar_def) != gimple_bb (use_stmt)
164 && !dominated_by_p (CDI_DOMINATORS,
165 gimple_bb (defvar_def),
166 gimple_bb (use_stmt)))
167 temp = use_stmt;
169 /* If the statement is a use the store is not dead. */
170 else if (ref_maybe_used_by_stmt_p (use_stmt, ref))
172 fail = true;
173 BREAK_FROM_IMM_USE_STMT (ui);
175 /* If this is a store, remember it or bail out if we have
176 multiple ones (the will be in different CFG parts then). */
177 else if (gimple_vdef (use_stmt))
179 if (temp)
181 fail = true;
182 BREAK_FROM_IMM_USE_STMT (ui);
184 temp = use_stmt;
188 if (fail)
189 return false;
191 /* If we didn't find any definition this means the store is dead
192 if it isn't a store to global reachable memory. In this case
193 just pretend the stmt makes itself dead. Otherwise fail. */
194 if (!temp)
196 if (ref_may_alias_global_p (ref))
197 return false;
199 temp = stmt;
200 break;
203 /* Continue walking until we reach a kill. */
204 while (!stmt_kills_ref_p (temp, ref));
206 *use_stmt = temp;
208 return true;
212 /* Attempt to eliminate dead stores in the statement referenced by BSI.
214 A dead store is a store into a memory location which will later be
215 overwritten by another store without any intervening loads. In this
216 case the earlier store can be deleted.
218 In our SSA + virtual operand world we use immediate uses of virtual
219 operands to detect dead stores. If a store's virtual definition
220 is used precisely once by a later store to the same location which
221 post dominates the first store, then the first store is dead. */
223 static void
224 dse_optimize_stmt (gimple_stmt_iterator *gsi)
226 gimple stmt = gsi_stmt (*gsi);
228 /* If this statement has no virtual defs, then there is nothing
229 to do. */
230 if (!gimple_vdef (stmt))
231 return;
233 /* Don't return early on *this_2(D) ={v} {CLOBBER}. */
234 if (gimple_has_volatile_ops (stmt)
235 && (!gimple_clobber_p (stmt)
236 || TREE_CODE (gimple_assign_lhs (stmt)) != MEM_REF))
237 return;
239 /* We know we have virtual definitions. We can handle assignments and
240 some builtin calls. */
241 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
243 switch (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)))
245 case BUILT_IN_MEMCPY:
246 case BUILT_IN_MEMMOVE:
247 case BUILT_IN_MEMSET:
249 gimple use_stmt;
250 ao_ref ref;
251 tree size = NULL_TREE;
252 if (gimple_call_num_args (stmt) == 3)
253 size = gimple_call_arg (stmt, 2);
254 tree ptr = gimple_call_arg (stmt, 0);
255 ao_ref_init_from_ptr_and_size (&ref, ptr, size);
256 if (!dse_possible_dead_store_p (&ref, stmt, &use_stmt))
257 return;
259 if (dump_file && (dump_flags & TDF_DETAILS))
261 fprintf (dump_file, " Deleted dead call '");
262 print_gimple_stmt (dump_file, gsi_stmt (*gsi), dump_flags, 0);
263 fprintf (dump_file, "'\n");
266 tree lhs = gimple_call_lhs (stmt);
267 if (lhs)
269 gimple new_stmt = gimple_build_assign (lhs, ptr);
270 unlink_stmt_vdef (stmt);
271 if (gsi_replace (gsi, new_stmt, true))
272 bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
274 else
276 /* Then we need to fix the operand of the consuming stmt. */
277 unlink_stmt_vdef (stmt);
279 /* Remove the dead store. */
280 if (gsi_remove (gsi, true))
281 bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
283 break;
285 default:
286 return;
290 if (is_gimple_assign (stmt))
292 gimple use_stmt;
294 /* Self-assignments are zombies. */
295 if (operand_equal_p (gimple_assign_rhs1 (stmt),
296 gimple_assign_lhs (stmt), 0))
297 use_stmt = stmt;
298 else
300 ao_ref ref;
301 ao_ref_init (&ref, gimple_assign_lhs (stmt));
302 if (!dse_possible_dead_store_p (&ref, stmt, &use_stmt))
303 return;
306 /* Now we know that use_stmt kills the LHS of stmt. */
308 /* But only remove *this_2(D) ={v} {CLOBBER} if killed by
309 another clobber stmt. */
310 if (gimple_clobber_p (stmt)
311 && !gimple_clobber_p (use_stmt))
312 return;
314 if (dump_file && (dump_flags & TDF_DETAILS))
316 fprintf (dump_file, " Deleted dead store '");
317 print_gimple_stmt (dump_file, gsi_stmt (*gsi), dump_flags, 0);
318 fprintf (dump_file, "'\n");
321 /* Then we need to fix the operand of the consuming stmt. */
322 unlink_stmt_vdef (stmt);
324 /* Remove the dead store. */
325 basic_block bb = gimple_bb (stmt);
326 if (gsi_remove (gsi, true))
327 bitmap_set_bit (need_eh_cleanup, bb->index);
329 /* And release any SSA_NAMEs set in this statement back to the
330 SSA_NAME manager. */
331 release_defs (stmt);
335 class dse_dom_walker : public dom_walker
337 public:
338 dse_dom_walker (cdi_direction direction) : dom_walker (direction) {}
340 virtual void before_dom_children (basic_block);
343 void
344 dse_dom_walker::before_dom_children (basic_block bb)
346 gimple_stmt_iterator gsi;
348 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi);)
350 dse_optimize_stmt (&gsi);
351 if (gsi_end_p (gsi))
352 gsi = gsi_last_bb (bb);
353 else
354 gsi_prev (&gsi);
358 namespace {
360 const pass_data pass_data_dse =
362 GIMPLE_PASS, /* type */
363 "dse", /* name */
364 OPTGROUP_NONE, /* optinfo_flags */
365 TV_TREE_DSE, /* tv_id */
366 ( PROP_cfg | PROP_ssa ), /* properties_required */
367 0, /* properties_provided */
368 0, /* properties_destroyed */
369 0, /* todo_flags_start */
370 0, /* todo_flags_finish */
373 class pass_dse : public gimple_opt_pass
375 public:
376 pass_dse (gcc::context *ctxt)
377 : gimple_opt_pass (pass_data_dse, ctxt)
380 /* opt_pass methods: */
381 opt_pass * clone () { return new pass_dse (m_ctxt); }
382 virtual bool gate (function *) { return flag_tree_dse != 0; }
383 virtual unsigned int execute (function *);
385 }; // class pass_dse
387 unsigned int
388 pass_dse::execute (function *fun)
390 need_eh_cleanup = BITMAP_ALLOC (NULL);
392 renumber_gimple_stmt_uids ();
394 /* We might consider making this a property of each pass so that it
395 can be [re]computed on an as-needed basis. Particularly since
396 this pass could be seen as an extension of DCE which needs post
397 dominators. */
398 calculate_dominance_info (CDI_POST_DOMINATORS);
399 calculate_dominance_info (CDI_DOMINATORS);
401 /* Dead store elimination is fundamentally a walk of the post-dominator
402 tree and a backwards walk of statements within each block. */
403 dse_dom_walker (CDI_POST_DOMINATORS).walk (fun->cfg->x_exit_block_ptr);
405 /* Removal of stores may make some EH edges dead. Purge such edges from
406 the CFG as needed. */
407 if (!bitmap_empty_p (need_eh_cleanup))
409 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
410 cleanup_tree_cfg ();
413 BITMAP_FREE (need_eh_cleanup);
415 /* For now, just wipe the post-dominator information. */
416 free_dominance_info (CDI_POST_DOMINATORS);
417 return 0;
420 } // anon namespace
422 gimple_opt_pass *
423 make_pass_dse (gcc::context *ctxt)
425 return new pass_dse (ctxt);