1 /* Function splitting pass
2 Copyright (C) 2010-2017 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. */
79 #include "coretypes.h"
85 #include "alloc-pool.h"
86 #include "tree-pass.h"
89 #include "diagnostic.h"
90 #include "fold-const.h"
94 #include "gimple-iterator.h"
95 #include "gimplify-me.h"
96 #include "gimple-walk.h"
97 #include "symbol-summary.h"
100 #include "tree-into-ssa.h"
101 #include "tree-dfa.h"
102 #include "tree-inline.h"
104 #include "gimple-pretty-print.h"
105 #include "ipa-fnsummary.h"
107 #include "tree-chkp.h"
109 /* Per basic block info. */
117 static vec
<split_bb_info
> bb_info_vec
;
119 /* Description of split point. */
123 /* Size of the partitions. */
124 unsigned int header_time
, header_size
, split_time
, 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 /* Basic blocks we are splitting away. */
135 /* True when return value is computed on split part and thus it needs
137 bool split_part_set_retval
;
140 /* Best split point found. */
142 struct split_point best_split_point
;
144 /* Set of basic blocks that are not allowed to dominate a split point. */
146 static bitmap forbidden_dominators
;
148 static tree
find_retval (basic_block return_bb
);
149 static tree
find_retbnd (basic_block return_bb
);
151 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
152 variable, check it if it is present in bitmap passed via DATA. */
155 test_nonssa_use (gimple
*, tree t
, tree
, void *data
)
157 t
= get_base_address (t
);
159 if (!t
|| is_gimple_reg (t
))
162 if (TREE_CODE (t
) == PARM_DECL
164 && auto_var_in_fn_p (t
, current_function_decl
))
165 || TREE_CODE (t
) == RESULT_DECL
166 /* Normal labels are part of CFG and will be handled gratefuly.
167 Forced labels however can be used directly by statements and
168 need to stay in one partition along with their uses. */
169 || (TREE_CODE (t
) == LABEL_DECL
170 && FORCED_LABEL (t
)))
171 return bitmap_bit_p ((bitmap
)data
, DECL_UID (t
));
173 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
174 to pretend that the value pointed to is actual result decl. */
175 if ((TREE_CODE (t
) == MEM_REF
|| INDIRECT_REF_P (t
))
176 && TREE_CODE (TREE_OPERAND (t
, 0)) == SSA_NAME
177 && SSA_NAME_VAR (TREE_OPERAND (t
, 0))
178 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t
, 0))) == RESULT_DECL
179 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
181 bitmap_bit_p ((bitmap
)data
,
182 DECL_UID (DECL_RESULT (current_function_decl
)));
187 /* Dump split point CURRENT. */
190 dump_split_point (FILE * file
, struct split_point
*current
)
193 "Split point at BB %i\n"
194 " header time: %i header size: %i\n"
195 " split time: %i split size: %i\n bbs: ",
196 current
->entry_bb
->index
, current
->header_time
,
197 current
->header_size
, current
->split_time
, current
->split_size
);
198 dump_bitmap (file
, current
->split_bbs
);
199 fprintf (file
, " SSA names to pass: ");
200 dump_bitmap (file
, current
->ssa_names_to_pass
);
203 /* Look for all BBs in header that might lead to the split part and verify
204 that they are not defining any non-SSA var used by the split part.
205 Parameters are the same as for consider_split. */
208 verify_non_ssa_vars (struct split_point
*current
, bitmap non_ssa_vars
,
209 basic_block return_bb
)
211 bitmap seen
= BITMAP_ALLOC (NULL
);
212 vec
<basic_block
> worklist
= vNULL
;
218 FOR_EACH_EDGE (e
, ei
, current
->entry_bb
->preds
)
219 if (e
->src
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
)
220 && !bitmap_bit_p (current
->split_bbs
, e
->src
->index
))
222 worklist
.safe_push (e
->src
);
223 bitmap_set_bit (seen
, e
->src
->index
);
226 while (!worklist
.is_empty ())
228 bb
= worklist
.pop ();
229 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
230 if (e
->src
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
)
231 && bitmap_set_bit (seen
, e
->src
->index
))
233 gcc_checking_assert (!bitmap_bit_p (current
->split_bbs
,
235 worklist
.safe_push (e
->src
);
237 for (gimple_stmt_iterator bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
);
240 gimple
*stmt
= gsi_stmt (bsi
);
241 if (is_gimple_debug (stmt
))
243 if (walk_stmt_load_store_addr_ops
244 (stmt
, non_ssa_vars
, test_nonssa_use
, test_nonssa_use
,
250 if (glabel
*label_stmt
= dyn_cast
<glabel
*> (stmt
))
251 if (test_nonssa_use (stmt
, gimple_label_label (label_stmt
),
252 NULL_TREE
, non_ssa_vars
))
258 for (gphi_iterator bsi
= gsi_start_phis (bb
); !gsi_end_p (bsi
);
261 if (walk_stmt_load_store_addr_ops
262 (gsi_stmt (bsi
), non_ssa_vars
, test_nonssa_use
, test_nonssa_use
,
269 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
271 if (e
->dest
!= return_bb
)
273 for (gphi_iterator bsi
= gsi_start_phis (return_bb
);
277 gphi
*stmt
= bsi
.phi ();
278 tree op
= gimple_phi_arg_def (stmt
, e
->dest_idx
);
280 if (virtual_operand_p (gimple_phi_result (stmt
)))
282 if (TREE_CODE (op
) != SSA_NAME
283 && test_nonssa_use (stmt
, op
, op
, non_ssa_vars
))
292 /* Verify that the rest of function does not define any label
293 used by the split part. */
294 FOR_EACH_BB_FN (bb
, cfun
)
295 if (!bitmap_bit_p (current
->split_bbs
, bb
->index
)
296 && !bitmap_bit_p (seen
, bb
->index
))
298 gimple_stmt_iterator bsi
;
299 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
300 if (glabel
*label_stmt
= dyn_cast
<glabel
*> (gsi_stmt (bsi
)))
302 if (test_nonssa_use (label_stmt
,
303 gimple_label_label (label_stmt
),
304 NULL_TREE
, non_ssa_vars
))
320 /* If STMT is a call, check the callee against a list of forbidden
321 predicate functions. If a match is found, look for uses of the
322 call result in condition statements that compare against zero.
323 For each such use, find the block targeted by the condition
324 statement for the nonzero result, and set the bit for this block
325 in the forbidden dominators bitmap. The purpose of this is to avoid
326 selecting a split point where we are likely to lose the chance
327 to optimize away an unused function call. */
330 check_forbidden_calls (gimple
*stmt
)
332 imm_use_iterator use_iter
;
336 /* At the moment, __builtin_constant_p is the only forbidden
337 predicate function call (see PR49642). */
338 if (!gimple_call_builtin_p (stmt
, BUILT_IN_CONSTANT_P
))
341 lhs
= gimple_call_lhs (stmt
);
343 if (!lhs
|| TREE_CODE (lhs
) != SSA_NAME
)
346 FOR_EACH_IMM_USE_FAST (use_p
, use_iter
, lhs
)
349 basic_block use_bb
, forbidden_bb
;
351 edge true_edge
, false_edge
;
354 use_stmt
= dyn_cast
<gcond
*> (USE_STMT (use_p
));
358 /* Assuming canonical form for GIMPLE_COND here, with constant
359 in second position. */
360 op1
= gimple_cond_rhs (use_stmt
);
361 code
= gimple_cond_code (use_stmt
);
362 use_bb
= gimple_bb (use_stmt
);
364 extract_true_false_edges_from_block (use_bb
, &true_edge
, &false_edge
);
366 /* We're only interested in comparisons that distinguish
367 unambiguously from zero. */
368 if (!integer_zerop (op1
) || code
== LE_EXPR
|| code
== GE_EXPR
)
372 forbidden_bb
= false_edge
->dest
;
374 forbidden_bb
= true_edge
->dest
;
376 bitmap_set_bit (forbidden_dominators
, forbidden_bb
->index
);
380 /* If BB is dominated by any block in the forbidden dominators set,
381 return TRUE; else FALSE. */
384 dominated_by_forbidden (basic_block bb
)
389 EXECUTE_IF_SET_IN_BITMAP (forbidden_dominators
, 1, dom_bb
, bi
)
391 if (dominated_by_p (CDI_DOMINATORS
, bb
,
392 BASIC_BLOCK_FOR_FN (cfun
, dom_bb
)))
399 /* For give split point CURRENT and return block RETURN_BB return 1
400 if ssa name VAL is set by split part and 0 otherwise. */
402 split_part_set_ssa_name_p (tree val
, struct split_point
*current
,
403 basic_block return_bb
)
405 if (TREE_CODE (val
) != SSA_NAME
)
408 return (!SSA_NAME_IS_DEFAULT_DEF (val
)
409 && (bitmap_bit_p (current
->split_bbs
,
410 gimple_bb (SSA_NAME_DEF_STMT (val
))->index
)
411 || gimple_bb (SSA_NAME_DEF_STMT (val
)) == return_bb
));
414 /* We found an split_point CURRENT. NON_SSA_VARS is bitmap of all non ssa
415 variables used and RETURN_BB is return basic block.
416 See if we can split function here. */
419 consider_split (struct split_point
*current
, bitmap non_ssa_vars
,
420 basic_block return_bb
)
423 unsigned int num_args
= 0;
424 unsigned int call_overhead
;
429 int incoming_freq
= 0;
432 bool back_edge
= false;
434 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
435 dump_split_point (dump_file
, current
);
437 FOR_EACH_EDGE (e
, ei
, current
->entry_bb
->preds
)
439 if (e
->flags
& EDGE_DFS_BACK
)
441 if (!bitmap_bit_p (current
->split_bbs
, e
->src
->index
))
442 incoming_freq
+= EDGE_FREQUENCY (e
);
445 /* Do not split when we would end up calling function anyway. */
447 >= (ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
.to_frequency (cfun
)
448 * PARAM_VALUE (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY
) / 100))
450 /* When profile is guessed, we can not expect it to give us
451 realistic estimate on likelyness of function taking the
452 complex path. As a special case, when tail of the function is
453 a loop, enable splitting since inlining code skipping the loop
454 is likely noticeable win. */
456 && profile_status_for_fn (cfun
) != PROFILE_READ
458 < ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
.to_frequency (cfun
))
460 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
462 " Split before loop, accepting despite low frequencies %i %i.\n",
464 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
.to_frequency (cfun
));
468 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
470 " Refused: incoming frequency is too large.\n");
475 if (!current
->header_size
)
477 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
478 fprintf (dump_file
, " Refused: header empty\n");
482 /* Verify that PHI args on entry are either virtual or all their operands
483 incoming from header are the same. */
484 for (bsi
= gsi_start_phis (current
->entry_bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
486 gphi
*stmt
= bsi
.phi ();
489 if (virtual_operand_p (gimple_phi_result (stmt
)))
491 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
493 edge e
= gimple_phi_arg_edge (stmt
, i
);
494 if (!bitmap_bit_p (current
->split_bbs
, e
->src
->index
))
496 tree edge_val
= gimple_phi_arg_def (stmt
, i
);
497 if (val
&& edge_val
!= val
)
499 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
501 " Refused: entry BB has PHI with multiple variants\n");
510 /* See what argument we will pass to the split function and compute
512 call_overhead
= eni_size_weights
.call_cost
;
513 for (parm
= DECL_ARGUMENTS (current_function_decl
); parm
;
514 parm
= DECL_CHAIN (parm
))
516 if (!is_gimple_reg (parm
))
518 if (bitmap_bit_p (non_ssa_vars
, DECL_UID (parm
)))
520 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
522 " Refused: need to pass non-ssa param values\n");
528 tree ddef
= ssa_default_def (cfun
, parm
);
530 && bitmap_bit_p (current
->ssa_names_to_pass
,
531 SSA_NAME_VERSION (ddef
)))
533 if (!VOID_TYPE_P (TREE_TYPE (parm
)))
534 call_overhead
+= estimate_move_cost (TREE_TYPE (parm
), false);
539 if (!VOID_TYPE_P (TREE_TYPE (current_function_decl
)))
540 call_overhead
+= estimate_move_cost (TREE_TYPE (current_function_decl
),
543 if (current
->split_size
<= call_overhead
)
545 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
547 " Refused: split size is smaller than call overhead\n");
550 if (current
->header_size
+ call_overhead
551 >= (unsigned int)(DECL_DECLARED_INLINE_P (current_function_decl
)
552 ? MAX_INLINE_INSNS_SINGLE
553 : MAX_INLINE_INSNS_AUTO
))
555 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
557 " Refused: header size is too large for inline candidate\n");
561 /* Splitting functions brings the target out of comdat group; this will
562 lead to code duplication if the function is reused by other unit.
563 Limit this duplication. This is consistent with limit in tree-sra.c
564 FIXME: with LTO we ought to be able to do better! */
565 if (DECL_ONE_ONLY (current_function_decl
)
566 && current
->split_size
>= (unsigned int) MAX_INLINE_INSNS_AUTO
)
568 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
570 " Refused: function is COMDAT and tail is too large\n");
573 /* For comdat functions also reject very small tails; those will likely get
574 inlined back and we do not want to risk the duplication overhead.
575 FIXME: with LTO we ought to be able to do better! */
576 if (DECL_ONE_ONLY (current_function_decl
)
577 && current
->split_size
578 <= (unsigned int) PARAM_VALUE (PARAM_EARLY_INLINING_INSNS
) / 2)
580 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
582 " Refused: function is COMDAT and tail is too small\n");
586 /* FIXME: we currently can pass only SSA function parameters to the split
587 arguments. Once parm_adjustment infrastructure is supported by cloning,
588 we can pass more than that. */
589 if (num_args
!= bitmap_count_bits (current
->ssa_names_to_pass
))
592 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
594 " Refused: need to pass non-param values\n");
598 /* When there are non-ssa vars used in the split region, see if they
599 are used in the header region. If so, reject the split.
600 FIXME: we can use nested function support to access both. */
601 if (!bitmap_empty_p (non_ssa_vars
)
602 && !verify_non_ssa_vars (current
, non_ssa_vars
, return_bb
))
604 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
606 " Refused: split part has non-ssa uses\n");
610 /* If the split point is dominated by a forbidden block, reject
612 if (!bitmap_empty_p (forbidden_dominators
)
613 && dominated_by_forbidden (current
->entry_bb
))
615 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
617 " Refused: split point dominated by forbidden block\n");
621 /* See if retval used by return bb is computed by header or split part.
622 When it is computed by split part, we need to produce return statement
623 in the split part and add code to header to pass it around.
625 This is bit tricky to test:
626 1) When there is no return_bb or no return value, we always pass
628 2) Invariants are always computed by caller.
629 3) For SSA we need to look if defining statement is in header or split part
630 4) For non-SSA we need to look where the var is computed. */
631 retval
= find_retval (return_bb
);
634 /* If there is a return_bb with no return value in function returning
635 value by reference, also make the split part return void, otherwise
636 we expansion would try to create a non-POD temporary, which is
638 if (return_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
)
639 && DECL_RESULT (current_function_decl
)
640 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
641 current
->split_part_set_retval
= false;
643 current
->split_part_set_retval
= true;
645 else if (is_gimple_min_invariant (retval
))
646 current
->split_part_set_retval
= false;
647 /* Special case is value returned by reference we record as if it was non-ssa
648 set to result_decl. */
649 else if (TREE_CODE (retval
) == SSA_NAME
650 && SSA_NAME_VAR (retval
)
651 && TREE_CODE (SSA_NAME_VAR (retval
)) == RESULT_DECL
652 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
653 current
->split_part_set_retval
654 = bitmap_bit_p (non_ssa_vars
, DECL_UID (SSA_NAME_VAR (retval
)));
655 else if (TREE_CODE (retval
) == SSA_NAME
)
656 current
->split_part_set_retval
657 = split_part_set_ssa_name_p (retval
, current
, return_bb
);
658 else if (TREE_CODE (retval
) == PARM_DECL
)
659 current
->split_part_set_retval
= false;
660 else if (VAR_P (retval
)
661 || TREE_CODE (retval
) == RESULT_DECL
)
662 current
->split_part_set_retval
663 = bitmap_bit_p (non_ssa_vars
, DECL_UID (retval
));
665 current
->split_part_set_retval
= true;
667 /* See if retbnd used by return bb is computed by header or split part. */
668 retbnd
= find_retbnd (return_bb
);
671 bool split_part_set_retbnd
672 = split_part_set_ssa_name_p (retbnd
, current
, return_bb
);
674 /* If we have both return value and bounds then keep their definitions
675 in a single function. We use SSA names to link returned bounds and
676 value and therefore do not handle cases when result is passed by
677 reference (which should not be our case anyway since bounds are
678 returned for pointers only). */
679 if ((DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
))
680 && current
->split_part_set_retval
)
681 || split_part_set_retbnd
!= current
->split_part_set_retval
)
683 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
685 " Refused: split point splits return value and bounds\n");
690 /* split_function fixes up at most one PHI non-virtual PHI node in return_bb,
691 for the return value. If there are other PHIs, give up. */
692 if (return_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
696 for (psi
= gsi_start_phis (return_bb
); !gsi_end_p (psi
); gsi_next (&psi
))
697 if (!virtual_operand_p (gimple_phi_result (psi
.phi ()))
699 && current
->split_part_set_retval
700 && TREE_CODE (retval
) == SSA_NAME
701 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
))
702 && SSA_NAME_DEF_STMT (retval
) == psi
.phi ()))
704 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
706 " Refused: return bb has extra PHIs\n");
711 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
712 fprintf (dump_file
, " Accepted!\n");
714 /* At the moment chose split point with lowest frequency and that leaves
715 out smallest size of header.
716 In future we might re-consider this heuristics. */
717 if (!best_split_point
.split_bbs
718 || best_split_point
.entry_bb
->count
.to_frequency (cfun
)
719 > current
->entry_bb
->count
.to_frequency (cfun
)
720 || (best_split_point
.entry_bb
->count
.to_frequency (cfun
)
721 == current
->entry_bb
->count
.to_frequency (cfun
)
722 && best_split_point
.split_size
< current
->split_size
))
725 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
726 fprintf (dump_file
, " New best split point!\n");
727 if (best_split_point
.ssa_names_to_pass
)
729 BITMAP_FREE (best_split_point
.ssa_names_to_pass
);
730 BITMAP_FREE (best_split_point
.split_bbs
);
732 best_split_point
= *current
;
733 best_split_point
.ssa_names_to_pass
= BITMAP_ALLOC (NULL
);
734 bitmap_copy (best_split_point
.ssa_names_to_pass
,
735 current
->ssa_names_to_pass
);
736 best_split_point
.split_bbs
= BITMAP_ALLOC (NULL
);
737 bitmap_copy (best_split_point
.split_bbs
, current
->split_bbs
);
741 /* Return basic block containing RETURN statement. We allow basic blocks
745 but return_bb can not be more complex than this (except for
746 -fsanitize=thread we allow TSAN_FUNC_EXIT () internal call in there).
747 If nothing is found, return the exit block.
749 When there are multiple RETURN statement, chose one with return value,
750 since that one is more likely shared by multiple code paths.
752 Return BB is special, because for function splitting it is the only
753 basic block that is duplicated in between header and split part of the
756 TODO: We might support multiple return blocks. */
759 find_return_bb (void)
762 basic_block return_bb
= EXIT_BLOCK_PTR_FOR_FN (cfun
);
763 gimple_stmt_iterator bsi
;
764 bool found_return
= false;
765 tree retval
= NULL_TREE
;
767 if (!single_pred_p (EXIT_BLOCK_PTR_FOR_FN (cfun
)))
770 e
= single_pred_edge (EXIT_BLOCK_PTR_FOR_FN (cfun
));
771 for (bsi
= gsi_last_bb (e
->src
); !gsi_end_p (bsi
); gsi_prev (&bsi
))
773 gimple
*stmt
= gsi_stmt (bsi
);
774 if (gimple_code (stmt
) == GIMPLE_LABEL
775 || is_gimple_debug (stmt
)
776 || gimple_clobber_p (stmt
))
778 else if (gimple_code (stmt
) == GIMPLE_ASSIGN
780 && gimple_assign_single_p (stmt
)
781 && (auto_var_in_fn_p (gimple_assign_rhs1 (stmt
),
782 current_function_decl
)
783 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
784 && retval
== gimple_assign_lhs (stmt
))
786 else if (greturn
*return_stmt
= dyn_cast
<greturn
*> (stmt
))
789 retval
= gimple_return_retval (return_stmt
);
791 /* For -fsanitize=thread, allow also TSAN_FUNC_EXIT () in the return
793 else if ((flag_sanitize
& SANITIZE_THREAD
)
794 && gimple_call_internal_p (stmt
, IFN_TSAN_FUNC_EXIT
))
799 if (gsi_end_p (bsi
) && found_return
)
805 /* Given return basic block RETURN_BB, see where return value is really
808 find_retval (basic_block return_bb
)
810 gimple_stmt_iterator bsi
;
811 for (bsi
= gsi_start_bb (return_bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
812 if (greturn
*return_stmt
= dyn_cast
<greturn
*> (gsi_stmt (bsi
)))
813 return gimple_return_retval (return_stmt
);
814 else if (gimple_code (gsi_stmt (bsi
)) == GIMPLE_ASSIGN
815 && !gimple_clobber_p (gsi_stmt (bsi
)))
816 return gimple_assign_rhs1 (gsi_stmt (bsi
));
820 /* Given return basic block RETURN_BB, see where return bounds are really
823 find_retbnd (basic_block return_bb
)
825 gimple_stmt_iterator bsi
;
826 for (bsi
= gsi_last_bb (return_bb
); !gsi_end_p (bsi
); gsi_prev (&bsi
))
827 if (gimple_code (gsi_stmt (bsi
)) == GIMPLE_RETURN
)
828 return gimple_return_retbnd (gsi_stmt (bsi
));
832 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
833 variable, mark it as used in bitmap passed via DATA.
834 Return true when access to T prevents splitting the function. */
837 mark_nonssa_use (gimple
*, tree t
, tree
, void *data
)
839 t
= get_base_address (t
);
841 if (!t
|| is_gimple_reg (t
))
844 /* At present we can't pass non-SSA arguments to split function.
845 FIXME: this can be relaxed by passing references to arguments. */
846 if (TREE_CODE (t
) == PARM_DECL
)
848 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
850 "Cannot split: use of non-ssa function parameter.\n");
854 if ((VAR_P (t
) && auto_var_in_fn_p (t
, current_function_decl
))
855 || TREE_CODE (t
) == RESULT_DECL
856 || (TREE_CODE (t
) == LABEL_DECL
&& FORCED_LABEL (t
)))
857 bitmap_set_bit ((bitmap
)data
, DECL_UID (t
));
859 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
860 to pretend that the value pointed to is actual result decl. */
861 if ((TREE_CODE (t
) == MEM_REF
|| INDIRECT_REF_P (t
))
862 && TREE_CODE (TREE_OPERAND (t
, 0)) == SSA_NAME
863 && SSA_NAME_VAR (TREE_OPERAND (t
, 0))
864 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t
, 0))) == RESULT_DECL
865 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
867 bitmap_bit_p ((bitmap
)data
,
868 DECL_UID (DECL_RESULT (current_function_decl
)));
873 /* Compute local properties of basic block BB we collect when looking for
874 split points. We look for ssa defs and store them in SET_SSA_NAMES,
875 for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic
876 vars stored in NON_SSA_VARS.
878 When BB has edge to RETURN_BB, collect uses in RETURN_BB too.
880 Return false when BB contains something that prevents it from being put into
884 visit_bb (basic_block bb
, basic_block return_bb
,
885 bitmap set_ssa_names
, bitmap used_ssa_names
,
890 bool can_split
= true;
892 for (gimple_stmt_iterator bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
);
895 gimple
*stmt
= gsi_stmt (bsi
);
900 if (is_gimple_debug (stmt
))
903 if (gimple_clobber_p (stmt
))
906 /* FIXME: We can split regions containing EH. We can not however
907 split RESX, EH_DISPATCH and EH_POINTER referring to same region
908 into different partitions. This would require tracking of
909 EH regions and checking in consider_split_point if they
910 are not used elsewhere. */
911 if (gimple_code (stmt
) == GIMPLE_RESX
)
913 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
914 fprintf (dump_file
, "Cannot split: resx.\n");
917 if (gimple_code (stmt
) == GIMPLE_EH_DISPATCH
)
919 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
920 fprintf (dump_file
, "Cannot split: eh dispatch.\n");
924 /* Check builtins that prevent splitting. */
925 if (gimple_code (stmt
) == GIMPLE_CALL
926 && (decl
= gimple_call_fndecl (stmt
)) != NULL_TREE
927 && DECL_BUILT_IN (decl
)
928 && DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
929 switch (DECL_FUNCTION_CODE (decl
))
931 /* FIXME: once we will allow passing non-parm values to split part,
932 we need to be sure to handle correct builtin_stack_save and
933 builtin_stack_restore. At the moment we are safe; there is no
934 way to store builtin_stack_save result in non-SSA variable
935 since all calls to those are compiler generated. */
937 case BUILT_IN_APPLY_ARGS
:
938 case BUILT_IN_VA_START
:
939 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
941 "Cannot split: builtin_apply and va_start.\n");
944 case BUILT_IN_EH_POINTER
:
945 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
946 fprintf (dump_file
, "Cannot split: builtin_eh_pointer.\n");
953 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_DEF
)
954 bitmap_set_bit (set_ssa_names
, SSA_NAME_VERSION (op
));
955 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
956 bitmap_set_bit (used_ssa_names
, SSA_NAME_VERSION (op
));
957 can_split
&= !walk_stmt_load_store_addr_ops (stmt
, non_ssa_vars
,
962 for (gphi_iterator bsi
= gsi_start_phis (bb
); !gsi_end_p (bsi
);
965 gphi
*stmt
= bsi
.phi ();
968 if (virtual_operand_p (gimple_phi_result (stmt
)))
970 bitmap_set_bit (set_ssa_names
,
971 SSA_NAME_VERSION (gimple_phi_result (stmt
)));
972 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
974 tree op
= gimple_phi_arg_def (stmt
, i
);
975 if (TREE_CODE (op
) == SSA_NAME
)
976 bitmap_set_bit (used_ssa_names
, SSA_NAME_VERSION (op
));
978 can_split
&= !walk_stmt_load_store_addr_ops (stmt
, non_ssa_vars
,
983 /* Record also uses coming from PHI operand in return BB. */
984 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
985 if (e
->dest
== return_bb
)
987 for (gphi_iterator bsi
= gsi_start_phis (return_bb
);
991 gphi
*stmt
= bsi
.phi ();
992 tree op
= gimple_phi_arg_def (stmt
, e
->dest_idx
);
994 if (virtual_operand_p (gimple_phi_result (stmt
)))
996 if (TREE_CODE (op
) == SSA_NAME
)
997 bitmap_set_bit (used_ssa_names
, SSA_NAME_VERSION (op
));
999 can_split
&= !mark_nonssa_use (stmt
, op
, op
, non_ssa_vars
);
1005 /* 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. */
1029 int overall_time
, overall_size
;
1031 /* When false we can not split on this BB. */
1036 /* Find all articulations and call consider_split on them.
1037 OVERALL_TIME and OVERALL_SIZE is time and size of the function.
1039 We perform basic algorithm for finding an articulation in a graph
1040 created from CFG by considering it to be an unoriented graph.
1042 The articulation is discovered via DFS walk. We collect earliest
1043 basic block on stack that is reachable via backward edge. Articulation
1044 is any basic block such that there is no backward edge bypassing it.
1045 To reduce stack usage we maintain heap allocated stack in STACK vector.
1046 AUX pointer of BB is set to index it appears in the stack or -1 once
1047 it is visited and popped off the stack.
1049 The algorithm finds articulation after visiting the whole component
1050 reachable by it. This makes it convenient to collect information about
1051 the component used by consider_split. */
1054 find_split_points (basic_block return_bb
, int overall_time
, int overall_size
)
1057 vec
<stack_entry
> stack
= vNULL
;
1059 struct split_point current
;
1061 current
.header_time
= overall_time
;
1062 current
.header_size
= overall_size
;
1063 current
.split_time
= 0;
1064 current
.split_size
= 0;
1065 current
.ssa_names_to_pass
= BITMAP_ALLOC (NULL
);
1067 first
.bb
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
1069 first
.overall_time
= 0;
1070 first
.overall_size
= 0;
1071 first
.earliest
= INT_MAX
;
1072 first
.set_ssa_names
= 0;
1073 first
.used_ssa_names
= 0;
1074 first
.non_ssa_vars
= 0;
1075 first
.bbs_visited
= 0;
1076 first
.can_split
= false;
1077 stack
.safe_push (first
);
1078 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->aux
= (void *)(intptr_t)-1;
1080 while (!stack
.is_empty ())
1082 stack_entry
*entry
= &stack
.last ();
1084 /* We are walking an acyclic graph, so edge_num counts
1085 succ and pred edges together. However when considering
1086 articulation, we want to have processed everything reachable
1087 from articulation but nothing that reaches into it. */
1088 if (entry
->edge_num
== EDGE_COUNT (entry
->bb
->succs
)
1089 && entry
->bb
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
))
1091 int pos
= stack
.length ();
1092 entry
->can_split
&= visit_bb (entry
->bb
, return_bb
,
1093 entry
->set_ssa_names
,
1094 entry
->used_ssa_names
,
1095 entry
->non_ssa_vars
);
1096 if (pos
<= entry
->earliest
&& !entry
->can_split
1097 && dump_file
&& (dump_flags
& TDF_DETAILS
))
1099 "found articulation at bb %i but can not split\n",
1101 if (pos
<= entry
->earliest
&& entry
->can_split
)
1103 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1104 fprintf (dump_file
, "found articulation at bb %i\n",
1106 current
.entry_bb
= entry
->bb
;
1107 current
.ssa_names_to_pass
= BITMAP_ALLOC (NULL
);
1108 bitmap_and_compl (current
.ssa_names_to_pass
,
1109 entry
->used_ssa_names
, entry
->set_ssa_names
);
1110 current
.header_time
= overall_time
- entry
->overall_time
;
1111 current
.header_size
= overall_size
- entry
->overall_size
;
1112 current
.split_time
= entry
->overall_time
;
1113 current
.split_size
= entry
->overall_size
;
1114 current
.split_bbs
= entry
->bbs_visited
;
1115 consider_split (¤t
, entry
->non_ssa_vars
, return_bb
);
1116 BITMAP_FREE (current
.ssa_names_to_pass
);
1119 /* Do actual DFS walk. */
1121 < (EDGE_COUNT (entry
->bb
->succs
)
1122 + EDGE_COUNT (entry
->bb
->preds
)))
1126 if (entry
->edge_num
< EDGE_COUNT (entry
->bb
->succs
))
1128 e
= EDGE_SUCC (entry
->bb
, entry
->edge_num
);
1133 e
= EDGE_PRED (entry
->bb
, entry
->edge_num
1134 - EDGE_COUNT (entry
->bb
->succs
));
1140 /* New BB to visit, push it to the stack. */
1141 if (dest
!= return_bb
&& dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
)
1144 stack_entry new_entry
;
1146 new_entry
.bb
= dest
;
1147 new_entry
.edge_num
= 0;
1148 new_entry
.overall_time
1149 = bb_info_vec
[dest
->index
].time
;
1150 new_entry
.overall_size
1151 = bb_info_vec
[dest
->index
].size
;
1152 new_entry
.earliest
= INT_MAX
;
1153 new_entry
.set_ssa_names
= BITMAP_ALLOC (NULL
);
1154 new_entry
.used_ssa_names
= BITMAP_ALLOC (NULL
);
1155 new_entry
.bbs_visited
= BITMAP_ALLOC (NULL
);
1156 new_entry
.non_ssa_vars
= BITMAP_ALLOC (NULL
);
1157 new_entry
.can_split
= true;
1158 bitmap_set_bit (new_entry
.bbs_visited
, dest
->index
);
1159 stack
.safe_push (new_entry
);
1160 dest
->aux
= (void *)(intptr_t)stack
.length ();
1162 /* Back edge found, record the earliest point. */
1163 else if ((intptr_t)dest
->aux
> 0
1164 && (intptr_t)dest
->aux
< entry
->earliest
)
1165 entry
->earliest
= (intptr_t)dest
->aux
;
1167 /* We are done with examining the edges. Pop off the value from stack
1168 and merge stuff we accumulate during the walk. */
1169 else if (entry
->bb
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
))
1171 stack_entry
*prev
= &stack
[stack
.length () - 2];
1173 entry
->bb
->aux
= (void *)(intptr_t)-1;
1174 prev
->can_split
&= entry
->can_split
;
1175 if (prev
->set_ssa_names
)
1177 bitmap_ior_into (prev
->set_ssa_names
, entry
->set_ssa_names
);
1178 bitmap_ior_into (prev
->used_ssa_names
, entry
->used_ssa_names
);
1179 bitmap_ior_into (prev
->bbs_visited
, entry
->bbs_visited
);
1180 bitmap_ior_into (prev
->non_ssa_vars
, entry
->non_ssa_vars
);
1182 if (prev
->earliest
> entry
->earliest
)
1183 prev
->earliest
= entry
->earliest
;
1184 prev
->overall_time
+= entry
->overall_time
;
1185 prev
->overall_size
+= entry
->overall_size
;
1186 BITMAP_FREE (entry
->set_ssa_names
);
1187 BITMAP_FREE (entry
->used_ssa_names
);
1188 BITMAP_FREE (entry
->bbs_visited
);
1189 BITMAP_FREE (entry
->non_ssa_vars
);
1195 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->aux
= NULL
;
1196 FOR_EACH_BB_FN (bb
, cfun
)
1199 BITMAP_FREE (current
.ssa_names_to_pass
);
1202 /* Split function at SPLIT_POINT. */
1205 split_function (basic_block return_bb
, struct split_point
*split_point
,
1206 bool add_tsan_func_exit
)
1208 vec
<tree
> args_to_pass
= vNULL
;
1209 bitmap args_to_skip
;
1212 cgraph_node
*node
, *cur_node
= cgraph_node::get (current_function_decl
);
1213 basic_block call_bb
;
1214 gcall
*call
, *tsan_func_exit_call
= NULL
;
1217 tree retval
= NULL
, real_retval
= NULL
, retbnd
= NULL
;
1218 bool with_bounds
= chkp_function_instrumented_p (current_function_decl
);
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
->local
.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
);
1300 retbnd
= find_retbnd (return_bb
);
1302 /* When we pass around the value, use existing return block. */
1305 bitmap_set_bit (split_point
->split_bbs
, return_bb
->index
);
1306 retbnd
= find_retbnd (return_bb
);
1309 /* If RETURN_BB has virtual operand PHIs, they must be removed and the
1310 virtual operand marked for renaming as we change the CFG in a way that
1311 tree-inline is not able to compensate for.
1313 Note this can happen whether or not we have a return value. If we have
1314 a return value, then RETURN_BB may have PHIs for real operands too. */
1315 if (return_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
1318 for (gphi_iterator gsi
= gsi_start_phis (return_bb
);
1321 gphi
*stmt
= gsi
.phi ();
1322 if (!virtual_operand_p (gimple_phi_result (stmt
)))
1327 mark_virtual_phi_result_for_renaming (stmt
);
1328 remove_phi_node (&gsi
, true);
1331 /* In reality we have to rename the reaching definition of the
1332 virtual operand at return_bb as we will eventually release it
1333 when we remove the code region we outlined.
1334 So we have to rename all immediate virtual uses of that region
1335 if we didn't see a PHI definition yet. */
1336 /* ??? In real reality we want to set the reaching vdef of the
1337 entry of the SESE region as the vuse of the call and the reaching
1338 vdef of the exit of the SESE region as the vdef of the call. */
1340 for (gimple_stmt_iterator gsi
= gsi_start_bb (return_bb
);
1344 gimple
*stmt
= gsi_stmt (gsi
);
1345 if (gimple_vuse (stmt
))
1347 gimple_set_vuse (stmt
, NULL_TREE
);
1350 if (gimple_vdef (stmt
))
1355 /* Now create the actual clone. */
1356 cgraph_edge::rebuild_edges ();
1357 node
= cur_node
->create_version_clone_with_body
1358 (vNULL
, NULL
, args_to_skip
,
1359 !split_part_return_p
|| !split_point
->split_part_set_retval
,
1360 split_point
->split_bbs
, split_point
->entry_bb
, "part");
1362 node
->split_part
= true;
1364 if (cur_node
->same_comdat_group
)
1366 /* TODO: call is versionable if we make sure that all
1367 callers are inside of a comdat group. */
1368 cur_node
->calls_comdat_local
= 1;
1369 node
->add_to_same_comdat_group (cur_node
);
1373 /* Let's take a time profile for splitted function. */
1374 node
->tp_first_run
= cur_node
->tp_first_run
+ 1;
1376 /* For usual cloning it is enough to clear builtin only when signature
1377 changes. For partial inlining we however can not expect the part
1378 of builtin implementation to have same semantic as the whole. */
1379 if (DECL_BUILT_IN (node
->decl
))
1381 DECL_BUILT_IN_CLASS (node
->decl
) = NOT_BUILT_IN
;
1382 DECL_FUNCTION_CODE (node
->decl
) = (enum built_in_function
) 0;
1385 /* If return_bb contains any clobbers that refer to SSA_NAMEs
1386 set in the split part, remove them. Also reset debug stmts that
1387 refer to SSA_NAMEs set in the split part. */
1388 if (return_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
1390 gimple_stmt_iterator gsi
= gsi_start_bb (return_bb
);
1391 while (!gsi_end_p (gsi
))
1395 gimple
*stmt
= gsi_stmt (gsi
);
1396 bool remove
= false;
1397 if (gimple_clobber_p (stmt
) || is_gimple_debug (stmt
))
1398 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
1400 basic_block bb
= gimple_bb (SSA_NAME_DEF_STMT (op
));
1404 && bitmap_bit_p (split_point
->split_bbs
, bb
->index
))
1406 if (is_gimple_debug (stmt
))
1408 gimple_debug_bind_reset_value (stmt
);
1417 gsi_remove (&gsi
, true);
1423 /* If the original function is instrumented then it's
1424 part is also instrumented. */
1426 chkp_function_mark_instrumented (node
->decl
);
1428 /* If the original function is declared inline, there is no point in issuing
1429 a warning for the non-inlinable part. */
1430 DECL_NO_INLINE_WARNING_P (node
->decl
) = 1;
1431 cur_node
->remove_callees ();
1432 cur_node
->remove_all_references ();
1433 if (!split_part_return_p
)
1434 TREE_THIS_VOLATILE (node
->decl
) = 1;
1436 dump_function_to_file (node
->decl
, dump_file
, dump_flags
);
1438 /* Create the basic block we place call into. It is the entry basic block
1439 split after last label. */
1440 call_bb
= split_point
->entry_bb
;
1441 for (gimple_stmt_iterator gsi
= gsi_start_bb (call_bb
); !gsi_end_p (gsi
);)
1442 if (gimple_code (gsi_stmt (gsi
)) == GIMPLE_LABEL
)
1444 last_stmt
= gsi_stmt (gsi
);
1449 e
= split_block (split_point
->entry_bb
, last_stmt
);
1452 /* Produce the call statement. */
1453 gimple_stmt_iterator gsi
= gsi_last_bb (call_bb
);
1454 FOR_EACH_VEC_ELT (args_to_pass
, i
, arg
)
1455 if (!is_gimple_val (arg
))
1457 arg
= force_gimple_operand_gsi (&gsi
, arg
, true, NULL_TREE
,
1458 false, GSI_CONTINUE_LINKING
);
1459 args_to_pass
[i
] = arg
;
1461 call
= gimple_build_call_vec (node
->decl
, args_to_pass
);
1462 gimple_call_set_with_bounds (call
, with_bounds
);
1463 gimple_set_block (call
, DECL_INITIAL (current_function_decl
));
1464 args_to_pass
.release ();
1466 /* For optimized away parameters, add on the caller side
1468 DEBUG D#X => parm_Y(D)
1469 stmts and associate D#X with parm in decl_debug_args_lookup
1470 vector to say for debug info that if parameter parm had been passed,
1471 it would have value parm_Y(D). */
1474 vec
<tree
, va_gc
> **debug_args
= NULL
;
1475 unsigned i
= 0, len
= 0;
1476 if (MAY_HAVE_DEBUG_STMTS
)
1478 debug_args
= decl_debug_args_lookup (node
->decl
);
1480 len
= vec_safe_length (*debug_args
);
1482 for (parm
= DECL_ARGUMENTS (current_function_decl
), num
= 0;
1483 parm
; parm
= DECL_CHAIN (parm
), num
++)
1484 if (bitmap_bit_p (args_to_skip
, num
) && is_gimple_reg (parm
))
1489 /* This needs to be done even without MAY_HAVE_DEBUG_STMTS,
1490 otherwise if it didn't exist before, we'd end up with
1491 different SSA_NAME_VERSIONs between -g and -g0. */
1492 arg
= get_or_create_ssa_default_def (cfun
, parm
);
1493 if (!MAY_HAVE_DEBUG_STMTS
|| debug_args
== NULL
)
1496 while (i
< len
&& (**debug_args
)[i
] != DECL_ORIGIN (parm
))
1500 ddecl
= (**debug_args
)[i
+ 1];
1502 = gimple_build_debug_bind (ddecl
, unshare_expr (arg
), call
);
1503 gsi_insert_after (&gsi
, def_temp
, GSI_NEW_STMT
);
1507 /* We avoid address being taken on any variable used by split part,
1508 so return slot optimization is always possible. Moreover this is
1509 required to make DECL_BY_REFERENCE work. */
1510 if (aggregate_value_p (DECL_RESULT (current_function_decl
),
1511 TREE_TYPE (current_function_decl
))
1512 && (!is_gimple_reg_type (TREE_TYPE (DECL_RESULT (current_function_decl
)))
1513 || DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
))))
1514 gimple_call_set_return_slot_opt (call
, true);
1516 if (add_tsan_func_exit
)
1517 tsan_func_exit_call
= gimple_build_call_internal (IFN_TSAN_FUNC_EXIT
, 0);
1519 /* Update return value. This is bit tricky. When we do not return,
1520 do nothing. When we return we might need to update return_bb
1521 or produce a new return statement. */
1522 if (!split_part_return_p
)
1524 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1525 if (tsan_func_exit_call
)
1526 gsi_insert_after (&gsi
, tsan_func_exit_call
, GSI_NEW_STMT
);
1530 e
= make_single_succ_edge (call_bb
, return_bb
,
1531 return_bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
)
1532 ? 0 : EDGE_FALLTHRU
);
1534 /* If there is return basic block, see what value we need to store
1535 return value into and put call just before it. */
1536 if (return_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
1538 real_retval
= retval
;
1539 if (real_retval
&& split_point
->split_part_set_retval
)
1543 /* See if we need new SSA_NAME for the result.
1544 When DECL_BY_REFERENCE is true, retval is actually pointer to
1545 return value and it is constant in whole function. */
1546 if (TREE_CODE (retval
) == SSA_NAME
1547 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
1549 retval
= copy_ssa_name (retval
, call
);
1551 /* See if there is PHI defining return value. */
1552 for (psi
= gsi_start_phis (return_bb
);
1553 !gsi_end_p (psi
); gsi_next (&psi
))
1554 if (!virtual_operand_p (gimple_phi_result (psi
.phi ())))
1557 /* When there is PHI, just update its value. */
1558 if (TREE_CODE (retval
) == SSA_NAME
1559 && !gsi_end_p (psi
))
1560 add_phi_arg (psi
.phi (), retval
, e
, UNKNOWN_LOCATION
);
1561 /* Otherwise update the return BB itself.
1562 find_return_bb allows at most one assignment to return value,
1563 so update first statement. */
1566 gimple_stmt_iterator bsi
;
1567 for (bsi
= gsi_start_bb (return_bb
); !gsi_end_p (bsi
);
1569 if (greturn
*return_stmt
1570 = dyn_cast
<greturn
*> (gsi_stmt (bsi
)))
1572 gimple_return_set_retval (return_stmt
, retval
);
1575 else if (gimple_code (gsi_stmt (bsi
)) == GIMPLE_ASSIGN
1576 && !gimple_clobber_p (gsi_stmt (bsi
)))
1578 gimple_assign_set_rhs1 (gsi_stmt (bsi
), retval
);
1581 update_stmt (gsi_stmt (bsi
));
1582 /* Also adjust clobbers and debug stmts in return_bb. */
1583 for (bsi
= gsi_start_bb (return_bb
); !gsi_end_p (bsi
);
1586 gimple
*stmt
= gsi_stmt (bsi
);
1587 if (gimple_clobber_p (stmt
)
1588 || is_gimple_debug (stmt
))
1591 use_operand_p use_p
;
1592 bool update
= false;
1593 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
,
1595 if (USE_FROM_PTR (use_p
) == real_retval
)
1597 SET_USE (use_p
, retval
);
1606 /* Replace retbnd with new one. */
1609 gimple_stmt_iterator bsi
;
1610 for (bsi
= gsi_last_bb (return_bb
); !gsi_end_p (bsi
);
1612 if (gimple_code (gsi_stmt (bsi
)) == GIMPLE_RETURN
)
1614 retbnd
= copy_ssa_name (retbnd
, call
);
1615 gimple_return_set_retbnd (gsi_stmt (bsi
), retbnd
);
1616 update_stmt (gsi_stmt (bsi
));
1621 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
1623 gimple_call_set_lhs (call
, build_simple_mem_ref (retval
));
1624 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1629 restype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
1630 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1631 if (!useless_type_conversion_p (TREE_TYPE (retval
), restype
))
1634 tree tem
= create_tmp_reg (restype
);
1635 tem
= make_ssa_name (tem
, call
);
1636 cpy
= gimple_build_assign (retval
, NOP_EXPR
, tem
);
1637 gsi_insert_after (&gsi
, cpy
, GSI_NEW_STMT
);
1640 /* Build bndret call to obtain returned bounds. */
1642 chkp_insert_retbnd_call (retbnd
, retval
, &gsi
);
1643 gimple_call_set_lhs (call
, retval
);
1648 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1649 if (tsan_func_exit_call
)
1650 gsi_insert_after (&gsi
, tsan_func_exit_call
, GSI_NEW_STMT
);
1652 /* We don't use return block (there is either no return in function or
1653 multiple of them). So create new basic block with return statement.
1658 if (split_point
->split_part_set_retval
1659 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl
))))
1661 retval
= DECL_RESULT (current_function_decl
);
1663 if (chkp_function_instrumented_p (current_function_decl
)
1664 && BOUNDED_P (retval
))
1665 retbnd
= create_tmp_reg (pointer_bounds_type_node
);
1667 /* We use temporary register to hold value when aggregate_value_p
1668 is false. Similarly for DECL_BY_REFERENCE we must avoid extra
1670 if (!aggregate_value_p (retval
, TREE_TYPE (current_function_decl
))
1671 && !DECL_BY_REFERENCE (retval
))
1672 retval
= create_tmp_reg (TREE_TYPE (retval
));
1673 if (is_gimple_reg (retval
))
1675 /* When returning by reference, there is only one SSA name
1676 assigned to RESULT_DECL (that is pointer to return value).
1677 Look it up or create new one if it is missing. */
1678 if (DECL_BY_REFERENCE (retval
))
1679 retval
= get_or_create_ssa_default_def (cfun
, retval
);
1680 /* Otherwise produce new SSA name for return value. */
1682 retval
= make_ssa_name (retval
, call
);
1684 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
1685 gimple_call_set_lhs (call
, build_simple_mem_ref (retval
));
1687 gimple_call_set_lhs (call
, retval
);
1688 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1692 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1694 && is_gimple_reg_type (TREE_TYPE (retval
))
1695 && !is_gimple_val (retval
))
1698 = gimple_build_assign (make_ssa_name (TREE_TYPE (retval
)),
1700 retval
= gimple_assign_lhs (g
);
1701 gsi_insert_after (&gsi
, g
, GSI_NEW_STMT
);
1704 /* Build bndret call to obtain returned bounds. */
1706 chkp_insert_retbnd_call (retbnd
, retval
, &gsi
);
1707 if (tsan_func_exit_call
)
1708 gsi_insert_after (&gsi
, tsan_func_exit_call
, GSI_NEW_STMT
);
1709 ret
= gimple_build_return (retval
);
1710 gsi_insert_after (&gsi
, ret
, GSI_NEW_STMT
);
1713 free_dominance_info (CDI_DOMINATORS
);
1714 free_dominance_info (CDI_POST_DOMINATORS
);
1715 compute_fn_summary (node
, true);
1718 /* Execute function splitting pass. */
1721 execute_split_functions (void)
1723 gimple_stmt_iterator bsi
;
1725 int overall_time
= 0, overall_size
= 0;
1727 struct cgraph_node
*node
= cgraph_node::get (current_function_decl
);
1729 if (flags_from_decl_or_type (current_function_decl
)
1730 & (ECF_NORETURN
|ECF_MALLOC
))
1733 fprintf (dump_file
, "Not splitting: noreturn/malloc function.\n");
1736 if (MAIN_NAME_P (DECL_NAME (current_function_decl
)))
1739 fprintf (dump_file
, "Not splitting: main function.\n");
1742 /* This can be relaxed; function might become inlinable after splitting
1743 away the uninlinable part. */
1744 if (ipa_fn_summaries
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
.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 /* We enforce splitting after loop headers when profile info is not
1807 if (profile_status_for_fn (cfun
) != PROFILE_READ
)
1808 mark_dfs_back_edges ();
1810 /* Initialize bitmap to track forbidden calls. */
1811 forbidden_dominators
= BITMAP_ALLOC (NULL
);
1812 calculate_dominance_info (CDI_DOMINATORS
);
1814 /* Compute local info about basic blocks and determine function size/time. */
1815 bb_info_vec
.safe_grow_cleared (last_basic_block_for_fn (cfun
) + 1);
1816 memset (&best_split_point
, 0, sizeof (best_split_point
));
1817 basic_block return_bb
= find_return_bb ();
1818 int tsan_exit_found
= -1;
1819 FOR_EACH_BB_FN (bb
, cfun
)
1823 int freq
= compute_call_stmt_bb_frequency (current_function_decl
, bb
);
1825 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1826 fprintf (dump_file
, "Basic block %i\n", bb
->index
);
1828 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
1830 int this_time
, this_size
;
1831 gimple
*stmt
= gsi_stmt (bsi
);
1833 this_size
= estimate_num_insns (stmt
, &eni_size_weights
);
1834 this_time
= estimate_num_insns (stmt
, &eni_time_weights
) * freq
;
1837 check_forbidden_calls (stmt
);
1839 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1841 fprintf (dump_file
, " freq:%6i size:%3i time:%3i ",
1842 freq
, this_size
, this_time
);
1843 print_gimple_stmt (dump_file
, stmt
, 0);
1846 if ((flag_sanitize
& SANITIZE_THREAD
)
1847 && gimple_call_internal_p (stmt
, IFN_TSAN_FUNC_EXIT
))
1849 /* We handle TSAN_FUNC_EXIT for splitting either in the
1850 return_bb, or in its immediate predecessors. */
1851 if ((bb
!= return_bb
&& !find_edge (bb
, return_bb
))
1852 || (tsan_exit_found
!= -1
1853 && tsan_exit_found
!= (bb
!= return_bb
)))
1856 fprintf (dump_file
, "Not splitting: TSAN_FUNC_EXIT"
1857 " in unexpected basic block.\n");
1858 BITMAP_FREE (forbidden_dominators
);
1859 bb_info_vec
.release ();
1862 tsan_exit_found
= bb
!= return_bb
;
1865 overall_time
+= time
;
1866 overall_size
+= size
;
1867 bb_info_vec
[bb
->index
].time
= time
;
1868 bb_info_vec
[bb
->index
].size
= size
;
1870 find_split_points (return_bb
, overall_time
, overall_size
);
1871 if (best_split_point
.split_bbs
)
1873 split_function (return_bb
, &best_split_point
, tsan_exit_found
== 1);
1874 BITMAP_FREE (best_split_point
.ssa_names_to_pass
);
1875 BITMAP_FREE (best_split_point
.split_bbs
);
1876 todo
= TODO_update_ssa
| TODO_cleanup_cfg
;
1878 BITMAP_FREE (forbidden_dominators
);
1879 bb_info_vec
.release ();
1885 const pass_data pass_data_split_functions
=
1887 GIMPLE_PASS
, /* type */
1888 "fnsplit", /* name */
1889 OPTGROUP_NONE
, /* optinfo_flags */
1890 TV_IPA_FNSPLIT
, /* tv_id */
1891 PROP_cfg
, /* properties_required */
1892 0, /* properties_provided */
1893 0, /* properties_destroyed */
1894 0, /* todo_flags_start */
1895 0, /* todo_flags_finish */
1898 class pass_split_functions
: public gimple_opt_pass
1901 pass_split_functions (gcc::context
*ctxt
)
1902 : gimple_opt_pass (pass_data_split_functions
, ctxt
)
1905 /* opt_pass methods: */
1906 virtual bool gate (function
*);
1907 virtual unsigned int execute (function
*)
1909 return execute_split_functions ();
1912 }; // class pass_split_functions
1915 pass_split_functions::gate (function
*)
1917 /* When doing profile feedback, we want to execute the pass after profiling
1918 is read. So disable one in early optimization. */
1919 return (flag_partial_inlining
1920 && !profile_arc_flag
&& !flag_branch_probabilities
);
1926 make_pass_split_functions (gcc::context
*ctxt
)
1928 return new pass_split_functions (ctxt
);
1931 /* Execute function splitting pass. */
1934 execute_feedback_split_functions (void)
1936 unsigned int retval
= execute_split_functions ();
1938 retval
|= TODO_rebuild_cgraph_edges
;
1944 const pass_data pass_data_feedback_split_functions
=
1946 GIMPLE_PASS
, /* type */
1947 "feedback_fnsplit", /* name */
1948 OPTGROUP_NONE
, /* optinfo_flags */
1949 TV_IPA_FNSPLIT
, /* tv_id */
1950 PROP_cfg
, /* properties_required */
1951 0, /* properties_provided */
1952 0, /* properties_destroyed */
1953 0, /* todo_flags_start */
1954 0, /* todo_flags_finish */
1957 class pass_feedback_split_functions
: public gimple_opt_pass
1960 pass_feedback_split_functions (gcc::context
*ctxt
)
1961 : gimple_opt_pass (pass_data_feedback_split_functions
, ctxt
)
1964 /* opt_pass methods: */
1965 virtual bool gate (function
*);
1966 virtual unsigned int execute (function
*)
1968 return execute_feedback_split_functions ();
1971 }; // class pass_feedback_split_functions
1974 pass_feedback_split_functions::gate (function
*)
1976 /* We don't need to split when profiling at all, we are producing
1977 lousy code anyway. */
1978 return (flag_partial_inlining
1979 && flag_branch_probabilities
);
1985 make_pass_feedback_split_functions (gcc::context
*ctxt
)
1987 return new pass_feedback_split_functions (ctxt
);