1 /* Function splitting pass
2 Copyright (C) 2010-2024 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
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
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:
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
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
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 #define INCLUDE_MEMORY
80 #include "coretypes.h"
86 #include "alloc-pool.h"
87 #include "tree-pass.h"
90 #include "diagnostic.h"
91 #include "fold-const.h"
95 #include "gimple-iterator.h"
96 #include "gimplify-me.h"
97 #include "gimple-walk.h"
98 #include "symbol-summary.h"
101 #include "ipa-prop.h"
102 #include "tree-cfg.h"
103 #include "tree-into-ssa.h"
104 #include "tree-dfa.h"
105 #include "tree-inline.h"
106 #include "gimple-pretty-print.h"
107 #include "ipa-fnsummary.h"
110 #include "ipa-strub.h"
112 /* Per basic block info. */
121 static vec
<split_bb_info
> bb_info_vec
;
123 /* Description of split point. */
128 /* Size of the partitions. */
129 sreal header_time
, split_time
;
130 unsigned int header_size
, split_size
;
132 /* SSA names that need to be passed into spit function. */
133 bitmap ssa_names_to_pass
;
135 /* Basic block where we split (that will become entry point of new function. */
136 basic_block entry_bb
;
138 /* Count for entering the split part.
139 This is not count of the entry_bb because it may be in loop. */
142 /* Basic blocks we are splitting away. */
145 /* True when return value is computed on split part and thus it needs
147 bool split_part_set_retval
;
150 /* Best split point found. */
152 class split_point best_split_point
;
154 /* Set of basic blocks that are not allowed to dominate a split point. */
156 static bitmap forbidden_dominators
;
158 static tree
find_retval (basic_block return_bb
);
160 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
161 variable, check it if it is present in bitmap passed via DATA. */
164 test_nonssa_use (gimple
*, tree t
, tree
, void *data
)
166 t
= get_base_address (t
);
168 if (!t
|| is_gimple_reg (t
))
171 if (TREE_CODE (t
) == PARM_DECL
173 && auto_var_in_fn_p (t
, current_function_decl
))
174 || TREE_CODE (t
) == RESULT_DECL
175 /* Normal labels are part of CFG and will be handled gratefully.
176 Forced labels however can be used directly by statements and
177 need to stay in one partition along with their uses. */
178 || (TREE_CODE (t
) == LABEL_DECL
179 && FORCED_LABEL (t
)))
180 return bitmap_bit_p ((bitmap
)data
, DECL_UID (t
));
182 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
183 to pretend that the value pointed to is actual result decl. */
184 if ((TREE_CODE (t
) == MEM_REF
|| INDIRECT_REF_P (t
))
185 && TREE_CODE (TREE_OPERAND (t
, 0)) == SSA_NAME
186 && SSA_NAME_VAR (TREE_OPERAND (t
, 0))
187 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t
, 0))) == RESULT_DECL
188 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
190 bitmap_bit_p ((bitmap
)data
,
191 DECL_UID (DECL_RESULT (current_function_decl
)));
196 /* Dump split point CURRENT. */
199 dump_split_point (FILE * file
, class split_point
*current
)
202 "Split point at BB %i\n"
203 " header time: %f header size: %i\n"
204 " split time: %f split size: %i\n bbs: ",
205 current
->entry_bb
->index
, current
->header_time
.to_double (),
206 current
->header_size
, current
->split_time
.to_double (),
207 current
->split_size
);
208 dump_bitmap (file
, current
->split_bbs
);
209 fprintf (file
, " SSA names to pass: ");
210 dump_bitmap (file
, current
->ssa_names_to_pass
);
213 /* Look for all BBs in header that might lead to the split part and verify
214 that they are not defining any non-SSA var used by the split part.
215 Parameters are the same as for consider_split. */
218 verify_non_ssa_vars (class split_point
*current
, bitmap non_ssa_vars
,
219 basic_block return_bb
)
221 bitmap seen
= BITMAP_ALLOC (NULL
);
222 vec
<basic_block
> worklist
= vNULL
;
228 FOR_EACH_EDGE (e
, ei
, current
->entry_bb
->preds
)
229 if (e
->src
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
)
230 && !bitmap_bit_p (current
->split_bbs
, e
->src
->index
))
232 worklist
.safe_push (e
->src
);
233 bitmap_set_bit (seen
, e
->src
->index
);
236 while (!worklist
.is_empty ())
238 bb
= worklist
.pop ();
239 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
240 if (e
->src
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
)
241 && bitmap_set_bit (seen
, e
->src
->index
))
243 gcc_checking_assert (!bitmap_bit_p (current
->split_bbs
,
245 worklist
.safe_push (e
->src
);
247 for (gimple_stmt_iterator bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
);
250 gimple
*stmt
= gsi_stmt (bsi
);
251 if (is_gimple_debug (stmt
))
253 if (walk_stmt_load_store_addr_ops
254 (stmt
, non_ssa_vars
, test_nonssa_use
, test_nonssa_use
,
260 if (glabel
*label_stmt
= dyn_cast
<glabel
*> (stmt
))
261 if (test_nonssa_use (stmt
, gimple_label_label (label_stmt
),
262 NULL_TREE
, non_ssa_vars
))
268 for (gphi_iterator bsi
= gsi_start_phis (bb
); !gsi_end_p (bsi
);
271 if (walk_stmt_load_store_addr_ops
272 (gsi_stmt (bsi
), non_ssa_vars
, test_nonssa_use
, test_nonssa_use
,
279 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
281 if (e
->dest
!= return_bb
)
283 for (gphi_iterator bsi
= gsi_start_phis (return_bb
);
287 gphi
*stmt
= bsi
.phi ();
288 tree op
= gimple_phi_arg_def (stmt
, e
->dest_idx
);
290 if (virtual_operand_p (gimple_phi_result (stmt
)))
292 if (TREE_CODE (op
) != SSA_NAME
293 && test_nonssa_use (stmt
, op
, op
, non_ssa_vars
))
302 /* Verify that the rest of function does not define any label
303 used by the split part. */
304 FOR_EACH_BB_FN (bb
, cfun
)
305 if (!bitmap_bit_p (current
->split_bbs
, bb
->index
)
306 && !bitmap_bit_p (seen
, bb
->index
))
308 gimple_stmt_iterator bsi
;
309 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
310 if (glabel
*label_stmt
= dyn_cast
<glabel
*> (gsi_stmt (bsi
)))
312 if (test_nonssa_use (label_stmt
,
313 gimple_label_label (label_stmt
),
314 NULL_TREE
, non_ssa_vars
))
330 /* If STMT is a call, check the callee against a list of forbidden
331 predicate functions. If a match is found, look for uses of the
332 call result in condition statements that compare against zero.
333 For each such use, find the block targeted by the condition
334 statement for the nonzero result, and set the bit for this block
335 in the forbidden dominators bitmap. The purpose of this is to avoid
336 selecting a split point where we are likely to lose the chance
337 to optimize away an unused function call. */
340 check_forbidden_calls (gimple
*stmt
)
342 imm_use_iterator use_iter
;
346 /* At the moment, __builtin_constant_p is the only forbidden
347 predicate function call (see PR49642). */
348 if (!gimple_call_builtin_p (stmt
, BUILT_IN_CONSTANT_P
))
351 lhs
= gimple_call_lhs (stmt
);
353 if (!lhs
|| TREE_CODE (lhs
) != SSA_NAME
)
356 FOR_EACH_IMM_USE_FAST (use_p
, use_iter
, lhs
)
359 basic_block use_bb
, forbidden_bb
;
361 edge true_edge
, false_edge
;
364 use_stmt
= dyn_cast
<gcond
*> (USE_STMT (use_p
));
368 /* Assuming canonical form for GIMPLE_COND here, with constant
369 in second position. */
370 op1
= gimple_cond_rhs (use_stmt
);
371 code
= gimple_cond_code (use_stmt
);
372 use_bb
= gimple_bb (use_stmt
);
374 extract_true_false_edges_from_block (use_bb
, &true_edge
, &false_edge
);
376 /* We're only interested in comparisons that distinguish
377 unambiguously from zero. */
378 if (!integer_zerop (op1
) || code
== LE_EXPR
|| code
== GE_EXPR
)
382 forbidden_bb
= false_edge
->dest
;
384 forbidden_bb
= true_edge
->dest
;
386 bitmap_set_bit (forbidden_dominators
, forbidden_bb
->index
);
390 /* If BB is dominated by any block in the forbidden dominators set,
391 return TRUE; else FALSE. */
394 dominated_by_forbidden (basic_block bb
)
399 EXECUTE_IF_SET_IN_BITMAP (forbidden_dominators
, 1, dom_bb
, bi
)
401 if (dominated_by_p (CDI_DOMINATORS
, bb
,
402 BASIC_BLOCK_FOR_FN (cfun
, dom_bb
)))
409 /* For give split point CURRENT and return block RETURN_BB return 1
410 if ssa name VAL is set by split part and 0 otherwise. */
412 split_part_set_ssa_name_p (tree val
, class split_point
*current
,
413 basic_block return_bb
)
415 if (TREE_CODE (val
) != SSA_NAME
)
418 return (!SSA_NAME_IS_DEFAULT_DEF (val
)
419 && (bitmap_bit_p (current
->split_bbs
,
420 gimple_bb (SSA_NAME_DEF_STMT (val
))->index
)
421 || gimple_bb (SSA_NAME_DEF_STMT (val
)) == return_bb
));
424 /* We found an split_point CURRENT. NON_SSA_VARS is bitmap of all non ssa
425 variables used and RETURN_BB is return basic block.
426 See if we can split function here. */
429 consider_split (class split_point
*current
, bitmap non_ssa_vars
,
430 basic_block return_bb
)
433 unsigned int num_args
= 0;
434 unsigned int call_overhead
;
440 bool back_edge
= false;
442 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
443 dump_split_point (dump_file
, current
);
445 current
->count
= profile_count::zero ();
446 FOR_EACH_EDGE (e
, ei
, current
->entry_bb
->preds
)
448 if (e
->flags
& EDGE_DFS_BACK
)
450 if (!bitmap_bit_p (current
->split_bbs
, e
->src
->index
))
451 current
->count
+= e
->count ();
454 /* Do not split when we would end up calling function anyway.
455 Compares are three state, use !(...<...) to also give up when outcome
458 < (ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
.apply_scale
459 (param_partial_inlining_entry_probability
, 100))))
461 /* When profile is guessed, we cannot expect it to give us
462 realistic estimate on likeliness of function taking the
463 complex path. As a special case, when tail of the function is
464 a loop, enable splitting since inlining code skipping the loop
465 is likely noticeable win. */
467 && profile_status_for_fn (cfun
) != PROFILE_READ
469 < ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
)
471 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
474 " Split before loop, accepting despite low counts");
475 current
->count
.dump (dump_file
);
476 fprintf (dump_file
, " ");
477 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
.dump (dump_file
);
482 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
484 " Refused: incoming frequency is too large.\n");
489 if (!current
->header_size
)
491 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
492 fprintf (dump_file
, " Refused: header empty\n");
496 /* Verify that PHI args on entry are either virtual or all their operands
497 incoming from header are the same. */
498 for (bsi
= gsi_start_phis (current
->entry_bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
500 gphi
*stmt
= bsi
.phi ();
503 if (virtual_operand_p (gimple_phi_result (stmt
)))
505 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
507 edge e
= gimple_phi_arg_edge (stmt
, i
);
508 if (!bitmap_bit_p (current
->split_bbs
, e
->src
->index
))
510 tree edge_val
= gimple_phi_arg_def (stmt
, i
);
511 if (val
&& edge_val
!= val
)
513 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
515 " Refused: entry BB has PHI with multiple variants\n");
524 /* See what argument we will pass to the split function and compute
526 call_overhead
= eni_size_weights
.call_cost
;
527 for (parm
= DECL_ARGUMENTS (current_function_decl
); parm
;
528 parm
= DECL_CHAIN (parm
))
530 if (!is_gimple_reg (parm
))
532 if (bitmap_bit_p (non_ssa_vars
, DECL_UID (parm
)))
534 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
536 " Refused: need to pass non-ssa param values\n");
542 tree ddef
= ssa_default_def (cfun
, parm
);
544 && bitmap_bit_p (current
->ssa_names_to_pass
,
545 SSA_NAME_VERSION (ddef
)))
547 if (!VOID_TYPE_P (TREE_TYPE (parm
)))
548 call_overhead
+= estimate_move_cost (TREE_TYPE (parm
), false);
553 if (!VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl
))))
554 call_overhead
+= estimate_move_cost (TREE_TYPE (TREE_TYPE
555 (current_function_decl
)),
558 if (current
->split_size
<= call_overhead
)
560 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
562 " Refused: split size is smaller than call overhead\n");
565 /* FIXME: The logic here is not very precise, because inliner does use
566 inline predicates to reduce function body size. We add 10 to anticipate
567 that. Next stage1 we should try to be more meaningful here. */
568 if (current
->header_size
+ call_overhead
569 >= (unsigned int)(DECL_DECLARED_INLINE_P (current_function_decl
)
570 ? param_max_inline_insns_single
571 : param_max_inline_insns_auto
) + 10)
573 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
575 " Refused: header size is too large for inline candidate\n");
579 /* Splitting functions brings the target out of comdat group; this will
580 lead to code duplication if the function is reused by other unit.
581 Limit this duplication. This is consistent with limit in tree-sra.cc
582 FIXME: with LTO we ought to be able to do better! */
583 if (DECL_ONE_ONLY (current_function_decl
)
584 && current
->split_size
>= (unsigned int) param_max_inline_insns_auto
+ 10)
586 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
588 " Refused: function is COMDAT and tail is too large\n");
591 /* For comdat functions also reject very small tails; those will likely get
592 inlined back and we do not want to risk the duplication overhead.
593 FIXME: with LTO we ought to be able to do better! */
594 if (DECL_ONE_ONLY (current_function_decl
)
595 && current
->split_size
596 <= (unsigned int) param_early_inlining_insns
/ 2)
598 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
600 " Refused: function is COMDAT and tail is too small\n");
604 /* FIXME: we currently can pass only SSA function parameters to the split
605 arguments. Once parm_adjustment infrastructure is supported by cloning,
606 we can pass more than that. */
607 if (num_args
!= bitmap_count_bits (current
->ssa_names_to_pass
))
610 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
612 " Refused: need to pass non-param values\n");
616 /* When there are non-ssa vars used in the split region, see if they
617 are used in the header region. If so, reject the split.
618 FIXME: we can use nested function support to access both. */
619 if (!bitmap_empty_p (non_ssa_vars
)
620 && !verify_non_ssa_vars (current
, non_ssa_vars
, return_bb
))
622 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
624 " Refused: split part has non-ssa uses\n");
628 /* If the split point is dominated by a forbidden block, reject
630 if (!bitmap_empty_p (forbidden_dominators
)
631 && dominated_by_forbidden (current
->entry_bb
))
633 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
635 " Refused: split point dominated by forbidden block\n");
639 /* See if retval used by return bb is computed by header or split part.
640 When it is computed by split part, we need to produce return statement
641 in the split part and add code to header to pass it around.
643 This is bit tricky to test:
644 1) When there is no return_bb or no return value, we always pass
646 2) Invariants are always computed by caller.
647 3) For SSA we need to look if defining statement is in header or split part
648 4) For non-SSA we need to look where the var is computed. */
649 retval
= find_retval (return_bb
);
652 /* If there is a return_bb with no return value in function returning
653 value by reference, also make the split part return void, otherwise
654 we expansion would try to create a non-POD temporary, which is
656 if (return_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
)
657 && DECL_RESULT (current_function_decl
)
658 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
659 current
->split_part_set_retval
= false;
661 current
->split_part_set_retval
= true;
663 else if (is_gimple_min_invariant (retval
))
664 current
->split_part_set_retval
= false;
665 /* Special case is value returned by reference we record as if it was non-ssa
666 set to result_decl. */
667 else if (TREE_CODE (retval
) == SSA_NAME
668 && SSA_NAME_VAR (retval
)
669 && TREE_CODE (SSA_NAME_VAR (retval
)) == RESULT_DECL
670 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
671 current
->split_part_set_retval
672 = bitmap_bit_p (non_ssa_vars
, DECL_UID (SSA_NAME_VAR (retval
)));
673 else if (TREE_CODE (retval
) == SSA_NAME
)
674 current
->split_part_set_retval
675 = split_part_set_ssa_name_p (retval
, current
, return_bb
);
676 else if (TREE_CODE (retval
) == PARM_DECL
)
677 current
->split_part_set_retval
= false;
678 else if (VAR_P (retval
)
679 || TREE_CODE (retval
) == RESULT_DECL
)
680 current
->split_part_set_retval
681 = bitmap_bit_p (non_ssa_vars
, DECL_UID (retval
));
683 current
->split_part_set_retval
= true;
685 /* split_function fixes up at most one PHI non-virtual PHI node in return_bb,
686 for the return value. If there are other PHIs, give up. */
687 if (return_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
691 for (psi
= gsi_start_phis (return_bb
); !gsi_end_p (psi
); gsi_next (&psi
))
692 if (!virtual_operand_p (gimple_phi_result (psi
.phi ()))
694 && current
->split_part_set_retval
695 && TREE_CODE (retval
) == SSA_NAME
696 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
))
697 && SSA_NAME_DEF_STMT (retval
) == psi
.phi ()))
699 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
701 " Refused: return bb has extra PHIs\n");
706 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
707 fprintf (dump_file
, " Accepted!\n");
709 /* At the moment chose split point with lowest count and that leaves
710 out smallest size of header.
711 In future we might re-consider this heuristics. */
712 if (!best_split_point
.split_bbs
713 || best_split_point
.count
715 || (best_split_point
.count
== current
->count
716 && best_split_point
.split_size
< current
->split_size
))
719 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
720 fprintf (dump_file
, " New best split point!\n");
721 if (best_split_point
.ssa_names_to_pass
)
723 BITMAP_FREE (best_split_point
.ssa_names_to_pass
);
724 BITMAP_FREE (best_split_point
.split_bbs
);
726 best_split_point
= *current
;
727 best_split_point
.ssa_names_to_pass
= BITMAP_ALLOC (NULL
);
728 bitmap_copy (best_split_point
.ssa_names_to_pass
,
729 current
->ssa_names_to_pass
);
730 best_split_point
.split_bbs
= BITMAP_ALLOC (NULL
);
731 bitmap_copy (best_split_point
.split_bbs
, current
->split_bbs
);
735 /* Return basic block containing RETURN statement. We allow basic blocks
739 but return_bb cannot be more complex than this (except for
740 -fsanitize=thread we allow TSAN_FUNC_EXIT () internal call in there).
741 If nothing is found, return the exit block.
743 When there are multiple RETURN statement, chose one with return value,
744 since that one is more likely shared by multiple code paths.
746 Return BB is special, because for function splitting it is the only
747 basic block that is duplicated in between header and split part of the
750 TODO: We might support multiple return blocks. */
753 find_return_bb (void)
756 basic_block return_bb
= EXIT_BLOCK_PTR_FOR_FN (cfun
);
757 gimple_stmt_iterator bsi
;
758 bool found_return
= false;
759 tree retval
= NULL_TREE
;
761 if (!single_pred_p (EXIT_BLOCK_PTR_FOR_FN (cfun
)))
764 e
= single_pred_edge (EXIT_BLOCK_PTR_FOR_FN (cfun
));
765 for (bsi
= gsi_last_bb (e
->src
); !gsi_end_p (bsi
); gsi_prev (&bsi
))
767 gimple
*stmt
= gsi_stmt (bsi
);
768 if (gimple_code (stmt
) == GIMPLE_LABEL
769 || is_gimple_debug (stmt
)
770 || gimple_clobber_p (stmt
))
772 else if (gimple_code (stmt
) == GIMPLE_ASSIGN
774 && gimple_assign_single_p (stmt
)
775 && (auto_var_in_fn_p (gimple_assign_rhs1 (stmt
),
776 current_function_decl
)
777 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
778 && retval
== gimple_assign_lhs (stmt
))
780 else if (greturn
*return_stmt
= dyn_cast
<greturn
*> (stmt
))
783 retval
= gimple_return_retval (return_stmt
);
785 /* For -fsanitize=thread, allow also TSAN_FUNC_EXIT () in the return
787 else if ((flag_sanitize
& SANITIZE_THREAD
)
788 && gimple_call_internal_p (stmt
, IFN_TSAN_FUNC_EXIT
))
793 if (gsi_end_p (bsi
) && found_return
)
799 /* Given return basic block RETURN_BB, see where return value is really
802 find_retval (basic_block return_bb
)
804 gimple_stmt_iterator bsi
;
805 for (bsi
= gsi_start_bb (return_bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
806 if (greturn
*return_stmt
= dyn_cast
<greturn
*> (gsi_stmt (bsi
)))
807 return gimple_return_retval (return_stmt
);
808 else if (gimple_code (gsi_stmt (bsi
)) == GIMPLE_ASSIGN
809 && !gimple_clobber_p (gsi_stmt (bsi
)))
810 return gimple_assign_rhs1 (gsi_stmt (bsi
));
814 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
815 variable, mark it as used in bitmap passed via DATA.
816 Return true when access to T prevents splitting the function. */
819 mark_nonssa_use (gimple
*, tree t
, tree
, void *data
)
821 t
= get_base_address (t
);
823 if (!t
|| is_gimple_reg (t
))
826 /* At present we can't pass non-SSA arguments to split function.
827 FIXME: this can be relaxed by passing references to arguments. */
828 if (TREE_CODE (t
) == PARM_DECL
)
830 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
832 "Cannot split: use of non-ssa function parameter.\n");
836 if ((VAR_P (t
) && auto_var_in_fn_p (t
, current_function_decl
))
837 || TREE_CODE (t
) == RESULT_DECL
838 || (TREE_CODE (t
) == LABEL_DECL
&& FORCED_LABEL (t
)))
839 bitmap_set_bit ((bitmap
)data
, DECL_UID (t
));
841 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
842 to pretend that the value pointed to is actual result decl. */
843 if ((TREE_CODE (t
) == MEM_REF
|| INDIRECT_REF_P (t
))
844 && TREE_CODE (TREE_OPERAND (t
, 0)) == SSA_NAME
845 && SSA_NAME_VAR (TREE_OPERAND (t
, 0))
846 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t
, 0))) == RESULT_DECL
847 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
849 bitmap_bit_p ((bitmap
)data
,
850 DECL_UID (DECL_RESULT (current_function_decl
)));
855 /* Compute local properties of basic block BB we collect when looking for
856 split points. We look for ssa defs and store them in SET_SSA_NAMES,
857 for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic
858 vars stored in NON_SSA_VARS.
860 When BB has edge to RETURN_BB, collect uses in RETURN_BB too.
862 Return false when BB contains something that prevents it from being put into
866 visit_bb (basic_block bb
, basic_block return_bb
,
867 bitmap set_ssa_names
, bitmap used_ssa_names
,
872 bool can_split
= true;
874 for (gimple_stmt_iterator bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
);
877 gimple
*stmt
= gsi_stmt (bsi
);
881 if (is_gimple_debug (stmt
))
884 if (gimple_clobber_p (stmt
))
887 /* FIXME: We can split regions containing EH. We cannot however
888 split RESX, EH_DISPATCH and EH_POINTER referring to same region
889 into different partitions. This would require tracking of
890 EH regions and checking in consider_split_point if they
891 are not used elsewhere. */
892 if (gimple_code (stmt
) == GIMPLE_RESX
)
894 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
895 fprintf (dump_file
, "Cannot split: resx.\n");
898 if (gimple_code (stmt
) == GIMPLE_EH_DISPATCH
)
900 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
901 fprintf (dump_file
, "Cannot split: eh dispatch.\n");
905 /* Check calls that would prevent splitting. */
906 if (gimple_code (stmt
) == GIMPLE_CALL
)
908 if (tree decl
= gimple_call_fndecl (stmt
))
910 /* Check builtins that would prevent splitting. */
911 if (fndecl_built_in_p (decl
, BUILT_IN_NORMAL
))
912 switch (DECL_FUNCTION_CODE (decl
))
914 /* FIXME: once we will allow passing non-parm values to
915 split part, we need to be sure to handle correct
916 builtin_stack_save and builtin_stack_restore. At the
917 moment we are safe; there is no way to store
918 builtin_stack_save result in non-SSA variable since all
919 calls to those are compiler generated. */
921 case BUILT_IN_APPLY_ARGS
:
922 case BUILT_IN_VA_START
:
923 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
925 "Cannot split: builtin_apply and va_start.\n");
928 case BUILT_IN_EH_POINTER
:
929 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
931 "Cannot split: builtin_eh_pointer.\n");
938 /* Calls to functions (which have the warning or error
939 attribute on them) should not be split off into another
941 if (lookup_attribute ("warning", DECL_ATTRIBUTES (decl
))
942 || lookup_attribute ("error", DECL_ATTRIBUTES (decl
)))
944 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
946 "Cannot split: warning or error attribute.\n");
952 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_DEF
)
953 bitmap_set_bit (set_ssa_names
, SSA_NAME_VERSION (op
));
954 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
955 bitmap_set_bit (used_ssa_names
, SSA_NAME_VERSION (op
));
956 can_split
&= !walk_stmt_load_store_addr_ops (stmt
, non_ssa_vars
,
961 for (gphi_iterator bsi
= gsi_start_phis (bb
); !gsi_end_p (bsi
);
964 gphi
*stmt
= bsi
.phi ();
967 if (virtual_operand_p (gimple_phi_result (stmt
)))
969 bitmap_set_bit (set_ssa_names
,
970 SSA_NAME_VERSION (gimple_phi_result (stmt
)));
971 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
973 tree op
= gimple_phi_arg_def (stmt
, i
);
974 if (TREE_CODE (op
) == SSA_NAME
)
975 bitmap_set_bit (used_ssa_names
, SSA_NAME_VERSION (op
));
977 can_split
&= !walk_stmt_load_store_addr_ops (stmt
, non_ssa_vars
,
982 /* Record also uses coming from PHI operand in return BB. */
983 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
984 if (e
->dest
== return_bb
)
986 for (gphi_iterator bsi
= gsi_start_phis (return_bb
);
990 gphi
*stmt
= bsi
.phi ();
991 tree op
= gimple_phi_arg_def (stmt
, e
->dest_idx
);
993 if (virtual_operand_p (gimple_phi_result (stmt
)))
995 if (TREE_CODE (op
) == SSA_NAME
)
996 bitmap_set_bit (used_ssa_names
, SSA_NAME_VERSION (op
));
998 can_split
&= !mark_nonssa_use (stmt
, op
, op
, non_ssa_vars
);
1004 /* Stack entry for recursive DFS walk in find_split_point. */
1009 /* Basic block we are examining. */
1012 /* SSA names set and used by the BB and all BBs reachable
1013 from it via DFS walk. */
1014 bitmap set_ssa_names
, used_ssa_names
;
1015 bitmap non_ssa_vars
;
1017 /* All BBS visited from this BB via DFS walk. */
1020 /* Last examined edge in DFS walk. Since we walk unoriented graph,
1021 the value is up to sum of incoming and outgoing edges of BB. */
1022 unsigned int edge_num
;
1024 /* Stack entry index of earliest BB reachable from current BB
1025 or any BB visited later in DFS walk. */
1028 /* Overall time and size of all BBs reached from this BB in DFS walk. */
1032 /* When false we cannot split on this BB. */
1037 /* Find all articulations and call consider_split on them.
1038 OVERALL_TIME and OVERALL_SIZE is time and size of the function.
1040 We perform basic algorithm for finding an articulation in a graph
1041 created from CFG by considering it to be an unoriented graph.
1043 The articulation is discovered via DFS walk. We collect earliest
1044 basic block on stack that is reachable via backward edge. Articulation
1045 is any basic block such that there is no backward edge bypassing it.
1046 To reduce stack usage we maintain heap allocated stack in STACK vector.
1047 AUX pointer of BB is set to index it appears in the stack or -1 once
1048 it is visited and popped off the stack.
1050 The algorithm finds articulation after visiting the whole component
1051 reachable by it. This makes it convenient to collect information about
1052 the component used by consider_split. */
1055 find_split_points (basic_block return_bb
, sreal overall_time
, int overall_size
)
1058 vec
<stack_entry
> stack
= vNULL
;
1060 class split_point current
;
1062 current
.header_time
= overall_time
;
1063 current
.header_size
= overall_size
;
1064 current
.split_time
= 0;
1065 current
.split_size
= 0;
1066 current
.ssa_names_to_pass
= BITMAP_ALLOC (NULL
);
1068 first
.bb
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
1070 first
.overall_time
= 0;
1071 first
.overall_size
= 0;
1072 first
.earliest
= INT_MAX
;
1073 first
.set_ssa_names
= 0;
1074 first
.used_ssa_names
= 0;
1075 first
.non_ssa_vars
= 0;
1076 first
.bbs_visited
= 0;
1077 first
.can_split
= false;
1078 stack
.safe_push (first
);
1079 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->aux
= (void *)(intptr_t)-1;
1081 while (!stack
.is_empty ())
1083 stack_entry
*entry
= &stack
.last ();
1085 /* We are walking an acyclic graph, so edge_num counts
1086 succ and pred edges together. However when considering
1087 articulation, we want to have processed everything reachable
1088 from articulation but nothing that reaches into it. */
1089 if (entry
->edge_num
== EDGE_COUNT (entry
->bb
->succs
)
1090 && entry
->bb
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
))
1092 int pos
= stack
.length ();
1093 entry
->can_split
&= visit_bb (entry
->bb
, return_bb
,
1094 entry
->set_ssa_names
,
1095 entry
->used_ssa_names
,
1096 entry
->non_ssa_vars
);
1097 if (pos
<= entry
->earliest
&& !entry
->can_split
1098 && dump_file
&& (dump_flags
& TDF_DETAILS
))
1100 "found articulation at bb %i but cannot split\n",
1102 if (pos
<= entry
->earliest
&& entry
->can_split
)
1104 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1105 fprintf (dump_file
, "found articulation at bb %i\n",
1107 current
.entry_bb
= entry
->bb
;
1108 current
.ssa_names_to_pass
= BITMAP_ALLOC (NULL
);
1109 bitmap_and_compl (current
.ssa_names_to_pass
,
1110 entry
->used_ssa_names
, entry
->set_ssa_names
);
1111 current
.header_time
= overall_time
- entry
->overall_time
;
1112 current
.header_size
= overall_size
- entry
->overall_size
;
1113 current
.split_time
= entry
->overall_time
;
1114 current
.split_size
= entry
->overall_size
;
1115 current
.split_bbs
= entry
->bbs_visited
;
1116 consider_split (¤t
, entry
->non_ssa_vars
, return_bb
);
1117 BITMAP_FREE (current
.ssa_names_to_pass
);
1120 /* Do actual DFS walk. */
1122 < (EDGE_COUNT (entry
->bb
->succs
)
1123 + EDGE_COUNT (entry
->bb
->preds
)))
1127 if (entry
->edge_num
< EDGE_COUNT (entry
->bb
->succs
))
1129 e
= EDGE_SUCC (entry
->bb
, entry
->edge_num
);
1134 e
= EDGE_PRED (entry
->bb
, entry
->edge_num
1135 - EDGE_COUNT (entry
->bb
->succs
));
1141 /* New BB to visit, push it to the stack. */
1142 if (dest
!= return_bb
&& dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
)
1145 stack_entry new_entry
;
1147 new_entry
.bb
= dest
;
1148 new_entry
.edge_num
= 0;
1149 new_entry
.overall_time
1150 = bb_info_vec
[dest
->index
].time
;
1151 new_entry
.overall_size
1152 = bb_info_vec
[dest
->index
].size
;
1153 new_entry
.earliest
= INT_MAX
;
1154 new_entry
.set_ssa_names
= BITMAP_ALLOC (NULL
);
1155 new_entry
.used_ssa_names
= BITMAP_ALLOC (NULL
);
1156 new_entry
.bbs_visited
= BITMAP_ALLOC (NULL
);
1157 new_entry
.non_ssa_vars
= BITMAP_ALLOC (NULL
);
1158 new_entry
.can_split
= true;
1159 bitmap_set_bit (new_entry
.bbs_visited
, dest
->index
);
1160 stack
.safe_push (new_entry
);
1161 dest
->aux
= (void *)(intptr_t)stack
.length ();
1163 /* Back edge found, record the earliest point. */
1164 else if ((intptr_t)dest
->aux
> 0
1165 && (intptr_t)dest
->aux
< entry
->earliest
)
1166 entry
->earliest
= (intptr_t)dest
->aux
;
1168 /* We are done with examining the edges. Pop off the value from stack
1169 and merge stuff we accumulate during the walk. */
1170 else if (entry
->bb
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
))
1172 stack_entry
*prev
= &stack
[stack
.length () - 2];
1174 entry
->bb
->aux
= (void *)(intptr_t)-1;
1175 prev
->can_split
&= entry
->can_split
;
1176 if (prev
->set_ssa_names
)
1178 bitmap_ior_into (prev
->set_ssa_names
, entry
->set_ssa_names
);
1179 bitmap_ior_into (prev
->used_ssa_names
, entry
->used_ssa_names
);
1180 bitmap_ior_into (prev
->bbs_visited
, entry
->bbs_visited
);
1181 bitmap_ior_into (prev
->non_ssa_vars
, entry
->non_ssa_vars
);
1183 if (prev
->earliest
> entry
->earliest
)
1184 prev
->earliest
= entry
->earliest
;
1185 prev
->overall_time
+= entry
->overall_time
;
1186 prev
->overall_size
+= entry
->overall_size
;
1187 BITMAP_FREE (entry
->set_ssa_names
);
1188 BITMAP_FREE (entry
->used_ssa_names
);
1189 BITMAP_FREE (entry
->bbs_visited
);
1190 BITMAP_FREE (entry
->non_ssa_vars
);
1196 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->aux
= NULL
;
1197 FOR_EACH_BB_FN (bb
, cfun
)
1200 BITMAP_FREE (current
.ssa_names_to_pass
);
1203 /* Split function at SPLIT_POINT. */
1206 split_function (basic_block return_bb
, class split_point
*split_point
,
1207 bool add_tsan_func_exit
)
1209 vec
<tree
> args_to_pass
= vNULL
;
1210 bitmap args_to_skip
;
1213 cgraph_node
*node
, *cur_node
= cgraph_node::get (current_function_decl
);
1214 basic_block call_bb
;
1215 gcall
*call
, *tsan_func_exit_call
= NULL
;
1218 tree retval
= NULL
, real_retval
= NULL
;
1219 gimple
*last_stmt
= NULL
;
1225 fprintf (dump_file
, "\n\nSplitting function at:\n");
1226 dump_split_point (dump_file
, split_point
);
1229 if (cur_node
->can_change_signature
)
1230 args_to_skip
= BITMAP_ALLOC (NULL
);
1232 args_to_skip
= NULL
;
1234 /* Collect the parameters of new function and args_to_skip bitmap. */
1235 for (parm
= DECL_ARGUMENTS (current_function_decl
);
1236 parm
; parm
= DECL_CHAIN (parm
), num
++)
1238 && (!is_gimple_reg (parm
)
1239 || (ddef
= ssa_default_def (cfun
, parm
)) == NULL_TREE
1240 || !bitmap_bit_p (split_point
->ssa_names_to_pass
,
1241 SSA_NAME_VERSION (ddef
))))
1242 bitmap_set_bit (args_to_skip
, num
);
1245 /* This parm might not have been used up to now, but is going to be
1246 used, hence register it. */
1247 if (is_gimple_reg (parm
))
1248 arg
= get_or_create_ssa_default_def (cfun
, parm
);
1252 if (!useless_type_conversion_p (DECL_ARG_TYPE (parm
), TREE_TYPE (arg
)))
1253 arg
= fold_convert (DECL_ARG_TYPE (parm
), arg
);
1254 args_to_pass
.safe_push (arg
);
1257 /* See if the split function will return. */
1258 bool split_part_return_p
= false;
1259 FOR_EACH_EDGE (e
, ei
, return_bb
->preds
)
1261 if (bitmap_bit_p (split_point
->split_bbs
, e
->src
->index
))
1262 split_part_return_p
= true;
1265 /* Add return block to what will become the split function.
1266 We do not return; no return block is needed. */
1267 if (!split_part_return_p
)
1269 /* We have no return block, so nothing is needed. */
1270 else if (return_bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
1272 /* When we do not want to return value, we need to construct
1273 new return block with empty return statement.
1274 FIXME: Once we are able to change return type, we should change function
1275 to return void instead of just outputting function with undefined return
1276 value. For structures this affects quality of codegen. */
1277 else if ((retval
= find_retval (return_bb
))
1278 && !split_point
->split_part_set_retval
)
1280 bool redirected
= true;
1281 basic_block new_return_bb
= create_basic_block (NULL
, 0, return_bb
);
1282 gimple_stmt_iterator gsi
= gsi_start_bb (new_return_bb
);
1283 gsi_insert_after (&gsi
, gimple_build_return (NULL
), GSI_NEW_STMT
);
1284 new_return_bb
->count
= profile_count::zero ();
1288 FOR_EACH_EDGE (e
, ei
, return_bb
->preds
)
1289 if (bitmap_bit_p (split_point
->split_bbs
, e
->src
->index
))
1291 new_return_bb
->count
+= e
->count ();
1292 redirect_edge_and_branch (e
, new_return_bb
);
1297 e
= make_single_succ_edge (new_return_bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
), 0);
1298 add_bb_to_loop (new_return_bb
, current_loops
->tree_root
);
1299 bitmap_set_bit (split_point
->split_bbs
, new_return_bb
->index
);
1301 /* When we pass around the value, use existing return block. */
1303 bitmap_set_bit (split_point
->split_bbs
, return_bb
->index
);
1305 /* If RETURN_BB has virtual operand PHIs, they must be removed and the
1306 virtual operand marked for renaming as we change the CFG in a way that
1307 tree-inline is not able to compensate for.
1309 Note this can happen whether or not we have a return value. If we have
1310 a return value, then RETURN_BB may have PHIs for real operands too. */
1311 if (return_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
1314 for (gphi_iterator gsi
= gsi_start_phis (return_bb
);
1317 gphi
*stmt
= gsi
.phi ();
1318 if (!virtual_operand_p (gimple_phi_result (stmt
)))
1323 mark_virtual_phi_result_for_renaming (stmt
);
1324 remove_phi_node (&gsi
, true);
1327 /* In reality we have to rename the reaching definition of the
1328 virtual operand at return_bb as we will eventually release it
1329 when we remove the code region we outlined.
1330 So we have to rename all immediate virtual uses of that region
1331 if we didn't see a PHI definition yet. */
1332 /* ??? In real reality we want to set the reaching vdef of the
1333 entry of the SESE region as the vuse of the call and the reaching
1334 vdef of the exit of the SESE region as the vdef of the call. */
1336 for (gimple_stmt_iterator gsi
= gsi_start_bb (return_bb
);
1340 gimple
*stmt
= gsi_stmt (gsi
);
1341 if (gimple_vuse (stmt
))
1343 gimple_set_vuse (stmt
, NULL_TREE
);
1346 if (gimple_vdef (stmt
))
1351 ipa_param_adjustments
*adjustments
;
1352 bool skip_return
= (!split_part_return_p
1353 || !split_point
->split_part_set_retval
);
1354 /* TODO: Perhaps get rid of args_to_skip entirely, after we make sure the
1355 debug info generation and discrepancy avoiding works well too. */
1356 if ((args_to_skip
&& !bitmap_empty_p (args_to_skip
))
1359 vec
<ipa_adjusted_param
, va_gc
> *new_params
= NULL
;
1361 for (parm
= DECL_ARGUMENTS (current_function_decl
), j
= 0;
1362 parm
; parm
= DECL_CHAIN (parm
), j
++)
1363 if (!args_to_skip
|| !bitmap_bit_p (args_to_skip
, j
))
1365 ipa_adjusted_param adj
;
1366 memset (&adj
, 0, sizeof (adj
));
1367 adj
.op
= IPA_PARAM_OP_COPY
;
1369 adj
.prev_clone_index
= j
;
1370 vec_safe_push (new_params
, adj
);
1372 adjustments
= new ipa_param_adjustments (new_params
, j
, skip_return
);
1377 /* Now create the actual clone. */
1378 cgraph_edge::rebuild_edges ();
1379 node
= cur_node
->create_version_clone_with_body
1380 (vNULL
, NULL
, adjustments
,
1381 split_point
->split_bbs
, split_point
->entry_bb
, "part");
1383 node
->split_part
= true;
1385 if (cur_node
->same_comdat_group
)
1387 /* TODO: call is versionable if we make sure that all
1388 callers are inside of a comdat group. */
1389 cur_node
->calls_comdat_local
= true;
1390 node
->add_to_same_comdat_group (cur_node
);
1394 /* Let's take a time profile for splitted function. */
1395 if (cur_node
->tp_first_run
)
1396 node
->tp_first_run
= cur_node
->tp_first_run
+ 1;
1398 /* For usual cloning it is enough to clear builtin only when signature
1399 changes. For partial inlining we however cannot expect the part
1400 of builtin implementation to have same semantic as the whole. */
1401 if (fndecl_built_in_p (node
->decl
))
1402 set_decl_built_in_function (node
->decl
, NOT_BUILT_IN
, 0);
1404 /* If return_bb contains any clobbers that refer to SSA_NAMEs
1405 set in the split part, remove them. Also reset debug stmts that
1406 refer to SSA_NAMEs set in the split part. */
1407 if (return_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
1409 gimple_stmt_iterator gsi
= gsi_start_bb (return_bb
);
1410 while (!gsi_end_p (gsi
))
1414 gimple
*stmt
= gsi_stmt (gsi
);
1415 bool remove
= false;
1416 if (gimple_clobber_p (stmt
) || is_gimple_debug (stmt
))
1417 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
1419 basic_block bb
= gimple_bb (SSA_NAME_DEF_STMT (op
));
1423 && bitmap_bit_p (split_point
->split_bbs
, bb
->index
))
1425 if (is_gimple_debug (stmt
))
1427 gimple_debug_bind_reset_value (stmt
);
1436 gsi_remove (&gsi
, true);
1442 /* If the original function is declared inline, there is no point in issuing
1443 a warning for the non-inlinable part. */
1444 DECL_NO_INLINE_WARNING_P (node
->decl
) = 1;
1445 cur_node
->remove_callees ();
1446 cur_node
->remove_all_references ();
1447 if (!split_part_return_p
)
1448 TREE_THIS_VOLATILE (node
->decl
) = 1;
1450 dump_function_to_file (node
->decl
, dump_file
, dump_flags
);
1452 /* Create the basic block we place call into. It is the entry basic block
1453 split after last label. */
1454 call_bb
= split_point
->entry_bb
;
1455 for (gimple_stmt_iterator gsi
= gsi_start_bb (call_bb
); !gsi_end_p (gsi
);)
1456 if (gimple_code (gsi_stmt (gsi
)) == GIMPLE_LABEL
)
1458 last_stmt
= gsi_stmt (gsi
);
1463 call_bb
->count
= split_point
->count
;
1464 e
= split_block (split_point
->entry_bb
, last_stmt
);
1467 /* Produce the call statement. */
1468 gimple_stmt_iterator gsi
= gsi_last_bb (call_bb
);
1469 FOR_EACH_VEC_ELT (args_to_pass
, i
, arg
)
1470 if (!is_gimple_val (arg
))
1472 arg
= force_gimple_operand_gsi (&gsi
, arg
, true, NULL_TREE
,
1473 false, GSI_CONTINUE_LINKING
);
1474 args_to_pass
[i
] = arg
;
1476 call
= gimple_build_call_vec (node
->decl
, args_to_pass
);
1477 gimple_set_block (call
, DECL_INITIAL (current_function_decl
));
1478 args_to_pass
.release ();
1480 /* For optimized away parameters, add on the caller side
1482 DEBUG D#X => parm_Y(D)
1483 stmts and associate D#X with parm in decl_debug_args_lookup
1484 vector to say for debug info that if parameter parm had been passed,
1485 it would have value parm_Y(D). */
1488 vec
<tree
, va_gc
> **debug_args
= NULL
;
1489 unsigned i
= 0, len
= 0;
1490 if (MAY_HAVE_DEBUG_BIND_STMTS
)
1492 debug_args
= decl_debug_args_lookup (node
->decl
);
1494 len
= vec_safe_length (*debug_args
);
1496 for (parm
= DECL_ARGUMENTS (current_function_decl
), num
= 0;
1497 parm
; parm
= DECL_CHAIN (parm
), num
++)
1498 if (bitmap_bit_p (args_to_skip
, num
) && is_gimple_reg (parm
))
1503 /* This needs to be done even without
1504 MAY_HAVE_DEBUG_BIND_STMTS, otherwise if it didn't exist
1505 before, we'd end up with different SSA_NAME_VERSIONs
1506 between -g and -g0. */
1507 arg
= get_or_create_ssa_default_def (cfun
, parm
);
1508 if (!MAY_HAVE_DEBUG_BIND_STMTS
|| debug_args
== NULL
)
1511 while (i
< len
&& (**debug_args
)[i
] != DECL_ORIGIN (parm
))
1515 ddecl
= (**debug_args
)[i
+ 1];
1517 = gimple_build_debug_bind (ddecl
, unshare_expr (arg
), call
);
1518 gsi_insert_after (&gsi
, def_temp
, GSI_NEW_STMT
);
1520 BITMAP_FREE (args_to_skip
);
1523 /* We avoid address being taken on any variable used by split part,
1524 so return slot optimization is always possible. Moreover this is
1525 required to make DECL_BY_REFERENCE work. */
1526 if (aggregate_value_p (DECL_RESULT (current_function_decl
),
1527 TREE_TYPE (current_function_decl
))
1528 && (!is_gimple_reg_type (TREE_TYPE (DECL_RESULT (current_function_decl
)))
1529 || DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
))))
1530 gimple_call_set_return_slot_opt (call
, true);
1532 if (add_tsan_func_exit
)
1533 tsan_func_exit_call
= gimple_build_call_internal (IFN_TSAN_FUNC_EXIT
, 0);
1535 /* Update return value. This is bit tricky. When we do not return,
1536 do nothing. When we return we might need to update return_bb
1537 or produce a new return statement. */
1538 if (!split_part_return_p
)
1540 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1541 if (tsan_func_exit_call
)
1542 gsi_insert_after (&gsi
, tsan_func_exit_call
, GSI_NEW_STMT
);
1546 e
= make_single_succ_edge (call_bb
, return_bb
,
1547 return_bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
)
1548 ? 0 : EDGE_FALLTHRU
);
1550 /* If there is return basic block, see what value we need to store
1551 return value into and put call just before it. */
1552 if (return_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
1554 real_retval
= retval
;
1555 if (real_retval
&& split_point
->split_part_set_retval
)
1559 /* See if we need new SSA_NAME for the result.
1560 When DECL_BY_REFERENCE is true, retval is actually pointer to
1561 return value and it is constant in whole function. */
1562 if (TREE_CODE (retval
) == SSA_NAME
1563 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
1565 retval
= copy_ssa_name (retval
, call
);
1567 /* See if there is PHI defining return value. */
1568 for (psi
= gsi_start_phis (return_bb
);
1569 !gsi_end_p (psi
); gsi_next (&psi
))
1570 if (!virtual_operand_p (gimple_phi_result (psi
.phi ())))
1573 /* When there is PHI, just update its value. */
1574 if (TREE_CODE (retval
) == SSA_NAME
1575 && !gsi_end_p (psi
))
1576 add_phi_arg (psi
.phi (), retval
, e
, UNKNOWN_LOCATION
);
1577 /* Otherwise update the return BB itself.
1578 find_return_bb allows at most one assignment to return value,
1579 so update first statement. */
1582 gimple_stmt_iterator bsi
;
1583 for (bsi
= gsi_start_bb (return_bb
); !gsi_end_p (bsi
);
1585 if (greturn
*return_stmt
1586 = dyn_cast
<greturn
*> (gsi_stmt (bsi
)))
1588 gimple_return_set_retval (return_stmt
, retval
);
1591 else if (gimple_code (gsi_stmt (bsi
)) == GIMPLE_ASSIGN
1592 && !gimple_clobber_p (gsi_stmt (bsi
)))
1594 gimple_assign_set_rhs1 (gsi_stmt (bsi
), retval
);
1597 update_stmt (gsi_stmt (bsi
));
1598 /* Also adjust clobbers and debug stmts in return_bb. */
1599 for (bsi
= gsi_start_bb (return_bb
); !gsi_end_p (bsi
);
1602 gimple
*stmt
= gsi_stmt (bsi
);
1603 if (gimple_clobber_p (stmt
)
1604 || is_gimple_debug (stmt
))
1607 use_operand_p use_p
;
1608 bool update
= false;
1609 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
,
1611 if (USE_FROM_PTR (use_p
) == real_retval
)
1613 SET_USE (use_p
, retval
);
1622 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
1624 gimple_call_set_lhs (call
, build_simple_mem_ref (retval
));
1625 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1630 restype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
1631 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1632 if (!useless_type_conversion_p (TREE_TYPE (retval
), restype
))
1635 tree tem
= create_tmp_reg (restype
);
1636 tem
= make_ssa_name (tem
, call
);
1637 cpy
= gimple_build_assign (retval
, NOP_EXPR
, tem
);
1638 gsi_insert_after (&gsi
, cpy
, GSI_NEW_STMT
);
1641 gimple_call_set_lhs (call
, retval
);
1646 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1647 if (tsan_func_exit_call
)
1648 gsi_insert_after (&gsi
, tsan_func_exit_call
, GSI_NEW_STMT
);
1650 /* We don't use return block (there is either no return in function or
1651 multiple of them). So create new basic block with return statement.
1656 if (split_point
->split_part_set_retval
1657 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl
))))
1659 retval
= DECL_RESULT (current_function_decl
);
1661 /* We use temporary register to hold value when aggregate_value_p
1662 is false. Similarly for DECL_BY_REFERENCE we must avoid extra
1664 if (!aggregate_value_p (retval
, TREE_TYPE (current_function_decl
))
1665 && !DECL_BY_REFERENCE (retval
))
1666 retval
= create_tmp_reg (TREE_TYPE (retval
));
1667 if (is_gimple_reg (retval
))
1669 /* When returning by reference, there is only one SSA name
1670 assigned to RESULT_DECL (that is pointer to return value).
1671 Look it up or create new one if it is missing. */
1672 if (DECL_BY_REFERENCE (retval
))
1673 retval
= get_or_create_ssa_default_def (cfun
, retval
);
1674 /* Otherwise produce new SSA name for return value. */
1676 retval
= make_ssa_name (retval
, call
);
1678 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
1679 gimple_call_set_lhs (call
, build_simple_mem_ref (retval
));
1681 gimple_call_set_lhs (call
, retval
);
1682 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1686 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1688 && is_gimple_reg_type (TREE_TYPE (retval
))
1689 && !is_gimple_val (retval
))
1692 = gimple_build_assign (make_ssa_name (TREE_TYPE (retval
)),
1694 retval
= gimple_assign_lhs (g
);
1695 gsi_insert_after (&gsi
, g
, GSI_NEW_STMT
);
1698 if (tsan_func_exit_call
)
1699 gsi_insert_after (&gsi
, tsan_func_exit_call
, GSI_NEW_STMT
);
1700 ret
= gimple_build_return (retval
);
1701 gsi_insert_after (&gsi
, ret
, GSI_NEW_STMT
);
1704 free_dominance_info (CDI_DOMINATORS
);
1705 free_dominance_info (CDI_POST_DOMINATORS
);
1706 compute_fn_summary (node
, true);
1709 /* Execute function splitting pass. */
1712 execute_split_functions (void)
1714 gimple_stmt_iterator bsi
;
1716 sreal overall_time
= 0;
1717 int overall_size
= 0;
1719 struct cgraph_node
*node
= cgraph_node::get (current_function_decl
);
1721 if (flags_from_decl_or_type (current_function_decl
)
1722 & (ECF_NORETURN
|ECF_MALLOC
|ECF_RETURNS_TWICE
))
1725 fprintf (dump_file
, "Not splitting: noreturn/malloc/returns_twice "
1729 if (MAIN_NAME_P (DECL_NAME (current_function_decl
)))
1732 fprintf (dump_file
, "Not splitting: main function.\n");
1735 if (node
->frequency
== NODE_FREQUENCY_UNLIKELY_EXECUTED
)
1738 fprintf (dump_file
, "Not splitting: function is unlikely executed.\n");
1741 /* This can be relaxed; function might become inlinable after splitting
1742 away the uninlinable part. */
1743 if (ipa_fn_summaries
1744 && ipa_fn_summaries
->get (node
)
1745 && !ipa_fn_summaries
->get (node
)->inlinable
)
1748 fprintf (dump_file
, "Not splitting: not inlinable.\n");
1751 if (DECL_DISREGARD_INLINE_LIMITS (node
->decl
))
1754 fprintf (dump_file
, "Not splitting: disregarding inline limits.\n");
1757 /* This can be relaxed; most of versioning tests actually prevents
1759 if (!tree_versionable_function_p (current_function_decl
))
1762 fprintf (dump_file
, "Not splitting: not versionable.\n");
1765 /* FIXME: we could support this. */
1766 if (DECL_STRUCT_FUNCTION (current_function_decl
)->static_chain_decl
)
1769 fprintf (dump_file
, "Not splitting: nested function.\n");
1773 /* See if it makes sense to try to split.
1774 It makes sense to split if we inline, that is if we have direct calls to
1775 handle or direct calls are possibly going to appear as result of indirect
1776 inlining or LTO. Also handle -fprofile-generate as LTO to allow non-LTO
1777 training for LTO -fprofile-use build.
1779 Note that we are not completely conservative about disqualifying functions
1780 called once. It is possible that the caller is called more then once and
1781 then inlining would still benefit. */
1783 /* Local functions called once will be completely inlined most of time. */
1784 || (!node
->callers
->next_caller
&& node
->local
))
1785 && !node
->address_taken
1786 && !node
->has_aliases_p ()
1787 && (!flag_lto
|| !node
->externally_visible
))
1790 fprintf (dump_file
, "Not splitting: not called directly "
1791 "or called once.\n");
1795 /* FIXME: We can actually split if splitting reduces call overhead. */
1796 if (!flag_inline_small_functions
1797 && !DECL_DECLARED_INLINE_P (current_function_decl
))
1800 fprintf (dump_file
, "Not splitting: not autoinlining and function"
1801 " is not inline.\n");
1805 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (current_function_decl
)))
1808 fprintf (dump_file
, "Not splitting: function is noinline.\n");
1811 if (lookup_attribute ("section", DECL_ATTRIBUTES (current_function_decl
)))
1814 fprintf (dump_file
, "Not splitting: function is in user defined "
1818 if (!strub_splittable_p (node
))
1821 fprintf (dump_file
, "Not splitting: function is a strub context.\n");
1825 /* We enforce splitting after loop headers when profile info is not
1827 if (profile_status_for_fn (cfun
) != PROFILE_READ
)
1828 mark_dfs_back_edges ();
1830 /* Initialize bitmap to track forbidden calls. */
1831 forbidden_dominators
= BITMAP_ALLOC (NULL
);
1832 calculate_dominance_info (CDI_DOMINATORS
);
1834 /* Compute local info about basic blocks and determine function size/time. */
1835 bb_info_vec
.safe_grow_cleared (last_basic_block_for_fn (cfun
) + 1, true);
1836 best_split_point
.split_bbs
= NULL
;
1837 basic_block return_bb
= find_return_bb ();
1838 int tsan_exit_found
= -1;
1839 FOR_EACH_BB_FN (bb
, cfun
)
1843 sreal freq
= bb
->count
.to_sreal_scale
1844 (ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
);
1846 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1847 fprintf (dump_file
, "Basic block %i\n", bb
->index
);
1849 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
1853 gimple
*stmt
= gsi_stmt (bsi
);
1855 this_size
= estimate_num_insns (stmt
, &eni_size_weights
);
1856 this_time
= (sreal
)estimate_num_insns (stmt
, &eni_time_weights
)
1860 check_forbidden_calls (stmt
);
1862 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1864 fprintf (dump_file
, " freq:%4.2f size:%3i time:%4.2f ",
1865 freq
.to_double (), this_size
, this_time
.to_double ());
1866 print_gimple_stmt (dump_file
, stmt
, 0);
1869 if ((flag_sanitize
& SANITIZE_THREAD
)
1870 && gimple_call_internal_p (stmt
, IFN_TSAN_FUNC_EXIT
))
1872 /* We handle TSAN_FUNC_EXIT for splitting either in the
1873 return_bb, or in its immediate predecessors. */
1874 if ((bb
!= return_bb
&& !find_edge (bb
, return_bb
))
1875 || (tsan_exit_found
!= -1
1876 && tsan_exit_found
!= (bb
!= return_bb
)))
1879 fprintf (dump_file
, "Not splitting: TSAN_FUNC_EXIT"
1880 " in unexpected basic block.\n");
1881 BITMAP_FREE (forbidden_dominators
);
1882 bb_info_vec
.release ();
1885 tsan_exit_found
= bb
!= return_bb
;
1888 overall_time
+= time
;
1889 overall_size
+= size
;
1890 bb_info_vec
[bb
->index
].time
= time
;
1891 bb_info_vec
[bb
->index
].size
= size
;
1893 find_split_points (return_bb
, overall_time
, overall_size
);
1894 if (best_split_point
.split_bbs
)
1896 split_function (return_bb
, &best_split_point
, tsan_exit_found
== 1);
1897 BITMAP_FREE (best_split_point
.ssa_names_to_pass
);
1898 BITMAP_FREE (best_split_point
.split_bbs
);
1899 todo
= TODO_update_ssa
| TODO_cleanup_cfg
;
1901 BITMAP_FREE (forbidden_dominators
);
1902 bb_info_vec
.release ();
1908 const pass_data pass_data_split_functions
=
1910 GIMPLE_PASS
, /* type */
1911 "fnsplit", /* name */
1912 OPTGROUP_NONE
, /* optinfo_flags */
1913 TV_IPA_FNSPLIT
, /* tv_id */
1914 PROP_cfg
, /* properties_required */
1915 0, /* properties_provided */
1916 0, /* properties_destroyed */
1917 0, /* todo_flags_start */
1918 0, /* todo_flags_finish */
1921 class pass_split_functions
: public gimple_opt_pass
1924 pass_split_functions (gcc::context
*ctxt
)
1925 : gimple_opt_pass (pass_data_split_functions
, ctxt
)
1928 /* opt_pass methods: */
1929 bool gate (function
*) final override
;
1930 unsigned int execute (function
*) final override
1932 return execute_split_functions ();
1935 }; // class pass_split_functions
1938 pass_split_functions::gate (function
*)
1940 /* When doing profile feedback, we want to execute the pass after profiling
1941 is read. So disable one in early optimization. */
1942 return (flag_partial_inlining
1943 && !profile_arc_flag
&& !flag_branch_probabilities
);
1949 make_pass_split_functions (gcc::context
*ctxt
)
1951 return new pass_split_functions (ctxt
);
1954 /* Execute function splitting pass. */
1957 execute_feedback_split_functions (void)
1959 unsigned int retval
= execute_split_functions ();
1961 retval
|= TODO_rebuild_cgraph_edges
;
1967 const pass_data pass_data_feedback_split_functions
=
1969 GIMPLE_PASS
, /* type */
1970 "feedback_fnsplit", /* name */
1971 OPTGROUP_NONE
, /* optinfo_flags */
1972 TV_IPA_FNSPLIT
, /* tv_id */
1973 PROP_cfg
, /* properties_required */
1974 0, /* properties_provided */
1975 0, /* properties_destroyed */
1976 0, /* todo_flags_start */
1977 0, /* todo_flags_finish */
1980 class pass_feedback_split_functions
: public gimple_opt_pass
1983 pass_feedback_split_functions (gcc::context
*ctxt
)
1984 : gimple_opt_pass (pass_data_feedback_split_functions
, ctxt
)
1987 /* opt_pass methods: */
1988 bool gate (function
*) final override
;
1989 unsigned int execute (function
*) final override
1991 return execute_feedback_split_functions ();
1994 }; // class pass_feedback_split_functions
1997 pass_feedback_split_functions::gate (function
*)
1999 /* We don't need to split when profiling at all, we are producing
2000 lousy code anyway. */
2001 return (flag_partial_inlining
2002 && flag_branch_probabilities
);
2008 make_pass_feedback_split_functions (gcc::context
*ctxt
)
2010 return new pass_feedback_split_functions (ctxt
);