xref: /dragonfly/contrib/gcc-8.0/gcc/ipa-split.c (revision 38fd1498)
1*38fd1498Szrj /* Function splitting pass
2*38fd1498Szrj    Copyright (C) 2010-2018 Free Software Foundation, Inc.
3*38fd1498Szrj    Contributed by Jan Hubicka  <jh@suse.cz>
4*38fd1498Szrj 
5*38fd1498Szrj This file is part of GCC.
6*38fd1498Szrj 
7*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
8*38fd1498Szrj the terms of the GNU General Public License as published by the Free
9*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
10*38fd1498Szrj version.
11*38fd1498Szrj 
12*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
14*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15*38fd1498Szrj for more details.
16*38fd1498Szrj 
17*38fd1498Szrj You should have received a copy of the GNU General Public License
18*38fd1498Szrj along with GCC; see the file COPYING3.  If not see
19*38fd1498Szrj <http://www.gnu.org/licenses/>.  */
20*38fd1498Szrj 
21*38fd1498Szrj /* The purpose of this pass is to split function bodies to improve
22*38fd1498Szrj    inlining.  I.e. for function of the form:
23*38fd1498Szrj 
24*38fd1498Szrj    func (...)
25*38fd1498Szrj      {
26*38fd1498Szrj        if (cheap_test)
27*38fd1498Szrj 	 something_small
28*38fd1498Szrj        else
29*38fd1498Szrj 	 something_big
30*38fd1498Szrj      }
31*38fd1498Szrj 
32*38fd1498Szrj    Produce:
33*38fd1498Szrj 
34*38fd1498Szrj    func.part (...)
35*38fd1498Szrj      {
36*38fd1498Szrj 	something_big
37*38fd1498Szrj      }
38*38fd1498Szrj 
39*38fd1498Szrj    func (...)
40*38fd1498Szrj      {
41*38fd1498Szrj        if (cheap_test)
42*38fd1498Szrj 	 something_small
43*38fd1498Szrj        else
44*38fd1498Szrj 	 func.part (...);
45*38fd1498Szrj      }
46*38fd1498Szrj 
47*38fd1498Szrj    When func becomes inlinable and when cheap_test is often true, inlining func,
48*38fd1498Szrj    but not fund.part leads to performance improvement similar as inlining
49*38fd1498Szrj    original func while the code size growth is smaller.
50*38fd1498Szrj 
51*38fd1498Szrj    The pass is organized in three stages:
52*38fd1498Szrj    1) Collect local info about basic block into BB_INFO structure and
53*38fd1498Szrj       compute function body estimated size and time.
54*38fd1498Szrj    2) Via DFS walk find all possible basic blocks where we can split
55*38fd1498Szrj       and chose best one.
56*38fd1498Szrj    3) If split point is found, split at the specified BB by creating a clone
57*38fd1498Szrj       and updating function to call it.
58*38fd1498Szrj 
59*38fd1498Szrj    The decisions what functions to split are in execute_split_functions
60*38fd1498Szrj    and consider_split.
61*38fd1498Szrj 
62*38fd1498Szrj    There are several possible future improvements for this pass including:
63*38fd1498Szrj 
64*38fd1498Szrj    1) Splitting to break up large functions
65*38fd1498Szrj    2) Splitting to reduce stack frame usage
66*38fd1498Szrj    3) Allow split part of function to use values computed in the header part.
67*38fd1498Szrj       The values needs to be passed to split function, perhaps via same
68*38fd1498Szrj       interface as for nested functions or as argument.
69*38fd1498Szrj    4) Support for simple rematerialization.  I.e. when split part use
70*38fd1498Szrj       value computed in header from function parameter in very cheap way, we
71*38fd1498Szrj       can just recompute it.
72*38fd1498Szrj    5) Support splitting of nested functions.
73*38fd1498Szrj    6) Support non-SSA arguments.
74*38fd1498Szrj    7) There is nothing preventing us from producing multiple parts of single function
75*38fd1498Szrj       when needed or splitting also the parts.  */
76*38fd1498Szrj 
77*38fd1498Szrj #include "config.h"
78*38fd1498Szrj #include "system.h"
79*38fd1498Szrj #include "coretypes.h"
80*38fd1498Szrj #include "backend.h"
81*38fd1498Szrj #include "rtl.h"
82*38fd1498Szrj #include "tree.h"
83*38fd1498Szrj #include "gimple.h"
84*38fd1498Szrj #include "cfghooks.h"
85*38fd1498Szrj #include "alloc-pool.h"
86*38fd1498Szrj #include "tree-pass.h"
87*38fd1498Szrj #include "ssa.h"
88*38fd1498Szrj #include "cgraph.h"
89*38fd1498Szrj #include "diagnostic.h"
90*38fd1498Szrj #include "fold-const.h"
91*38fd1498Szrj #include "cfganal.h"
92*38fd1498Szrj #include "calls.h"
93*38fd1498Szrj #include "gimplify.h"
94*38fd1498Szrj #include "gimple-iterator.h"
95*38fd1498Szrj #include "gimplify-me.h"
96*38fd1498Szrj #include "gimple-walk.h"
97*38fd1498Szrj #include "symbol-summary.h"
98*38fd1498Szrj #include "ipa-prop.h"
99*38fd1498Szrj #include "tree-cfg.h"
100*38fd1498Szrj #include "tree-into-ssa.h"
101*38fd1498Szrj #include "tree-dfa.h"
102*38fd1498Szrj #include "tree-inline.h"
103*38fd1498Szrj #include "params.h"
104*38fd1498Szrj #include "gimple-pretty-print.h"
105*38fd1498Szrj #include "ipa-fnsummary.h"
106*38fd1498Szrj #include "cfgloop.h"
107*38fd1498Szrj #include "tree-chkp.h"
108*38fd1498Szrj 
109*38fd1498Szrj /* Per basic block info.  */
110*38fd1498Szrj 
111*38fd1498Szrj struct split_bb_info
112*38fd1498Szrj {
113*38fd1498Szrj   unsigned int size;
114*38fd1498Szrj   sreal time;
115*38fd1498Szrj };
116*38fd1498Szrj 
117*38fd1498Szrj static vec<split_bb_info> bb_info_vec;
118*38fd1498Szrj 
119*38fd1498Szrj /* Description of split point.  */
120*38fd1498Szrj 
121*38fd1498Szrj struct split_point
122*38fd1498Szrj {
123*38fd1498Szrj   /* Size of the partitions.  */
124*38fd1498Szrj   sreal header_time, split_time;
125*38fd1498Szrj   unsigned int header_size, split_size;
126*38fd1498Szrj 
127*38fd1498Szrj   /* SSA names that need to be passed into spit function.  */
128*38fd1498Szrj   bitmap ssa_names_to_pass;
129*38fd1498Szrj 
130*38fd1498Szrj   /* Basic block where we split (that will become entry point of new function.  */
131*38fd1498Szrj   basic_block entry_bb;
132*38fd1498Szrj 
133*38fd1498Szrj   /* Count for entering the split part.
134*38fd1498Szrj      This is not count of the entry_bb because it may be in loop.  */
135*38fd1498Szrj   profile_count count;
136*38fd1498Szrj 
137*38fd1498Szrj   /* Basic blocks we are splitting away.  */
138*38fd1498Szrj   bitmap split_bbs;
139*38fd1498Szrj 
140*38fd1498Szrj   /* True when return value is computed on split part and thus it needs
141*38fd1498Szrj      to be returned.  */
142*38fd1498Szrj   bool split_part_set_retval;
143*38fd1498Szrj };
144*38fd1498Szrj 
145*38fd1498Szrj /* Best split point found.  */
146*38fd1498Szrj 
147*38fd1498Szrj struct split_point best_split_point;
148*38fd1498Szrj 
149*38fd1498Szrj /* Set of basic blocks that are not allowed to dominate a split point.  */
150*38fd1498Szrj 
151*38fd1498Szrj static bitmap forbidden_dominators;
152*38fd1498Szrj 
153*38fd1498Szrj static tree find_retval (basic_block return_bb);
154*38fd1498Szrj static tree find_retbnd (basic_block return_bb);
155*38fd1498Szrj 
156*38fd1498Szrj /* Callback for walk_stmt_load_store_addr_ops.  If T is non-SSA automatic
157*38fd1498Szrj    variable, check it if it is present in bitmap passed via DATA.  */
158*38fd1498Szrj 
159*38fd1498Szrj static bool
test_nonssa_use(gimple *,tree t,tree,void * data)160*38fd1498Szrj test_nonssa_use (gimple *, tree t, tree, void *data)
161*38fd1498Szrj {
162*38fd1498Szrj   t = get_base_address (t);
163*38fd1498Szrj 
164*38fd1498Szrj   if (!t || is_gimple_reg (t))
165*38fd1498Szrj     return false;
166*38fd1498Szrj 
167*38fd1498Szrj   if (TREE_CODE (t) == PARM_DECL
168*38fd1498Szrj       || (VAR_P (t)
169*38fd1498Szrj 	  && auto_var_in_fn_p (t, current_function_decl))
170*38fd1498Szrj       || TREE_CODE (t) == RESULT_DECL
171*38fd1498Szrj 	 /* Normal labels are part of CFG and will be handled gratefuly.
172*38fd1498Szrj 	    Forced labels however can be used directly by statements and
173*38fd1498Szrj 	    need to stay in one partition along with their uses.  */
174*38fd1498Szrj       || (TREE_CODE (t) == LABEL_DECL
175*38fd1498Szrj 	  && FORCED_LABEL (t)))
176*38fd1498Szrj     return bitmap_bit_p ((bitmap)data, DECL_UID (t));
177*38fd1498Szrj 
178*38fd1498Szrj   /* For DECL_BY_REFERENCE, the return value is actually a pointer.  We want
179*38fd1498Szrj      to pretend that the value pointed to is actual result decl.  */
180*38fd1498Szrj   if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
181*38fd1498Szrj       && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
182*38fd1498Szrj       && SSA_NAME_VAR (TREE_OPERAND (t, 0))
183*38fd1498Szrj       && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
184*38fd1498Szrj       && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
185*38fd1498Szrj     return
186*38fd1498Szrj       bitmap_bit_p ((bitmap)data,
187*38fd1498Szrj 		    DECL_UID (DECL_RESULT (current_function_decl)));
188*38fd1498Szrj 
189*38fd1498Szrj   return false;
190*38fd1498Szrj }
191*38fd1498Szrj 
192*38fd1498Szrj /* Dump split point CURRENT.  */
193*38fd1498Szrj 
194*38fd1498Szrj static void
dump_split_point(FILE * file,struct split_point * current)195*38fd1498Szrj dump_split_point (FILE * file, struct split_point *current)
196*38fd1498Szrj {
197*38fd1498Szrj   fprintf (file,
198*38fd1498Szrj 	   "Split point at BB %i\n"
199*38fd1498Szrj 	   "  header time: %f header size: %i\n"
200*38fd1498Szrj 	   "  split time: %f split size: %i\n  bbs: ",
201*38fd1498Szrj 	   current->entry_bb->index, current->header_time.to_double (),
202*38fd1498Szrj 	   current->header_size, current->split_time.to_double (),
203*38fd1498Szrj 	   current->split_size);
204*38fd1498Szrj   dump_bitmap (file, current->split_bbs);
205*38fd1498Szrj   fprintf (file, "  SSA names to pass: ");
206*38fd1498Szrj   dump_bitmap (file, current->ssa_names_to_pass);
207*38fd1498Szrj }
208*38fd1498Szrj 
209*38fd1498Szrj /* Look for all BBs in header that might lead to the split part and verify
210*38fd1498Szrj    that they are not defining any non-SSA var used by the split part.
211*38fd1498Szrj    Parameters are the same as for consider_split.  */
212*38fd1498Szrj 
213*38fd1498Szrj static bool
verify_non_ssa_vars(struct split_point * current,bitmap non_ssa_vars,basic_block return_bb)214*38fd1498Szrj verify_non_ssa_vars (struct split_point *current, bitmap non_ssa_vars,
215*38fd1498Szrj 		     basic_block return_bb)
216*38fd1498Szrj {
217*38fd1498Szrj   bitmap seen = BITMAP_ALLOC (NULL);
218*38fd1498Szrj   vec<basic_block> worklist = vNULL;
219*38fd1498Szrj   edge e;
220*38fd1498Szrj   edge_iterator ei;
221*38fd1498Szrj   bool ok = true;
222*38fd1498Szrj   basic_block bb;
223*38fd1498Szrj 
224*38fd1498Szrj   FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
225*38fd1498Szrj     if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
226*38fd1498Szrj 	&& !bitmap_bit_p (current->split_bbs, e->src->index))
227*38fd1498Szrj       {
228*38fd1498Szrj         worklist.safe_push (e->src);
229*38fd1498Szrj 	bitmap_set_bit (seen, e->src->index);
230*38fd1498Szrj       }
231*38fd1498Szrj 
232*38fd1498Szrj   while (!worklist.is_empty ())
233*38fd1498Szrj     {
234*38fd1498Szrj       bb = worklist.pop ();
235*38fd1498Szrj       FOR_EACH_EDGE (e, ei, bb->preds)
236*38fd1498Szrj 	if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
237*38fd1498Szrj 	    && bitmap_set_bit (seen, e->src->index))
238*38fd1498Szrj 	  {
239*38fd1498Szrj 	    gcc_checking_assert (!bitmap_bit_p (current->split_bbs,
240*38fd1498Szrj 					        e->src->index));
241*38fd1498Szrj 	    worklist.safe_push (e->src);
242*38fd1498Szrj 	  }
243*38fd1498Szrj       for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
244*38fd1498Szrj 	   gsi_next (&bsi))
245*38fd1498Szrj 	{
246*38fd1498Szrj 	  gimple *stmt = gsi_stmt (bsi);
247*38fd1498Szrj 	  if (is_gimple_debug (stmt))
248*38fd1498Szrj 	    continue;
249*38fd1498Szrj 	  if (walk_stmt_load_store_addr_ops
250*38fd1498Szrj 	      (stmt, non_ssa_vars, test_nonssa_use, test_nonssa_use,
251*38fd1498Szrj 	       test_nonssa_use))
252*38fd1498Szrj 	    {
253*38fd1498Szrj 	      ok = false;
254*38fd1498Szrj 	      goto done;
255*38fd1498Szrj 	    }
256*38fd1498Szrj 	  if (glabel *label_stmt = dyn_cast <glabel *> (stmt))
257*38fd1498Szrj 	    if (test_nonssa_use (stmt, gimple_label_label (label_stmt),
258*38fd1498Szrj 				 NULL_TREE, non_ssa_vars))
259*38fd1498Szrj 	      {
260*38fd1498Szrj 		ok = false;
261*38fd1498Szrj 		goto done;
262*38fd1498Szrj 	      }
263*38fd1498Szrj 	}
264*38fd1498Szrj       for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
265*38fd1498Szrj 	   gsi_next (&bsi))
266*38fd1498Szrj 	{
267*38fd1498Szrj 	  if (walk_stmt_load_store_addr_ops
268*38fd1498Szrj 	      (gsi_stmt (bsi), non_ssa_vars, test_nonssa_use, test_nonssa_use,
269*38fd1498Szrj 	       test_nonssa_use))
270*38fd1498Szrj 	    {
271*38fd1498Szrj 	      ok = false;
272*38fd1498Szrj 	      goto done;
273*38fd1498Szrj 	    }
274*38fd1498Szrj 	}
275*38fd1498Szrj       FOR_EACH_EDGE (e, ei, bb->succs)
276*38fd1498Szrj 	{
277*38fd1498Szrj 	  if (e->dest != return_bb)
278*38fd1498Szrj 	    continue;
279*38fd1498Szrj 	  for (gphi_iterator bsi = gsi_start_phis (return_bb);
280*38fd1498Szrj 	       !gsi_end_p (bsi);
281*38fd1498Szrj 	       gsi_next (&bsi))
282*38fd1498Szrj 	    {
283*38fd1498Szrj 	      gphi *stmt = bsi.phi ();
284*38fd1498Szrj 	      tree op = gimple_phi_arg_def (stmt, e->dest_idx);
285*38fd1498Szrj 
286*38fd1498Szrj 	      if (virtual_operand_p (gimple_phi_result (stmt)))
287*38fd1498Szrj 		continue;
288*38fd1498Szrj 	      if (TREE_CODE (op) != SSA_NAME
289*38fd1498Szrj 		  && test_nonssa_use (stmt, op, op, non_ssa_vars))
290*38fd1498Szrj 		{
291*38fd1498Szrj 		  ok = false;
292*38fd1498Szrj 		  goto done;
293*38fd1498Szrj 		}
294*38fd1498Szrj 	    }
295*38fd1498Szrj 	}
296*38fd1498Szrj     }
297*38fd1498Szrj 
298*38fd1498Szrj   /* Verify that the rest of function does not define any label
299*38fd1498Szrj      used by the split part.  */
300*38fd1498Szrj   FOR_EACH_BB_FN (bb, cfun)
301*38fd1498Szrj     if (!bitmap_bit_p (current->split_bbs, bb->index)
302*38fd1498Szrj 	&& !bitmap_bit_p (seen, bb->index))
303*38fd1498Szrj       {
304*38fd1498Szrj         gimple_stmt_iterator bsi;
305*38fd1498Szrj         for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
306*38fd1498Szrj 	  if (glabel *label_stmt = dyn_cast <glabel *> (gsi_stmt (bsi)))
307*38fd1498Szrj 	    {
308*38fd1498Szrj 	      if (test_nonssa_use (label_stmt,
309*38fd1498Szrj 				   gimple_label_label (label_stmt),
310*38fd1498Szrj 				   NULL_TREE, non_ssa_vars))
311*38fd1498Szrj 		{
312*38fd1498Szrj 		  ok = false;
313*38fd1498Szrj 		  goto done;
314*38fd1498Szrj 		}
315*38fd1498Szrj 	    }
316*38fd1498Szrj 	  else
317*38fd1498Szrj 	    break;
318*38fd1498Szrj       }
319*38fd1498Szrj 
320*38fd1498Szrj done:
321*38fd1498Szrj   BITMAP_FREE (seen);
322*38fd1498Szrj   worklist.release ();
323*38fd1498Szrj   return ok;
324*38fd1498Szrj }
325*38fd1498Szrj 
326*38fd1498Szrj /* If STMT is a call, check the callee against a list of forbidden
327*38fd1498Szrj    predicate functions.  If a match is found, look for uses of the
328*38fd1498Szrj    call result in condition statements that compare against zero.
329*38fd1498Szrj    For each such use, find the block targeted by the condition
330*38fd1498Szrj    statement for the nonzero result, and set the bit for this block
331*38fd1498Szrj    in the forbidden dominators bitmap.  The purpose of this is to avoid
332*38fd1498Szrj    selecting a split point where we are likely to lose the chance
333*38fd1498Szrj    to optimize away an unused function call.  */
334*38fd1498Szrj 
335*38fd1498Szrj static void
check_forbidden_calls(gimple * stmt)336*38fd1498Szrj check_forbidden_calls (gimple *stmt)
337*38fd1498Szrj {
338*38fd1498Szrj   imm_use_iterator use_iter;
339*38fd1498Szrj   use_operand_p use_p;
340*38fd1498Szrj   tree lhs;
341*38fd1498Szrj 
342*38fd1498Szrj   /* At the moment, __builtin_constant_p is the only forbidden
343*38fd1498Szrj      predicate function call (see PR49642).  */
344*38fd1498Szrj   if (!gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P))
345*38fd1498Szrj     return;
346*38fd1498Szrj 
347*38fd1498Szrj   lhs = gimple_call_lhs (stmt);
348*38fd1498Szrj 
349*38fd1498Szrj   if (!lhs || TREE_CODE (lhs) != SSA_NAME)
350*38fd1498Szrj     return;
351*38fd1498Szrj 
352*38fd1498Szrj   FOR_EACH_IMM_USE_FAST (use_p, use_iter, lhs)
353*38fd1498Szrj     {
354*38fd1498Szrj       tree op1;
355*38fd1498Szrj       basic_block use_bb, forbidden_bb;
356*38fd1498Szrj       enum tree_code code;
357*38fd1498Szrj       edge true_edge, false_edge;
358*38fd1498Szrj       gcond *use_stmt;
359*38fd1498Szrj 
360*38fd1498Szrj       use_stmt = dyn_cast <gcond *> (USE_STMT (use_p));
361*38fd1498Szrj       if (!use_stmt)
362*38fd1498Szrj 	continue;
363*38fd1498Szrj 
364*38fd1498Szrj       /* Assuming canonical form for GIMPLE_COND here, with constant
365*38fd1498Szrj 	 in second position.  */
366*38fd1498Szrj       op1 = gimple_cond_rhs (use_stmt);
367*38fd1498Szrj       code = gimple_cond_code (use_stmt);
368*38fd1498Szrj       use_bb = gimple_bb (use_stmt);
369*38fd1498Szrj 
370*38fd1498Szrj       extract_true_false_edges_from_block (use_bb, &true_edge, &false_edge);
371*38fd1498Szrj 
372*38fd1498Szrj       /* We're only interested in comparisons that distinguish
373*38fd1498Szrj 	 unambiguously from zero.  */
374*38fd1498Szrj       if (!integer_zerop (op1) || code == LE_EXPR || code == GE_EXPR)
375*38fd1498Szrj 	continue;
376*38fd1498Szrj 
377*38fd1498Szrj       if (code == EQ_EXPR)
378*38fd1498Szrj 	forbidden_bb = false_edge->dest;
379*38fd1498Szrj       else
380*38fd1498Szrj 	forbidden_bb = true_edge->dest;
381*38fd1498Szrj 
382*38fd1498Szrj       bitmap_set_bit (forbidden_dominators, forbidden_bb->index);
383*38fd1498Szrj     }
384*38fd1498Szrj }
385*38fd1498Szrj 
386*38fd1498Szrj /* If BB is dominated by any block in the forbidden dominators set,
387*38fd1498Szrj    return TRUE; else FALSE.  */
388*38fd1498Szrj 
389*38fd1498Szrj static bool
dominated_by_forbidden(basic_block bb)390*38fd1498Szrj dominated_by_forbidden (basic_block bb)
391*38fd1498Szrj {
392*38fd1498Szrj   unsigned dom_bb;
393*38fd1498Szrj   bitmap_iterator bi;
394*38fd1498Szrj 
395*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (forbidden_dominators, 1, dom_bb, bi)
396*38fd1498Szrj     {
397*38fd1498Szrj       if (dominated_by_p (CDI_DOMINATORS, bb,
398*38fd1498Szrj 			  BASIC_BLOCK_FOR_FN (cfun, dom_bb)))
399*38fd1498Szrj 	return true;
400*38fd1498Szrj     }
401*38fd1498Szrj 
402*38fd1498Szrj   return false;
403*38fd1498Szrj }
404*38fd1498Szrj 
405*38fd1498Szrj /* For give split point CURRENT and return block RETURN_BB return 1
406*38fd1498Szrj    if ssa name VAL is set by split part and 0 otherwise.  */
407*38fd1498Szrj static bool
split_part_set_ssa_name_p(tree val,struct split_point * current,basic_block return_bb)408*38fd1498Szrj split_part_set_ssa_name_p (tree val, struct split_point *current,
409*38fd1498Szrj 			   basic_block return_bb)
410*38fd1498Szrj {
411*38fd1498Szrj   if (TREE_CODE (val) != SSA_NAME)
412*38fd1498Szrj     return false;
413*38fd1498Szrj 
414*38fd1498Szrj   return (!SSA_NAME_IS_DEFAULT_DEF (val)
415*38fd1498Szrj 	  && (bitmap_bit_p (current->split_bbs,
416*38fd1498Szrj 			    gimple_bb (SSA_NAME_DEF_STMT (val))->index)
417*38fd1498Szrj 	      || gimple_bb (SSA_NAME_DEF_STMT (val)) == return_bb));
418*38fd1498Szrj }
419*38fd1498Szrj 
420*38fd1498Szrj /* We found an split_point CURRENT.  NON_SSA_VARS is bitmap of all non ssa
421*38fd1498Szrj    variables used and RETURN_BB is return basic block.
422*38fd1498Szrj    See if we can split function here.  */
423*38fd1498Szrj 
424*38fd1498Szrj static void
consider_split(struct split_point * current,bitmap non_ssa_vars,basic_block return_bb)425*38fd1498Szrj consider_split (struct split_point *current, bitmap non_ssa_vars,
426*38fd1498Szrj 		basic_block return_bb)
427*38fd1498Szrj {
428*38fd1498Szrj   tree parm;
429*38fd1498Szrj   unsigned int num_args = 0;
430*38fd1498Szrj   unsigned int call_overhead;
431*38fd1498Szrj   edge e;
432*38fd1498Szrj   edge_iterator ei;
433*38fd1498Szrj   gphi_iterator bsi;
434*38fd1498Szrj   unsigned int i;
435*38fd1498Szrj   tree retval;
436*38fd1498Szrj   tree retbnd;
437*38fd1498Szrj   bool back_edge = false;
438*38fd1498Szrj 
439*38fd1498Szrj   if (dump_file && (dump_flags & TDF_DETAILS))
440*38fd1498Szrj     dump_split_point (dump_file, current);
441*38fd1498Szrj 
442*38fd1498Szrj   current->count = profile_count::zero ();
443*38fd1498Szrj   FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
444*38fd1498Szrj     {
445*38fd1498Szrj       if (e->flags & EDGE_DFS_BACK)
446*38fd1498Szrj 	back_edge = true;
447*38fd1498Szrj       if (!bitmap_bit_p (current->split_bbs, e->src->index))
448*38fd1498Szrj 	current->count += e->count ();
449*38fd1498Szrj     }
450*38fd1498Szrj 
451*38fd1498Szrj   /* Do not split when we would end up calling function anyway.
452*38fd1498Szrj      Compares are three state, use !(...<...) to also give up when outcome
453*38fd1498Szrj      is unknown.  */
454*38fd1498Szrj   if (!(current->count
455*38fd1498Szrj        < (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.apply_scale
456*38fd1498Szrj 	   (PARAM_VALUE (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY), 100))))
457*38fd1498Szrj     {
458*38fd1498Szrj       /* When profile is guessed, we can not expect it to give us
459*38fd1498Szrj 	 realistic estimate on likelyness of function taking the
460*38fd1498Szrj 	 complex path.  As a special case, when tail of the function is
461*38fd1498Szrj 	 a loop, enable splitting since inlining code skipping the loop
462*38fd1498Szrj 	 is likely noticeable win.  */
463*38fd1498Szrj       if (back_edge
464*38fd1498Szrj 	  && profile_status_for_fn (cfun) != PROFILE_READ
465*38fd1498Szrj 	  && current->count
466*38fd1498Szrj 		 < ENTRY_BLOCK_PTR_FOR_FN (cfun)->count)
467*38fd1498Szrj 	{
468*38fd1498Szrj 	  if (dump_file && (dump_flags & TDF_DETAILS))
469*38fd1498Szrj 	    {
470*38fd1498Szrj 	      fprintf (dump_file,
471*38fd1498Szrj 		       "  Split before loop, accepting despite low counts");
472*38fd1498Szrj 	      current->count.dump (dump_file);
473*38fd1498Szrj 	      fprintf (dump_file, " ");
474*38fd1498Szrj 	      ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.dump (dump_file);
475*38fd1498Szrj 	    }
476*38fd1498Szrj 	}
477*38fd1498Szrj       else
478*38fd1498Szrj 	{
479*38fd1498Szrj 	  if (dump_file && (dump_flags & TDF_DETAILS))
480*38fd1498Szrj 	    fprintf (dump_file,
481*38fd1498Szrj 		     "  Refused: incoming frequency is too large.\n");
482*38fd1498Szrj 	  return;
483*38fd1498Szrj 	}
484*38fd1498Szrj     }
485*38fd1498Szrj 
486*38fd1498Szrj   if (!current->header_size)
487*38fd1498Szrj     {
488*38fd1498Szrj       if (dump_file && (dump_flags & TDF_DETAILS))
489*38fd1498Szrj 	fprintf (dump_file, "  Refused: header empty\n");
490*38fd1498Szrj       return;
491*38fd1498Szrj     }
492*38fd1498Szrj 
493*38fd1498Szrj   /* Verify that PHI args on entry are either virtual or all their operands
494*38fd1498Szrj      incoming from header are the same.  */
495*38fd1498Szrj   for (bsi = gsi_start_phis (current->entry_bb); !gsi_end_p (bsi); gsi_next (&bsi))
496*38fd1498Szrj     {
497*38fd1498Szrj       gphi *stmt = bsi.phi ();
498*38fd1498Szrj       tree val = NULL;
499*38fd1498Szrj 
500*38fd1498Szrj       if (virtual_operand_p (gimple_phi_result (stmt)))
501*38fd1498Szrj 	continue;
502*38fd1498Szrj       for (i = 0; i < gimple_phi_num_args (stmt); i++)
503*38fd1498Szrj 	{
504*38fd1498Szrj 	  edge e = gimple_phi_arg_edge (stmt, i);
505*38fd1498Szrj 	  if (!bitmap_bit_p (current->split_bbs, e->src->index))
506*38fd1498Szrj 	    {
507*38fd1498Szrj 	      tree edge_val = gimple_phi_arg_def (stmt, i);
508*38fd1498Szrj 	      if (val && edge_val != val)
509*38fd1498Szrj 	        {
510*38fd1498Szrj 		  if (dump_file && (dump_flags & TDF_DETAILS))
511*38fd1498Szrj 		    fprintf (dump_file,
512*38fd1498Szrj 			     "  Refused: entry BB has PHI with multiple variants\n");
513*38fd1498Szrj 		  return;
514*38fd1498Szrj 	        }
515*38fd1498Szrj 	      val = edge_val;
516*38fd1498Szrj 	    }
517*38fd1498Szrj 	}
518*38fd1498Szrj     }
519*38fd1498Szrj 
520*38fd1498Szrj 
521*38fd1498Szrj   /* See what argument we will pass to the split function and compute
522*38fd1498Szrj      call overhead.  */
523*38fd1498Szrj   call_overhead = eni_size_weights.call_cost;
524*38fd1498Szrj   for (parm = DECL_ARGUMENTS (current_function_decl); parm;
525*38fd1498Szrj        parm = DECL_CHAIN (parm))
526*38fd1498Szrj     {
527*38fd1498Szrj       if (!is_gimple_reg (parm))
528*38fd1498Szrj 	{
529*38fd1498Szrj 	  if (bitmap_bit_p (non_ssa_vars, DECL_UID (parm)))
530*38fd1498Szrj 	    {
531*38fd1498Szrj 	      if (dump_file && (dump_flags & TDF_DETAILS))
532*38fd1498Szrj 		fprintf (dump_file,
533*38fd1498Szrj 			 "  Refused: need to pass non-ssa param values\n");
534*38fd1498Szrj 	      return;
535*38fd1498Szrj 	    }
536*38fd1498Szrj 	}
537*38fd1498Szrj       else
538*38fd1498Szrj 	{
539*38fd1498Szrj 	  tree ddef = ssa_default_def (cfun, parm);
540*38fd1498Szrj 	  if (ddef
541*38fd1498Szrj 	      && bitmap_bit_p (current->ssa_names_to_pass,
542*38fd1498Szrj 			       SSA_NAME_VERSION (ddef)))
543*38fd1498Szrj 	    {
544*38fd1498Szrj 	      if (!VOID_TYPE_P (TREE_TYPE (parm)))
545*38fd1498Szrj 		call_overhead += estimate_move_cost (TREE_TYPE (parm), false);
546*38fd1498Szrj 	      num_args++;
547*38fd1498Szrj 	    }
548*38fd1498Szrj 	}
549*38fd1498Szrj     }
550*38fd1498Szrj   if (!VOID_TYPE_P (TREE_TYPE (current_function_decl)))
551*38fd1498Szrj     call_overhead += estimate_move_cost (TREE_TYPE (current_function_decl),
552*38fd1498Szrj 					 false);
553*38fd1498Szrj 
554*38fd1498Szrj   if (current->split_size <= call_overhead)
555*38fd1498Szrj     {
556*38fd1498Szrj       if (dump_file && (dump_flags & TDF_DETAILS))
557*38fd1498Szrj 	fprintf (dump_file,
558*38fd1498Szrj 		 "  Refused: split size is smaller than call overhead\n");
559*38fd1498Szrj       return;
560*38fd1498Szrj     }
561*38fd1498Szrj   /* FIXME: The logic here is not very precise, because inliner does use
562*38fd1498Szrj      inline predicates to reduce function body size.  We add 10 to anticipate
563*38fd1498Szrj      that.  Next stage1 we should try to be more meaningful here.  */
564*38fd1498Szrj   if (current->header_size + call_overhead
565*38fd1498Szrj       >= (unsigned int)(DECL_DECLARED_INLINE_P (current_function_decl)
566*38fd1498Szrj 			? MAX_INLINE_INSNS_SINGLE
567*38fd1498Szrj 			: MAX_INLINE_INSNS_AUTO) + 10)
568*38fd1498Szrj     {
569*38fd1498Szrj       if (dump_file && (dump_flags & TDF_DETAILS))
570*38fd1498Szrj 	fprintf (dump_file,
571*38fd1498Szrj 		 "  Refused: header size is too large for inline candidate\n");
572*38fd1498Szrj       return;
573*38fd1498Szrj     }
574*38fd1498Szrj 
575*38fd1498Szrj   /* Splitting functions brings the target out of comdat group; this will
576*38fd1498Szrj      lead to code duplication if the function is reused by other unit.
577*38fd1498Szrj      Limit this duplication.  This is consistent with limit in tree-sra.c
578*38fd1498Szrj      FIXME: with LTO we ought to be able to do better!  */
579*38fd1498Szrj   if (DECL_ONE_ONLY (current_function_decl)
580*38fd1498Szrj       && current->split_size >= (unsigned int) MAX_INLINE_INSNS_AUTO + 10)
581*38fd1498Szrj     {
582*38fd1498Szrj       if (dump_file && (dump_flags & TDF_DETAILS))
583*38fd1498Szrj 	fprintf (dump_file,
584*38fd1498Szrj 		 "  Refused: function is COMDAT and tail is too large\n");
585*38fd1498Szrj       return;
586*38fd1498Szrj     }
587*38fd1498Szrj   /* For comdat functions also reject very small tails; those will likely get
588*38fd1498Szrj      inlined back and we do not want to risk the duplication overhead.
589*38fd1498Szrj      FIXME: with LTO we ought to be able to do better!  */
590*38fd1498Szrj   if (DECL_ONE_ONLY (current_function_decl)
591*38fd1498Szrj       && current->split_size
592*38fd1498Szrj 	 <= (unsigned int) PARAM_VALUE (PARAM_EARLY_INLINING_INSNS) / 2)
593*38fd1498Szrj     {
594*38fd1498Szrj       if (dump_file && (dump_flags & TDF_DETAILS))
595*38fd1498Szrj 	fprintf (dump_file,
596*38fd1498Szrj 		 "  Refused: function is COMDAT and tail is too small\n");
597*38fd1498Szrj       return;
598*38fd1498Szrj     }
599*38fd1498Szrj 
600*38fd1498Szrj   /* FIXME: we currently can pass only SSA function parameters to the split
601*38fd1498Szrj      arguments.  Once parm_adjustment infrastructure is supported by cloning,
602*38fd1498Szrj      we can pass more than that.  */
603*38fd1498Szrj   if (num_args != bitmap_count_bits (current->ssa_names_to_pass))
604*38fd1498Szrj     {
605*38fd1498Szrj 
606*38fd1498Szrj       if (dump_file && (dump_flags & TDF_DETAILS))
607*38fd1498Szrj 	fprintf (dump_file,
608*38fd1498Szrj 		 "  Refused: need to pass non-param values\n");
609*38fd1498Szrj       return;
610*38fd1498Szrj     }
611*38fd1498Szrj 
612*38fd1498Szrj   /* When there are non-ssa vars used in the split region, see if they
613*38fd1498Szrj      are used in the header region.  If so, reject the split.
614*38fd1498Szrj      FIXME: we can use nested function support to access both.  */
615*38fd1498Szrj   if (!bitmap_empty_p (non_ssa_vars)
616*38fd1498Szrj       && !verify_non_ssa_vars (current, non_ssa_vars, return_bb))
617*38fd1498Szrj     {
618*38fd1498Szrj       if (dump_file && (dump_flags & TDF_DETAILS))
619*38fd1498Szrj 	fprintf (dump_file,
620*38fd1498Szrj 		 "  Refused: split part has non-ssa uses\n");
621*38fd1498Szrj       return;
622*38fd1498Szrj     }
623*38fd1498Szrj 
624*38fd1498Szrj   /* If the split point is dominated by a forbidden block, reject
625*38fd1498Szrj      the split.  */
626*38fd1498Szrj   if (!bitmap_empty_p (forbidden_dominators)
627*38fd1498Szrj       && dominated_by_forbidden (current->entry_bb))
628*38fd1498Szrj     {
629*38fd1498Szrj       if (dump_file && (dump_flags & TDF_DETAILS))
630*38fd1498Szrj 	fprintf (dump_file,
631*38fd1498Szrj 		 "  Refused: split point dominated by forbidden block\n");
632*38fd1498Szrj       return;
633*38fd1498Szrj     }
634*38fd1498Szrj 
635*38fd1498Szrj   /* See if retval used by return bb is computed by header or split part.
636*38fd1498Szrj      When it is computed by split part, we need to produce return statement
637*38fd1498Szrj      in the split part and add code to header to pass it around.
638*38fd1498Szrj 
639*38fd1498Szrj      This is bit tricky to test:
640*38fd1498Szrj        1) When there is no return_bb or no return value, we always pass
641*38fd1498Szrj           value around.
642*38fd1498Szrj        2) Invariants are always computed by caller.
643*38fd1498Szrj        3) For SSA we need to look if defining statement is in header or split part
644*38fd1498Szrj        4) For non-SSA we need to look where the var is computed. */
645*38fd1498Szrj   retval = find_retval (return_bb);
646*38fd1498Szrj   if (!retval)
647*38fd1498Szrj     {
648*38fd1498Szrj       /* If there is a return_bb with no return value in function returning
649*38fd1498Szrj 	 value by reference, also make the split part return void, otherwise
650*38fd1498Szrj 	 we expansion would try to create a non-POD temporary, which is
651*38fd1498Szrj 	 invalid.  */
652*38fd1498Szrj       if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
653*38fd1498Szrj 	  && DECL_RESULT (current_function_decl)
654*38fd1498Szrj 	  && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
655*38fd1498Szrj 	current->split_part_set_retval = false;
656*38fd1498Szrj       else
657*38fd1498Szrj 	current->split_part_set_retval = true;
658*38fd1498Szrj     }
659*38fd1498Szrj   else if (is_gimple_min_invariant (retval))
660*38fd1498Szrj     current->split_part_set_retval = false;
661*38fd1498Szrj   /* Special case is value returned by reference we record as if it was non-ssa
662*38fd1498Szrj      set to result_decl.  */
663*38fd1498Szrj   else if (TREE_CODE (retval) == SSA_NAME
664*38fd1498Szrj 	   && SSA_NAME_VAR (retval)
665*38fd1498Szrj 	   && TREE_CODE (SSA_NAME_VAR (retval)) == RESULT_DECL
666*38fd1498Szrj 	   && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
667*38fd1498Szrj     current->split_part_set_retval
668*38fd1498Szrj        = bitmap_bit_p (non_ssa_vars, DECL_UID (SSA_NAME_VAR (retval)));
669*38fd1498Szrj   else if (TREE_CODE (retval) == SSA_NAME)
670*38fd1498Szrj     current->split_part_set_retval
671*38fd1498Szrj       = split_part_set_ssa_name_p (retval, current, return_bb);
672*38fd1498Szrj   else if (TREE_CODE (retval) == PARM_DECL)
673*38fd1498Szrj     current->split_part_set_retval = false;
674*38fd1498Szrj   else if (VAR_P (retval)
675*38fd1498Szrj 	   || TREE_CODE (retval) == RESULT_DECL)
676*38fd1498Szrj     current->split_part_set_retval
677*38fd1498Szrj       = bitmap_bit_p (non_ssa_vars, DECL_UID (retval));
678*38fd1498Szrj   else
679*38fd1498Szrj     current->split_part_set_retval = true;
680*38fd1498Szrj 
681*38fd1498Szrj   /* See if retbnd used by return bb is computed by header or split part.  */
682*38fd1498Szrj   retbnd = find_retbnd (return_bb);
683*38fd1498Szrj   if (retbnd)
684*38fd1498Szrj     {
685*38fd1498Szrj       bool split_part_set_retbnd
686*38fd1498Szrj 	= split_part_set_ssa_name_p (retbnd, current, return_bb);
687*38fd1498Szrj 
688*38fd1498Szrj       /* If we have both return value and bounds then keep their definitions
689*38fd1498Szrj 	 in a single function.  We use SSA names to link returned bounds and
690*38fd1498Szrj 	 value and therefore do not handle cases when result is passed by
691*38fd1498Szrj 	 reference (which should not be our case anyway since bounds are
692*38fd1498Szrj 	 returned for pointers only).  */
693*38fd1498Szrj       if ((DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
694*38fd1498Szrj 	   && current->split_part_set_retval)
695*38fd1498Szrj 	  || split_part_set_retbnd != current->split_part_set_retval)
696*38fd1498Szrj 	{
697*38fd1498Szrj 	  if (dump_file && (dump_flags & TDF_DETAILS))
698*38fd1498Szrj 	    fprintf (dump_file,
699*38fd1498Szrj 		     "  Refused: split point splits return value and bounds\n");
700*38fd1498Szrj 	  return;
701*38fd1498Szrj 	}
702*38fd1498Szrj     }
703*38fd1498Szrj 
704*38fd1498Szrj   /* split_function fixes up at most one PHI non-virtual PHI node in return_bb,
705*38fd1498Szrj      for the return value.  If there are other PHIs, give up.  */
706*38fd1498Szrj   if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
707*38fd1498Szrj     {
708*38fd1498Szrj       gphi_iterator psi;
709*38fd1498Szrj 
710*38fd1498Szrj       for (psi = gsi_start_phis (return_bb); !gsi_end_p (psi); gsi_next (&psi))
711*38fd1498Szrj 	if (!virtual_operand_p (gimple_phi_result (psi.phi ()))
712*38fd1498Szrj 	    && !(retval
713*38fd1498Szrj 		 && current->split_part_set_retval
714*38fd1498Szrj 		 && TREE_CODE (retval) == SSA_NAME
715*38fd1498Szrj 		 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
716*38fd1498Szrj 		 && SSA_NAME_DEF_STMT (retval) == psi.phi ()))
717*38fd1498Szrj 	  {
718*38fd1498Szrj 	    if (dump_file && (dump_flags & TDF_DETAILS))
719*38fd1498Szrj 	      fprintf (dump_file,
720*38fd1498Szrj 		       "  Refused: return bb has extra PHIs\n");
721*38fd1498Szrj 	    return;
722*38fd1498Szrj 	  }
723*38fd1498Szrj     }
724*38fd1498Szrj 
725*38fd1498Szrj   if (dump_file && (dump_flags & TDF_DETAILS))
726*38fd1498Szrj     fprintf (dump_file, "  Accepted!\n");
727*38fd1498Szrj 
728*38fd1498Szrj   /* At the moment chose split point with lowest count and that leaves
729*38fd1498Szrj      out smallest size of header.
730*38fd1498Szrj      In future we might re-consider this heuristics.  */
731*38fd1498Szrj   if (!best_split_point.split_bbs
732*38fd1498Szrj       || best_split_point.count
733*38fd1498Szrj 	 > current->count
734*38fd1498Szrj       || (best_split_point.count == current->count
735*38fd1498Szrj 	  && best_split_point.split_size < current->split_size))
736*38fd1498Szrj 
737*38fd1498Szrj     {
738*38fd1498Szrj       if (dump_file && (dump_flags & TDF_DETAILS))
739*38fd1498Szrj 	fprintf (dump_file, "  New best split point!\n");
740*38fd1498Szrj       if (best_split_point.ssa_names_to_pass)
741*38fd1498Szrj 	{
742*38fd1498Szrj 	  BITMAP_FREE (best_split_point.ssa_names_to_pass);
743*38fd1498Szrj 	  BITMAP_FREE (best_split_point.split_bbs);
744*38fd1498Szrj 	}
745*38fd1498Szrj       best_split_point = *current;
746*38fd1498Szrj       best_split_point.ssa_names_to_pass = BITMAP_ALLOC (NULL);
747*38fd1498Szrj       bitmap_copy (best_split_point.ssa_names_to_pass,
748*38fd1498Szrj 		   current->ssa_names_to_pass);
749*38fd1498Szrj       best_split_point.split_bbs = BITMAP_ALLOC (NULL);
750*38fd1498Szrj       bitmap_copy (best_split_point.split_bbs, current->split_bbs);
751*38fd1498Szrj     }
752*38fd1498Szrj }
753*38fd1498Szrj 
754*38fd1498Szrj /* Return basic block containing RETURN statement.  We allow basic blocks
755*38fd1498Szrj    of the form:
756*38fd1498Szrj    <retval> = tmp_var;
757*38fd1498Szrj    return <retval>
758*38fd1498Szrj    but return_bb can not be more complex than this (except for
759*38fd1498Szrj    -fsanitize=thread we allow TSAN_FUNC_EXIT () internal call in there).
760*38fd1498Szrj    If nothing is found, return the exit block.
761*38fd1498Szrj 
762*38fd1498Szrj    When there are multiple RETURN statement, chose one with return value,
763*38fd1498Szrj    since that one is more likely shared by multiple code paths.
764*38fd1498Szrj 
765*38fd1498Szrj    Return BB is special, because for function splitting it is the only
766*38fd1498Szrj    basic block that is duplicated in between header and split part of the
767*38fd1498Szrj    function.
768*38fd1498Szrj 
769*38fd1498Szrj    TODO: We might support multiple return blocks.  */
770*38fd1498Szrj 
771*38fd1498Szrj static basic_block
find_return_bb(void)772*38fd1498Szrj find_return_bb (void)
773*38fd1498Szrj {
774*38fd1498Szrj   edge e;
775*38fd1498Szrj   basic_block return_bb = EXIT_BLOCK_PTR_FOR_FN (cfun);
776*38fd1498Szrj   gimple_stmt_iterator bsi;
777*38fd1498Szrj   bool found_return = false;
778*38fd1498Szrj   tree retval = NULL_TREE;
779*38fd1498Szrj 
780*38fd1498Szrj   if (!single_pred_p (EXIT_BLOCK_PTR_FOR_FN (cfun)))
781*38fd1498Szrj     return return_bb;
782*38fd1498Szrj 
783*38fd1498Szrj   e = single_pred_edge (EXIT_BLOCK_PTR_FOR_FN (cfun));
784*38fd1498Szrj   for (bsi = gsi_last_bb (e->src); !gsi_end_p (bsi); gsi_prev (&bsi))
785*38fd1498Szrj     {
786*38fd1498Szrj       gimple *stmt = gsi_stmt (bsi);
787*38fd1498Szrj       if (gimple_code (stmt) == GIMPLE_LABEL
788*38fd1498Szrj 	  || is_gimple_debug (stmt)
789*38fd1498Szrj 	  || gimple_clobber_p (stmt))
790*38fd1498Szrj 	;
791*38fd1498Szrj       else if (gimple_code (stmt) == GIMPLE_ASSIGN
792*38fd1498Szrj 	       && found_return
793*38fd1498Szrj 	       && gimple_assign_single_p (stmt)
794*38fd1498Szrj 	       && (auto_var_in_fn_p (gimple_assign_rhs1 (stmt),
795*38fd1498Szrj 				     current_function_decl)
796*38fd1498Szrj 		   || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
797*38fd1498Szrj 	       && retval == gimple_assign_lhs (stmt))
798*38fd1498Szrj 	;
799*38fd1498Szrj       else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
800*38fd1498Szrj 	{
801*38fd1498Szrj 	  found_return = true;
802*38fd1498Szrj 	  retval = gimple_return_retval (return_stmt);
803*38fd1498Szrj 	}
804*38fd1498Szrj       /* For -fsanitize=thread, allow also TSAN_FUNC_EXIT () in the return
805*38fd1498Szrj 	 bb.  */
806*38fd1498Szrj       else if ((flag_sanitize & SANITIZE_THREAD)
807*38fd1498Szrj 	       && gimple_call_internal_p (stmt, IFN_TSAN_FUNC_EXIT))
808*38fd1498Szrj 	;
809*38fd1498Szrj       else
810*38fd1498Szrj 	break;
811*38fd1498Szrj     }
812*38fd1498Szrj   if (gsi_end_p (bsi) && found_return)
813*38fd1498Szrj     return_bb = e->src;
814*38fd1498Szrj 
815*38fd1498Szrj   return return_bb;
816*38fd1498Szrj }
817*38fd1498Szrj 
818*38fd1498Szrj /* Given return basic block RETURN_BB, see where return value is really
819*38fd1498Szrj    stored.  */
820*38fd1498Szrj static tree
find_retval(basic_block return_bb)821*38fd1498Szrj find_retval (basic_block return_bb)
822*38fd1498Szrj {
823*38fd1498Szrj   gimple_stmt_iterator bsi;
824*38fd1498Szrj   for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
825*38fd1498Szrj     if (greturn *return_stmt = dyn_cast <greturn *> (gsi_stmt (bsi)))
826*38fd1498Szrj       return gimple_return_retval (return_stmt);
827*38fd1498Szrj     else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
828*38fd1498Szrj 	     && !gimple_clobber_p (gsi_stmt (bsi)))
829*38fd1498Szrj       return gimple_assign_rhs1 (gsi_stmt (bsi));
830*38fd1498Szrj   return NULL;
831*38fd1498Szrj }
832*38fd1498Szrj 
833*38fd1498Szrj /* Given return basic block RETURN_BB, see where return bounds are really
834*38fd1498Szrj    stored.  */
835*38fd1498Szrj static tree
find_retbnd(basic_block return_bb)836*38fd1498Szrj find_retbnd (basic_block return_bb)
837*38fd1498Szrj {
838*38fd1498Szrj   gimple_stmt_iterator bsi;
839*38fd1498Szrj   for (bsi = gsi_last_bb (return_bb); !gsi_end_p (bsi); gsi_prev (&bsi))
840*38fd1498Szrj     if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
841*38fd1498Szrj       return gimple_return_retbnd (gsi_stmt (bsi));
842*38fd1498Szrj   return NULL;
843*38fd1498Szrj }
844*38fd1498Szrj 
845*38fd1498Szrj /* Callback for walk_stmt_load_store_addr_ops.  If T is non-SSA automatic
846*38fd1498Szrj    variable, mark it as used in bitmap passed via DATA.
847*38fd1498Szrj    Return true when access to T prevents splitting the function.  */
848*38fd1498Szrj 
849*38fd1498Szrj static bool
mark_nonssa_use(gimple *,tree t,tree,void * data)850*38fd1498Szrj mark_nonssa_use (gimple *, tree t, tree, void *data)
851*38fd1498Szrj {
852*38fd1498Szrj   t = get_base_address (t);
853*38fd1498Szrj 
854*38fd1498Szrj   if (!t || is_gimple_reg (t))
855*38fd1498Szrj     return false;
856*38fd1498Szrj 
857*38fd1498Szrj   /* At present we can't pass non-SSA arguments to split function.
858*38fd1498Szrj      FIXME: this can be relaxed by passing references to arguments.  */
859*38fd1498Szrj   if (TREE_CODE (t) == PARM_DECL)
860*38fd1498Szrj     {
861*38fd1498Szrj       if (dump_file && (dump_flags & TDF_DETAILS))
862*38fd1498Szrj 	fprintf (dump_file,
863*38fd1498Szrj 		 "Cannot split: use of non-ssa function parameter.\n");
864*38fd1498Szrj       return true;
865*38fd1498Szrj     }
866*38fd1498Szrj 
867*38fd1498Szrj   if ((VAR_P (t) && auto_var_in_fn_p (t, current_function_decl))
868*38fd1498Szrj       || TREE_CODE (t) == RESULT_DECL
869*38fd1498Szrj       || (TREE_CODE (t) == LABEL_DECL && FORCED_LABEL (t)))
870*38fd1498Szrj     bitmap_set_bit ((bitmap)data, DECL_UID (t));
871*38fd1498Szrj 
872*38fd1498Szrj   /* For DECL_BY_REFERENCE, the return value is actually a pointer.  We want
873*38fd1498Szrj      to pretend that the value pointed to is actual result decl.  */
874*38fd1498Szrj   if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
875*38fd1498Szrj       && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
876*38fd1498Szrj       && SSA_NAME_VAR (TREE_OPERAND (t, 0))
877*38fd1498Szrj       && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
878*38fd1498Szrj       && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
879*38fd1498Szrj     return
880*38fd1498Szrj       bitmap_bit_p ((bitmap)data,
881*38fd1498Szrj 		    DECL_UID (DECL_RESULT (current_function_decl)));
882*38fd1498Szrj 
883*38fd1498Szrj   return false;
884*38fd1498Szrj }
885*38fd1498Szrj 
886*38fd1498Szrj /* Compute local properties of basic block BB we collect when looking for
887*38fd1498Szrj    split points.  We look for ssa defs and store them in SET_SSA_NAMES,
888*38fd1498Szrj    for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic
889*38fd1498Szrj    vars stored in NON_SSA_VARS.
890*38fd1498Szrj 
891*38fd1498Szrj    When BB has edge to RETURN_BB, collect uses in RETURN_BB too.
892*38fd1498Szrj 
893*38fd1498Szrj    Return false when BB contains something that prevents it from being put into
894*38fd1498Szrj    split function.  */
895*38fd1498Szrj 
896*38fd1498Szrj static bool
visit_bb(basic_block bb,basic_block return_bb,bitmap set_ssa_names,bitmap used_ssa_names,bitmap non_ssa_vars)897*38fd1498Szrj visit_bb (basic_block bb, basic_block return_bb,
898*38fd1498Szrj 	  bitmap set_ssa_names, bitmap used_ssa_names,
899*38fd1498Szrj 	  bitmap non_ssa_vars)
900*38fd1498Szrj {
901*38fd1498Szrj   edge e;
902*38fd1498Szrj   edge_iterator ei;
903*38fd1498Szrj   bool can_split = true;
904*38fd1498Szrj 
905*38fd1498Szrj   for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
906*38fd1498Szrj        gsi_next (&bsi))
907*38fd1498Szrj     {
908*38fd1498Szrj       gimple *stmt = gsi_stmt (bsi);
909*38fd1498Szrj       tree op;
910*38fd1498Szrj       ssa_op_iter iter;
911*38fd1498Szrj       tree decl;
912*38fd1498Szrj 
913*38fd1498Szrj       if (is_gimple_debug (stmt))
914*38fd1498Szrj 	continue;
915*38fd1498Szrj 
916*38fd1498Szrj       if (gimple_clobber_p (stmt))
917*38fd1498Szrj 	continue;
918*38fd1498Szrj 
919*38fd1498Szrj       /* FIXME: We can split regions containing EH.  We can not however
920*38fd1498Szrj 	 split RESX, EH_DISPATCH and EH_POINTER referring to same region
921*38fd1498Szrj 	 into different partitions.  This would require tracking of
922*38fd1498Szrj 	 EH regions and checking in consider_split_point if they
923*38fd1498Szrj 	 are not used elsewhere.  */
924*38fd1498Szrj       if (gimple_code (stmt) == GIMPLE_RESX)
925*38fd1498Szrj 	{
926*38fd1498Szrj 	  if (dump_file && (dump_flags & TDF_DETAILS))
927*38fd1498Szrj 	    fprintf (dump_file, "Cannot split: resx.\n");
928*38fd1498Szrj 	  can_split = false;
929*38fd1498Szrj 	}
930*38fd1498Szrj       if (gimple_code (stmt) == GIMPLE_EH_DISPATCH)
931*38fd1498Szrj 	{
932*38fd1498Szrj 	  if (dump_file && (dump_flags & TDF_DETAILS))
933*38fd1498Szrj 	    fprintf (dump_file, "Cannot split: eh dispatch.\n");
934*38fd1498Szrj 	  can_split = false;
935*38fd1498Szrj 	}
936*38fd1498Szrj 
937*38fd1498Szrj       /* Check builtins that prevent splitting.  */
938*38fd1498Szrj       if (gimple_code (stmt) == GIMPLE_CALL
939*38fd1498Szrj 	  && (decl = gimple_call_fndecl (stmt)) != NULL_TREE
940*38fd1498Szrj 	  && DECL_BUILT_IN (decl)
941*38fd1498Szrj 	  && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
942*38fd1498Szrj 	switch (DECL_FUNCTION_CODE (decl))
943*38fd1498Szrj 	  {
944*38fd1498Szrj 	  /* FIXME: once we will allow passing non-parm values to split part,
945*38fd1498Szrj 	     we need to be sure to handle correct builtin_stack_save and
946*38fd1498Szrj 	     builtin_stack_restore.  At the moment we are safe; there is no
947*38fd1498Szrj 	     way to store builtin_stack_save result in non-SSA variable
948*38fd1498Szrj 	     since all calls to those are compiler generated.  */
949*38fd1498Szrj 	  case BUILT_IN_APPLY:
950*38fd1498Szrj 	  case BUILT_IN_APPLY_ARGS:
951*38fd1498Szrj 	  case BUILT_IN_VA_START:
952*38fd1498Szrj 	    if (dump_file && (dump_flags & TDF_DETAILS))
953*38fd1498Szrj 	      fprintf (dump_file,
954*38fd1498Szrj 		       "Cannot split: builtin_apply and va_start.\n");
955*38fd1498Szrj 	    can_split = false;
956*38fd1498Szrj 	    break;
957*38fd1498Szrj 	  case BUILT_IN_EH_POINTER:
958*38fd1498Szrj 	    if (dump_file && (dump_flags & TDF_DETAILS))
959*38fd1498Szrj 	      fprintf (dump_file, "Cannot split: builtin_eh_pointer.\n");
960*38fd1498Szrj 	    can_split = false;
961*38fd1498Szrj 	    break;
962*38fd1498Szrj 	  default:
963*38fd1498Szrj 	    break;
964*38fd1498Szrj 	  }
965*38fd1498Szrj 
966*38fd1498Szrj       FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
967*38fd1498Szrj 	bitmap_set_bit (set_ssa_names, SSA_NAME_VERSION (op));
968*38fd1498Szrj       FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
969*38fd1498Szrj 	bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
970*38fd1498Szrj       can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
971*38fd1498Szrj 						   mark_nonssa_use,
972*38fd1498Szrj 						   mark_nonssa_use,
973*38fd1498Szrj 						   mark_nonssa_use);
974*38fd1498Szrj     }
975*38fd1498Szrj   for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
976*38fd1498Szrj        gsi_next (&bsi))
977*38fd1498Szrj     {
978*38fd1498Szrj       gphi *stmt = bsi.phi ();
979*38fd1498Szrj       unsigned int i;
980*38fd1498Szrj 
981*38fd1498Szrj       if (virtual_operand_p (gimple_phi_result (stmt)))
982*38fd1498Szrj 	continue;
983*38fd1498Szrj       bitmap_set_bit (set_ssa_names,
984*38fd1498Szrj 		      SSA_NAME_VERSION (gimple_phi_result (stmt)));
985*38fd1498Szrj       for (i = 0; i < gimple_phi_num_args (stmt); i++)
986*38fd1498Szrj 	{
987*38fd1498Szrj 	  tree op = gimple_phi_arg_def (stmt, i);
988*38fd1498Szrj 	  if (TREE_CODE (op) == SSA_NAME)
989*38fd1498Szrj 	    bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
990*38fd1498Szrj 	}
991*38fd1498Szrj       can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
992*38fd1498Szrj 						   mark_nonssa_use,
993*38fd1498Szrj 						   mark_nonssa_use,
994*38fd1498Szrj 						   mark_nonssa_use);
995*38fd1498Szrj     }
996*38fd1498Szrj   /* Record also uses coming from PHI operand in return BB.  */
997*38fd1498Szrj   FOR_EACH_EDGE (e, ei, bb->succs)
998*38fd1498Szrj     if (e->dest == return_bb)
999*38fd1498Szrj       {
1000*38fd1498Szrj 	for (gphi_iterator bsi = gsi_start_phis (return_bb);
1001*38fd1498Szrj 	     !gsi_end_p (bsi);
1002*38fd1498Szrj 	     gsi_next (&bsi))
1003*38fd1498Szrj 	  {
1004*38fd1498Szrj 	    gphi *stmt = bsi.phi ();
1005*38fd1498Szrj 	    tree op = gimple_phi_arg_def (stmt, e->dest_idx);
1006*38fd1498Szrj 
1007*38fd1498Szrj 	    if (virtual_operand_p (gimple_phi_result (stmt)))
1008*38fd1498Szrj 	      continue;
1009*38fd1498Szrj 	    if (TREE_CODE (op) == SSA_NAME)
1010*38fd1498Szrj 	      bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
1011*38fd1498Szrj 	    else
1012*38fd1498Szrj 	      can_split &= !mark_nonssa_use (stmt, op, op, non_ssa_vars);
1013*38fd1498Szrj 	  }
1014*38fd1498Szrj       }
1015*38fd1498Szrj   return can_split;
1016*38fd1498Szrj }
1017*38fd1498Szrj 
1018*38fd1498Szrj /* Stack entry for recursive DFS walk in find_split_point.  */
1019*38fd1498Szrj 
1020*38fd1498Szrj struct stack_entry
1021*38fd1498Szrj {
1022*38fd1498Szrj   /* Basic block we are examining.  */
1023*38fd1498Szrj   basic_block bb;
1024*38fd1498Szrj 
1025*38fd1498Szrj   /* SSA names set and used by the BB and all BBs reachable
1026*38fd1498Szrj      from it via DFS walk.  */
1027*38fd1498Szrj   bitmap set_ssa_names, used_ssa_names;
1028*38fd1498Szrj   bitmap non_ssa_vars;
1029*38fd1498Szrj 
1030*38fd1498Szrj   /* All BBS visited from this BB via DFS walk.  */
1031*38fd1498Szrj   bitmap bbs_visited;
1032*38fd1498Szrj 
1033*38fd1498Szrj   /* Last examined edge in DFS walk.  Since we walk unoriented graph,
1034*38fd1498Szrj      the value is up to sum of incoming and outgoing edges of BB.  */
1035*38fd1498Szrj   unsigned int edge_num;
1036*38fd1498Szrj 
1037*38fd1498Szrj   /* Stack entry index of earliest BB reachable from current BB
1038*38fd1498Szrj      or any BB visited later in DFS walk.  */
1039*38fd1498Szrj   int earliest;
1040*38fd1498Szrj 
1041*38fd1498Szrj   /* Overall time and size of all BBs reached from this BB in DFS walk.  */
1042*38fd1498Szrj   sreal overall_time;
1043*38fd1498Szrj   int overall_size;
1044*38fd1498Szrj 
1045*38fd1498Szrj   /* When false we can not split on this BB.  */
1046*38fd1498Szrj   bool can_split;
1047*38fd1498Szrj };
1048*38fd1498Szrj 
1049*38fd1498Szrj 
1050*38fd1498Szrj /* Find all articulations and call consider_split on them.
1051*38fd1498Szrj    OVERALL_TIME and OVERALL_SIZE is time and size of the function.
1052*38fd1498Szrj 
1053*38fd1498Szrj    We perform basic algorithm for finding an articulation in a graph
1054*38fd1498Szrj    created from CFG by considering it to be an unoriented graph.
1055*38fd1498Szrj 
1056*38fd1498Szrj    The articulation is discovered via DFS walk. We collect earliest
1057*38fd1498Szrj    basic block on stack that is reachable via backward edge.  Articulation
1058*38fd1498Szrj    is any basic block such that there is no backward edge bypassing it.
1059*38fd1498Szrj    To reduce stack usage we maintain heap allocated stack in STACK vector.
1060*38fd1498Szrj    AUX pointer of BB is set to index it appears in the stack or -1 once
1061*38fd1498Szrj    it is visited and popped off the stack.
1062*38fd1498Szrj 
1063*38fd1498Szrj    The algorithm finds articulation after visiting the whole component
1064*38fd1498Szrj    reachable by it.  This makes it convenient to collect information about
1065*38fd1498Szrj    the component used by consider_split.  */
1066*38fd1498Szrj 
1067*38fd1498Szrj static void
find_split_points(basic_block return_bb,sreal overall_time,int overall_size)1068*38fd1498Szrj find_split_points (basic_block return_bb, sreal overall_time, int overall_size)
1069*38fd1498Szrj {
1070*38fd1498Szrj   stack_entry first;
1071*38fd1498Szrj   vec<stack_entry> stack = vNULL;
1072*38fd1498Szrj   basic_block bb;
1073*38fd1498Szrj   struct split_point current;
1074*38fd1498Szrj 
1075*38fd1498Szrj   current.header_time = overall_time;
1076*38fd1498Szrj   current.header_size = overall_size;
1077*38fd1498Szrj   current.split_time = 0;
1078*38fd1498Szrj   current.split_size = 0;
1079*38fd1498Szrj   current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
1080*38fd1498Szrj 
1081*38fd1498Szrj   first.bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1082*38fd1498Szrj   first.edge_num = 0;
1083*38fd1498Szrj   first.overall_time = 0;
1084*38fd1498Szrj   first.overall_size = 0;
1085*38fd1498Szrj   first.earliest = INT_MAX;
1086*38fd1498Szrj   first.set_ssa_names = 0;
1087*38fd1498Szrj   first.used_ssa_names = 0;
1088*38fd1498Szrj   first.non_ssa_vars = 0;
1089*38fd1498Szrj   first.bbs_visited = 0;
1090*38fd1498Szrj   first.can_split = false;
1091*38fd1498Szrj   stack.safe_push (first);
1092*38fd1498Szrj   ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = (void *)(intptr_t)-1;
1093*38fd1498Szrj 
1094*38fd1498Szrj   while (!stack.is_empty ())
1095*38fd1498Szrj     {
1096*38fd1498Szrj       stack_entry *entry = &stack.last ();
1097*38fd1498Szrj 
1098*38fd1498Szrj       /* We are walking an acyclic graph, so edge_num counts
1099*38fd1498Szrj 	 succ and pred edges together.  However when considering
1100*38fd1498Szrj          articulation, we want to have processed everything reachable
1101*38fd1498Szrj 	 from articulation but nothing that reaches into it.  */
1102*38fd1498Szrj       if (entry->edge_num == EDGE_COUNT (entry->bb->succs)
1103*38fd1498Szrj 	  && entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1104*38fd1498Szrj 	{
1105*38fd1498Szrj 	  int pos = stack.length ();
1106*38fd1498Szrj 	  entry->can_split &= visit_bb (entry->bb, return_bb,
1107*38fd1498Szrj 					entry->set_ssa_names,
1108*38fd1498Szrj 					entry->used_ssa_names,
1109*38fd1498Szrj 					entry->non_ssa_vars);
1110*38fd1498Szrj 	  if (pos <= entry->earliest && !entry->can_split
1111*38fd1498Szrj 	      && dump_file && (dump_flags & TDF_DETAILS))
1112*38fd1498Szrj 	    fprintf (dump_file,
1113*38fd1498Szrj 		     "found articulation at bb %i but can not split\n",
1114*38fd1498Szrj 		     entry->bb->index);
1115*38fd1498Szrj 	  if (pos <= entry->earliest && entry->can_split)
1116*38fd1498Szrj 	     {
1117*38fd1498Szrj 	       if (dump_file && (dump_flags & TDF_DETAILS))
1118*38fd1498Szrj 		 fprintf (dump_file, "found articulation at bb %i\n",
1119*38fd1498Szrj 			  entry->bb->index);
1120*38fd1498Szrj 	       current.entry_bb = entry->bb;
1121*38fd1498Szrj 	       current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
1122*38fd1498Szrj 	       bitmap_and_compl (current.ssa_names_to_pass,
1123*38fd1498Szrj 				 entry->used_ssa_names, entry->set_ssa_names);
1124*38fd1498Szrj 	       current.header_time = overall_time - entry->overall_time;
1125*38fd1498Szrj 	       current.header_size = overall_size - entry->overall_size;
1126*38fd1498Szrj 	       current.split_time = entry->overall_time;
1127*38fd1498Szrj 	       current.split_size = entry->overall_size;
1128*38fd1498Szrj 	       current.split_bbs = entry->bbs_visited;
1129*38fd1498Szrj 	       consider_split (&current, entry->non_ssa_vars, return_bb);
1130*38fd1498Szrj 	       BITMAP_FREE (current.ssa_names_to_pass);
1131*38fd1498Szrj 	     }
1132*38fd1498Szrj 	}
1133*38fd1498Szrj       /* Do actual DFS walk.  */
1134*38fd1498Szrj       if (entry->edge_num
1135*38fd1498Szrj 	  < (EDGE_COUNT (entry->bb->succs)
1136*38fd1498Szrj 	     + EDGE_COUNT (entry->bb->preds)))
1137*38fd1498Szrj 	{
1138*38fd1498Szrj           edge e;
1139*38fd1498Szrj 	  basic_block dest;
1140*38fd1498Szrj 	  if (entry->edge_num < EDGE_COUNT (entry->bb->succs))
1141*38fd1498Szrj 	    {
1142*38fd1498Szrj 	      e = EDGE_SUCC (entry->bb, entry->edge_num);
1143*38fd1498Szrj 	      dest = e->dest;
1144*38fd1498Szrj 	    }
1145*38fd1498Szrj 	  else
1146*38fd1498Szrj 	    {
1147*38fd1498Szrj 	      e = EDGE_PRED (entry->bb, entry->edge_num
1148*38fd1498Szrj 			     - EDGE_COUNT (entry->bb->succs));
1149*38fd1498Szrj 	      dest = e->src;
1150*38fd1498Szrj 	    }
1151*38fd1498Szrj 
1152*38fd1498Szrj 	  entry->edge_num++;
1153*38fd1498Szrj 
1154*38fd1498Szrj 	  /* New BB to visit, push it to the stack.  */
1155*38fd1498Szrj 	  if (dest != return_bb && dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
1156*38fd1498Szrj 	      && !dest->aux)
1157*38fd1498Szrj 	    {
1158*38fd1498Szrj 	      stack_entry new_entry;
1159*38fd1498Szrj 
1160*38fd1498Szrj 	      new_entry.bb = dest;
1161*38fd1498Szrj 	      new_entry.edge_num = 0;
1162*38fd1498Szrj 	      new_entry.overall_time
1163*38fd1498Szrj 		 = bb_info_vec[dest->index].time;
1164*38fd1498Szrj 	      new_entry.overall_size
1165*38fd1498Szrj 		 = bb_info_vec[dest->index].size;
1166*38fd1498Szrj 	      new_entry.earliest = INT_MAX;
1167*38fd1498Szrj 	      new_entry.set_ssa_names = BITMAP_ALLOC (NULL);
1168*38fd1498Szrj 	      new_entry.used_ssa_names = BITMAP_ALLOC (NULL);
1169*38fd1498Szrj 	      new_entry.bbs_visited = BITMAP_ALLOC (NULL);
1170*38fd1498Szrj 	      new_entry.non_ssa_vars = BITMAP_ALLOC (NULL);
1171*38fd1498Szrj 	      new_entry.can_split = true;
1172*38fd1498Szrj 	      bitmap_set_bit (new_entry.bbs_visited, dest->index);
1173*38fd1498Szrj 	      stack.safe_push (new_entry);
1174*38fd1498Szrj 	      dest->aux = (void *)(intptr_t)stack.length ();
1175*38fd1498Szrj 	    }
1176*38fd1498Szrj 	  /* Back edge found, record the earliest point.  */
1177*38fd1498Szrj 	  else if ((intptr_t)dest->aux > 0
1178*38fd1498Szrj 		   && (intptr_t)dest->aux < entry->earliest)
1179*38fd1498Szrj 	    entry->earliest = (intptr_t)dest->aux;
1180*38fd1498Szrj 	}
1181*38fd1498Szrj       /* We are done with examining the edges.  Pop off the value from stack
1182*38fd1498Szrj 	 and merge stuff we accumulate during the walk.  */
1183*38fd1498Szrj       else if (entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1184*38fd1498Szrj 	{
1185*38fd1498Szrj 	  stack_entry *prev = &stack[stack.length () - 2];
1186*38fd1498Szrj 
1187*38fd1498Szrj 	  entry->bb->aux = (void *)(intptr_t)-1;
1188*38fd1498Szrj 	  prev->can_split &= entry->can_split;
1189*38fd1498Szrj 	  if (prev->set_ssa_names)
1190*38fd1498Szrj 	    {
1191*38fd1498Szrj 	      bitmap_ior_into (prev->set_ssa_names, entry->set_ssa_names);
1192*38fd1498Szrj 	      bitmap_ior_into (prev->used_ssa_names, entry->used_ssa_names);
1193*38fd1498Szrj 	      bitmap_ior_into (prev->bbs_visited, entry->bbs_visited);
1194*38fd1498Szrj 	      bitmap_ior_into (prev->non_ssa_vars, entry->non_ssa_vars);
1195*38fd1498Szrj 	    }
1196*38fd1498Szrj 	  if (prev->earliest > entry->earliest)
1197*38fd1498Szrj 	    prev->earliest = entry->earliest;
1198*38fd1498Szrj 	  prev->overall_time += entry->overall_time;
1199*38fd1498Szrj 	  prev->overall_size += entry->overall_size;
1200*38fd1498Szrj 	  BITMAP_FREE (entry->set_ssa_names);
1201*38fd1498Szrj 	  BITMAP_FREE (entry->used_ssa_names);
1202*38fd1498Szrj 	  BITMAP_FREE (entry->bbs_visited);
1203*38fd1498Szrj 	  BITMAP_FREE (entry->non_ssa_vars);
1204*38fd1498Szrj 	  stack.pop ();
1205*38fd1498Szrj 	}
1206*38fd1498Szrj       else
1207*38fd1498Szrj         stack.pop ();
1208*38fd1498Szrj     }
1209*38fd1498Szrj   ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = NULL;
1210*38fd1498Szrj   FOR_EACH_BB_FN (bb, cfun)
1211*38fd1498Szrj     bb->aux = NULL;
1212*38fd1498Szrj   stack.release ();
1213*38fd1498Szrj   BITMAP_FREE (current.ssa_names_to_pass);
1214*38fd1498Szrj }
1215*38fd1498Szrj 
1216*38fd1498Szrj /* Split function at SPLIT_POINT.  */
1217*38fd1498Szrj 
1218*38fd1498Szrj static void
split_function(basic_block return_bb,struct split_point * split_point,bool add_tsan_func_exit)1219*38fd1498Szrj split_function (basic_block return_bb, struct split_point *split_point,
1220*38fd1498Szrj 		bool add_tsan_func_exit)
1221*38fd1498Szrj {
1222*38fd1498Szrj   vec<tree> args_to_pass = vNULL;
1223*38fd1498Szrj   bitmap args_to_skip;
1224*38fd1498Szrj   tree parm;
1225*38fd1498Szrj   int num = 0;
1226*38fd1498Szrj   cgraph_node *node, *cur_node = cgraph_node::get (current_function_decl);
1227*38fd1498Szrj   basic_block call_bb;
1228*38fd1498Szrj   gcall *call, *tsan_func_exit_call = NULL;
1229*38fd1498Szrj   edge e;
1230*38fd1498Szrj   edge_iterator ei;
1231*38fd1498Szrj   tree retval = NULL, real_retval = NULL, retbnd = NULL;
1232*38fd1498Szrj   bool with_bounds = chkp_function_instrumented_p (current_function_decl);
1233*38fd1498Szrj   gimple *last_stmt = NULL;
1234*38fd1498Szrj   unsigned int i;
1235*38fd1498Szrj   tree arg, ddef;
1236*38fd1498Szrj 
1237*38fd1498Szrj   if (dump_file)
1238*38fd1498Szrj     {
1239*38fd1498Szrj       fprintf (dump_file, "\n\nSplitting function at:\n");
1240*38fd1498Szrj       dump_split_point (dump_file, split_point);
1241*38fd1498Szrj     }
1242*38fd1498Szrj 
1243*38fd1498Szrj   if (cur_node->local.can_change_signature)
1244*38fd1498Szrj     args_to_skip = BITMAP_ALLOC (NULL);
1245*38fd1498Szrj   else
1246*38fd1498Szrj     args_to_skip = NULL;
1247*38fd1498Szrj 
1248*38fd1498Szrj   /* Collect the parameters of new function and args_to_skip bitmap.  */
1249*38fd1498Szrj   for (parm = DECL_ARGUMENTS (current_function_decl);
1250*38fd1498Szrj        parm; parm = DECL_CHAIN (parm), num++)
1251*38fd1498Szrj     if (args_to_skip
1252*38fd1498Szrj 	&& (!is_gimple_reg (parm)
1253*38fd1498Szrj 	    || (ddef = ssa_default_def (cfun, parm)) == NULL_TREE
1254*38fd1498Szrj 	    || !bitmap_bit_p (split_point->ssa_names_to_pass,
1255*38fd1498Szrj 			      SSA_NAME_VERSION (ddef))))
1256*38fd1498Szrj       bitmap_set_bit (args_to_skip, num);
1257*38fd1498Szrj     else
1258*38fd1498Szrj       {
1259*38fd1498Szrj 	/* This parm might not have been used up to now, but is going to be
1260*38fd1498Szrj 	   used, hence register it.  */
1261*38fd1498Szrj 	if (is_gimple_reg (parm))
1262*38fd1498Szrj 	  arg = get_or_create_ssa_default_def (cfun, parm);
1263*38fd1498Szrj 	else
1264*38fd1498Szrj 	  arg = parm;
1265*38fd1498Szrj 
1266*38fd1498Szrj 	if (!useless_type_conversion_p (DECL_ARG_TYPE (parm), TREE_TYPE (arg)))
1267*38fd1498Szrj 	  arg = fold_convert (DECL_ARG_TYPE (parm), arg);
1268*38fd1498Szrj 	args_to_pass.safe_push (arg);
1269*38fd1498Szrj       }
1270*38fd1498Szrj 
1271*38fd1498Szrj   /* See if the split function will return.  */
1272*38fd1498Szrj   bool split_part_return_p = false;
1273*38fd1498Szrj   FOR_EACH_EDGE (e, ei, return_bb->preds)
1274*38fd1498Szrj     {
1275*38fd1498Szrj       if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1276*38fd1498Szrj 	split_part_return_p = true;
1277*38fd1498Szrj     }
1278*38fd1498Szrj 
1279*38fd1498Szrj   /* Add return block to what will become the split function.
1280*38fd1498Szrj      We do not return; no return block is needed.  */
1281*38fd1498Szrj   if (!split_part_return_p)
1282*38fd1498Szrj     ;
1283*38fd1498Szrj   /* We have no return block, so nothing is needed.  */
1284*38fd1498Szrj   else if (return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1285*38fd1498Szrj     ;
1286*38fd1498Szrj   /* When we do not want to return value, we need to construct
1287*38fd1498Szrj      new return block with empty return statement.
1288*38fd1498Szrj      FIXME: Once we are able to change return type, we should change function
1289*38fd1498Szrj      to return void instead of just outputting function with undefined return
1290*38fd1498Szrj      value.  For structures this affects quality of codegen.  */
1291*38fd1498Szrj   else if ((retval = find_retval (return_bb))
1292*38fd1498Szrj 	   && !split_point->split_part_set_retval)
1293*38fd1498Szrj     {
1294*38fd1498Szrj       bool redirected = true;
1295*38fd1498Szrj       basic_block new_return_bb = create_basic_block (NULL, 0, return_bb);
1296*38fd1498Szrj       gimple_stmt_iterator gsi = gsi_start_bb (new_return_bb);
1297*38fd1498Szrj       gsi_insert_after (&gsi, gimple_build_return (NULL), GSI_NEW_STMT);
1298*38fd1498Szrj       new_return_bb->count = profile_count::zero ();
1299*38fd1498Szrj       while (redirected)
1300*38fd1498Szrj 	{
1301*38fd1498Szrj 	  redirected = false;
1302*38fd1498Szrj 	  FOR_EACH_EDGE (e, ei, return_bb->preds)
1303*38fd1498Szrj 	    if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1304*38fd1498Szrj 	      {
1305*38fd1498Szrj 		new_return_bb->count += e->count ();
1306*38fd1498Szrj 		redirect_edge_and_branch (e, new_return_bb);
1307*38fd1498Szrj 		redirected = true;
1308*38fd1498Szrj 		break;
1309*38fd1498Szrj 	      }
1310*38fd1498Szrj 	}
1311*38fd1498Szrj       e = make_single_succ_edge (new_return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1312*38fd1498Szrj       add_bb_to_loop (new_return_bb, current_loops->tree_root);
1313*38fd1498Szrj       bitmap_set_bit (split_point->split_bbs, new_return_bb->index);
1314*38fd1498Szrj       retbnd = find_retbnd (return_bb);
1315*38fd1498Szrj     }
1316*38fd1498Szrj   /* When we pass around the value, use existing return block.  */
1317*38fd1498Szrj   else
1318*38fd1498Szrj     {
1319*38fd1498Szrj       bitmap_set_bit (split_point->split_bbs, return_bb->index);
1320*38fd1498Szrj       retbnd = find_retbnd (return_bb);
1321*38fd1498Szrj     }
1322*38fd1498Szrj 
1323*38fd1498Szrj   /* If RETURN_BB has virtual operand PHIs, they must be removed and the
1324*38fd1498Szrj      virtual operand marked for renaming as we change the CFG in a way that
1325*38fd1498Szrj      tree-inline is not able to compensate for.
1326*38fd1498Szrj 
1327*38fd1498Szrj      Note this can happen whether or not we have a return value.  If we have
1328*38fd1498Szrj      a return value, then RETURN_BB may have PHIs for real operands too.  */
1329*38fd1498Szrj   if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1330*38fd1498Szrj     {
1331*38fd1498Szrj       bool phi_p = false;
1332*38fd1498Szrj       for (gphi_iterator gsi = gsi_start_phis (return_bb);
1333*38fd1498Szrj 	   !gsi_end_p (gsi);)
1334*38fd1498Szrj 	{
1335*38fd1498Szrj 	  gphi *stmt = gsi.phi ();
1336*38fd1498Szrj 	  if (!virtual_operand_p (gimple_phi_result (stmt)))
1337*38fd1498Szrj 	    {
1338*38fd1498Szrj 	      gsi_next (&gsi);
1339*38fd1498Szrj 	      continue;
1340*38fd1498Szrj 	    }
1341*38fd1498Szrj 	  mark_virtual_phi_result_for_renaming (stmt);
1342*38fd1498Szrj 	  remove_phi_node (&gsi, true);
1343*38fd1498Szrj 	  phi_p = true;
1344*38fd1498Szrj 	}
1345*38fd1498Szrj       /* In reality we have to rename the reaching definition of the
1346*38fd1498Szrj 	 virtual operand at return_bb as we will eventually release it
1347*38fd1498Szrj 	 when we remove the code region we outlined.
1348*38fd1498Szrj 	 So we have to rename all immediate virtual uses of that region
1349*38fd1498Szrj 	 if we didn't see a PHI definition yet.  */
1350*38fd1498Szrj       /* ???  In real reality we want to set the reaching vdef of the
1351*38fd1498Szrj          entry of the SESE region as the vuse of the call and the reaching
1352*38fd1498Szrj 	 vdef of the exit of the SESE region as the vdef of the call.  */
1353*38fd1498Szrj       if (!phi_p)
1354*38fd1498Szrj 	for (gimple_stmt_iterator gsi = gsi_start_bb (return_bb);
1355*38fd1498Szrj 	     !gsi_end_p (gsi);
1356*38fd1498Szrj 	     gsi_next (&gsi))
1357*38fd1498Szrj 	  {
1358*38fd1498Szrj 	    gimple *stmt = gsi_stmt (gsi);
1359*38fd1498Szrj 	    if (gimple_vuse (stmt))
1360*38fd1498Szrj 	      {
1361*38fd1498Szrj 		gimple_set_vuse (stmt, NULL_TREE);
1362*38fd1498Szrj 		update_stmt (stmt);
1363*38fd1498Szrj 	      }
1364*38fd1498Szrj 	    if (gimple_vdef (stmt))
1365*38fd1498Szrj 	      break;
1366*38fd1498Szrj 	  }
1367*38fd1498Szrj     }
1368*38fd1498Szrj 
1369*38fd1498Szrj   /* Now create the actual clone.  */
1370*38fd1498Szrj   cgraph_edge::rebuild_edges ();
1371*38fd1498Szrj   node = cur_node->create_version_clone_with_body
1372*38fd1498Szrj     (vNULL, NULL, args_to_skip,
1373*38fd1498Szrj      !split_part_return_p || !split_point->split_part_set_retval,
1374*38fd1498Szrj      split_point->split_bbs, split_point->entry_bb, "part");
1375*38fd1498Szrj 
1376*38fd1498Szrj   node->split_part = true;
1377*38fd1498Szrj 
1378*38fd1498Szrj   if (cur_node->same_comdat_group)
1379*38fd1498Szrj     {
1380*38fd1498Szrj       /* TODO: call is versionable if we make sure that all
1381*38fd1498Szrj 	 callers are inside of a comdat group.  */
1382*38fd1498Szrj       cur_node->calls_comdat_local = 1;
1383*38fd1498Szrj       node->add_to_same_comdat_group (cur_node);
1384*38fd1498Szrj     }
1385*38fd1498Szrj 
1386*38fd1498Szrj 
1387*38fd1498Szrj   /* Let's take a time profile for splitted function.  */
1388*38fd1498Szrj   node->tp_first_run = cur_node->tp_first_run + 1;
1389*38fd1498Szrj 
1390*38fd1498Szrj   /* For usual cloning it is enough to clear builtin only when signature
1391*38fd1498Szrj      changes.  For partial inlining we however can not expect the part
1392*38fd1498Szrj      of builtin implementation to have same semantic as the whole.  */
1393*38fd1498Szrj   if (DECL_BUILT_IN (node->decl))
1394*38fd1498Szrj     {
1395*38fd1498Szrj       DECL_BUILT_IN_CLASS (node->decl) = NOT_BUILT_IN;
1396*38fd1498Szrj       DECL_FUNCTION_CODE (node->decl) = (enum built_in_function) 0;
1397*38fd1498Szrj     }
1398*38fd1498Szrj 
1399*38fd1498Szrj   /* If return_bb contains any clobbers that refer to SSA_NAMEs
1400*38fd1498Szrj      set in the split part, remove them.  Also reset debug stmts that
1401*38fd1498Szrj      refer to SSA_NAMEs set in the split part.  */
1402*38fd1498Szrj   if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1403*38fd1498Szrj     {
1404*38fd1498Szrj       gimple_stmt_iterator gsi = gsi_start_bb (return_bb);
1405*38fd1498Szrj       while (!gsi_end_p (gsi))
1406*38fd1498Szrj 	{
1407*38fd1498Szrj 	  tree op;
1408*38fd1498Szrj 	  ssa_op_iter iter;
1409*38fd1498Szrj 	  gimple *stmt = gsi_stmt (gsi);
1410*38fd1498Szrj 	  bool remove = false;
1411*38fd1498Szrj 	  if (gimple_clobber_p (stmt) || is_gimple_debug (stmt))
1412*38fd1498Szrj 	    FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
1413*38fd1498Szrj 	      {
1414*38fd1498Szrj 		basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (op));
1415*38fd1498Szrj 		if (op != retval
1416*38fd1498Szrj 		    && bb
1417*38fd1498Szrj 		    && bb != return_bb
1418*38fd1498Szrj 		    && bitmap_bit_p (split_point->split_bbs, bb->index))
1419*38fd1498Szrj 		  {
1420*38fd1498Szrj 		    if (is_gimple_debug (stmt))
1421*38fd1498Szrj 		      {
1422*38fd1498Szrj 			gimple_debug_bind_reset_value (stmt);
1423*38fd1498Szrj 			update_stmt (stmt);
1424*38fd1498Szrj 		      }
1425*38fd1498Szrj 		    else
1426*38fd1498Szrj 		      remove = true;
1427*38fd1498Szrj 		    break;
1428*38fd1498Szrj 		  }
1429*38fd1498Szrj 	      }
1430*38fd1498Szrj 	  if (remove)
1431*38fd1498Szrj 	    gsi_remove (&gsi, true);
1432*38fd1498Szrj 	  else
1433*38fd1498Szrj 	    gsi_next (&gsi);
1434*38fd1498Szrj 	}
1435*38fd1498Szrj     }
1436*38fd1498Szrj 
1437*38fd1498Szrj   /* If the original function is instrumented then it's
1438*38fd1498Szrj      part is also instrumented.  */
1439*38fd1498Szrj   if (with_bounds)
1440*38fd1498Szrj     chkp_function_mark_instrumented (node->decl);
1441*38fd1498Szrj 
1442*38fd1498Szrj   /* If the original function is declared inline, there is no point in issuing
1443*38fd1498Szrj      a warning for the non-inlinable part.  */
1444*38fd1498Szrj   DECL_NO_INLINE_WARNING_P (node->decl) = 1;
1445*38fd1498Szrj   cur_node->remove_callees ();
1446*38fd1498Szrj   cur_node->remove_all_references ();
1447*38fd1498Szrj   if (!split_part_return_p)
1448*38fd1498Szrj     TREE_THIS_VOLATILE (node->decl) = 1;
1449*38fd1498Szrj   if (dump_file)
1450*38fd1498Szrj     dump_function_to_file (node->decl, dump_file, dump_flags);
1451*38fd1498Szrj 
1452*38fd1498Szrj   /* Create the basic block we place call into.  It is the entry basic block
1453*38fd1498Szrj      split after last label.  */
1454*38fd1498Szrj   call_bb = split_point->entry_bb;
1455*38fd1498Szrj   for (gimple_stmt_iterator gsi = gsi_start_bb (call_bb); !gsi_end_p (gsi);)
1456*38fd1498Szrj     if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
1457*38fd1498Szrj       {
1458*38fd1498Szrj 	last_stmt = gsi_stmt (gsi);
1459*38fd1498Szrj 	gsi_next (&gsi);
1460*38fd1498Szrj       }
1461*38fd1498Szrj     else
1462*38fd1498Szrj       break;
1463*38fd1498Szrj   call_bb->count = split_point->count;
1464*38fd1498Szrj   e = split_block (split_point->entry_bb, last_stmt);
1465*38fd1498Szrj   remove_edge (e);
1466*38fd1498Szrj 
1467*38fd1498Szrj   /* Produce the call statement.  */
1468*38fd1498Szrj   gimple_stmt_iterator gsi = gsi_last_bb (call_bb);
1469*38fd1498Szrj   FOR_EACH_VEC_ELT (args_to_pass, i, arg)
1470*38fd1498Szrj     if (!is_gimple_val (arg))
1471*38fd1498Szrj       {
1472*38fd1498Szrj 	arg = force_gimple_operand_gsi (&gsi, arg, true, NULL_TREE,
1473*38fd1498Szrj 					false, GSI_CONTINUE_LINKING);
1474*38fd1498Szrj 	args_to_pass[i] = arg;
1475*38fd1498Szrj       }
1476*38fd1498Szrj   call = gimple_build_call_vec (node->decl, args_to_pass);
1477*38fd1498Szrj   gimple_call_set_with_bounds (call, with_bounds);
1478*38fd1498Szrj   gimple_set_block (call, DECL_INITIAL (current_function_decl));
1479*38fd1498Szrj   args_to_pass.release ();
1480*38fd1498Szrj 
1481*38fd1498Szrj   /* For optimized away parameters, add on the caller side
1482*38fd1498Szrj      before the call
1483*38fd1498Szrj      DEBUG D#X => parm_Y(D)
1484*38fd1498Szrj      stmts and associate D#X with parm in decl_debug_args_lookup
1485*38fd1498Szrj      vector to say for debug info that if parameter parm had been passed,
1486*38fd1498Szrj      it would have value parm_Y(D).  */
1487*38fd1498Szrj   if (args_to_skip)
1488*38fd1498Szrj     {
1489*38fd1498Szrj       vec<tree, va_gc> **debug_args = NULL;
1490*38fd1498Szrj       unsigned i = 0, len = 0;
1491*38fd1498Szrj       if (MAY_HAVE_DEBUG_BIND_STMTS)
1492*38fd1498Szrj 	{
1493*38fd1498Szrj 	  debug_args = decl_debug_args_lookup (node->decl);
1494*38fd1498Szrj 	  if (debug_args)
1495*38fd1498Szrj 	    len = vec_safe_length (*debug_args);
1496*38fd1498Szrj 	}
1497*38fd1498Szrj       for (parm = DECL_ARGUMENTS (current_function_decl), num = 0;
1498*38fd1498Szrj 	   parm; parm = DECL_CHAIN (parm), num++)
1499*38fd1498Szrj 	if (bitmap_bit_p (args_to_skip, num) && is_gimple_reg (parm))
1500*38fd1498Szrj 	  {
1501*38fd1498Szrj 	    tree ddecl;
1502*38fd1498Szrj 	    gimple *def_temp;
1503*38fd1498Szrj 
1504*38fd1498Szrj 	    /* This needs to be done even without
1505*38fd1498Szrj 	       MAY_HAVE_DEBUG_BIND_STMTS, otherwise if it didn't exist
1506*38fd1498Szrj 	       before, we'd end up with different SSA_NAME_VERSIONs
1507*38fd1498Szrj 	       between -g and -g0.  */
1508*38fd1498Szrj 	    arg = get_or_create_ssa_default_def (cfun, parm);
1509*38fd1498Szrj 	    if (!MAY_HAVE_DEBUG_BIND_STMTS || debug_args == NULL)
1510*38fd1498Szrj 	      continue;
1511*38fd1498Szrj 
1512*38fd1498Szrj 	    while (i < len && (**debug_args)[i] != DECL_ORIGIN (parm))
1513*38fd1498Szrj 	      i += 2;
1514*38fd1498Szrj 	    if (i >= len)
1515*38fd1498Szrj 	      continue;
1516*38fd1498Szrj 	    ddecl = (**debug_args)[i + 1];
1517*38fd1498Szrj 	    def_temp
1518*38fd1498Szrj 	      = gimple_build_debug_bind (ddecl, unshare_expr (arg), call);
1519*38fd1498Szrj 	    gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
1520*38fd1498Szrj 	  }
1521*38fd1498Szrj     }
1522*38fd1498Szrj 
1523*38fd1498Szrj   /* We avoid address being taken on any variable used by split part,
1524*38fd1498Szrj      so return slot optimization is always possible.  Moreover this is
1525*38fd1498Szrj      required to make DECL_BY_REFERENCE work.  */
1526*38fd1498Szrj   if (aggregate_value_p (DECL_RESULT (current_function_decl),
1527*38fd1498Szrj 			 TREE_TYPE (current_function_decl))
1528*38fd1498Szrj       && (!is_gimple_reg_type (TREE_TYPE (DECL_RESULT (current_function_decl)))
1529*38fd1498Szrj 	  || DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))))
1530*38fd1498Szrj     gimple_call_set_return_slot_opt (call, true);
1531*38fd1498Szrj 
1532*38fd1498Szrj   if (add_tsan_func_exit)
1533*38fd1498Szrj     tsan_func_exit_call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
1534*38fd1498Szrj 
1535*38fd1498Szrj   /* Update return value.  This is bit tricky.  When we do not return,
1536*38fd1498Szrj      do nothing.  When we return we might need to update return_bb
1537*38fd1498Szrj      or produce a new return statement.  */
1538*38fd1498Szrj   if (!split_part_return_p)
1539*38fd1498Szrj     {
1540*38fd1498Szrj       gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1541*38fd1498Szrj       if (tsan_func_exit_call)
1542*38fd1498Szrj 	gsi_insert_after (&gsi, tsan_func_exit_call, GSI_NEW_STMT);
1543*38fd1498Szrj     }
1544*38fd1498Szrj   else
1545*38fd1498Szrj     {
1546*38fd1498Szrj       e = make_single_succ_edge (call_bb, return_bb,
1547*38fd1498Szrj 				 return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
1548*38fd1498Szrj 				 ? 0 : EDGE_FALLTHRU);
1549*38fd1498Szrj 
1550*38fd1498Szrj       /* If there is return basic block, see what value we need to store
1551*38fd1498Szrj          return value into and put call just before it.  */
1552*38fd1498Szrj       if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1553*38fd1498Szrj 	{
1554*38fd1498Szrj 	  real_retval = retval;
1555*38fd1498Szrj 	  if (real_retval && split_point->split_part_set_retval)
1556*38fd1498Szrj 	    {
1557*38fd1498Szrj 	      gphi_iterator psi;
1558*38fd1498Szrj 
1559*38fd1498Szrj 	      /* See if we need new SSA_NAME for the result.
1560*38fd1498Szrj 		 When DECL_BY_REFERENCE is true, retval is actually pointer to
1561*38fd1498Szrj 		 return value and it is constant in whole function.  */
1562*38fd1498Szrj 	      if (TREE_CODE (retval) == SSA_NAME
1563*38fd1498Szrj 		  && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1564*38fd1498Szrj 		{
1565*38fd1498Szrj 		  retval = copy_ssa_name (retval, call);
1566*38fd1498Szrj 
1567*38fd1498Szrj 		  /* See if there is PHI defining return value.  */
1568*38fd1498Szrj 		  for (psi = gsi_start_phis (return_bb);
1569*38fd1498Szrj 		       !gsi_end_p (psi); gsi_next (&psi))
1570*38fd1498Szrj 		    if (!virtual_operand_p (gimple_phi_result (psi.phi ())))
1571*38fd1498Szrj 		      break;
1572*38fd1498Szrj 
1573*38fd1498Szrj 		  /* When there is PHI, just update its value.  */
1574*38fd1498Szrj 		  if (TREE_CODE (retval) == SSA_NAME
1575*38fd1498Szrj 		      && !gsi_end_p (psi))
1576*38fd1498Szrj 		    add_phi_arg (psi.phi (), retval, e, UNKNOWN_LOCATION);
1577*38fd1498Szrj 		  /* Otherwise update the return BB itself.
1578*38fd1498Szrj 		     find_return_bb allows at most one assignment to return value,
1579*38fd1498Szrj 		     so update first statement.  */
1580*38fd1498Szrj 		  else
1581*38fd1498Szrj 		    {
1582*38fd1498Szrj 		      gimple_stmt_iterator bsi;
1583*38fd1498Szrj 		      for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi);
1584*38fd1498Szrj 			   gsi_next (&bsi))
1585*38fd1498Szrj 			if (greturn *return_stmt
1586*38fd1498Szrj 			      = dyn_cast <greturn *> (gsi_stmt (bsi)))
1587*38fd1498Szrj 			  {
1588*38fd1498Szrj 			    gimple_return_set_retval (return_stmt, retval);
1589*38fd1498Szrj 			    break;
1590*38fd1498Szrj 			  }
1591*38fd1498Szrj 			else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
1592*38fd1498Szrj 				 && !gimple_clobber_p (gsi_stmt (bsi)))
1593*38fd1498Szrj 			  {
1594*38fd1498Szrj 			    gimple_assign_set_rhs1 (gsi_stmt (bsi), retval);
1595*38fd1498Szrj 			    break;
1596*38fd1498Szrj 			  }
1597*38fd1498Szrj 		      update_stmt (gsi_stmt (bsi));
1598*38fd1498Szrj 		      /* Also adjust clobbers and debug stmts in return_bb.  */
1599*38fd1498Szrj 		      for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi);
1600*38fd1498Szrj 			   gsi_next (&bsi))
1601*38fd1498Szrj 			{
1602*38fd1498Szrj 			  gimple *stmt = gsi_stmt (bsi);
1603*38fd1498Szrj 			  if (gimple_clobber_p (stmt)
1604*38fd1498Szrj 			      || is_gimple_debug (stmt))
1605*38fd1498Szrj 			    {
1606*38fd1498Szrj 			      ssa_op_iter iter;
1607*38fd1498Szrj 			      use_operand_p use_p;
1608*38fd1498Szrj 			      bool update = false;
1609*38fd1498Szrj 			      FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter,
1610*38fd1498Szrj 							SSA_OP_USE)
1611*38fd1498Szrj 				if (USE_FROM_PTR (use_p) == real_retval)
1612*38fd1498Szrj 				  {
1613*38fd1498Szrj 				    SET_USE (use_p, retval);
1614*38fd1498Szrj 				    update = true;
1615*38fd1498Szrj 				  }
1616*38fd1498Szrj 			      if (update)
1617*38fd1498Szrj 				update_stmt (stmt);
1618*38fd1498Szrj 			    }
1619*38fd1498Szrj 			}
1620*38fd1498Szrj 		    }
1621*38fd1498Szrj 
1622*38fd1498Szrj 		  /* Replace retbnd with new one.  */
1623*38fd1498Szrj 		  if (retbnd)
1624*38fd1498Szrj 		    {
1625*38fd1498Szrj 		      gimple_stmt_iterator bsi;
1626*38fd1498Szrj 		      for (bsi = gsi_last_bb (return_bb); !gsi_end_p (bsi);
1627*38fd1498Szrj 			   gsi_prev (&bsi))
1628*38fd1498Szrj 			if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
1629*38fd1498Szrj 			  {
1630*38fd1498Szrj 			    retbnd = copy_ssa_name (retbnd, call);
1631*38fd1498Szrj 			    gimple_return_set_retbnd (gsi_stmt (bsi), retbnd);
1632*38fd1498Szrj 			    update_stmt (gsi_stmt (bsi));
1633*38fd1498Szrj 			    break;
1634*38fd1498Szrj 			  }
1635*38fd1498Szrj 		    }
1636*38fd1498Szrj 		}
1637*38fd1498Szrj 	      if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1638*38fd1498Szrj 		{
1639*38fd1498Szrj 		  gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1640*38fd1498Szrj 		  gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1641*38fd1498Szrj 		}
1642*38fd1498Szrj 	      else
1643*38fd1498Szrj 		{
1644*38fd1498Szrj 		  tree restype;
1645*38fd1498Szrj 		  restype = TREE_TYPE (DECL_RESULT (current_function_decl));
1646*38fd1498Szrj 		  gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1647*38fd1498Szrj 		  if (!useless_type_conversion_p (TREE_TYPE (retval), restype))
1648*38fd1498Szrj 		    {
1649*38fd1498Szrj 		      gimple *cpy;
1650*38fd1498Szrj 		      tree tem = create_tmp_reg (restype);
1651*38fd1498Szrj 		      tem = make_ssa_name (tem, call);
1652*38fd1498Szrj 		      cpy = gimple_build_assign (retval, NOP_EXPR, tem);
1653*38fd1498Szrj 		      gsi_insert_after (&gsi, cpy, GSI_NEW_STMT);
1654*38fd1498Szrj 		      retval = tem;
1655*38fd1498Szrj 		    }
1656*38fd1498Szrj 		  /* Build bndret call to obtain returned bounds.  */
1657*38fd1498Szrj 		  if (retbnd)
1658*38fd1498Szrj 		    chkp_insert_retbnd_call (retbnd, retval, &gsi);
1659*38fd1498Szrj 		  gimple_call_set_lhs (call, retval);
1660*38fd1498Szrj 		  update_stmt (call);
1661*38fd1498Szrj 		}
1662*38fd1498Szrj 	    }
1663*38fd1498Szrj 	  else
1664*38fd1498Szrj 	    gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1665*38fd1498Szrj 	  if (tsan_func_exit_call)
1666*38fd1498Szrj 	    gsi_insert_after (&gsi, tsan_func_exit_call, GSI_NEW_STMT);
1667*38fd1498Szrj 	}
1668*38fd1498Szrj       /* We don't use return block (there is either no return in function or
1669*38fd1498Szrj 	 multiple of them).  So create new basic block with return statement.
1670*38fd1498Szrj 	 */
1671*38fd1498Szrj       else
1672*38fd1498Szrj 	{
1673*38fd1498Szrj 	  greturn *ret;
1674*38fd1498Szrj 	  if (split_point->split_part_set_retval
1675*38fd1498Szrj 	      && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1676*38fd1498Szrj 	    {
1677*38fd1498Szrj 	      retval = DECL_RESULT (current_function_decl);
1678*38fd1498Szrj 
1679*38fd1498Szrj 	      if (chkp_function_instrumented_p (current_function_decl)
1680*38fd1498Szrj 		  && BOUNDED_P (retval))
1681*38fd1498Szrj 		retbnd = create_tmp_reg (pointer_bounds_type_node);
1682*38fd1498Szrj 
1683*38fd1498Szrj 	      /* We use temporary register to hold value when aggregate_value_p
1684*38fd1498Szrj 		 is false.  Similarly for DECL_BY_REFERENCE we must avoid extra
1685*38fd1498Szrj 		 copy.  */
1686*38fd1498Szrj 	      if (!aggregate_value_p (retval, TREE_TYPE (current_function_decl))
1687*38fd1498Szrj 		  && !DECL_BY_REFERENCE (retval))
1688*38fd1498Szrj 		retval = create_tmp_reg (TREE_TYPE (retval));
1689*38fd1498Szrj 	      if (is_gimple_reg (retval))
1690*38fd1498Szrj 		{
1691*38fd1498Szrj 		  /* When returning by reference, there is only one SSA name
1692*38fd1498Szrj 		     assigned to RESULT_DECL (that is pointer to return value).
1693*38fd1498Szrj 		     Look it up or create new one if it is missing.  */
1694*38fd1498Szrj 		  if (DECL_BY_REFERENCE (retval))
1695*38fd1498Szrj 		    retval = get_or_create_ssa_default_def (cfun, retval);
1696*38fd1498Szrj 		  /* Otherwise produce new SSA name for return value.  */
1697*38fd1498Szrj 		  else
1698*38fd1498Szrj 		    retval = make_ssa_name (retval, call);
1699*38fd1498Szrj 		}
1700*38fd1498Szrj 	      if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1701*38fd1498Szrj 	        gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1702*38fd1498Szrj 	      else
1703*38fd1498Szrj 	        gimple_call_set_lhs (call, retval);
1704*38fd1498Szrj 	      gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1705*38fd1498Szrj 	    }
1706*38fd1498Szrj 	  else
1707*38fd1498Szrj 	    {
1708*38fd1498Szrj 	      gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1709*38fd1498Szrj 	      if (retval
1710*38fd1498Szrj 		  && is_gimple_reg_type (TREE_TYPE (retval))
1711*38fd1498Szrj 		  && !is_gimple_val (retval))
1712*38fd1498Szrj 		{
1713*38fd1498Szrj 		  gassign *g
1714*38fd1498Szrj 		    = gimple_build_assign (make_ssa_name (TREE_TYPE (retval)),
1715*38fd1498Szrj 					   retval);
1716*38fd1498Szrj 		  retval = gimple_assign_lhs (g);
1717*38fd1498Szrj 		  gsi_insert_after (&gsi, g, GSI_NEW_STMT);
1718*38fd1498Szrj 		}
1719*38fd1498Szrj 	    }
1720*38fd1498Szrj 	  /* Build bndret call to obtain returned bounds.  */
1721*38fd1498Szrj 	  if (retbnd)
1722*38fd1498Szrj 	    chkp_insert_retbnd_call (retbnd, retval, &gsi);
1723*38fd1498Szrj 	  if (tsan_func_exit_call)
1724*38fd1498Szrj 	    gsi_insert_after (&gsi, tsan_func_exit_call, GSI_NEW_STMT);
1725*38fd1498Szrj 	  ret = gimple_build_return (retval);
1726*38fd1498Szrj 	  gsi_insert_after (&gsi, ret, GSI_NEW_STMT);
1727*38fd1498Szrj 	}
1728*38fd1498Szrj     }
1729*38fd1498Szrj   free_dominance_info (CDI_DOMINATORS);
1730*38fd1498Szrj   free_dominance_info (CDI_POST_DOMINATORS);
1731*38fd1498Szrj   compute_fn_summary (node, true);
1732*38fd1498Szrj }
1733*38fd1498Szrj 
1734*38fd1498Szrj /* Execute function splitting pass.  */
1735*38fd1498Szrj 
1736*38fd1498Szrj static unsigned int
execute_split_functions(void)1737*38fd1498Szrj execute_split_functions (void)
1738*38fd1498Szrj {
1739*38fd1498Szrj   gimple_stmt_iterator bsi;
1740*38fd1498Szrj   basic_block bb;
1741*38fd1498Szrj   sreal overall_time = 0;
1742*38fd1498Szrj   int overall_size = 0;
1743*38fd1498Szrj   int todo = 0;
1744*38fd1498Szrj   struct cgraph_node *node = cgraph_node::get (current_function_decl);
1745*38fd1498Szrj 
1746*38fd1498Szrj   if (flags_from_decl_or_type (current_function_decl)
1747*38fd1498Szrj       & (ECF_NORETURN|ECF_MALLOC))
1748*38fd1498Szrj     {
1749*38fd1498Szrj       if (dump_file)
1750*38fd1498Szrj 	fprintf (dump_file, "Not splitting: noreturn/malloc function.\n");
1751*38fd1498Szrj       return 0;
1752*38fd1498Szrj     }
1753*38fd1498Szrj   if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
1754*38fd1498Szrj     {
1755*38fd1498Szrj       if (dump_file)
1756*38fd1498Szrj 	fprintf (dump_file, "Not splitting: main function.\n");
1757*38fd1498Szrj       return 0;
1758*38fd1498Szrj     }
1759*38fd1498Szrj   if (node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
1760*38fd1498Szrj     {
1761*38fd1498Szrj       if (dump_file)
1762*38fd1498Szrj 	fprintf (dump_file, "Not splitting: function is unlikely executed.\n");
1763*38fd1498Szrj       return 0;
1764*38fd1498Szrj     }
1765*38fd1498Szrj   /* This can be relaxed; function might become inlinable after splitting
1766*38fd1498Szrj      away the uninlinable part.  */
1767*38fd1498Szrj   if (ipa_fn_summaries
1768*38fd1498Szrj       && !ipa_fn_summaries->get (node)->inlinable)
1769*38fd1498Szrj     {
1770*38fd1498Szrj       if (dump_file)
1771*38fd1498Szrj 	fprintf (dump_file, "Not splitting: not inlinable.\n");
1772*38fd1498Szrj       return 0;
1773*38fd1498Szrj     }
1774*38fd1498Szrj   if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1775*38fd1498Szrj     {
1776*38fd1498Szrj       if (dump_file)
1777*38fd1498Szrj 	fprintf (dump_file, "Not splitting: disregarding inline limits.\n");
1778*38fd1498Szrj       return 0;
1779*38fd1498Szrj     }
1780*38fd1498Szrj   /* This can be relaxed; most of versioning tests actually prevents
1781*38fd1498Szrj      a duplication.  */
1782*38fd1498Szrj   if (!tree_versionable_function_p (current_function_decl))
1783*38fd1498Szrj     {
1784*38fd1498Szrj       if (dump_file)
1785*38fd1498Szrj 	fprintf (dump_file, "Not splitting: not versionable.\n");
1786*38fd1498Szrj       return 0;
1787*38fd1498Szrj     }
1788*38fd1498Szrj   /* FIXME: we could support this.  */
1789*38fd1498Szrj   if (DECL_STRUCT_FUNCTION (current_function_decl)->static_chain_decl)
1790*38fd1498Szrj     {
1791*38fd1498Szrj       if (dump_file)
1792*38fd1498Szrj 	fprintf (dump_file, "Not splitting: nested function.\n");
1793*38fd1498Szrj       return 0;
1794*38fd1498Szrj     }
1795*38fd1498Szrj 
1796*38fd1498Szrj   /* See if it makes sense to try to split.
1797*38fd1498Szrj      It makes sense to split if we inline, that is if we have direct calls to
1798*38fd1498Szrj      handle or direct calls are possibly going to appear as result of indirect
1799*38fd1498Szrj      inlining or LTO.  Also handle -fprofile-generate as LTO to allow non-LTO
1800*38fd1498Szrj      training for LTO -fprofile-use build.
1801*38fd1498Szrj 
1802*38fd1498Szrj      Note that we are not completely conservative about disqualifying functions
1803*38fd1498Szrj      called once.  It is possible that the caller is called more then once and
1804*38fd1498Szrj      then inlining would still benefit.  */
1805*38fd1498Szrj   if ((!node->callers
1806*38fd1498Szrj        /* Local functions called once will be completely inlined most of time.  */
1807*38fd1498Szrj        || (!node->callers->next_caller && node->local.local))
1808*38fd1498Szrj       && !node->address_taken
1809*38fd1498Szrj       && !node->has_aliases_p ()
1810*38fd1498Szrj       && (!flag_lto || !node->externally_visible))
1811*38fd1498Szrj     {
1812*38fd1498Szrj       if (dump_file)
1813*38fd1498Szrj 	fprintf (dump_file, "Not splitting: not called directly "
1814*38fd1498Szrj 		 "or called once.\n");
1815*38fd1498Szrj       return 0;
1816*38fd1498Szrj     }
1817*38fd1498Szrj 
1818*38fd1498Szrj   /* FIXME: We can actually split if splitting reduces call overhead.  */
1819*38fd1498Szrj   if (!flag_inline_small_functions
1820*38fd1498Szrj       && !DECL_DECLARED_INLINE_P (current_function_decl))
1821*38fd1498Szrj     {
1822*38fd1498Szrj       if (dump_file)
1823*38fd1498Szrj 	fprintf (dump_file, "Not splitting: not autoinlining and function"
1824*38fd1498Szrj 		 " is not inline.\n");
1825*38fd1498Szrj       return 0;
1826*38fd1498Szrj     }
1827*38fd1498Szrj 
1828*38fd1498Szrj   /* We enforce splitting after loop headers when profile info is not
1829*38fd1498Szrj      available.  */
1830*38fd1498Szrj   if (profile_status_for_fn (cfun) != PROFILE_READ)
1831*38fd1498Szrj     mark_dfs_back_edges ();
1832*38fd1498Szrj 
1833*38fd1498Szrj   /* Initialize bitmap to track forbidden calls.  */
1834*38fd1498Szrj   forbidden_dominators = BITMAP_ALLOC (NULL);
1835*38fd1498Szrj   calculate_dominance_info (CDI_DOMINATORS);
1836*38fd1498Szrj 
1837*38fd1498Szrj   /* Compute local info about basic blocks and determine function size/time.  */
1838*38fd1498Szrj   bb_info_vec.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
1839*38fd1498Szrj   best_split_point.split_bbs = NULL;
1840*38fd1498Szrj   basic_block return_bb = find_return_bb ();
1841*38fd1498Szrj   int tsan_exit_found = -1;
1842*38fd1498Szrj   FOR_EACH_BB_FN (bb, cfun)
1843*38fd1498Szrj     {
1844*38fd1498Szrj       sreal time = 0;
1845*38fd1498Szrj       int size = 0;
1846*38fd1498Szrj       sreal freq = bb->count.to_sreal_scale
1847*38fd1498Szrj 			 (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count);
1848*38fd1498Szrj 
1849*38fd1498Szrj       if (dump_file && (dump_flags & TDF_DETAILS))
1850*38fd1498Szrj 	fprintf (dump_file, "Basic block %i\n", bb->index);
1851*38fd1498Szrj 
1852*38fd1498Szrj       for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1853*38fd1498Szrj 	{
1854*38fd1498Szrj 	  sreal this_time;
1855*38fd1498Szrj 	  int this_size;
1856*38fd1498Szrj 	  gimple *stmt = gsi_stmt (bsi);
1857*38fd1498Szrj 
1858*38fd1498Szrj 	  this_size = estimate_num_insns (stmt, &eni_size_weights);
1859*38fd1498Szrj 	  this_time = (sreal)estimate_num_insns (stmt, &eni_time_weights)
1860*38fd1498Szrj 			 * freq;
1861*38fd1498Szrj 	  size += this_size;
1862*38fd1498Szrj 	  time += this_time;
1863*38fd1498Szrj 	  check_forbidden_calls (stmt);
1864*38fd1498Szrj 
1865*38fd1498Szrj 	  if (dump_file && (dump_flags & TDF_DETAILS))
1866*38fd1498Szrj 	    {
1867*38fd1498Szrj 	      fprintf (dump_file, "  freq:%4.2f size:%3i time:%4.2f ",
1868*38fd1498Szrj 		       freq.to_double (), this_size, this_time.to_double ());
1869*38fd1498Szrj 	      print_gimple_stmt (dump_file, stmt, 0);
1870*38fd1498Szrj 	    }
1871*38fd1498Szrj 
1872*38fd1498Szrj 	  if ((flag_sanitize & SANITIZE_THREAD)
1873*38fd1498Szrj 	      && gimple_call_internal_p (stmt, IFN_TSAN_FUNC_EXIT))
1874*38fd1498Szrj 	    {
1875*38fd1498Szrj 	      /* We handle TSAN_FUNC_EXIT for splitting either in the
1876*38fd1498Szrj 		 return_bb, or in its immediate predecessors.  */
1877*38fd1498Szrj 	      if ((bb != return_bb && !find_edge (bb, return_bb))
1878*38fd1498Szrj 		  || (tsan_exit_found != -1
1879*38fd1498Szrj 		      && tsan_exit_found != (bb != return_bb)))
1880*38fd1498Szrj 		{
1881*38fd1498Szrj 		  if (dump_file)
1882*38fd1498Szrj 		    fprintf (dump_file, "Not splitting: TSAN_FUNC_EXIT"
1883*38fd1498Szrj 			     " in unexpected basic block.\n");
1884*38fd1498Szrj 		  BITMAP_FREE (forbidden_dominators);
1885*38fd1498Szrj 		  bb_info_vec.release ();
1886*38fd1498Szrj 		  return 0;
1887*38fd1498Szrj 		}
1888*38fd1498Szrj 	      tsan_exit_found = bb != return_bb;
1889*38fd1498Szrj 	    }
1890*38fd1498Szrj 	}
1891*38fd1498Szrj       overall_time += time;
1892*38fd1498Szrj       overall_size += size;
1893*38fd1498Szrj       bb_info_vec[bb->index].time = time;
1894*38fd1498Szrj       bb_info_vec[bb->index].size = size;
1895*38fd1498Szrj     }
1896*38fd1498Szrj   find_split_points (return_bb, overall_time, overall_size);
1897*38fd1498Szrj   if (best_split_point.split_bbs)
1898*38fd1498Szrj     {
1899*38fd1498Szrj       split_function (return_bb, &best_split_point, tsan_exit_found == 1);
1900*38fd1498Szrj       BITMAP_FREE (best_split_point.ssa_names_to_pass);
1901*38fd1498Szrj       BITMAP_FREE (best_split_point.split_bbs);
1902*38fd1498Szrj       todo = TODO_update_ssa | TODO_cleanup_cfg;
1903*38fd1498Szrj     }
1904*38fd1498Szrj   BITMAP_FREE (forbidden_dominators);
1905*38fd1498Szrj   bb_info_vec.release ();
1906*38fd1498Szrj   return todo;
1907*38fd1498Szrj }
1908*38fd1498Szrj 
1909*38fd1498Szrj namespace {
1910*38fd1498Szrj 
1911*38fd1498Szrj const pass_data pass_data_split_functions =
1912*38fd1498Szrj {
1913*38fd1498Szrj   GIMPLE_PASS, /* type */
1914*38fd1498Szrj   "fnsplit", /* name */
1915*38fd1498Szrj   OPTGROUP_NONE, /* optinfo_flags */
1916*38fd1498Szrj   TV_IPA_FNSPLIT, /* tv_id */
1917*38fd1498Szrj   PROP_cfg, /* properties_required */
1918*38fd1498Szrj   0, /* properties_provided */
1919*38fd1498Szrj   0, /* properties_destroyed */
1920*38fd1498Szrj   0, /* todo_flags_start */
1921*38fd1498Szrj   0, /* todo_flags_finish */
1922*38fd1498Szrj };
1923*38fd1498Szrj 
1924*38fd1498Szrj class pass_split_functions : public gimple_opt_pass
1925*38fd1498Szrj {
1926*38fd1498Szrj public:
pass_split_functions(gcc::context * ctxt)1927*38fd1498Szrj   pass_split_functions (gcc::context *ctxt)
1928*38fd1498Szrj     : gimple_opt_pass (pass_data_split_functions, ctxt)
1929*38fd1498Szrj   {}
1930*38fd1498Szrj 
1931*38fd1498Szrj   /* opt_pass methods: */
1932*38fd1498Szrj   virtual bool gate (function *);
execute(function *)1933*38fd1498Szrj   virtual unsigned int execute (function *)
1934*38fd1498Szrj     {
1935*38fd1498Szrj       return execute_split_functions ();
1936*38fd1498Szrj     }
1937*38fd1498Szrj 
1938*38fd1498Szrj }; // class pass_split_functions
1939*38fd1498Szrj 
1940*38fd1498Szrj bool
gate(function *)1941*38fd1498Szrj pass_split_functions::gate (function *)
1942*38fd1498Szrj {
1943*38fd1498Szrj   /* When doing profile feedback, we want to execute the pass after profiling
1944*38fd1498Szrj      is read.  So disable one in early optimization.  */
1945*38fd1498Szrj   return (flag_partial_inlining
1946*38fd1498Szrj 	  && !profile_arc_flag && !flag_branch_probabilities);
1947*38fd1498Szrj }
1948*38fd1498Szrj 
1949*38fd1498Szrj } // anon namespace
1950*38fd1498Szrj 
1951*38fd1498Szrj gimple_opt_pass *
make_pass_split_functions(gcc::context * ctxt)1952*38fd1498Szrj make_pass_split_functions (gcc::context *ctxt)
1953*38fd1498Szrj {
1954*38fd1498Szrj   return new pass_split_functions (ctxt);
1955*38fd1498Szrj }
1956*38fd1498Szrj 
1957*38fd1498Szrj /* Execute function splitting pass.  */
1958*38fd1498Szrj 
1959*38fd1498Szrj static unsigned int
execute_feedback_split_functions(void)1960*38fd1498Szrj execute_feedback_split_functions (void)
1961*38fd1498Szrj {
1962*38fd1498Szrj   unsigned int retval = execute_split_functions ();
1963*38fd1498Szrj   if (retval)
1964*38fd1498Szrj     retval |= TODO_rebuild_cgraph_edges;
1965*38fd1498Szrj   return retval;
1966*38fd1498Szrj }
1967*38fd1498Szrj 
1968*38fd1498Szrj namespace {
1969*38fd1498Szrj 
1970*38fd1498Szrj const pass_data pass_data_feedback_split_functions =
1971*38fd1498Szrj {
1972*38fd1498Szrj   GIMPLE_PASS, /* type */
1973*38fd1498Szrj   "feedback_fnsplit", /* name */
1974*38fd1498Szrj   OPTGROUP_NONE, /* optinfo_flags */
1975*38fd1498Szrj   TV_IPA_FNSPLIT, /* tv_id */
1976*38fd1498Szrj   PROP_cfg, /* properties_required */
1977*38fd1498Szrj   0, /* properties_provided */
1978*38fd1498Szrj   0, /* properties_destroyed */
1979*38fd1498Szrj   0, /* todo_flags_start */
1980*38fd1498Szrj   0, /* todo_flags_finish */
1981*38fd1498Szrj };
1982*38fd1498Szrj 
1983*38fd1498Szrj class pass_feedback_split_functions : public gimple_opt_pass
1984*38fd1498Szrj {
1985*38fd1498Szrj public:
pass_feedback_split_functions(gcc::context * ctxt)1986*38fd1498Szrj   pass_feedback_split_functions (gcc::context *ctxt)
1987*38fd1498Szrj     : gimple_opt_pass (pass_data_feedback_split_functions, ctxt)
1988*38fd1498Szrj   {}
1989*38fd1498Szrj 
1990*38fd1498Szrj   /* opt_pass methods: */
1991*38fd1498Szrj   virtual bool gate (function *);
execute(function *)1992*38fd1498Szrj   virtual unsigned int execute (function *)
1993*38fd1498Szrj     {
1994*38fd1498Szrj       return execute_feedback_split_functions ();
1995*38fd1498Szrj     }
1996*38fd1498Szrj 
1997*38fd1498Szrj }; // class pass_feedback_split_functions
1998*38fd1498Szrj 
1999*38fd1498Szrj bool
gate(function *)2000*38fd1498Szrj pass_feedback_split_functions::gate (function *)
2001*38fd1498Szrj {
2002*38fd1498Szrj   /* We don't need to split when profiling at all, we are producing
2003*38fd1498Szrj      lousy code anyway.  */
2004*38fd1498Szrj   return (flag_partial_inlining
2005*38fd1498Szrj 	  && flag_branch_probabilities);
2006*38fd1498Szrj }
2007*38fd1498Szrj 
2008*38fd1498Szrj } // anon namespace
2009*38fd1498Szrj 
2010*38fd1498Szrj gimple_opt_pass *
make_pass_feedback_split_functions(gcc::context * ctxt)2011*38fd1498Szrj make_pass_feedback_split_functions (gcc::context *ctxt)
2012*38fd1498Szrj {
2013*38fd1498Szrj   return new pass_feedback_split_functions (ctxt);
2014*38fd1498Szrj }
2015