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