1e4b17023SJohn Marino /* Dead code elimination pass for the GNU compiler.
2e4b17023SJohn Marino    Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
3e4b17023SJohn Marino    Free Software Foundation, Inc.
4e4b17023SJohn Marino    Contributed by Ben Elliston <bje@redhat.com>
5e4b17023SJohn Marino    and Andrew MacLeod <amacleod@redhat.com>
6e4b17023SJohn Marino    Adapted to use control dependence by Steven Bosscher, SUSE Labs.
7e4b17023SJohn Marino 
8e4b17023SJohn Marino This file is part of GCC.
9e4b17023SJohn Marino 
10e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it
11e4b17023SJohn Marino under the terms of the GNU General Public License as published by the
12e4b17023SJohn Marino Free Software Foundation; either version 3, or (at your option) any
13e4b17023SJohn Marino later version.
14e4b17023SJohn Marino 
15e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT
16e4b17023SJohn Marino ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18e4b17023SJohn Marino for more details.
19e4b17023SJohn Marino 
20e4b17023SJohn Marino You should have received a copy of the GNU General Public License
21e4b17023SJohn Marino along with GCC; see the file COPYING3.  If not see
22e4b17023SJohn Marino <http://www.gnu.org/licenses/>.  */
23e4b17023SJohn Marino 
24e4b17023SJohn Marino /* Dead code elimination.
25e4b17023SJohn Marino 
26e4b17023SJohn Marino    References:
27e4b17023SJohn Marino 
28e4b17023SJohn Marino      Building an Optimizing Compiler,
29e4b17023SJohn Marino      Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
30e4b17023SJohn Marino 
31e4b17023SJohn Marino      Advanced Compiler Design and Implementation,
32e4b17023SJohn Marino      Steven Muchnick, Morgan Kaufmann, 1997, Section 18.10.
33e4b17023SJohn Marino 
34e4b17023SJohn Marino    Dead-code elimination is the removal of statements which have no
35e4b17023SJohn Marino    impact on the program's output.  "Dead statements" have no impact
36e4b17023SJohn Marino    on the program's output, while "necessary statements" may have
37e4b17023SJohn Marino    impact on the output.
38e4b17023SJohn Marino 
39e4b17023SJohn Marino    The algorithm consists of three phases:
40e4b17023SJohn Marino    1. Marking as necessary all statements known to be necessary,
41e4b17023SJohn Marino       e.g. most function calls, writing a value to memory, etc;
42e4b17023SJohn Marino    2. Propagating necessary statements, e.g., the statements
43e4b17023SJohn Marino       giving values to operands in necessary statements; and
44e4b17023SJohn Marino    3. Removing dead statements.  */
45e4b17023SJohn Marino 
46e4b17023SJohn Marino #include "config.h"
47e4b17023SJohn Marino #include "system.h"
48e4b17023SJohn Marino #include "coretypes.h"
49e4b17023SJohn Marino #include "tm.h"
50e4b17023SJohn Marino 
51e4b17023SJohn Marino #include "tree.h"
52e4b17023SJohn Marino #include "tree-pretty-print.h"
53e4b17023SJohn Marino #include "gimple-pretty-print.h"
54e4b17023SJohn Marino #include "basic-block.h"
55e4b17023SJohn Marino #include "tree-flow.h"
56e4b17023SJohn Marino #include "gimple.h"
57e4b17023SJohn Marino #include "tree-dump.h"
58e4b17023SJohn Marino #include "tree-pass.h"
59e4b17023SJohn Marino #include "timevar.h"
60e4b17023SJohn Marino #include "flags.h"
61e4b17023SJohn Marino #include "cfgloop.h"
62e4b17023SJohn Marino #include "tree-scalar-evolution.h"
63e4b17023SJohn Marino 
64e4b17023SJohn Marino static struct stmt_stats
65e4b17023SJohn Marino {
66e4b17023SJohn Marino   int total;
67e4b17023SJohn Marino   int total_phis;
68e4b17023SJohn Marino   int removed;
69e4b17023SJohn Marino   int removed_phis;
70e4b17023SJohn Marino } stats;
71e4b17023SJohn Marino 
72e4b17023SJohn Marino #define STMT_NECESSARY GF_PLF_1
73e4b17023SJohn Marino 
74e4b17023SJohn Marino static VEC(gimple,heap) *worklist;
75e4b17023SJohn Marino 
76e4b17023SJohn Marino /* Vector indicating an SSA name has already been processed and marked
77e4b17023SJohn Marino    as necessary.  */
78e4b17023SJohn Marino static sbitmap processed;
79e4b17023SJohn Marino 
80e4b17023SJohn Marino /* Vector indicating that the last statement of a basic block has already
81e4b17023SJohn Marino    been marked as necessary.  */
82e4b17023SJohn Marino static sbitmap last_stmt_necessary;
83e4b17023SJohn Marino 
84e4b17023SJohn Marino /* Vector indicating that BB contains statements that are live.  */
85e4b17023SJohn Marino static sbitmap bb_contains_live_stmts;
86e4b17023SJohn Marino 
87e4b17023SJohn Marino /* Before we can determine whether a control branch is dead, we need to
88e4b17023SJohn Marino    compute which blocks are control dependent on which edges.
89e4b17023SJohn Marino 
90e4b17023SJohn Marino    We expect each block to be control dependent on very few edges so we
91e4b17023SJohn Marino    use a bitmap for each block recording its edges.  An array holds the
92e4b17023SJohn Marino    bitmap.  The Ith bit in the bitmap is set if that block is dependent
93e4b17023SJohn Marino    on the Ith edge.  */
94e4b17023SJohn Marino static bitmap *control_dependence_map;
95e4b17023SJohn Marino 
96e4b17023SJohn Marino /* Vector indicating that a basic block has already had all the edges
97e4b17023SJohn Marino    processed that it is control dependent on.  */
98e4b17023SJohn Marino static sbitmap visited_control_parents;
99e4b17023SJohn Marino 
100e4b17023SJohn Marino /* TRUE if this pass alters the CFG (by removing control statements).
101e4b17023SJohn Marino    FALSE otherwise.
102e4b17023SJohn Marino 
103e4b17023SJohn Marino    If this pass alters the CFG, then it will arrange for the dominators
104e4b17023SJohn Marino    to be recomputed.  */
105e4b17023SJohn Marino static bool cfg_altered;
106e4b17023SJohn Marino 
107e4b17023SJohn Marino /* Execute code that follows the macro for each edge (given number
108e4b17023SJohn Marino    EDGE_NUMBER within the CODE) for which the block with index N is
109e4b17023SJohn Marino    control dependent.  */
110e4b17023SJohn Marino #define EXECUTE_IF_CONTROL_DEPENDENT(BI, N, EDGE_NUMBER)	\
111e4b17023SJohn Marino   EXECUTE_IF_SET_IN_BITMAP (control_dependence_map[(N)], 0,	\
112e4b17023SJohn Marino 			    (EDGE_NUMBER), (BI))
113e4b17023SJohn Marino 
114e4b17023SJohn Marino 
115e4b17023SJohn Marino /* Indicate block BB is control dependent on an edge with index EDGE_INDEX.  */
116e4b17023SJohn Marino static inline void
set_control_dependence_map_bit(basic_block bb,int edge_index)117e4b17023SJohn Marino set_control_dependence_map_bit (basic_block bb, int edge_index)
118e4b17023SJohn Marino {
119e4b17023SJohn Marino   if (bb == ENTRY_BLOCK_PTR)
120e4b17023SJohn Marino     return;
121e4b17023SJohn Marino   gcc_assert (bb != EXIT_BLOCK_PTR);
122e4b17023SJohn Marino   bitmap_set_bit (control_dependence_map[bb->index], edge_index);
123e4b17023SJohn Marino }
124e4b17023SJohn Marino 
125e4b17023SJohn Marino /* Clear all control dependences for block BB.  */
126e4b17023SJohn Marino static inline void
clear_control_dependence_bitmap(basic_block bb)127e4b17023SJohn Marino clear_control_dependence_bitmap (basic_block bb)
128e4b17023SJohn Marino {
129e4b17023SJohn Marino   bitmap_clear (control_dependence_map[bb->index]);
130e4b17023SJohn Marino }
131e4b17023SJohn Marino 
132e4b17023SJohn Marino 
133e4b17023SJohn Marino /* Find the immediate postdominator PDOM of the specified basic block BLOCK.
134e4b17023SJohn Marino    This function is necessary because some blocks have negative numbers.  */
135e4b17023SJohn Marino 
136e4b17023SJohn Marino static inline basic_block
find_pdom(basic_block block)137e4b17023SJohn Marino find_pdom (basic_block block)
138e4b17023SJohn Marino {
139e4b17023SJohn Marino   gcc_assert (block != ENTRY_BLOCK_PTR);
140e4b17023SJohn Marino 
141e4b17023SJohn Marino   if (block == EXIT_BLOCK_PTR)
142e4b17023SJohn Marino     return EXIT_BLOCK_PTR;
143e4b17023SJohn Marino   else
144e4b17023SJohn Marino     {
145e4b17023SJohn Marino       basic_block bb = get_immediate_dominator (CDI_POST_DOMINATORS, block);
146e4b17023SJohn Marino       if (! bb)
147e4b17023SJohn Marino 	return EXIT_BLOCK_PTR;
148e4b17023SJohn Marino       return bb;
149e4b17023SJohn Marino     }
150e4b17023SJohn Marino }
151e4b17023SJohn Marino 
152e4b17023SJohn Marino 
153e4b17023SJohn Marino /* Determine all blocks' control dependences on the given edge with edge_list
154e4b17023SJohn Marino    EL index EDGE_INDEX, ala Morgan, Section 3.6.  */
155e4b17023SJohn Marino 
156e4b17023SJohn Marino static void
find_control_dependence(struct edge_list * el,int edge_index)157e4b17023SJohn Marino find_control_dependence (struct edge_list *el, int edge_index)
158e4b17023SJohn Marino {
159e4b17023SJohn Marino   basic_block current_block;
160e4b17023SJohn Marino   basic_block ending_block;
161e4b17023SJohn Marino 
162e4b17023SJohn Marino   gcc_assert (INDEX_EDGE_PRED_BB (el, edge_index) != EXIT_BLOCK_PTR);
163e4b17023SJohn Marino 
164e4b17023SJohn Marino   if (INDEX_EDGE_PRED_BB (el, edge_index) == ENTRY_BLOCK_PTR)
165e4b17023SJohn Marino     ending_block = single_succ (ENTRY_BLOCK_PTR);
166e4b17023SJohn Marino   else
167e4b17023SJohn Marino     ending_block = find_pdom (INDEX_EDGE_PRED_BB (el, edge_index));
168e4b17023SJohn Marino 
169e4b17023SJohn Marino   for (current_block = INDEX_EDGE_SUCC_BB (el, edge_index);
170e4b17023SJohn Marino        current_block != ending_block && current_block != EXIT_BLOCK_PTR;
171e4b17023SJohn Marino        current_block = find_pdom (current_block))
172e4b17023SJohn Marino     {
173e4b17023SJohn Marino       edge e = INDEX_EDGE (el, edge_index);
174e4b17023SJohn Marino 
175e4b17023SJohn Marino       /* For abnormal edges, we don't make current_block control
176e4b17023SJohn Marino 	 dependent because instructions that throw are always necessary
177e4b17023SJohn Marino 	 anyway.  */
178e4b17023SJohn Marino       if (e->flags & EDGE_ABNORMAL)
179e4b17023SJohn Marino 	continue;
180e4b17023SJohn Marino 
181e4b17023SJohn Marino       set_control_dependence_map_bit (current_block, edge_index);
182e4b17023SJohn Marino     }
183e4b17023SJohn Marino }
184e4b17023SJohn Marino 
185e4b17023SJohn Marino 
186e4b17023SJohn Marino /* Record all blocks' control dependences on all edges in the edge
187e4b17023SJohn Marino    list EL, ala Morgan, Section 3.6.  */
188e4b17023SJohn Marino 
189e4b17023SJohn Marino static void
find_all_control_dependences(struct edge_list * el)190e4b17023SJohn Marino find_all_control_dependences (struct edge_list *el)
191e4b17023SJohn Marino {
192e4b17023SJohn Marino   int i;
193e4b17023SJohn Marino 
194e4b17023SJohn Marino   for (i = 0; i < NUM_EDGES (el); ++i)
195e4b17023SJohn Marino     find_control_dependence (el, i);
196e4b17023SJohn Marino }
197e4b17023SJohn Marino 
198e4b17023SJohn Marino /* If STMT is not already marked necessary, mark it, and add it to the
199e4b17023SJohn Marino    worklist if ADD_TO_WORKLIST is true.  */
200e4b17023SJohn Marino 
201e4b17023SJohn Marino static inline void
mark_stmt_necessary(gimple stmt,bool add_to_worklist)202e4b17023SJohn Marino mark_stmt_necessary (gimple stmt, bool add_to_worklist)
203e4b17023SJohn Marino {
204e4b17023SJohn Marino   gcc_assert (stmt);
205e4b17023SJohn Marino 
206e4b17023SJohn Marino   if (gimple_plf (stmt, STMT_NECESSARY))
207e4b17023SJohn Marino     return;
208e4b17023SJohn Marino 
209e4b17023SJohn Marino   if (dump_file && (dump_flags & TDF_DETAILS))
210e4b17023SJohn Marino     {
211e4b17023SJohn Marino       fprintf (dump_file, "Marking useful stmt: ");
212e4b17023SJohn Marino       print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
213e4b17023SJohn Marino       fprintf (dump_file, "\n");
214e4b17023SJohn Marino     }
215e4b17023SJohn Marino 
216e4b17023SJohn Marino   gimple_set_plf (stmt, STMT_NECESSARY, true);
217e4b17023SJohn Marino   if (add_to_worklist)
218e4b17023SJohn Marino     VEC_safe_push (gimple, heap, worklist, stmt);
219e4b17023SJohn Marino   if (bb_contains_live_stmts && !is_gimple_debug (stmt))
220e4b17023SJohn Marino     SET_BIT (bb_contains_live_stmts, gimple_bb (stmt)->index);
221e4b17023SJohn Marino }
222e4b17023SJohn Marino 
223e4b17023SJohn Marino 
224e4b17023SJohn Marino /* Mark the statement defining operand OP as necessary.  */
225e4b17023SJohn Marino 
226e4b17023SJohn Marino static inline void
mark_operand_necessary(tree op)227e4b17023SJohn Marino mark_operand_necessary (tree op)
228e4b17023SJohn Marino {
229e4b17023SJohn Marino   gimple stmt;
230e4b17023SJohn Marino   int ver;
231e4b17023SJohn Marino 
232e4b17023SJohn Marino   gcc_assert (op);
233e4b17023SJohn Marino 
234e4b17023SJohn Marino   ver = SSA_NAME_VERSION (op);
235e4b17023SJohn Marino   if (TEST_BIT (processed, ver))
236e4b17023SJohn Marino     {
237e4b17023SJohn Marino       stmt = SSA_NAME_DEF_STMT (op);
238e4b17023SJohn Marino       gcc_assert (gimple_nop_p (stmt)
239e4b17023SJohn Marino 		  || gimple_plf (stmt, STMT_NECESSARY));
240e4b17023SJohn Marino       return;
241e4b17023SJohn Marino     }
242e4b17023SJohn Marino   SET_BIT (processed, ver);
243e4b17023SJohn Marino 
244e4b17023SJohn Marino   stmt = SSA_NAME_DEF_STMT (op);
245e4b17023SJohn Marino   gcc_assert (stmt);
246e4b17023SJohn Marino 
247e4b17023SJohn Marino   if (gimple_plf (stmt, STMT_NECESSARY) || gimple_nop_p (stmt))
248e4b17023SJohn Marino     return;
249e4b17023SJohn Marino 
250e4b17023SJohn Marino   if (dump_file && (dump_flags & TDF_DETAILS))
251e4b17023SJohn Marino     {
252e4b17023SJohn Marino       fprintf (dump_file, "marking necessary through ");
253e4b17023SJohn Marino       print_generic_expr (dump_file, op, 0);
254e4b17023SJohn Marino       fprintf (dump_file, " stmt ");
255e4b17023SJohn Marino       print_gimple_stmt (dump_file, stmt, 0, 0);
256e4b17023SJohn Marino     }
257e4b17023SJohn Marino 
258e4b17023SJohn Marino   gimple_set_plf (stmt, STMT_NECESSARY, true);
259e4b17023SJohn Marino   if (bb_contains_live_stmts)
260e4b17023SJohn Marino     SET_BIT (bb_contains_live_stmts, gimple_bb (stmt)->index);
261e4b17023SJohn Marino   VEC_safe_push (gimple, heap, worklist, stmt);
262e4b17023SJohn Marino }
263e4b17023SJohn Marino 
264e4b17023SJohn Marino 
265e4b17023SJohn Marino /* Mark STMT as necessary if it obviously is.  Add it to the worklist if
266e4b17023SJohn Marino    it can make other statements necessary.
267e4b17023SJohn Marino 
268e4b17023SJohn Marino    If AGGRESSIVE is false, control statements are conservatively marked as
269e4b17023SJohn Marino    necessary.  */
270e4b17023SJohn Marino 
271e4b17023SJohn Marino static void
mark_stmt_if_obviously_necessary(gimple stmt,bool aggressive)272e4b17023SJohn Marino mark_stmt_if_obviously_necessary (gimple stmt, bool aggressive)
273e4b17023SJohn Marino {
274e4b17023SJohn Marino   /* With non-call exceptions, we have to assume that all statements could
275e4b17023SJohn Marino      throw.  If a statement may throw, it is inherently necessary.  */
276e4b17023SJohn Marino   if (cfun->can_throw_non_call_exceptions && stmt_could_throw_p (stmt))
277e4b17023SJohn Marino     {
278e4b17023SJohn Marino       mark_stmt_necessary (stmt, true);
279e4b17023SJohn Marino       return;
280e4b17023SJohn Marino     }
281e4b17023SJohn Marino 
282e4b17023SJohn Marino   /* Statements that are implicitly live.  Most function calls, asm
283e4b17023SJohn Marino      and return statements are required.  Labels and GIMPLE_BIND nodes
284e4b17023SJohn Marino      are kept because they are control flow, and we have no way of
285e4b17023SJohn Marino      knowing whether they can be removed.  DCE can eliminate all the
286e4b17023SJohn Marino      other statements in a block, and CFG can then remove the block
287e4b17023SJohn Marino      and labels.  */
288e4b17023SJohn Marino   switch (gimple_code (stmt))
289e4b17023SJohn Marino     {
290e4b17023SJohn Marino     case GIMPLE_PREDICT:
291e4b17023SJohn Marino     case GIMPLE_LABEL:
292e4b17023SJohn Marino       mark_stmt_necessary (stmt, false);
293e4b17023SJohn Marino       return;
294e4b17023SJohn Marino 
295e4b17023SJohn Marino     case GIMPLE_ASM:
296e4b17023SJohn Marino     case GIMPLE_RESX:
297e4b17023SJohn Marino     case GIMPLE_RETURN:
298e4b17023SJohn Marino       mark_stmt_necessary (stmt, true);
299e4b17023SJohn Marino       return;
300e4b17023SJohn Marino 
301e4b17023SJohn Marino     case GIMPLE_CALL:
302e4b17023SJohn Marino       {
303e4b17023SJohn Marino 	tree callee = gimple_call_fndecl (stmt);
304e4b17023SJohn Marino 	if (callee != NULL_TREE
305e4b17023SJohn Marino 	    && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
306e4b17023SJohn Marino 	  switch (DECL_FUNCTION_CODE (callee))
307e4b17023SJohn Marino 	    {
308e4b17023SJohn Marino 	    case BUILT_IN_MALLOC:
309e4b17023SJohn Marino 	    case BUILT_IN_CALLOC:
310e4b17023SJohn Marino 	    case BUILT_IN_ALLOCA:
311e4b17023SJohn Marino 	    case BUILT_IN_ALLOCA_WITH_ALIGN:
312e4b17023SJohn Marino 	      return;
313e4b17023SJohn Marino 
314e4b17023SJohn Marino 	    default:;
315e4b17023SJohn Marino 	    }
316e4b17023SJohn Marino 	/* Most, but not all function calls are required.  Function calls that
317e4b17023SJohn Marino 	   produce no result and have no side effects (i.e. const pure
318e4b17023SJohn Marino 	   functions) are unnecessary.  */
319e4b17023SJohn Marino 	if (gimple_has_side_effects (stmt))
320e4b17023SJohn Marino 	  {
321e4b17023SJohn Marino 	    mark_stmt_necessary (stmt, true);
322e4b17023SJohn Marino 	    return;
323e4b17023SJohn Marino 	  }
324e4b17023SJohn Marino 	if (!gimple_call_lhs (stmt))
325e4b17023SJohn Marino 	  return;
326e4b17023SJohn Marino 	break;
327e4b17023SJohn Marino       }
328e4b17023SJohn Marino 
329e4b17023SJohn Marino     case GIMPLE_DEBUG:
330e4b17023SJohn Marino       /* Debug temps without a value are not useful.  ??? If we could
331e4b17023SJohn Marino 	 easily locate the debug temp bind stmt for a use thereof,
332e4b17023SJohn Marino 	 would could refrain from marking all debug temps here, and
333e4b17023SJohn Marino 	 mark them only if they're used.  */
334e4b17023SJohn Marino       if (!gimple_debug_bind_p (stmt)
335e4b17023SJohn Marino 	  || gimple_debug_bind_has_value_p (stmt)
336e4b17023SJohn Marino 	  || TREE_CODE (gimple_debug_bind_get_var (stmt)) != DEBUG_EXPR_DECL)
337e4b17023SJohn Marino 	mark_stmt_necessary (stmt, false);
338e4b17023SJohn Marino       return;
339e4b17023SJohn Marino 
340e4b17023SJohn Marino     case GIMPLE_GOTO:
341e4b17023SJohn Marino       gcc_assert (!simple_goto_p (stmt));
342e4b17023SJohn Marino       mark_stmt_necessary (stmt, true);
343e4b17023SJohn Marino       return;
344e4b17023SJohn Marino 
345e4b17023SJohn Marino     case GIMPLE_COND:
346e4b17023SJohn Marino       gcc_assert (EDGE_COUNT (gimple_bb (stmt)->succs) == 2);
347e4b17023SJohn Marino       /* Fall through.  */
348e4b17023SJohn Marino 
349e4b17023SJohn Marino     case GIMPLE_SWITCH:
350e4b17023SJohn Marino       if (! aggressive)
351e4b17023SJohn Marino 	mark_stmt_necessary (stmt, true);
352e4b17023SJohn Marino       break;
353e4b17023SJohn Marino 
354e4b17023SJohn Marino     case GIMPLE_ASSIGN:
355e4b17023SJohn Marino       if (TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME
356e4b17023SJohn Marino 	  && TREE_CLOBBER_P (gimple_assign_rhs1 (stmt)))
357e4b17023SJohn Marino 	return;
358e4b17023SJohn Marino       break;
359e4b17023SJohn Marino 
360e4b17023SJohn Marino     default:
361e4b17023SJohn Marino       break;
362e4b17023SJohn Marino     }
363e4b17023SJohn Marino 
364e4b17023SJohn Marino   /* If the statement has volatile operands, it needs to be preserved.
365e4b17023SJohn Marino      Same for statements that can alter control flow in unpredictable
366e4b17023SJohn Marino      ways.  */
367e4b17023SJohn Marino   if (gimple_has_volatile_ops (stmt) || is_ctrl_altering_stmt (stmt))
368e4b17023SJohn Marino     {
369e4b17023SJohn Marino       mark_stmt_necessary (stmt, true);
370e4b17023SJohn Marino       return;
371e4b17023SJohn Marino     }
372e4b17023SJohn Marino 
373e4b17023SJohn Marino   if (is_hidden_global_store (stmt))
374e4b17023SJohn Marino     {
375e4b17023SJohn Marino       mark_stmt_necessary (stmt, true);
376e4b17023SJohn Marino       return;
377e4b17023SJohn Marino     }
378e4b17023SJohn Marino 
379e4b17023SJohn Marino   return;
380e4b17023SJohn Marino }
381e4b17023SJohn Marino 
382e4b17023SJohn Marino 
383e4b17023SJohn Marino /* Mark the last statement of BB as necessary.  */
384e4b17023SJohn Marino 
385e4b17023SJohn Marino static void
mark_last_stmt_necessary(basic_block bb)386e4b17023SJohn Marino mark_last_stmt_necessary (basic_block bb)
387e4b17023SJohn Marino {
388e4b17023SJohn Marino   gimple stmt = last_stmt (bb);
389e4b17023SJohn Marino 
390e4b17023SJohn Marino   SET_BIT (last_stmt_necessary, bb->index);
391e4b17023SJohn Marino   SET_BIT (bb_contains_live_stmts, bb->index);
392e4b17023SJohn Marino 
393e4b17023SJohn Marino   /* We actually mark the statement only if it is a control statement.  */
394e4b17023SJohn Marino   if (stmt && is_ctrl_stmt (stmt))
395e4b17023SJohn Marino     mark_stmt_necessary (stmt, true);
396e4b17023SJohn Marino }
397e4b17023SJohn Marino 
398e4b17023SJohn Marino 
399e4b17023SJohn Marino /* Mark control dependent edges of BB as necessary.  We have to do this only
400e4b17023SJohn Marino    once for each basic block so we set the appropriate bit after we're done.
401e4b17023SJohn Marino 
402e4b17023SJohn Marino    When IGNORE_SELF is true, ignore BB in the list of control dependences.  */
403e4b17023SJohn Marino 
404e4b17023SJohn Marino static void
mark_control_dependent_edges_necessary(basic_block bb,struct edge_list * el,bool ignore_self)405e4b17023SJohn Marino mark_control_dependent_edges_necessary (basic_block bb, struct edge_list *el,
406e4b17023SJohn Marino 					bool ignore_self)
407e4b17023SJohn Marino {
408e4b17023SJohn Marino   bitmap_iterator bi;
409e4b17023SJohn Marino   unsigned edge_number;
410e4b17023SJohn Marino   bool skipped = false;
411e4b17023SJohn Marino 
412e4b17023SJohn Marino   gcc_assert (bb != EXIT_BLOCK_PTR);
413e4b17023SJohn Marino 
414e4b17023SJohn Marino   if (bb == ENTRY_BLOCK_PTR)
415e4b17023SJohn Marino     return;
416e4b17023SJohn Marino 
417e4b17023SJohn Marino   EXECUTE_IF_CONTROL_DEPENDENT (bi, bb->index, edge_number)
418e4b17023SJohn Marino     {
419e4b17023SJohn Marino       basic_block cd_bb = INDEX_EDGE_PRED_BB (el, edge_number);
420e4b17023SJohn Marino 
421e4b17023SJohn Marino       if (ignore_self && cd_bb == bb)
422e4b17023SJohn Marino 	{
423e4b17023SJohn Marino 	  skipped = true;
424e4b17023SJohn Marino 	  continue;
425e4b17023SJohn Marino 	}
426e4b17023SJohn Marino 
427e4b17023SJohn Marino       if (!TEST_BIT (last_stmt_necessary, cd_bb->index))
428e4b17023SJohn Marino 	mark_last_stmt_necessary (cd_bb);
429e4b17023SJohn Marino     }
430e4b17023SJohn Marino 
431e4b17023SJohn Marino   if (!skipped)
432e4b17023SJohn Marino     SET_BIT (visited_control_parents, bb->index);
433e4b17023SJohn Marino }
434e4b17023SJohn Marino 
435e4b17023SJohn Marino 
436e4b17023SJohn Marino /* Find obviously necessary statements.  These are things like most function
437e4b17023SJohn Marino    calls, and stores to file level variables.
438e4b17023SJohn Marino 
439e4b17023SJohn Marino    If EL is NULL, control statements are conservatively marked as
440e4b17023SJohn Marino    necessary.  Otherwise it contains the list of edges used by control
441e4b17023SJohn Marino    dependence analysis.  */
442e4b17023SJohn Marino 
443e4b17023SJohn Marino static void
find_obviously_necessary_stmts(struct edge_list * el)444e4b17023SJohn Marino find_obviously_necessary_stmts (struct edge_list *el)
445e4b17023SJohn Marino {
446e4b17023SJohn Marino   basic_block bb;
447e4b17023SJohn Marino   gimple_stmt_iterator gsi;
448e4b17023SJohn Marino   edge e;
449e4b17023SJohn Marino   gimple phi, stmt;
450e4b17023SJohn Marino   int flags;
451e4b17023SJohn Marino 
452e4b17023SJohn Marino   FOR_EACH_BB (bb)
453e4b17023SJohn Marino     {
454e4b17023SJohn Marino       /* PHI nodes are never inherently necessary.  */
455e4b17023SJohn Marino       for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
456e4b17023SJohn Marino 	{
457e4b17023SJohn Marino 	  phi = gsi_stmt (gsi);
458e4b17023SJohn Marino 	  gimple_set_plf (phi, STMT_NECESSARY, false);
459e4b17023SJohn Marino 	}
460e4b17023SJohn Marino 
461e4b17023SJohn Marino       /* Check all statements in the block.  */
462e4b17023SJohn Marino       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
463e4b17023SJohn Marino 	{
464e4b17023SJohn Marino 	  stmt = gsi_stmt (gsi);
465e4b17023SJohn Marino 	  gimple_set_plf (stmt, STMT_NECESSARY, false);
466e4b17023SJohn Marino 	  mark_stmt_if_obviously_necessary (stmt, el != NULL);
467e4b17023SJohn Marino 	}
468e4b17023SJohn Marino     }
469e4b17023SJohn Marino 
470e4b17023SJohn Marino   /* Pure and const functions are finite and thus have no infinite loops in
471e4b17023SJohn Marino      them.  */
472e4b17023SJohn Marino   flags = flags_from_decl_or_type (current_function_decl);
473e4b17023SJohn Marino   if ((flags & (ECF_CONST|ECF_PURE)) && !(flags & ECF_LOOPING_CONST_OR_PURE))
474e4b17023SJohn Marino     return;
475e4b17023SJohn Marino 
476e4b17023SJohn Marino   /* Prevent the empty possibly infinite loops from being removed.  */
477e4b17023SJohn Marino   if (el)
478e4b17023SJohn Marino     {
479e4b17023SJohn Marino       loop_iterator li;
480e4b17023SJohn Marino       struct loop *loop;
481e4b17023SJohn Marino       scev_initialize ();
482e4b17023SJohn Marino       if (mark_irreducible_loops ())
483e4b17023SJohn Marino 	FOR_EACH_BB (bb)
484e4b17023SJohn Marino 	  {
485e4b17023SJohn Marino 	    edge_iterator ei;
486e4b17023SJohn Marino 	    FOR_EACH_EDGE (e, ei, bb->succs)
487e4b17023SJohn Marino 	      if ((e->flags & EDGE_DFS_BACK)
488e4b17023SJohn Marino 		  && (e->flags & EDGE_IRREDUCIBLE_LOOP))
489e4b17023SJohn Marino 		{
490e4b17023SJohn Marino 	          if (dump_file)
491e4b17023SJohn Marino 	            fprintf (dump_file, "Marking back edge of irreducible loop %i->%i\n",
492e4b17023SJohn Marino 		    	     e->src->index, e->dest->index);
493e4b17023SJohn Marino 		  mark_control_dependent_edges_necessary (e->dest, el, false);
494e4b17023SJohn Marino 		}
495e4b17023SJohn Marino 	  }
496e4b17023SJohn Marino 
497e4b17023SJohn Marino       FOR_EACH_LOOP (li, loop, 0)
498e4b17023SJohn Marino 	if (!finite_loop_p (loop))
499e4b17023SJohn Marino 	  {
500e4b17023SJohn Marino 	    if (dump_file)
501e4b17023SJohn Marino 	      fprintf (dump_file, "can not prove finiteness of loop %i\n", loop->num);
502e4b17023SJohn Marino 	    mark_control_dependent_edges_necessary (loop->latch, el, false);
503e4b17023SJohn Marino 	  }
504e4b17023SJohn Marino       scev_finalize ();
505e4b17023SJohn Marino     }
506e4b17023SJohn Marino }
507e4b17023SJohn Marino 
508e4b17023SJohn Marino 
509e4b17023SJohn Marino /* Return true if REF is based on an aliased base, otherwise false.  */
510e4b17023SJohn Marino 
511e4b17023SJohn Marino static bool
ref_may_be_aliased(tree ref)512e4b17023SJohn Marino ref_may_be_aliased (tree ref)
513e4b17023SJohn Marino {
514e4b17023SJohn Marino   gcc_assert (TREE_CODE (ref) != WITH_SIZE_EXPR);
515e4b17023SJohn Marino   while (handled_component_p (ref))
516e4b17023SJohn Marino     ref = TREE_OPERAND (ref, 0);
517e4b17023SJohn Marino   if (TREE_CODE (ref) == MEM_REF
518e4b17023SJohn Marino       && TREE_CODE (TREE_OPERAND (ref, 0)) == ADDR_EXPR)
519e4b17023SJohn Marino     ref = TREE_OPERAND (TREE_OPERAND (ref, 0), 0);
520e4b17023SJohn Marino   return !(DECL_P (ref)
521e4b17023SJohn Marino 	   && !may_be_aliased (ref));
522e4b17023SJohn Marino }
523e4b17023SJohn Marino 
524e4b17023SJohn Marino static bitmap visited = NULL;
525e4b17023SJohn Marino static unsigned int longest_chain = 0;
526e4b17023SJohn Marino static unsigned int total_chain = 0;
527e4b17023SJohn Marino static unsigned int nr_walks = 0;
528e4b17023SJohn Marino static bool chain_ovfl = false;
529e4b17023SJohn Marino 
530e4b17023SJohn Marino /* Worker for the walker that marks reaching definitions of REF,
531e4b17023SJohn Marino    which is based on a non-aliased decl, necessary.  It returns
532e4b17023SJohn Marino    true whenever the defining statement of the current VDEF is
533e4b17023SJohn Marino    a kill for REF, as no dominating may-defs are necessary for REF
534e4b17023SJohn Marino    anymore.  DATA points to the basic-block that contains the
535e4b17023SJohn Marino    stmt that refers to REF.  */
536e4b17023SJohn Marino 
537e4b17023SJohn Marino static bool
mark_aliased_reaching_defs_necessary_1(ao_ref * ref,tree vdef,void * data)538e4b17023SJohn Marino mark_aliased_reaching_defs_necessary_1 (ao_ref *ref, tree vdef, void *data)
539e4b17023SJohn Marino {
540e4b17023SJohn Marino   gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
541e4b17023SJohn Marino 
542e4b17023SJohn Marino   /* All stmts we visit are necessary.  */
543e4b17023SJohn Marino   mark_operand_necessary (vdef);
544e4b17023SJohn Marino 
545e4b17023SJohn Marino   /* If the stmt lhs kills ref, then we can stop walking.  */
546e4b17023SJohn Marino   if (gimple_has_lhs (def_stmt)
547e4b17023SJohn Marino       && TREE_CODE (gimple_get_lhs (def_stmt)) != SSA_NAME
548e4b17023SJohn Marino       /* The assignment is not necessarily carried out if it can throw
549e4b17023SJohn Marino          and we can catch it in the current function where we could inspect
550e4b17023SJohn Marino 	 the previous value.
551e4b17023SJohn Marino          ???  We only need to care about the RHS throwing.  For aggregate
552e4b17023SJohn Marino 	 assignments or similar calls and non-call exceptions the LHS
553e4b17023SJohn Marino 	 might throw as well.  */
554e4b17023SJohn Marino       && !stmt_can_throw_internal (def_stmt))
555e4b17023SJohn Marino     {
556e4b17023SJohn Marino       tree base, lhs = gimple_get_lhs (def_stmt);
557e4b17023SJohn Marino       HOST_WIDE_INT size, offset, max_size;
558e4b17023SJohn Marino       ao_ref_base (ref);
559e4b17023SJohn Marino       base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
560e4b17023SJohn Marino       /* We can get MEM[symbol: sZ, index: D.8862_1] here,
561e4b17023SJohn Marino 	 so base == refd->base does not always hold.  */
562e4b17023SJohn Marino       if (base == ref->base)
563e4b17023SJohn Marino 	{
564e4b17023SJohn Marino 	  /* For a must-alias check we need to be able to constrain
565e4b17023SJohn Marino 	     the accesses properly.  */
566e4b17023SJohn Marino 	  if (size != -1 && size == max_size
567e4b17023SJohn Marino 	      && ref->max_size != -1)
568e4b17023SJohn Marino 	    {
569e4b17023SJohn Marino 	      if (offset <= ref->offset
570e4b17023SJohn Marino 		  && offset + size >= ref->offset + ref->max_size)
571e4b17023SJohn Marino 		return true;
572e4b17023SJohn Marino 	    }
573e4b17023SJohn Marino 	  /* Or they need to be exactly the same.  */
574e4b17023SJohn Marino 	  else if (ref->ref
575e4b17023SJohn Marino 		   /* Make sure there is no induction variable involved
576e4b17023SJohn Marino 		      in the references (gcc.c-torture/execute/pr42142.c).
577e4b17023SJohn Marino 		      The simplest way is to check if the kill dominates
578e4b17023SJohn Marino 		      the use.  */
579*95d28233SJohn Marino 		   /* But when both are in the same block we cannot
580*95d28233SJohn Marino 		      easily tell whether we came from a backedge
581*95d28233SJohn Marino 		      unless we decide to compute stmt UIDs
582*95d28233SJohn Marino 		      (see PR58246).  */
583*95d28233SJohn Marino 		   && (basic_block) data != gimple_bb (def_stmt)
584e4b17023SJohn Marino 		   && dominated_by_p (CDI_DOMINATORS, (basic_block) data,
585e4b17023SJohn Marino 				      gimple_bb (def_stmt))
586e4b17023SJohn Marino 		   && operand_equal_p (ref->ref, lhs, 0))
587e4b17023SJohn Marino 	    return true;
588e4b17023SJohn Marino 	}
589e4b17023SJohn Marino     }
590e4b17023SJohn Marino 
591e4b17023SJohn Marino   /* Otherwise keep walking.  */
592e4b17023SJohn Marino   return false;
593e4b17023SJohn Marino }
594e4b17023SJohn Marino 
595e4b17023SJohn Marino static void
mark_aliased_reaching_defs_necessary(gimple stmt,tree ref)596e4b17023SJohn Marino mark_aliased_reaching_defs_necessary (gimple stmt, tree ref)
597e4b17023SJohn Marino {
598e4b17023SJohn Marino   unsigned int chain;
599e4b17023SJohn Marino   ao_ref refd;
600e4b17023SJohn Marino   gcc_assert (!chain_ovfl);
601e4b17023SJohn Marino   ao_ref_init (&refd, ref);
602e4b17023SJohn Marino   chain = walk_aliased_vdefs (&refd, gimple_vuse (stmt),
603e4b17023SJohn Marino 			      mark_aliased_reaching_defs_necessary_1,
604e4b17023SJohn Marino 			      gimple_bb (stmt), NULL);
605e4b17023SJohn Marino   if (chain > longest_chain)
606e4b17023SJohn Marino     longest_chain = chain;
607e4b17023SJohn Marino   total_chain += chain;
608e4b17023SJohn Marino   nr_walks++;
609e4b17023SJohn Marino }
610e4b17023SJohn Marino 
611e4b17023SJohn Marino /* Worker for the walker that marks reaching definitions of REF, which
612e4b17023SJohn Marino    is not based on a non-aliased decl.  For simplicity we need to end
613e4b17023SJohn Marino    up marking all may-defs necessary that are not based on a non-aliased
614e4b17023SJohn Marino    decl.  The only job of this walker is to skip may-defs based on
615e4b17023SJohn Marino    a non-aliased decl.  */
616e4b17023SJohn Marino 
617e4b17023SJohn Marino static bool
mark_all_reaching_defs_necessary_1(ao_ref * ref ATTRIBUTE_UNUSED,tree vdef,void * data ATTRIBUTE_UNUSED)618e4b17023SJohn Marino mark_all_reaching_defs_necessary_1 (ao_ref *ref ATTRIBUTE_UNUSED,
619e4b17023SJohn Marino 				    tree vdef, void *data ATTRIBUTE_UNUSED)
620e4b17023SJohn Marino {
621e4b17023SJohn Marino   gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
622e4b17023SJohn Marino 
623e4b17023SJohn Marino   /* We have to skip already visited (and thus necessary) statements
624e4b17023SJohn Marino      to make the chaining work after we dropped back to simple mode.  */
625e4b17023SJohn Marino   if (chain_ovfl
626e4b17023SJohn Marino       && TEST_BIT (processed, SSA_NAME_VERSION (vdef)))
627e4b17023SJohn Marino     {
628e4b17023SJohn Marino       gcc_assert (gimple_nop_p (def_stmt)
629e4b17023SJohn Marino 		  || gimple_plf (def_stmt, STMT_NECESSARY));
630e4b17023SJohn Marino       return false;
631e4b17023SJohn Marino     }
632e4b17023SJohn Marino 
633e4b17023SJohn Marino   /* We want to skip stores to non-aliased variables.  */
634e4b17023SJohn Marino   if (!chain_ovfl
635e4b17023SJohn Marino       && gimple_assign_single_p (def_stmt))
636e4b17023SJohn Marino     {
637e4b17023SJohn Marino       tree lhs = gimple_assign_lhs (def_stmt);
638e4b17023SJohn Marino       if (!ref_may_be_aliased (lhs))
639e4b17023SJohn Marino 	return false;
640e4b17023SJohn Marino     }
641e4b17023SJohn Marino 
642e4b17023SJohn Marino   /* We want to skip statments that do not constitute stores but have
643e4b17023SJohn Marino      a virtual definition.  */
644e4b17023SJohn Marino   if (is_gimple_call (def_stmt))
645e4b17023SJohn Marino     {
646e4b17023SJohn Marino       tree callee = gimple_call_fndecl (def_stmt);
647e4b17023SJohn Marino       if (callee != NULL_TREE
648e4b17023SJohn Marino 	  && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
649e4b17023SJohn Marino 	switch (DECL_FUNCTION_CODE (callee))
650e4b17023SJohn Marino 	  {
651e4b17023SJohn Marino 	  case BUILT_IN_MALLOC:
652e4b17023SJohn Marino 	  case BUILT_IN_CALLOC:
653e4b17023SJohn Marino 	  case BUILT_IN_ALLOCA:
654e4b17023SJohn Marino 	  case BUILT_IN_ALLOCA_WITH_ALIGN:
655e4b17023SJohn Marino 	  case BUILT_IN_FREE:
656e4b17023SJohn Marino 	    return false;
657e4b17023SJohn Marino 
658e4b17023SJohn Marino 	  default:;
659e4b17023SJohn Marino 	  }
660e4b17023SJohn Marino     }
661e4b17023SJohn Marino 
662e4b17023SJohn Marino   mark_operand_necessary (vdef);
663e4b17023SJohn Marino 
664e4b17023SJohn Marino   return false;
665e4b17023SJohn Marino }
666e4b17023SJohn Marino 
667e4b17023SJohn Marino static void
mark_all_reaching_defs_necessary(gimple stmt)668e4b17023SJohn Marino mark_all_reaching_defs_necessary (gimple stmt)
669e4b17023SJohn Marino {
670e4b17023SJohn Marino   walk_aliased_vdefs (NULL, gimple_vuse (stmt),
671e4b17023SJohn Marino 		      mark_all_reaching_defs_necessary_1, NULL, &visited);
672e4b17023SJohn Marino }
673e4b17023SJohn Marino 
674e4b17023SJohn Marino /* Return true for PHI nodes with one or identical arguments
675e4b17023SJohn Marino    can be removed.  */
676e4b17023SJohn Marino static bool
degenerate_phi_p(gimple phi)677e4b17023SJohn Marino degenerate_phi_p (gimple phi)
678e4b17023SJohn Marino {
679e4b17023SJohn Marino   unsigned int i;
680e4b17023SJohn Marino   tree op = gimple_phi_arg_def (phi, 0);
681e4b17023SJohn Marino   for (i = 1; i < gimple_phi_num_args (phi); i++)
682e4b17023SJohn Marino     if (gimple_phi_arg_def (phi, i) != op)
683e4b17023SJohn Marino       return false;
684e4b17023SJohn Marino   return true;
685e4b17023SJohn Marino }
686e4b17023SJohn Marino 
687e4b17023SJohn Marino /* Propagate necessity using the operands of necessary statements.
688e4b17023SJohn Marino    Process the uses on each statement in the worklist, and add all
689e4b17023SJohn Marino    feeding statements which contribute to the calculation of this
690e4b17023SJohn Marino    value to the worklist.
691e4b17023SJohn Marino 
692e4b17023SJohn Marino    In conservative mode, EL is NULL.  */
693e4b17023SJohn Marino 
694e4b17023SJohn Marino static void
propagate_necessity(struct edge_list * el)695e4b17023SJohn Marino propagate_necessity (struct edge_list *el)
696e4b17023SJohn Marino {
697e4b17023SJohn Marino   gimple stmt;
698e4b17023SJohn Marino   bool aggressive = (el ? true : false);
699e4b17023SJohn Marino 
700e4b17023SJohn Marino   if (dump_file && (dump_flags & TDF_DETAILS))
701e4b17023SJohn Marino     fprintf (dump_file, "\nProcessing worklist:\n");
702e4b17023SJohn Marino 
703e4b17023SJohn Marino   while (VEC_length (gimple, worklist) > 0)
704e4b17023SJohn Marino     {
705e4b17023SJohn Marino       /* Take STMT from worklist.  */
706e4b17023SJohn Marino       stmt = VEC_pop (gimple, worklist);
707e4b17023SJohn Marino 
708e4b17023SJohn Marino       if (dump_file && (dump_flags & TDF_DETAILS))
709e4b17023SJohn Marino 	{
710e4b17023SJohn Marino 	  fprintf (dump_file, "processing: ");
711e4b17023SJohn Marino 	  print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
712e4b17023SJohn Marino 	  fprintf (dump_file, "\n");
713e4b17023SJohn Marino 	}
714e4b17023SJohn Marino 
715e4b17023SJohn Marino       if (aggressive)
716e4b17023SJohn Marino 	{
717e4b17023SJohn Marino 	  /* Mark the last statement of the basic blocks on which the block
718e4b17023SJohn Marino 	     containing STMT is control dependent, but only if we haven't
719e4b17023SJohn Marino 	     already done so.  */
720e4b17023SJohn Marino 	  basic_block bb = gimple_bb (stmt);
721e4b17023SJohn Marino 	  if (bb != ENTRY_BLOCK_PTR
722e4b17023SJohn Marino 	      && !TEST_BIT (visited_control_parents, bb->index))
723e4b17023SJohn Marino 	    mark_control_dependent_edges_necessary (bb, el, false);
724e4b17023SJohn Marino 	}
725e4b17023SJohn Marino 
726e4b17023SJohn Marino       if (gimple_code (stmt) == GIMPLE_PHI
727e4b17023SJohn Marino 	  /* We do not process virtual PHI nodes nor do we track their
728e4b17023SJohn Marino 	     necessity.  */
729e4b17023SJohn Marino 	  && is_gimple_reg (gimple_phi_result (stmt)))
730e4b17023SJohn Marino 	{
731e4b17023SJohn Marino 	  /* PHI nodes are somewhat special in that each PHI alternative has
732e4b17023SJohn Marino 	     data and control dependencies.  All the statements feeding the
733e4b17023SJohn Marino 	     PHI node's arguments are always necessary.  In aggressive mode,
734e4b17023SJohn Marino 	     we also consider the control dependent edges leading to the
735e4b17023SJohn Marino 	     predecessor block associated with each PHI alternative as
736e4b17023SJohn Marino 	     necessary.  */
737e4b17023SJohn Marino 	  size_t k;
738e4b17023SJohn Marino 
739e4b17023SJohn Marino 	  for (k = 0; k < gimple_phi_num_args (stmt); k++)
740e4b17023SJohn Marino             {
741e4b17023SJohn Marino 	      tree arg = PHI_ARG_DEF (stmt, k);
742e4b17023SJohn Marino 	      if (TREE_CODE (arg) == SSA_NAME)
743e4b17023SJohn Marino 		mark_operand_necessary (arg);
744e4b17023SJohn Marino 	    }
745e4b17023SJohn Marino 
746e4b17023SJohn Marino 	  /* For PHI operands it matters from where the control flow arrives
747e4b17023SJohn Marino 	     to the BB.  Consider the following example:
748e4b17023SJohn Marino 
749e4b17023SJohn Marino 	     a=exp1;
750e4b17023SJohn Marino 	     b=exp2;
751e4b17023SJohn Marino 	     if (test)
752e4b17023SJohn Marino 		;
753e4b17023SJohn Marino 	     else
754e4b17023SJohn Marino 		;
755e4b17023SJohn Marino 	     c=PHI(a,b)
756e4b17023SJohn Marino 
757e4b17023SJohn Marino 	     We need to mark control dependence of the empty basic blocks, since they
758e4b17023SJohn Marino 	     contains computation of PHI operands.
759e4b17023SJohn Marino 
760e4b17023SJohn Marino 	     Doing so is too restrictive in the case the predecestor block is in
761e4b17023SJohn Marino 	     the loop. Consider:
762e4b17023SJohn Marino 
763e4b17023SJohn Marino 	      if (b)
764e4b17023SJohn Marino 		{
765e4b17023SJohn Marino 		  int i;
766e4b17023SJohn Marino 		  for (i = 0; i<1000; ++i)
767e4b17023SJohn Marino 		    ;
768e4b17023SJohn Marino 		  j = 0;
769e4b17023SJohn Marino 		}
770e4b17023SJohn Marino 	      return j;
771e4b17023SJohn Marino 
772e4b17023SJohn Marino 	     There is PHI for J in the BB containing return statement.
773e4b17023SJohn Marino 	     In this case the control dependence of predecestor block (that is
774e4b17023SJohn Marino 	     within the empty loop) also contains the block determining number
775e4b17023SJohn Marino 	     of iterations of the block that would prevent removing of empty
776e4b17023SJohn Marino 	     loop in this case.
777e4b17023SJohn Marino 
778e4b17023SJohn Marino 	     This scenario can be avoided by splitting critical edges.
779e4b17023SJohn Marino 	     To save the critical edge splitting pass we identify how the control
780e4b17023SJohn Marino 	     dependence would look like if the edge was split.
781e4b17023SJohn Marino 
782e4b17023SJohn Marino 	     Consider the modified CFG created from current CFG by splitting
783e4b17023SJohn Marino 	     edge B->C.  In the postdominance tree of modified CFG, C' is
784e4b17023SJohn Marino 	     always child of C.  There are two cases how chlids of C' can look
785e4b17023SJohn Marino 	     like:
786e4b17023SJohn Marino 
787e4b17023SJohn Marino 		1) C' is leaf
788e4b17023SJohn Marino 
789e4b17023SJohn Marino 		   In this case the only basic block C' is control dependent on is B.
790e4b17023SJohn Marino 
791e4b17023SJohn Marino 		2) C' has single child that is B
792e4b17023SJohn Marino 
793e4b17023SJohn Marino 		   In this case control dependence of C' is same as control
794e4b17023SJohn Marino 		   dependence of B in original CFG except for block B itself.
795e4b17023SJohn Marino 		   (since C' postdominate B in modified CFG)
796e4b17023SJohn Marino 
797e4b17023SJohn Marino 	     Now how to decide what case happens?  There are two basic options:
798e4b17023SJohn Marino 
799e4b17023SJohn Marino 		a) C postdominate B.  Then C immediately postdominate B and
800e4b17023SJohn Marino 		   case 2 happens iff there is no other way from B to C except
801e4b17023SJohn Marino 		   the edge B->C.
802e4b17023SJohn Marino 
803e4b17023SJohn Marino 		   There is other way from B to C iff there is succesor of B that
804e4b17023SJohn Marino 		   is not postdominated by B.  Testing this condition is somewhat
805e4b17023SJohn Marino 		   expensive, because we need to iterate all succesors of B.
806e4b17023SJohn Marino 		   We are safe to assume that this does not happen: we will mark B
807e4b17023SJohn Marino 		   as needed when processing the other path from B to C that is
808e4b17023SJohn Marino 		   conrol dependent on B and marking control dependencies of B
809e4b17023SJohn Marino 		   itself is harmless because they will be processed anyway after
810e4b17023SJohn Marino 		   processing control statement in B.
811e4b17023SJohn Marino 
812e4b17023SJohn Marino 		b) C does not postdominate B.  Always case 1 happens since there is
813e4b17023SJohn Marino 		   path from C to exit that does not go through B and thus also C'.  */
814e4b17023SJohn Marino 
815e4b17023SJohn Marino 	  if (aggressive && !degenerate_phi_p (stmt))
816e4b17023SJohn Marino 	    {
817e4b17023SJohn Marino 	      for (k = 0; k < gimple_phi_num_args (stmt); k++)
818e4b17023SJohn Marino 		{
819e4b17023SJohn Marino 		  basic_block arg_bb = gimple_phi_arg_edge (stmt, k)->src;
820e4b17023SJohn Marino 
821e4b17023SJohn Marino 		  if (gimple_bb (stmt)
822e4b17023SJohn Marino 		      != get_immediate_dominator (CDI_POST_DOMINATORS, arg_bb))
823e4b17023SJohn Marino 		    {
824e4b17023SJohn Marino 		      if (!TEST_BIT (last_stmt_necessary, arg_bb->index))
825e4b17023SJohn Marino 			mark_last_stmt_necessary (arg_bb);
826e4b17023SJohn Marino 		    }
827e4b17023SJohn Marino 		  else if (arg_bb != ENTRY_BLOCK_PTR
828e4b17023SJohn Marino 		           && !TEST_BIT (visited_control_parents,
829e4b17023SJohn Marino 					 arg_bb->index))
830e4b17023SJohn Marino 		    mark_control_dependent_edges_necessary (arg_bb, el, true);
831e4b17023SJohn Marino 		}
832e4b17023SJohn Marino 	    }
833e4b17023SJohn Marino 	}
834e4b17023SJohn Marino       else
835e4b17023SJohn Marino 	{
836e4b17023SJohn Marino 	  /* Propagate through the operands.  Examine all the USE, VUSE and
837e4b17023SJohn Marino 	     VDEF operands in this statement.  Mark all the statements
838e4b17023SJohn Marino 	     which feed this statement's uses as necessary.  */
839e4b17023SJohn Marino 	  ssa_op_iter iter;
840e4b17023SJohn Marino 	  tree use;
841e4b17023SJohn Marino 
842e4b17023SJohn Marino 	  /* If this is a call to free which is directly fed by an
843e4b17023SJohn Marino 	     allocation function do not mark that necessary through
844e4b17023SJohn Marino 	     processing the argument.  */
845e4b17023SJohn Marino 	  if (gimple_call_builtin_p (stmt, BUILT_IN_FREE))
846e4b17023SJohn Marino 	    {
847e4b17023SJohn Marino 	      tree ptr = gimple_call_arg (stmt, 0);
848e4b17023SJohn Marino 	      gimple def_stmt;
849e4b17023SJohn Marino 	      tree def_callee;
850e4b17023SJohn Marino 	      /* If the pointer we free is defined by an allocation
851e4b17023SJohn Marino 		 function do not add the call to the worklist.  */
852e4b17023SJohn Marino 	      if (TREE_CODE (ptr) == SSA_NAME
853e4b17023SJohn Marino 		  && is_gimple_call (def_stmt = SSA_NAME_DEF_STMT (ptr))
854e4b17023SJohn Marino 		  && (def_callee = gimple_call_fndecl (def_stmt))
855e4b17023SJohn Marino 		  && DECL_BUILT_IN_CLASS (def_callee) == BUILT_IN_NORMAL
856e4b17023SJohn Marino 		  && (DECL_FUNCTION_CODE (def_callee) == BUILT_IN_MALLOC
857e4b17023SJohn Marino 		      || DECL_FUNCTION_CODE (def_callee) == BUILT_IN_CALLOC))
858e4b17023SJohn Marino 		continue;
859e4b17023SJohn Marino 	    }
860e4b17023SJohn Marino 
861e4b17023SJohn Marino 	  FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
862e4b17023SJohn Marino 	    mark_operand_necessary (use);
863e4b17023SJohn Marino 
864e4b17023SJohn Marino 	  use = gimple_vuse (stmt);
865e4b17023SJohn Marino 	  if (!use)
866e4b17023SJohn Marino 	    continue;
867e4b17023SJohn Marino 
868e4b17023SJohn Marino 	  /* If we dropped to simple mode make all immediately
869e4b17023SJohn Marino 	     reachable definitions necessary.  */
870e4b17023SJohn Marino 	  if (chain_ovfl)
871e4b17023SJohn Marino 	    {
872e4b17023SJohn Marino 	      mark_all_reaching_defs_necessary (stmt);
873e4b17023SJohn Marino 	      continue;
874e4b17023SJohn Marino 	    }
875e4b17023SJohn Marino 
876e4b17023SJohn Marino 	  /* For statements that may load from memory (have a VUSE) we
877e4b17023SJohn Marino 	     have to mark all reaching (may-)definitions as necessary.
878e4b17023SJohn Marino 	     We partition this task into two cases:
879e4b17023SJohn Marino 	      1) explicit loads based on decls that are not aliased
880e4b17023SJohn Marino 	      2) implicit loads (like calls) and explicit loads not
881e4b17023SJohn Marino 	         based on decls that are not aliased (like indirect
882e4b17023SJohn Marino 		 references or loads from globals)
883e4b17023SJohn Marino 	     For 1) we mark all reaching may-defs as necessary, stopping
884e4b17023SJohn Marino 	     at dominating kills.  For 2) we want to mark all dominating
885e4b17023SJohn Marino 	     references necessary, but non-aliased ones which we handle
886e4b17023SJohn Marino 	     in 1).  By keeping a global visited bitmap for references
887e4b17023SJohn Marino 	     we walk for 2) we avoid quadratic behavior for those.  */
888e4b17023SJohn Marino 
889e4b17023SJohn Marino 	  if (is_gimple_call (stmt))
890e4b17023SJohn Marino 	    {
891e4b17023SJohn Marino 	      tree callee = gimple_call_fndecl (stmt);
892e4b17023SJohn Marino 	      unsigned i;
893e4b17023SJohn Marino 
894e4b17023SJohn Marino 	      /* Calls to functions that are merely acting as barriers
895e4b17023SJohn Marino 		 or that only store to memory do not make any previous
896e4b17023SJohn Marino 		 stores necessary.  */
897e4b17023SJohn Marino 	      if (callee != NULL_TREE
898e4b17023SJohn Marino 		  && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
899e4b17023SJohn Marino 		  && (DECL_FUNCTION_CODE (callee) == BUILT_IN_MEMSET
900e4b17023SJohn Marino 		      || DECL_FUNCTION_CODE (callee) == BUILT_IN_MEMSET_CHK
901e4b17023SJohn Marino 		      || DECL_FUNCTION_CODE (callee) == BUILT_IN_MALLOC
902e4b17023SJohn Marino 		      || DECL_FUNCTION_CODE (callee) == BUILT_IN_CALLOC
903e4b17023SJohn Marino 		      || DECL_FUNCTION_CODE (callee) == BUILT_IN_FREE
904e4b17023SJohn Marino 		      || DECL_FUNCTION_CODE (callee) == BUILT_IN_VA_END
905e4b17023SJohn Marino 		      || DECL_FUNCTION_CODE (callee) == BUILT_IN_ALLOCA
906e4b17023SJohn Marino 		      || (DECL_FUNCTION_CODE (callee)
907e4b17023SJohn Marino 			  == BUILT_IN_ALLOCA_WITH_ALIGN)
908e4b17023SJohn Marino 		      || DECL_FUNCTION_CODE (callee) == BUILT_IN_STACK_SAVE
909e4b17023SJohn Marino 		      || DECL_FUNCTION_CODE (callee) == BUILT_IN_STACK_RESTORE
910e4b17023SJohn Marino 		      || DECL_FUNCTION_CODE (callee) == BUILT_IN_ASSUME_ALIGNED))
911e4b17023SJohn Marino 		continue;
912e4b17023SJohn Marino 
913e4b17023SJohn Marino 	      /* Calls implicitly load from memory, their arguments
914e4b17023SJohn Marino 	         in addition may explicitly perform memory loads.  */
915e4b17023SJohn Marino 	      mark_all_reaching_defs_necessary (stmt);
916e4b17023SJohn Marino 	      for (i = 0; i < gimple_call_num_args (stmt); ++i)
917e4b17023SJohn Marino 		{
918e4b17023SJohn Marino 		  tree arg = gimple_call_arg (stmt, i);
919e4b17023SJohn Marino 		  if (TREE_CODE (arg) == SSA_NAME
920e4b17023SJohn Marino 		      || is_gimple_min_invariant (arg))
921e4b17023SJohn Marino 		    continue;
922e4b17023SJohn Marino 		  if (TREE_CODE (arg) == WITH_SIZE_EXPR)
923e4b17023SJohn Marino 		    arg = TREE_OPERAND (arg, 0);
924e4b17023SJohn Marino 		  if (!ref_may_be_aliased (arg))
925e4b17023SJohn Marino 		    mark_aliased_reaching_defs_necessary (stmt, arg);
926e4b17023SJohn Marino 		}
927e4b17023SJohn Marino 	    }
928e4b17023SJohn Marino 	  else if (gimple_assign_single_p (stmt))
929e4b17023SJohn Marino 	    {
930e4b17023SJohn Marino 	      tree rhs;
931e4b17023SJohn Marino 	      /* If this is a load mark things necessary.  */
932e4b17023SJohn Marino 	      rhs = gimple_assign_rhs1 (stmt);
933e4b17023SJohn Marino 	      if (TREE_CODE (rhs) != SSA_NAME
934e4b17023SJohn Marino 		  && !is_gimple_min_invariant (rhs)
935e4b17023SJohn Marino 		  && TREE_CODE (rhs) != CONSTRUCTOR)
936e4b17023SJohn Marino 		{
937e4b17023SJohn Marino 		  if (!ref_may_be_aliased (rhs))
938e4b17023SJohn Marino 		    mark_aliased_reaching_defs_necessary (stmt, rhs);
939e4b17023SJohn Marino 		  else
940e4b17023SJohn Marino 		    mark_all_reaching_defs_necessary (stmt);
941e4b17023SJohn Marino 		}
942e4b17023SJohn Marino 	    }
943e4b17023SJohn Marino 	  else if (gimple_code (stmt) == GIMPLE_RETURN)
944e4b17023SJohn Marino 	    {
945e4b17023SJohn Marino 	      tree rhs = gimple_return_retval (stmt);
946e4b17023SJohn Marino 	      /* A return statement may perform a load.  */
947e4b17023SJohn Marino 	      if (rhs
948e4b17023SJohn Marino 		  && TREE_CODE (rhs) != SSA_NAME
949e4b17023SJohn Marino 		  && !is_gimple_min_invariant (rhs)
950e4b17023SJohn Marino 		  && TREE_CODE (rhs) != CONSTRUCTOR)
951e4b17023SJohn Marino 		{
952e4b17023SJohn Marino 		  if (!ref_may_be_aliased (rhs))
953e4b17023SJohn Marino 		    mark_aliased_reaching_defs_necessary (stmt, rhs);
954e4b17023SJohn Marino 		  else
955e4b17023SJohn Marino 		    mark_all_reaching_defs_necessary (stmt);
956e4b17023SJohn Marino 		}
957e4b17023SJohn Marino 	    }
958e4b17023SJohn Marino 	  else if (gimple_code (stmt) == GIMPLE_ASM)
959e4b17023SJohn Marino 	    {
960e4b17023SJohn Marino 	      unsigned i;
961e4b17023SJohn Marino 	      mark_all_reaching_defs_necessary (stmt);
962e4b17023SJohn Marino 	      /* Inputs may perform loads.  */
963e4b17023SJohn Marino 	      for (i = 0; i < gimple_asm_ninputs (stmt); ++i)
964e4b17023SJohn Marino 		{
965e4b17023SJohn Marino 		  tree op = TREE_VALUE (gimple_asm_input_op (stmt, i));
966e4b17023SJohn Marino 		  if (TREE_CODE (op) != SSA_NAME
967e4b17023SJohn Marino 		      && !is_gimple_min_invariant (op)
968e4b17023SJohn Marino 		      && TREE_CODE (op) != CONSTRUCTOR
969e4b17023SJohn Marino 		      && !ref_may_be_aliased (op))
970e4b17023SJohn Marino 		    mark_aliased_reaching_defs_necessary (stmt, op);
971e4b17023SJohn Marino 		}
972e4b17023SJohn Marino 	    }
973e4b17023SJohn Marino 	  else if (gimple_code (stmt) == GIMPLE_TRANSACTION)
974e4b17023SJohn Marino 	    {
975e4b17023SJohn Marino 	      /* The beginning of a transaction is a memory barrier.  */
976e4b17023SJohn Marino 	      /* ??? If we were really cool, we'd only be a barrier
977e4b17023SJohn Marino 		 for the memories touched within the transaction.  */
978e4b17023SJohn Marino 	      mark_all_reaching_defs_necessary (stmt);
979e4b17023SJohn Marino 	    }
980e4b17023SJohn Marino 	  else
981e4b17023SJohn Marino 	    gcc_unreachable ();
982e4b17023SJohn Marino 
983e4b17023SJohn Marino 	  /* If we over-used our alias oracle budget drop to simple
984e4b17023SJohn Marino 	     mode.  The cost metric allows quadratic behavior
985e4b17023SJohn Marino 	     (number of uses times number of may-defs queries) up to
986e4b17023SJohn Marino 	     a constant maximal number of queries and after that falls back to
987e4b17023SJohn Marino 	     super-linear complexity.  */
988e4b17023SJohn Marino 	  if (/* Constant but quadratic for small functions.  */
989e4b17023SJohn Marino 	      total_chain > 128 * 128
990e4b17023SJohn Marino 	      /* Linear in the number of may-defs.  */
991e4b17023SJohn Marino 	      && total_chain > 32 * longest_chain
992e4b17023SJohn Marino 	      /* Linear in the number of uses.  */
993e4b17023SJohn Marino 	      && total_chain > nr_walks * 32)
994e4b17023SJohn Marino 	    {
995e4b17023SJohn Marino 	      chain_ovfl = true;
996e4b17023SJohn Marino 	      if (visited)
997e4b17023SJohn Marino 		bitmap_clear (visited);
998e4b17023SJohn Marino 	    }
999e4b17023SJohn Marino 	}
1000e4b17023SJohn Marino     }
1001e4b17023SJohn Marino }
1002e4b17023SJohn Marino 
1003e4b17023SJohn Marino /* Replace all uses of NAME by underlying variable and mark it
1004e4b17023SJohn Marino    for renaming.  */
1005e4b17023SJohn Marino 
1006e4b17023SJohn Marino void
mark_virtual_operand_for_renaming(tree name)1007e4b17023SJohn Marino mark_virtual_operand_for_renaming (tree name)
1008e4b17023SJohn Marino {
1009e4b17023SJohn Marino   bool used = false;
1010e4b17023SJohn Marino   imm_use_iterator iter;
1011e4b17023SJohn Marino   use_operand_p use_p;
1012e4b17023SJohn Marino   gimple stmt;
1013e4b17023SJohn Marino   tree name_var;
1014e4b17023SJohn Marino 
1015e4b17023SJohn Marino   name_var = SSA_NAME_VAR (name);
1016e4b17023SJohn Marino   FOR_EACH_IMM_USE_STMT (stmt, iter, name)
1017e4b17023SJohn Marino     {
1018e4b17023SJohn Marino       FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1019e4b17023SJohn Marino         SET_USE (use_p, name_var);
1020e4b17023SJohn Marino       update_stmt (stmt);
1021e4b17023SJohn Marino       used = true;
1022e4b17023SJohn Marino     }
1023e4b17023SJohn Marino   if (used)
1024e4b17023SJohn Marino     mark_sym_for_renaming (name_var);
1025e4b17023SJohn Marino }
1026e4b17023SJohn Marino 
1027e4b17023SJohn Marino /* Replace all uses of result of PHI by underlying variable and mark it
1028e4b17023SJohn Marino    for renaming.  */
1029e4b17023SJohn Marino 
1030e4b17023SJohn Marino void
mark_virtual_phi_result_for_renaming(gimple phi)1031e4b17023SJohn Marino mark_virtual_phi_result_for_renaming (gimple phi)
1032e4b17023SJohn Marino {
1033e4b17023SJohn Marino   if (dump_file && (dump_flags & TDF_DETAILS))
1034e4b17023SJohn Marino     {
1035e4b17023SJohn Marino       fprintf (dump_file, "Marking result for renaming : ");
1036e4b17023SJohn Marino       print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
1037e4b17023SJohn Marino       fprintf (dump_file, "\n");
1038e4b17023SJohn Marino     }
1039e4b17023SJohn Marino 
1040e4b17023SJohn Marino   mark_virtual_operand_for_renaming (gimple_phi_result (phi));
1041e4b17023SJohn Marino }
1042e4b17023SJohn Marino 
1043e4b17023SJohn Marino 
1044e4b17023SJohn Marino /* Remove dead PHI nodes from block BB.  */
1045e4b17023SJohn Marino 
1046e4b17023SJohn Marino static bool
remove_dead_phis(basic_block bb)1047e4b17023SJohn Marino remove_dead_phis (basic_block bb)
1048e4b17023SJohn Marino {
1049e4b17023SJohn Marino   bool something_changed = false;
1050e4b17023SJohn Marino   gimple_seq phis;
1051e4b17023SJohn Marino   gimple phi;
1052e4b17023SJohn Marino   gimple_stmt_iterator gsi;
1053e4b17023SJohn Marino   phis = phi_nodes (bb);
1054e4b17023SJohn Marino 
1055e4b17023SJohn Marino   for (gsi = gsi_start (phis); !gsi_end_p (gsi);)
1056e4b17023SJohn Marino     {
1057e4b17023SJohn Marino       stats.total_phis++;
1058e4b17023SJohn Marino       phi = gsi_stmt (gsi);
1059e4b17023SJohn Marino 
1060e4b17023SJohn Marino       /* We do not track necessity of virtual PHI nodes.  Instead do
1061e4b17023SJohn Marino          very simple dead PHI removal here.  */
1062e4b17023SJohn Marino       if (!is_gimple_reg (gimple_phi_result (phi)))
1063e4b17023SJohn Marino 	{
1064e4b17023SJohn Marino 	  /* Virtual PHI nodes with one or identical arguments
1065e4b17023SJohn Marino 	     can be removed.  */
1066e4b17023SJohn Marino 	  if (degenerate_phi_p (phi))
1067e4b17023SJohn Marino 	    {
1068e4b17023SJohn Marino 	      tree vdef = gimple_phi_result (phi);
1069e4b17023SJohn Marino 	      tree vuse = gimple_phi_arg_def (phi, 0);
1070e4b17023SJohn Marino 
1071e4b17023SJohn Marino 	      use_operand_p use_p;
1072e4b17023SJohn Marino 	      imm_use_iterator iter;
1073e4b17023SJohn Marino 	      gimple use_stmt;
1074e4b17023SJohn Marino 	      FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
1075e4b17023SJohn Marino 		FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1076e4b17023SJohn Marino 		  SET_USE (use_p, vuse);
1077e4b17023SJohn Marino 	      if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vdef)
1078e4b17023SJohn Marino 	          && TREE_CODE (vuse) == SSA_NAME)
1079e4b17023SJohn Marino 		SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vuse) = 1;
1080e4b17023SJohn Marino 	    }
1081e4b17023SJohn Marino 	  else
1082e4b17023SJohn Marino 	    gimple_set_plf (phi, STMT_NECESSARY, true);
1083e4b17023SJohn Marino 	}
1084e4b17023SJohn Marino 
1085e4b17023SJohn Marino       if (!gimple_plf (phi, STMT_NECESSARY))
1086e4b17023SJohn Marino 	{
1087e4b17023SJohn Marino 	  something_changed = true;
1088e4b17023SJohn Marino 	  if (dump_file && (dump_flags & TDF_DETAILS))
1089e4b17023SJohn Marino 	    {
1090e4b17023SJohn Marino 	      fprintf (dump_file, "Deleting : ");
1091e4b17023SJohn Marino 	      print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
1092e4b17023SJohn Marino 	      fprintf (dump_file, "\n");
1093e4b17023SJohn Marino 	    }
1094e4b17023SJohn Marino 
1095e4b17023SJohn Marino 	  remove_phi_node (&gsi, true);
1096e4b17023SJohn Marino 	  stats.removed_phis++;
1097e4b17023SJohn Marino 	  continue;
1098e4b17023SJohn Marino 	}
1099e4b17023SJohn Marino 
1100e4b17023SJohn Marino       gsi_next (&gsi);
1101e4b17023SJohn Marino     }
1102e4b17023SJohn Marino   return something_changed;
1103e4b17023SJohn Marino }
1104e4b17023SJohn Marino 
1105e4b17023SJohn Marino /* Forward edge E to respective POST_DOM_BB and update PHIs.  */
1106e4b17023SJohn Marino 
1107e4b17023SJohn Marino static edge
forward_edge_to_pdom(edge e,basic_block post_dom_bb)1108e4b17023SJohn Marino forward_edge_to_pdom (edge e, basic_block post_dom_bb)
1109e4b17023SJohn Marino {
1110e4b17023SJohn Marino   gimple_stmt_iterator gsi;
1111e4b17023SJohn Marino   edge e2 = NULL;
1112e4b17023SJohn Marino   edge_iterator ei;
1113e4b17023SJohn Marino 
1114e4b17023SJohn Marino   if (dump_file && (dump_flags & TDF_DETAILS))
1115e4b17023SJohn Marino     fprintf (dump_file, "Redirecting edge %i->%i to %i\n", e->src->index,
1116e4b17023SJohn Marino 	     e->dest->index, post_dom_bb->index);
1117e4b17023SJohn Marino 
1118e4b17023SJohn Marino   e2 = redirect_edge_and_branch (e, post_dom_bb);
1119e4b17023SJohn Marino   cfg_altered = true;
1120e4b17023SJohn Marino 
1121e4b17023SJohn Marino   /* If edge was already around, no updating is neccesary.  */
1122e4b17023SJohn Marino   if (e2 != e)
1123e4b17023SJohn Marino     return e2;
1124e4b17023SJohn Marino 
1125e4b17023SJohn Marino   if (!gimple_seq_empty_p (phi_nodes (post_dom_bb)))
1126e4b17023SJohn Marino     {
1127e4b17023SJohn Marino       /* We are sure that for every live PHI we are seeing control dependent BB.
1128e4b17023SJohn Marino          This means that we can pick any edge to duplicate PHI args from.  */
1129e4b17023SJohn Marino       FOR_EACH_EDGE (e2, ei, post_dom_bb->preds)
1130e4b17023SJohn Marino 	if (e2 != e)
1131e4b17023SJohn Marino 	  break;
1132e4b17023SJohn Marino       for (gsi = gsi_start_phis (post_dom_bb); !gsi_end_p (gsi);)
1133e4b17023SJohn Marino 	{
1134e4b17023SJohn Marino 	  gimple phi = gsi_stmt (gsi);
1135e4b17023SJohn Marino 	  tree op;
1136e4b17023SJohn Marino 	  source_location locus;
1137e4b17023SJohn Marino 
1138e4b17023SJohn Marino 	  /* PHIs for virtuals have no control dependency relation on them.
1139e4b17023SJohn Marino 	     We are lost here and must force renaming of the symbol.  */
1140e4b17023SJohn Marino 	  if (!is_gimple_reg (gimple_phi_result (phi)))
1141e4b17023SJohn Marino 	    {
1142e4b17023SJohn Marino 	      mark_virtual_phi_result_for_renaming (phi);
1143e4b17023SJohn Marino 	      remove_phi_node (&gsi, true);
1144e4b17023SJohn Marino 	      continue;
1145e4b17023SJohn Marino 	    }
1146e4b17023SJohn Marino 
1147e4b17023SJohn Marino 	  /* Dead PHI do not imply control dependency.  */
1148e4b17023SJohn Marino           if (!gimple_plf (phi, STMT_NECESSARY))
1149e4b17023SJohn Marino 	    {
1150e4b17023SJohn Marino 	      gsi_next (&gsi);
1151e4b17023SJohn Marino 	      continue;
1152e4b17023SJohn Marino 	    }
1153e4b17023SJohn Marino 
1154e4b17023SJohn Marino 	  op = gimple_phi_arg_def (phi, e2->dest_idx);
1155e4b17023SJohn Marino 	  locus = gimple_phi_arg_location (phi, e2->dest_idx);
1156e4b17023SJohn Marino 	  add_phi_arg (phi, op, e, locus);
1157e4b17023SJohn Marino 	  /* The resulting PHI if not dead can only be degenerate.  */
1158e4b17023SJohn Marino 	  gcc_assert (degenerate_phi_p (phi));
1159e4b17023SJohn Marino 	  gsi_next (&gsi);
1160e4b17023SJohn Marino 	}
1161e4b17023SJohn Marino     }
1162e4b17023SJohn Marino   return e;
1163e4b17023SJohn Marino }
1164e4b17023SJohn Marino 
1165e4b17023SJohn Marino /* Remove dead statement pointed to by iterator I.  Receives the basic block BB
1166e4b17023SJohn Marino    containing I so that we don't have to look it up.  */
1167e4b17023SJohn Marino 
1168e4b17023SJohn Marino static void
remove_dead_stmt(gimple_stmt_iterator * i,basic_block bb)1169e4b17023SJohn Marino remove_dead_stmt (gimple_stmt_iterator *i, basic_block bb)
1170e4b17023SJohn Marino {
1171e4b17023SJohn Marino   gimple stmt = gsi_stmt (*i);
1172e4b17023SJohn Marino 
1173e4b17023SJohn Marino   if (dump_file && (dump_flags & TDF_DETAILS))
1174e4b17023SJohn Marino     {
1175e4b17023SJohn Marino       fprintf (dump_file, "Deleting : ");
1176e4b17023SJohn Marino       print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1177e4b17023SJohn Marino       fprintf (dump_file, "\n");
1178e4b17023SJohn Marino     }
1179e4b17023SJohn Marino 
1180e4b17023SJohn Marino   stats.removed++;
1181e4b17023SJohn Marino 
1182e4b17023SJohn Marino   /* If we have determined that a conditional branch statement contributes
1183e4b17023SJohn Marino      nothing to the program, then we not only remove it, but we also change
1184e4b17023SJohn Marino      the flow graph so that the current block will simply fall-thru to its
1185e4b17023SJohn Marino      immediate post-dominator.  The blocks we are circumventing will be
1186e4b17023SJohn Marino      removed by cleanup_tree_cfg if this change in the flow graph makes them
1187e4b17023SJohn Marino      unreachable.  */
1188e4b17023SJohn Marino   if (is_ctrl_stmt (stmt))
1189e4b17023SJohn Marino     {
1190e4b17023SJohn Marino       basic_block post_dom_bb;
1191e4b17023SJohn Marino       edge e, e2;
1192e4b17023SJohn Marino       edge_iterator ei;
1193e4b17023SJohn Marino 
1194e4b17023SJohn Marino       post_dom_bb = get_immediate_dominator (CDI_POST_DOMINATORS, bb);
1195e4b17023SJohn Marino 
1196e4b17023SJohn Marino       e = find_edge (bb, post_dom_bb);
1197e4b17023SJohn Marino 
1198e4b17023SJohn Marino       /* If edge is already there, try to use it.  This avoids need to update
1199e4b17023SJohn Marino          PHI nodes.  Also watch for cases where post dominator does not exists
1200e4b17023SJohn Marino 	 or is exit block.  These can happen for infinite loops as we create
1201e4b17023SJohn Marino 	 fake edges in the dominator tree.  */
1202e4b17023SJohn Marino       if (e)
1203e4b17023SJohn Marino         ;
1204e4b17023SJohn Marino       else if (! post_dom_bb || post_dom_bb == EXIT_BLOCK_PTR)
1205e4b17023SJohn Marino 	e = EDGE_SUCC (bb, 0);
1206e4b17023SJohn Marino       else
1207e4b17023SJohn Marino         e = forward_edge_to_pdom (EDGE_SUCC (bb, 0), post_dom_bb);
1208e4b17023SJohn Marino       gcc_assert (e);
1209e4b17023SJohn Marino       e->probability = REG_BR_PROB_BASE;
1210e4b17023SJohn Marino       e->count = bb->count;
1211e4b17023SJohn Marino 
1212e4b17023SJohn Marino       /* The edge is no longer associated with a conditional, so it does
1213e4b17023SJohn Marino 	 not have TRUE/FALSE flags.  */
1214e4b17023SJohn Marino       e->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
1215e4b17023SJohn Marino 
1216e4b17023SJohn Marino       /* The lone outgoing edge from BB will be a fallthru edge.  */
1217e4b17023SJohn Marino       e->flags |= EDGE_FALLTHRU;
1218e4b17023SJohn Marino 
1219e4b17023SJohn Marino       /* Remove the remaining outgoing edges.  */
1220e4b17023SJohn Marino       for (ei = ei_start (bb->succs); (e2 = ei_safe_edge (ei)); )
1221e4b17023SJohn Marino 	if (e != e2)
1222e4b17023SJohn Marino 	  {
1223e4b17023SJohn Marino 	    cfg_altered = true;
1224e4b17023SJohn Marino             remove_edge (e2);
1225e4b17023SJohn Marino 	  }
1226e4b17023SJohn Marino 	else
1227e4b17023SJohn Marino 	  ei_next (&ei);
1228e4b17023SJohn Marino     }
1229e4b17023SJohn Marino 
1230e4b17023SJohn Marino   /* If this is a store into a variable that is being optimized away,
1231e4b17023SJohn Marino      add a debug bind stmt if possible.  */
1232e4b17023SJohn Marino   if (MAY_HAVE_DEBUG_STMTS
1233e4b17023SJohn Marino       && gimple_assign_single_p (stmt)
1234e4b17023SJohn Marino       && is_gimple_val (gimple_assign_rhs1 (stmt)))
1235e4b17023SJohn Marino     {
1236e4b17023SJohn Marino       tree lhs = gimple_assign_lhs (stmt);
1237e4b17023SJohn Marino       if ((TREE_CODE (lhs) == VAR_DECL || TREE_CODE (lhs) == PARM_DECL)
1238e4b17023SJohn Marino 	  && !DECL_IGNORED_P (lhs)
1239e4b17023SJohn Marino 	  && is_gimple_reg_type (TREE_TYPE (lhs))
1240e4b17023SJohn Marino 	  && !is_global_var (lhs)
1241e4b17023SJohn Marino 	  && !DECL_HAS_VALUE_EXPR_P (lhs))
1242e4b17023SJohn Marino 	{
1243e4b17023SJohn Marino 	  tree rhs = gimple_assign_rhs1 (stmt);
1244e4b17023SJohn Marino 	  gimple note
1245e4b17023SJohn Marino 	    = gimple_build_debug_bind (lhs, unshare_expr (rhs), stmt);
1246e4b17023SJohn Marino 	  gsi_insert_after (i, note, GSI_SAME_STMT);
1247e4b17023SJohn Marino 	}
1248e4b17023SJohn Marino     }
1249e4b17023SJohn Marino 
1250e4b17023SJohn Marino   unlink_stmt_vdef (stmt);
1251e4b17023SJohn Marino   gsi_remove (i, true);
1252e4b17023SJohn Marino   release_defs (stmt);
1253e4b17023SJohn Marino }
1254e4b17023SJohn Marino 
1255e4b17023SJohn Marino /* Eliminate unnecessary statements. Any instruction not marked as necessary
1256e4b17023SJohn Marino    contributes nothing to the program, and can be deleted.  */
1257e4b17023SJohn Marino 
1258e4b17023SJohn Marino static bool
eliminate_unnecessary_stmts(void)1259e4b17023SJohn Marino eliminate_unnecessary_stmts (void)
1260e4b17023SJohn Marino {
1261e4b17023SJohn Marino   bool something_changed = false;
1262e4b17023SJohn Marino   basic_block bb;
1263e4b17023SJohn Marino   gimple_stmt_iterator gsi, psi;
1264e4b17023SJohn Marino   gimple stmt;
1265e4b17023SJohn Marino   tree call;
1266e4b17023SJohn Marino   VEC (basic_block, heap) *h;
1267e4b17023SJohn Marino 
1268e4b17023SJohn Marino   if (dump_file && (dump_flags & TDF_DETAILS))
1269e4b17023SJohn Marino     fprintf (dump_file, "\nEliminating unnecessary statements:\n");
1270e4b17023SJohn Marino 
1271e4b17023SJohn Marino   clear_special_calls ();
1272e4b17023SJohn Marino 
1273e4b17023SJohn Marino   /* Walking basic blocks and statements in reverse order avoids
1274e4b17023SJohn Marino      releasing SSA names before any other DEFs that refer to them are
1275e4b17023SJohn Marino      released.  This helps avoid loss of debug information, as we get
1276e4b17023SJohn Marino      a chance to propagate all RHSs of removed SSAs into debug uses,
1277e4b17023SJohn Marino      rather than only the latest ones.  E.g., consider:
1278e4b17023SJohn Marino 
1279e4b17023SJohn Marino      x_3 = y_1 + z_2;
1280e4b17023SJohn Marino      a_5 = x_3 - b_4;
1281e4b17023SJohn Marino      # DEBUG a => a_5
1282e4b17023SJohn Marino 
1283e4b17023SJohn Marino      If we were to release x_3 before a_5, when we reached a_5 and
1284e4b17023SJohn Marino      tried to substitute it into the debug stmt, we'd see x_3 there,
1285e4b17023SJohn Marino      but x_3's DEF, type, etc would have already been disconnected.
1286e4b17023SJohn Marino      By going backwards, the debug stmt first changes to:
1287e4b17023SJohn Marino 
1288e4b17023SJohn Marino      # DEBUG a => x_3 - b_4
1289e4b17023SJohn Marino 
1290e4b17023SJohn Marino      and then to:
1291e4b17023SJohn Marino 
1292e4b17023SJohn Marino      # DEBUG a => y_1 + z_2 - b_4
1293e4b17023SJohn Marino 
1294e4b17023SJohn Marino      as desired.  */
1295e4b17023SJohn Marino   gcc_assert (dom_info_available_p (CDI_DOMINATORS));
1296e4b17023SJohn Marino   h = get_all_dominated_blocks (CDI_DOMINATORS, single_succ (ENTRY_BLOCK_PTR));
1297e4b17023SJohn Marino 
1298e4b17023SJohn Marino   while (VEC_length (basic_block, h))
1299e4b17023SJohn Marino     {
1300e4b17023SJohn Marino       bb = VEC_pop (basic_block, h);
1301e4b17023SJohn Marino 
1302e4b17023SJohn Marino       /* Remove dead statements.  */
1303e4b17023SJohn Marino       for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi = psi)
1304e4b17023SJohn Marino 	{
1305e4b17023SJohn Marino 	  stmt = gsi_stmt (gsi);
1306e4b17023SJohn Marino 
1307e4b17023SJohn Marino 	  psi = gsi;
1308e4b17023SJohn Marino 	  gsi_prev (&psi);
1309e4b17023SJohn Marino 
1310e4b17023SJohn Marino 	  stats.total++;
1311e4b17023SJohn Marino 
1312e4b17023SJohn Marino 	  /* We can mark a call to free as not necessary if the
1313*95d28233SJohn Marino 	     defining statement of its argument is not necessary
1314*95d28233SJohn Marino 	     (and thus is getting removed).  */
1315*95d28233SJohn Marino 	  if (gimple_plf (stmt, STMT_NECESSARY)
1316*95d28233SJohn Marino 	      && gimple_call_builtin_p (stmt, BUILT_IN_FREE))
1317e4b17023SJohn Marino 	    {
1318e4b17023SJohn Marino 	      tree ptr = gimple_call_arg (stmt, 0);
1319*95d28233SJohn Marino 	      if (TREE_CODE (ptr) == SSA_NAME)
1320*95d28233SJohn Marino 		{
1321*95d28233SJohn Marino 		  gimple def_stmt = SSA_NAME_DEF_STMT (ptr);
1322*95d28233SJohn Marino 		  if (!gimple_nop_p (def_stmt)
1323*95d28233SJohn Marino 		      && !gimple_plf (def_stmt, STMT_NECESSARY))
1324e4b17023SJohn Marino 		    gimple_set_plf (stmt, STMT_NECESSARY, false);
1325e4b17023SJohn Marino 		}
1326*95d28233SJohn Marino 	    }
1327e4b17023SJohn Marino 
1328e4b17023SJohn Marino 	  /* If GSI is not necessary then remove it.  */
1329e4b17023SJohn Marino 	  if (!gimple_plf (stmt, STMT_NECESSARY))
1330e4b17023SJohn Marino 	    {
1331e4b17023SJohn Marino 	      if (!is_gimple_debug (stmt))
1332e4b17023SJohn Marino 		something_changed = true;
1333e4b17023SJohn Marino 	      remove_dead_stmt (&gsi, bb);
1334e4b17023SJohn Marino 	    }
1335e4b17023SJohn Marino 	  else if (is_gimple_call (stmt))
1336e4b17023SJohn Marino 	    {
1337e4b17023SJohn Marino 	      tree name = gimple_call_lhs (stmt);
1338e4b17023SJohn Marino 
1339e4b17023SJohn Marino 	      notice_special_calls (stmt);
1340e4b17023SJohn Marino 
1341e4b17023SJohn Marino 	      /* When LHS of var = call (); is dead, simplify it into
1342e4b17023SJohn Marino 		 call (); saving one operand.  */
1343e4b17023SJohn Marino 	      if (name
1344e4b17023SJohn Marino 		  && TREE_CODE (name) == SSA_NAME
1345e4b17023SJohn Marino 		  && !TEST_BIT (processed, SSA_NAME_VERSION (name))
1346e4b17023SJohn Marino 		  /* Avoid doing so for allocation calls which we
1347e4b17023SJohn Marino 		     did not mark as necessary, it will confuse the
1348e4b17023SJohn Marino 		     special logic we apply to malloc/free pair removal.  */
1349e4b17023SJohn Marino 		  && (!(call = gimple_call_fndecl (stmt))
1350e4b17023SJohn Marino 		      || DECL_BUILT_IN_CLASS (call) != BUILT_IN_NORMAL
1351e4b17023SJohn Marino 		      || (DECL_FUNCTION_CODE (call) != BUILT_IN_MALLOC
1352e4b17023SJohn Marino 			  && DECL_FUNCTION_CODE (call) != BUILT_IN_CALLOC
1353e4b17023SJohn Marino 			  && DECL_FUNCTION_CODE (call) != BUILT_IN_ALLOCA
1354e4b17023SJohn Marino 			  && (DECL_FUNCTION_CODE (call)
1355e4b17023SJohn Marino 			      != BUILT_IN_ALLOCA_WITH_ALIGN))))
1356e4b17023SJohn Marino 		{
1357e4b17023SJohn Marino 		  something_changed = true;
1358e4b17023SJohn Marino 		  if (dump_file && (dump_flags & TDF_DETAILS))
1359e4b17023SJohn Marino 		    {
1360e4b17023SJohn Marino 		      fprintf (dump_file, "Deleting LHS of call: ");
1361e4b17023SJohn Marino 		      print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1362e4b17023SJohn Marino 		      fprintf (dump_file, "\n");
1363e4b17023SJohn Marino 		    }
1364e4b17023SJohn Marino 
1365e4b17023SJohn Marino 		  gimple_call_set_lhs (stmt, NULL_TREE);
1366e4b17023SJohn Marino 		  maybe_clean_or_replace_eh_stmt (stmt, stmt);
1367e4b17023SJohn Marino 		  update_stmt (stmt);
1368e4b17023SJohn Marino 		  release_ssa_name (name);
1369e4b17023SJohn Marino 		}
1370e4b17023SJohn Marino 	    }
1371e4b17023SJohn Marino 	}
1372e4b17023SJohn Marino     }
1373e4b17023SJohn Marino 
1374e4b17023SJohn Marino   VEC_free (basic_block, heap, h);
1375e4b17023SJohn Marino 
1376e4b17023SJohn Marino   /* Since we don't track liveness of virtual PHI nodes, it is possible that we
1377e4b17023SJohn Marino      rendered some PHI nodes unreachable while they are still in use.
1378e4b17023SJohn Marino      Mark them for renaming.  */
1379e4b17023SJohn Marino   if (cfg_altered)
1380e4b17023SJohn Marino     {
1381e4b17023SJohn Marino       basic_block prev_bb;
1382e4b17023SJohn Marino 
1383e4b17023SJohn Marino       find_unreachable_blocks ();
1384e4b17023SJohn Marino 
1385e4b17023SJohn Marino       /* Delete all unreachable basic blocks in reverse dominator order.  */
1386e4b17023SJohn Marino       for (bb = EXIT_BLOCK_PTR->prev_bb; bb != ENTRY_BLOCK_PTR; bb = prev_bb)
1387e4b17023SJohn Marino 	{
1388e4b17023SJohn Marino 	  prev_bb = bb->prev_bb;
1389e4b17023SJohn Marino 
1390e4b17023SJohn Marino 	  if (!TEST_BIT (bb_contains_live_stmts, bb->index)
1391e4b17023SJohn Marino 	      || !(bb->flags & BB_REACHABLE))
1392e4b17023SJohn Marino 	    {
1393e4b17023SJohn Marino 	      for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1394e4b17023SJohn Marino 		if (!is_gimple_reg (gimple_phi_result (gsi_stmt (gsi))))
1395e4b17023SJohn Marino 		  {
1396e4b17023SJohn Marino 		    bool found = false;
1397e4b17023SJohn Marino 		    imm_use_iterator iter;
1398e4b17023SJohn Marino 
1399e4b17023SJohn Marino 		    FOR_EACH_IMM_USE_STMT (stmt, iter, gimple_phi_result (gsi_stmt (gsi)))
1400e4b17023SJohn Marino 		      {
1401e4b17023SJohn Marino 			if (!(gimple_bb (stmt)->flags & BB_REACHABLE))
1402e4b17023SJohn Marino 			  continue;
1403e4b17023SJohn Marino 			if (gimple_code (stmt) == GIMPLE_PHI
1404e4b17023SJohn Marino 			    || gimple_plf (stmt, STMT_NECESSARY))
1405e4b17023SJohn Marino 			  {
1406e4b17023SJohn Marino 			    found = true;
1407e4b17023SJohn Marino 			    BREAK_FROM_IMM_USE_STMT (iter);
1408e4b17023SJohn Marino 			  }
1409e4b17023SJohn Marino 		      }
1410e4b17023SJohn Marino 		    if (found)
1411e4b17023SJohn Marino 		      mark_virtual_phi_result_for_renaming (gsi_stmt (gsi));
1412e4b17023SJohn Marino 		  }
1413e4b17023SJohn Marino 
1414e4b17023SJohn Marino 	      if (!(bb->flags & BB_REACHABLE))
1415e4b17023SJohn Marino 		{
1416e4b17023SJohn Marino 		  /* Speed up the removal of blocks that don't
1417e4b17023SJohn Marino 		     dominate others.  Walking backwards, this should
1418e4b17023SJohn Marino 		     be the common case.  ??? Do we need to recompute
1419e4b17023SJohn Marino 		     dominators because of cfg_altered?  */
1420e4b17023SJohn Marino 		  if (!MAY_HAVE_DEBUG_STMTS
1421e4b17023SJohn Marino 		      || !first_dom_son (CDI_DOMINATORS, bb))
1422e4b17023SJohn Marino 		    delete_basic_block (bb);
1423e4b17023SJohn Marino 		  else
1424e4b17023SJohn Marino 		    {
1425e4b17023SJohn Marino 		      h = get_all_dominated_blocks (CDI_DOMINATORS, bb);
1426e4b17023SJohn Marino 
1427e4b17023SJohn Marino 		      while (VEC_length (basic_block, h))
1428e4b17023SJohn Marino 			{
1429e4b17023SJohn Marino 			  bb = VEC_pop (basic_block, h);
1430e4b17023SJohn Marino 			  prev_bb = bb->prev_bb;
1431e4b17023SJohn Marino 			  /* Rearrangements to the CFG may have failed
1432e4b17023SJohn Marino 			     to update the dominators tree, so that
1433e4b17023SJohn Marino 			     formerly-dominated blocks are now
1434e4b17023SJohn Marino 			     otherwise reachable.  */
1435e4b17023SJohn Marino 			  if (!!(bb->flags & BB_REACHABLE))
1436e4b17023SJohn Marino 			    continue;
1437e4b17023SJohn Marino 			  delete_basic_block (bb);
1438e4b17023SJohn Marino 			}
1439e4b17023SJohn Marino 
1440e4b17023SJohn Marino 		      VEC_free (basic_block, heap, h);
1441e4b17023SJohn Marino 		    }
1442e4b17023SJohn Marino 		}
1443e4b17023SJohn Marino 	    }
1444e4b17023SJohn Marino 	}
1445e4b17023SJohn Marino     }
1446e4b17023SJohn Marino   FOR_EACH_BB (bb)
1447e4b17023SJohn Marino     {
1448e4b17023SJohn Marino       /* Remove dead PHI nodes.  */
1449e4b17023SJohn Marino       something_changed |= remove_dead_phis (bb);
1450e4b17023SJohn Marino     }
1451e4b17023SJohn Marino 
1452e4b17023SJohn Marino   return something_changed;
1453e4b17023SJohn Marino }
1454e4b17023SJohn Marino 
1455e4b17023SJohn Marino 
1456e4b17023SJohn Marino /* Print out removed statement statistics.  */
1457e4b17023SJohn Marino 
1458e4b17023SJohn Marino static void
print_stats(void)1459e4b17023SJohn Marino print_stats (void)
1460e4b17023SJohn Marino {
1461e4b17023SJohn Marino   float percg;
1462e4b17023SJohn Marino 
1463e4b17023SJohn Marino   percg = ((float) stats.removed / (float) stats.total) * 100;
1464e4b17023SJohn Marino   fprintf (dump_file, "Removed %d of %d statements (%d%%)\n",
1465e4b17023SJohn Marino 	   stats.removed, stats.total, (int) percg);
1466e4b17023SJohn Marino 
1467e4b17023SJohn Marino   if (stats.total_phis == 0)
1468e4b17023SJohn Marino     percg = 0;
1469e4b17023SJohn Marino   else
1470e4b17023SJohn Marino     percg = ((float) stats.removed_phis / (float) stats.total_phis) * 100;
1471e4b17023SJohn Marino 
1472e4b17023SJohn Marino   fprintf (dump_file, "Removed %d of %d PHI nodes (%d%%)\n",
1473e4b17023SJohn Marino 	   stats.removed_phis, stats.total_phis, (int) percg);
1474e4b17023SJohn Marino }
1475e4b17023SJohn Marino 
1476e4b17023SJohn Marino /* Initialization for this pass.  Set up the used data structures.  */
1477e4b17023SJohn Marino 
1478e4b17023SJohn Marino static void
tree_dce_init(bool aggressive)1479e4b17023SJohn Marino tree_dce_init (bool aggressive)
1480e4b17023SJohn Marino {
1481e4b17023SJohn Marino   memset ((void *) &stats, 0, sizeof (stats));
1482e4b17023SJohn Marino 
1483e4b17023SJohn Marino   if (aggressive)
1484e4b17023SJohn Marino     {
1485e4b17023SJohn Marino       int i;
1486e4b17023SJohn Marino 
1487e4b17023SJohn Marino       control_dependence_map = XNEWVEC (bitmap, last_basic_block);
1488e4b17023SJohn Marino       for (i = 0; i < last_basic_block; ++i)
1489e4b17023SJohn Marino 	control_dependence_map[i] = BITMAP_ALLOC (NULL);
1490e4b17023SJohn Marino 
1491e4b17023SJohn Marino       last_stmt_necessary = sbitmap_alloc (last_basic_block);
1492e4b17023SJohn Marino       sbitmap_zero (last_stmt_necessary);
1493e4b17023SJohn Marino       bb_contains_live_stmts = sbitmap_alloc (last_basic_block);
1494e4b17023SJohn Marino       sbitmap_zero (bb_contains_live_stmts);
1495e4b17023SJohn Marino     }
1496e4b17023SJohn Marino 
1497e4b17023SJohn Marino   processed = sbitmap_alloc (num_ssa_names + 1);
1498e4b17023SJohn Marino   sbitmap_zero (processed);
1499e4b17023SJohn Marino 
1500e4b17023SJohn Marino   worklist = VEC_alloc (gimple, heap, 64);
1501e4b17023SJohn Marino   cfg_altered = false;
1502e4b17023SJohn Marino }
1503e4b17023SJohn Marino 
1504e4b17023SJohn Marino /* Cleanup after this pass.  */
1505e4b17023SJohn Marino 
1506e4b17023SJohn Marino static void
tree_dce_done(bool aggressive)1507e4b17023SJohn Marino tree_dce_done (bool aggressive)
1508e4b17023SJohn Marino {
1509e4b17023SJohn Marino   if (aggressive)
1510e4b17023SJohn Marino     {
1511e4b17023SJohn Marino       int i;
1512e4b17023SJohn Marino 
1513e4b17023SJohn Marino       for (i = 0; i < last_basic_block; ++i)
1514e4b17023SJohn Marino 	BITMAP_FREE (control_dependence_map[i]);
1515e4b17023SJohn Marino       free (control_dependence_map);
1516e4b17023SJohn Marino 
1517e4b17023SJohn Marino       sbitmap_free (visited_control_parents);
1518e4b17023SJohn Marino       sbitmap_free (last_stmt_necessary);
1519e4b17023SJohn Marino       sbitmap_free (bb_contains_live_stmts);
1520e4b17023SJohn Marino       bb_contains_live_stmts = NULL;
1521e4b17023SJohn Marino     }
1522e4b17023SJohn Marino 
1523e4b17023SJohn Marino   sbitmap_free (processed);
1524e4b17023SJohn Marino 
1525e4b17023SJohn Marino   VEC_free (gimple, heap, worklist);
1526e4b17023SJohn Marino }
1527e4b17023SJohn Marino 
1528e4b17023SJohn Marino /* Main routine to eliminate dead code.
1529e4b17023SJohn Marino 
1530e4b17023SJohn Marino    AGGRESSIVE controls the aggressiveness of the algorithm.
1531e4b17023SJohn Marino    In conservative mode, we ignore control dependence and simply declare
1532e4b17023SJohn Marino    all but the most trivially dead branches necessary.  This mode is fast.
1533e4b17023SJohn Marino    In aggressive mode, control dependences are taken into account, which
1534e4b17023SJohn Marino    results in more dead code elimination, but at the cost of some time.
1535e4b17023SJohn Marino 
1536e4b17023SJohn Marino    FIXME: Aggressive mode before PRE doesn't work currently because
1537e4b17023SJohn Marino 	  the dominance info is not invalidated after DCE1.  This is
1538e4b17023SJohn Marino 	  not an issue right now because we only run aggressive DCE
1539e4b17023SJohn Marino 	  as the last tree SSA pass, but keep this in mind when you
1540e4b17023SJohn Marino 	  start experimenting with pass ordering.  */
1541e4b17023SJohn Marino 
1542e4b17023SJohn Marino static unsigned int
perform_tree_ssa_dce(bool aggressive)1543e4b17023SJohn Marino perform_tree_ssa_dce (bool aggressive)
1544e4b17023SJohn Marino {
1545e4b17023SJohn Marino   struct edge_list *el = NULL;
1546e4b17023SJohn Marino   bool something_changed = 0;
1547e4b17023SJohn Marino 
1548e4b17023SJohn Marino   calculate_dominance_info (CDI_DOMINATORS);
1549e4b17023SJohn Marino 
1550e4b17023SJohn Marino   /* Preheaders are needed for SCEV to work.
1551e4b17023SJohn Marino      Simple lateches and recorded exits improve chances that loop will
1552e4b17023SJohn Marino      proved to be finite in testcases such as in loop-15.c and loop-24.c  */
1553e4b17023SJohn Marino   if (aggressive)
1554e4b17023SJohn Marino     loop_optimizer_init (LOOPS_NORMAL
1555e4b17023SJohn Marino 			 | LOOPS_HAVE_RECORDED_EXITS);
1556e4b17023SJohn Marino 
1557e4b17023SJohn Marino   tree_dce_init (aggressive);
1558e4b17023SJohn Marino 
1559e4b17023SJohn Marino   if (aggressive)
1560e4b17023SJohn Marino     {
1561e4b17023SJohn Marino       /* Compute control dependence.  */
1562e4b17023SJohn Marino       timevar_push (TV_CONTROL_DEPENDENCES);
1563e4b17023SJohn Marino       calculate_dominance_info (CDI_POST_DOMINATORS);
1564e4b17023SJohn Marino       el = create_edge_list ();
1565e4b17023SJohn Marino       find_all_control_dependences (el);
1566e4b17023SJohn Marino       timevar_pop (TV_CONTROL_DEPENDENCES);
1567e4b17023SJohn Marino 
1568e4b17023SJohn Marino       visited_control_parents = sbitmap_alloc (last_basic_block);
1569e4b17023SJohn Marino       sbitmap_zero (visited_control_parents);
1570e4b17023SJohn Marino 
1571e4b17023SJohn Marino       mark_dfs_back_edges ();
1572e4b17023SJohn Marino     }
1573e4b17023SJohn Marino 
1574e4b17023SJohn Marino   find_obviously_necessary_stmts (el);
1575e4b17023SJohn Marino 
1576e4b17023SJohn Marino   if (aggressive)
1577e4b17023SJohn Marino     loop_optimizer_finalize ();
1578e4b17023SJohn Marino 
1579e4b17023SJohn Marino   longest_chain = 0;
1580e4b17023SJohn Marino   total_chain = 0;
1581e4b17023SJohn Marino   nr_walks = 0;
1582e4b17023SJohn Marino   chain_ovfl = false;
1583e4b17023SJohn Marino   visited = BITMAP_ALLOC (NULL);
1584e4b17023SJohn Marino   propagate_necessity (el);
1585e4b17023SJohn Marino   BITMAP_FREE (visited);
1586e4b17023SJohn Marino 
1587e4b17023SJohn Marino   something_changed |= eliminate_unnecessary_stmts ();
1588e4b17023SJohn Marino   something_changed |= cfg_altered;
1589e4b17023SJohn Marino 
1590e4b17023SJohn Marino   /* We do not update postdominators, so free them unconditionally.  */
1591e4b17023SJohn Marino   free_dominance_info (CDI_POST_DOMINATORS);
1592e4b17023SJohn Marino 
1593e4b17023SJohn Marino   /* If we removed paths in the CFG, then we need to update
1594e4b17023SJohn Marino      dominators as well.  I haven't investigated the possibility
1595e4b17023SJohn Marino      of incrementally updating dominators.  */
1596e4b17023SJohn Marino   if (cfg_altered)
1597e4b17023SJohn Marino     free_dominance_info (CDI_DOMINATORS);
1598e4b17023SJohn Marino 
1599e4b17023SJohn Marino   statistics_counter_event (cfun, "Statements deleted", stats.removed);
1600e4b17023SJohn Marino   statistics_counter_event (cfun, "PHI nodes deleted", stats.removed_phis);
1601e4b17023SJohn Marino 
1602e4b17023SJohn Marino   /* Debugging dumps.  */
1603e4b17023SJohn Marino   if (dump_file && (dump_flags & (TDF_STATS|TDF_DETAILS)))
1604e4b17023SJohn Marino     print_stats ();
1605e4b17023SJohn Marino 
1606e4b17023SJohn Marino   tree_dce_done (aggressive);
1607e4b17023SJohn Marino 
1608e4b17023SJohn Marino   free_edge_list (el);
1609e4b17023SJohn Marino 
1610e4b17023SJohn Marino   if (something_changed)
1611e4b17023SJohn Marino     return (TODO_update_ssa | TODO_cleanup_cfg | TODO_ggc_collect
1612e4b17023SJohn Marino 	    | TODO_remove_unused_locals);
1613e4b17023SJohn Marino   else
1614e4b17023SJohn Marino     return 0;
1615e4b17023SJohn Marino }
1616e4b17023SJohn Marino 
1617e4b17023SJohn Marino /* Pass entry points.  */
1618e4b17023SJohn Marino static unsigned int
tree_ssa_dce(void)1619e4b17023SJohn Marino tree_ssa_dce (void)
1620e4b17023SJohn Marino {
1621e4b17023SJohn Marino   return perform_tree_ssa_dce (/*aggressive=*/false);
1622e4b17023SJohn Marino }
1623e4b17023SJohn Marino 
1624e4b17023SJohn Marino static unsigned int
tree_ssa_dce_loop(void)1625e4b17023SJohn Marino tree_ssa_dce_loop (void)
1626e4b17023SJohn Marino {
1627e4b17023SJohn Marino   unsigned int todo;
1628e4b17023SJohn Marino   todo = perform_tree_ssa_dce (/*aggressive=*/false);
1629e4b17023SJohn Marino   if (todo)
1630e4b17023SJohn Marino     {
1631e4b17023SJohn Marino       free_numbers_of_iterations_estimates ();
1632e4b17023SJohn Marino       scev_reset ();
1633e4b17023SJohn Marino     }
1634e4b17023SJohn Marino   return todo;
1635e4b17023SJohn Marino }
1636e4b17023SJohn Marino 
1637e4b17023SJohn Marino static unsigned int
tree_ssa_cd_dce(void)1638e4b17023SJohn Marino tree_ssa_cd_dce (void)
1639e4b17023SJohn Marino {
1640e4b17023SJohn Marino   return perform_tree_ssa_dce (/*aggressive=*/optimize >= 2);
1641e4b17023SJohn Marino }
1642e4b17023SJohn Marino 
1643e4b17023SJohn Marino static bool
gate_dce(void)1644e4b17023SJohn Marino gate_dce (void)
1645e4b17023SJohn Marino {
1646e4b17023SJohn Marino   return flag_tree_dce != 0;
1647e4b17023SJohn Marino }
1648e4b17023SJohn Marino 
1649e4b17023SJohn Marino struct gimple_opt_pass pass_dce =
1650e4b17023SJohn Marino {
1651e4b17023SJohn Marino  {
1652e4b17023SJohn Marino   GIMPLE_PASS,
1653e4b17023SJohn Marino   "dce",				/* name */
1654e4b17023SJohn Marino   gate_dce,				/* gate */
1655e4b17023SJohn Marino   tree_ssa_dce,				/* execute */
1656e4b17023SJohn Marino   NULL,					/* sub */
1657e4b17023SJohn Marino   NULL,					/* next */
1658e4b17023SJohn Marino   0,					/* static_pass_number */
1659e4b17023SJohn Marino   TV_TREE_DCE,				/* tv_id */
1660e4b17023SJohn Marino   PROP_cfg | PROP_ssa,			/* properties_required */
1661e4b17023SJohn Marino   0,					/* properties_provided */
1662e4b17023SJohn Marino   0,					/* properties_destroyed */
1663e4b17023SJohn Marino   0,					/* todo_flags_start */
1664e4b17023SJohn Marino   TODO_verify_ssa	                /* todo_flags_finish */
1665e4b17023SJohn Marino  }
1666e4b17023SJohn Marino };
1667e4b17023SJohn Marino 
1668e4b17023SJohn Marino struct gimple_opt_pass pass_dce_loop =
1669e4b17023SJohn Marino {
1670e4b17023SJohn Marino  {
1671e4b17023SJohn Marino   GIMPLE_PASS,
1672e4b17023SJohn Marino   "dceloop",				/* name */
1673e4b17023SJohn Marino   gate_dce,				/* gate */
1674e4b17023SJohn Marino   tree_ssa_dce_loop,			/* execute */
1675e4b17023SJohn Marino   NULL,					/* sub */
1676e4b17023SJohn Marino   NULL,					/* next */
1677e4b17023SJohn Marino   0,					/* static_pass_number */
1678e4b17023SJohn Marino   TV_TREE_DCE,				/* tv_id */
1679e4b17023SJohn Marino   PROP_cfg | PROP_ssa,			/* properties_required */
1680e4b17023SJohn Marino   0,					/* properties_provided */
1681e4b17023SJohn Marino   0,					/* properties_destroyed */
1682e4b17023SJohn Marino   0,					/* todo_flags_start */
1683e4b17023SJohn Marino   TODO_verify_ssa	                /* todo_flags_finish */
1684e4b17023SJohn Marino  }
1685e4b17023SJohn Marino };
1686e4b17023SJohn Marino 
1687e4b17023SJohn Marino struct gimple_opt_pass pass_cd_dce =
1688e4b17023SJohn Marino {
1689e4b17023SJohn Marino  {
1690e4b17023SJohn Marino   GIMPLE_PASS,
1691e4b17023SJohn Marino   "cddce",				/* name */
1692e4b17023SJohn Marino   gate_dce,				/* gate */
1693e4b17023SJohn Marino   tree_ssa_cd_dce,			/* execute */
1694e4b17023SJohn Marino   NULL,					/* sub */
1695e4b17023SJohn Marino   NULL,					/* next */
1696e4b17023SJohn Marino   0,					/* static_pass_number */
1697e4b17023SJohn Marino   TV_TREE_CD_DCE,			/* tv_id */
1698e4b17023SJohn Marino   PROP_cfg | PROP_ssa,			/* properties_required */
1699e4b17023SJohn Marino   0,					/* properties_provided */
1700e4b17023SJohn Marino   0,					/* properties_destroyed */
1701e4b17023SJohn Marino   0,					/* todo_flags_start */
1702e4b17023SJohn Marino   TODO_verify_ssa
1703e4b17023SJohn Marino   | TODO_verify_flow			/* todo_flags_finish */
1704e4b17023SJohn Marino  }
1705e4b17023SJohn Marino };
1706