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"
81 #include "basic-block.h"
82 #include "tree-ssa-alias.h"
83 #include "internal-fn.h"
84 #include "gimple-expr.h"
87 #include "stringpool.h"
91 #include "gimple-iterator.h"
92 #include "gimplify-me.h"
93 #include "gimple-walk.h"
96 #include "gimple-ssa.h"
98 #include "tree-phinodes.h"
99 #include "ssa-iterators.h"
100 #include "stringpool.h"
101 #include "tree-ssanames.h"
102 #include "tree-into-ssa.h"
103 #include "tree-dfa.h"
104 #include "tree-pass.h"
106 #include "diagnostic.h"
107 #include "tree-dump.h"
108 #include "tree-inline.h"
110 #include "gimple-pretty-print.h"
111 #include "ipa-inline.h"
114 /* Per basic block info. */
122 static vec
<bb_info
> bb_info_vec
;
124 /* Description of split point. */
128 /* Size of the partitions. */
129 unsigned int header_time
, header_size
, split_time
, split_size
;
131 /* SSA names that need to be passed into spit function. */
132 bitmap ssa_names_to_pass
;
134 /* Basic block where we split (that will become entry point of new function. */
135 basic_block entry_bb
;
137 /* Basic blocks we are splitting away. */
140 /* True when return value is computed on split part and thus it needs
142 bool split_part_set_retval
;
145 /* Best split point found. */
147 struct split_point best_split_point
;
149 /* Set of basic blocks that are not allowed to dominate a split point. */
151 static bitmap forbidden_dominators
;
153 static tree
find_retval (basic_block return_bb
);
155 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
156 variable, check it if it is present in bitmap passed via DATA. */
159 test_nonssa_use (gimple stmt ATTRIBUTE_UNUSED
, tree t
, void *data
)
161 t
= get_base_address (t
);
163 if (!t
|| is_gimple_reg (t
))
166 if (TREE_CODE (t
) == PARM_DECL
167 || (TREE_CODE (t
) == VAR_DECL
168 && auto_var_in_fn_p (t
, current_function_decl
))
169 || TREE_CODE (t
) == RESULT_DECL
170 || TREE_CODE (t
) == LABEL_DECL
)
171 return bitmap_bit_p ((bitmap
)data
, DECL_UID (t
));
173 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
174 to pretend that the value pointed to is actual result decl. */
175 if ((TREE_CODE (t
) == MEM_REF
|| INDIRECT_REF_P (t
))
176 && TREE_CODE (TREE_OPERAND (t
, 0)) == SSA_NAME
177 && SSA_NAME_VAR (TREE_OPERAND (t
, 0))
178 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t
, 0))) == RESULT_DECL
179 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
181 bitmap_bit_p ((bitmap
)data
,
182 DECL_UID (DECL_RESULT (current_function_decl
)));
187 /* Dump split point CURRENT. */
190 dump_split_point (FILE * file
, struct split_point
*current
)
193 "Split point at BB %i\n"
194 " header time: %i header size: %i\n"
195 " split time: %i split size: %i\n bbs: ",
196 current
->entry_bb
->index
, current
->header_time
,
197 current
->header_size
, current
->split_time
, current
->split_size
);
198 dump_bitmap (file
, current
->split_bbs
);
199 fprintf (file
, " SSA names to pass: ");
200 dump_bitmap (file
, current
->ssa_names_to_pass
);
203 /* Look for all BBs in header that might lead to the split part and verify
204 that they are not defining any non-SSA var used by the split part.
205 Parameters are the same as for consider_split. */
208 verify_non_ssa_vars (struct split_point
*current
, bitmap non_ssa_vars
,
209 basic_block return_bb
)
211 bitmap seen
= BITMAP_ALLOC (NULL
);
212 vec
<basic_block
> worklist
= vNULL
;
217 FOR_EACH_EDGE (e
, ei
, current
->entry_bb
->preds
)
218 if (e
->src
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
)
219 && !bitmap_bit_p (current
->split_bbs
, e
->src
->index
))
221 worklist
.safe_push (e
->src
);
222 bitmap_set_bit (seen
, e
->src
->index
);
225 while (!worklist
.is_empty ())
227 gimple_stmt_iterator bsi
;
228 basic_block bb
= worklist
.pop ();
230 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
231 if (e
->src
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
)
232 && bitmap_set_bit (seen
, e
->src
->index
))
234 gcc_checking_assert (!bitmap_bit_p (current
->split_bbs
,
236 worklist
.safe_push (e
->src
);
238 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
240 gimple stmt
= gsi_stmt (bsi
);
241 if (is_gimple_debug (stmt
))
243 if (walk_stmt_load_store_addr_ops
244 (stmt
, non_ssa_vars
, test_nonssa_use
, test_nonssa_use
,
250 if (gimple_code (stmt
) == GIMPLE_LABEL
251 && test_nonssa_use (stmt
, gimple_label_label (stmt
),
258 for (bsi
= gsi_start_phis (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
260 if (walk_stmt_load_store_addr_ops
261 (gsi_stmt (bsi
), non_ssa_vars
, test_nonssa_use
, test_nonssa_use
,
268 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
270 if (e
->dest
!= return_bb
)
272 for (bsi
= gsi_start_phis (return_bb
); !gsi_end_p (bsi
);
275 gimple stmt
= gsi_stmt (bsi
);
276 tree op
= gimple_phi_arg_def (stmt
, e
->dest_idx
);
278 if (virtual_operand_p (gimple_phi_result (stmt
)))
280 if (TREE_CODE (op
) != SSA_NAME
281 && test_nonssa_use (stmt
, op
, non_ssa_vars
))
295 /* If STMT is a call, check the callee against a list of forbidden
296 predicate functions. If a match is found, look for uses of the
297 call result in condition statements that compare against zero.
298 For each such use, find the block targeted by the condition
299 statement for the nonzero result, and set the bit for this block
300 in the forbidden dominators bitmap. The purpose of this is to avoid
301 selecting a split point where we are likely to lose the chance
302 to optimize away an unused function call. */
305 check_forbidden_calls (gimple stmt
)
307 imm_use_iterator use_iter
;
311 /* At the moment, __builtin_constant_p is the only forbidden
312 predicate function call (see PR49642). */
313 if (!gimple_call_builtin_p (stmt
, BUILT_IN_CONSTANT_P
))
316 lhs
= gimple_call_lhs (stmt
);
318 if (!lhs
|| TREE_CODE (lhs
) != SSA_NAME
)
321 FOR_EACH_IMM_USE_FAST (use_p
, use_iter
, lhs
)
324 basic_block use_bb
, forbidden_bb
;
326 edge true_edge
, false_edge
;
327 gimple use_stmt
= USE_STMT (use_p
);
329 if (gimple_code (use_stmt
) != GIMPLE_COND
)
332 /* Assuming canonical form for GIMPLE_COND here, with constant
333 in second position. */
334 op1
= gimple_cond_rhs (use_stmt
);
335 code
= gimple_cond_code (use_stmt
);
336 use_bb
= gimple_bb (use_stmt
);
338 extract_true_false_edges_from_block (use_bb
, &true_edge
, &false_edge
);
340 /* We're only interested in comparisons that distinguish
341 unambiguously from zero. */
342 if (!integer_zerop (op1
) || code
== LE_EXPR
|| code
== GE_EXPR
)
346 forbidden_bb
= false_edge
->dest
;
348 forbidden_bb
= true_edge
->dest
;
350 bitmap_set_bit (forbidden_dominators
, forbidden_bb
->index
);
354 /* If BB is dominated by any block in the forbidden dominators set,
355 return TRUE; else FALSE. */
358 dominated_by_forbidden (basic_block bb
)
363 EXECUTE_IF_SET_IN_BITMAP (forbidden_dominators
, 1, dom_bb
, bi
)
365 if (dominated_by_p (CDI_DOMINATORS
, bb
, BASIC_BLOCK (dom_bb
)))
372 /* We found an split_point CURRENT. NON_SSA_VARS is bitmap of all non ssa
373 variables used and RETURN_BB is return basic block.
374 See if we can split function here. */
377 consider_split (struct split_point
*current
, bitmap non_ssa_vars
,
378 basic_block return_bb
)
381 unsigned int num_args
= 0;
382 unsigned int call_overhead
;
385 gimple_stmt_iterator bsi
;
387 int incoming_freq
= 0;
389 bool back_edge
= false;
391 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
392 dump_split_point (dump_file
, current
);
394 FOR_EACH_EDGE (e
, ei
, current
->entry_bb
->preds
)
396 if (e
->flags
& EDGE_DFS_BACK
)
398 if (!bitmap_bit_p (current
->split_bbs
, e
->src
->index
))
399 incoming_freq
+= EDGE_FREQUENCY (e
);
402 /* Do not split when we would end up calling function anyway. */
404 >= (ENTRY_BLOCK_PTR_FOR_FN (cfun
)->frequency
405 * PARAM_VALUE (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY
) / 100))
407 /* When profile is guessed, we can not expect it to give us
408 realistic estimate on likelyness of function taking the
409 complex path. As a special case, when tail of the function is
410 a loop, enable splitting since inlining code skipping the loop
411 is likely noticeable win. */
413 && profile_status
!= PROFILE_READ
414 && incoming_freq
< ENTRY_BLOCK_PTR_FOR_FN (cfun
)->frequency
)
416 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
418 " Split before loop, accepting despite low frequencies %i %i.\n",
420 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->frequency
);
424 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
426 " Refused: incoming frequency is too large.\n");
431 if (!current
->header_size
)
433 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
434 fprintf (dump_file
, " Refused: header empty\n");
438 /* Verify that PHI args on entry are either virtual or all their operands
439 incoming from header are the same. */
440 for (bsi
= gsi_start_phis (current
->entry_bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
442 gimple stmt
= gsi_stmt (bsi
);
445 if (virtual_operand_p (gimple_phi_result (stmt
)))
447 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
449 edge e
= gimple_phi_arg_edge (stmt
, i
);
450 if (!bitmap_bit_p (current
->split_bbs
, e
->src
->index
))
452 tree edge_val
= gimple_phi_arg_def (stmt
, i
);
453 if (val
&& edge_val
!= val
)
455 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
457 " Refused: entry BB has PHI with multiple variants\n");
466 /* See what argument we will pass to the split function and compute
468 call_overhead
= eni_size_weights
.call_cost
;
469 for (parm
= DECL_ARGUMENTS (current_function_decl
); parm
;
470 parm
= DECL_CHAIN (parm
))
472 if (!is_gimple_reg (parm
))
474 if (bitmap_bit_p (non_ssa_vars
, DECL_UID (parm
)))
476 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
478 " Refused: need to pass non-ssa param values\n");
484 tree ddef
= ssa_default_def (cfun
, parm
);
486 && bitmap_bit_p (current
->ssa_names_to_pass
,
487 SSA_NAME_VERSION (ddef
)))
489 if (!VOID_TYPE_P (TREE_TYPE (parm
)))
490 call_overhead
+= estimate_move_cost (TREE_TYPE (parm
));
495 if (!VOID_TYPE_P (TREE_TYPE (current_function_decl
)))
496 call_overhead
+= estimate_move_cost (TREE_TYPE (current_function_decl
));
498 if (current
->split_size
<= call_overhead
)
500 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
502 " Refused: split size is smaller than call overhead\n");
505 if (current
->header_size
+ call_overhead
506 >= (unsigned int)(DECL_DECLARED_INLINE_P (current_function_decl
)
507 ? MAX_INLINE_INSNS_SINGLE
508 : MAX_INLINE_INSNS_AUTO
))
510 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
512 " Refused: header size is too large for inline candidate\n");
516 /* FIXME: we currently can pass only SSA function parameters to the split
517 arguments. Once parm_adjustment infrastructure is supported by cloning,
518 we can pass more than that. */
519 if (num_args
!= bitmap_count_bits (current
->ssa_names_to_pass
))
522 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
524 " Refused: need to pass non-param values\n");
528 /* When there are non-ssa vars used in the split region, see if they
529 are used in the header region. If so, reject the split.
530 FIXME: we can use nested function support to access both. */
531 if (!bitmap_empty_p (non_ssa_vars
)
532 && !verify_non_ssa_vars (current
, non_ssa_vars
, return_bb
))
534 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
536 " Refused: split part has non-ssa uses\n");
540 /* If the split point is dominated by a forbidden block, reject
542 if (!bitmap_empty_p (forbidden_dominators
)
543 && dominated_by_forbidden (current
->entry_bb
))
545 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
547 " Refused: split point dominated by forbidden block\n");
551 /* See if retval used by return bb is computed by header or split part.
552 When it is computed by split part, we need to produce return statement
553 in the split part and add code to header to pass it around.
555 This is bit tricky to test:
556 1) When there is no return_bb or no return value, we always pass
558 2) Invariants are always computed by caller.
559 3) For SSA we need to look if defining statement is in header or split part
560 4) For non-SSA we need to look where the var is computed. */
561 retval
= find_retval (return_bb
);
563 current
->split_part_set_retval
= true;
564 else if (is_gimple_min_invariant (retval
))
565 current
->split_part_set_retval
= false;
566 /* Special case is value returned by reference we record as if it was non-ssa
567 set to result_decl. */
568 else if (TREE_CODE (retval
) == SSA_NAME
569 && SSA_NAME_VAR (retval
)
570 && TREE_CODE (SSA_NAME_VAR (retval
)) == RESULT_DECL
571 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
572 current
->split_part_set_retval
573 = bitmap_bit_p (non_ssa_vars
, DECL_UID (SSA_NAME_VAR (retval
)));
574 else if (TREE_CODE (retval
) == SSA_NAME
)
575 current
->split_part_set_retval
576 = (!SSA_NAME_IS_DEFAULT_DEF (retval
)
577 && (bitmap_bit_p (current
->split_bbs
,
578 gimple_bb (SSA_NAME_DEF_STMT (retval
))->index
)
579 || gimple_bb (SSA_NAME_DEF_STMT (retval
)) == return_bb
));
580 else if (TREE_CODE (retval
) == PARM_DECL
)
581 current
->split_part_set_retval
= false;
582 else if (TREE_CODE (retval
) == VAR_DECL
583 || TREE_CODE (retval
) == RESULT_DECL
)
584 current
->split_part_set_retval
585 = bitmap_bit_p (non_ssa_vars
, DECL_UID (retval
));
587 current
->split_part_set_retval
= true;
589 /* split_function fixes up at most one PHI non-virtual PHI node in return_bb,
590 for the return value. If there are other PHIs, give up. */
591 if (return_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
593 gimple_stmt_iterator psi
;
595 for (psi
= gsi_start_phis (return_bb
); !gsi_end_p (psi
); gsi_next (&psi
))
596 if (!virtual_operand_p (gimple_phi_result (gsi_stmt (psi
)))
598 && current
->split_part_set_retval
599 && TREE_CODE (retval
) == SSA_NAME
600 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
))
601 && SSA_NAME_DEF_STMT (retval
) == gsi_stmt (psi
)))
603 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
605 " Refused: return bb has extra PHIs\n");
610 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
611 fprintf (dump_file
, " Accepted!\n");
613 /* At the moment chose split point with lowest frequency and that leaves
614 out smallest size of header.
615 In future we might re-consider this heuristics. */
616 if (!best_split_point
.split_bbs
617 || best_split_point
.entry_bb
->frequency
> current
->entry_bb
->frequency
618 || (best_split_point
.entry_bb
->frequency
== current
->entry_bb
->frequency
619 && best_split_point
.split_size
< current
->split_size
))
622 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
623 fprintf (dump_file
, " New best split point!\n");
624 if (best_split_point
.ssa_names_to_pass
)
626 BITMAP_FREE (best_split_point
.ssa_names_to_pass
);
627 BITMAP_FREE (best_split_point
.split_bbs
);
629 best_split_point
= *current
;
630 best_split_point
.ssa_names_to_pass
= BITMAP_ALLOC (NULL
);
631 bitmap_copy (best_split_point
.ssa_names_to_pass
,
632 current
->ssa_names_to_pass
);
633 best_split_point
.split_bbs
= BITMAP_ALLOC (NULL
);
634 bitmap_copy (best_split_point
.split_bbs
, current
->split_bbs
);
638 /* Return basic block containing RETURN statement. We allow basic blocks
642 but return_bb can not be more complex than this.
643 If nothing is found, return the exit block.
645 When there are multiple RETURN statement, chose one with return value,
646 since that one is more likely shared by multiple code paths.
648 Return BB is special, because for function splitting it is the only
649 basic block that is duplicated in between header and split part of the
652 TODO: We might support multiple return blocks. */
655 find_return_bb (void)
658 basic_block return_bb
= EXIT_BLOCK_PTR_FOR_FN (cfun
);
659 gimple_stmt_iterator bsi
;
660 bool found_return
= false;
661 tree retval
= NULL_TREE
;
663 if (!single_pred_p (EXIT_BLOCK_PTR_FOR_FN (cfun
)))
666 e
= single_pred_edge (EXIT_BLOCK_PTR_FOR_FN (cfun
));
667 for (bsi
= gsi_last_bb (e
->src
); !gsi_end_p (bsi
); gsi_prev (&bsi
))
669 gimple stmt
= gsi_stmt (bsi
);
670 if (gimple_code (stmt
) == GIMPLE_LABEL
671 || is_gimple_debug (stmt
)
672 || gimple_clobber_p (stmt
))
674 else if (gimple_code (stmt
) == GIMPLE_ASSIGN
676 && gimple_assign_single_p (stmt
)
677 && (auto_var_in_fn_p (gimple_assign_rhs1 (stmt
),
678 current_function_decl
)
679 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
680 && retval
== gimple_assign_lhs (stmt
))
682 else if (gimple_code (stmt
) == GIMPLE_RETURN
)
685 retval
= gimple_return_retval (stmt
);
690 if (gsi_end_p (bsi
) && found_return
)
696 /* Given return basic block RETURN_BB, see where return value is really
699 find_retval (basic_block return_bb
)
701 gimple_stmt_iterator bsi
;
702 for (bsi
= gsi_start_bb (return_bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
703 if (gimple_code (gsi_stmt (bsi
)) == GIMPLE_RETURN
)
704 return gimple_return_retval (gsi_stmt (bsi
));
705 else if (gimple_code (gsi_stmt (bsi
)) == GIMPLE_ASSIGN
706 && !gimple_clobber_p (gsi_stmt (bsi
)))
707 return gimple_assign_rhs1 (gsi_stmt (bsi
));
711 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
712 variable, mark it as used in bitmap passed via DATA.
713 Return true when access to T prevents splitting the function. */
716 mark_nonssa_use (gimple stmt ATTRIBUTE_UNUSED
, tree t
, void *data
)
718 t
= get_base_address (t
);
720 if (!t
|| is_gimple_reg (t
))
723 /* At present we can't pass non-SSA arguments to split function.
724 FIXME: this can be relaxed by passing references to arguments. */
725 if (TREE_CODE (t
) == PARM_DECL
)
727 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
729 "Cannot split: use of non-ssa function parameter.\n");
733 if ((TREE_CODE (t
) == VAR_DECL
734 && auto_var_in_fn_p (t
, current_function_decl
))
735 || TREE_CODE (t
) == RESULT_DECL
736 || TREE_CODE (t
) == LABEL_DECL
)
737 bitmap_set_bit ((bitmap
)data
, DECL_UID (t
));
739 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
740 to pretend that the value pointed to is actual result decl. */
741 if ((TREE_CODE (t
) == MEM_REF
|| INDIRECT_REF_P (t
))
742 && TREE_CODE (TREE_OPERAND (t
, 0)) == SSA_NAME
743 && SSA_NAME_VAR (TREE_OPERAND (t
, 0))
744 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t
, 0))) == RESULT_DECL
745 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
747 bitmap_bit_p ((bitmap
)data
,
748 DECL_UID (DECL_RESULT (current_function_decl
)));
753 /* Compute local properties of basic block BB we collect when looking for
754 split points. We look for ssa defs and store them in SET_SSA_NAMES,
755 for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic
756 vars stored in NON_SSA_VARS.
758 When BB has edge to RETURN_BB, collect uses in RETURN_BB too.
760 Return false when BB contains something that prevents it from being put into
764 visit_bb (basic_block bb
, basic_block return_bb
,
765 bitmap set_ssa_names
, bitmap used_ssa_names
,
768 gimple_stmt_iterator bsi
;
771 bool can_split
= true;
773 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
775 gimple stmt
= gsi_stmt (bsi
);
780 if (is_gimple_debug (stmt
))
783 if (gimple_clobber_p (stmt
))
786 /* FIXME: We can split regions containing EH. We can not however
787 split RESX, EH_DISPATCH and EH_POINTER referring to same region
788 into different partitions. This would require tracking of
789 EH regions and checking in consider_split_point if they
790 are not used elsewhere. */
791 if (gimple_code (stmt
) == GIMPLE_RESX
)
793 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
794 fprintf (dump_file
, "Cannot split: resx.\n");
797 if (gimple_code (stmt
) == GIMPLE_EH_DISPATCH
)
799 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
800 fprintf (dump_file
, "Cannot split: eh dispatch.\n");
804 /* Check builtins that prevent splitting. */
805 if (gimple_code (stmt
) == GIMPLE_CALL
806 && (decl
= gimple_call_fndecl (stmt
)) != NULL_TREE
807 && DECL_BUILT_IN (decl
)
808 && DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
809 switch (DECL_FUNCTION_CODE (decl
))
811 /* FIXME: once we will allow passing non-parm values to split part,
812 we need to be sure to handle correct builtin_stack_save and
813 builtin_stack_restore. At the moment we are safe; there is no
814 way to store builtin_stack_save result in non-SSA variable
815 since all calls to those are compiler generated. */
817 case BUILT_IN_APPLY_ARGS
:
818 case BUILT_IN_VA_START
:
819 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
821 "Cannot split: builtin_apply and va_start.\n");
824 case BUILT_IN_EH_POINTER
:
825 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
826 fprintf (dump_file
, "Cannot split: builtin_eh_pointer.\n");
833 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_DEF
)
834 bitmap_set_bit (set_ssa_names
, SSA_NAME_VERSION (op
));
835 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
836 bitmap_set_bit (used_ssa_names
, SSA_NAME_VERSION (op
));
837 can_split
&= !walk_stmt_load_store_addr_ops (stmt
, non_ssa_vars
,
842 for (bsi
= gsi_start_phis (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
844 gimple stmt
= gsi_stmt (bsi
);
847 if (virtual_operand_p (gimple_phi_result (stmt
)))
849 bitmap_set_bit (set_ssa_names
,
850 SSA_NAME_VERSION (gimple_phi_result (stmt
)));
851 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
853 tree op
= gimple_phi_arg_def (stmt
, i
);
854 if (TREE_CODE (op
) == SSA_NAME
)
855 bitmap_set_bit (used_ssa_names
, SSA_NAME_VERSION (op
));
857 can_split
&= !walk_stmt_load_store_addr_ops (stmt
, non_ssa_vars
,
862 /* Record also uses coming from PHI operand in return BB. */
863 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
864 if (e
->dest
== return_bb
)
866 for (bsi
= gsi_start_phis (return_bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
868 gimple stmt
= gsi_stmt (bsi
);
869 tree op
= gimple_phi_arg_def (stmt
, e
->dest_idx
);
871 if (virtual_operand_p (gimple_phi_result (stmt
)))
873 if (TREE_CODE (op
) == SSA_NAME
)
874 bitmap_set_bit (used_ssa_names
, SSA_NAME_VERSION (op
));
876 can_split
&= !mark_nonssa_use (stmt
, op
, non_ssa_vars
);
882 /* Stack entry for recursive DFS walk in find_split_point. */
886 /* Basic block we are examining. */
889 /* SSA names set and used by the BB and all BBs reachable
890 from it via DFS walk. */
891 bitmap set_ssa_names
, used_ssa_names
;
894 /* All BBS visited from this BB via DFS walk. */
897 /* Last examined edge in DFS walk. Since we walk unoriented graph,
898 the value is up to sum of incoming and outgoing edges of BB. */
899 unsigned int edge_num
;
901 /* Stack entry index of earliest BB reachable from current BB
902 or any BB visited later in DFS walk. */
905 /* Overall time and size of all BBs reached from this BB in DFS walk. */
906 int overall_time
, overall_size
;
908 /* When false we can not split on this BB. */
913 /* Find all articulations and call consider_split on them.
914 OVERALL_TIME and OVERALL_SIZE is time and size of the function.
916 We perform basic algorithm for finding an articulation in a graph
917 created from CFG by considering it to be an unoriented graph.
919 The articulation is discovered via DFS walk. We collect earliest
920 basic block on stack that is reachable via backward edge. Articulation
921 is any basic block such that there is no backward edge bypassing it.
922 To reduce stack usage we maintain heap allocated stack in STACK vector.
923 AUX pointer of BB is set to index it appears in the stack or -1 once
924 it is visited and popped off the stack.
926 The algorithm finds articulation after visiting the whole component
927 reachable by it. This makes it convenient to collect information about
928 the component used by consider_split. */
931 find_split_points (int overall_time
, int overall_size
)
934 vec
<stack_entry
> stack
= vNULL
;
936 basic_block return_bb
= find_return_bb ();
937 struct split_point current
;
939 current
.header_time
= overall_time
;
940 current
.header_size
= overall_size
;
941 current
.split_time
= 0;
942 current
.split_size
= 0;
943 current
.ssa_names_to_pass
= BITMAP_ALLOC (NULL
);
945 first
.bb
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
947 first
.overall_time
= 0;
948 first
.overall_size
= 0;
949 first
.earliest
= INT_MAX
;
950 first
.set_ssa_names
= 0;
951 first
.used_ssa_names
= 0;
952 first
.bbs_visited
= 0;
953 stack
.safe_push (first
);
954 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->aux
= (void *)(intptr_t)-1;
956 while (!stack
.is_empty ())
958 stack_entry
*entry
= &stack
.last ();
960 /* We are walking an acyclic graph, so edge_num counts
961 succ and pred edges together. However when considering
962 articulation, we want to have processed everything reachable
963 from articulation but nothing that reaches into it. */
964 if (entry
->edge_num
== EDGE_COUNT (entry
->bb
->succs
)
965 && entry
->bb
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
))
967 int pos
= stack
.length ();
968 entry
->can_split
&= visit_bb (entry
->bb
, return_bb
,
969 entry
->set_ssa_names
,
970 entry
->used_ssa_names
,
971 entry
->non_ssa_vars
);
972 if (pos
<= entry
->earliest
&& !entry
->can_split
973 && dump_file
&& (dump_flags
& TDF_DETAILS
))
975 "found articulation at bb %i but can not split\n",
977 if (pos
<= entry
->earliest
&& entry
->can_split
)
979 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
980 fprintf (dump_file
, "found articulation at bb %i\n",
982 current
.entry_bb
= entry
->bb
;
983 current
.ssa_names_to_pass
= BITMAP_ALLOC (NULL
);
984 bitmap_and_compl (current
.ssa_names_to_pass
,
985 entry
->used_ssa_names
, entry
->set_ssa_names
);
986 current
.header_time
= overall_time
- entry
->overall_time
;
987 current
.header_size
= overall_size
- entry
->overall_size
;
988 current
.split_time
= entry
->overall_time
;
989 current
.split_size
= entry
->overall_size
;
990 current
.split_bbs
= entry
->bbs_visited
;
991 consider_split (¤t
, entry
->non_ssa_vars
, return_bb
);
992 BITMAP_FREE (current
.ssa_names_to_pass
);
995 /* Do actual DFS walk. */
997 < (EDGE_COUNT (entry
->bb
->succs
)
998 + EDGE_COUNT (entry
->bb
->preds
)))
1002 if (entry
->edge_num
< EDGE_COUNT (entry
->bb
->succs
))
1004 e
= EDGE_SUCC (entry
->bb
, entry
->edge_num
);
1009 e
= EDGE_PRED (entry
->bb
, entry
->edge_num
1010 - EDGE_COUNT (entry
->bb
->succs
));
1016 /* New BB to visit, push it to the stack. */
1017 if (dest
!= return_bb
&& dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
)
1020 stack_entry new_entry
;
1022 new_entry
.bb
= dest
;
1023 new_entry
.edge_num
= 0;
1024 new_entry
.overall_time
1025 = bb_info_vec
[dest
->index
].time
;
1026 new_entry
.overall_size
1027 = bb_info_vec
[dest
->index
].size
;
1028 new_entry
.earliest
= INT_MAX
;
1029 new_entry
.set_ssa_names
= BITMAP_ALLOC (NULL
);
1030 new_entry
.used_ssa_names
= BITMAP_ALLOC (NULL
);
1031 new_entry
.bbs_visited
= BITMAP_ALLOC (NULL
);
1032 new_entry
.non_ssa_vars
= BITMAP_ALLOC (NULL
);
1033 new_entry
.can_split
= true;
1034 bitmap_set_bit (new_entry
.bbs_visited
, dest
->index
);
1035 stack
.safe_push (new_entry
);
1036 dest
->aux
= (void *)(intptr_t)stack
.length ();
1038 /* Back edge found, record the earliest point. */
1039 else if ((intptr_t)dest
->aux
> 0
1040 && (intptr_t)dest
->aux
< entry
->earliest
)
1041 entry
->earliest
= (intptr_t)dest
->aux
;
1043 /* We are done with examining the edges. Pop off the value from stack
1044 and merge stuff we accumulate during the walk. */
1045 else if (entry
->bb
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
))
1047 stack_entry
*prev
= &stack
[stack
.length () - 2];
1049 entry
->bb
->aux
= (void *)(intptr_t)-1;
1050 prev
->can_split
&= entry
->can_split
;
1051 if (prev
->set_ssa_names
)
1053 bitmap_ior_into (prev
->set_ssa_names
, entry
->set_ssa_names
);
1054 bitmap_ior_into (prev
->used_ssa_names
, entry
->used_ssa_names
);
1055 bitmap_ior_into (prev
->bbs_visited
, entry
->bbs_visited
);
1056 bitmap_ior_into (prev
->non_ssa_vars
, entry
->non_ssa_vars
);
1058 if (prev
->earliest
> entry
->earliest
)
1059 prev
->earliest
= entry
->earliest
;
1060 prev
->overall_time
+= entry
->overall_time
;
1061 prev
->overall_size
+= entry
->overall_size
;
1062 BITMAP_FREE (entry
->set_ssa_names
);
1063 BITMAP_FREE (entry
->used_ssa_names
);
1064 BITMAP_FREE (entry
->bbs_visited
);
1065 BITMAP_FREE (entry
->non_ssa_vars
);
1071 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->aux
= NULL
;
1075 BITMAP_FREE (current
.ssa_names_to_pass
);
1078 /* Split function at SPLIT_POINT. */
1081 split_function (struct split_point
*split_point
)
1083 vec
<tree
> args_to_pass
= vNULL
;
1084 bitmap args_to_skip
;
1087 struct cgraph_node
*node
, *cur_node
= cgraph_get_node (current_function_decl
);
1088 basic_block return_bb
= find_return_bb ();
1089 basic_block call_bb
;
1090 gimple_stmt_iterator gsi
;
1094 tree retval
= NULL
, real_retval
= NULL
;
1095 bool split_part_return_p
= false;
1096 gimple last_stmt
= NULL
;
1099 vec
<tree
, va_gc
> **debug_args
= NULL
;
1103 fprintf (dump_file
, "\n\nSplitting function at:\n");
1104 dump_split_point (dump_file
, split_point
);
1107 if (cur_node
->local
.can_change_signature
)
1108 args_to_skip
= BITMAP_ALLOC (NULL
);
1110 args_to_skip
= NULL
;
1112 /* Collect the parameters of new function and args_to_skip bitmap. */
1113 for (parm
= DECL_ARGUMENTS (current_function_decl
);
1114 parm
; parm
= DECL_CHAIN (parm
), num
++)
1116 && (!is_gimple_reg (parm
)
1117 || (ddef
= ssa_default_def (cfun
, parm
)) == NULL_TREE
1118 || !bitmap_bit_p (split_point
->ssa_names_to_pass
,
1119 SSA_NAME_VERSION (ddef
))))
1120 bitmap_set_bit (args_to_skip
, num
);
1123 /* This parm might not have been used up to now, but is going to be
1124 used, hence register it. */
1125 if (is_gimple_reg (parm
))
1126 arg
= get_or_create_ssa_default_def (cfun
, parm
);
1130 if (!useless_type_conversion_p (DECL_ARG_TYPE (parm
), TREE_TYPE (arg
)))
1131 arg
= fold_convert (DECL_ARG_TYPE (parm
), arg
);
1132 args_to_pass
.safe_push (arg
);
1135 /* See if the split function will return. */
1136 FOR_EACH_EDGE (e
, ei
, return_bb
->preds
)
1137 if (bitmap_bit_p (split_point
->split_bbs
, e
->src
->index
))
1140 split_part_return_p
= true;
1142 /* Add return block to what will become the split function.
1143 We do not return; no return block is needed. */
1144 if (!split_part_return_p
)
1146 /* We have no return block, so nothing is needed. */
1147 else if (return_bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
1149 /* When we do not want to return value, we need to construct
1150 new return block with empty return statement.
1151 FIXME: Once we are able to change return type, we should change function
1152 to return void instead of just outputting function with undefined return
1153 value. For structures this affects quality of codegen. */
1154 else if (!split_point
->split_part_set_retval
1155 && find_retval (return_bb
))
1157 bool redirected
= true;
1158 basic_block new_return_bb
= create_basic_block (NULL
, 0, return_bb
);
1159 gimple_stmt_iterator gsi
= gsi_start_bb (new_return_bb
);
1160 gsi_insert_after (&gsi
, gimple_build_return (NULL
), GSI_NEW_STMT
);
1164 FOR_EACH_EDGE (e
, ei
, return_bb
->preds
)
1165 if (bitmap_bit_p (split_point
->split_bbs
, e
->src
->index
))
1167 new_return_bb
->count
+= e
->count
;
1168 new_return_bb
->frequency
+= EDGE_FREQUENCY (e
);
1169 redirect_edge_and_branch (e
, new_return_bb
);
1174 e
= make_edge (new_return_bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
), 0);
1175 e
->probability
= REG_BR_PROB_BASE
;
1176 e
->count
= new_return_bb
->count
;
1178 add_bb_to_loop (new_return_bb
, current_loops
->tree_root
);
1179 bitmap_set_bit (split_point
->split_bbs
, new_return_bb
->index
);
1181 /* When we pass around the value, use existing return block. */
1183 bitmap_set_bit (split_point
->split_bbs
, return_bb
->index
);
1185 /* If RETURN_BB has virtual operand PHIs, they must be removed and the
1186 virtual operand marked for renaming as we change the CFG in a way that
1187 tree-inline is not able to compensate for.
1189 Note this can happen whether or not we have a return value. If we have
1190 a return value, then RETURN_BB may have PHIs for real operands too. */
1191 if (return_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
1194 for (gsi
= gsi_start_phis (return_bb
); !gsi_end_p (gsi
);)
1196 gimple stmt
= gsi_stmt (gsi
);
1197 if (!virtual_operand_p (gimple_phi_result (stmt
)))
1202 mark_virtual_phi_result_for_renaming (stmt
);
1203 remove_phi_node (&gsi
, true);
1206 /* In reality we have to rename the reaching definition of the
1207 virtual operand at return_bb as we will eventually release it
1208 when we remove the code region we outlined.
1209 So we have to rename all immediate virtual uses of that region
1210 if we didn't see a PHI definition yet. */
1211 /* ??? In real reality we want to set the reaching vdef of the
1212 entry of the SESE region as the vuse of the call and the reaching
1213 vdef of the exit of the SESE region as the vdef of the call. */
1215 for (gsi
= gsi_start_bb (return_bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1217 gimple stmt
= gsi_stmt (gsi
);
1218 if (gimple_vuse (stmt
))
1220 gimple_set_vuse (stmt
, NULL_TREE
);
1223 if (gimple_vdef (stmt
))
1228 /* Now create the actual clone. */
1229 rebuild_cgraph_edges ();
1230 node
= cgraph_function_versioning (cur_node
, vNULL
,
1233 !split_part_return_p
,
1234 split_point
->split_bbs
,
1235 split_point
->entry_bb
, "part");
1236 /* For usual cloning it is enough to clear builtin only when signature
1237 changes. For partial inlining we however can not expect the part
1238 of builtin implementation to have same semantic as the whole. */
1239 if (DECL_BUILT_IN (node
->decl
))
1241 DECL_BUILT_IN_CLASS (node
->decl
) = NOT_BUILT_IN
;
1242 DECL_FUNCTION_CODE (node
->decl
) = (enum built_in_function
) 0;
1244 /* If the original function is declared inline, there is no point in issuing
1245 a warning for the non-inlinable part. */
1246 DECL_NO_INLINE_WARNING_P (node
->decl
) = 1;
1247 cgraph_node_remove_callees (cur_node
);
1248 ipa_remove_all_references (&cur_node
->ref_list
);
1249 if (!split_part_return_p
)
1250 TREE_THIS_VOLATILE (node
->decl
) = 1;
1252 dump_function_to_file (node
->decl
, dump_file
, dump_flags
);
1254 /* Create the basic block we place call into. It is the entry basic block
1255 split after last label. */
1256 call_bb
= split_point
->entry_bb
;
1257 for (gsi
= gsi_start_bb (call_bb
); !gsi_end_p (gsi
);)
1258 if (gimple_code (gsi_stmt (gsi
)) == GIMPLE_LABEL
)
1260 last_stmt
= gsi_stmt (gsi
);
1265 e
= split_block (split_point
->entry_bb
, last_stmt
);
1268 /* Produce the call statement. */
1269 gsi
= gsi_last_bb (call_bb
);
1270 FOR_EACH_VEC_ELT (args_to_pass
, i
, arg
)
1271 if (!is_gimple_val (arg
))
1273 arg
= force_gimple_operand_gsi (&gsi
, arg
, true, NULL_TREE
,
1274 false, GSI_CONTINUE_LINKING
);
1275 args_to_pass
[i
] = arg
;
1277 call
= gimple_build_call_vec (node
->decl
, args_to_pass
);
1278 gimple_set_block (call
, DECL_INITIAL (current_function_decl
));
1279 args_to_pass
.release ();
1281 /* For optimized away parameters, add on the caller side
1283 DEBUG D#X => parm_Y(D)
1284 stmts and associate D#X with parm in decl_debug_args_lookup
1285 vector to say for debug info that if parameter parm had been passed,
1286 it would have value parm_Y(D). */
1288 for (parm
= DECL_ARGUMENTS (current_function_decl
), num
= 0;
1289 parm
; parm
= DECL_CHAIN (parm
), num
++)
1290 if (bitmap_bit_p (args_to_skip
, num
)
1291 && is_gimple_reg (parm
))
1296 /* This needs to be done even without MAY_HAVE_DEBUG_STMTS,
1297 otherwise if it didn't exist before, we'd end up with
1298 different SSA_NAME_VERSIONs between -g and -g0. */
1299 arg
= get_or_create_ssa_default_def (cfun
, parm
);
1300 if (!MAY_HAVE_DEBUG_STMTS
)
1303 if (debug_args
== NULL
)
1304 debug_args
= decl_debug_args_insert (node
->decl
);
1305 ddecl
= make_node (DEBUG_EXPR_DECL
);
1306 DECL_ARTIFICIAL (ddecl
) = 1;
1307 TREE_TYPE (ddecl
) = TREE_TYPE (parm
);
1308 DECL_MODE (ddecl
) = DECL_MODE (parm
);
1309 vec_safe_push (*debug_args
, DECL_ORIGIN (parm
));
1310 vec_safe_push (*debug_args
, ddecl
);
1311 def_temp
= gimple_build_debug_bind (ddecl
, unshare_expr (arg
),
1313 gsi_insert_after (&gsi
, def_temp
, GSI_NEW_STMT
);
1315 /* And on the callee side, add
1318 stmts to the first bb where var is a VAR_DECL created for the
1319 optimized away parameter in DECL_INITIAL block. This hints
1320 in the debug info that var (whole DECL_ORIGIN is the parm PARM_DECL)
1321 is optimized away, but could be looked up at the call site
1322 as value of D#X there. */
1323 if (debug_args
!= NULL
)
1327 gimple_stmt_iterator cgsi
;
1330 push_cfun (DECL_STRUCT_FUNCTION (node
->decl
));
1331 var
= BLOCK_VARS (DECL_INITIAL (node
->decl
));
1332 i
= vec_safe_length (*debug_args
);
1333 cgsi
= gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
)));
1337 while (var
!= NULL_TREE
1338 && DECL_ABSTRACT_ORIGIN (var
) != (**debug_args
)[i
])
1339 var
= TREE_CHAIN (var
);
1340 if (var
== NULL_TREE
)
1342 vexpr
= make_node (DEBUG_EXPR_DECL
);
1343 parm
= (**debug_args
)[i
];
1344 DECL_ARTIFICIAL (vexpr
) = 1;
1345 TREE_TYPE (vexpr
) = TREE_TYPE (parm
);
1346 DECL_MODE (vexpr
) = DECL_MODE (parm
);
1347 def_temp
= gimple_build_debug_source_bind (vexpr
, parm
,
1349 gsi_insert_before (&cgsi
, def_temp
, GSI_SAME_STMT
);
1350 def_temp
= gimple_build_debug_bind (var
, vexpr
, NULL
);
1351 gsi_insert_before (&cgsi
, def_temp
, GSI_SAME_STMT
);
1357 /* We avoid address being taken on any variable used by split part,
1358 so return slot optimization is always possible. Moreover this is
1359 required to make DECL_BY_REFERENCE work. */
1360 if (aggregate_value_p (DECL_RESULT (current_function_decl
),
1361 TREE_TYPE (current_function_decl
))
1362 && (!is_gimple_reg_type (TREE_TYPE (DECL_RESULT (current_function_decl
)))
1363 || DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
))))
1364 gimple_call_set_return_slot_opt (call
, true);
1366 /* Update return value. This is bit tricky. When we do not return,
1367 do nothing. When we return we might need to update return_bb
1368 or produce a new return statement. */
1369 if (!split_part_return_p
)
1370 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1373 e
= make_edge (call_bb
, return_bb
,
1374 return_bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
)
1375 ? 0 : EDGE_FALLTHRU
);
1376 e
->count
= call_bb
->count
;
1377 e
->probability
= REG_BR_PROB_BASE
;
1379 /* If there is return basic block, see what value we need to store
1380 return value into and put call just before it. */
1381 if (return_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
1383 real_retval
= retval
= find_retval (return_bb
);
1385 if (real_retval
&& split_point
->split_part_set_retval
)
1387 gimple_stmt_iterator psi
;
1389 /* See if we need new SSA_NAME for the result.
1390 When DECL_BY_REFERENCE is true, retval is actually pointer to
1391 return value and it is constant in whole function. */
1392 if (TREE_CODE (retval
) == SSA_NAME
1393 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
1395 retval
= copy_ssa_name (retval
, call
);
1397 /* See if there is PHI defining return value. */
1398 for (psi
= gsi_start_phis (return_bb
);
1399 !gsi_end_p (psi
); gsi_next (&psi
))
1400 if (!virtual_operand_p (gimple_phi_result (gsi_stmt (psi
))))
1403 /* When there is PHI, just update its value. */
1404 if (TREE_CODE (retval
) == SSA_NAME
1405 && !gsi_end_p (psi
))
1406 add_phi_arg (gsi_stmt (psi
), retval
, e
, UNKNOWN_LOCATION
);
1407 /* Otherwise update the return BB itself.
1408 find_return_bb allows at most one assignment to return value,
1409 so update first statement. */
1412 gimple_stmt_iterator bsi
;
1413 for (bsi
= gsi_start_bb (return_bb
); !gsi_end_p (bsi
);
1415 if (gimple_code (gsi_stmt (bsi
)) == GIMPLE_RETURN
)
1417 gimple_return_set_retval (gsi_stmt (bsi
), retval
);
1420 else if (gimple_code (gsi_stmt (bsi
)) == GIMPLE_ASSIGN
1421 && !gimple_clobber_p (gsi_stmt (bsi
)))
1423 gimple_assign_set_rhs1 (gsi_stmt (bsi
), retval
);
1426 update_stmt (gsi_stmt (bsi
));
1429 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
1431 gimple_call_set_lhs (call
, build_simple_mem_ref (retval
));
1432 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1437 restype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
1438 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1439 if (!useless_type_conversion_p (TREE_TYPE (retval
), restype
))
1442 tree tem
= create_tmp_reg (restype
, NULL
);
1443 tem
= make_ssa_name (tem
, call
);
1444 cpy
= gimple_build_assign_with_ops (NOP_EXPR
, retval
,
1446 gsi_insert_after (&gsi
, cpy
, GSI_NEW_STMT
);
1449 gimple_call_set_lhs (call
, retval
);
1454 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1456 /* We don't use return block (there is either no return in function or
1457 multiple of them). So create new basic block with return statement.
1462 if (split_point
->split_part_set_retval
1463 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl
))))
1465 retval
= DECL_RESULT (current_function_decl
);
1467 /* We use temporary register to hold value when aggregate_value_p
1468 is false. Similarly for DECL_BY_REFERENCE we must avoid extra
1470 if (!aggregate_value_p (retval
, TREE_TYPE (current_function_decl
))
1471 && !DECL_BY_REFERENCE (retval
))
1472 retval
= create_tmp_reg (TREE_TYPE (retval
), NULL
);
1473 if (is_gimple_reg (retval
))
1475 /* When returning by reference, there is only one SSA name
1476 assigned to RESULT_DECL (that is pointer to return value).
1477 Look it up or create new one if it is missing. */
1478 if (DECL_BY_REFERENCE (retval
))
1479 retval
= get_or_create_ssa_default_def (cfun
, retval
);
1480 /* Otherwise produce new SSA name for return value. */
1482 retval
= make_ssa_name (retval
, call
);
1484 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
1485 gimple_call_set_lhs (call
, build_simple_mem_ref (retval
));
1487 gimple_call_set_lhs (call
, retval
);
1489 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1490 ret
= gimple_build_return (retval
);
1491 gsi_insert_after (&gsi
, ret
, GSI_NEW_STMT
);
1494 free_dominance_info (CDI_DOMINATORS
);
1495 free_dominance_info (CDI_POST_DOMINATORS
);
1496 compute_inline_parameters (node
, true);
1499 /* Execute function splitting pass. */
1502 execute_split_functions (void)
1504 gimple_stmt_iterator bsi
;
1506 int overall_time
= 0, overall_size
= 0;
1508 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
1510 if (flags_from_decl_or_type (current_function_decl
)
1511 & (ECF_NORETURN
|ECF_MALLOC
))
1514 fprintf (dump_file
, "Not splitting: noreturn/malloc function.\n");
1517 if (MAIN_NAME_P (DECL_NAME (current_function_decl
)))
1520 fprintf (dump_file
, "Not splitting: main function.\n");
1523 /* This can be relaxed; function might become inlinable after splitting
1524 away the uninlinable part. */
1525 if (inline_edge_summary_vec
.exists ()
1526 && !inline_summary (node
)->inlinable
)
1529 fprintf (dump_file
, "Not splitting: not inlinable.\n");
1532 if (DECL_DISREGARD_INLINE_LIMITS (node
->decl
))
1535 fprintf (dump_file
, "Not splitting: disregarding inline limits.\n");
1538 /* This can be relaxed; most of versioning tests actually prevents
1540 if (!tree_versionable_function_p (current_function_decl
))
1543 fprintf (dump_file
, "Not splitting: not versionable.\n");
1546 /* FIXME: we could support this. */
1547 if (DECL_STRUCT_FUNCTION (current_function_decl
)->static_chain_decl
)
1550 fprintf (dump_file
, "Not splitting: nested function.\n");
1554 /* See if it makes sense to try to split.
1555 It makes sense to split if we inline, that is if we have direct calls to
1556 handle or direct calls are possibly going to appear as result of indirect
1557 inlining or LTO. Also handle -fprofile-generate as LTO to allow non-LTO
1558 training for LTO -fprofile-use build.
1560 Note that we are not completely conservative about disqualifying functions
1561 called once. It is possible that the caller is called more then once and
1562 then inlining would still benefit. */
1564 /* Local functions called once will be completely inlined most of time. */
1565 || (!node
->callers
->next_caller
&& node
->local
.local
))
1566 && !node
->address_taken
1567 && (!flag_lto
|| !node
->externally_visible
))
1570 fprintf (dump_file
, "Not splitting: not called directly "
1571 "or called once.\n");
1575 /* FIXME: We can actually split if splitting reduces call overhead. */
1576 if (!flag_inline_small_functions
1577 && !DECL_DECLARED_INLINE_P (current_function_decl
))
1580 fprintf (dump_file
, "Not splitting: not autoinlining and function"
1581 " is not inline.\n");
1585 /* We enforce splitting after loop headers when profile info is not
1587 if (profile_status
!= PROFILE_READ
)
1588 mark_dfs_back_edges ();
1590 /* Initialize bitmap to track forbidden calls. */
1591 forbidden_dominators
= BITMAP_ALLOC (NULL
);
1592 calculate_dominance_info (CDI_DOMINATORS
);
1594 /* Compute local info about basic blocks and determine function size/time. */
1595 bb_info_vec
.safe_grow_cleared (last_basic_block
+ 1);
1596 memset (&best_split_point
, 0, sizeof (best_split_point
));
1601 int freq
= compute_call_stmt_bb_frequency (current_function_decl
, bb
);
1603 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1604 fprintf (dump_file
, "Basic block %i\n", bb
->index
);
1606 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
1608 int this_time
, this_size
;
1609 gimple stmt
= gsi_stmt (bsi
);
1611 this_size
= estimate_num_insns (stmt
, &eni_size_weights
);
1612 this_time
= estimate_num_insns (stmt
, &eni_time_weights
) * freq
;
1615 check_forbidden_calls (stmt
);
1617 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1619 fprintf (dump_file
, " freq:%6i size:%3i time:%3i ",
1620 freq
, this_size
, this_time
);
1621 print_gimple_stmt (dump_file
, stmt
, 0, 0);
1624 overall_time
+= time
;
1625 overall_size
+= size
;
1626 bb_info_vec
[bb
->index
].time
= time
;
1627 bb_info_vec
[bb
->index
].size
= size
;
1629 find_split_points (overall_time
, overall_size
);
1630 if (best_split_point
.split_bbs
)
1632 split_function (&best_split_point
);
1633 BITMAP_FREE (best_split_point
.ssa_names_to_pass
);
1634 BITMAP_FREE (best_split_point
.split_bbs
);
1635 todo
= TODO_update_ssa
| TODO_cleanup_cfg
;
1637 BITMAP_FREE (forbidden_dominators
);
1638 bb_info_vec
.release ();
1642 /* Gate function splitting pass. When doing profile feedback, we want
1643 to execute the pass after profiling is read. So disable one in
1644 early optimization. */
1647 gate_split_functions (void)
1649 return (flag_partial_inlining
1650 && !profile_arc_flag
&& !flag_branch_probabilities
);
1655 const pass_data pass_data_split_functions
=
1657 GIMPLE_PASS
, /* type */
1658 "fnsplit", /* name */
1659 OPTGROUP_NONE
, /* optinfo_flags */
1660 true, /* has_gate */
1661 true, /* has_execute */
1662 TV_IPA_FNSPLIT
, /* tv_id */
1663 PROP_cfg
, /* properties_required */
1664 0, /* properties_provided */
1665 0, /* properties_destroyed */
1666 0, /* todo_flags_start */
1667 TODO_verify_all
, /* todo_flags_finish */
1670 class pass_split_functions
: public gimple_opt_pass
1673 pass_split_functions (gcc::context
*ctxt
)
1674 : gimple_opt_pass (pass_data_split_functions
, ctxt
)
1677 /* opt_pass methods: */
1678 bool gate () { return gate_split_functions (); }
1679 unsigned int execute () { return execute_split_functions (); }
1681 }; // class pass_split_functions
1686 make_pass_split_functions (gcc::context
*ctxt
)
1688 return new pass_split_functions (ctxt
);
1691 /* Gate feedback driven function splitting pass.
1692 We don't need to split when profiling at all, we are producing
1693 lousy code anyway. */
1696 gate_feedback_split_functions (void)
1698 return (flag_partial_inlining
1699 && flag_branch_probabilities
);
1702 /* Execute function splitting pass. */
1705 execute_feedback_split_functions (void)
1707 unsigned int retval
= execute_split_functions ();
1709 retval
|= TODO_rebuild_cgraph_edges
;
1715 const pass_data pass_data_feedback_split_functions
=
1717 GIMPLE_PASS
, /* type */
1718 "feedback_fnsplit", /* name */
1719 OPTGROUP_NONE
, /* optinfo_flags */
1720 true, /* has_gate */
1721 true, /* has_execute */
1722 TV_IPA_FNSPLIT
, /* tv_id */
1723 PROP_cfg
, /* properties_required */
1724 0, /* properties_provided */
1725 0, /* properties_destroyed */
1726 0, /* todo_flags_start */
1727 TODO_verify_all
, /* todo_flags_finish */
1730 class pass_feedback_split_functions
: public gimple_opt_pass
1733 pass_feedback_split_functions (gcc::context
*ctxt
)
1734 : gimple_opt_pass (pass_data_feedback_split_functions
, ctxt
)
1737 /* opt_pass methods: */
1738 bool gate () { return gate_feedback_split_functions (); }
1739 unsigned int execute () { return execute_feedback_split_functions (); }
1741 }; // class pass_feedback_split_functions
1746 make_pass_feedback_split_functions (gcc::context
*ctxt
)
1748 return new pass_feedback_split_functions (ctxt
);