1 /* Dead store elimination
2 Copyright (C) 2004-2013 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)
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/>. */
22 #include "coretypes.h"
27 #include "basic-block.h"
28 #include "gimple-pretty-print.h"
30 #include "tree-pass.h"
33 #include "langhooks.h"
35 /* This file implements dead store elimination.
37 A dead store is a store into a memory location which will later be
38 overwritten by another store without any intervening loads. In this
39 case the earlier store can be deleted.
41 In our SSA + virtual operand world we use immediate uses of virtual
42 operands to detect dead stores. If a store's virtual definition
43 is used precisely once by a later store to the same location which
44 post dominates the first store, then the first store is dead.
46 The single use of the store's virtual definition ensures that
47 there are no intervening aliased loads and the requirement that
48 the second load post dominate the first ensures that if the earlier
49 store executes, then the later stores will execute before the function
52 It may help to think of this as first moving the earlier store to
53 the point immediately before the later store. Again, the single
54 use of the virtual definition and the post-dominance relationship
55 ensure that such movement would be safe. Clearly if there are
56 back to back stores, then the second is redundant.
58 Reviewing section 10.7.2 in Morgan's "Building an Optimizing Compiler"
59 may also help in understanding this code since it discusses the
60 relationship between dead store and redundant load elimination. In
61 fact, they are the same transformation applied to different views of
65 /* Bitmap of blocks that have had EH statements cleaned. We should
66 remove their dead edges eventually. */
67 static bitmap need_eh_cleanup
;
69 static bool gate_dse (void);
70 static unsigned int tree_ssa_dse (void);
73 /* A helper of dse_optimize_stmt.
74 Given a GIMPLE_ASSIGN in STMT, find a candidate statement *USE_STMT that
75 may prove STMT to be dead.
76 Return TRUE if the above conditions are met, otherwise FALSE. */
79 dse_possible_dead_store_p (gimple stmt
, gimple
*use_stmt
)
86 /* Self-assignments are zombies. */
87 if (operand_equal_p (gimple_assign_rhs1 (stmt
), gimple_assign_lhs (stmt
), 0))
93 /* Find the first dominated statement that clobbers (part of) the
94 memory stmt stores to with no intermediate statement that may use
95 part of the memory stmt stores. That is, find a store that may
96 prove stmt to be a dead store. */
100 gimple use_stmt
, defvar_def
;
105 /* Limit stmt walking to be linear in the number of possibly
110 if (gimple_code (temp
) == GIMPLE_PHI
)
111 defvar
= PHI_RESULT (temp
);
113 defvar
= gimple_vdef (temp
);
116 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, defvar
)
120 /* If we ever reach our DSE candidate stmt again fail. We
121 cannot handle dead stores in loops. */
122 if (use_stmt
== stmt
)
125 BREAK_FROM_IMM_USE_STMT (ui
);
127 /* In simple cases we can look through PHI nodes, but we
128 have to be careful with loops and with memory references
129 containing operands that are also operands of PHI nodes.
130 See gcc.c-torture/execute/20051110-*.c. */
131 else if (gimple_code (use_stmt
) == GIMPLE_PHI
)
134 /* Make sure we are not in a loop latch block. */
135 || gimple_bb (stmt
) == gimple_bb (use_stmt
)
136 || dominated_by_p (CDI_DOMINATORS
,
137 gimple_bb (stmt
), gimple_bb (use_stmt
))
138 /* We can look through PHIs to regions post-dominating
139 the DSE candidate stmt. */
140 || !dominated_by_p (CDI_POST_DOMINATORS
,
141 gimple_bb (stmt
), gimple_bb (use_stmt
)))
144 BREAK_FROM_IMM_USE_STMT (ui
);
146 /* Do not consider the PHI as use if it dominates the
147 stmt defining the virtual operand we are processing,
148 we have processed it already in this case. */
149 if (gimple_bb (defvar_def
) != gimple_bb (use_stmt
)
150 && !dominated_by_p (CDI_DOMINATORS
,
151 gimple_bb (defvar_def
),
152 gimple_bb (use_stmt
)))
155 /* If the statement is a use the store is not dead. */
156 else if (ref_maybe_used_by_stmt_p (use_stmt
,
157 gimple_assign_lhs (stmt
)))
160 BREAK_FROM_IMM_USE_STMT (ui
);
162 /* If this is a store, remember it or bail out if we have
163 multiple ones (the will be in different CFG parts then). */
164 else if (gimple_vdef (use_stmt
))
169 BREAK_FROM_IMM_USE_STMT (ui
);
178 /* If we didn't find any definition this means the store is dead
179 if it isn't a store to global reachable memory. In this case
180 just pretend the stmt makes itself dead. Otherwise fail. */
183 if (stmt_may_clobber_global_p (stmt
))
190 /* We deliberately stop on clobbering statements and not only on
191 killing ones to make walking cheaper. Otherwise we can just
192 continue walking until both stores have equal reference trees. */
193 while (!stmt_may_clobber_ref_p (temp
, gimple_assign_lhs (stmt
)));
201 /* Attempt to eliminate dead stores in the statement referenced by BSI.
203 A dead store is a store into a memory location which will later be
204 overwritten by another store without any intervening loads. In this
205 case the earlier store can be deleted.
207 In our SSA + virtual operand world we use immediate uses of virtual
208 operands to detect dead stores. If a store's virtual definition
209 is used precisely once by a later store to the same location which
210 post dominates the first store, then the first store is dead. */
213 dse_optimize_stmt (gimple_stmt_iterator
*gsi
)
215 gimple stmt
= gsi_stmt (*gsi
);
217 /* If this statement has no virtual defs, then there is nothing
219 if (!gimple_vdef (stmt
))
222 /* We know we have virtual definitions. If this is a GIMPLE_ASSIGN
223 that's not also a function call, then record it into our table. */
224 if (is_gimple_call (stmt
) && gimple_call_fndecl (stmt
))
227 /* Don't return early on *this_2(D) ={v} {CLOBBER}. */
228 if (gimple_has_volatile_ops (stmt
)
229 && (!gimple_clobber_p (stmt
)
230 || TREE_CODE (gimple_assign_lhs (stmt
)) != MEM_REF
))
233 if (is_gimple_assign (stmt
))
237 if (!dse_possible_dead_store_p (stmt
, &use_stmt
))
240 /* But only remove *this_2(D) ={v} {CLOBBER} if killed by
241 another clobber stmt. */
242 if (gimple_clobber_p (stmt
)
243 && !gimple_clobber_p (use_stmt
))
246 /* If we have precisely one immediate use at this point and the
247 stores are to the same memory location or there is a chain of
248 virtual uses from stmt and the stmt which stores to that same
249 memory location, then we may have found redundant store. */
250 if ((gimple_has_lhs (use_stmt
)
251 && (operand_equal_p (gimple_assign_lhs (stmt
),
252 gimple_get_lhs (use_stmt
), 0)))
253 || stmt_kills_ref_p (use_stmt
, gimple_assign_lhs (stmt
)))
257 /* If use_stmt is or might be a nop assignment, e.g. for
258 struct { ... } S a, b, *p; ...
261 b = a; b = *p; where p might be &b,
263 *p = a; *p = b; where p might be &b,
265 *p = *u; *p = *v; where p might be v, then USE_STMT
266 acts as a use as well as definition, so store in STMT
269 && ref_maybe_used_by_stmt_p (use_stmt
, gimple_assign_lhs (stmt
)))
272 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
274 fprintf (dump_file
, " Deleted dead store '");
275 print_gimple_stmt (dump_file
, gsi_stmt (*gsi
), dump_flags
, 0);
276 fprintf (dump_file
, "'\n");
279 /* Then we need to fix the operand of the consuming stmt. */
280 unlink_stmt_vdef (stmt
);
282 /* Remove the dead store. */
283 bb
= gimple_bb (stmt
);
284 if (gsi_remove (gsi
, true))
285 bitmap_set_bit (need_eh_cleanup
, bb
->index
);
287 /* And release any SSA_NAMEs set in this statement back to the
294 class dse_dom_walker
: public dom_walker
297 dse_dom_walker (cdi_direction direction
) : dom_walker (direction
) {}
299 virtual void before_dom_children (basic_block
);
303 dse_dom_walker::before_dom_children (basic_block bb
)
305 gimple_stmt_iterator gsi
;
307 for (gsi
= gsi_last_bb (bb
); !gsi_end_p (gsi
);)
309 dse_optimize_stmt (&gsi
);
311 gsi
= gsi_last_bb (bb
);
317 /* Main entry point. */
322 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
324 renumber_gimple_stmt_uids ();
326 /* We might consider making this a property of each pass so that it
327 can be [re]computed on an as-needed basis. Particularly since
328 this pass could be seen as an extension of DCE which needs post
330 calculate_dominance_info (CDI_POST_DOMINATORS
);
331 calculate_dominance_info (CDI_DOMINATORS
);
333 /* Dead store elimination is fundamentally a walk of the post-dominator
334 tree and a backwards walk of statements within each block. */
335 dse_dom_walker (CDI_POST_DOMINATORS
).walk (cfun
->cfg
->x_exit_block_ptr
);
337 /* Removal of stores may make some EH edges dead. Purge such edges from
338 the CFG as needed. */
339 if (!bitmap_empty_p (need_eh_cleanup
))
341 gimple_purge_all_dead_eh_edges (need_eh_cleanup
);
345 BITMAP_FREE (need_eh_cleanup
);
347 /* For now, just wipe the post-dominator information. */
348 free_dominance_info (CDI_POST_DOMINATORS
);
355 return flag_tree_dse
!= 0;
360 const pass_data pass_data_dse
=
362 GIMPLE_PASS
, /* type */
364 OPTGROUP_NONE
, /* optinfo_flags */
366 true, /* has_execute */
367 TV_TREE_DSE
, /* tv_id */
368 ( PROP_cfg
| PROP_ssa
), /* properties_required */
369 0, /* properties_provided */
370 0, /* properties_destroyed */
371 0, /* todo_flags_start */
372 TODO_verify_ssa
, /* todo_flags_finish */
375 class pass_dse
: public gimple_opt_pass
378 pass_dse(gcc::context
*ctxt
)
379 : gimple_opt_pass(pass_data_dse
, ctxt
)
382 /* opt_pass methods: */
383 opt_pass
* clone () { return new pass_dse (ctxt_
); }
384 bool gate () { return gate_dse (); }
385 unsigned int execute () { return tree_ssa_dse (); }
392 make_pass_dse (gcc::context
*ctxt
)
394 return new pass_dse (ctxt
);