xref: /dflybsd-src/contrib/gcc-4.7/gcc/tree-cfgcleanup.c (revision 04febcfb30580676d3e95f58a16c5137ee478b32)
1*e4b17023SJohn Marino /* CFG cleanup for trees.
2*e4b17023SJohn Marino    Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3*e4b17023SJohn Marino    Free Software Foundation, Inc.
4*e4b17023SJohn Marino 
5*e4b17023SJohn Marino This file is part of GCC.
6*e4b17023SJohn Marino 
7*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify
8*e4b17023SJohn Marino it under the terms of the GNU General Public License as published by
9*e4b17023SJohn Marino the Free Software Foundation; either version 3, or (at your option)
10*e4b17023SJohn Marino any later version.
11*e4b17023SJohn Marino 
12*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful,
13*e4b17023SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
14*e4b17023SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*e4b17023SJohn Marino GNU General Public License for more details.
16*e4b17023SJohn Marino 
17*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
18*e4b17023SJohn Marino along with GCC; see the file COPYING3.  If not see
19*e4b17023SJohn Marino <http://www.gnu.org/licenses/>.  */
20*e4b17023SJohn Marino 
21*e4b17023SJohn Marino #include "config.h"
22*e4b17023SJohn Marino #include "system.h"
23*e4b17023SJohn Marino #include "coretypes.h"
24*e4b17023SJohn Marino #include "tm.h"
25*e4b17023SJohn Marino #include "tree.h"
26*e4b17023SJohn Marino #include "tm_p.h"
27*e4b17023SJohn Marino #include "basic-block.h"
28*e4b17023SJohn Marino #include "output.h"
29*e4b17023SJohn Marino #include "diagnostic-core.h"
30*e4b17023SJohn Marino #include "flags.h"
31*e4b17023SJohn Marino #include "function.h"
32*e4b17023SJohn Marino #include "ggc.h"
33*e4b17023SJohn Marino #include "langhooks.h"
34*e4b17023SJohn Marino #include "tree-flow.h"
35*e4b17023SJohn Marino #include "timevar.h"
36*e4b17023SJohn Marino #include "tree-dump.h"
37*e4b17023SJohn Marino #include "tree-pass.h"
38*e4b17023SJohn Marino #include "except.h"
39*e4b17023SJohn Marino #include "cfgloop.h"
40*e4b17023SJohn Marino #include "cfglayout.h"
41*e4b17023SJohn Marino #include "hashtab.h"
42*e4b17023SJohn Marino #include "tree-ssa-propagate.h"
43*e4b17023SJohn Marino #include "tree-scalar-evolution.h"
44*e4b17023SJohn Marino 
45*e4b17023SJohn Marino /* The set of blocks in that at least one of the following changes happened:
46*e4b17023SJohn Marino    -- the statement at the end of the block was changed
47*e4b17023SJohn Marino    -- the block was newly created
48*e4b17023SJohn Marino    -- the set of the predecessors of the block changed
49*e4b17023SJohn Marino    -- the set of the successors of the block changed
50*e4b17023SJohn Marino    ??? Maybe we could track these changes separately, since they determine
51*e4b17023SJohn Marino        what cleanups it makes sense to try on the block.  */
52*e4b17023SJohn Marino bitmap cfgcleanup_altered_bbs;
53*e4b17023SJohn Marino 
54*e4b17023SJohn Marino /* Remove any fallthru edge from EV.  Return true if an edge was removed.  */
55*e4b17023SJohn Marino 
56*e4b17023SJohn Marino static bool
remove_fallthru_edge(VEC (edge,gc)* ev)57*e4b17023SJohn Marino remove_fallthru_edge (VEC(edge,gc) *ev)
58*e4b17023SJohn Marino {
59*e4b17023SJohn Marino   edge_iterator ei;
60*e4b17023SJohn Marino   edge e;
61*e4b17023SJohn Marino 
62*e4b17023SJohn Marino   FOR_EACH_EDGE (e, ei, ev)
63*e4b17023SJohn Marino     if ((e->flags & EDGE_FALLTHRU) != 0)
64*e4b17023SJohn Marino       {
65*e4b17023SJohn Marino 	remove_edge_and_dominated_blocks (e);
66*e4b17023SJohn Marino 	return true;
67*e4b17023SJohn Marino       }
68*e4b17023SJohn Marino   return false;
69*e4b17023SJohn Marino }
70*e4b17023SJohn Marino 
71*e4b17023SJohn Marino 
72*e4b17023SJohn Marino /* Disconnect an unreachable block in the control expression starting
73*e4b17023SJohn Marino    at block BB.  */
74*e4b17023SJohn Marino 
75*e4b17023SJohn Marino static bool
cleanup_control_expr_graph(basic_block bb,gimple_stmt_iterator gsi)76*e4b17023SJohn Marino cleanup_control_expr_graph (basic_block bb, gimple_stmt_iterator gsi)
77*e4b17023SJohn Marino {
78*e4b17023SJohn Marino   edge taken_edge;
79*e4b17023SJohn Marino   bool retval = false;
80*e4b17023SJohn Marino   gimple stmt = gsi_stmt (gsi);
81*e4b17023SJohn Marino   tree val;
82*e4b17023SJohn Marino 
83*e4b17023SJohn Marino   if (!single_succ_p (bb))
84*e4b17023SJohn Marino     {
85*e4b17023SJohn Marino       edge e;
86*e4b17023SJohn Marino       edge_iterator ei;
87*e4b17023SJohn Marino       bool warned;
88*e4b17023SJohn Marino       location_t loc;
89*e4b17023SJohn Marino 
90*e4b17023SJohn Marino       fold_defer_overflow_warnings ();
91*e4b17023SJohn Marino       loc = gimple_location (stmt);
92*e4b17023SJohn Marino       switch (gimple_code (stmt))
93*e4b17023SJohn Marino 	{
94*e4b17023SJohn Marino 	case GIMPLE_COND:
95*e4b17023SJohn Marino 	  {
96*e4b17023SJohn Marino 	    tree lhs = gimple_cond_lhs (stmt);
97*e4b17023SJohn Marino 	    tree rhs = gimple_cond_rhs (stmt);
98*e4b17023SJohn Marino 	    /* For conditions try harder and lookup single-argument
99*e4b17023SJohn Marino 	       PHI nodes.  Only do so from the same basic-block though
100*e4b17023SJohn Marino 	       as other basic-blocks may be dead already.  */
101*e4b17023SJohn Marino 	    if (TREE_CODE (lhs) == SSA_NAME
102*e4b17023SJohn Marino 		&& !name_registered_for_update_p (lhs))
103*e4b17023SJohn Marino 	      {
104*e4b17023SJohn Marino 		gimple def_stmt = SSA_NAME_DEF_STMT (lhs);
105*e4b17023SJohn Marino 		if (gimple_code (def_stmt) == GIMPLE_PHI
106*e4b17023SJohn Marino 		    && gimple_phi_num_args (def_stmt) == 1
107*e4b17023SJohn Marino 		    && gimple_bb (def_stmt) == gimple_bb (stmt)
108*e4b17023SJohn Marino 		    && (TREE_CODE (PHI_ARG_DEF (def_stmt, 0)) != SSA_NAME
109*e4b17023SJohn Marino 			|| !name_registered_for_update_p (PHI_ARG_DEF (def_stmt,
110*e4b17023SJohn Marino 								       0))))
111*e4b17023SJohn Marino 		  lhs = PHI_ARG_DEF (def_stmt, 0);
112*e4b17023SJohn Marino 	      }
113*e4b17023SJohn Marino 	    if (TREE_CODE (rhs) == SSA_NAME
114*e4b17023SJohn Marino 		&& !name_registered_for_update_p (rhs))
115*e4b17023SJohn Marino 	      {
116*e4b17023SJohn Marino 		gimple def_stmt = SSA_NAME_DEF_STMT (rhs);
117*e4b17023SJohn Marino 		if (gimple_code (def_stmt) == GIMPLE_PHI
118*e4b17023SJohn Marino 		    && gimple_phi_num_args (def_stmt) == 1
119*e4b17023SJohn Marino 		    && gimple_bb (def_stmt) == gimple_bb (stmt)
120*e4b17023SJohn Marino 		    && (TREE_CODE (PHI_ARG_DEF (def_stmt, 0)) != SSA_NAME
121*e4b17023SJohn Marino 			|| !name_registered_for_update_p (PHI_ARG_DEF (def_stmt,
122*e4b17023SJohn Marino 								       0))))
123*e4b17023SJohn Marino 		  rhs = PHI_ARG_DEF (def_stmt, 0);
124*e4b17023SJohn Marino 	      }
125*e4b17023SJohn Marino 	    val = fold_binary_loc (loc, gimple_cond_code (stmt),
126*e4b17023SJohn Marino 				   boolean_type_node, lhs, rhs);
127*e4b17023SJohn Marino 	    break;
128*e4b17023SJohn Marino 	  }
129*e4b17023SJohn Marino 
130*e4b17023SJohn Marino 	case GIMPLE_SWITCH:
131*e4b17023SJohn Marino 	  val = gimple_switch_index (stmt);
132*e4b17023SJohn Marino 	  break;
133*e4b17023SJohn Marino 
134*e4b17023SJohn Marino 	default:
135*e4b17023SJohn Marino 	  val = NULL_TREE;
136*e4b17023SJohn Marino 	}
137*e4b17023SJohn Marino       taken_edge = find_taken_edge (bb, val);
138*e4b17023SJohn Marino       if (!taken_edge)
139*e4b17023SJohn Marino 	{
140*e4b17023SJohn Marino 	  fold_undefer_and_ignore_overflow_warnings ();
141*e4b17023SJohn Marino 	  return false;
142*e4b17023SJohn Marino 	}
143*e4b17023SJohn Marino 
144*e4b17023SJohn Marino       /* Remove all the edges except the one that is always executed.  */
145*e4b17023SJohn Marino       warned = false;
146*e4b17023SJohn Marino       for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
147*e4b17023SJohn Marino 	{
148*e4b17023SJohn Marino 	  if (e != taken_edge)
149*e4b17023SJohn Marino 	    {
150*e4b17023SJohn Marino 	      if (!warned)
151*e4b17023SJohn Marino 		{
152*e4b17023SJohn Marino 		  fold_undefer_overflow_warnings
153*e4b17023SJohn Marino 		    (true, stmt, WARN_STRICT_OVERFLOW_CONDITIONAL);
154*e4b17023SJohn Marino 		  warned = true;
155*e4b17023SJohn Marino 		}
156*e4b17023SJohn Marino 
157*e4b17023SJohn Marino 	      taken_edge->probability += e->probability;
158*e4b17023SJohn Marino 	      taken_edge->count += e->count;
159*e4b17023SJohn Marino 	      remove_edge_and_dominated_blocks (e);
160*e4b17023SJohn Marino 	      retval = true;
161*e4b17023SJohn Marino 	    }
162*e4b17023SJohn Marino 	  else
163*e4b17023SJohn Marino 	    ei_next (&ei);
164*e4b17023SJohn Marino 	}
165*e4b17023SJohn Marino       if (!warned)
166*e4b17023SJohn Marino 	fold_undefer_and_ignore_overflow_warnings ();
167*e4b17023SJohn Marino       if (taken_edge->probability > REG_BR_PROB_BASE)
168*e4b17023SJohn Marino 	taken_edge->probability = REG_BR_PROB_BASE;
169*e4b17023SJohn Marino     }
170*e4b17023SJohn Marino   else
171*e4b17023SJohn Marino     taken_edge = single_succ_edge (bb);
172*e4b17023SJohn Marino 
173*e4b17023SJohn Marino   bitmap_set_bit (cfgcleanup_altered_bbs, bb->index);
174*e4b17023SJohn Marino   gsi_remove (&gsi, true);
175*e4b17023SJohn Marino   taken_edge->flags = EDGE_FALLTHRU;
176*e4b17023SJohn Marino 
177*e4b17023SJohn Marino   return retval;
178*e4b17023SJohn Marino }
179*e4b17023SJohn Marino 
180*e4b17023SJohn Marino /* Try to remove superfluous control structures in basic block BB.  Returns
181*e4b17023SJohn Marino    true if anything changes.  */
182*e4b17023SJohn Marino 
183*e4b17023SJohn Marino static bool
cleanup_control_flow_bb(basic_block bb)184*e4b17023SJohn Marino cleanup_control_flow_bb (basic_block bb)
185*e4b17023SJohn Marino {
186*e4b17023SJohn Marino   gimple_stmt_iterator gsi;
187*e4b17023SJohn Marino   bool retval = false;
188*e4b17023SJohn Marino   gimple stmt;
189*e4b17023SJohn Marino 
190*e4b17023SJohn Marino   /* If the last statement of the block could throw and now cannot,
191*e4b17023SJohn Marino      we need to prune cfg.  */
192*e4b17023SJohn Marino   retval |= gimple_purge_dead_eh_edges (bb);
193*e4b17023SJohn Marino 
194*e4b17023SJohn Marino   gsi = gsi_last_bb (bb);
195*e4b17023SJohn Marino   if (gsi_end_p (gsi))
196*e4b17023SJohn Marino     return retval;
197*e4b17023SJohn Marino 
198*e4b17023SJohn Marino   stmt = gsi_stmt (gsi);
199*e4b17023SJohn Marino 
200*e4b17023SJohn Marino   if (gimple_code (stmt) == GIMPLE_COND
201*e4b17023SJohn Marino       || gimple_code (stmt) == GIMPLE_SWITCH)
202*e4b17023SJohn Marino     retval |= cleanup_control_expr_graph (bb, gsi);
203*e4b17023SJohn Marino   else if (gimple_code (stmt) == GIMPLE_GOTO
204*e4b17023SJohn Marino 	   && TREE_CODE (gimple_goto_dest (stmt)) == ADDR_EXPR
205*e4b17023SJohn Marino 	   && (TREE_CODE (TREE_OPERAND (gimple_goto_dest (stmt), 0))
206*e4b17023SJohn Marino 	       == LABEL_DECL))
207*e4b17023SJohn Marino     {
208*e4b17023SJohn Marino       /* If we had a computed goto which has a compile-time determinable
209*e4b17023SJohn Marino 	 destination, then we can eliminate the goto.  */
210*e4b17023SJohn Marino       edge e;
211*e4b17023SJohn Marino       tree label;
212*e4b17023SJohn Marino       edge_iterator ei;
213*e4b17023SJohn Marino       basic_block target_block;
214*e4b17023SJohn Marino 
215*e4b17023SJohn Marino       /* First look at all the outgoing edges.  Delete any outgoing
216*e4b17023SJohn Marino 	 edges which do not go to the right block.  For the one
217*e4b17023SJohn Marino 	 edge which goes to the right block, fix up its flags.  */
218*e4b17023SJohn Marino       label = TREE_OPERAND (gimple_goto_dest (stmt), 0);
219*e4b17023SJohn Marino       target_block = label_to_block (label);
220*e4b17023SJohn Marino       for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
221*e4b17023SJohn Marino 	{
222*e4b17023SJohn Marino 	  if (e->dest != target_block)
223*e4b17023SJohn Marino 	    remove_edge_and_dominated_blocks (e);
224*e4b17023SJohn Marino 	  else
225*e4b17023SJohn Marino 	    {
226*e4b17023SJohn Marino 	      /* Turn off the EDGE_ABNORMAL flag.  */
227*e4b17023SJohn Marino 	      e->flags &= ~EDGE_ABNORMAL;
228*e4b17023SJohn Marino 
229*e4b17023SJohn Marino 	      /* And set EDGE_FALLTHRU.  */
230*e4b17023SJohn Marino 	      e->flags |= EDGE_FALLTHRU;
231*e4b17023SJohn Marino 	      ei_next (&ei);
232*e4b17023SJohn Marino 	    }
233*e4b17023SJohn Marino 	}
234*e4b17023SJohn Marino 
235*e4b17023SJohn Marino       bitmap_set_bit (cfgcleanup_altered_bbs, bb->index);
236*e4b17023SJohn Marino       bitmap_set_bit (cfgcleanup_altered_bbs, target_block->index);
237*e4b17023SJohn Marino 
238*e4b17023SJohn Marino       /* Remove the GOTO_EXPR as it is not needed.  The CFG has all the
239*e4b17023SJohn Marino 	 relevant information we need.  */
240*e4b17023SJohn Marino       gsi_remove (&gsi, true);
241*e4b17023SJohn Marino       retval = true;
242*e4b17023SJohn Marino     }
243*e4b17023SJohn Marino 
244*e4b17023SJohn Marino   /* Check for indirect calls that have been turned into
245*e4b17023SJohn Marino      noreturn calls.  */
246*e4b17023SJohn Marino   else if (is_gimple_call (stmt)
247*e4b17023SJohn Marino            && gimple_call_noreturn_p (stmt)
248*e4b17023SJohn Marino            && remove_fallthru_edge (bb->succs))
249*e4b17023SJohn Marino     retval = true;
250*e4b17023SJohn Marino 
251*e4b17023SJohn Marino   return retval;
252*e4b17023SJohn Marino }
253*e4b17023SJohn Marino 
254*e4b17023SJohn Marino /* Return true if basic block BB does nothing except pass control
255*e4b17023SJohn Marino    flow to another block and that we can safely insert a label at
256*e4b17023SJohn Marino    the start of the successor block.
257*e4b17023SJohn Marino 
258*e4b17023SJohn Marino    As a precondition, we require that BB be not equal to
259*e4b17023SJohn Marino    ENTRY_BLOCK_PTR.  */
260*e4b17023SJohn Marino 
261*e4b17023SJohn Marino static bool
tree_forwarder_block_p(basic_block bb,bool phi_wanted)262*e4b17023SJohn Marino tree_forwarder_block_p (basic_block bb, bool phi_wanted)
263*e4b17023SJohn Marino {
264*e4b17023SJohn Marino   gimple_stmt_iterator gsi;
265*e4b17023SJohn Marino   location_t locus;
266*e4b17023SJohn Marino 
267*e4b17023SJohn Marino   /* BB must have a single outgoing edge.  */
268*e4b17023SJohn Marino   if (single_succ_p (bb) != 1
269*e4b17023SJohn Marino       /* If PHI_WANTED is false, BB must not have any PHI nodes.
270*e4b17023SJohn Marino 	 Otherwise, BB must have PHI nodes.  */
271*e4b17023SJohn Marino       || gimple_seq_empty_p (phi_nodes (bb)) == phi_wanted
272*e4b17023SJohn Marino       /* BB may not be a predecessor of EXIT_BLOCK_PTR.  */
273*e4b17023SJohn Marino       || single_succ (bb) == EXIT_BLOCK_PTR
274*e4b17023SJohn Marino       /* Nor should this be an infinite loop.  */
275*e4b17023SJohn Marino       || single_succ (bb) == bb
276*e4b17023SJohn Marino       /* BB may not have an abnormal outgoing edge.  */
277*e4b17023SJohn Marino       || (single_succ_edge (bb)->flags & EDGE_ABNORMAL))
278*e4b17023SJohn Marino     return false;
279*e4b17023SJohn Marino 
280*e4b17023SJohn Marino   gcc_checking_assert (bb != ENTRY_BLOCK_PTR);
281*e4b17023SJohn Marino 
282*e4b17023SJohn Marino   locus = single_succ_edge (bb)->goto_locus;
283*e4b17023SJohn Marino 
284*e4b17023SJohn Marino   /* There should not be an edge coming from entry, or an EH edge.  */
285*e4b17023SJohn Marino   {
286*e4b17023SJohn Marino     edge_iterator ei;
287*e4b17023SJohn Marino     edge e;
288*e4b17023SJohn Marino 
289*e4b17023SJohn Marino     FOR_EACH_EDGE (e, ei, bb->preds)
290*e4b17023SJohn Marino       if (e->src == ENTRY_BLOCK_PTR || (e->flags & EDGE_EH))
291*e4b17023SJohn Marino 	return false;
292*e4b17023SJohn Marino       /* If goto_locus of any of the edges differs, prevent removing
293*e4b17023SJohn Marino 	 the forwarder block for -O0.  */
294*e4b17023SJohn Marino       else if (optimize == 0 && e->goto_locus != locus)
295*e4b17023SJohn Marino 	return false;
296*e4b17023SJohn Marino   }
297*e4b17023SJohn Marino 
298*e4b17023SJohn Marino   /* Now walk through the statements backward.  We can ignore labels,
299*e4b17023SJohn Marino      anything else means this is not a forwarder block.  */
300*e4b17023SJohn Marino   for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
301*e4b17023SJohn Marino     {
302*e4b17023SJohn Marino       gimple stmt = gsi_stmt (gsi);
303*e4b17023SJohn Marino 
304*e4b17023SJohn Marino       switch (gimple_code (stmt))
305*e4b17023SJohn Marino 	{
306*e4b17023SJohn Marino 	case GIMPLE_LABEL:
307*e4b17023SJohn Marino 	  if (DECL_NONLOCAL (gimple_label_label (stmt)))
308*e4b17023SJohn Marino 	    return false;
309*e4b17023SJohn Marino 	  if (optimize == 0 && gimple_location (stmt) != locus)
310*e4b17023SJohn Marino 	    return false;
311*e4b17023SJohn Marino 	  break;
312*e4b17023SJohn Marino 
313*e4b17023SJohn Marino 	  /* ??? For now, hope there's a corresponding debug
314*e4b17023SJohn Marino 	     assignment at the destination.  */
315*e4b17023SJohn Marino 	case GIMPLE_DEBUG:
316*e4b17023SJohn Marino 	  break;
317*e4b17023SJohn Marino 
318*e4b17023SJohn Marino 	default:
319*e4b17023SJohn Marino 	  return false;
320*e4b17023SJohn Marino 	}
321*e4b17023SJohn Marino     }
322*e4b17023SJohn Marino 
323*e4b17023SJohn Marino   if (current_loops)
324*e4b17023SJohn Marino     {
325*e4b17023SJohn Marino       basic_block dest;
326*e4b17023SJohn Marino       /* Protect loop latches, headers and preheaders.  */
327*e4b17023SJohn Marino       if (bb->loop_father->header == bb)
328*e4b17023SJohn Marino 	return false;
329*e4b17023SJohn Marino       dest = EDGE_SUCC (bb, 0)->dest;
330*e4b17023SJohn Marino 
331*e4b17023SJohn Marino       if (dest->loop_father->header == dest)
332*e4b17023SJohn Marino 	return false;
333*e4b17023SJohn Marino     }
334*e4b17023SJohn Marino   return true;
335*e4b17023SJohn Marino }
336*e4b17023SJohn Marino 
337*e4b17023SJohn Marino /* If all the PHI nodes in DEST have alternatives for E1 and E2 and
338*e4b17023SJohn Marino    those alternatives are equal in each of the PHI nodes, then return
339*e4b17023SJohn Marino    true, else return false.  */
340*e4b17023SJohn Marino 
341*e4b17023SJohn Marino static bool
phi_alternatives_equal(basic_block dest,edge e1,edge e2)342*e4b17023SJohn Marino phi_alternatives_equal (basic_block dest, edge e1, edge e2)
343*e4b17023SJohn Marino {
344*e4b17023SJohn Marino   int n1 = e1->dest_idx;
345*e4b17023SJohn Marino   int n2 = e2->dest_idx;
346*e4b17023SJohn Marino   gimple_stmt_iterator gsi;
347*e4b17023SJohn Marino 
348*e4b17023SJohn Marino   for (gsi = gsi_start_phis (dest); !gsi_end_p (gsi); gsi_next (&gsi))
349*e4b17023SJohn Marino     {
350*e4b17023SJohn Marino       gimple phi = gsi_stmt (gsi);
351*e4b17023SJohn Marino       tree val1 = gimple_phi_arg_def (phi, n1);
352*e4b17023SJohn Marino       tree val2 = gimple_phi_arg_def (phi, n2);
353*e4b17023SJohn Marino 
354*e4b17023SJohn Marino       gcc_assert (val1 != NULL_TREE);
355*e4b17023SJohn Marino       gcc_assert (val2 != NULL_TREE);
356*e4b17023SJohn Marino 
357*e4b17023SJohn Marino       if (!operand_equal_for_phi_arg_p (val1, val2))
358*e4b17023SJohn Marino 	return false;
359*e4b17023SJohn Marino     }
360*e4b17023SJohn Marino 
361*e4b17023SJohn Marino   return true;
362*e4b17023SJohn Marino }
363*e4b17023SJohn Marino 
364*e4b17023SJohn Marino /* Removes forwarder block BB.  Returns false if this failed.  */
365*e4b17023SJohn Marino 
366*e4b17023SJohn Marino static bool
remove_forwarder_block(basic_block bb)367*e4b17023SJohn Marino remove_forwarder_block (basic_block bb)
368*e4b17023SJohn Marino {
369*e4b17023SJohn Marino   edge succ = single_succ_edge (bb), e, s;
370*e4b17023SJohn Marino   basic_block dest = succ->dest;
371*e4b17023SJohn Marino   gimple label;
372*e4b17023SJohn Marino   edge_iterator ei;
373*e4b17023SJohn Marino   gimple_stmt_iterator gsi, gsi_to;
374*e4b17023SJohn Marino   bool can_move_debug_stmts;
375*e4b17023SJohn Marino 
376*e4b17023SJohn Marino   /* We check for infinite loops already in tree_forwarder_block_p.
377*e4b17023SJohn Marino      However it may happen that the infinite loop is created
378*e4b17023SJohn Marino      afterwards due to removal of forwarders.  */
379*e4b17023SJohn Marino   if (dest == bb)
380*e4b17023SJohn Marino     return false;
381*e4b17023SJohn Marino 
382*e4b17023SJohn Marino   /* If the destination block consists of a nonlocal label or is a
383*e4b17023SJohn Marino      EH landing pad, do not merge it.  */
384*e4b17023SJohn Marino   label = first_stmt (dest);
385*e4b17023SJohn Marino   if (label
386*e4b17023SJohn Marino       && gimple_code (label) == GIMPLE_LABEL
387*e4b17023SJohn Marino       && (DECL_NONLOCAL (gimple_label_label (label))
388*e4b17023SJohn Marino 	  || EH_LANDING_PAD_NR (gimple_label_label (label)) != 0))
389*e4b17023SJohn Marino     return false;
390*e4b17023SJohn Marino 
391*e4b17023SJohn Marino   /* If there is an abnormal edge to basic block BB, but not into
392*e4b17023SJohn Marino      dest, problems might occur during removal of the phi node at out
393*e4b17023SJohn Marino      of ssa due to overlapping live ranges of registers.
394*e4b17023SJohn Marino 
395*e4b17023SJohn Marino      If there is an abnormal edge in DEST, the problems would occur
396*e4b17023SJohn Marino      anyway since cleanup_dead_labels would then merge the labels for
397*e4b17023SJohn Marino      two different eh regions, and rest of exception handling code
398*e4b17023SJohn Marino      does not like it.
399*e4b17023SJohn Marino 
400*e4b17023SJohn Marino      So if there is an abnormal edge to BB, proceed only if there is
401*e4b17023SJohn Marino      no abnormal edge to DEST and there are no phi nodes in DEST.  */
402*e4b17023SJohn Marino   if (bb_has_abnormal_pred (bb)
403*e4b17023SJohn Marino       && (bb_has_abnormal_pred (dest)
404*e4b17023SJohn Marino 	  || !gimple_seq_empty_p (phi_nodes (dest))))
405*e4b17023SJohn Marino     return false;
406*e4b17023SJohn Marino 
407*e4b17023SJohn Marino   /* If there are phi nodes in DEST, and some of the blocks that are
408*e4b17023SJohn Marino      predecessors of BB are also predecessors of DEST, check that the
409*e4b17023SJohn Marino      phi node arguments match.  */
410*e4b17023SJohn Marino   if (!gimple_seq_empty_p (phi_nodes (dest)))
411*e4b17023SJohn Marino     {
412*e4b17023SJohn Marino       FOR_EACH_EDGE (e, ei, bb->preds)
413*e4b17023SJohn Marino 	{
414*e4b17023SJohn Marino 	  s = find_edge (e->src, dest);
415*e4b17023SJohn Marino 	  if (!s)
416*e4b17023SJohn Marino 	    continue;
417*e4b17023SJohn Marino 
418*e4b17023SJohn Marino 	  if (!phi_alternatives_equal (dest, succ, s))
419*e4b17023SJohn Marino 	    return false;
420*e4b17023SJohn Marino 	}
421*e4b17023SJohn Marino     }
422*e4b17023SJohn Marino 
423*e4b17023SJohn Marino   can_move_debug_stmts = MAY_HAVE_DEBUG_STMTS && single_pred_p (dest);
424*e4b17023SJohn Marino 
425*e4b17023SJohn Marino   /* Redirect the edges.  */
426*e4b17023SJohn Marino   for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
427*e4b17023SJohn Marino     {
428*e4b17023SJohn Marino       bitmap_set_bit (cfgcleanup_altered_bbs, e->src->index);
429*e4b17023SJohn Marino 
430*e4b17023SJohn Marino       if (e->flags & EDGE_ABNORMAL)
431*e4b17023SJohn Marino 	{
432*e4b17023SJohn Marino 	  /* If there is an abnormal edge, redirect it anyway, and
433*e4b17023SJohn Marino 	     move the labels to the new block to make it legal.  */
434*e4b17023SJohn Marino 	  s = redirect_edge_succ_nodup (e, dest);
435*e4b17023SJohn Marino 	}
436*e4b17023SJohn Marino       else
437*e4b17023SJohn Marino 	s = redirect_edge_and_branch (e, dest);
438*e4b17023SJohn Marino 
439*e4b17023SJohn Marino       if (s == e)
440*e4b17023SJohn Marino 	{
441*e4b17023SJohn Marino 	  /* Create arguments for the phi nodes, since the edge was not
442*e4b17023SJohn Marino 	     here before.  */
443*e4b17023SJohn Marino 	  for (gsi = gsi_start_phis (dest);
444*e4b17023SJohn Marino 	       !gsi_end_p (gsi);
445*e4b17023SJohn Marino 	       gsi_next (&gsi))
446*e4b17023SJohn Marino 	    {
447*e4b17023SJohn Marino 	      gimple phi = gsi_stmt (gsi);
448*e4b17023SJohn Marino 	      source_location l = gimple_phi_arg_location_from_edge (phi, succ);
449*e4b17023SJohn Marino 	      add_phi_arg (phi, gimple_phi_arg_def (phi, succ->dest_idx), s, l);
450*e4b17023SJohn Marino 	    }
451*e4b17023SJohn Marino 	}
452*e4b17023SJohn Marino     }
453*e4b17023SJohn Marino 
454*e4b17023SJohn Marino   /* Move nonlocal labels and computed goto targets as well as user
455*e4b17023SJohn Marino      defined labels and labels with an EH landing pad number to the
456*e4b17023SJohn Marino      new block, so that the redirection of the abnormal edges works,
457*e4b17023SJohn Marino      jump targets end up in a sane place and debug information for
458*e4b17023SJohn Marino      labels is retained.  */
459*e4b17023SJohn Marino   gsi_to = gsi_start_bb (dest);
460*e4b17023SJohn Marino   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
461*e4b17023SJohn Marino     {
462*e4b17023SJohn Marino       tree decl;
463*e4b17023SJohn Marino       label = gsi_stmt (gsi);
464*e4b17023SJohn Marino       if (is_gimple_debug (label))
465*e4b17023SJohn Marino 	break;
466*e4b17023SJohn Marino       decl = gimple_label_label (label);
467*e4b17023SJohn Marino       if (EH_LANDING_PAD_NR (decl) != 0
468*e4b17023SJohn Marino 	  || DECL_NONLOCAL (decl)
469*e4b17023SJohn Marino 	  || FORCED_LABEL (decl)
470*e4b17023SJohn Marino 	  || !DECL_ARTIFICIAL (decl))
471*e4b17023SJohn Marino 	{
472*e4b17023SJohn Marino 	  gsi_remove (&gsi, false);
473*e4b17023SJohn Marino 	  gsi_insert_before (&gsi_to, label, GSI_SAME_STMT);
474*e4b17023SJohn Marino 	}
475*e4b17023SJohn Marino       else
476*e4b17023SJohn Marino 	gsi_next (&gsi);
477*e4b17023SJohn Marino     }
478*e4b17023SJohn Marino 
479*e4b17023SJohn Marino   /* Move debug statements if the destination has a single predecessor.  */
480*e4b17023SJohn Marino   if (can_move_debug_stmts)
481*e4b17023SJohn Marino     {
482*e4b17023SJohn Marino       gsi_to = gsi_after_labels (dest);
483*e4b17023SJohn Marino       for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); )
484*e4b17023SJohn Marino 	{
485*e4b17023SJohn Marino 	  gimple debug = gsi_stmt (gsi);
486*e4b17023SJohn Marino 	  if (!is_gimple_debug (debug))
487*e4b17023SJohn Marino 	    break;
488*e4b17023SJohn Marino 	  gsi_remove (&gsi, false);
489*e4b17023SJohn Marino 	  gsi_insert_before (&gsi_to, debug, GSI_SAME_STMT);
490*e4b17023SJohn Marino 	}
491*e4b17023SJohn Marino     }
492*e4b17023SJohn Marino 
493*e4b17023SJohn Marino   bitmap_set_bit (cfgcleanup_altered_bbs, dest->index);
494*e4b17023SJohn Marino 
495*e4b17023SJohn Marino   /* Update the dominators.  */
496*e4b17023SJohn Marino   if (dom_info_available_p (CDI_DOMINATORS))
497*e4b17023SJohn Marino     {
498*e4b17023SJohn Marino       basic_block dom, dombb, domdest;
499*e4b17023SJohn Marino 
500*e4b17023SJohn Marino       dombb = get_immediate_dominator (CDI_DOMINATORS, bb);
501*e4b17023SJohn Marino       domdest = get_immediate_dominator (CDI_DOMINATORS, dest);
502*e4b17023SJohn Marino       if (domdest == bb)
503*e4b17023SJohn Marino 	{
504*e4b17023SJohn Marino 	  /* Shortcut to avoid calling (relatively expensive)
505*e4b17023SJohn Marino 	     nearest_common_dominator unless necessary.  */
506*e4b17023SJohn Marino 	  dom = dombb;
507*e4b17023SJohn Marino 	}
508*e4b17023SJohn Marino       else
509*e4b17023SJohn Marino 	dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb);
510*e4b17023SJohn Marino 
511*e4b17023SJohn Marino       set_immediate_dominator (CDI_DOMINATORS, dest, dom);
512*e4b17023SJohn Marino     }
513*e4b17023SJohn Marino 
514*e4b17023SJohn Marino   /* And kill the forwarder block.  */
515*e4b17023SJohn Marino   delete_basic_block (bb);
516*e4b17023SJohn Marino 
517*e4b17023SJohn Marino   return true;
518*e4b17023SJohn Marino }
519*e4b17023SJohn Marino 
520*e4b17023SJohn Marino /* STMT is a call that has been discovered noreturn.  Fixup the CFG
521*e4b17023SJohn Marino    and remove LHS.  Return true if something changed.  */
522*e4b17023SJohn Marino 
523*e4b17023SJohn Marino bool
fixup_noreturn_call(gimple stmt)524*e4b17023SJohn Marino fixup_noreturn_call (gimple stmt)
525*e4b17023SJohn Marino {
526*e4b17023SJohn Marino   basic_block bb = gimple_bb (stmt);
527*e4b17023SJohn Marino   bool changed = false;
528*e4b17023SJohn Marino 
529*e4b17023SJohn Marino   if (gimple_call_builtin_p (stmt, BUILT_IN_RETURN))
530*e4b17023SJohn Marino     return false;
531*e4b17023SJohn Marino 
532*e4b17023SJohn Marino   /* First split basic block if stmt is not last.  */
533*e4b17023SJohn Marino   if (stmt != gsi_stmt (gsi_last_bb (bb)))
534*e4b17023SJohn Marino     split_block (bb, stmt);
535*e4b17023SJohn Marino 
536*e4b17023SJohn Marino   changed |= remove_fallthru_edge (bb->succs);
537*e4b17023SJohn Marino 
538*e4b17023SJohn Marino   /* If there is LHS, remove it.  */
539*e4b17023SJohn Marino   if (gimple_call_lhs (stmt))
540*e4b17023SJohn Marino     {
541*e4b17023SJohn Marino       tree op = gimple_call_lhs (stmt);
542*e4b17023SJohn Marino       gimple_call_set_lhs (stmt, NULL_TREE);
543*e4b17023SJohn Marino 
544*e4b17023SJohn Marino       /* We need to remove SSA name to avoid checking errors.
545*e4b17023SJohn Marino 	 All uses are dominated by the noreturn and thus will
546*e4b17023SJohn Marino 	 be removed afterwards.
547*e4b17023SJohn Marino 	 We proactively remove affected non-PHI statements to avoid
548*e4b17023SJohn Marino 	 fixup_cfg from trying to update them and crashing.  */
549*e4b17023SJohn Marino       if (TREE_CODE (op) == SSA_NAME)
550*e4b17023SJohn Marino 	{
551*e4b17023SJohn Marino 	  use_operand_p use_p;
552*e4b17023SJohn Marino           imm_use_iterator iter;
553*e4b17023SJohn Marino 	  gimple use_stmt;
554*e4b17023SJohn Marino 	  bitmap_iterator bi;
555*e4b17023SJohn Marino 	  unsigned int bb_index;
556*e4b17023SJohn Marino 
557*e4b17023SJohn Marino 	  bitmap blocks = BITMAP_ALLOC (NULL);
558*e4b17023SJohn Marino 
559*e4b17023SJohn Marino           FOR_EACH_IMM_USE_STMT (use_stmt, iter, op)
560*e4b17023SJohn Marino 	    {
561*e4b17023SJohn Marino 	      if (gimple_code (use_stmt) != GIMPLE_PHI)
562*e4b17023SJohn Marino 	        bitmap_set_bit (blocks, gimple_bb (use_stmt)->index);
563*e4b17023SJohn Marino 	      else
564*e4b17023SJohn Marino 		FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
565*e4b17023SJohn Marino 		  SET_USE (use_p, error_mark_node);
566*e4b17023SJohn Marino 	    }
567*e4b17023SJohn Marino 	  EXECUTE_IF_SET_IN_BITMAP (blocks, 0, bb_index, bi)
568*e4b17023SJohn Marino 	    delete_basic_block (BASIC_BLOCK (bb_index));
569*e4b17023SJohn Marino 	  BITMAP_FREE (blocks);
570*e4b17023SJohn Marino 	  release_ssa_name (op);
571*e4b17023SJohn Marino 	}
572*e4b17023SJohn Marino       update_stmt (stmt);
573*e4b17023SJohn Marino       changed = true;
574*e4b17023SJohn Marino     }
575*e4b17023SJohn Marino   /* Similarly remove VDEF if there is any.  */
576*e4b17023SJohn Marino   else if (gimple_vdef (stmt))
577*e4b17023SJohn Marino     update_stmt (stmt);
578*e4b17023SJohn Marino   return changed;
579*e4b17023SJohn Marino }
580*e4b17023SJohn Marino 
581*e4b17023SJohn Marino 
582*e4b17023SJohn Marino /* Split basic blocks on calls in the middle of a basic block that are now
583*e4b17023SJohn Marino    known not to return, and remove the unreachable code.  */
584*e4b17023SJohn Marino 
585*e4b17023SJohn Marino static bool
split_bbs_on_noreturn_calls(void)586*e4b17023SJohn Marino split_bbs_on_noreturn_calls (void)
587*e4b17023SJohn Marino {
588*e4b17023SJohn Marino   bool changed = false;
589*e4b17023SJohn Marino   gimple stmt;
590*e4b17023SJohn Marino   basic_block bb;
591*e4b17023SJohn Marino 
592*e4b17023SJohn Marino   /* Detect cases where a mid-block call is now known not to return.  */
593*e4b17023SJohn Marino   if (cfun->gimple_df)
594*e4b17023SJohn Marino     while (VEC_length (gimple, MODIFIED_NORETURN_CALLS (cfun)))
595*e4b17023SJohn Marino       {
596*e4b17023SJohn Marino 	stmt = VEC_pop (gimple, MODIFIED_NORETURN_CALLS (cfun));
597*e4b17023SJohn Marino 	bb = gimple_bb (stmt);
598*e4b17023SJohn Marino 	/* BB might be deleted at this point, so verify first
599*e4b17023SJohn Marino 	   BB is present in the cfg.  */
600*e4b17023SJohn Marino 	if (bb == NULL
601*e4b17023SJohn Marino 	    || bb->index < NUM_FIXED_BLOCKS
602*e4b17023SJohn Marino 	    || bb->index >= last_basic_block
603*e4b17023SJohn Marino 	    || BASIC_BLOCK (bb->index) != bb
604*e4b17023SJohn Marino 	    || !gimple_call_noreturn_p (stmt))
605*e4b17023SJohn Marino 	  continue;
606*e4b17023SJohn Marino 
607*e4b17023SJohn Marino 	changed |= fixup_noreturn_call (stmt);
608*e4b17023SJohn Marino       }
609*e4b17023SJohn Marino 
610*e4b17023SJohn Marino   return changed;
611*e4b17023SJohn Marino }
612*e4b17023SJohn Marino 
613*e4b17023SJohn Marino /* Tries to cleanup cfg in basic block BB.  Returns true if anything
614*e4b17023SJohn Marino    changes.  */
615*e4b17023SJohn Marino 
616*e4b17023SJohn Marino static bool
cleanup_tree_cfg_bb(basic_block bb)617*e4b17023SJohn Marino cleanup_tree_cfg_bb (basic_block bb)
618*e4b17023SJohn Marino {
619*e4b17023SJohn Marino   bool retval = cleanup_control_flow_bb (bb);
620*e4b17023SJohn Marino 
621*e4b17023SJohn Marino   if (tree_forwarder_block_p (bb, false)
622*e4b17023SJohn Marino       && remove_forwarder_block (bb))
623*e4b17023SJohn Marino     return true;
624*e4b17023SJohn Marino 
625*e4b17023SJohn Marino   /* Merging the blocks may create new opportunities for folding
626*e4b17023SJohn Marino      conditional branches (due to the elimination of single-valued PHI
627*e4b17023SJohn Marino      nodes).  */
628*e4b17023SJohn Marino   if (single_succ_p (bb)
629*e4b17023SJohn Marino       && can_merge_blocks_p (bb, single_succ (bb)))
630*e4b17023SJohn Marino     {
631*e4b17023SJohn Marino       merge_blocks (bb, single_succ (bb));
632*e4b17023SJohn Marino       return true;
633*e4b17023SJohn Marino     }
634*e4b17023SJohn Marino 
635*e4b17023SJohn Marino   return retval;
636*e4b17023SJohn Marino }
637*e4b17023SJohn Marino 
638*e4b17023SJohn Marino /* Iterate the cfg cleanups, while anything changes.  */
639*e4b17023SJohn Marino 
640*e4b17023SJohn Marino static bool
cleanup_tree_cfg_1(void)641*e4b17023SJohn Marino cleanup_tree_cfg_1 (void)
642*e4b17023SJohn Marino {
643*e4b17023SJohn Marino   bool retval = false;
644*e4b17023SJohn Marino   basic_block bb;
645*e4b17023SJohn Marino   unsigned i, n;
646*e4b17023SJohn Marino 
647*e4b17023SJohn Marino   retval |= split_bbs_on_noreturn_calls ();
648*e4b17023SJohn Marino 
649*e4b17023SJohn Marino   /* Prepare the worklists of altered blocks.  */
650*e4b17023SJohn Marino   cfgcleanup_altered_bbs = BITMAP_ALLOC (NULL);
651*e4b17023SJohn Marino 
652*e4b17023SJohn Marino   /* During forwarder block cleanup, we may redirect edges out of
653*e4b17023SJohn Marino      SWITCH_EXPRs, which can get expensive.  So we want to enable
654*e4b17023SJohn Marino      recording of edge to CASE_LABEL_EXPR.  */
655*e4b17023SJohn Marino   start_recording_case_labels ();
656*e4b17023SJohn Marino 
657*e4b17023SJohn Marino   /* Start by iterating over all basic blocks.  We cannot use FOR_EACH_BB,
658*e4b17023SJohn Marino      since the basic blocks may get removed.  */
659*e4b17023SJohn Marino   n = last_basic_block;
660*e4b17023SJohn Marino   for (i = NUM_FIXED_BLOCKS; i < n; i++)
661*e4b17023SJohn Marino     {
662*e4b17023SJohn Marino       bb = BASIC_BLOCK (i);
663*e4b17023SJohn Marino       if (bb)
664*e4b17023SJohn Marino 	retval |= cleanup_tree_cfg_bb (bb);
665*e4b17023SJohn Marino     }
666*e4b17023SJohn Marino 
667*e4b17023SJohn Marino   /* Now process the altered blocks, as long as any are available.  */
668*e4b17023SJohn Marino   while (!bitmap_empty_p (cfgcleanup_altered_bbs))
669*e4b17023SJohn Marino     {
670*e4b17023SJohn Marino       i = bitmap_first_set_bit (cfgcleanup_altered_bbs);
671*e4b17023SJohn Marino       bitmap_clear_bit (cfgcleanup_altered_bbs, i);
672*e4b17023SJohn Marino       if (i < NUM_FIXED_BLOCKS)
673*e4b17023SJohn Marino 	continue;
674*e4b17023SJohn Marino 
675*e4b17023SJohn Marino       bb = BASIC_BLOCK (i);
676*e4b17023SJohn Marino       if (!bb)
677*e4b17023SJohn Marino 	continue;
678*e4b17023SJohn Marino 
679*e4b17023SJohn Marino       retval |= cleanup_tree_cfg_bb (bb);
680*e4b17023SJohn Marino 
681*e4b17023SJohn Marino       /* Rerun split_bbs_on_noreturn_calls, in case we have altered any noreturn
682*e4b17023SJohn Marino 	 calls.  */
683*e4b17023SJohn Marino       retval |= split_bbs_on_noreturn_calls ();
684*e4b17023SJohn Marino     }
685*e4b17023SJohn Marino 
686*e4b17023SJohn Marino   end_recording_case_labels ();
687*e4b17023SJohn Marino   BITMAP_FREE (cfgcleanup_altered_bbs);
688*e4b17023SJohn Marino   return retval;
689*e4b17023SJohn Marino }
690*e4b17023SJohn Marino 
691*e4b17023SJohn Marino 
692*e4b17023SJohn Marino /* Remove unreachable blocks and other miscellaneous clean up work.
693*e4b17023SJohn Marino    Return true if the flowgraph was modified, false otherwise.  */
694*e4b17023SJohn Marino 
695*e4b17023SJohn Marino static bool
cleanup_tree_cfg_noloop(void)696*e4b17023SJohn Marino cleanup_tree_cfg_noloop (void)
697*e4b17023SJohn Marino {
698*e4b17023SJohn Marino   bool changed;
699*e4b17023SJohn Marino 
700*e4b17023SJohn Marino   timevar_push (TV_TREE_CLEANUP_CFG);
701*e4b17023SJohn Marino 
702*e4b17023SJohn Marino   /* Iterate until there are no more cleanups left to do.  If any
703*e4b17023SJohn Marino      iteration changed the flowgraph, set CHANGED to true.
704*e4b17023SJohn Marino 
705*e4b17023SJohn Marino      If dominance information is available, there cannot be any unreachable
706*e4b17023SJohn Marino      blocks.  */
707*e4b17023SJohn Marino   if (!dom_info_available_p (CDI_DOMINATORS))
708*e4b17023SJohn Marino     {
709*e4b17023SJohn Marino       changed = delete_unreachable_blocks ();
710*e4b17023SJohn Marino       calculate_dominance_info (CDI_DOMINATORS);
711*e4b17023SJohn Marino     }
712*e4b17023SJohn Marino   else
713*e4b17023SJohn Marino     {
714*e4b17023SJohn Marino #ifdef ENABLE_CHECKING
715*e4b17023SJohn Marino       verify_dominators (CDI_DOMINATORS);
716*e4b17023SJohn Marino #endif
717*e4b17023SJohn Marino       changed = false;
718*e4b17023SJohn Marino     }
719*e4b17023SJohn Marino 
720*e4b17023SJohn Marino   changed |= cleanup_tree_cfg_1 ();
721*e4b17023SJohn Marino 
722*e4b17023SJohn Marino   gcc_assert (dom_info_available_p (CDI_DOMINATORS));
723*e4b17023SJohn Marino   compact_blocks ();
724*e4b17023SJohn Marino 
725*e4b17023SJohn Marino #ifdef ENABLE_CHECKING
726*e4b17023SJohn Marino   verify_flow_info ();
727*e4b17023SJohn Marino #endif
728*e4b17023SJohn Marino 
729*e4b17023SJohn Marino   timevar_pop (TV_TREE_CLEANUP_CFG);
730*e4b17023SJohn Marino 
731*e4b17023SJohn Marino   if (changed && current_loops)
732*e4b17023SJohn Marino     loops_state_set (LOOPS_NEED_FIXUP);
733*e4b17023SJohn Marino 
734*e4b17023SJohn Marino   return changed;
735*e4b17023SJohn Marino }
736*e4b17023SJohn Marino 
737*e4b17023SJohn Marino /* Repairs loop structures.  */
738*e4b17023SJohn Marino 
739*e4b17023SJohn Marino static void
repair_loop_structures(void)740*e4b17023SJohn Marino repair_loop_structures (void)
741*e4b17023SJohn Marino {
742*e4b17023SJohn Marino   bitmap changed_bbs;
743*e4b17023SJohn Marino 
744*e4b17023SJohn Marino   timevar_push (TV_REPAIR_LOOPS);
745*e4b17023SJohn Marino   changed_bbs = BITMAP_ALLOC (NULL);
746*e4b17023SJohn Marino   fix_loop_structure (changed_bbs);
747*e4b17023SJohn Marino 
748*e4b17023SJohn Marino   /* This usually does nothing.  But sometimes parts of cfg that originally
749*e4b17023SJohn Marino      were inside a loop get out of it due to edge removal (since they
750*e4b17023SJohn Marino      become unreachable by back edges from latch).  */
751*e4b17023SJohn Marino   if (loops_state_satisfies_p (LOOP_CLOSED_SSA))
752*e4b17023SJohn Marino     rewrite_into_loop_closed_ssa (changed_bbs, TODO_update_ssa);
753*e4b17023SJohn Marino 
754*e4b17023SJohn Marino   BITMAP_FREE (changed_bbs);
755*e4b17023SJohn Marino 
756*e4b17023SJohn Marino #ifdef ENABLE_CHECKING
757*e4b17023SJohn Marino   verify_loop_structure ();
758*e4b17023SJohn Marino #endif
759*e4b17023SJohn Marino   scev_reset ();
760*e4b17023SJohn Marino 
761*e4b17023SJohn Marino   loops_state_clear (LOOPS_NEED_FIXUP);
762*e4b17023SJohn Marino   timevar_pop (TV_REPAIR_LOOPS);
763*e4b17023SJohn Marino }
764*e4b17023SJohn Marino 
765*e4b17023SJohn Marino /* Cleanup cfg and repair loop structures.  */
766*e4b17023SJohn Marino 
767*e4b17023SJohn Marino bool
cleanup_tree_cfg(void)768*e4b17023SJohn Marino cleanup_tree_cfg (void)
769*e4b17023SJohn Marino {
770*e4b17023SJohn Marino   bool changed = cleanup_tree_cfg_noloop ();
771*e4b17023SJohn Marino 
772*e4b17023SJohn Marino   if (current_loops != NULL
773*e4b17023SJohn Marino       && loops_state_satisfies_p (LOOPS_NEED_FIXUP))
774*e4b17023SJohn Marino     repair_loop_structures ();
775*e4b17023SJohn Marino 
776*e4b17023SJohn Marino   return changed;
777*e4b17023SJohn Marino }
778*e4b17023SJohn Marino 
779*e4b17023SJohn Marino /* Merge the PHI nodes at BB into those at BB's sole successor.  */
780*e4b17023SJohn Marino 
781*e4b17023SJohn Marino static void
remove_forwarder_block_with_phi(basic_block bb)782*e4b17023SJohn Marino remove_forwarder_block_with_phi (basic_block bb)
783*e4b17023SJohn Marino {
784*e4b17023SJohn Marino   edge succ = single_succ_edge (bb);
785*e4b17023SJohn Marino   basic_block dest = succ->dest;
786*e4b17023SJohn Marino   gimple label;
787*e4b17023SJohn Marino   basic_block dombb, domdest, dom;
788*e4b17023SJohn Marino 
789*e4b17023SJohn Marino   /* We check for infinite loops already in tree_forwarder_block_p.
790*e4b17023SJohn Marino      However it may happen that the infinite loop is created
791*e4b17023SJohn Marino      afterwards due to removal of forwarders.  */
792*e4b17023SJohn Marino   if (dest == bb)
793*e4b17023SJohn Marino     return;
794*e4b17023SJohn Marino 
795*e4b17023SJohn Marino   /* If the destination block consists of a nonlocal label, do not
796*e4b17023SJohn Marino      merge it.  */
797*e4b17023SJohn Marino   label = first_stmt (dest);
798*e4b17023SJohn Marino   if (label
799*e4b17023SJohn Marino       && gimple_code (label) == GIMPLE_LABEL
800*e4b17023SJohn Marino       && DECL_NONLOCAL (gimple_label_label (label)))
801*e4b17023SJohn Marino     return;
802*e4b17023SJohn Marino 
803*e4b17023SJohn Marino   /* Redirect each incoming edge to BB to DEST.  */
804*e4b17023SJohn Marino   while (EDGE_COUNT (bb->preds) > 0)
805*e4b17023SJohn Marino     {
806*e4b17023SJohn Marino       edge e = EDGE_PRED (bb, 0), s;
807*e4b17023SJohn Marino       gimple_stmt_iterator gsi;
808*e4b17023SJohn Marino 
809*e4b17023SJohn Marino       s = find_edge (e->src, dest);
810*e4b17023SJohn Marino       if (s)
811*e4b17023SJohn Marino 	{
812*e4b17023SJohn Marino 	  /* We already have an edge S from E->src to DEST.  If S and
813*e4b17023SJohn Marino 	     E->dest's sole successor edge have the same PHI arguments
814*e4b17023SJohn Marino 	     at DEST, redirect S to DEST.  */
815*e4b17023SJohn Marino 	  if (phi_alternatives_equal (dest, s, succ))
816*e4b17023SJohn Marino 	    {
817*e4b17023SJohn Marino 	      e = redirect_edge_and_branch (e, dest);
818*e4b17023SJohn Marino 	      redirect_edge_var_map_clear (e);
819*e4b17023SJohn Marino 	      continue;
820*e4b17023SJohn Marino 	    }
821*e4b17023SJohn Marino 
822*e4b17023SJohn Marino 	  /* PHI arguments are different.  Create a forwarder block by
823*e4b17023SJohn Marino 	     splitting E so that we can merge PHI arguments on E to
824*e4b17023SJohn Marino 	     DEST.  */
825*e4b17023SJohn Marino 	  e = single_succ_edge (split_edge (e));
826*e4b17023SJohn Marino 	}
827*e4b17023SJohn Marino 
828*e4b17023SJohn Marino       s = redirect_edge_and_branch (e, dest);
829*e4b17023SJohn Marino 
830*e4b17023SJohn Marino       /* redirect_edge_and_branch must not create a new edge.  */
831*e4b17023SJohn Marino       gcc_assert (s == e);
832*e4b17023SJohn Marino 
833*e4b17023SJohn Marino       /* Add to the PHI nodes at DEST each PHI argument removed at the
834*e4b17023SJohn Marino 	 destination of E.  */
835*e4b17023SJohn Marino       for (gsi = gsi_start_phis (dest);
836*e4b17023SJohn Marino 	   !gsi_end_p (gsi);
837*e4b17023SJohn Marino 	   gsi_next (&gsi))
838*e4b17023SJohn Marino 	{
839*e4b17023SJohn Marino 	  gimple phi = gsi_stmt (gsi);
840*e4b17023SJohn Marino 	  tree def = gimple_phi_arg_def (phi, succ->dest_idx);
841*e4b17023SJohn Marino 	  source_location locus = gimple_phi_arg_location_from_edge (phi, succ);
842*e4b17023SJohn Marino 
843*e4b17023SJohn Marino 	  if (TREE_CODE (def) == SSA_NAME)
844*e4b17023SJohn Marino 	    {
845*e4b17023SJohn Marino 	      edge_var_map_vector head;
846*e4b17023SJohn Marino 	      edge_var_map *vm;
847*e4b17023SJohn Marino 	      size_t i;
848*e4b17023SJohn Marino 
849*e4b17023SJohn Marino 	      /* If DEF is one of the results of PHI nodes removed during
850*e4b17023SJohn Marino 		 redirection, replace it with the PHI argument that used
851*e4b17023SJohn Marino 		 to be on E.  */
852*e4b17023SJohn Marino 	      head = redirect_edge_var_map_vector (e);
853*e4b17023SJohn Marino 	      FOR_EACH_VEC_ELT (edge_var_map, head, i, vm)
854*e4b17023SJohn Marino 		{
855*e4b17023SJohn Marino 		  tree old_arg = redirect_edge_var_map_result (vm);
856*e4b17023SJohn Marino 		  tree new_arg = redirect_edge_var_map_def (vm);
857*e4b17023SJohn Marino 
858*e4b17023SJohn Marino 		  if (def == old_arg)
859*e4b17023SJohn Marino 		    {
860*e4b17023SJohn Marino 		      def = new_arg;
861*e4b17023SJohn Marino 		      locus = redirect_edge_var_map_location (vm);
862*e4b17023SJohn Marino 		      break;
863*e4b17023SJohn Marino 		    }
864*e4b17023SJohn Marino 		}
865*e4b17023SJohn Marino 	    }
866*e4b17023SJohn Marino 
867*e4b17023SJohn Marino 	  add_phi_arg (phi, def, s, locus);
868*e4b17023SJohn Marino 	}
869*e4b17023SJohn Marino 
870*e4b17023SJohn Marino       redirect_edge_var_map_clear (e);
871*e4b17023SJohn Marino     }
872*e4b17023SJohn Marino 
873*e4b17023SJohn Marino   /* Update the dominators.  */
874*e4b17023SJohn Marino   dombb = get_immediate_dominator (CDI_DOMINATORS, bb);
875*e4b17023SJohn Marino   domdest = get_immediate_dominator (CDI_DOMINATORS, dest);
876*e4b17023SJohn Marino   if (domdest == bb)
877*e4b17023SJohn Marino     {
878*e4b17023SJohn Marino       /* Shortcut to avoid calling (relatively expensive)
879*e4b17023SJohn Marino 	 nearest_common_dominator unless necessary.  */
880*e4b17023SJohn Marino       dom = dombb;
881*e4b17023SJohn Marino     }
882*e4b17023SJohn Marino   else
883*e4b17023SJohn Marino     dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb);
884*e4b17023SJohn Marino 
885*e4b17023SJohn Marino   set_immediate_dominator (CDI_DOMINATORS, dest, dom);
886*e4b17023SJohn Marino 
887*e4b17023SJohn Marino   /* Remove BB since all of BB's incoming edges have been redirected
888*e4b17023SJohn Marino      to DEST.  */
889*e4b17023SJohn Marino   delete_basic_block (bb);
890*e4b17023SJohn Marino }
891*e4b17023SJohn Marino 
892*e4b17023SJohn Marino /* This pass merges PHI nodes if one feeds into another.  For example,
893*e4b17023SJohn Marino    suppose we have the following:
894*e4b17023SJohn Marino 
895*e4b17023SJohn Marino   goto <bb 9> (<L9>);
896*e4b17023SJohn Marino 
897*e4b17023SJohn Marino <L8>:;
898*e4b17023SJohn Marino   tem_17 = foo ();
899*e4b17023SJohn Marino 
900*e4b17023SJohn Marino   # tem_6 = PHI <tem_17(8), tem_23(7)>;
901*e4b17023SJohn Marino <L9>:;
902*e4b17023SJohn Marino 
903*e4b17023SJohn Marino   # tem_3 = PHI <tem_6(9), tem_2(5)>;
904*e4b17023SJohn Marino <L10>:;
905*e4b17023SJohn Marino 
906*e4b17023SJohn Marino   Then we merge the first PHI node into the second one like so:
907*e4b17023SJohn Marino 
908*e4b17023SJohn Marino   goto <bb 9> (<L10>);
909*e4b17023SJohn Marino 
910*e4b17023SJohn Marino <L8>:;
911*e4b17023SJohn Marino   tem_17 = foo ();
912*e4b17023SJohn Marino 
913*e4b17023SJohn Marino   # tem_3 = PHI <tem_23(7), tem_2(5), tem_17(8)>;
914*e4b17023SJohn Marino <L10>:;
915*e4b17023SJohn Marino */
916*e4b17023SJohn Marino 
917*e4b17023SJohn Marino static unsigned int
merge_phi_nodes(void)918*e4b17023SJohn Marino merge_phi_nodes (void)
919*e4b17023SJohn Marino {
920*e4b17023SJohn Marino   basic_block *worklist = XNEWVEC (basic_block, n_basic_blocks);
921*e4b17023SJohn Marino   basic_block *current = worklist;
922*e4b17023SJohn Marino   basic_block bb;
923*e4b17023SJohn Marino 
924*e4b17023SJohn Marino   calculate_dominance_info (CDI_DOMINATORS);
925*e4b17023SJohn Marino 
926*e4b17023SJohn Marino   /* Find all PHI nodes that we may be able to merge.  */
927*e4b17023SJohn Marino   FOR_EACH_BB (bb)
928*e4b17023SJohn Marino     {
929*e4b17023SJohn Marino       basic_block dest;
930*e4b17023SJohn Marino 
931*e4b17023SJohn Marino       /* Look for a forwarder block with PHI nodes.  */
932*e4b17023SJohn Marino       if (!tree_forwarder_block_p (bb, true))
933*e4b17023SJohn Marino 	continue;
934*e4b17023SJohn Marino 
935*e4b17023SJohn Marino       dest = single_succ (bb);
936*e4b17023SJohn Marino 
937*e4b17023SJohn Marino       /* We have to feed into another basic block with PHI
938*e4b17023SJohn Marino 	 nodes.  */
939*e4b17023SJohn Marino       if (gimple_seq_empty_p (phi_nodes (dest))
940*e4b17023SJohn Marino 	  /* We don't want to deal with a basic block with
941*e4b17023SJohn Marino 	     abnormal edges.  */
942*e4b17023SJohn Marino 	  || bb_has_abnormal_pred (bb))
943*e4b17023SJohn Marino 	continue;
944*e4b17023SJohn Marino 
945*e4b17023SJohn Marino       if (!dominated_by_p (CDI_DOMINATORS, dest, bb))
946*e4b17023SJohn Marino 	{
947*e4b17023SJohn Marino 	  /* If BB does not dominate DEST, then the PHI nodes at
948*e4b17023SJohn Marino 	     DEST must be the only users of the results of the PHI
949*e4b17023SJohn Marino 	     nodes at BB.  */
950*e4b17023SJohn Marino 	  *current++ = bb;
951*e4b17023SJohn Marino 	}
952*e4b17023SJohn Marino       else
953*e4b17023SJohn Marino 	{
954*e4b17023SJohn Marino 	  gimple_stmt_iterator gsi;
955*e4b17023SJohn Marino 	  unsigned int dest_idx = single_succ_edge (bb)->dest_idx;
956*e4b17023SJohn Marino 
957*e4b17023SJohn Marino 	  /* BB dominates DEST.  There may be many users of the PHI
958*e4b17023SJohn Marino 	     nodes in BB.  However, there is still a trivial case we
959*e4b17023SJohn Marino 	     can handle.  If the result of every PHI in BB is used
960*e4b17023SJohn Marino 	     only by a PHI in DEST, then we can trivially merge the
961*e4b17023SJohn Marino 	     PHI nodes from BB into DEST.  */
962*e4b17023SJohn Marino 	  for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
963*e4b17023SJohn Marino 	       gsi_next (&gsi))
964*e4b17023SJohn Marino 	    {
965*e4b17023SJohn Marino 	      gimple phi = gsi_stmt (gsi);
966*e4b17023SJohn Marino 	      tree result = gimple_phi_result (phi);
967*e4b17023SJohn Marino 	      use_operand_p imm_use;
968*e4b17023SJohn Marino 	      gimple use_stmt;
969*e4b17023SJohn Marino 
970*e4b17023SJohn Marino 	      /* If the PHI's result is never used, then we can just
971*e4b17023SJohn Marino 		 ignore it.  */
972*e4b17023SJohn Marino 	      if (has_zero_uses (result))
973*e4b17023SJohn Marino 		continue;
974*e4b17023SJohn Marino 
975*e4b17023SJohn Marino 	      /* Get the single use of the result of this PHI node.  */
976*e4b17023SJohn Marino   	      if (!single_imm_use (result, &imm_use, &use_stmt)
977*e4b17023SJohn Marino 		  || gimple_code (use_stmt) != GIMPLE_PHI
978*e4b17023SJohn Marino 		  || gimple_bb (use_stmt) != dest
979*e4b17023SJohn Marino 		  || gimple_phi_arg_def (use_stmt, dest_idx) != result)
980*e4b17023SJohn Marino 		break;
981*e4b17023SJohn Marino 	    }
982*e4b17023SJohn Marino 
983*e4b17023SJohn Marino 	  /* If the loop above iterated through all the PHI nodes
984*e4b17023SJohn Marino 	     in BB, then we can merge the PHIs from BB into DEST.  */
985*e4b17023SJohn Marino 	  if (gsi_end_p (gsi))
986*e4b17023SJohn Marino 	    *current++ = bb;
987*e4b17023SJohn Marino 	}
988*e4b17023SJohn Marino     }
989*e4b17023SJohn Marino 
990*e4b17023SJohn Marino   /* Now let's drain WORKLIST.  */
991*e4b17023SJohn Marino   while (current != worklist)
992*e4b17023SJohn Marino     {
993*e4b17023SJohn Marino       bb = *--current;
994*e4b17023SJohn Marino       remove_forwarder_block_with_phi (bb);
995*e4b17023SJohn Marino     }
996*e4b17023SJohn Marino 
997*e4b17023SJohn Marino   free (worklist);
998*e4b17023SJohn Marino   return 0;
999*e4b17023SJohn Marino }
1000*e4b17023SJohn Marino 
1001*e4b17023SJohn Marino static bool
gate_merge_phi(void)1002*e4b17023SJohn Marino gate_merge_phi (void)
1003*e4b17023SJohn Marino {
1004*e4b17023SJohn Marino   return 1;
1005*e4b17023SJohn Marino }
1006*e4b17023SJohn Marino 
1007*e4b17023SJohn Marino struct gimple_opt_pass pass_merge_phi =
1008*e4b17023SJohn Marino {
1009*e4b17023SJohn Marino  {
1010*e4b17023SJohn Marino   GIMPLE_PASS,
1011*e4b17023SJohn Marino   "mergephi",			/* name */
1012*e4b17023SJohn Marino   gate_merge_phi,		/* gate */
1013*e4b17023SJohn Marino   merge_phi_nodes,		/* execute */
1014*e4b17023SJohn Marino   NULL,				/* sub */
1015*e4b17023SJohn Marino   NULL,				/* next */
1016*e4b17023SJohn Marino   0,				/* static_pass_number */
1017*e4b17023SJohn Marino   TV_TREE_MERGE_PHI,		/* tv_id */
1018*e4b17023SJohn Marino   PROP_cfg | PROP_ssa,		/* properties_required */
1019*e4b17023SJohn Marino   0,				/* properties_provided */
1020*e4b17023SJohn Marino   0,				/* properties_destroyed */
1021*e4b17023SJohn Marino   0,				/* todo_flags_start */
1022*e4b17023SJohn Marino   TODO_ggc_collect      	/* todo_flags_finish */
1023*e4b17023SJohn Marino   | TODO_verify_ssa
1024*e4b17023SJohn Marino  }
1025*e4b17023SJohn Marino };
1026