ipa-utils.h (polymorphic_call_context): Add metdhos dump, debug and clear_outer_type.
[official-gcc.git] / gcc / tree-ssa-dse.c
blob6e6204ab90174747eee210752fc92030beb15217
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 "basic-block.h"
27 #include "gimple-pretty-print.h"
28 #include "bitmap.h"
29 #include "tree-ssa-alias.h"
30 #include "internal-fn.h"
31 #include "gimple-expr.h"
32 #include "is-a.h"
33 #include "gimple.h"
34 #include "gimple-iterator.h"
35 #include "gimple-ssa.h"
36 #include "tree-cfg.h"
37 #include "tree-phinodes.h"
38 #include "ssa-iterators.h"
39 #include "stringpool.h"
40 #include "tree-ssanames.h"
41 #include "expr.h"
42 #include "tree-dfa.h"
43 #include "tree-pass.h"
44 #include "domwalk.h"
45 #include "flags.h"
46 #include "langhooks.h"
47 #include "tree-cfgcleanup.h"
49 /* This file implements dead store elimination.
51 A dead store is a store into a memory location which will later be
52 overwritten by another store without any intervening loads. In this
53 case the earlier store can be deleted.
55 In our SSA + virtual operand world we use immediate uses of virtual
56 operands to detect dead stores. If a store's virtual definition
57 is used precisely once by a later store to the same location which
58 post dominates the first store, then the first store is dead.
60 The single use of the store's virtual definition ensures that
61 there are no intervening aliased loads and the requirement that
62 the second load post dominate the first ensures that if the earlier
63 store executes, then the later stores will execute before the function
64 exits.
66 It may help to think of this as first moving the earlier store to
67 the point immediately before the later store. Again, the single
68 use of the virtual definition and the post-dominance relationship
69 ensure that such movement would be safe. Clearly if there are
70 back to back stores, then the second is redundant.
72 Reviewing section 10.7.2 in Morgan's "Building an Optimizing Compiler"
73 may also help in understanding this code since it discusses the
74 relationship between dead store and redundant load elimination. In
75 fact, they are the same transformation applied to different views of
76 the CFG. */
79 /* Bitmap of blocks that have had EH statements cleaned. We should
80 remove their dead edges eventually. */
81 static bitmap need_eh_cleanup;
84 /* A helper of dse_optimize_stmt.
85 Given a GIMPLE_ASSIGN in STMT that writes to REF, find a candidate
86 statement *USE_STMT that may prove STMT to be dead.
87 Return TRUE if the above conditions are met, otherwise FALSE. */
89 static bool
90 dse_possible_dead_store_p (ao_ref *ref, gimple stmt, gimple *use_stmt)
92 gimple temp;
93 unsigned cnt = 0;
95 *use_stmt = NULL;
97 /* Find the first dominated statement that clobbers (part of) the
98 memory stmt stores to with no intermediate statement that may use
99 part of the memory stmt stores. That is, find a store that may
100 prove stmt to be a dead store. */
101 temp = stmt;
104 gimple use_stmt, defvar_def;
105 imm_use_iterator ui;
106 bool fail = false;
107 tree defvar;
109 /* Limit stmt walking to be linear in the number of possibly
110 dead stores. */
111 if (++cnt > 256)
112 return false;
114 if (gimple_code (temp) == GIMPLE_PHI)
115 defvar = PHI_RESULT (temp);
116 else
117 defvar = gimple_vdef (temp);
118 defvar_def = temp;
119 temp = NULL;
120 FOR_EACH_IMM_USE_STMT (use_stmt, ui, defvar)
122 cnt++;
124 /* If we ever reach our DSE candidate stmt again fail. We
125 cannot handle dead stores in loops. */
126 if (use_stmt == stmt)
128 fail = true;
129 BREAK_FROM_IMM_USE_STMT (ui);
131 /* In simple cases we can look through PHI nodes, but we
132 have to be careful with loops and with memory references
133 containing operands that are also operands of PHI nodes.
134 See gcc.c-torture/execute/20051110-*.c. */
135 else if (gimple_code (use_stmt) == GIMPLE_PHI)
137 if (temp
138 /* Make sure we are not in a loop latch block. */
139 || gimple_bb (stmt) == gimple_bb (use_stmt)
140 || dominated_by_p (CDI_DOMINATORS,
141 gimple_bb (stmt), gimple_bb (use_stmt))
142 /* We can look through PHIs to regions post-dominating
143 the DSE candidate stmt. */
144 || !dominated_by_p (CDI_POST_DOMINATORS,
145 gimple_bb (stmt), gimple_bb (use_stmt)))
147 fail = true;
148 BREAK_FROM_IMM_USE_STMT (ui);
150 /* Do not consider the PHI as use if it dominates the
151 stmt defining the virtual operand we are processing,
152 we have processed it already in this case. */
153 if (gimple_bb (defvar_def) != gimple_bb (use_stmt)
154 && !dominated_by_p (CDI_DOMINATORS,
155 gimple_bb (defvar_def),
156 gimple_bb (use_stmt)))
157 temp = use_stmt;
159 /* If the statement is a use the store is not dead. */
160 else if (ref_maybe_used_by_stmt_p (use_stmt, ref))
162 fail = true;
163 BREAK_FROM_IMM_USE_STMT (ui);
165 /* If this is a store, remember it or bail out if we have
166 multiple ones (the will be in different CFG parts then). */
167 else if (gimple_vdef (use_stmt))
169 if (temp)
171 fail = true;
172 BREAK_FROM_IMM_USE_STMT (ui);
174 temp = use_stmt;
178 if (fail)
179 return false;
181 /* If we didn't find any definition this means the store is dead
182 if it isn't a store to global reachable memory. In this case
183 just pretend the stmt makes itself dead. Otherwise fail. */
184 if (!temp)
186 if (ref_may_alias_global_p (ref))
187 return false;
189 temp = stmt;
190 break;
193 /* Continue walking until we reach a kill. */
194 while (!stmt_kills_ref_p (temp, ref));
196 *use_stmt = temp;
198 return true;
202 /* Attempt to eliminate dead stores in the statement referenced by BSI.
204 A dead store is a store into a memory location which will later be
205 overwritten by another store without any intervening loads. In this
206 case the earlier store can be deleted.
208 In our SSA + virtual operand world we use immediate uses of virtual
209 operands to detect dead stores. If a store's virtual definition
210 is used precisely once by a later store to the same location which
211 post dominates the first store, then the first store is dead. */
213 static void
214 dse_optimize_stmt (gimple_stmt_iterator *gsi)
216 gimple stmt = gsi_stmt (*gsi);
218 /* If this statement has no virtual defs, then there is nothing
219 to do. */
220 if (!gimple_vdef (stmt))
221 return;
223 /* Don't return early on *this_2(D) ={v} {CLOBBER}. */
224 if (gimple_has_volatile_ops (stmt)
225 && (!gimple_clobber_p (stmt)
226 || TREE_CODE (gimple_assign_lhs (stmt)) != MEM_REF))
227 return;
229 /* We know we have virtual definitions. We can handle assignments and
230 some builtin calls. */
231 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
233 switch (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)))
235 case BUILT_IN_MEMCPY:
236 case BUILT_IN_MEMMOVE:
237 case BUILT_IN_MEMSET:
239 gimple use_stmt;
240 ao_ref ref;
241 tree size = NULL_TREE;
242 if (gimple_call_num_args (stmt) == 3)
243 size = gimple_call_arg (stmt, 2);
244 tree ptr = gimple_call_arg (stmt, 0);
245 ao_ref_init_from_ptr_and_size (&ref, ptr, size);
246 if (!dse_possible_dead_store_p (&ref, stmt, &use_stmt))
247 return;
249 if (dump_file && (dump_flags & TDF_DETAILS))
251 fprintf (dump_file, " Deleted dead call '");
252 print_gimple_stmt (dump_file, gsi_stmt (*gsi), dump_flags, 0);
253 fprintf (dump_file, "'\n");
256 tree lhs = gimple_call_lhs (stmt);
257 if (lhs)
259 gimple new_stmt = gimple_build_assign (lhs, ptr);
260 unlink_stmt_vdef (stmt);
261 if (gsi_replace (gsi, new_stmt, true))
262 bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
264 else
266 /* Then we need to fix the operand of the consuming stmt. */
267 unlink_stmt_vdef (stmt);
269 /* Remove the dead store. */
270 if (gsi_remove (gsi, true))
271 bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
273 break;
275 default:
276 return;
280 if (is_gimple_assign (stmt))
282 gimple use_stmt;
284 /* Self-assignments are zombies. */
285 if (operand_equal_p (gimple_assign_rhs1 (stmt),
286 gimple_assign_lhs (stmt), 0))
287 use_stmt = stmt;
288 else
290 ao_ref ref;
291 ao_ref_init (&ref, gimple_assign_lhs (stmt));
292 if (!dse_possible_dead_store_p (&ref, stmt, &use_stmt))
293 return;
296 /* Now we know that use_stmt kills the LHS of stmt. */
298 /* But only remove *this_2(D) ={v} {CLOBBER} if killed by
299 another clobber stmt. */
300 if (gimple_clobber_p (stmt)
301 && !gimple_clobber_p (use_stmt))
302 return;
304 if (dump_file && (dump_flags & TDF_DETAILS))
306 fprintf (dump_file, " Deleted dead store '");
307 print_gimple_stmt (dump_file, gsi_stmt (*gsi), dump_flags, 0);
308 fprintf (dump_file, "'\n");
311 /* Then we need to fix the operand of the consuming stmt. */
312 unlink_stmt_vdef (stmt);
314 /* Remove the dead store. */
315 basic_block bb = gimple_bb (stmt);
316 if (gsi_remove (gsi, true))
317 bitmap_set_bit (need_eh_cleanup, bb->index);
319 /* And release any SSA_NAMEs set in this statement back to the
320 SSA_NAME manager. */
321 release_defs (stmt);
325 class dse_dom_walker : public dom_walker
327 public:
328 dse_dom_walker (cdi_direction direction) : dom_walker (direction) {}
330 virtual void before_dom_children (basic_block);
333 void
334 dse_dom_walker::before_dom_children (basic_block bb)
336 gimple_stmt_iterator gsi;
338 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi);)
340 dse_optimize_stmt (&gsi);
341 if (gsi_end_p (gsi))
342 gsi = gsi_last_bb (bb);
343 else
344 gsi_prev (&gsi);
348 namespace {
350 const pass_data pass_data_dse =
352 GIMPLE_PASS, /* type */
353 "dse", /* name */
354 OPTGROUP_NONE, /* optinfo_flags */
355 TV_TREE_DSE, /* tv_id */
356 ( PROP_cfg | PROP_ssa ), /* properties_required */
357 0, /* properties_provided */
358 0, /* properties_destroyed */
359 0, /* todo_flags_start */
360 0, /* todo_flags_finish */
363 class pass_dse : public gimple_opt_pass
365 public:
366 pass_dse (gcc::context *ctxt)
367 : gimple_opt_pass (pass_data_dse, ctxt)
370 /* opt_pass methods: */
371 opt_pass * clone () { return new pass_dse (m_ctxt); }
372 virtual bool gate (function *) { return flag_tree_dse != 0; }
373 virtual unsigned int execute (function *);
375 }; // class pass_dse
377 unsigned int
378 pass_dse::execute (function *fun)
380 need_eh_cleanup = BITMAP_ALLOC (NULL);
382 renumber_gimple_stmt_uids ();
384 /* We might consider making this a property of each pass so that it
385 can be [re]computed on an as-needed basis. Particularly since
386 this pass could be seen as an extension of DCE which needs post
387 dominators. */
388 calculate_dominance_info (CDI_POST_DOMINATORS);
389 calculate_dominance_info (CDI_DOMINATORS);
391 /* Dead store elimination is fundamentally a walk of the post-dominator
392 tree and a backwards walk of statements within each block. */
393 dse_dom_walker (CDI_POST_DOMINATORS).walk (fun->cfg->x_exit_block_ptr);
395 /* Removal of stores may make some EH edges dead. Purge such edges from
396 the CFG as needed. */
397 if (!bitmap_empty_p (need_eh_cleanup))
399 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
400 cleanup_tree_cfg ();
403 BITMAP_FREE (need_eh_cleanup);
405 /* For now, just wipe the post-dominator information. */
406 free_dominance_info (CDI_POST_DOMINATORS);
407 return 0;
410 } // anon namespace
412 gimple_opt_pass *
413 make_pass_dse (gcc::context *ctxt)
415 return new pass_dse (ctxt);