* builtins.h (c_srlen): Add argument.
[official-gcc.git] / gcc / ipa-split.c
blob38f5bcf00a6f2b36052caccd520e0b3ba81cb24c
1 /* Function splitting pass
2 Copyright (C) 2010-2018 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka <jh@suse.cz>
5 This file is part of GCC.
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.
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.
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/>. */
21 /* The purpose of this pass is to split function bodies to improve
22 inlining. I.e. for function of the form:
24 func (...)
26 if (cheap_test)
27 something_small
28 else
29 something_big
32 Produce:
34 func.part (...)
36 something_big
39 func (...)
41 if (cheap_test)
42 something_small
43 else
44 func.part (...);
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.
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.
59 The decisions what functions to split are in execute_split_functions
60 and consider_split.
62 There are several possible future improvements for this pass including:
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. */
77 #include "config.h"
78 #include "system.h"
79 #include "coretypes.h"
80 #include "backend.h"
81 #include "rtl.h"
82 #include "tree.h"
83 #include "gimple.h"
84 #include "cfghooks.h"
85 #include "alloc-pool.h"
86 #include "tree-pass.h"
87 #include "ssa.h"
88 #include "cgraph.h"
89 #include "diagnostic.h"
90 #include "fold-const.h"
91 #include "cfganal.h"
92 #include "calls.h"
93 #include "gimplify.h"
94 #include "gimple-iterator.h"
95 #include "gimplify-me.h"
96 #include "gimple-walk.h"
97 #include "symbol-summary.h"
98 #include "ipa-prop.h"
99 #include "tree-cfg.h"
100 #include "tree-into-ssa.h"
101 #include "tree-dfa.h"
102 #include "tree-inline.h"
103 #include "params.h"
104 #include "gimple-pretty-print.h"
105 #include "ipa-fnsummary.h"
106 #include "cfgloop.h"
108 /* Per basic block info. */
110 struct split_bb_info
112 unsigned int size;
113 sreal time;
116 static vec<split_bb_info> bb_info_vec;
118 /* Description of split point. */
120 struct split_point
122 /* Size of the partitions. */
123 sreal header_time, split_time;
124 unsigned int header_size, split_size;
126 /* SSA names that need to be passed into spit function. */
127 bitmap ssa_names_to_pass;
129 /* Basic block where we split (that will become entry point of new function. */
130 basic_block entry_bb;
132 /* Count for entering the split part.
133 This is not count of the entry_bb because it may be in loop. */
134 profile_count count;
136 /* Basic blocks we are splitting away. */
137 bitmap split_bbs;
139 /* True when return value is computed on split part and thus it needs
140 to be returned. */
141 bool split_part_set_retval;
144 /* Best split point found. */
146 struct split_point best_split_point;
148 /* Set of basic blocks that are not allowed to dominate a split point. */
150 static bitmap forbidden_dominators;
152 static tree find_retval (basic_block return_bb);
154 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
155 variable, check it if it is present in bitmap passed via DATA. */
157 static bool
158 test_nonssa_use (gimple *, tree t, tree, void *data)
160 t = get_base_address (t);
162 if (!t || is_gimple_reg (t))
163 return false;
165 if (TREE_CODE (t) == PARM_DECL
166 || (VAR_P (t)
167 && auto_var_in_fn_p (t, current_function_decl))
168 || TREE_CODE (t) == RESULT_DECL
169 /* Normal labels are part of CFG and will be handled gratefuly.
170 Forced labels however can be used directly by statements and
171 need to stay in one partition along with their uses. */
172 || (TREE_CODE (t) == LABEL_DECL
173 && FORCED_LABEL (t)))
174 return bitmap_bit_p ((bitmap)data, DECL_UID (t));
176 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
177 to pretend that the value pointed to is actual result decl. */
178 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
179 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
180 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
181 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
182 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
183 return
184 bitmap_bit_p ((bitmap)data,
185 DECL_UID (DECL_RESULT (current_function_decl)));
187 return false;
190 /* Dump split point CURRENT. */
192 static void
193 dump_split_point (FILE * file, struct split_point *current)
195 fprintf (file,
196 "Split point at BB %i\n"
197 " header time: %f header size: %i\n"
198 " split time: %f split size: %i\n bbs: ",
199 current->entry_bb->index, current->header_time.to_double (),
200 current->header_size, current->split_time.to_double (),
201 current->split_size);
202 dump_bitmap (file, current->split_bbs);
203 fprintf (file, " SSA names to pass: ");
204 dump_bitmap (file, current->ssa_names_to_pass);
207 /* Look for all BBs in header that might lead to the split part and verify
208 that they are not defining any non-SSA var used by the split part.
209 Parameters are the same as for consider_split. */
211 static bool
212 verify_non_ssa_vars (struct split_point *current, bitmap non_ssa_vars,
213 basic_block return_bb)
215 bitmap seen = BITMAP_ALLOC (NULL);
216 vec<basic_block> worklist = vNULL;
217 edge e;
218 edge_iterator ei;
219 bool ok = true;
220 basic_block bb;
222 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
223 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
224 && !bitmap_bit_p (current->split_bbs, e->src->index))
226 worklist.safe_push (e->src);
227 bitmap_set_bit (seen, e->src->index);
230 while (!worklist.is_empty ())
232 bb = worklist.pop ();
233 FOR_EACH_EDGE (e, ei, bb->preds)
234 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
235 && bitmap_set_bit (seen, e->src->index))
237 gcc_checking_assert (!bitmap_bit_p (current->split_bbs,
238 e->src->index));
239 worklist.safe_push (e->src);
241 for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
242 gsi_next (&bsi))
244 gimple *stmt = gsi_stmt (bsi);
245 if (is_gimple_debug (stmt))
246 continue;
247 if (walk_stmt_load_store_addr_ops
248 (stmt, non_ssa_vars, test_nonssa_use, test_nonssa_use,
249 test_nonssa_use))
251 ok = false;
252 goto done;
254 if (glabel *label_stmt = dyn_cast <glabel *> (stmt))
255 if (test_nonssa_use (stmt, gimple_label_label (label_stmt),
256 NULL_TREE, non_ssa_vars))
258 ok = false;
259 goto done;
262 for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
263 gsi_next (&bsi))
265 if (walk_stmt_load_store_addr_ops
266 (gsi_stmt (bsi), non_ssa_vars, test_nonssa_use, test_nonssa_use,
267 test_nonssa_use))
269 ok = false;
270 goto done;
273 FOR_EACH_EDGE (e, ei, bb->succs)
275 if (e->dest != return_bb)
276 continue;
277 for (gphi_iterator bsi = gsi_start_phis (return_bb);
278 !gsi_end_p (bsi);
279 gsi_next (&bsi))
281 gphi *stmt = bsi.phi ();
282 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
284 if (virtual_operand_p (gimple_phi_result (stmt)))
285 continue;
286 if (TREE_CODE (op) != SSA_NAME
287 && test_nonssa_use (stmt, op, op, non_ssa_vars))
289 ok = false;
290 goto done;
296 /* Verify that the rest of function does not define any label
297 used by the split part. */
298 FOR_EACH_BB_FN (bb, cfun)
299 if (!bitmap_bit_p (current->split_bbs, bb->index)
300 && !bitmap_bit_p (seen, bb->index))
302 gimple_stmt_iterator bsi;
303 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
304 if (glabel *label_stmt = dyn_cast <glabel *> (gsi_stmt (bsi)))
306 if (test_nonssa_use (label_stmt,
307 gimple_label_label (label_stmt),
308 NULL_TREE, non_ssa_vars))
310 ok = false;
311 goto done;
314 else
315 break;
318 done:
319 BITMAP_FREE (seen);
320 worklist.release ();
321 return ok;
324 /* If STMT is a call, check the callee against a list of forbidden
325 predicate functions. If a match is found, look for uses of the
326 call result in condition statements that compare against zero.
327 For each such use, find the block targeted by the condition
328 statement for the nonzero result, and set the bit for this block
329 in the forbidden dominators bitmap. The purpose of this is to avoid
330 selecting a split point where we are likely to lose the chance
331 to optimize away an unused function call. */
333 static void
334 check_forbidden_calls (gimple *stmt)
336 imm_use_iterator use_iter;
337 use_operand_p use_p;
338 tree lhs;
340 /* At the moment, __builtin_constant_p is the only forbidden
341 predicate function call (see PR49642). */
342 if (!gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P))
343 return;
345 lhs = gimple_call_lhs (stmt);
347 if (!lhs || TREE_CODE (lhs) != SSA_NAME)
348 return;
350 FOR_EACH_IMM_USE_FAST (use_p, use_iter, lhs)
352 tree op1;
353 basic_block use_bb, forbidden_bb;
354 enum tree_code code;
355 edge true_edge, false_edge;
356 gcond *use_stmt;
358 use_stmt = dyn_cast <gcond *> (USE_STMT (use_p));
359 if (!use_stmt)
360 continue;
362 /* Assuming canonical form for GIMPLE_COND here, with constant
363 in second position. */
364 op1 = gimple_cond_rhs (use_stmt);
365 code = gimple_cond_code (use_stmt);
366 use_bb = gimple_bb (use_stmt);
368 extract_true_false_edges_from_block (use_bb, &true_edge, &false_edge);
370 /* We're only interested in comparisons that distinguish
371 unambiguously from zero. */
372 if (!integer_zerop (op1) || code == LE_EXPR || code == GE_EXPR)
373 continue;
375 if (code == EQ_EXPR)
376 forbidden_bb = false_edge->dest;
377 else
378 forbidden_bb = true_edge->dest;
380 bitmap_set_bit (forbidden_dominators, forbidden_bb->index);
384 /* If BB is dominated by any block in the forbidden dominators set,
385 return TRUE; else FALSE. */
387 static bool
388 dominated_by_forbidden (basic_block bb)
390 unsigned dom_bb;
391 bitmap_iterator bi;
393 EXECUTE_IF_SET_IN_BITMAP (forbidden_dominators, 1, dom_bb, bi)
395 if (dominated_by_p (CDI_DOMINATORS, bb,
396 BASIC_BLOCK_FOR_FN (cfun, dom_bb)))
397 return true;
400 return false;
403 /* For give split point CURRENT and return block RETURN_BB return 1
404 if ssa name VAL is set by split part and 0 otherwise. */
405 static bool
406 split_part_set_ssa_name_p (tree val, struct split_point *current,
407 basic_block return_bb)
409 if (TREE_CODE (val) != SSA_NAME)
410 return false;
412 return (!SSA_NAME_IS_DEFAULT_DEF (val)
413 && (bitmap_bit_p (current->split_bbs,
414 gimple_bb (SSA_NAME_DEF_STMT (val))->index)
415 || gimple_bb (SSA_NAME_DEF_STMT (val)) == return_bb));
418 /* We found an split_point CURRENT. NON_SSA_VARS is bitmap of all non ssa
419 variables used and RETURN_BB is return basic block.
420 See if we can split function here. */
422 static void
423 consider_split (struct split_point *current, bitmap non_ssa_vars,
424 basic_block return_bb)
426 tree parm;
427 unsigned int num_args = 0;
428 unsigned int call_overhead;
429 edge e;
430 edge_iterator ei;
431 gphi_iterator bsi;
432 unsigned int i;
433 tree retval;
434 bool back_edge = false;
436 if (dump_file && (dump_flags & TDF_DETAILS))
437 dump_split_point (dump_file, current);
439 current->count = profile_count::zero ();
440 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
442 if (e->flags & EDGE_DFS_BACK)
443 back_edge = true;
444 if (!bitmap_bit_p (current->split_bbs, e->src->index))
445 current->count += e->count ();
448 /* Do not split when we would end up calling function anyway.
449 Compares are three state, use !(...<...) to also give up when outcome
450 is unknown. */
451 if (!(current->count
452 < (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.apply_scale
453 (PARAM_VALUE (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY), 100))))
455 /* When profile is guessed, we can not expect it to give us
456 realistic estimate on likelyness of function taking the
457 complex path. As a special case, when tail of the function is
458 a loop, enable splitting since inlining code skipping the loop
459 is likely noticeable win. */
460 if (back_edge
461 && profile_status_for_fn (cfun) != PROFILE_READ
462 && current->count
463 < ENTRY_BLOCK_PTR_FOR_FN (cfun)->count)
465 if (dump_file && (dump_flags & TDF_DETAILS))
467 fprintf (dump_file,
468 " Split before loop, accepting despite low counts");
469 current->count.dump (dump_file);
470 fprintf (dump_file, " ");
471 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.dump (dump_file);
474 else
476 if (dump_file && (dump_flags & TDF_DETAILS))
477 fprintf (dump_file,
478 " Refused: incoming frequency is too large.\n");
479 return;
483 if (!current->header_size)
485 if (dump_file && (dump_flags & TDF_DETAILS))
486 fprintf (dump_file, " Refused: header empty\n");
487 return;
490 /* Verify that PHI args on entry are either virtual or all their operands
491 incoming from header are the same. */
492 for (bsi = gsi_start_phis (current->entry_bb); !gsi_end_p (bsi); gsi_next (&bsi))
494 gphi *stmt = bsi.phi ();
495 tree val = NULL;
497 if (virtual_operand_p (gimple_phi_result (stmt)))
498 continue;
499 for (i = 0; i < gimple_phi_num_args (stmt); i++)
501 edge e = gimple_phi_arg_edge (stmt, i);
502 if (!bitmap_bit_p (current->split_bbs, e->src->index))
504 tree edge_val = gimple_phi_arg_def (stmt, i);
505 if (val && edge_val != val)
507 if (dump_file && (dump_flags & TDF_DETAILS))
508 fprintf (dump_file,
509 " Refused: entry BB has PHI with multiple variants\n");
510 return;
512 val = edge_val;
518 /* See what argument we will pass to the split function and compute
519 call overhead. */
520 call_overhead = eni_size_weights.call_cost;
521 for (parm = DECL_ARGUMENTS (current_function_decl); parm;
522 parm = DECL_CHAIN (parm))
524 if (!is_gimple_reg (parm))
526 if (bitmap_bit_p (non_ssa_vars, DECL_UID (parm)))
528 if (dump_file && (dump_flags & TDF_DETAILS))
529 fprintf (dump_file,
530 " Refused: need to pass non-ssa param values\n");
531 return;
534 else
536 tree ddef = ssa_default_def (cfun, parm);
537 if (ddef
538 && bitmap_bit_p (current->ssa_names_to_pass,
539 SSA_NAME_VERSION (ddef)))
541 if (!VOID_TYPE_P (TREE_TYPE (parm)))
542 call_overhead += estimate_move_cost (TREE_TYPE (parm), false);
543 num_args++;
547 if (!VOID_TYPE_P (TREE_TYPE (current_function_decl)))
548 call_overhead += estimate_move_cost (TREE_TYPE (current_function_decl),
549 false);
551 if (current->split_size <= call_overhead)
553 if (dump_file && (dump_flags & TDF_DETAILS))
554 fprintf (dump_file,
555 " Refused: split size is smaller than call overhead\n");
556 return;
558 /* FIXME: The logic here is not very precise, because inliner does use
559 inline predicates to reduce function body size. We add 10 to anticipate
560 that. Next stage1 we should try to be more meaningful here. */
561 if (current->header_size + call_overhead
562 >= (unsigned int)(DECL_DECLARED_INLINE_P (current_function_decl)
563 ? MAX_INLINE_INSNS_SINGLE
564 : MAX_INLINE_INSNS_AUTO) + 10)
566 if (dump_file && (dump_flags & TDF_DETAILS))
567 fprintf (dump_file,
568 " Refused: header size is too large for inline candidate\n");
569 return;
572 /* Splitting functions brings the target out of comdat group; this will
573 lead to code duplication if the function is reused by other unit.
574 Limit this duplication. This is consistent with limit in tree-sra.c
575 FIXME: with LTO we ought to be able to do better! */
576 if (DECL_ONE_ONLY (current_function_decl)
577 && current->split_size >= (unsigned int) MAX_INLINE_INSNS_AUTO + 10)
579 if (dump_file && (dump_flags & TDF_DETAILS))
580 fprintf (dump_file,
581 " Refused: function is COMDAT and tail is too large\n");
582 return;
584 /* For comdat functions also reject very small tails; those will likely get
585 inlined back and we do not want to risk the duplication overhead.
586 FIXME: with LTO we ought to be able to do better! */
587 if (DECL_ONE_ONLY (current_function_decl)
588 && current->split_size
589 <= (unsigned int) PARAM_VALUE (PARAM_EARLY_INLINING_INSNS) / 2)
591 if (dump_file && (dump_flags & TDF_DETAILS))
592 fprintf (dump_file,
593 " Refused: function is COMDAT and tail is too small\n");
594 return;
597 /* FIXME: we currently can pass only SSA function parameters to the split
598 arguments. Once parm_adjustment infrastructure is supported by cloning,
599 we can pass more than that. */
600 if (num_args != bitmap_count_bits (current->ssa_names_to_pass))
603 if (dump_file && (dump_flags & TDF_DETAILS))
604 fprintf (dump_file,
605 " Refused: need to pass non-param values\n");
606 return;
609 /* When there are non-ssa vars used in the split region, see if they
610 are used in the header region. If so, reject the split.
611 FIXME: we can use nested function support to access both. */
612 if (!bitmap_empty_p (non_ssa_vars)
613 && !verify_non_ssa_vars (current, non_ssa_vars, return_bb))
615 if (dump_file && (dump_flags & TDF_DETAILS))
616 fprintf (dump_file,
617 " Refused: split part has non-ssa uses\n");
618 return;
621 /* If the split point is dominated by a forbidden block, reject
622 the split. */
623 if (!bitmap_empty_p (forbidden_dominators)
624 && dominated_by_forbidden (current->entry_bb))
626 if (dump_file && (dump_flags & TDF_DETAILS))
627 fprintf (dump_file,
628 " Refused: split point dominated by forbidden block\n");
629 return;
632 /* See if retval used by return bb is computed by header or split part.
633 When it is computed by split part, we need to produce return statement
634 in the split part and add code to header to pass it around.
636 This is bit tricky to test:
637 1) When there is no return_bb or no return value, we always pass
638 value around.
639 2) Invariants are always computed by caller.
640 3) For SSA we need to look if defining statement is in header or split part
641 4) For non-SSA we need to look where the var is computed. */
642 retval = find_retval (return_bb);
643 if (!retval)
645 /* If there is a return_bb with no return value in function returning
646 value by reference, also make the split part return void, otherwise
647 we expansion would try to create a non-POD temporary, which is
648 invalid. */
649 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
650 && DECL_RESULT (current_function_decl)
651 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
652 current->split_part_set_retval = false;
653 else
654 current->split_part_set_retval = true;
656 else if (is_gimple_min_invariant (retval))
657 current->split_part_set_retval = false;
658 /* Special case is value returned by reference we record as if it was non-ssa
659 set to result_decl. */
660 else if (TREE_CODE (retval) == SSA_NAME
661 && SSA_NAME_VAR (retval)
662 && TREE_CODE (SSA_NAME_VAR (retval)) == RESULT_DECL
663 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
664 current->split_part_set_retval
665 = bitmap_bit_p (non_ssa_vars, DECL_UID (SSA_NAME_VAR (retval)));
666 else if (TREE_CODE (retval) == SSA_NAME)
667 current->split_part_set_retval
668 = split_part_set_ssa_name_p (retval, current, return_bb);
669 else if (TREE_CODE (retval) == PARM_DECL)
670 current->split_part_set_retval = false;
671 else if (VAR_P (retval)
672 || TREE_CODE (retval) == RESULT_DECL)
673 current->split_part_set_retval
674 = bitmap_bit_p (non_ssa_vars, DECL_UID (retval));
675 else
676 current->split_part_set_retval = true;
678 /* split_function fixes up at most one PHI non-virtual PHI node in return_bb,
679 for the return value. If there are other PHIs, give up. */
680 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
682 gphi_iterator psi;
684 for (psi = gsi_start_phis (return_bb); !gsi_end_p (psi); gsi_next (&psi))
685 if (!virtual_operand_p (gimple_phi_result (psi.phi ()))
686 && !(retval
687 && current->split_part_set_retval
688 && TREE_CODE (retval) == SSA_NAME
689 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
690 && SSA_NAME_DEF_STMT (retval) == psi.phi ()))
692 if (dump_file && (dump_flags & TDF_DETAILS))
693 fprintf (dump_file,
694 " Refused: return bb has extra PHIs\n");
695 return;
699 if (dump_file && (dump_flags & TDF_DETAILS))
700 fprintf (dump_file, " Accepted!\n");
702 /* At the moment chose split point with lowest count and that leaves
703 out smallest size of header.
704 In future we might re-consider this heuristics. */
705 if (!best_split_point.split_bbs
706 || best_split_point.count
707 > current->count
708 || (best_split_point.count == current->count
709 && best_split_point.split_size < current->split_size))
712 if (dump_file && (dump_flags & TDF_DETAILS))
713 fprintf (dump_file, " New best split point!\n");
714 if (best_split_point.ssa_names_to_pass)
716 BITMAP_FREE (best_split_point.ssa_names_to_pass);
717 BITMAP_FREE (best_split_point.split_bbs);
719 best_split_point = *current;
720 best_split_point.ssa_names_to_pass = BITMAP_ALLOC (NULL);
721 bitmap_copy (best_split_point.ssa_names_to_pass,
722 current->ssa_names_to_pass);
723 best_split_point.split_bbs = BITMAP_ALLOC (NULL);
724 bitmap_copy (best_split_point.split_bbs, current->split_bbs);
728 /* Return basic block containing RETURN statement. We allow basic blocks
729 of the form:
730 <retval> = tmp_var;
731 return <retval>
732 but return_bb can not be more complex than this (except for
733 -fsanitize=thread we allow TSAN_FUNC_EXIT () internal call in there).
734 If nothing is found, return the exit block.
736 When there are multiple RETURN statement, chose one with return value,
737 since that one is more likely shared by multiple code paths.
739 Return BB is special, because for function splitting it is the only
740 basic block that is duplicated in between header and split part of the
741 function.
743 TODO: We might support multiple return blocks. */
745 static basic_block
746 find_return_bb (void)
748 edge e;
749 basic_block return_bb = EXIT_BLOCK_PTR_FOR_FN (cfun);
750 gimple_stmt_iterator bsi;
751 bool found_return = false;
752 tree retval = NULL_TREE;
754 if (!single_pred_p (EXIT_BLOCK_PTR_FOR_FN (cfun)))
755 return return_bb;
757 e = single_pred_edge (EXIT_BLOCK_PTR_FOR_FN (cfun));
758 for (bsi = gsi_last_bb (e->src); !gsi_end_p (bsi); gsi_prev (&bsi))
760 gimple *stmt = gsi_stmt (bsi);
761 if (gimple_code (stmt) == GIMPLE_LABEL
762 || is_gimple_debug (stmt)
763 || gimple_clobber_p (stmt))
765 else if (gimple_code (stmt) == GIMPLE_ASSIGN
766 && found_return
767 && gimple_assign_single_p (stmt)
768 && (auto_var_in_fn_p (gimple_assign_rhs1 (stmt),
769 current_function_decl)
770 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
771 && retval == gimple_assign_lhs (stmt))
773 else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
775 found_return = true;
776 retval = gimple_return_retval (return_stmt);
778 /* For -fsanitize=thread, allow also TSAN_FUNC_EXIT () in the return
779 bb. */
780 else if ((flag_sanitize & SANITIZE_THREAD)
781 && gimple_call_internal_p (stmt, IFN_TSAN_FUNC_EXIT))
783 else
784 break;
786 if (gsi_end_p (bsi) && found_return)
787 return_bb = e->src;
789 return return_bb;
792 /* Given return basic block RETURN_BB, see where return value is really
793 stored. */
794 static tree
795 find_retval (basic_block return_bb)
797 gimple_stmt_iterator bsi;
798 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
799 if (greturn *return_stmt = dyn_cast <greturn *> (gsi_stmt (bsi)))
800 return gimple_return_retval (return_stmt);
801 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
802 && !gimple_clobber_p (gsi_stmt (bsi)))
803 return gimple_assign_rhs1 (gsi_stmt (bsi));
804 return NULL;
807 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
808 variable, mark it as used in bitmap passed via DATA.
809 Return true when access to T prevents splitting the function. */
811 static bool
812 mark_nonssa_use (gimple *, tree t, tree, void *data)
814 t = get_base_address (t);
816 if (!t || is_gimple_reg (t))
817 return false;
819 /* At present we can't pass non-SSA arguments to split function.
820 FIXME: this can be relaxed by passing references to arguments. */
821 if (TREE_CODE (t) == PARM_DECL)
823 if (dump_file && (dump_flags & TDF_DETAILS))
824 fprintf (dump_file,
825 "Cannot split: use of non-ssa function parameter.\n");
826 return true;
829 if ((VAR_P (t) && auto_var_in_fn_p (t, current_function_decl))
830 || TREE_CODE (t) == RESULT_DECL
831 || (TREE_CODE (t) == LABEL_DECL && FORCED_LABEL (t)))
832 bitmap_set_bit ((bitmap)data, DECL_UID (t));
834 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
835 to pretend that the value pointed to is actual result decl. */
836 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
837 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
838 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
839 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
840 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
841 return
842 bitmap_bit_p ((bitmap)data,
843 DECL_UID (DECL_RESULT (current_function_decl)));
845 return false;
848 /* Compute local properties of basic block BB we collect when looking for
849 split points. We look for ssa defs and store them in SET_SSA_NAMES,
850 for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic
851 vars stored in NON_SSA_VARS.
853 When BB has edge to RETURN_BB, collect uses in RETURN_BB too.
855 Return false when BB contains something that prevents it from being put into
856 split function. */
858 static bool
859 visit_bb (basic_block bb, basic_block return_bb,
860 bitmap set_ssa_names, bitmap used_ssa_names,
861 bitmap non_ssa_vars)
863 edge e;
864 edge_iterator ei;
865 bool can_split = true;
867 for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
868 gsi_next (&bsi))
870 gimple *stmt = gsi_stmt (bsi);
871 tree op;
872 ssa_op_iter iter;
873 tree decl;
875 if (is_gimple_debug (stmt))
876 continue;
878 if (gimple_clobber_p (stmt))
879 continue;
881 /* FIXME: We can split regions containing EH. We can not however
882 split RESX, EH_DISPATCH and EH_POINTER referring to same region
883 into different partitions. This would require tracking of
884 EH regions and checking in consider_split_point if they
885 are not used elsewhere. */
886 if (gimple_code (stmt) == GIMPLE_RESX)
888 if (dump_file && (dump_flags & TDF_DETAILS))
889 fprintf (dump_file, "Cannot split: resx.\n");
890 can_split = false;
892 if (gimple_code (stmt) == GIMPLE_EH_DISPATCH)
894 if (dump_file && (dump_flags & TDF_DETAILS))
895 fprintf (dump_file, "Cannot split: eh dispatch.\n");
896 can_split = false;
899 /* Check builtins that prevent splitting. */
900 if (gimple_code (stmt) == GIMPLE_CALL
901 && (decl = gimple_call_fndecl (stmt)) != NULL_TREE
902 && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
903 switch (DECL_FUNCTION_CODE (decl))
905 /* FIXME: once we will allow passing non-parm values to split part,
906 we need to be sure to handle correct builtin_stack_save and
907 builtin_stack_restore. At the moment we are safe; there is no
908 way to store builtin_stack_save result in non-SSA variable
909 since all calls to those are compiler generated. */
910 case BUILT_IN_APPLY:
911 case BUILT_IN_APPLY_ARGS:
912 case BUILT_IN_VA_START:
913 if (dump_file && (dump_flags & TDF_DETAILS))
914 fprintf (dump_file,
915 "Cannot split: builtin_apply and va_start.\n");
916 can_split = false;
917 break;
918 case BUILT_IN_EH_POINTER:
919 if (dump_file && (dump_flags & TDF_DETAILS))
920 fprintf (dump_file, "Cannot split: builtin_eh_pointer.\n");
921 can_split = false;
922 break;
923 default:
924 break;
927 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
928 bitmap_set_bit (set_ssa_names, SSA_NAME_VERSION (op));
929 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
930 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
931 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
932 mark_nonssa_use,
933 mark_nonssa_use,
934 mark_nonssa_use);
936 for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
937 gsi_next (&bsi))
939 gphi *stmt = bsi.phi ();
940 unsigned int i;
942 if (virtual_operand_p (gimple_phi_result (stmt)))
943 continue;
944 bitmap_set_bit (set_ssa_names,
945 SSA_NAME_VERSION (gimple_phi_result (stmt)));
946 for (i = 0; i < gimple_phi_num_args (stmt); i++)
948 tree op = gimple_phi_arg_def (stmt, i);
949 if (TREE_CODE (op) == SSA_NAME)
950 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
952 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
953 mark_nonssa_use,
954 mark_nonssa_use,
955 mark_nonssa_use);
957 /* Record also uses coming from PHI operand in return BB. */
958 FOR_EACH_EDGE (e, ei, bb->succs)
959 if (e->dest == return_bb)
961 for (gphi_iterator bsi = gsi_start_phis (return_bb);
962 !gsi_end_p (bsi);
963 gsi_next (&bsi))
965 gphi *stmt = bsi.phi ();
966 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
968 if (virtual_operand_p (gimple_phi_result (stmt)))
969 continue;
970 if (TREE_CODE (op) == SSA_NAME)
971 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
972 else
973 can_split &= !mark_nonssa_use (stmt, op, op, non_ssa_vars);
976 return can_split;
979 /* Stack entry for recursive DFS walk in find_split_point. */
981 struct stack_entry
983 /* Basic block we are examining. */
984 basic_block bb;
986 /* SSA names set and used by the BB and all BBs reachable
987 from it via DFS walk. */
988 bitmap set_ssa_names, used_ssa_names;
989 bitmap non_ssa_vars;
991 /* All BBS visited from this BB via DFS walk. */
992 bitmap bbs_visited;
994 /* Last examined edge in DFS walk. Since we walk unoriented graph,
995 the value is up to sum of incoming and outgoing edges of BB. */
996 unsigned int edge_num;
998 /* Stack entry index of earliest BB reachable from current BB
999 or any BB visited later in DFS walk. */
1000 int earliest;
1002 /* Overall time and size of all BBs reached from this BB in DFS walk. */
1003 sreal overall_time;
1004 int overall_size;
1006 /* When false we can not split on this BB. */
1007 bool can_split;
1011 /* Find all articulations and call consider_split on them.
1012 OVERALL_TIME and OVERALL_SIZE is time and size of the function.
1014 We perform basic algorithm for finding an articulation in a graph
1015 created from CFG by considering it to be an unoriented graph.
1017 The articulation is discovered via DFS walk. We collect earliest
1018 basic block on stack that is reachable via backward edge. Articulation
1019 is any basic block such that there is no backward edge bypassing it.
1020 To reduce stack usage we maintain heap allocated stack in STACK vector.
1021 AUX pointer of BB is set to index it appears in the stack or -1 once
1022 it is visited and popped off the stack.
1024 The algorithm finds articulation after visiting the whole component
1025 reachable by it. This makes it convenient to collect information about
1026 the component used by consider_split. */
1028 static void
1029 find_split_points (basic_block return_bb, sreal overall_time, int overall_size)
1031 stack_entry first;
1032 vec<stack_entry> stack = vNULL;
1033 basic_block bb;
1034 struct split_point current;
1036 current.header_time = overall_time;
1037 current.header_size = overall_size;
1038 current.split_time = 0;
1039 current.split_size = 0;
1040 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
1042 first.bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1043 first.edge_num = 0;
1044 first.overall_time = 0;
1045 first.overall_size = 0;
1046 first.earliest = INT_MAX;
1047 first.set_ssa_names = 0;
1048 first.used_ssa_names = 0;
1049 first.non_ssa_vars = 0;
1050 first.bbs_visited = 0;
1051 first.can_split = false;
1052 stack.safe_push (first);
1053 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = (void *)(intptr_t)-1;
1055 while (!stack.is_empty ())
1057 stack_entry *entry = &stack.last ();
1059 /* We are walking an acyclic graph, so edge_num counts
1060 succ and pred edges together. However when considering
1061 articulation, we want to have processed everything reachable
1062 from articulation but nothing that reaches into it. */
1063 if (entry->edge_num == EDGE_COUNT (entry->bb->succs)
1064 && entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1066 int pos = stack.length ();
1067 entry->can_split &= visit_bb (entry->bb, return_bb,
1068 entry->set_ssa_names,
1069 entry->used_ssa_names,
1070 entry->non_ssa_vars);
1071 if (pos <= entry->earliest && !entry->can_split
1072 && dump_file && (dump_flags & TDF_DETAILS))
1073 fprintf (dump_file,
1074 "found articulation at bb %i but can not split\n",
1075 entry->bb->index);
1076 if (pos <= entry->earliest && entry->can_split)
1078 if (dump_file && (dump_flags & TDF_DETAILS))
1079 fprintf (dump_file, "found articulation at bb %i\n",
1080 entry->bb->index);
1081 current.entry_bb = entry->bb;
1082 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
1083 bitmap_and_compl (current.ssa_names_to_pass,
1084 entry->used_ssa_names, entry->set_ssa_names);
1085 current.header_time = overall_time - entry->overall_time;
1086 current.header_size = overall_size - entry->overall_size;
1087 current.split_time = entry->overall_time;
1088 current.split_size = entry->overall_size;
1089 current.split_bbs = entry->bbs_visited;
1090 consider_split (&current, entry->non_ssa_vars, return_bb);
1091 BITMAP_FREE (current.ssa_names_to_pass);
1094 /* Do actual DFS walk. */
1095 if (entry->edge_num
1096 < (EDGE_COUNT (entry->bb->succs)
1097 + EDGE_COUNT (entry->bb->preds)))
1099 edge e;
1100 basic_block dest;
1101 if (entry->edge_num < EDGE_COUNT (entry->bb->succs))
1103 e = EDGE_SUCC (entry->bb, entry->edge_num);
1104 dest = e->dest;
1106 else
1108 e = EDGE_PRED (entry->bb, entry->edge_num
1109 - EDGE_COUNT (entry->bb->succs));
1110 dest = e->src;
1113 entry->edge_num++;
1115 /* New BB to visit, push it to the stack. */
1116 if (dest != return_bb && dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
1117 && !dest->aux)
1119 stack_entry new_entry;
1121 new_entry.bb = dest;
1122 new_entry.edge_num = 0;
1123 new_entry.overall_time
1124 = bb_info_vec[dest->index].time;
1125 new_entry.overall_size
1126 = bb_info_vec[dest->index].size;
1127 new_entry.earliest = INT_MAX;
1128 new_entry.set_ssa_names = BITMAP_ALLOC (NULL);
1129 new_entry.used_ssa_names = BITMAP_ALLOC (NULL);
1130 new_entry.bbs_visited = BITMAP_ALLOC (NULL);
1131 new_entry.non_ssa_vars = BITMAP_ALLOC (NULL);
1132 new_entry.can_split = true;
1133 bitmap_set_bit (new_entry.bbs_visited, dest->index);
1134 stack.safe_push (new_entry);
1135 dest->aux = (void *)(intptr_t)stack.length ();
1137 /* Back edge found, record the earliest point. */
1138 else if ((intptr_t)dest->aux > 0
1139 && (intptr_t)dest->aux < entry->earliest)
1140 entry->earliest = (intptr_t)dest->aux;
1142 /* We are done with examining the edges. Pop off the value from stack
1143 and merge stuff we accumulate during the walk. */
1144 else if (entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1146 stack_entry *prev = &stack[stack.length () - 2];
1148 entry->bb->aux = (void *)(intptr_t)-1;
1149 prev->can_split &= entry->can_split;
1150 if (prev->set_ssa_names)
1152 bitmap_ior_into (prev->set_ssa_names, entry->set_ssa_names);
1153 bitmap_ior_into (prev->used_ssa_names, entry->used_ssa_names);
1154 bitmap_ior_into (prev->bbs_visited, entry->bbs_visited);
1155 bitmap_ior_into (prev->non_ssa_vars, entry->non_ssa_vars);
1157 if (prev->earliest > entry->earliest)
1158 prev->earliest = entry->earliest;
1159 prev->overall_time += entry->overall_time;
1160 prev->overall_size += entry->overall_size;
1161 BITMAP_FREE (entry->set_ssa_names);
1162 BITMAP_FREE (entry->used_ssa_names);
1163 BITMAP_FREE (entry->bbs_visited);
1164 BITMAP_FREE (entry->non_ssa_vars);
1165 stack.pop ();
1167 else
1168 stack.pop ();
1170 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = NULL;
1171 FOR_EACH_BB_FN (bb, cfun)
1172 bb->aux = NULL;
1173 stack.release ();
1174 BITMAP_FREE (current.ssa_names_to_pass);
1177 /* Split function at SPLIT_POINT. */
1179 static void
1180 split_function (basic_block return_bb, struct split_point *split_point,
1181 bool add_tsan_func_exit)
1183 vec<tree> args_to_pass = vNULL;
1184 bitmap args_to_skip;
1185 tree parm;
1186 int num = 0;
1187 cgraph_node *node, *cur_node = cgraph_node::get (current_function_decl);
1188 basic_block call_bb;
1189 gcall *call, *tsan_func_exit_call = NULL;
1190 edge e;
1191 edge_iterator ei;
1192 tree retval = NULL, real_retval = NULL;
1193 gimple *last_stmt = NULL;
1194 unsigned int i;
1195 tree arg, ddef;
1197 if (dump_file)
1199 fprintf (dump_file, "\n\nSplitting function at:\n");
1200 dump_split_point (dump_file, split_point);
1203 if (cur_node->local.can_change_signature)
1204 args_to_skip = BITMAP_ALLOC (NULL);
1205 else
1206 args_to_skip = NULL;
1208 /* Collect the parameters of new function and args_to_skip bitmap. */
1209 for (parm = DECL_ARGUMENTS (current_function_decl);
1210 parm; parm = DECL_CHAIN (parm), num++)
1211 if (args_to_skip
1212 && (!is_gimple_reg (parm)
1213 || (ddef = ssa_default_def (cfun, parm)) == NULL_TREE
1214 || !bitmap_bit_p (split_point->ssa_names_to_pass,
1215 SSA_NAME_VERSION (ddef))))
1216 bitmap_set_bit (args_to_skip, num);
1217 else
1219 /* This parm might not have been used up to now, but is going to be
1220 used, hence register it. */
1221 if (is_gimple_reg (parm))
1222 arg = get_or_create_ssa_default_def (cfun, parm);
1223 else
1224 arg = parm;
1226 if (!useless_type_conversion_p (DECL_ARG_TYPE (parm), TREE_TYPE (arg)))
1227 arg = fold_convert (DECL_ARG_TYPE (parm), arg);
1228 args_to_pass.safe_push (arg);
1231 /* See if the split function will return. */
1232 bool split_part_return_p = false;
1233 FOR_EACH_EDGE (e, ei, return_bb->preds)
1235 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1236 split_part_return_p = true;
1239 /* Add return block to what will become the split function.
1240 We do not return; no return block is needed. */
1241 if (!split_part_return_p)
1243 /* We have no return block, so nothing is needed. */
1244 else if (return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1246 /* When we do not want to return value, we need to construct
1247 new return block with empty return statement.
1248 FIXME: Once we are able to change return type, we should change function
1249 to return void instead of just outputting function with undefined return
1250 value. For structures this affects quality of codegen. */
1251 else if ((retval = find_retval (return_bb))
1252 && !split_point->split_part_set_retval)
1254 bool redirected = true;
1255 basic_block new_return_bb = create_basic_block (NULL, 0, return_bb);
1256 gimple_stmt_iterator gsi = gsi_start_bb (new_return_bb);
1257 gsi_insert_after (&gsi, gimple_build_return (NULL), GSI_NEW_STMT);
1258 new_return_bb->count = profile_count::zero ();
1259 while (redirected)
1261 redirected = false;
1262 FOR_EACH_EDGE (e, ei, return_bb->preds)
1263 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1265 new_return_bb->count += e->count ();
1266 redirect_edge_and_branch (e, new_return_bb);
1267 redirected = true;
1268 break;
1271 e = make_single_succ_edge (new_return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1272 add_bb_to_loop (new_return_bb, current_loops->tree_root);
1273 bitmap_set_bit (split_point->split_bbs, new_return_bb->index);
1275 /* When we pass around the value, use existing return block. */
1276 else
1277 bitmap_set_bit (split_point->split_bbs, return_bb->index);
1279 /* If RETURN_BB has virtual operand PHIs, they must be removed and the
1280 virtual operand marked for renaming as we change the CFG in a way that
1281 tree-inline is not able to compensate for.
1283 Note this can happen whether or not we have a return value. If we have
1284 a return value, then RETURN_BB may have PHIs for real operands too. */
1285 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1287 bool phi_p = false;
1288 for (gphi_iterator gsi = gsi_start_phis (return_bb);
1289 !gsi_end_p (gsi);)
1291 gphi *stmt = gsi.phi ();
1292 if (!virtual_operand_p (gimple_phi_result (stmt)))
1294 gsi_next (&gsi);
1295 continue;
1297 mark_virtual_phi_result_for_renaming (stmt);
1298 remove_phi_node (&gsi, true);
1299 phi_p = true;
1301 /* In reality we have to rename the reaching definition of the
1302 virtual operand at return_bb as we will eventually release it
1303 when we remove the code region we outlined.
1304 So we have to rename all immediate virtual uses of that region
1305 if we didn't see a PHI definition yet. */
1306 /* ??? In real reality we want to set the reaching vdef of the
1307 entry of the SESE region as the vuse of the call and the reaching
1308 vdef of the exit of the SESE region as the vdef of the call. */
1309 if (!phi_p)
1310 for (gimple_stmt_iterator gsi = gsi_start_bb (return_bb);
1311 !gsi_end_p (gsi);
1312 gsi_next (&gsi))
1314 gimple *stmt = gsi_stmt (gsi);
1315 if (gimple_vuse (stmt))
1317 gimple_set_vuse (stmt, NULL_TREE);
1318 update_stmt (stmt);
1320 if (gimple_vdef (stmt))
1321 break;
1325 /* Now create the actual clone. */
1326 cgraph_edge::rebuild_edges ();
1327 node = cur_node->create_version_clone_with_body
1328 (vNULL, NULL, args_to_skip,
1329 !split_part_return_p || !split_point->split_part_set_retval,
1330 split_point->split_bbs, split_point->entry_bb, "part");
1332 node->split_part = true;
1334 if (cur_node->same_comdat_group)
1336 /* TODO: call is versionable if we make sure that all
1337 callers are inside of a comdat group. */
1338 cur_node->calls_comdat_local = 1;
1339 node->add_to_same_comdat_group (cur_node);
1343 /* Let's take a time profile for splitted function. */
1344 node->tp_first_run = cur_node->tp_first_run + 1;
1346 /* For usual cloning it is enough to clear builtin only when signature
1347 changes. For partial inlining we however can not expect the part
1348 of builtin implementation to have same semantic as the whole. */
1349 if (fndecl_built_in_p (node->decl))
1351 DECL_BUILT_IN_CLASS (node->decl) = NOT_BUILT_IN;
1352 DECL_FUNCTION_CODE (node->decl) = (enum built_in_function) 0;
1355 /* If return_bb contains any clobbers that refer to SSA_NAMEs
1356 set in the split part, remove them. Also reset debug stmts that
1357 refer to SSA_NAMEs set in the split part. */
1358 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1360 gimple_stmt_iterator gsi = gsi_start_bb (return_bb);
1361 while (!gsi_end_p (gsi))
1363 tree op;
1364 ssa_op_iter iter;
1365 gimple *stmt = gsi_stmt (gsi);
1366 bool remove = false;
1367 if (gimple_clobber_p (stmt) || is_gimple_debug (stmt))
1368 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
1370 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (op));
1371 if (op != retval
1372 && bb
1373 && bb != return_bb
1374 && bitmap_bit_p (split_point->split_bbs, bb->index))
1376 if (is_gimple_debug (stmt))
1378 gimple_debug_bind_reset_value (stmt);
1379 update_stmt (stmt);
1381 else
1382 remove = true;
1383 break;
1386 if (remove)
1387 gsi_remove (&gsi, true);
1388 else
1389 gsi_next (&gsi);
1393 /* If the original function is declared inline, there is no point in issuing
1394 a warning for the non-inlinable part. */
1395 DECL_NO_INLINE_WARNING_P (node->decl) = 1;
1396 cur_node->remove_callees ();
1397 cur_node->remove_all_references ();
1398 if (!split_part_return_p)
1399 TREE_THIS_VOLATILE (node->decl) = 1;
1400 if (dump_file)
1401 dump_function_to_file (node->decl, dump_file, dump_flags);
1403 /* Create the basic block we place call into. It is the entry basic block
1404 split after last label. */
1405 call_bb = split_point->entry_bb;
1406 for (gimple_stmt_iterator gsi = gsi_start_bb (call_bb); !gsi_end_p (gsi);)
1407 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
1409 last_stmt = gsi_stmt (gsi);
1410 gsi_next (&gsi);
1412 else
1413 break;
1414 call_bb->count = split_point->count;
1415 e = split_block (split_point->entry_bb, last_stmt);
1416 remove_edge (e);
1418 /* Produce the call statement. */
1419 gimple_stmt_iterator gsi = gsi_last_bb (call_bb);
1420 FOR_EACH_VEC_ELT (args_to_pass, i, arg)
1421 if (!is_gimple_val (arg))
1423 arg = force_gimple_operand_gsi (&gsi, arg, true, NULL_TREE,
1424 false, GSI_CONTINUE_LINKING);
1425 args_to_pass[i] = arg;
1427 call = gimple_build_call_vec (node->decl, args_to_pass);
1428 gimple_set_block (call, DECL_INITIAL (current_function_decl));
1429 args_to_pass.release ();
1431 /* For optimized away parameters, add on the caller side
1432 before the call
1433 DEBUG D#X => parm_Y(D)
1434 stmts and associate D#X with parm in decl_debug_args_lookup
1435 vector to say for debug info that if parameter parm had been passed,
1436 it would have value parm_Y(D). */
1437 if (args_to_skip)
1439 vec<tree, va_gc> **debug_args = NULL;
1440 unsigned i = 0, len = 0;
1441 if (MAY_HAVE_DEBUG_BIND_STMTS)
1443 debug_args = decl_debug_args_lookup (node->decl);
1444 if (debug_args)
1445 len = vec_safe_length (*debug_args);
1447 for (parm = DECL_ARGUMENTS (current_function_decl), num = 0;
1448 parm; parm = DECL_CHAIN (parm), num++)
1449 if (bitmap_bit_p (args_to_skip, num) && is_gimple_reg (parm))
1451 tree ddecl;
1452 gimple *def_temp;
1454 /* This needs to be done even without
1455 MAY_HAVE_DEBUG_BIND_STMTS, otherwise if it didn't exist
1456 before, we'd end up with different SSA_NAME_VERSIONs
1457 between -g and -g0. */
1458 arg = get_or_create_ssa_default_def (cfun, parm);
1459 if (!MAY_HAVE_DEBUG_BIND_STMTS || debug_args == NULL)
1460 continue;
1462 while (i < len && (**debug_args)[i] != DECL_ORIGIN (parm))
1463 i += 2;
1464 if (i >= len)
1465 continue;
1466 ddecl = (**debug_args)[i + 1];
1467 def_temp
1468 = gimple_build_debug_bind (ddecl, unshare_expr (arg), call);
1469 gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
1473 /* We avoid address being taken on any variable used by split part,
1474 so return slot optimization is always possible. Moreover this is
1475 required to make DECL_BY_REFERENCE work. */
1476 if (aggregate_value_p (DECL_RESULT (current_function_decl),
1477 TREE_TYPE (current_function_decl))
1478 && (!is_gimple_reg_type (TREE_TYPE (DECL_RESULT (current_function_decl)))
1479 || DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))))
1480 gimple_call_set_return_slot_opt (call, true);
1482 if (add_tsan_func_exit)
1483 tsan_func_exit_call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
1485 /* Update return value. This is bit tricky. When we do not return,
1486 do nothing. When we return we might need to update return_bb
1487 or produce a new return statement. */
1488 if (!split_part_return_p)
1490 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1491 if (tsan_func_exit_call)
1492 gsi_insert_after (&gsi, tsan_func_exit_call, GSI_NEW_STMT);
1494 else
1496 e = make_single_succ_edge (call_bb, return_bb,
1497 return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
1498 ? 0 : EDGE_FALLTHRU);
1500 /* If there is return basic block, see what value we need to store
1501 return value into and put call just before it. */
1502 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1504 real_retval = retval;
1505 if (real_retval && split_point->split_part_set_retval)
1507 gphi_iterator psi;
1509 /* See if we need new SSA_NAME for the result.
1510 When DECL_BY_REFERENCE is true, retval is actually pointer to
1511 return value and it is constant in whole function. */
1512 if (TREE_CODE (retval) == SSA_NAME
1513 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1515 retval = copy_ssa_name (retval, call);
1517 /* See if there is PHI defining return value. */
1518 for (psi = gsi_start_phis (return_bb);
1519 !gsi_end_p (psi); gsi_next (&psi))
1520 if (!virtual_operand_p (gimple_phi_result (psi.phi ())))
1521 break;
1523 /* When there is PHI, just update its value. */
1524 if (TREE_CODE (retval) == SSA_NAME
1525 && !gsi_end_p (psi))
1526 add_phi_arg (psi.phi (), retval, e, UNKNOWN_LOCATION);
1527 /* Otherwise update the return BB itself.
1528 find_return_bb allows at most one assignment to return value,
1529 so update first statement. */
1530 else
1532 gimple_stmt_iterator bsi;
1533 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi);
1534 gsi_next (&bsi))
1535 if (greturn *return_stmt
1536 = dyn_cast <greturn *> (gsi_stmt (bsi)))
1538 gimple_return_set_retval (return_stmt, retval);
1539 break;
1541 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
1542 && !gimple_clobber_p (gsi_stmt (bsi)))
1544 gimple_assign_set_rhs1 (gsi_stmt (bsi), retval);
1545 break;
1547 update_stmt (gsi_stmt (bsi));
1548 /* Also adjust clobbers and debug stmts in return_bb. */
1549 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi);
1550 gsi_next (&bsi))
1552 gimple *stmt = gsi_stmt (bsi);
1553 if (gimple_clobber_p (stmt)
1554 || is_gimple_debug (stmt))
1556 ssa_op_iter iter;
1557 use_operand_p use_p;
1558 bool update = false;
1559 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter,
1560 SSA_OP_USE)
1561 if (USE_FROM_PTR (use_p) == real_retval)
1563 SET_USE (use_p, retval);
1564 update = true;
1566 if (update)
1567 update_stmt (stmt);
1572 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1574 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1575 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1577 else
1579 tree restype;
1580 restype = TREE_TYPE (DECL_RESULT (current_function_decl));
1581 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1582 if (!useless_type_conversion_p (TREE_TYPE (retval), restype))
1584 gimple *cpy;
1585 tree tem = create_tmp_reg (restype);
1586 tem = make_ssa_name (tem, call);
1587 cpy = gimple_build_assign (retval, NOP_EXPR, tem);
1588 gsi_insert_after (&gsi, cpy, GSI_NEW_STMT);
1589 retval = tem;
1591 gimple_call_set_lhs (call, retval);
1592 update_stmt (call);
1595 else
1596 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1597 if (tsan_func_exit_call)
1598 gsi_insert_after (&gsi, tsan_func_exit_call, GSI_NEW_STMT);
1600 /* We don't use return block (there is either no return in function or
1601 multiple of them). So create new basic block with return statement.
1603 else
1605 greturn *ret;
1606 if (split_point->split_part_set_retval
1607 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1609 retval = DECL_RESULT (current_function_decl);
1611 /* We use temporary register to hold value when aggregate_value_p
1612 is false. Similarly for DECL_BY_REFERENCE we must avoid extra
1613 copy. */
1614 if (!aggregate_value_p (retval, TREE_TYPE (current_function_decl))
1615 && !DECL_BY_REFERENCE (retval))
1616 retval = create_tmp_reg (TREE_TYPE (retval));
1617 if (is_gimple_reg (retval))
1619 /* When returning by reference, there is only one SSA name
1620 assigned to RESULT_DECL (that is pointer to return value).
1621 Look it up or create new one if it is missing. */
1622 if (DECL_BY_REFERENCE (retval))
1623 retval = get_or_create_ssa_default_def (cfun, retval);
1624 /* Otherwise produce new SSA name for return value. */
1625 else
1626 retval = make_ssa_name (retval, call);
1628 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1629 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1630 else
1631 gimple_call_set_lhs (call, retval);
1632 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1634 else
1636 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1637 if (retval
1638 && is_gimple_reg_type (TREE_TYPE (retval))
1639 && !is_gimple_val (retval))
1641 gassign *g
1642 = gimple_build_assign (make_ssa_name (TREE_TYPE (retval)),
1643 retval);
1644 retval = gimple_assign_lhs (g);
1645 gsi_insert_after (&gsi, g, GSI_NEW_STMT);
1648 if (tsan_func_exit_call)
1649 gsi_insert_after (&gsi, tsan_func_exit_call, GSI_NEW_STMT);
1650 ret = gimple_build_return (retval);
1651 gsi_insert_after (&gsi, ret, GSI_NEW_STMT);
1654 free_dominance_info (CDI_DOMINATORS);
1655 free_dominance_info (CDI_POST_DOMINATORS);
1656 compute_fn_summary (node, true);
1659 /* Execute function splitting pass. */
1661 static unsigned int
1662 execute_split_functions (void)
1664 gimple_stmt_iterator bsi;
1665 basic_block bb;
1666 sreal overall_time = 0;
1667 int overall_size = 0;
1668 int todo = 0;
1669 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1671 if (flags_from_decl_or_type (current_function_decl)
1672 & (ECF_NORETURN|ECF_MALLOC))
1674 if (dump_file)
1675 fprintf (dump_file, "Not splitting: noreturn/malloc function.\n");
1676 return 0;
1678 if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
1680 if (dump_file)
1681 fprintf (dump_file, "Not splitting: main function.\n");
1682 return 0;
1684 if (node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
1686 if (dump_file)
1687 fprintf (dump_file, "Not splitting: function is unlikely executed.\n");
1688 return 0;
1690 /* This can be relaxed; function might become inlinable after splitting
1691 away the uninlinable part. */
1692 if (ipa_fn_summaries
1693 && ipa_fn_summaries->get (node)
1694 && !ipa_fn_summaries->get (node)->inlinable)
1696 if (dump_file)
1697 fprintf (dump_file, "Not splitting: not inlinable.\n");
1698 return 0;
1700 if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1702 if (dump_file)
1703 fprintf (dump_file, "Not splitting: disregarding inline limits.\n");
1704 return 0;
1706 /* This can be relaxed; most of versioning tests actually prevents
1707 a duplication. */
1708 if (!tree_versionable_function_p (current_function_decl))
1710 if (dump_file)
1711 fprintf (dump_file, "Not splitting: not versionable.\n");
1712 return 0;
1714 /* FIXME: we could support this. */
1715 if (DECL_STRUCT_FUNCTION (current_function_decl)->static_chain_decl)
1717 if (dump_file)
1718 fprintf (dump_file, "Not splitting: nested function.\n");
1719 return 0;
1722 /* See if it makes sense to try to split.
1723 It makes sense to split if we inline, that is if we have direct calls to
1724 handle or direct calls are possibly going to appear as result of indirect
1725 inlining or LTO. Also handle -fprofile-generate as LTO to allow non-LTO
1726 training for LTO -fprofile-use build.
1728 Note that we are not completely conservative about disqualifying functions
1729 called once. It is possible that the caller is called more then once and
1730 then inlining would still benefit. */
1731 if ((!node->callers
1732 /* Local functions called once will be completely inlined most of time. */
1733 || (!node->callers->next_caller && node->local.local))
1734 && !node->address_taken
1735 && !node->has_aliases_p ()
1736 && (!flag_lto || !node->externally_visible))
1738 if (dump_file)
1739 fprintf (dump_file, "Not splitting: not called directly "
1740 "or called once.\n");
1741 return 0;
1744 /* FIXME: We can actually split if splitting reduces call overhead. */
1745 if (!flag_inline_small_functions
1746 && !DECL_DECLARED_INLINE_P (current_function_decl))
1748 if (dump_file)
1749 fprintf (dump_file, "Not splitting: not autoinlining and function"
1750 " is not inline.\n");
1751 return 0;
1754 /* We enforce splitting after loop headers when profile info is not
1755 available. */
1756 if (profile_status_for_fn (cfun) != PROFILE_READ)
1757 mark_dfs_back_edges ();
1759 /* Initialize bitmap to track forbidden calls. */
1760 forbidden_dominators = BITMAP_ALLOC (NULL);
1761 calculate_dominance_info (CDI_DOMINATORS);
1763 /* Compute local info about basic blocks and determine function size/time. */
1764 bb_info_vec.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
1765 best_split_point.split_bbs = NULL;
1766 basic_block return_bb = find_return_bb ();
1767 int tsan_exit_found = -1;
1768 FOR_EACH_BB_FN (bb, cfun)
1770 sreal time = 0;
1771 int size = 0;
1772 sreal freq = bb->count.to_sreal_scale
1773 (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count);
1775 if (dump_file && (dump_flags & TDF_DETAILS))
1776 fprintf (dump_file, "Basic block %i\n", bb->index);
1778 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1780 sreal this_time;
1781 int this_size;
1782 gimple *stmt = gsi_stmt (bsi);
1784 this_size = estimate_num_insns (stmt, &eni_size_weights);
1785 this_time = (sreal)estimate_num_insns (stmt, &eni_time_weights)
1786 * freq;
1787 size += this_size;
1788 time += this_time;
1789 check_forbidden_calls (stmt);
1791 if (dump_file && (dump_flags & TDF_DETAILS))
1793 fprintf (dump_file, " freq:%4.2f size:%3i time:%4.2f ",
1794 freq.to_double (), this_size, this_time.to_double ());
1795 print_gimple_stmt (dump_file, stmt, 0);
1798 if ((flag_sanitize & SANITIZE_THREAD)
1799 && gimple_call_internal_p (stmt, IFN_TSAN_FUNC_EXIT))
1801 /* We handle TSAN_FUNC_EXIT for splitting either in the
1802 return_bb, or in its immediate predecessors. */
1803 if ((bb != return_bb && !find_edge (bb, return_bb))
1804 || (tsan_exit_found != -1
1805 && tsan_exit_found != (bb != return_bb)))
1807 if (dump_file)
1808 fprintf (dump_file, "Not splitting: TSAN_FUNC_EXIT"
1809 " in unexpected basic block.\n");
1810 BITMAP_FREE (forbidden_dominators);
1811 bb_info_vec.release ();
1812 return 0;
1814 tsan_exit_found = bb != return_bb;
1817 overall_time += time;
1818 overall_size += size;
1819 bb_info_vec[bb->index].time = time;
1820 bb_info_vec[bb->index].size = size;
1822 find_split_points (return_bb, overall_time, overall_size);
1823 if (best_split_point.split_bbs)
1825 split_function (return_bb, &best_split_point, tsan_exit_found == 1);
1826 BITMAP_FREE (best_split_point.ssa_names_to_pass);
1827 BITMAP_FREE (best_split_point.split_bbs);
1828 todo = TODO_update_ssa | TODO_cleanup_cfg;
1830 BITMAP_FREE (forbidden_dominators);
1831 bb_info_vec.release ();
1832 return todo;
1835 namespace {
1837 const pass_data pass_data_split_functions =
1839 GIMPLE_PASS, /* type */
1840 "fnsplit", /* name */
1841 OPTGROUP_NONE, /* optinfo_flags */
1842 TV_IPA_FNSPLIT, /* tv_id */
1843 PROP_cfg, /* properties_required */
1844 0, /* properties_provided */
1845 0, /* properties_destroyed */
1846 0, /* todo_flags_start */
1847 0, /* todo_flags_finish */
1850 class pass_split_functions : public gimple_opt_pass
1852 public:
1853 pass_split_functions (gcc::context *ctxt)
1854 : gimple_opt_pass (pass_data_split_functions, ctxt)
1857 /* opt_pass methods: */
1858 virtual bool gate (function *);
1859 virtual unsigned int execute (function *)
1861 return execute_split_functions ();
1864 }; // class pass_split_functions
1866 bool
1867 pass_split_functions::gate (function *)
1869 /* When doing profile feedback, we want to execute the pass after profiling
1870 is read. So disable one in early optimization. */
1871 return (flag_partial_inlining
1872 && !profile_arc_flag && !flag_branch_probabilities);
1875 } // anon namespace
1877 gimple_opt_pass *
1878 make_pass_split_functions (gcc::context *ctxt)
1880 return new pass_split_functions (ctxt);
1883 /* Execute function splitting pass. */
1885 static unsigned int
1886 execute_feedback_split_functions (void)
1888 unsigned int retval = execute_split_functions ();
1889 if (retval)
1890 retval |= TODO_rebuild_cgraph_edges;
1891 return retval;
1894 namespace {
1896 const pass_data pass_data_feedback_split_functions =
1898 GIMPLE_PASS, /* type */
1899 "feedback_fnsplit", /* name */
1900 OPTGROUP_NONE, /* optinfo_flags */
1901 TV_IPA_FNSPLIT, /* tv_id */
1902 PROP_cfg, /* properties_required */
1903 0, /* properties_provided */
1904 0, /* properties_destroyed */
1905 0, /* todo_flags_start */
1906 0, /* todo_flags_finish */
1909 class pass_feedback_split_functions : public gimple_opt_pass
1911 public:
1912 pass_feedback_split_functions (gcc::context *ctxt)
1913 : gimple_opt_pass (pass_data_feedback_split_functions, ctxt)
1916 /* opt_pass methods: */
1917 virtual bool gate (function *);
1918 virtual unsigned int execute (function *)
1920 return execute_feedback_split_functions ();
1923 }; // class pass_feedback_split_functions
1925 bool
1926 pass_feedback_split_functions::gate (function *)
1928 /* We don't need to split when profiling at all, we are producing
1929 lousy code anyway. */
1930 return (flag_partial_inlining
1931 && flag_branch_probabilities);
1934 } // anon namespace
1936 gimple_opt_pass *
1937 make_pass_feedback_split_functions (gcc::context *ctxt)
1939 return new pass_feedback_split_functions (ctxt);