1 /* Function splitting pass
2 Copyright (C) 2010, 2011, 2012
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka <jh@suse.cz>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* The purpose of this pass is to split function bodies to improve
23 inlining. I.e. for function of the form:
48 When func becomes inlinable and when cheap_test is often true, inlining func,
49 but not fund.part leads to performance improvement similar as inlining
50 original func while the code size growth is smaller.
52 The pass is organized in three stages:
53 1) Collect local info about basic block into BB_INFO structure and
54 compute function body estimated size and time.
55 2) Via DFS walk find all possible basic blocks where we can split
57 3) If split point is found, split at the specified BB by creating a clone
58 and updating function to call it.
60 The decisions what functions to split are in execute_split_functions
63 There are several possible future improvements for this pass including:
65 1) Splitting to break up large functions
66 2) Splitting to reduce stack frame usage
67 3) Allow split part of function to use values computed in the header part.
68 The values needs to be passed to split function, perhaps via same
69 interface as for nested functions or as argument.
70 4) Support for simple rematerialization. I.e. when split part use
71 value computed in header from function parameter in very cheap way, we
72 can just recompute it.
73 5) Support splitting of nested functions.
74 6) Support non-SSA arguments.
75 7) There is nothing preventing us from producing multiple parts of single function
76 when needed or splitting also the parts. */
80 #include "coretypes.h"
85 #include "tree-flow.h"
86 #include "tree-pass.h"
88 #include "diagnostic.h"
89 #include "tree-dump.h"
90 #include "tree-inline.h"
92 #include "gimple-pretty-print.h"
93 #include "ipa-inline.h"
95 /* Per basic block info. */
103 DEF_VEC_ALLOC_O(bb_info
,heap
);
105 static VEC(bb_info
, heap
) *bb_info_vec
;
107 /* Description of split point. */
111 /* Size of the partitions. */
112 unsigned int header_time
, header_size
, split_time
, split_size
;
114 /* SSA names that need to be passed into spit function. */
115 bitmap ssa_names_to_pass
;
117 /* Basic block where we split (that will become entry point of new function. */
118 basic_block entry_bb
;
120 /* Basic blocks we are splitting away. */
123 /* True when return value is computed on split part and thus it needs
125 bool split_part_set_retval
;
128 /* Best split point found. */
130 struct split_point best_split_point
;
132 /* Set of basic blocks that are not allowed to dominate a split point. */
134 static bitmap forbidden_dominators
;
136 static tree
find_retval (basic_block return_bb
);
138 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
139 variable, check it if it is present in bitmap passed via DATA. */
142 test_nonssa_use (gimple stmt ATTRIBUTE_UNUSED
, tree t
, void *data
)
144 t
= get_base_address (t
);
146 if (!t
|| is_gimple_reg (t
))
149 if (TREE_CODE (t
) == PARM_DECL
150 || (TREE_CODE (t
) == VAR_DECL
151 && auto_var_in_fn_p (t
, current_function_decl
))
152 || TREE_CODE (t
) == RESULT_DECL
153 || TREE_CODE (t
) == LABEL_DECL
)
154 return bitmap_bit_p ((bitmap
)data
, DECL_UID (t
));
156 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
157 to pretend that the value pointed to is actual result decl. */
158 if ((TREE_CODE (t
) == MEM_REF
|| INDIRECT_REF_P (t
))
159 && TREE_CODE (TREE_OPERAND (t
, 0)) == SSA_NAME
160 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t
, 0))) == RESULT_DECL
161 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
163 bitmap_bit_p ((bitmap
)data
,
164 DECL_UID (DECL_RESULT (current_function_decl
)));
169 /* Dump split point CURRENT. */
172 dump_split_point (FILE * file
, struct split_point
*current
)
175 "Split point at BB %i\n"
176 " header time: %i header size: %i\n"
177 " split time: %i split size: %i\n bbs: ",
178 current
->entry_bb
->index
, current
->header_time
,
179 current
->header_size
, current
->split_time
, current
->split_size
);
180 dump_bitmap (file
, current
->split_bbs
);
181 fprintf (file
, " SSA names to pass: ");
182 dump_bitmap (file
, current
->ssa_names_to_pass
);
185 /* Look for all BBs in header that might lead to the split part and verify
186 that they are not defining any non-SSA var used by the split part.
187 Parameters are the same as for consider_split. */
190 verify_non_ssa_vars (struct split_point
*current
, bitmap non_ssa_vars
,
191 basic_block return_bb
)
193 bitmap seen
= BITMAP_ALLOC (NULL
);
194 VEC (basic_block
,heap
) *worklist
= NULL
;
199 FOR_EACH_EDGE (e
, ei
, current
->entry_bb
->preds
)
200 if (e
->src
!= ENTRY_BLOCK_PTR
201 && !bitmap_bit_p (current
->split_bbs
, e
->src
->index
))
203 VEC_safe_push (basic_block
, heap
, worklist
, e
->src
);
204 bitmap_set_bit (seen
, e
->src
->index
);
207 while (!VEC_empty (basic_block
, worklist
))
209 gimple_stmt_iterator bsi
;
210 basic_block bb
= VEC_pop (basic_block
, worklist
);
212 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
213 if (e
->src
!= ENTRY_BLOCK_PTR
214 && bitmap_set_bit (seen
, e
->src
->index
))
216 gcc_checking_assert (!bitmap_bit_p (current
->split_bbs
,
218 VEC_safe_push (basic_block
, heap
, worklist
, e
->src
);
220 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
222 gimple stmt
= gsi_stmt (bsi
);
223 if (is_gimple_debug (stmt
))
225 if (walk_stmt_load_store_addr_ops
226 (stmt
, non_ssa_vars
, test_nonssa_use
, test_nonssa_use
,
232 if (gimple_code (stmt
) == GIMPLE_LABEL
233 && test_nonssa_use (stmt
, gimple_label_label (stmt
),
240 for (bsi
= gsi_start_phis (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
242 if (walk_stmt_load_store_addr_ops
243 (gsi_stmt (bsi
), non_ssa_vars
, test_nonssa_use
, test_nonssa_use
,
250 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
252 if (e
->dest
!= return_bb
)
254 for (bsi
= gsi_start_phis (return_bb
); !gsi_end_p (bsi
);
257 gimple stmt
= gsi_stmt (bsi
);
258 tree op
= gimple_phi_arg_def (stmt
, e
->dest_idx
);
260 if (!is_gimple_reg (gimple_phi_result (stmt
)))
262 if (TREE_CODE (op
) != SSA_NAME
263 && test_nonssa_use (stmt
, op
, non_ssa_vars
))
273 VEC_free (basic_block
, heap
, worklist
);
277 /* If STMT is a call, check the callee against a list of forbidden
278 predicate functions. If a match is found, look for uses of the
279 call result in condition statements that compare against zero.
280 For each such use, find the block targeted by the condition
281 statement for the nonzero result, and set the bit for this block
282 in the forbidden dominators bitmap. The purpose of this is to avoid
283 selecting a split point where we are likely to lose the chance
284 to optimize away an unused function call. */
287 check_forbidden_calls (gimple stmt
)
289 imm_use_iterator use_iter
;
293 /* At the moment, __builtin_constant_p is the only forbidden
294 predicate function call (see PR49642). */
295 if (!gimple_call_builtin_p (stmt
, BUILT_IN_CONSTANT_P
))
298 lhs
= gimple_call_lhs (stmt
);
300 if (!lhs
|| TREE_CODE (lhs
) != SSA_NAME
)
303 FOR_EACH_IMM_USE_FAST (use_p
, use_iter
, lhs
)
306 basic_block use_bb
, forbidden_bb
;
308 edge true_edge
, false_edge
;
309 gimple use_stmt
= USE_STMT (use_p
);
311 if (gimple_code (use_stmt
) != GIMPLE_COND
)
314 /* Assuming canonical form for GIMPLE_COND here, with constant
315 in second position. */
316 op1
= gimple_cond_rhs (use_stmt
);
317 code
= gimple_cond_code (use_stmt
);
318 use_bb
= gimple_bb (use_stmt
);
320 extract_true_false_edges_from_block (use_bb
, &true_edge
, &false_edge
);
322 /* We're only interested in comparisons that distinguish
323 unambiguously from zero. */
324 if (!integer_zerop (op1
) || code
== LE_EXPR
|| code
== GE_EXPR
)
328 forbidden_bb
= false_edge
->dest
;
330 forbidden_bb
= true_edge
->dest
;
332 bitmap_set_bit (forbidden_dominators
, forbidden_bb
->index
);
336 /* If BB is dominated by any block in the forbidden dominators set,
337 return TRUE; else FALSE. */
340 dominated_by_forbidden (basic_block bb
)
345 EXECUTE_IF_SET_IN_BITMAP (forbidden_dominators
, 1, dom_bb
, bi
)
347 if (dominated_by_p (CDI_DOMINATORS
, bb
, BASIC_BLOCK (dom_bb
)))
354 /* We found an split_point CURRENT. NON_SSA_VARS is bitmap of all non ssa
355 variables used and RETURN_BB is return basic block.
356 See if we can split function here. */
359 consider_split (struct split_point
*current
, bitmap non_ssa_vars
,
360 basic_block return_bb
)
363 unsigned int num_args
= 0;
364 unsigned int call_overhead
;
367 gimple_stmt_iterator bsi
;
369 int incoming_freq
= 0;
372 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
373 dump_split_point (dump_file
, current
);
375 FOR_EACH_EDGE (e
, ei
, current
->entry_bb
->preds
)
376 if (!bitmap_bit_p (current
->split_bbs
, e
->src
->index
))
377 incoming_freq
+= EDGE_FREQUENCY (e
);
379 /* Do not split when we would end up calling function anyway. */
381 >= (ENTRY_BLOCK_PTR
->frequency
382 * PARAM_VALUE (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY
) / 100))
384 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
386 " Refused: incoming frequency is too large.\n");
390 if (!current
->header_size
)
392 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
393 fprintf (dump_file
, " Refused: header empty\n");
397 /* Verify that PHI args on entry are either virtual or all their operands
398 incoming from header are the same. */
399 for (bsi
= gsi_start_phis (current
->entry_bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
401 gimple stmt
= gsi_stmt (bsi
);
404 if (!is_gimple_reg (gimple_phi_result (stmt
)))
406 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
408 edge e
= gimple_phi_arg_edge (stmt
, i
);
409 if (!bitmap_bit_p (current
->split_bbs
, e
->src
->index
))
411 tree edge_val
= gimple_phi_arg_def (stmt
, i
);
412 if (val
&& edge_val
!= val
)
414 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
416 " Refused: entry BB has PHI with multiple variants\n");
425 /* See what argument we will pass to the split function and compute
427 call_overhead
= eni_size_weights
.call_cost
;
428 for (parm
= DECL_ARGUMENTS (current_function_decl
); parm
;
429 parm
= DECL_CHAIN (parm
))
431 if (!is_gimple_reg (parm
))
433 if (bitmap_bit_p (non_ssa_vars
, DECL_UID (parm
)))
435 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
437 " Refused: need to pass non-ssa param values\n");
441 else if (gimple_default_def (cfun
, parm
)
442 && bitmap_bit_p (current
->ssa_names_to_pass
,
443 SSA_NAME_VERSION (gimple_default_def
446 if (!VOID_TYPE_P (TREE_TYPE (parm
)))
447 call_overhead
+= estimate_move_cost (TREE_TYPE (parm
));
451 if (!VOID_TYPE_P (TREE_TYPE (current_function_decl
)))
452 call_overhead
+= estimate_move_cost (TREE_TYPE (current_function_decl
));
454 if (current
->split_size
<= call_overhead
)
456 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
458 " Refused: split size is smaller than call overhead\n");
461 if (current
->header_size
+ call_overhead
462 >= (unsigned int)(DECL_DECLARED_INLINE_P (current_function_decl
)
463 ? MAX_INLINE_INSNS_SINGLE
464 : MAX_INLINE_INSNS_AUTO
))
466 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
468 " Refused: header size is too large for inline candidate\n");
472 /* FIXME: we currently can pass only SSA function parameters to the split
473 arguments. Once parm_adjustment infrastructure is supported by cloning,
474 we can pass more than that. */
475 if (num_args
!= bitmap_count_bits (current
->ssa_names_to_pass
))
478 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
480 " Refused: need to pass non-param values\n");
484 /* When there are non-ssa vars used in the split region, see if they
485 are used in the header region. If so, reject the split.
486 FIXME: we can use nested function support to access both. */
487 if (!bitmap_empty_p (non_ssa_vars
)
488 && !verify_non_ssa_vars (current
, non_ssa_vars
, return_bb
))
490 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
492 " Refused: split part has non-ssa uses\n");
496 /* If the split point is dominated by a forbidden block, reject
498 if (!bitmap_empty_p (forbidden_dominators
)
499 && dominated_by_forbidden (current
->entry_bb
))
501 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
503 " Refused: split point dominated by forbidden block\n");
507 /* See if retval used by return bb is computed by header or split part.
508 When it is computed by split part, we need to produce return statement
509 in the split part and add code to header to pass it around.
511 This is bit tricky to test:
512 1) When there is no return_bb or no return value, we always pass
514 2) Invariants are always computed by caller.
515 3) For SSA we need to look if defining statement is in header or split part
516 4) For non-SSA we need to look where the var is computed. */
517 retval
= find_retval (return_bb
);
519 current
->split_part_set_retval
= true;
520 else if (is_gimple_min_invariant (retval
))
521 current
->split_part_set_retval
= false;
522 /* Special case is value returned by reference we record as if it was non-ssa
523 set to result_decl. */
524 else if (TREE_CODE (retval
) == SSA_NAME
525 && TREE_CODE (SSA_NAME_VAR (retval
)) == RESULT_DECL
526 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
527 current
->split_part_set_retval
528 = bitmap_bit_p (non_ssa_vars
, DECL_UID (SSA_NAME_VAR (retval
)));
529 else if (TREE_CODE (retval
) == SSA_NAME
)
530 current
->split_part_set_retval
531 = (!SSA_NAME_IS_DEFAULT_DEF (retval
)
532 && (bitmap_bit_p (current
->split_bbs
,
533 gimple_bb (SSA_NAME_DEF_STMT (retval
))->index
)
534 || gimple_bb (SSA_NAME_DEF_STMT (retval
)) == return_bb
));
535 else if (TREE_CODE (retval
) == PARM_DECL
)
536 current
->split_part_set_retval
= false;
537 else if (TREE_CODE (retval
) == VAR_DECL
538 || TREE_CODE (retval
) == RESULT_DECL
)
539 current
->split_part_set_retval
540 = bitmap_bit_p (non_ssa_vars
, DECL_UID (retval
));
542 current
->split_part_set_retval
= true;
544 /* split_function fixes up at most one PHI non-virtual PHI node in return_bb,
545 for the return value. If there are other PHIs, give up. */
546 if (return_bb
!= EXIT_BLOCK_PTR
)
548 gimple_stmt_iterator psi
;
550 for (psi
= gsi_start_phis (return_bb
); !gsi_end_p (psi
); gsi_next (&psi
))
551 if (is_gimple_reg (gimple_phi_result (gsi_stmt (psi
)))
553 && current
->split_part_set_retval
554 && TREE_CODE (retval
) == SSA_NAME
555 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
))
556 && SSA_NAME_DEF_STMT (retval
) == gsi_stmt (psi
)))
558 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
560 " Refused: return bb has extra PHIs\n");
565 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
566 fprintf (dump_file
, " Accepted!\n");
568 /* At the moment chose split point with lowest frequency and that leaves
569 out smallest size of header.
570 In future we might re-consider this heuristics. */
571 if (!best_split_point
.split_bbs
572 || best_split_point
.entry_bb
->frequency
> current
->entry_bb
->frequency
573 || (best_split_point
.entry_bb
->frequency
== current
->entry_bb
->frequency
574 && best_split_point
.split_size
< current
->split_size
))
577 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
578 fprintf (dump_file
, " New best split point!\n");
579 if (best_split_point
.ssa_names_to_pass
)
581 BITMAP_FREE (best_split_point
.ssa_names_to_pass
);
582 BITMAP_FREE (best_split_point
.split_bbs
);
584 best_split_point
= *current
;
585 best_split_point
.ssa_names_to_pass
= BITMAP_ALLOC (NULL
);
586 bitmap_copy (best_split_point
.ssa_names_to_pass
,
587 current
->ssa_names_to_pass
);
588 best_split_point
.split_bbs
= BITMAP_ALLOC (NULL
);
589 bitmap_copy (best_split_point
.split_bbs
, current
->split_bbs
);
593 /* Return basic block containing RETURN statement. We allow basic blocks
597 but return_bb can not be more complex than this.
598 If nothing is found, return EXIT_BLOCK_PTR.
600 When there are multiple RETURN statement, chose one with return value,
601 since that one is more likely shared by multiple code paths.
603 Return BB is special, because for function splitting it is the only
604 basic block that is duplicated in between header and split part of the
607 TODO: We might support multiple return blocks. */
610 find_return_bb (void)
613 basic_block return_bb
= EXIT_BLOCK_PTR
;
614 gimple_stmt_iterator bsi
;
615 bool found_return
= false;
616 tree retval
= NULL_TREE
;
618 if (!single_pred_p (EXIT_BLOCK_PTR
))
621 e
= single_pred_edge (EXIT_BLOCK_PTR
);
622 for (bsi
= gsi_last_bb (e
->src
); !gsi_end_p (bsi
); gsi_prev (&bsi
))
624 gimple stmt
= gsi_stmt (bsi
);
625 if (gimple_code (stmt
) == GIMPLE_LABEL
626 || is_gimple_debug (stmt
)
627 || gimple_clobber_p (stmt
))
629 else if (gimple_code (stmt
) == GIMPLE_ASSIGN
631 && gimple_assign_single_p (stmt
)
632 && (auto_var_in_fn_p (gimple_assign_rhs1 (stmt
),
633 current_function_decl
)
634 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
635 && retval
== gimple_assign_lhs (stmt
))
637 else if (gimple_code (stmt
) == GIMPLE_RETURN
)
640 retval
= gimple_return_retval (stmt
);
645 if (gsi_end_p (bsi
) && found_return
)
651 /* Given return basic block RETURN_BB, see where return value is really
654 find_retval (basic_block return_bb
)
656 gimple_stmt_iterator bsi
;
657 for (bsi
= gsi_start_bb (return_bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
658 if (gimple_code (gsi_stmt (bsi
)) == GIMPLE_RETURN
)
659 return gimple_return_retval (gsi_stmt (bsi
));
660 else if (gimple_code (gsi_stmt (bsi
)) == GIMPLE_ASSIGN
661 && !gimple_clobber_p (gsi_stmt (bsi
)))
662 return gimple_assign_rhs1 (gsi_stmt (bsi
));
666 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
667 variable, mark it as used in bitmap passed via DATA.
668 Return true when access to T prevents splitting the function. */
671 mark_nonssa_use (gimple stmt ATTRIBUTE_UNUSED
, tree t
, void *data
)
673 t
= get_base_address (t
);
675 if (!t
|| is_gimple_reg (t
))
678 /* At present we can't pass non-SSA arguments to split function.
679 FIXME: this can be relaxed by passing references to arguments. */
680 if (TREE_CODE (t
) == PARM_DECL
)
682 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
684 "Cannot split: use of non-ssa function parameter.\n");
688 if ((TREE_CODE (t
) == VAR_DECL
689 && auto_var_in_fn_p (t
, current_function_decl
))
690 || TREE_CODE (t
) == RESULT_DECL
691 || TREE_CODE (t
) == LABEL_DECL
)
692 bitmap_set_bit ((bitmap
)data
, DECL_UID (t
));
694 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
695 to pretend that the value pointed to is actual result decl. */
696 if ((TREE_CODE (t
) == MEM_REF
|| INDIRECT_REF_P (t
))
697 && TREE_CODE (TREE_OPERAND (t
, 0)) == SSA_NAME
698 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t
, 0))) == RESULT_DECL
699 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
701 bitmap_bit_p ((bitmap
)data
,
702 DECL_UID (DECL_RESULT (current_function_decl
)));
707 /* Compute local properties of basic block BB we collect when looking for
708 split points. We look for ssa defs and store them in SET_SSA_NAMES,
709 for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic
710 vars stored in NON_SSA_VARS.
712 When BB has edge to RETURN_BB, collect uses in RETURN_BB too.
714 Return false when BB contains something that prevents it from being put into
718 visit_bb (basic_block bb
, basic_block return_bb
,
719 bitmap set_ssa_names
, bitmap used_ssa_names
,
722 gimple_stmt_iterator bsi
;
725 bool can_split
= true;
727 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
729 gimple stmt
= gsi_stmt (bsi
);
734 if (is_gimple_debug (stmt
))
737 if (gimple_clobber_p (stmt
))
740 /* FIXME: We can split regions containing EH. We can not however
741 split RESX, EH_DISPATCH and EH_POINTER referring to same region
742 into different partitions. This would require tracking of
743 EH regions and checking in consider_split_point if they
744 are not used elsewhere. */
745 if (gimple_code (stmt
) == GIMPLE_RESX
)
747 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
748 fprintf (dump_file
, "Cannot split: resx.\n");
751 if (gimple_code (stmt
) == GIMPLE_EH_DISPATCH
)
753 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
754 fprintf (dump_file
, "Cannot split: eh dispatch.\n");
758 /* Check builtins that prevent splitting. */
759 if (gimple_code (stmt
) == GIMPLE_CALL
760 && (decl
= gimple_call_fndecl (stmt
)) != NULL_TREE
761 && DECL_BUILT_IN (decl
)
762 && DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
763 switch (DECL_FUNCTION_CODE (decl
))
765 /* FIXME: once we will allow passing non-parm values to split part,
766 we need to be sure to handle correct builtin_stack_save and
767 builtin_stack_restore. At the moment we are safe; there is no
768 way to store builtin_stack_save result in non-SSA variable
769 since all calls to those are compiler generated. */
771 case BUILT_IN_APPLY_ARGS
:
772 case BUILT_IN_VA_START
:
773 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
775 "Cannot split: builtin_apply and va_start.\n");
778 case BUILT_IN_EH_POINTER
:
779 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
780 fprintf (dump_file
, "Cannot split: builtin_eh_pointer.\n");
787 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_DEF
)
788 bitmap_set_bit (set_ssa_names
, SSA_NAME_VERSION (op
));
789 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
790 bitmap_set_bit (used_ssa_names
, SSA_NAME_VERSION (op
));
791 can_split
&= !walk_stmt_load_store_addr_ops (stmt
, non_ssa_vars
,
796 for (bsi
= gsi_start_phis (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
798 gimple stmt
= gsi_stmt (bsi
);
801 if (is_gimple_debug (stmt
))
803 if (!is_gimple_reg (gimple_phi_result (stmt
)))
805 bitmap_set_bit (set_ssa_names
,
806 SSA_NAME_VERSION (gimple_phi_result (stmt
)));
807 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
809 tree op
= gimple_phi_arg_def (stmt
, i
);
810 if (TREE_CODE (op
) == SSA_NAME
)
811 bitmap_set_bit (used_ssa_names
, SSA_NAME_VERSION (op
));
813 can_split
&= !walk_stmt_load_store_addr_ops (stmt
, non_ssa_vars
,
818 /* Record also uses coming from PHI operand in return BB. */
819 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
820 if (e
->dest
== return_bb
)
822 for (bsi
= gsi_start_phis (return_bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
824 gimple stmt
= gsi_stmt (bsi
);
825 tree op
= gimple_phi_arg_def (stmt
, e
->dest_idx
);
827 if (is_gimple_debug (stmt
))
829 if (!is_gimple_reg (gimple_phi_result (stmt
)))
831 if (TREE_CODE (op
) == SSA_NAME
)
832 bitmap_set_bit (used_ssa_names
, SSA_NAME_VERSION (op
));
834 can_split
&= !mark_nonssa_use (stmt
, op
, non_ssa_vars
);
840 /* Stack entry for recursive DFS walk in find_split_point. */
844 /* Basic block we are examining. */
847 /* SSA names set and used by the BB and all BBs reachable
848 from it via DFS walk. */
849 bitmap set_ssa_names
, used_ssa_names
;
852 /* All BBS visited from this BB via DFS walk. */
855 /* Last examined edge in DFS walk. Since we walk unoriented graph,
856 the value is up to sum of incoming and outgoing edges of BB. */
857 unsigned int edge_num
;
859 /* Stack entry index of earliest BB reachable from current BB
860 or any BB visited later in DFS walk. */
863 /* Overall time and size of all BBs reached from this BB in DFS walk. */
864 int overall_time
, overall_size
;
866 /* When false we can not split on this BB. */
869 DEF_VEC_O(stack_entry
);
870 DEF_VEC_ALLOC_O(stack_entry
,heap
);
873 /* Find all articulations and call consider_split on them.
874 OVERALL_TIME and OVERALL_SIZE is time and size of the function.
876 We perform basic algorithm for finding an articulation in a graph
877 created from CFG by considering it to be an unoriented graph.
879 The articulation is discovered via DFS walk. We collect earliest
880 basic block on stack that is reachable via backward edge. Articulation
881 is any basic block such that there is no backward edge bypassing it.
882 To reduce stack usage we maintain heap allocated stack in STACK vector.
883 AUX pointer of BB is set to index it appears in the stack or -1 once
884 it is visited and popped off the stack.
886 The algorithm finds articulation after visiting the whole component
887 reachable by it. This makes it convenient to collect information about
888 the component used by consider_split. */
891 find_split_points (int overall_time
, int overall_size
)
894 VEC(stack_entry
, heap
) *stack
= NULL
;
896 basic_block return_bb
= find_return_bb ();
897 struct split_point current
;
899 current
.header_time
= overall_time
;
900 current
.header_size
= overall_size
;
901 current
.split_time
= 0;
902 current
.split_size
= 0;
903 current
.ssa_names_to_pass
= BITMAP_ALLOC (NULL
);
905 first
.bb
= ENTRY_BLOCK_PTR
;
907 first
.overall_time
= 0;
908 first
.overall_size
= 0;
909 first
.earliest
= INT_MAX
;
910 first
.set_ssa_names
= 0;
911 first
.used_ssa_names
= 0;
912 first
.bbs_visited
= 0;
913 VEC_safe_push (stack_entry
, heap
, stack
, &first
);
914 ENTRY_BLOCK_PTR
->aux
= (void *)(intptr_t)-1;
916 while (!VEC_empty (stack_entry
, stack
))
918 stack_entry
*entry
= VEC_last (stack_entry
, stack
);
920 /* We are walking an acyclic graph, so edge_num counts
921 succ and pred edges together. However when considering
922 articulation, we want to have processed everything reachable
923 from articulation but nothing that reaches into it. */
924 if (entry
->edge_num
== EDGE_COUNT (entry
->bb
->succs
)
925 && entry
->bb
!= ENTRY_BLOCK_PTR
)
927 int pos
= VEC_length (stack_entry
, stack
);
928 entry
->can_split
&= visit_bb (entry
->bb
, return_bb
,
929 entry
->set_ssa_names
,
930 entry
->used_ssa_names
,
931 entry
->non_ssa_vars
);
932 if (pos
<= entry
->earliest
&& !entry
->can_split
933 && dump_file
&& (dump_flags
& TDF_DETAILS
))
935 "found articulation at bb %i but can not split\n",
937 if (pos
<= entry
->earliest
&& entry
->can_split
)
939 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
940 fprintf (dump_file
, "found articulation at bb %i\n",
942 current
.entry_bb
= entry
->bb
;
943 current
.ssa_names_to_pass
= BITMAP_ALLOC (NULL
);
944 bitmap_and_compl (current
.ssa_names_to_pass
,
945 entry
->used_ssa_names
, entry
->set_ssa_names
);
946 current
.header_time
= overall_time
- entry
->overall_time
;
947 current
.header_size
= overall_size
- entry
->overall_size
;
948 current
.split_time
= entry
->overall_time
;
949 current
.split_size
= entry
->overall_size
;
950 current
.split_bbs
= entry
->bbs_visited
;
951 consider_split (¤t
, entry
->non_ssa_vars
, return_bb
);
952 BITMAP_FREE (current
.ssa_names_to_pass
);
955 /* Do actual DFS walk. */
957 < (EDGE_COUNT (entry
->bb
->succs
)
958 + EDGE_COUNT (entry
->bb
->preds
)))
962 if (entry
->edge_num
< EDGE_COUNT (entry
->bb
->succs
))
964 e
= EDGE_SUCC (entry
->bb
, entry
->edge_num
);
969 e
= EDGE_PRED (entry
->bb
, entry
->edge_num
970 - EDGE_COUNT (entry
->bb
->succs
));
976 /* New BB to visit, push it to the stack. */
977 if (dest
!= return_bb
&& dest
!= EXIT_BLOCK_PTR
980 stack_entry new_entry
;
983 new_entry
.edge_num
= 0;
984 new_entry
.overall_time
985 = VEC_index (bb_info
, bb_info_vec
, dest
->index
)->time
;
986 new_entry
.overall_size
987 = VEC_index (bb_info
, bb_info_vec
, dest
->index
)->size
;
988 new_entry
.earliest
= INT_MAX
;
989 new_entry
.set_ssa_names
= BITMAP_ALLOC (NULL
);
990 new_entry
.used_ssa_names
= BITMAP_ALLOC (NULL
);
991 new_entry
.bbs_visited
= BITMAP_ALLOC (NULL
);
992 new_entry
.non_ssa_vars
= BITMAP_ALLOC (NULL
);
993 new_entry
.can_split
= true;
994 bitmap_set_bit (new_entry
.bbs_visited
, dest
->index
);
995 VEC_safe_push (stack_entry
, heap
, stack
, &new_entry
);
996 dest
->aux
= (void *)(intptr_t)VEC_length (stack_entry
, stack
);
998 /* Back edge found, record the earliest point. */
999 else if ((intptr_t)dest
->aux
> 0
1000 && (intptr_t)dest
->aux
< entry
->earliest
)
1001 entry
->earliest
= (intptr_t)dest
->aux
;
1003 /* We are done with examining the edges. Pop off the value from stack
1004 and merge stuff we accumulate during the walk. */
1005 else if (entry
->bb
!= ENTRY_BLOCK_PTR
)
1007 stack_entry
*prev
= VEC_index (stack_entry
, stack
,
1008 VEC_length (stack_entry
, stack
) - 2);
1010 entry
->bb
->aux
= (void *)(intptr_t)-1;
1011 prev
->can_split
&= entry
->can_split
;
1012 if (prev
->set_ssa_names
)
1014 bitmap_ior_into (prev
->set_ssa_names
, entry
->set_ssa_names
);
1015 bitmap_ior_into (prev
->used_ssa_names
, entry
->used_ssa_names
);
1016 bitmap_ior_into (prev
->bbs_visited
, entry
->bbs_visited
);
1017 bitmap_ior_into (prev
->non_ssa_vars
, entry
->non_ssa_vars
);
1019 if (prev
->earliest
> entry
->earliest
)
1020 prev
->earliest
= entry
->earliest
;
1021 prev
->overall_time
+= entry
->overall_time
;
1022 prev
->overall_size
+= entry
->overall_size
;
1023 BITMAP_FREE (entry
->set_ssa_names
);
1024 BITMAP_FREE (entry
->used_ssa_names
);
1025 BITMAP_FREE (entry
->bbs_visited
);
1026 BITMAP_FREE (entry
->non_ssa_vars
);
1027 VEC_pop (stack_entry
, stack
);
1030 VEC_pop (stack_entry
, stack
);
1032 ENTRY_BLOCK_PTR
->aux
= NULL
;
1035 VEC_free (stack_entry
, heap
, stack
);
1036 BITMAP_FREE (current
.ssa_names_to_pass
);
1039 /* Split function at SPLIT_POINT. */
1042 split_function (struct split_point
*split_point
)
1044 VEC (tree
, heap
) *args_to_pass
= NULL
;
1045 bitmap args_to_skip
;
1048 struct cgraph_node
*node
, *cur_node
= cgraph_get_node (current_function_decl
);
1049 basic_block return_bb
= find_return_bb ();
1050 basic_block call_bb
;
1051 gimple_stmt_iterator gsi
;
1055 tree retval
= NULL
, real_retval
= NULL
;
1056 bool split_part_return_p
= false;
1057 gimple last_stmt
= NULL
;
1063 fprintf (dump_file
, "\n\nSplitting function at:\n");
1064 dump_split_point (dump_file
, split_point
);
1067 if (cur_node
->local
.can_change_signature
)
1068 args_to_skip
= BITMAP_ALLOC (NULL
);
1070 args_to_skip
= NULL
;
1072 /* Collect the parameters of new function and args_to_skip bitmap. */
1073 for (parm
= DECL_ARGUMENTS (current_function_decl
);
1074 parm
; parm
= DECL_CHAIN (parm
), num
++)
1076 && (!is_gimple_reg (parm
)
1077 || !gimple_default_def (cfun
, parm
)
1078 || !bitmap_bit_p (split_point
->ssa_names_to_pass
,
1079 SSA_NAME_VERSION (gimple_default_def (cfun
,
1081 bitmap_set_bit (args_to_skip
, num
);
1084 /* This parm might not have been used up to now, but is going to be
1085 used, hence register it. */
1086 if (is_gimple_reg (parm
))
1088 arg
= gimple_default_def (cfun
, parm
);
1091 arg
= make_ssa_name (parm
, gimple_build_nop ());
1092 set_default_def (parm
, arg
);
1098 if (!useless_type_conversion_p (DECL_ARG_TYPE (parm
), TREE_TYPE (arg
)))
1099 arg
= fold_convert (DECL_ARG_TYPE (parm
), arg
);
1100 VEC_safe_push (tree
, heap
, args_to_pass
, arg
);
1103 /* See if the split function will return. */
1104 FOR_EACH_EDGE (e
, ei
, return_bb
->preds
)
1105 if (bitmap_bit_p (split_point
->split_bbs
, e
->src
->index
))
1108 split_part_return_p
= true;
1110 /* Add return block to what will become the split function.
1111 We do not return; no return block is needed. */
1112 if (!split_part_return_p
)
1114 /* We have no return block, so nothing is needed. */
1115 else if (return_bb
== EXIT_BLOCK_PTR
)
1117 /* When we do not want to return value, we need to construct
1118 new return block with empty return statement.
1119 FIXME: Once we are able to change return type, we should change function
1120 to return void instead of just outputting function with undefined return
1121 value. For structures this affects quality of codegen. */
1122 else if (!split_point
->split_part_set_retval
1123 && find_retval (return_bb
))
1125 bool redirected
= true;
1126 basic_block new_return_bb
= create_basic_block (NULL
, 0, return_bb
);
1127 gimple_stmt_iterator gsi
= gsi_start_bb (new_return_bb
);
1128 gsi_insert_after (&gsi
, gimple_build_return (NULL
), GSI_NEW_STMT
);
1132 FOR_EACH_EDGE (e
, ei
, return_bb
->preds
)
1133 if (bitmap_bit_p (split_point
->split_bbs
, e
->src
->index
))
1135 new_return_bb
->count
+= e
->count
;
1136 new_return_bb
->frequency
+= EDGE_FREQUENCY (e
);
1137 redirect_edge_and_branch (e
, new_return_bb
);
1142 e
= make_edge (new_return_bb
, EXIT_BLOCK_PTR
, 0);
1143 e
->probability
= REG_BR_PROB_BASE
;
1144 e
->count
= new_return_bb
->count
;
1145 bitmap_set_bit (split_point
->split_bbs
, new_return_bb
->index
);
1147 /* When we pass around the value, use existing return block. */
1149 bitmap_set_bit (split_point
->split_bbs
, return_bb
->index
);
1151 /* If RETURN_BB has virtual operand PHIs, they must be removed and the
1152 virtual operand marked for renaming as we change the CFG in a way that
1153 tree-inline is not able to compensate for.
1155 Note this can happen whether or not we have a return value. If we have
1156 a return value, then RETURN_BB may have PHIs for real operands too. */
1157 if (return_bb
!= EXIT_BLOCK_PTR
)
1160 for (gsi
= gsi_start_phis (return_bb
); !gsi_end_p (gsi
);)
1162 gimple stmt
= gsi_stmt (gsi
);
1163 if (is_gimple_reg (gimple_phi_result (stmt
)))
1168 mark_virtual_phi_result_for_renaming (stmt
);
1169 remove_phi_node (&gsi
, true);
1172 /* In reality we have to rename the reaching definition of the
1173 virtual operand at return_bb as we will eventually release it
1174 when we remove the code region we outlined.
1175 So we have to rename all immediate virtual uses of that region
1176 if we didn't see a PHI definition yet. */
1177 /* ??? In real reality we want to set the reaching vdef of the
1178 entry of the SESE region as the vuse of the call and the reaching
1179 vdef of the exit of the SESE region as the vdef of the call. */
1181 for (gsi
= gsi_start_bb (return_bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1183 gimple stmt
= gsi_stmt (gsi
);
1184 if (gimple_vuse (stmt
))
1186 gimple_set_vuse (stmt
, NULL_TREE
);
1189 if (gimple_vdef (stmt
))
1194 /* Now create the actual clone. */
1195 rebuild_cgraph_edges ();
1196 node
= cgraph_function_versioning (cur_node
, NULL
, NULL
, args_to_skip
,
1197 !split_part_return_p
,
1198 split_point
->split_bbs
,
1199 split_point
->entry_bb
, "part");
1200 /* For usual cloning it is enough to clear builtin only when signature
1201 changes. For partial inlining we however can not expect the part
1202 of builtin implementation to have same semantic as the whole. */
1203 if (DECL_BUILT_IN (node
->symbol
.decl
))
1205 DECL_BUILT_IN_CLASS (node
->symbol
.decl
) = NOT_BUILT_IN
;
1206 DECL_FUNCTION_CODE (node
->symbol
.decl
) = (enum built_in_function
) 0;
1208 cgraph_node_remove_callees (cur_node
);
1209 if (!split_part_return_p
)
1210 TREE_THIS_VOLATILE (node
->symbol
.decl
) = 1;
1212 dump_function_to_file (node
->symbol
.decl
, dump_file
, dump_flags
);
1214 /* Create the basic block we place call into. It is the entry basic block
1215 split after last label. */
1216 call_bb
= split_point
->entry_bb
;
1217 for (gsi
= gsi_start_bb (call_bb
); !gsi_end_p (gsi
);)
1218 if (gimple_code (gsi_stmt (gsi
)) == GIMPLE_LABEL
)
1220 last_stmt
= gsi_stmt (gsi
);
1225 e
= split_block (split_point
->entry_bb
, last_stmt
);
1228 /* Produce the call statement. */
1229 gsi
= gsi_last_bb (call_bb
);
1230 FOR_EACH_VEC_ELT (tree
, args_to_pass
, i
, arg
)
1231 if (!is_gimple_val (arg
))
1233 arg
= force_gimple_operand_gsi (&gsi
, arg
, true, NULL_TREE
,
1234 false, GSI_CONTINUE_LINKING
);
1235 VEC_replace (tree
, args_to_pass
, i
, arg
);
1237 call
= gimple_build_call_vec (node
->symbol
.decl
, args_to_pass
);
1238 gimple_set_block (call
, DECL_INITIAL (current_function_decl
));
1240 /* We avoid address being taken on any variable used by split part,
1241 so return slot optimization is always possible. Moreover this is
1242 required to make DECL_BY_REFERENCE work. */
1243 if (aggregate_value_p (DECL_RESULT (current_function_decl
),
1244 TREE_TYPE (current_function_decl
)))
1245 gimple_call_set_return_slot_opt (call
, true);
1247 /* Update return value. This is bit tricky. When we do not return,
1248 do nothing. When we return we might need to update return_bb
1249 or produce a new return statement. */
1250 if (!split_part_return_p
)
1251 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1254 e
= make_edge (call_bb
, return_bb
,
1255 return_bb
== EXIT_BLOCK_PTR
? 0 : EDGE_FALLTHRU
);
1256 e
->count
= call_bb
->count
;
1257 e
->probability
= REG_BR_PROB_BASE
;
1259 /* If there is return basic block, see what value we need to store
1260 return value into and put call just before it. */
1261 if (return_bb
!= EXIT_BLOCK_PTR
)
1263 real_retval
= retval
= find_retval (return_bb
);
1265 if (real_retval
&& split_point
->split_part_set_retval
)
1267 gimple_stmt_iterator psi
;
1269 /* See if we need new SSA_NAME for the result.
1270 When DECL_BY_REFERENCE is true, retval is actually pointer to
1271 return value and it is constant in whole function. */
1272 if (TREE_CODE (retval
) == SSA_NAME
1273 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
1275 retval
= make_ssa_name (SSA_NAME_VAR (retval
), call
);
1277 /* See if there is PHI defining return value. */
1278 for (psi
= gsi_start_phis (return_bb
);
1279 !gsi_end_p (psi
); gsi_next (&psi
))
1280 if (is_gimple_reg (gimple_phi_result (gsi_stmt (psi
))))
1283 /* When there is PHI, just update its value. */
1284 if (TREE_CODE (retval
) == SSA_NAME
1285 && !gsi_end_p (psi
))
1286 add_phi_arg (gsi_stmt (psi
), retval
, e
, UNKNOWN_LOCATION
);
1287 /* Otherwise update the return BB itself.
1288 find_return_bb allows at most one assignment to return value,
1289 so update first statement. */
1292 gimple_stmt_iterator bsi
;
1293 for (bsi
= gsi_start_bb (return_bb
); !gsi_end_p (bsi
);
1295 if (gimple_code (gsi_stmt (bsi
)) == GIMPLE_RETURN
)
1297 gimple_return_set_retval (gsi_stmt (bsi
), retval
);
1300 else if (gimple_code (gsi_stmt (bsi
)) == GIMPLE_ASSIGN
1301 && !gimple_clobber_p (gsi_stmt (bsi
)))
1303 gimple_assign_set_rhs1 (gsi_stmt (bsi
), retval
);
1306 update_stmt (gsi_stmt (bsi
));
1309 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
1311 gimple_call_set_lhs (call
, build_simple_mem_ref (retval
));
1312 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1317 restype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
1318 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1319 if (!useless_type_conversion_p (TREE_TYPE (retval
), restype
))
1322 tree tem
= create_tmp_reg (restype
, NULL
);
1323 tem
= make_ssa_name (tem
, call
);
1324 cpy
= gimple_build_assign_with_ops (NOP_EXPR
, retval
,
1326 gsi_insert_after (&gsi
, cpy
, GSI_NEW_STMT
);
1329 gimple_call_set_lhs (call
, retval
);
1334 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1336 /* We don't use return block (there is either no return in function or
1337 multiple of them). So create new basic block with return statement.
1342 if (split_point
->split_part_set_retval
1343 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl
))))
1345 retval
= DECL_RESULT (current_function_decl
);
1347 /* We use temporary register to hold value when aggregate_value_p
1348 is false. Similarly for DECL_BY_REFERENCE we must avoid extra
1350 if (!aggregate_value_p (retval
, TREE_TYPE (current_function_decl
))
1351 && !DECL_BY_REFERENCE (retval
))
1352 retval
= create_tmp_reg (TREE_TYPE (retval
), NULL
);
1353 if (is_gimple_reg (retval
))
1355 /* When returning by reference, there is only one SSA name
1356 assigned to RESULT_DECL (that is pointer to return value).
1357 Look it up or create new one if it is missing. */
1358 if (DECL_BY_REFERENCE (retval
))
1361 if ((retval_name
= gimple_default_def (cfun
, retval
))
1363 retval
= retval_name
;
1366 retval_name
= make_ssa_name (retval
,
1367 gimple_build_nop ());
1368 set_default_def (retval
, retval_name
);
1369 retval
= retval_name
;
1372 /* Otherwise produce new SSA name for return value. */
1374 retval
= make_ssa_name (retval
, call
);
1376 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
1377 gimple_call_set_lhs (call
, build_simple_mem_ref (retval
));
1379 gimple_call_set_lhs (call
, retval
);
1381 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1382 ret
= gimple_build_return (retval
);
1383 gsi_insert_after (&gsi
, ret
, GSI_NEW_STMT
);
1386 free_dominance_info (CDI_DOMINATORS
);
1387 free_dominance_info (CDI_POST_DOMINATORS
);
1388 compute_inline_parameters (node
, true);
1391 /* Execute function splitting pass. */
1394 execute_split_functions (void)
1396 gimple_stmt_iterator bsi
;
1398 int overall_time
= 0, overall_size
= 0;
1400 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
1402 if (flags_from_decl_or_type (current_function_decl
)
1403 & (ECF_NORETURN
|ECF_MALLOC
))
1406 fprintf (dump_file
, "Not splitting: noreturn/malloc function.\n");
1409 if (MAIN_NAME_P (DECL_NAME (current_function_decl
)))
1412 fprintf (dump_file
, "Not splitting: main function.\n");
1415 /* This can be relaxed; function might become inlinable after splitting
1416 away the uninlinable part. */
1417 if (!inline_summary (node
)->inlinable
)
1420 fprintf (dump_file
, "Not splitting: not inlinable.\n");
1423 if (DECL_DISREGARD_INLINE_LIMITS (node
->symbol
.decl
))
1426 fprintf (dump_file
, "Not splitting: disregarding inline limits.\n");
1429 /* This can be relaxed; most of versioning tests actually prevents
1431 if (!tree_versionable_function_p (current_function_decl
))
1434 fprintf (dump_file
, "Not splitting: not versionable.\n");
1437 /* FIXME: we could support this. */
1438 if (DECL_STRUCT_FUNCTION (current_function_decl
)->static_chain_decl
)
1441 fprintf (dump_file
, "Not splitting: nested function.\n");
1445 /* See if it makes sense to try to split.
1446 It makes sense to split if we inline, that is if we have direct calls to
1447 handle or direct calls are possibly going to appear as result of indirect
1448 inlining or LTO. Also handle -fprofile-generate as LTO to allow non-LTO
1449 training for LTO -fprofile-use build.
1451 Note that we are not completely conservative about disqualifying functions
1452 called once. It is possible that the caller is called more then once and
1453 then inlining would still benefit. */
1454 if ((!node
->callers
|| !node
->callers
->next_caller
)
1455 && !node
->symbol
.address_taken
1456 && (!flag_lto
|| !node
->symbol
.externally_visible
))
1459 fprintf (dump_file
, "Not splitting: not called directly "
1460 "or called once.\n");
1464 /* FIXME: We can actually split if splitting reduces call overhead. */
1465 if (!flag_inline_small_functions
1466 && !DECL_DECLARED_INLINE_P (current_function_decl
))
1469 fprintf (dump_file
, "Not splitting: not autoinlining and function"
1470 " is not inline.\n");
1474 /* Initialize bitmap to track forbidden calls. */
1475 forbidden_dominators
= BITMAP_ALLOC (NULL
);
1476 calculate_dominance_info (CDI_DOMINATORS
);
1478 /* Compute local info about basic blocks and determine function size/time. */
1479 VEC_safe_grow_cleared (bb_info
, heap
, bb_info_vec
, last_basic_block
+ 1);
1480 memset (&best_split_point
, 0, sizeof (best_split_point
));
1485 int freq
= compute_call_stmt_bb_frequency (current_function_decl
, bb
);
1487 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1488 fprintf (dump_file
, "Basic block %i\n", bb
->index
);
1490 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
1492 int this_time
, this_size
;
1493 gimple stmt
= gsi_stmt (bsi
);
1495 this_size
= estimate_num_insns (stmt
, &eni_size_weights
);
1496 this_time
= estimate_num_insns (stmt
, &eni_time_weights
) * freq
;
1499 check_forbidden_calls (stmt
);
1501 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1503 fprintf (dump_file
, " freq:%6i size:%3i time:%3i ",
1504 freq
, this_size
, this_time
);
1505 print_gimple_stmt (dump_file
, stmt
, 0, 0);
1508 overall_time
+= time
;
1509 overall_size
+= size
;
1510 VEC_index (bb_info
, bb_info_vec
, bb
->index
)->time
= time
;
1511 VEC_index (bb_info
, bb_info_vec
, bb
->index
)->size
= size
;
1513 find_split_points (overall_time
, overall_size
);
1514 if (best_split_point
.split_bbs
)
1516 split_function (&best_split_point
);
1517 BITMAP_FREE (best_split_point
.ssa_names_to_pass
);
1518 BITMAP_FREE (best_split_point
.split_bbs
);
1519 todo
= TODO_update_ssa
| TODO_cleanup_cfg
;
1521 BITMAP_FREE (forbidden_dominators
);
1522 VEC_free (bb_info
, heap
, bb_info_vec
);
1527 /* Gate function splitting pass. When doing profile feedback, we want
1528 to execute the pass after profiling is read. So disable one in
1529 early optimization. */
1532 gate_split_functions (void)
1534 return (flag_partial_inlining
1535 && !profile_arc_flag
&& !flag_branch_probabilities
);
1538 struct gimple_opt_pass pass_split_functions
=
1542 "fnsplit", /* name */
1543 gate_split_functions
, /* gate */
1544 execute_split_functions
, /* execute */
1547 0, /* static_pass_number */
1548 TV_IPA_FNSPLIT
, /* tv_id */
1549 PROP_cfg
, /* properties_required */
1550 0, /* properties_provided */
1551 0, /* properties_destroyed */
1552 0, /* todo_flags_start */
1553 TODO_verify_all
/* todo_flags_finish */
1557 /* Gate feedback driven function splitting pass.
1558 We don't need to split when profiling at all, we are producing
1559 lousy code anyway. */
1562 gate_feedback_split_functions (void)
1564 return (flag_partial_inlining
1565 && flag_branch_probabilities
);
1568 /* Execute function splitting pass. */
1571 execute_feedback_split_functions (void)
1573 unsigned int retval
= execute_split_functions ();
1575 retval
|= TODO_rebuild_cgraph_edges
;
1579 struct gimple_opt_pass pass_feedback_split_functions
=
1583 "feedback_fnsplit", /* name */
1584 gate_feedback_split_functions
, /* gate */
1585 execute_feedback_split_functions
, /* execute */
1588 0, /* static_pass_number */
1589 TV_IPA_FNSPLIT
, /* tv_id */
1590 PROP_cfg
, /* properties_required */
1591 0, /* properties_provided */
1592 0, /* properties_destroyed */
1593 0, /* todo_flags_start */
1594 TODO_verify_all
/* todo_flags_finish */