1 /* Function splitting pass
2 Copyright (C) 2010-2013 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"
82 #include "stringpool.h"
86 #include "gimple-iterator.h"
87 #include "gimplify-me.h"
88 #include "gimple-walk.h"
91 #include "gimple-ssa.h"
93 #include "tree-phinodes.h"
94 #include "ssa-iterators.h"
95 #include "stringpool.h"
96 #include "tree-ssanames.h"
97 #include "tree-into-ssa.h"
99 #include "tree-pass.h"
101 #include "diagnostic.h"
102 #include "tree-dump.h"
103 #include "tree-inline.h"
105 #include "gimple-pretty-print.h"
106 #include "ipa-inline.h"
109 /* Per basic block info. */
117 static vec
<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
);
150 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
151 variable, check it if it is present in bitmap passed via DATA. */
154 test_nonssa_use (gimple stmt ATTRIBUTE_UNUSED
, tree t
, void *data
)
156 t
= get_base_address (t
);
158 if (!t
|| is_gimple_reg (t
))
161 if (TREE_CODE (t
) == PARM_DECL
162 || (TREE_CODE (t
) == VAR_DECL
163 && auto_var_in_fn_p (t
, current_function_decl
))
164 || TREE_CODE (t
) == RESULT_DECL
165 || TREE_CODE (t
) == LABEL_DECL
)
166 return bitmap_bit_p ((bitmap
)data
, DECL_UID (t
));
168 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
169 to pretend that the value pointed to is actual result decl. */
170 if ((TREE_CODE (t
) == MEM_REF
|| INDIRECT_REF_P (t
))
171 && TREE_CODE (TREE_OPERAND (t
, 0)) == SSA_NAME
172 && SSA_NAME_VAR (TREE_OPERAND (t
, 0))
173 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t
, 0))) == RESULT_DECL
174 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
176 bitmap_bit_p ((bitmap
)data
,
177 DECL_UID (DECL_RESULT (current_function_decl
)));
182 /* Dump split point CURRENT. */
185 dump_split_point (FILE * file
, struct split_point
*current
)
188 "Split point at BB %i\n"
189 " header time: %i header size: %i\n"
190 " split time: %i split size: %i\n bbs: ",
191 current
->entry_bb
->index
, current
->header_time
,
192 current
->header_size
, current
->split_time
, current
->split_size
);
193 dump_bitmap (file
, current
->split_bbs
);
194 fprintf (file
, " SSA names to pass: ");
195 dump_bitmap (file
, current
->ssa_names_to_pass
);
198 /* Look for all BBs in header that might lead to the split part and verify
199 that they are not defining any non-SSA var used by the split part.
200 Parameters are the same as for consider_split. */
203 verify_non_ssa_vars (struct split_point
*current
, bitmap non_ssa_vars
,
204 basic_block return_bb
)
206 bitmap seen
= BITMAP_ALLOC (NULL
);
207 vec
<basic_block
> worklist
= vNULL
;
212 FOR_EACH_EDGE (e
, ei
, current
->entry_bb
->preds
)
213 if (e
->src
!= ENTRY_BLOCK_PTR
214 && !bitmap_bit_p (current
->split_bbs
, e
->src
->index
))
216 worklist
.safe_push (e
->src
);
217 bitmap_set_bit (seen
, e
->src
->index
);
220 while (!worklist
.is_empty ())
222 gimple_stmt_iterator bsi
;
223 basic_block bb
= worklist
.pop ();
225 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
226 if (e
->src
!= ENTRY_BLOCK_PTR
227 && bitmap_set_bit (seen
, e
->src
->index
))
229 gcc_checking_assert (!bitmap_bit_p (current
->split_bbs
,
231 worklist
.safe_push (e
->src
);
233 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
235 gimple stmt
= gsi_stmt (bsi
);
236 if (is_gimple_debug (stmt
))
238 if (walk_stmt_load_store_addr_ops
239 (stmt
, non_ssa_vars
, test_nonssa_use
, test_nonssa_use
,
245 if (gimple_code (stmt
) == GIMPLE_LABEL
246 && test_nonssa_use (stmt
, gimple_label_label (stmt
),
253 for (bsi
= gsi_start_phis (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
255 if (walk_stmt_load_store_addr_ops
256 (gsi_stmt (bsi
), non_ssa_vars
, test_nonssa_use
, test_nonssa_use
,
263 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
265 if (e
->dest
!= return_bb
)
267 for (bsi
= gsi_start_phis (return_bb
); !gsi_end_p (bsi
);
270 gimple stmt
= gsi_stmt (bsi
);
271 tree op
= gimple_phi_arg_def (stmt
, e
->dest_idx
);
273 if (virtual_operand_p (gimple_phi_result (stmt
)))
275 if (TREE_CODE (op
) != SSA_NAME
276 && test_nonssa_use (stmt
, op
, non_ssa_vars
))
290 /* If STMT is a call, check the callee against a list of forbidden
291 predicate functions. If a match is found, look for uses of the
292 call result in condition statements that compare against zero.
293 For each such use, find the block targeted by the condition
294 statement for the nonzero result, and set the bit for this block
295 in the forbidden dominators bitmap. The purpose of this is to avoid
296 selecting a split point where we are likely to lose the chance
297 to optimize away an unused function call. */
300 check_forbidden_calls (gimple stmt
)
302 imm_use_iterator use_iter
;
306 /* At the moment, __builtin_constant_p is the only forbidden
307 predicate function call (see PR49642). */
308 if (!gimple_call_builtin_p (stmt
, BUILT_IN_CONSTANT_P
))
311 lhs
= gimple_call_lhs (stmt
);
313 if (!lhs
|| TREE_CODE (lhs
) != SSA_NAME
)
316 FOR_EACH_IMM_USE_FAST (use_p
, use_iter
, lhs
)
319 basic_block use_bb
, forbidden_bb
;
321 edge true_edge
, false_edge
;
322 gimple use_stmt
= USE_STMT (use_p
);
324 if (gimple_code (use_stmt
) != GIMPLE_COND
)
327 /* Assuming canonical form for GIMPLE_COND here, with constant
328 in second position. */
329 op1
= gimple_cond_rhs (use_stmt
);
330 code
= gimple_cond_code (use_stmt
);
331 use_bb
= gimple_bb (use_stmt
);
333 extract_true_false_edges_from_block (use_bb
, &true_edge
, &false_edge
);
335 /* We're only interested in comparisons that distinguish
336 unambiguously from zero. */
337 if (!integer_zerop (op1
) || code
== LE_EXPR
|| code
== GE_EXPR
)
341 forbidden_bb
= false_edge
->dest
;
343 forbidden_bb
= true_edge
->dest
;
345 bitmap_set_bit (forbidden_dominators
, forbidden_bb
->index
);
349 /* If BB is dominated by any block in the forbidden dominators set,
350 return TRUE; else FALSE. */
353 dominated_by_forbidden (basic_block bb
)
358 EXECUTE_IF_SET_IN_BITMAP (forbidden_dominators
, 1, dom_bb
, bi
)
360 if (dominated_by_p (CDI_DOMINATORS
, bb
, BASIC_BLOCK (dom_bb
)))
367 /* We found an split_point CURRENT. NON_SSA_VARS is bitmap of all non ssa
368 variables used and RETURN_BB is return basic block.
369 See if we can split function here. */
372 consider_split (struct split_point
*current
, bitmap non_ssa_vars
,
373 basic_block return_bb
)
376 unsigned int num_args
= 0;
377 unsigned int call_overhead
;
380 gimple_stmt_iterator bsi
;
382 int incoming_freq
= 0;
384 bool back_edge
= false;
386 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
387 dump_split_point (dump_file
, current
);
389 FOR_EACH_EDGE (e
, ei
, current
->entry_bb
->preds
)
391 if (e
->flags
& EDGE_DFS_BACK
)
393 if (!bitmap_bit_p (current
->split_bbs
, e
->src
->index
))
394 incoming_freq
+= EDGE_FREQUENCY (e
);
397 /* Do not split when we would end up calling function anyway. */
399 >= (ENTRY_BLOCK_PTR
->frequency
400 * PARAM_VALUE (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY
) / 100))
402 /* When profile is guessed, we can not expect it to give us
403 realistic estimate on likelyness of function taking the
404 complex path. As a special case, when tail of the function is
405 a loop, enable splitting since inlining code skipping the loop
406 is likely noticeable win. */
408 && profile_status
!= PROFILE_READ
409 && incoming_freq
< ENTRY_BLOCK_PTR
->frequency
)
411 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
413 " Split before loop, accepting despite low frequencies %i %i.\n",
415 ENTRY_BLOCK_PTR
->frequency
);
419 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
421 " Refused: incoming frequency is too large.\n");
426 if (!current
->header_size
)
428 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
429 fprintf (dump_file
, " Refused: header empty\n");
433 /* Verify that PHI args on entry are either virtual or all their operands
434 incoming from header are the same. */
435 for (bsi
= gsi_start_phis (current
->entry_bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
437 gimple stmt
= gsi_stmt (bsi
);
440 if (virtual_operand_p (gimple_phi_result (stmt
)))
442 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
444 edge e
= gimple_phi_arg_edge (stmt
, i
);
445 if (!bitmap_bit_p (current
->split_bbs
, e
->src
->index
))
447 tree edge_val
= gimple_phi_arg_def (stmt
, i
);
448 if (val
&& edge_val
!= val
)
450 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
452 " Refused: entry BB has PHI with multiple variants\n");
461 /* See what argument we will pass to the split function and compute
463 call_overhead
= eni_size_weights
.call_cost
;
464 for (parm
= DECL_ARGUMENTS (current_function_decl
); parm
;
465 parm
= DECL_CHAIN (parm
))
467 if (!is_gimple_reg (parm
))
469 if (bitmap_bit_p (non_ssa_vars
, DECL_UID (parm
)))
471 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
473 " Refused: need to pass non-ssa param values\n");
479 tree ddef
= ssa_default_def (cfun
, parm
);
481 && bitmap_bit_p (current
->ssa_names_to_pass
,
482 SSA_NAME_VERSION (ddef
)))
484 if (!VOID_TYPE_P (TREE_TYPE (parm
)))
485 call_overhead
+= estimate_move_cost (TREE_TYPE (parm
));
490 if (!VOID_TYPE_P (TREE_TYPE (current_function_decl
)))
491 call_overhead
+= estimate_move_cost (TREE_TYPE (current_function_decl
));
493 if (current
->split_size
<= call_overhead
)
495 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
497 " Refused: split size is smaller than call overhead\n");
500 if (current
->header_size
+ call_overhead
501 >= (unsigned int)(DECL_DECLARED_INLINE_P (current_function_decl
)
502 ? MAX_INLINE_INSNS_SINGLE
503 : MAX_INLINE_INSNS_AUTO
))
505 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
507 " Refused: header size is too large for inline candidate\n");
511 /* FIXME: we currently can pass only SSA function parameters to the split
512 arguments. Once parm_adjustment infrastructure is supported by cloning,
513 we can pass more than that. */
514 if (num_args
!= bitmap_count_bits (current
->ssa_names_to_pass
))
517 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
519 " Refused: need to pass non-param values\n");
523 /* When there are non-ssa vars used in the split region, see if they
524 are used in the header region. If so, reject the split.
525 FIXME: we can use nested function support to access both. */
526 if (!bitmap_empty_p (non_ssa_vars
)
527 && !verify_non_ssa_vars (current
, non_ssa_vars
, return_bb
))
529 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
531 " Refused: split part has non-ssa uses\n");
535 /* If the split point is dominated by a forbidden block, reject
537 if (!bitmap_empty_p (forbidden_dominators
)
538 && dominated_by_forbidden (current
->entry_bb
))
540 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
542 " Refused: split point dominated by forbidden block\n");
546 /* See if retval used by return bb is computed by header or split part.
547 When it is computed by split part, we need to produce return statement
548 in the split part and add code to header to pass it around.
550 This is bit tricky to test:
551 1) When there is no return_bb or no return value, we always pass
553 2) Invariants are always computed by caller.
554 3) For SSA we need to look if defining statement is in header or split part
555 4) For non-SSA we need to look where the var is computed. */
556 retval
= find_retval (return_bb
);
558 current
->split_part_set_retval
= true;
559 else if (is_gimple_min_invariant (retval
))
560 current
->split_part_set_retval
= false;
561 /* Special case is value returned by reference we record as if it was non-ssa
562 set to result_decl. */
563 else if (TREE_CODE (retval
) == SSA_NAME
564 && SSA_NAME_VAR (retval
)
565 && TREE_CODE (SSA_NAME_VAR (retval
)) == RESULT_DECL
566 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
567 current
->split_part_set_retval
568 = bitmap_bit_p (non_ssa_vars
, DECL_UID (SSA_NAME_VAR (retval
)));
569 else if (TREE_CODE (retval
) == SSA_NAME
)
570 current
->split_part_set_retval
571 = (!SSA_NAME_IS_DEFAULT_DEF (retval
)
572 && (bitmap_bit_p (current
->split_bbs
,
573 gimple_bb (SSA_NAME_DEF_STMT (retval
))->index
)
574 || gimple_bb (SSA_NAME_DEF_STMT (retval
)) == return_bb
));
575 else if (TREE_CODE (retval
) == PARM_DECL
)
576 current
->split_part_set_retval
= false;
577 else if (TREE_CODE (retval
) == VAR_DECL
578 || TREE_CODE (retval
) == RESULT_DECL
)
579 current
->split_part_set_retval
580 = bitmap_bit_p (non_ssa_vars
, DECL_UID (retval
));
582 current
->split_part_set_retval
= true;
584 /* split_function fixes up at most one PHI non-virtual PHI node in return_bb,
585 for the return value. If there are other PHIs, give up. */
586 if (return_bb
!= EXIT_BLOCK_PTR
)
588 gimple_stmt_iterator psi
;
590 for (psi
= gsi_start_phis (return_bb
); !gsi_end_p (psi
); gsi_next (&psi
))
591 if (!virtual_operand_p (gimple_phi_result (gsi_stmt (psi
)))
593 && current
->split_part_set_retval
594 && TREE_CODE (retval
) == SSA_NAME
595 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
))
596 && SSA_NAME_DEF_STMT (retval
) == gsi_stmt (psi
)))
598 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
600 " Refused: return bb has extra PHIs\n");
605 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
606 fprintf (dump_file
, " Accepted!\n");
608 /* At the moment chose split point with lowest frequency and that leaves
609 out smallest size of header.
610 In future we might re-consider this heuristics. */
611 if (!best_split_point
.split_bbs
612 || best_split_point
.entry_bb
->frequency
> current
->entry_bb
->frequency
613 || (best_split_point
.entry_bb
->frequency
== current
->entry_bb
->frequency
614 && best_split_point
.split_size
< current
->split_size
))
617 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
618 fprintf (dump_file
, " New best split point!\n");
619 if (best_split_point
.ssa_names_to_pass
)
621 BITMAP_FREE (best_split_point
.ssa_names_to_pass
);
622 BITMAP_FREE (best_split_point
.split_bbs
);
624 best_split_point
= *current
;
625 best_split_point
.ssa_names_to_pass
= BITMAP_ALLOC (NULL
);
626 bitmap_copy (best_split_point
.ssa_names_to_pass
,
627 current
->ssa_names_to_pass
);
628 best_split_point
.split_bbs
= BITMAP_ALLOC (NULL
);
629 bitmap_copy (best_split_point
.split_bbs
, current
->split_bbs
);
633 /* Return basic block containing RETURN statement. We allow basic blocks
637 but return_bb can not be more complex than this.
638 If nothing is found, return EXIT_BLOCK_PTR.
640 When there are multiple RETURN statement, chose one with return value,
641 since that one is more likely shared by multiple code paths.
643 Return BB is special, because for function splitting it is the only
644 basic block that is duplicated in between header and split part of the
647 TODO: We might support multiple return blocks. */
650 find_return_bb (void)
653 basic_block return_bb
= EXIT_BLOCK_PTR
;
654 gimple_stmt_iterator bsi
;
655 bool found_return
= false;
656 tree retval
= NULL_TREE
;
658 if (!single_pred_p (EXIT_BLOCK_PTR
))
661 e
= single_pred_edge (EXIT_BLOCK_PTR
);
662 for (bsi
= gsi_last_bb (e
->src
); !gsi_end_p (bsi
); gsi_prev (&bsi
))
664 gimple stmt
= gsi_stmt (bsi
);
665 if (gimple_code (stmt
) == GIMPLE_LABEL
666 || is_gimple_debug (stmt
)
667 || gimple_clobber_p (stmt
))
669 else if (gimple_code (stmt
) == GIMPLE_ASSIGN
671 && gimple_assign_single_p (stmt
)
672 && (auto_var_in_fn_p (gimple_assign_rhs1 (stmt
),
673 current_function_decl
)
674 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
675 && retval
== gimple_assign_lhs (stmt
))
677 else if (gimple_code (stmt
) == GIMPLE_RETURN
)
680 retval
= gimple_return_retval (stmt
);
685 if (gsi_end_p (bsi
) && found_return
)
691 /* Given return basic block RETURN_BB, see where return value is really
694 find_retval (basic_block return_bb
)
696 gimple_stmt_iterator bsi
;
697 for (bsi
= gsi_start_bb (return_bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
698 if (gimple_code (gsi_stmt (bsi
)) == GIMPLE_RETURN
)
699 return gimple_return_retval (gsi_stmt (bsi
));
700 else if (gimple_code (gsi_stmt (bsi
)) == GIMPLE_ASSIGN
701 && !gimple_clobber_p (gsi_stmt (bsi
)))
702 return gimple_assign_rhs1 (gsi_stmt (bsi
));
706 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
707 variable, mark it as used in bitmap passed via DATA.
708 Return true when access to T prevents splitting the function. */
711 mark_nonssa_use (gimple stmt ATTRIBUTE_UNUSED
, tree t
, void *data
)
713 t
= get_base_address (t
);
715 if (!t
|| is_gimple_reg (t
))
718 /* At present we can't pass non-SSA arguments to split function.
719 FIXME: this can be relaxed by passing references to arguments. */
720 if (TREE_CODE (t
) == PARM_DECL
)
722 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
724 "Cannot split: use of non-ssa function parameter.\n");
728 if ((TREE_CODE (t
) == VAR_DECL
729 && auto_var_in_fn_p (t
, current_function_decl
))
730 || TREE_CODE (t
) == RESULT_DECL
731 || TREE_CODE (t
) == LABEL_DECL
)
732 bitmap_set_bit ((bitmap
)data
, DECL_UID (t
));
734 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
735 to pretend that the value pointed to is actual result decl. */
736 if ((TREE_CODE (t
) == MEM_REF
|| INDIRECT_REF_P (t
))
737 && TREE_CODE (TREE_OPERAND (t
, 0)) == SSA_NAME
738 && SSA_NAME_VAR (TREE_OPERAND (t
, 0))
739 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t
, 0))) == RESULT_DECL
740 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
742 bitmap_bit_p ((bitmap
)data
,
743 DECL_UID (DECL_RESULT (current_function_decl
)));
748 /* Compute local properties of basic block BB we collect when looking for
749 split points. We look for ssa defs and store them in SET_SSA_NAMES,
750 for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic
751 vars stored in NON_SSA_VARS.
753 When BB has edge to RETURN_BB, collect uses in RETURN_BB too.
755 Return false when BB contains something that prevents it from being put into
759 visit_bb (basic_block bb
, basic_block return_bb
,
760 bitmap set_ssa_names
, bitmap used_ssa_names
,
763 gimple_stmt_iterator bsi
;
766 bool can_split
= true;
768 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
770 gimple stmt
= gsi_stmt (bsi
);
775 if (is_gimple_debug (stmt
))
778 if (gimple_clobber_p (stmt
))
781 /* FIXME: We can split regions containing EH. We can not however
782 split RESX, EH_DISPATCH and EH_POINTER referring to same region
783 into different partitions. This would require tracking of
784 EH regions and checking in consider_split_point if they
785 are not used elsewhere. */
786 if (gimple_code (stmt
) == GIMPLE_RESX
)
788 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
789 fprintf (dump_file
, "Cannot split: resx.\n");
792 if (gimple_code (stmt
) == GIMPLE_EH_DISPATCH
)
794 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
795 fprintf (dump_file
, "Cannot split: eh dispatch.\n");
799 /* Check builtins that prevent splitting. */
800 if (gimple_code (stmt
) == GIMPLE_CALL
801 && (decl
= gimple_call_fndecl (stmt
)) != NULL_TREE
802 && DECL_BUILT_IN (decl
)
803 && DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
804 switch (DECL_FUNCTION_CODE (decl
))
806 /* FIXME: once we will allow passing non-parm values to split part,
807 we need to be sure to handle correct builtin_stack_save and
808 builtin_stack_restore. At the moment we are safe; there is no
809 way to store builtin_stack_save result in non-SSA variable
810 since all calls to those are compiler generated. */
812 case BUILT_IN_APPLY_ARGS
:
813 case BUILT_IN_VA_START
:
814 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
816 "Cannot split: builtin_apply and va_start.\n");
819 case BUILT_IN_EH_POINTER
:
820 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
821 fprintf (dump_file
, "Cannot split: builtin_eh_pointer.\n");
828 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_DEF
)
829 bitmap_set_bit (set_ssa_names
, SSA_NAME_VERSION (op
));
830 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
831 bitmap_set_bit (used_ssa_names
, SSA_NAME_VERSION (op
));
832 can_split
&= !walk_stmt_load_store_addr_ops (stmt
, non_ssa_vars
,
837 for (bsi
= gsi_start_phis (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
839 gimple stmt
= gsi_stmt (bsi
);
842 if (virtual_operand_p (gimple_phi_result (stmt
)))
844 bitmap_set_bit (set_ssa_names
,
845 SSA_NAME_VERSION (gimple_phi_result (stmt
)));
846 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
848 tree op
= gimple_phi_arg_def (stmt
, i
);
849 if (TREE_CODE (op
) == SSA_NAME
)
850 bitmap_set_bit (used_ssa_names
, SSA_NAME_VERSION (op
));
852 can_split
&= !walk_stmt_load_store_addr_ops (stmt
, non_ssa_vars
,
857 /* Record also uses coming from PHI operand in return BB. */
858 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
859 if (e
->dest
== return_bb
)
861 for (bsi
= gsi_start_phis (return_bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
863 gimple stmt
= gsi_stmt (bsi
);
864 tree op
= gimple_phi_arg_def (stmt
, e
->dest_idx
);
866 if (virtual_operand_p (gimple_phi_result (stmt
)))
868 if (TREE_CODE (op
) == SSA_NAME
)
869 bitmap_set_bit (used_ssa_names
, SSA_NAME_VERSION (op
));
871 can_split
&= !mark_nonssa_use (stmt
, op
, non_ssa_vars
);
877 /* Stack entry for recursive DFS walk in find_split_point. */
881 /* Basic block we are examining. */
884 /* SSA names set and used by the BB and all BBs reachable
885 from it via DFS walk. */
886 bitmap set_ssa_names
, used_ssa_names
;
889 /* All BBS visited from this BB via DFS walk. */
892 /* Last examined edge in DFS walk. Since we walk unoriented graph,
893 the value is up to sum of incoming and outgoing edges of BB. */
894 unsigned int edge_num
;
896 /* Stack entry index of earliest BB reachable from current BB
897 or any BB visited later in DFS walk. */
900 /* Overall time and size of all BBs reached from this BB in DFS walk. */
901 int overall_time
, overall_size
;
903 /* When false we can not split on this BB. */
908 /* Find all articulations and call consider_split on them.
909 OVERALL_TIME and OVERALL_SIZE is time and size of the function.
911 We perform basic algorithm for finding an articulation in a graph
912 created from CFG by considering it to be an unoriented graph.
914 The articulation is discovered via DFS walk. We collect earliest
915 basic block on stack that is reachable via backward edge. Articulation
916 is any basic block such that there is no backward edge bypassing it.
917 To reduce stack usage we maintain heap allocated stack in STACK vector.
918 AUX pointer of BB is set to index it appears in the stack or -1 once
919 it is visited and popped off the stack.
921 The algorithm finds articulation after visiting the whole component
922 reachable by it. This makes it convenient to collect information about
923 the component used by consider_split. */
926 find_split_points (int overall_time
, int overall_size
)
929 vec
<stack_entry
> stack
= vNULL
;
931 basic_block return_bb
= find_return_bb ();
932 struct split_point current
;
934 current
.header_time
= overall_time
;
935 current
.header_size
= overall_size
;
936 current
.split_time
= 0;
937 current
.split_size
= 0;
938 current
.ssa_names_to_pass
= BITMAP_ALLOC (NULL
);
940 first
.bb
= ENTRY_BLOCK_PTR
;
942 first
.overall_time
= 0;
943 first
.overall_size
= 0;
944 first
.earliest
= INT_MAX
;
945 first
.set_ssa_names
= 0;
946 first
.used_ssa_names
= 0;
947 first
.bbs_visited
= 0;
948 stack
.safe_push (first
);
949 ENTRY_BLOCK_PTR
->aux
= (void *)(intptr_t)-1;
951 while (!stack
.is_empty ())
953 stack_entry
*entry
= &stack
.last ();
955 /* We are walking an acyclic graph, so edge_num counts
956 succ and pred edges together. However when considering
957 articulation, we want to have processed everything reachable
958 from articulation but nothing that reaches into it. */
959 if (entry
->edge_num
== EDGE_COUNT (entry
->bb
->succs
)
960 && entry
->bb
!= ENTRY_BLOCK_PTR
)
962 int pos
= stack
.length ();
963 entry
->can_split
&= visit_bb (entry
->bb
, return_bb
,
964 entry
->set_ssa_names
,
965 entry
->used_ssa_names
,
966 entry
->non_ssa_vars
);
967 if (pos
<= entry
->earliest
&& !entry
->can_split
968 && dump_file
&& (dump_flags
& TDF_DETAILS
))
970 "found articulation at bb %i but can not split\n",
972 if (pos
<= entry
->earliest
&& entry
->can_split
)
974 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
975 fprintf (dump_file
, "found articulation at bb %i\n",
977 current
.entry_bb
= entry
->bb
;
978 current
.ssa_names_to_pass
= BITMAP_ALLOC (NULL
);
979 bitmap_and_compl (current
.ssa_names_to_pass
,
980 entry
->used_ssa_names
, entry
->set_ssa_names
);
981 current
.header_time
= overall_time
- entry
->overall_time
;
982 current
.header_size
= overall_size
- entry
->overall_size
;
983 current
.split_time
= entry
->overall_time
;
984 current
.split_size
= entry
->overall_size
;
985 current
.split_bbs
= entry
->bbs_visited
;
986 consider_split (¤t
, entry
->non_ssa_vars
, return_bb
);
987 BITMAP_FREE (current
.ssa_names_to_pass
);
990 /* Do actual DFS walk. */
992 < (EDGE_COUNT (entry
->bb
->succs
)
993 + EDGE_COUNT (entry
->bb
->preds
)))
997 if (entry
->edge_num
< EDGE_COUNT (entry
->bb
->succs
))
999 e
= EDGE_SUCC (entry
->bb
, entry
->edge_num
);
1004 e
= EDGE_PRED (entry
->bb
, entry
->edge_num
1005 - EDGE_COUNT (entry
->bb
->succs
));
1011 /* New BB to visit, push it to the stack. */
1012 if (dest
!= return_bb
&& dest
!= EXIT_BLOCK_PTR
1015 stack_entry new_entry
;
1017 new_entry
.bb
= dest
;
1018 new_entry
.edge_num
= 0;
1019 new_entry
.overall_time
1020 = bb_info_vec
[dest
->index
].time
;
1021 new_entry
.overall_size
1022 = bb_info_vec
[dest
->index
].size
;
1023 new_entry
.earliest
= INT_MAX
;
1024 new_entry
.set_ssa_names
= BITMAP_ALLOC (NULL
);
1025 new_entry
.used_ssa_names
= BITMAP_ALLOC (NULL
);
1026 new_entry
.bbs_visited
= BITMAP_ALLOC (NULL
);
1027 new_entry
.non_ssa_vars
= BITMAP_ALLOC (NULL
);
1028 new_entry
.can_split
= true;
1029 bitmap_set_bit (new_entry
.bbs_visited
, dest
->index
);
1030 stack
.safe_push (new_entry
);
1031 dest
->aux
= (void *)(intptr_t)stack
.length ();
1033 /* Back edge found, record the earliest point. */
1034 else if ((intptr_t)dest
->aux
> 0
1035 && (intptr_t)dest
->aux
< entry
->earliest
)
1036 entry
->earliest
= (intptr_t)dest
->aux
;
1038 /* We are done with examining the edges. Pop off the value from stack
1039 and merge stuff we accumulate during the walk. */
1040 else if (entry
->bb
!= ENTRY_BLOCK_PTR
)
1042 stack_entry
*prev
= &stack
[stack
.length () - 2];
1044 entry
->bb
->aux
= (void *)(intptr_t)-1;
1045 prev
->can_split
&= entry
->can_split
;
1046 if (prev
->set_ssa_names
)
1048 bitmap_ior_into (prev
->set_ssa_names
, entry
->set_ssa_names
);
1049 bitmap_ior_into (prev
->used_ssa_names
, entry
->used_ssa_names
);
1050 bitmap_ior_into (prev
->bbs_visited
, entry
->bbs_visited
);
1051 bitmap_ior_into (prev
->non_ssa_vars
, entry
->non_ssa_vars
);
1053 if (prev
->earliest
> entry
->earliest
)
1054 prev
->earliest
= entry
->earliest
;
1055 prev
->overall_time
+= entry
->overall_time
;
1056 prev
->overall_size
+= entry
->overall_size
;
1057 BITMAP_FREE (entry
->set_ssa_names
);
1058 BITMAP_FREE (entry
->used_ssa_names
);
1059 BITMAP_FREE (entry
->bbs_visited
);
1060 BITMAP_FREE (entry
->non_ssa_vars
);
1066 ENTRY_BLOCK_PTR
->aux
= NULL
;
1070 BITMAP_FREE (current
.ssa_names_to_pass
);
1073 /* Split function at SPLIT_POINT. */
1076 split_function (struct split_point
*split_point
)
1078 vec
<tree
> args_to_pass
= vNULL
;
1079 bitmap args_to_skip
;
1082 struct cgraph_node
*node
, *cur_node
= cgraph_get_node (current_function_decl
);
1083 basic_block return_bb
= find_return_bb ();
1084 basic_block call_bb
;
1085 gimple_stmt_iterator gsi
;
1089 tree retval
= NULL
, real_retval
= NULL
;
1090 bool split_part_return_p
= false;
1091 gimple last_stmt
= NULL
;
1094 vec
<tree
, va_gc
> **debug_args
= NULL
;
1098 fprintf (dump_file
, "\n\nSplitting function at:\n");
1099 dump_split_point (dump_file
, split_point
);
1102 if (cur_node
->local
.can_change_signature
)
1103 args_to_skip
= BITMAP_ALLOC (NULL
);
1105 args_to_skip
= NULL
;
1107 /* Collect the parameters of new function and args_to_skip bitmap. */
1108 for (parm
= DECL_ARGUMENTS (current_function_decl
);
1109 parm
; parm
= DECL_CHAIN (parm
), num
++)
1111 && (!is_gimple_reg (parm
)
1112 || (ddef
= ssa_default_def (cfun
, parm
)) == NULL_TREE
1113 || !bitmap_bit_p (split_point
->ssa_names_to_pass
,
1114 SSA_NAME_VERSION (ddef
))))
1115 bitmap_set_bit (args_to_skip
, num
);
1118 /* This parm might not have been used up to now, but is going to be
1119 used, hence register it. */
1120 if (is_gimple_reg (parm
))
1121 arg
= get_or_create_ssa_default_def (cfun
, parm
);
1125 if (!useless_type_conversion_p (DECL_ARG_TYPE (parm
), TREE_TYPE (arg
)))
1126 arg
= fold_convert (DECL_ARG_TYPE (parm
), arg
);
1127 args_to_pass
.safe_push (arg
);
1130 /* See if the split function will return. */
1131 FOR_EACH_EDGE (e
, ei
, return_bb
->preds
)
1132 if (bitmap_bit_p (split_point
->split_bbs
, e
->src
->index
))
1135 split_part_return_p
= true;
1137 /* Add return block to what will become the split function.
1138 We do not return; no return block is needed. */
1139 if (!split_part_return_p
)
1141 /* We have no return block, so nothing is needed. */
1142 else if (return_bb
== EXIT_BLOCK_PTR
)
1144 /* When we do not want to return value, we need to construct
1145 new return block with empty return statement.
1146 FIXME: Once we are able to change return type, we should change function
1147 to return void instead of just outputting function with undefined return
1148 value. For structures this affects quality of codegen. */
1149 else if (!split_point
->split_part_set_retval
1150 && find_retval (return_bb
))
1152 bool redirected
= true;
1153 basic_block new_return_bb
= create_basic_block (NULL
, 0, return_bb
);
1154 gimple_stmt_iterator gsi
= gsi_start_bb (new_return_bb
);
1155 gsi_insert_after (&gsi
, gimple_build_return (NULL
), GSI_NEW_STMT
);
1159 FOR_EACH_EDGE (e
, ei
, return_bb
->preds
)
1160 if (bitmap_bit_p (split_point
->split_bbs
, e
->src
->index
))
1162 new_return_bb
->count
+= e
->count
;
1163 new_return_bb
->frequency
+= EDGE_FREQUENCY (e
);
1164 redirect_edge_and_branch (e
, new_return_bb
);
1169 e
= make_edge (new_return_bb
, EXIT_BLOCK_PTR
, 0);
1170 e
->probability
= REG_BR_PROB_BASE
;
1171 e
->count
= new_return_bb
->count
;
1173 add_bb_to_loop (new_return_bb
, current_loops
->tree_root
);
1174 bitmap_set_bit (split_point
->split_bbs
, new_return_bb
->index
);
1176 /* When we pass around the value, use existing return block. */
1178 bitmap_set_bit (split_point
->split_bbs
, return_bb
->index
);
1180 /* If RETURN_BB has virtual operand PHIs, they must be removed and the
1181 virtual operand marked for renaming as we change the CFG in a way that
1182 tree-inline is not able to compensate for.
1184 Note this can happen whether or not we have a return value. If we have
1185 a return value, then RETURN_BB may have PHIs for real operands too. */
1186 if (return_bb
!= EXIT_BLOCK_PTR
)
1189 for (gsi
= gsi_start_phis (return_bb
); !gsi_end_p (gsi
);)
1191 gimple stmt
= gsi_stmt (gsi
);
1192 if (!virtual_operand_p (gimple_phi_result (stmt
)))
1197 mark_virtual_phi_result_for_renaming (stmt
);
1198 remove_phi_node (&gsi
, true);
1201 /* In reality we have to rename the reaching definition of the
1202 virtual operand at return_bb as we will eventually release it
1203 when we remove the code region we outlined.
1204 So we have to rename all immediate virtual uses of that region
1205 if we didn't see a PHI definition yet. */
1206 /* ??? In real reality we want to set the reaching vdef of the
1207 entry of the SESE region as the vuse of the call and the reaching
1208 vdef of the exit of the SESE region as the vdef of the call. */
1210 for (gsi
= gsi_start_bb (return_bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1212 gimple stmt
= gsi_stmt (gsi
);
1213 if (gimple_vuse (stmt
))
1215 gimple_set_vuse (stmt
, NULL_TREE
);
1218 if (gimple_vdef (stmt
))
1223 /* Now create the actual clone. */
1224 rebuild_cgraph_edges ();
1225 node
= cgraph_function_versioning (cur_node
, vNULL
,
1228 !split_part_return_p
,
1229 split_point
->split_bbs
,
1230 split_point
->entry_bb
, "part");
1231 /* For usual cloning it is enough to clear builtin only when signature
1232 changes. For partial inlining we however can not expect the part
1233 of builtin implementation to have same semantic as the whole. */
1234 if (DECL_BUILT_IN (node
->decl
))
1236 DECL_BUILT_IN_CLASS (node
->decl
) = NOT_BUILT_IN
;
1237 DECL_FUNCTION_CODE (node
->decl
) = (enum built_in_function
) 0;
1239 /* If the original function is declared inline, there is no point in issuing
1240 a warning for the non-inlinable part. */
1241 DECL_NO_INLINE_WARNING_P (node
->decl
) = 1;
1242 cgraph_node_remove_callees (cur_node
);
1243 ipa_remove_all_references (&cur_node
->ref_list
);
1244 if (!split_part_return_p
)
1245 TREE_THIS_VOLATILE (node
->decl
) = 1;
1247 dump_function_to_file (node
->decl
, dump_file
, dump_flags
);
1249 /* Create the basic block we place call into. It is the entry basic block
1250 split after last label. */
1251 call_bb
= split_point
->entry_bb
;
1252 for (gsi
= gsi_start_bb (call_bb
); !gsi_end_p (gsi
);)
1253 if (gimple_code (gsi_stmt (gsi
)) == GIMPLE_LABEL
)
1255 last_stmt
= gsi_stmt (gsi
);
1260 e
= split_block (split_point
->entry_bb
, last_stmt
);
1263 /* Produce the call statement. */
1264 gsi
= gsi_last_bb (call_bb
);
1265 FOR_EACH_VEC_ELT (args_to_pass
, i
, arg
)
1266 if (!is_gimple_val (arg
))
1268 arg
= force_gimple_operand_gsi (&gsi
, arg
, true, NULL_TREE
,
1269 false, GSI_CONTINUE_LINKING
);
1270 args_to_pass
[i
] = arg
;
1272 call
= gimple_build_call_vec (node
->decl
, args_to_pass
);
1273 gimple_set_block (call
, DECL_INITIAL (current_function_decl
));
1274 args_to_pass
.release ();
1276 /* For optimized away parameters, add on the caller side
1278 DEBUG D#X => parm_Y(D)
1279 stmts and associate D#X with parm in decl_debug_args_lookup
1280 vector to say for debug info that if parameter parm had been passed,
1281 it would have value parm_Y(D). */
1283 for (parm
= DECL_ARGUMENTS (current_function_decl
), num
= 0;
1284 parm
; parm
= DECL_CHAIN (parm
), num
++)
1285 if (bitmap_bit_p (args_to_skip
, num
)
1286 && is_gimple_reg (parm
))
1291 /* This needs to be done even without MAY_HAVE_DEBUG_STMTS,
1292 otherwise if it didn't exist before, we'd end up with
1293 different SSA_NAME_VERSIONs between -g and -g0. */
1294 arg
= get_or_create_ssa_default_def (cfun
, parm
);
1295 if (!MAY_HAVE_DEBUG_STMTS
)
1298 if (debug_args
== NULL
)
1299 debug_args
= decl_debug_args_insert (node
->decl
);
1300 ddecl
= make_node (DEBUG_EXPR_DECL
);
1301 DECL_ARTIFICIAL (ddecl
) = 1;
1302 TREE_TYPE (ddecl
) = TREE_TYPE (parm
);
1303 DECL_MODE (ddecl
) = DECL_MODE (parm
);
1304 vec_safe_push (*debug_args
, DECL_ORIGIN (parm
));
1305 vec_safe_push (*debug_args
, ddecl
);
1306 def_temp
= gimple_build_debug_bind (ddecl
, unshare_expr (arg
),
1308 gsi_insert_after (&gsi
, def_temp
, GSI_NEW_STMT
);
1310 /* And on the callee side, add
1313 stmts to the first bb where var is a VAR_DECL created for the
1314 optimized away parameter in DECL_INITIAL block. This hints
1315 in the debug info that var (whole DECL_ORIGIN is the parm PARM_DECL)
1316 is optimized away, but could be looked up at the call site
1317 as value of D#X there. */
1318 if (debug_args
!= NULL
)
1322 gimple_stmt_iterator cgsi
;
1325 push_cfun (DECL_STRUCT_FUNCTION (node
->decl
));
1326 var
= BLOCK_VARS (DECL_INITIAL (node
->decl
));
1327 i
= vec_safe_length (*debug_args
);
1328 cgsi
= gsi_after_labels (single_succ (ENTRY_BLOCK_PTR
));
1332 while (var
!= NULL_TREE
1333 && DECL_ABSTRACT_ORIGIN (var
) != (**debug_args
)[i
])
1334 var
= TREE_CHAIN (var
);
1335 if (var
== NULL_TREE
)
1337 vexpr
= make_node (DEBUG_EXPR_DECL
);
1338 parm
= (**debug_args
)[i
];
1339 DECL_ARTIFICIAL (vexpr
) = 1;
1340 TREE_TYPE (vexpr
) = TREE_TYPE (parm
);
1341 DECL_MODE (vexpr
) = DECL_MODE (parm
);
1342 def_temp
= gimple_build_debug_source_bind (vexpr
, parm
,
1344 gsi_insert_before (&cgsi
, def_temp
, GSI_SAME_STMT
);
1345 def_temp
= gimple_build_debug_bind (var
, vexpr
, NULL
);
1346 gsi_insert_before (&cgsi
, def_temp
, GSI_SAME_STMT
);
1352 /* We avoid address being taken on any variable used by split part,
1353 so return slot optimization is always possible. Moreover this is
1354 required to make DECL_BY_REFERENCE work. */
1355 if (aggregate_value_p (DECL_RESULT (current_function_decl
),
1356 TREE_TYPE (current_function_decl
))
1357 && (!is_gimple_reg_type (TREE_TYPE (DECL_RESULT (current_function_decl
)))
1358 || DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
))))
1359 gimple_call_set_return_slot_opt (call
, true);
1361 /* Update return value. This is bit tricky. When we do not return,
1362 do nothing. When we return we might need to update return_bb
1363 or produce a new return statement. */
1364 if (!split_part_return_p
)
1365 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1368 e
= make_edge (call_bb
, return_bb
,
1369 return_bb
== EXIT_BLOCK_PTR
? 0 : EDGE_FALLTHRU
);
1370 e
->count
= call_bb
->count
;
1371 e
->probability
= REG_BR_PROB_BASE
;
1373 /* If there is return basic block, see what value we need to store
1374 return value into and put call just before it. */
1375 if (return_bb
!= EXIT_BLOCK_PTR
)
1377 real_retval
= retval
= find_retval (return_bb
);
1379 if (real_retval
&& split_point
->split_part_set_retval
)
1381 gimple_stmt_iterator psi
;
1383 /* See if we need new SSA_NAME for the result.
1384 When DECL_BY_REFERENCE is true, retval is actually pointer to
1385 return value and it is constant in whole function. */
1386 if (TREE_CODE (retval
) == SSA_NAME
1387 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
1389 retval
= copy_ssa_name (retval
, call
);
1391 /* See if there is PHI defining return value. */
1392 for (psi
= gsi_start_phis (return_bb
);
1393 !gsi_end_p (psi
); gsi_next (&psi
))
1394 if (!virtual_operand_p (gimple_phi_result (gsi_stmt (psi
))))
1397 /* When there is PHI, just update its value. */
1398 if (TREE_CODE (retval
) == SSA_NAME
1399 && !gsi_end_p (psi
))
1400 add_phi_arg (gsi_stmt (psi
), retval
, e
, UNKNOWN_LOCATION
);
1401 /* Otherwise update the return BB itself.
1402 find_return_bb allows at most one assignment to return value,
1403 so update first statement. */
1406 gimple_stmt_iterator bsi
;
1407 for (bsi
= gsi_start_bb (return_bb
); !gsi_end_p (bsi
);
1409 if (gimple_code (gsi_stmt (bsi
)) == GIMPLE_RETURN
)
1411 gimple_return_set_retval (gsi_stmt (bsi
), retval
);
1414 else if (gimple_code (gsi_stmt (bsi
)) == GIMPLE_ASSIGN
1415 && !gimple_clobber_p (gsi_stmt (bsi
)))
1417 gimple_assign_set_rhs1 (gsi_stmt (bsi
), retval
);
1420 update_stmt (gsi_stmt (bsi
));
1423 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
1425 gimple_call_set_lhs (call
, build_simple_mem_ref (retval
));
1426 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1431 restype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
1432 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1433 if (!useless_type_conversion_p (TREE_TYPE (retval
), restype
))
1436 tree tem
= create_tmp_reg (restype
, NULL
);
1437 tem
= make_ssa_name (tem
, call
);
1438 cpy
= gimple_build_assign_with_ops (NOP_EXPR
, retval
,
1440 gsi_insert_after (&gsi
, cpy
, GSI_NEW_STMT
);
1443 gimple_call_set_lhs (call
, retval
);
1448 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1450 /* We don't use return block (there is either no return in function or
1451 multiple of them). So create new basic block with return statement.
1456 if (split_point
->split_part_set_retval
1457 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl
))))
1459 retval
= DECL_RESULT (current_function_decl
);
1461 /* We use temporary register to hold value when aggregate_value_p
1462 is false. Similarly for DECL_BY_REFERENCE we must avoid extra
1464 if (!aggregate_value_p (retval
, TREE_TYPE (current_function_decl
))
1465 && !DECL_BY_REFERENCE (retval
))
1466 retval
= create_tmp_reg (TREE_TYPE (retval
), NULL
);
1467 if (is_gimple_reg (retval
))
1469 /* When returning by reference, there is only one SSA name
1470 assigned to RESULT_DECL (that is pointer to return value).
1471 Look it up or create new one if it is missing. */
1472 if (DECL_BY_REFERENCE (retval
))
1473 retval
= get_or_create_ssa_default_def (cfun
, retval
);
1474 /* Otherwise produce new SSA name for return value. */
1476 retval
= make_ssa_name (retval
, call
);
1478 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
1479 gimple_call_set_lhs (call
, build_simple_mem_ref (retval
));
1481 gimple_call_set_lhs (call
, retval
);
1483 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1484 ret
= gimple_build_return (retval
);
1485 gsi_insert_after (&gsi
, ret
, GSI_NEW_STMT
);
1488 free_dominance_info (CDI_DOMINATORS
);
1489 free_dominance_info (CDI_POST_DOMINATORS
);
1490 compute_inline_parameters (node
, true);
1493 /* Execute function splitting pass. */
1496 execute_split_functions (void)
1498 gimple_stmt_iterator bsi
;
1500 int overall_time
= 0, overall_size
= 0;
1502 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
1504 if (flags_from_decl_or_type (current_function_decl
)
1505 & (ECF_NORETURN
|ECF_MALLOC
))
1508 fprintf (dump_file
, "Not splitting: noreturn/malloc function.\n");
1511 if (MAIN_NAME_P (DECL_NAME (current_function_decl
)))
1514 fprintf (dump_file
, "Not splitting: main function.\n");
1517 /* This can be relaxed; function might become inlinable after splitting
1518 away the uninlinable part. */
1519 if (inline_edge_summary_vec
.exists ()
1520 && !inline_summary (node
)->inlinable
)
1523 fprintf (dump_file
, "Not splitting: not inlinable.\n");
1526 if (DECL_DISREGARD_INLINE_LIMITS (node
->decl
))
1529 fprintf (dump_file
, "Not splitting: disregarding inline limits.\n");
1532 /* This can be relaxed; most of versioning tests actually prevents
1534 if (!tree_versionable_function_p (current_function_decl
))
1537 fprintf (dump_file
, "Not splitting: not versionable.\n");
1540 /* FIXME: we could support this. */
1541 if (DECL_STRUCT_FUNCTION (current_function_decl
)->static_chain_decl
)
1544 fprintf (dump_file
, "Not splitting: nested function.\n");
1548 /* See if it makes sense to try to split.
1549 It makes sense to split if we inline, that is if we have direct calls to
1550 handle or direct calls are possibly going to appear as result of indirect
1551 inlining or LTO. Also handle -fprofile-generate as LTO to allow non-LTO
1552 training for LTO -fprofile-use build.
1554 Note that we are not completely conservative about disqualifying functions
1555 called once. It is possible that the caller is called more then once and
1556 then inlining would still benefit. */
1558 /* Local functions called once will be completely inlined most of time. */
1559 || (!node
->callers
->next_caller
&& node
->local
.local
))
1560 && !node
->address_taken
1561 && (!flag_lto
|| !node
->externally_visible
))
1564 fprintf (dump_file
, "Not splitting: not called directly "
1565 "or called once.\n");
1569 /* FIXME: We can actually split if splitting reduces call overhead. */
1570 if (!flag_inline_small_functions
1571 && !DECL_DECLARED_INLINE_P (current_function_decl
))
1574 fprintf (dump_file
, "Not splitting: not autoinlining and function"
1575 " is not inline.\n");
1579 /* We enforce splitting after loop headers when profile info is not
1581 if (profile_status
!= PROFILE_READ
)
1582 mark_dfs_back_edges ();
1584 /* Initialize bitmap to track forbidden calls. */
1585 forbidden_dominators
= BITMAP_ALLOC (NULL
);
1586 calculate_dominance_info (CDI_DOMINATORS
);
1588 /* Compute local info about basic blocks and determine function size/time. */
1589 bb_info_vec
.safe_grow_cleared (last_basic_block
+ 1);
1590 memset (&best_split_point
, 0, sizeof (best_split_point
));
1595 int freq
= compute_call_stmt_bb_frequency (current_function_decl
, bb
);
1597 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1598 fprintf (dump_file
, "Basic block %i\n", bb
->index
);
1600 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
1602 int this_time
, this_size
;
1603 gimple stmt
= gsi_stmt (bsi
);
1605 this_size
= estimate_num_insns (stmt
, &eni_size_weights
);
1606 this_time
= estimate_num_insns (stmt
, &eni_time_weights
) * freq
;
1609 check_forbidden_calls (stmt
);
1611 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1613 fprintf (dump_file
, " freq:%6i size:%3i time:%3i ",
1614 freq
, this_size
, this_time
);
1615 print_gimple_stmt (dump_file
, stmt
, 0, 0);
1618 overall_time
+= time
;
1619 overall_size
+= size
;
1620 bb_info_vec
[bb
->index
].time
= time
;
1621 bb_info_vec
[bb
->index
].size
= size
;
1623 find_split_points (overall_time
, overall_size
);
1624 if (best_split_point
.split_bbs
)
1626 split_function (&best_split_point
);
1627 BITMAP_FREE (best_split_point
.ssa_names_to_pass
);
1628 BITMAP_FREE (best_split_point
.split_bbs
);
1629 todo
= TODO_update_ssa
| TODO_cleanup_cfg
;
1631 BITMAP_FREE (forbidden_dominators
);
1632 bb_info_vec
.release ();
1636 /* Gate function splitting pass. When doing profile feedback, we want
1637 to execute the pass after profiling is read. So disable one in
1638 early optimization. */
1641 gate_split_functions (void)
1643 return (flag_partial_inlining
1644 && !profile_arc_flag
&& !flag_branch_probabilities
);
1649 const pass_data pass_data_split_functions
=
1651 GIMPLE_PASS
, /* type */
1652 "fnsplit", /* name */
1653 OPTGROUP_NONE
, /* optinfo_flags */
1654 true, /* has_gate */
1655 true, /* has_execute */
1656 TV_IPA_FNSPLIT
, /* tv_id */
1657 PROP_cfg
, /* properties_required */
1658 0, /* properties_provided */
1659 0, /* properties_destroyed */
1660 0, /* todo_flags_start */
1661 TODO_verify_all
, /* todo_flags_finish */
1664 class pass_split_functions
: public gimple_opt_pass
1667 pass_split_functions (gcc::context
*ctxt
)
1668 : gimple_opt_pass (pass_data_split_functions
, ctxt
)
1671 /* opt_pass methods: */
1672 bool gate () { return gate_split_functions (); }
1673 unsigned int execute () { return execute_split_functions (); }
1675 }; // class pass_split_functions
1680 make_pass_split_functions (gcc::context
*ctxt
)
1682 return new pass_split_functions (ctxt
);
1685 /* Gate feedback driven function splitting pass.
1686 We don't need to split when profiling at all, we are producing
1687 lousy code anyway. */
1690 gate_feedback_split_functions (void)
1692 return (flag_partial_inlining
1693 && flag_branch_probabilities
);
1696 /* Execute function splitting pass. */
1699 execute_feedback_split_functions (void)
1701 unsigned int retval
= execute_split_functions ();
1703 retval
|= TODO_rebuild_cgraph_edges
;
1709 const pass_data pass_data_feedback_split_functions
=
1711 GIMPLE_PASS
, /* type */
1712 "feedback_fnsplit", /* name */
1713 OPTGROUP_NONE
, /* optinfo_flags */
1714 true, /* has_gate */
1715 true, /* has_execute */
1716 TV_IPA_FNSPLIT
, /* tv_id */
1717 PROP_cfg
, /* properties_required */
1718 0, /* properties_provided */
1719 0, /* properties_destroyed */
1720 0, /* todo_flags_start */
1721 TODO_verify_all
, /* todo_flags_finish */
1724 class pass_feedback_split_functions
: public gimple_opt_pass
1727 pass_feedback_split_functions (gcc::context
*ctxt
)
1728 : gimple_opt_pass (pass_data_feedback_split_functions
, ctxt
)
1731 /* opt_pass methods: */
1732 bool gate () { return gate_feedback_split_functions (); }
1733 unsigned int execute () { return execute_feedback_split_functions (); }
1735 }; // class pass_feedback_split_functions
1740 make_pass_feedback_split_functions (gcc::context
*ctxt
)
1742 return new pass_feedback_split_functions (ctxt
);