1*e4b17023SJohn Marino /* Dead store elimination
2*e4b17023SJohn Marino Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
3*e4b17023SJohn Marino Free Software Foundation, Inc.
4*e4b17023SJohn Marino
5*e4b17023SJohn Marino This file is part of GCC.
6*e4b17023SJohn Marino
7*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify
8*e4b17023SJohn Marino it under the terms of the GNU General Public License as published by
9*e4b17023SJohn Marino the Free Software Foundation; either version 3, or (at your option)
10*e4b17023SJohn Marino any later version.
11*e4b17023SJohn Marino
12*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful,
13*e4b17023SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
14*e4b17023SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15*e4b17023SJohn Marino GNU General Public License for more details.
16*e4b17023SJohn Marino
17*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
18*e4b17023SJohn Marino along with GCC; see the file COPYING3. If not see
19*e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */
20*e4b17023SJohn Marino
21*e4b17023SJohn Marino #include "config.h"
22*e4b17023SJohn Marino #include "system.h"
23*e4b17023SJohn Marino #include "coretypes.h"
24*e4b17023SJohn Marino #include "tm.h"
25*e4b17023SJohn Marino #include "ggc.h"
26*e4b17023SJohn Marino #include "tree.h"
27*e4b17023SJohn Marino #include "tm_p.h"
28*e4b17023SJohn Marino #include "basic-block.h"
29*e4b17023SJohn Marino #include "timevar.h"
30*e4b17023SJohn Marino #include "gimple-pretty-print.h"
31*e4b17023SJohn Marino #include "tree-flow.h"
32*e4b17023SJohn Marino #include "tree-pass.h"
33*e4b17023SJohn Marino #include "tree-dump.h"
34*e4b17023SJohn Marino #include "domwalk.h"
35*e4b17023SJohn Marino #include "flags.h"
36*e4b17023SJohn Marino #include "langhooks.h"
37*e4b17023SJohn Marino
38*e4b17023SJohn Marino /* This file implements dead store elimination.
39*e4b17023SJohn Marino
40*e4b17023SJohn Marino A dead store is a store into a memory location which will later be
41*e4b17023SJohn Marino overwritten by another store without any intervening loads. In this
42*e4b17023SJohn Marino case the earlier store can be deleted.
43*e4b17023SJohn Marino
44*e4b17023SJohn Marino In our SSA + virtual operand world we use immediate uses of virtual
45*e4b17023SJohn Marino operands to detect dead stores. If a store's virtual definition
46*e4b17023SJohn Marino is used precisely once by a later store to the same location which
47*e4b17023SJohn Marino post dominates the first store, then the first store is dead.
48*e4b17023SJohn Marino
49*e4b17023SJohn Marino The single use of the store's virtual definition ensures that
50*e4b17023SJohn Marino there are no intervening aliased loads and the requirement that
51*e4b17023SJohn Marino the second load post dominate the first ensures that if the earlier
52*e4b17023SJohn Marino store executes, then the later stores will execute before the function
53*e4b17023SJohn Marino exits.
54*e4b17023SJohn Marino
55*e4b17023SJohn Marino It may help to think of this as first moving the earlier store to
56*e4b17023SJohn Marino the point immediately before the later store. Again, the single
57*e4b17023SJohn Marino use of the virtual definition and the post-dominance relationship
58*e4b17023SJohn Marino ensure that such movement would be safe. Clearly if there are
59*e4b17023SJohn Marino back to back stores, then the second is redundant.
60*e4b17023SJohn Marino
61*e4b17023SJohn Marino Reviewing section 10.7.2 in Morgan's "Building an Optimizing Compiler"
62*e4b17023SJohn Marino may also help in understanding this code since it discusses the
63*e4b17023SJohn Marino relationship between dead store and redundant load elimination. In
64*e4b17023SJohn Marino fact, they are the same transformation applied to different views of
65*e4b17023SJohn Marino the CFG. */
66*e4b17023SJohn Marino
67*e4b17023SJohn Marino
68*e4b17023SJohn Marino /* Bitmap of blocks that have had EH statements cleaned. We should
69*e4b17023SJohn Marino remove their dead edges eventually. */
70*e4b17023SJohn Marino static bitmap need_eh_cleanup;
71*e4b17023SJohn Marino
72*e4b17023SJohn Marino static bool gate_dse (void);
73*e4b17023SJohn Marino static unsigned int tree_ssa_dse (void);
74*e4b17023SJohn Marino static void dse_enter_block (struct dom_walk_data *, basic_block);
75*e4b17023SJohn Marino
76*e4b17023SJohn Marino
77*e4b17023SJohn Marino /* A helper of dse_optimize_stmt.
78*e4b17023SJohn Marino Given a GIMPLE_ASSIGN in STMT, find a candidate statement *USE_STMT that
79*e4b17023SJohn Marino may prove STMT to be dead.
80*e4b17023SJohn Marino Return TRUE if the above conditions are met, otherwise FALSE. */
81*e4b17023SJohn Marino
82*e4b17023SJohn Marino static bool
dse_possible_dead_store_p(gimple stmt,gimple * use_stmt)83*e4b17023SJohn Marino dse_possible_dead_store_p (gimple stmt, gimple *use_stmt)
84*e4b17023SJohn Marino {
85*e4b17023SJohn Marino gimple temp;
86*e4b17023SJohn Marino unsigned cnt = 0;
87*e4b17023SJohn Marino
88*e4b17023SJohn Marino *use_stmt = NULL;
89*e4b17023SJohn Marino
90*e4b17023SJohn Marino /* Find the first dominated statement that clobbers (part of) the
91*e4b17023SJohn Marino memory stmt stores to with no intermediate statement that may use
92*e4b17023SJohn Marino part of the memory stmt stores. That is, find a store that may
93*e4b17023SJohn Marino prove stmt to be a dead store. */
94*e4b17023SJohn Marino temp = stmt;
95*e4b17023SJohn Marino do
96*e4b17023SJohn Marino {
97*e4b17023SJohn Marino gimple use_stmt;
98*e4b17023SJohn Marino imm_use_iterator ui;
99*e4b17023SJohn Marino bool fail = false;
100*e4b17023SJohn Marino tree defvar;
101*e4b17023SJohn Marino
102*e4b17023SJohn Marino /* Limit stmt walking to be linear in the number of possibly
103*e4b17023SJohn Marino dead stores. */
104*e4b17023SJohn Marino if (++cnt > 256)
105*e4b17023SJohn Marino return false;
106*e4b17023SJohn Marino
107*e4b17023SJohn Marino if (gimple_code (temp) == GIMPLE_PHI)
108*e4b17023SJohn Marino defvar = PHI_RESULT (temp);
109*e4b17023SJohn Marino else
110*e4b17023SJohn Marino defvar = gimple_vdef (temp);
111*e4b17023SJohn Marino temp = NULL;
112*e4b17023SJohn Marino FOR_EACH_IMM_USE_STMT (use_stmt, ui, defvar)
113*e4b17023SJohn Marino {
114*e4b17023SJohn Marino cnt++;
115*e4b17023SJohn Marino
116*e4b17023SJohn Marino /* If we ever reach our DSE candidate stmt again fail. We
117*e4b17023SJohn Marino cannot handle dead stores in loops. */
118*e4b17023SJohn Marino if (use_stmt == stmt)
119*e4b17023SJohn Marino {
120*e4b17023SJohn Marino fail = true;
121*e4b17023SJohn Marino BREAK_FROM_IMM_USE_STMT (ui);
122*e4b17023SJohn Marino }
123*e4b17023SJohn Marino /* In simple cases we can look through PHI nodes, but we
124*e4b17023SJohn Marino have to be careful with loops and with memory references
125*e4b17023SJohn Marino containing operands that are also operands of PHI nodes.
126*e4b17023SJohn Marino See gcc.c-torture/execute/20051110-*.c. */
127*e4b17023SJohn Marino else if (gimple_code (use_stmt) == GIMPLE_PHI)
128*e4b17023SJohn Marino {
129*e4b17023SJohn Marino if (temp
130*e4b17023SJohn Marino /* Make sure we are not in a loop latch block. */
131*e4b17023SJohn Marino || gimple_bb (stmt) == gimple_bb (use_stmt)
132*e4b17023SJohn Marino || dominated_by_p (CDI_DOMINATORS,
133*e4b17023SJohn Marino gimple_bb (stmt), gimple_bb (use_stmt))
134*e4b17023SJohn Marino /* We can look through PHIs to regions post-dominating
135*e4b17023SJohn Marino the DSE candidate stmt. */
136*e4b17023SJohn Marino || !dominated_by_p (CDI_POST_DOMINATORS,
137*e4b17023SJohn Marino gimple_bb (stmt), gimple_bb (use_stmt)))
138*e4b17023SJohn Marino {
139*e4b17023SJohn Marino fail = true;
140*e4b17023SJohn Marino BREAK_FROM_IMM_USE_STMT (ui);
141*e4b17023SJohn Marino }
142*e4b17023SJohn Marino temp = use_stmt;
143*e4b17023SJohn Marino }
144*e4b17023SJohn Marino /* If the statement is a use the store is not dead. */
145*e4b17023SJohn Marino else if (ref_maybe_used_by_stmt_p (use_stmt,
146*e4b17023SJohn Marino gimple_assign_lhs (stmt)))
147*e4b17023SJohn Marino {
148*e4b17023SJohn Marino fail = true;
149*e4b17023SJohn Marino BREAK_FROM_IMM_USE_STMT (ui);
150*e4b17023SJohn Marino }
151*e4b17023SJohn Marino /* If this is a store, remember it or bail out if we have
152*e4b17023SJohn Marino multiple ones (the will be in different CFG parts then). */
153*e4b17023SJohn Marino else if (gimple_vdef (use_stmt))
154*e4b17023SJohn Marino {
155*e4b17023SJohn Marino if (temp)
156*e4b17023SJohn Marino {
157*e4b17023SJohn Marino fail = true;
158*e4b17023SJohn Marino BREAK_FROM_IMM_USE_STMT (ui);
159*e4b17023SJohn Marino }
160*e4b17023SJohn Marino temp = use_stmt;
161*e4b17023SJohn Marino }
162*e4b17023SJohn Marino }
163*e4b17023SJohn Marino
164*e4b17023SJohn Marino if (fail)
165*e4b17023SJohn Marino return false;
166*e4b17023SJohn Marino
167*e4b17023SJohn Marino /* If we didn't find any definition this means the store is dead
168*e4b17023SJohn Marino if it isn't a store to global reachable memory. In this case
169*e4b17023SJohn Marino just pretend the stmt makes itself dead. Otherwise fail. */
170*e4b17023SJohn Marino if (!temp)
171*e4b17023SJohn Marino {
172*e4b17023SJohn Marino if (is_hidden_global_store (stmt))
173*e4b17023SJohn Marino return false;
174*e4b17023SJohn Marino
175*e4b17023SJohn Marino temp = stmt;
176*e4b17023SJohn Marino break;
177*e4b17023SJohn Marino }
178*e4b17023SJohn Marino }
179*e4b17023SJohn Marino /* We deliberately stop on clobbering statements and not only on
180*e4b17023SJohn Marino killing ones to make walking cheaper. Otherwise we can just
181*e4b17023SJohn Marino continue walking until both stores have equal reference trees. */
182*e4b17023SJohn Marino while (!stmt_may_clobber_ref_p (temp, gimple_assign_lhs (stmt)));
183*e4b17023SJohn Marino
184*e4b17023SJohn Marino *use_stmt = temp;
185*e4b17023SJohn Marino
186*e4b17023SJohn Marino return true;
187*e4b17023SJohn Marino }
188*e4b17023SJohn Marino
189*e4b17023SJohn Marino
190*e4b17023SJohn Marino /* Attempt to eliminate dead stores in the statement referenced by BSI.
191*e4b17023SJohn Marino
192*e4b17023SJohn Marino A dead store is a store into a memory location which will later be
193*e4b17023SJohn Marino overwritten by another store without any intervening loads. In this
194*e4b17023SJohn Marino case the earlier store can be deleted.
195*e4b17023SJohn Marino
196*e4b17023SJohn Marino In our SSA + virtual operand world we use immediate uses of virtual
197*e4b17023SJohn Marino operands to detect dead stores. If a store's virtual definition
198*e4b17023SJohn Marino is used precisely once by a later store to the same location which
199*e4b17023SJohn Marino post dominates the first store, then the first store is dead. */
200*e4b17023SJohn Marino
201*e4b17023SJohn Marino static void
dse_optimize_stmt(gimple_stmt_iterator gsi)202*e4b17023SJohn Marino dse_optimize_stmt (gimple_stmt_iterator gsi)
203*e4b17023SJohn Marino {
204*e4b17023SJohn Marino gimple stmt = gsi_stmt (gsi);
205*e4b17023SJohn Marino
206*e4b17023SJohn Marino /* If this statement has no virtual defs, then there is nothing
207*e4b17023SJohn Marino to do. */
208*e4b17023SJohn Marino if (!gimple_vdef (stmt))
209*e4b17023SJohn Marino return;
210*e4b17023SJohn Marino
211*e4b17023SJohn Marino /* We know we have virtual definitions. If this is a GIMPLE_ASSIGN
212*e4b17023SJohn Marino that's not also a function call, then record it into our table. */
213*e4b17023SJohn Marino if (is_gimple_call (stmt) && gimple_call_fndecl (stmt))
214*e4b17023SJohn Marino return;
215*e4b17023SJohn Marino
216*e4b17023SJohn Marino if (gimple_has_volatile_ops (stmt))
217*e4b17023SJohn Marino return;
218*e4b17023SJohn Marino
219*e4b17023SJohn Marino if (is_gimple_assign (stmt))
220*e4b17023SJohn Marino {
221*e4b17023SJohn Marino gimple use_stmt;
222*e4b17023SJohn Marino
223*e4b17023SJohn Marino if (!dse_possible_dead_store_p (stmt, &use_stmt))
224*e4b17023SJohn Marino return;
225*e4b17023SJohn Marino
226*e4b17023SJohn Marino /* If we have precisely one immediate use at this point and the
227*e4b17023SJohn Marino stores are to the same memory location or there is a chain of
228*e4b17023SJohn Marino virtual uses from stmt and the stmt which stores to that same
229*e4b17023SJohn Marino memory location, then we may have found redundant store. */
230*e4b17023SJohn Marino if ((gimple_has_lhs (use_stmt)
231*e4b17023SJohn Marino && (operand_equal_p (gimple_assign_lhs (stmt),
232*e4b17023SJohn Marino gimple_get_lhs (use_stmt), 0)))
233*e4b17023SJohn Marino || stmt_kills_ref_p (use_stmt, gimple_assign_lhs (stmt)))
234*e4b17023SJohn Marino {
235*e4b17023SJohn Marino /* If use_stmt is or might be a nop assignment, e.g. for
236*e4b17023SJohn Marino struct { ... } S a, b, *p; ...
237*e4b17023SJohn Marino b = a; b = b;
238*e4b17023SJohn Marino or
239*e4b17023SJohn Marino b = a; b = *p; where p might be &b,
240*e4b17023SJohn Marino or
241*e4b17023SJohn Marino *p = a; *p = b; where p might be &b,
242*e4b17023SJohn Marino or
243*e4b17023SJohn Marino *p = *u; *p = *v; where p might be v, then USE_STMT
244*e4b17023SJohn Marino acts as a use as well as definition, so store in STMT
245*e4b17023SJohn Marino is not dead. */
246*e4b17023SJohn Marino if (stmt != use_stmt
247*e4b17023SJohn Marino && ref_maybe_used_by_stmt_p (use_stmt, gimple_assign_lhs (stmt)))
248*e4b17023SJohn Marino return;
249*e4b17023SJohn Marino
250*e4b17023SJohn Marino if (dump_file && (dump_flags & TDF_DETAILS))
251*e4b17023SJohn Marino {
252*e4b17023SJohn Marino fprintf (dump_file, " Deleted dead store '");
253*e4b17023SJohn Marino print_gimple_stmt (dump_file, gsi_stmt (gsi), dump_flags, 0);
254*e4b17023SJohn Marino fprintf (dump_file, "'\n");
255*e4b17023SJohn Marino }
256*e4b17023SJohn Marino
257*e4b17023SJohn Marino /* Then we need to fix the operand of the consuming stmt. */
258*e4b17023SJohn Marino unlink_stmt_vdef (stmt);
259*e4b17023SJohn Marino
260*e4b17023SJohn Marino bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
261*e4b17023SJohn Marino
262*e4b17023SJohn Marino /* Remove the dead store. */
263*e4b17023SJohn Marino gsi_remove (&gsi, true);
264*e4b17023SJohn Marino
265*e4b17023SJohn Marino /* And release any SSA_NAMEs set in this statement back to the
266*e4b17023SJohn Marino SSA_NAME manager. */
267*e4b17023SJohn Marino release_defs (stmt);
268*e4b17023SJohn Marino }
269*e4b17023SJohn Marino }
270*e4b17023SJohn Marino }
271*e4b17023SJohn Marino
272*e4b17023SJohn Marino static void
dse_enter_block(struct dom_walk_data * walk_data ATTRIBUTE_UNUSED,basic_block bb)273*e4b17023SJohn Marino dse_enter_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
274*e4b17023SJohn Marino basic_block bb)
275*e4b17023SJohn Marino {
276*e4b17023SJohn Marino gimple_stmt_iterator gsi;
277*e4b17023SJohn Marino
278*e4b17023SJohn Marino for (gsi = gsi_last (bb_seq (bb)); !gsi_end_p (gsi); gsi_prev (&gsi))
279*e4b17023SJohn Marino dse_optimize_stmt (gsi);
280*e4b17023SJohn Marino }
281*e4b17023SJohn Marino
282*e4b17023SJohn Marino /* Main entry point. */
283*e4b17023SJohn Marino
284*e4b17023SJohn Marino static unsigned int
tree_ssa_dse(void)285*e4b17023SJohn Marino tree_ssa_dse (void)
286*e4b17023SJohn Marino {
287*e4b17023SJohn Marino struct dom_walk_data walk_data;
288*e4b17023SJohn Marino
289*e4b17023SJohn Marino need_eh_cleanup = BITMAP_ALLOC (NULL);
290*e4b17023SJohn Marino
291*e4b17023SJohn Marino renumber_gimple_stmt_uids ();
292*e4b17023SJohn Marino
293*e4b17023SJohn Marino /* We might consider making this a property of each pass so that it
294*e4b17023SJohn Marino can be [re]computed on an as-needed basis. Particularly since
295*e4b17023SJohn Marino this pass could be seen as an extension of DCE which needs post
296*e4b17023SJohn Marino dominators. */
297*e4b17023SJohn Marino calculate_dominance_info (CDI_POST_DOMINATORS);
298*e4b17023SJohn Marino calculate_dominance_info (CDI_DOMINATORS);
299*e4b17023SJohn Marino
300*e4b17023SJohn Marino /* Dead store elimination is fundamentally a walk of the post-dominator
301*e4b17023SJohn Marino tree and a backwards walk of statements within each block. */
302*e4b17023SJohn Marino walk_data.dom_direction = CDI_POST_DOMINATORS;
303*e4b17023SJohn Marino walk_data.initialize_block_local_data = NULL;
304*e4b17023SJohn Marino walk_data.before_dom_children = dse_enter_block;
305*e4b17023SJohn Marino walk_data.after_dom_children = NULL;
306*e4b17023SJohn Marino
307*e4b17023SJohn Marino walk_data.block_local_data_size = 0;
308*e4b17023SJohn Marino walk_data.global_data = NULL;
309*e4b17023SJohn Marino
310*e4b17023SJohn Marino /* Initialize the dominator walker. */
311*e4b17023SJohn Marino init_walk_dominator_tree (&walk_data);
312*e4b17023SJohn Marino
313*e4b17023SJohn Marino /* Recursively walk the dominator tree. */
314*e4b17023SJohn Marino walk_dominator_tree (&walk_data, EXIT_BLOCK_PTR);
315*e4b17023SJohn Marino
316*e4b17023SJohn Marino /* Finalize the dominator walker. */
317*e4b17023SJohn Marino fini_walk_dominator_tree (&walk_data);
318*e4b17023SJohn Marino
319*e4b17023SJohn Marino /* Removal of stores may make some EH edges dead. Purge such edges from
320*e4b17023SJohn Marino the CFG as needed. */
321*e4b17023SJohn Marino if (!bitmap_empty_p (need_eh_cleanup))
322*e4b17023SJohn Marino {
323*e4b17023SJohn Marino gimple_purge_all_dead_eh_edges (need_eh_cleanup);
324*e4b17023SJohn Marino cleanup_tree_cfg ();
325*e4b17023SJohn Marino }
326*e4b17023SJohn Marino
327*e4b17023SJohn Marino BITMAP_FREE (need_eh_cleanup);
328*e4b17023SJohn Marino
329*e4b17023SJohn Marino /* For now, just wipe the post-dominator information. */
330*e4b17023SJohn Marino free_dominance_info (CDI_POST_DOMINATORS);
331*e4b17023SJohn Marino return 0;
332*e4b17023SJohn Marino }
333*e4b17023SJohn Marino
334*e4b17023SJohn Marino static bool
gate_dse(void)335*e4b17023SJohn Marino gate_dse (void)
336*e4b17023SJohn Marino {
337*e4b17023SJohn Marino return flag_tree_dse != 0;
338*e4b17023SJohn Marino }
339*e4b17023SJohn Marino
340*e4b17023SJohn Marino struct gimple_opt_pass pass_dse =
341*e4b17023SJohn Marino {
342*e4b17023SJohn Marino {
343*e4b17023SJohn Marino GIMPLE_PASS,
344*e4b17023SJohn Marino "dse", /* name */
345*e4b17023SJohn Marino gate_dse, /* gate */
346*e4b17023SJohn Marino tree_ssa_dse, /* execute */
347*e4b17023SJohn Marino NULL, /* sub */
348*e4b17023SJohn Marino NULL, /* next */
349*e4b17023SJohn Marino 0, /* static_pass_number */
350*e4b17023SJohn Marino TV_TREE_DSE, /* tv_id */
351*e4b17023SJohn Marino PROP_cfg | PROP_ssa, /* properties_required */
352*e4b17023SJohn Marino 0, /* properties_provided */
353*e4b17023SJohn Marino 0, /* properties_destroyed */
354*e4b17023SJohn Marino 0, /* todo_flags_start */
355*e4b17023SJohn Marino TODO_ggc_collect
356*e4b17023SJohn Marino | TODO_verify_ssa /* todo_flags_finish */
357*e4b17023SJohn Marino }
358*e4b17023SJohn Marino };
359