1 /* Function splitting pass
2 Copyright (C) 2010-2014 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"
87 #include "hard-reg-set.h"
90 #include "dominance.h"
93 #include "basic-block.h"
94 #include "tree-ssa-alias.h"
95 #include "internal-fn.h"
96 #include "gimple-expr.h"
99 #include "stringpool.h"
102 #include "gimplify.h"
103 #include "gimple-iterator.h"
104 #include "gimplify-me.h"
105 #include "gimple-walk.h"
107 #include "hash-map.h"
108 #include "plugin-api.h"
111 #include "alloc-pool.h"
112 #include "symbol-summary.h"
113 #include "ipa-prop.h"
114 #include "gimple-ssa.h"
115 #include "tree-cfg.h"
116 #include "tree-phinodes.h"
117 #include "ssa-iterators.h"
118 #include "stringpool.h"
119 #include "tree-ssanames.h"
120 #include "tree-into-ssa.h"
121 #include "tree-dfa.h"
122 #include "tree-pass.h"
124 #include "diagnostic.h"
125 #include "tree-dump.h"
126 #include "tree-inline.h"
128 #include "gimple-pretty-print.h"
129 #include "ipa-inline.h"
131 #include "tree-chkp.h"
133 /* Per basic block info. */
141 static vec
<split_bb_info
> bb_info_vec
;
143 /* Description of split point. */
147 /* Size of the partitions. */
148 unsigned int header_time
, header_size
, split_time
, split_size
;
150 /* SSA names that need to be passed into spit function. */
151 bitmap ssa_names_to_pass
;
153 /* Basic block where we split (that will become entry point of new function. */
154 basic_block entry_bb
;
156 /* Basic blocks we are splitting away. */
159 /* True when return value is computed on split part and thus it needs
161 bool split_part_set_retval
;
164 /* Best split point found. */
166 struct split_point best_split_point
;
168 /* Set of basic blocks that are not allowed to dominate a split point. */
170 static bitmap forbidden_dominators
;
172 static tree
find_retval (basic_block return_bb
);
173 static tree
find_retbnd (basic_block return_bb
);
175 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
176 variable, check it if it is present in bitmap passed via DATA. */
179 test_nonssa_use (gimple
, tree t
, tree
, void *data
)
181 t
= get_base_address (t
);
183 if (!t
|| is_gimple_reg (t
))
186 if (TREE_CODE (t
) == PARM_DECL
187 || (TREE_CODE (t
) == VAR_DECL
188 && auto_var_in_fn_p (t
, current_function_decl
))
189 || TREE_CODE (t
) == RESULT_DECL
190 /* Normal labels are part of CFG and will be handled gratefuly.
191 Forced labels however can be used directly by statements and
192 need to stay in one partition along with their uses. */
193 || (TREE_CODE (t
) == LABEL_DECL
194 && FORCED_LABEL (t
)))
195 return bitmap_bit_p ((bitmap
)data
, DECL_UID (t
));
197 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
198 to pretend that the value pointed to is actual result decl. */
199 if ((TREE_CODE (t
) == MEM_REF
|| INDIRECT_REF_P (t
))
200 && TREE_CODE (TREE_OPERAND (t
, 0)) == SSA_NAME
201 && SSA_NAME_VAR (TREE_OPERAND (t
, 0))
202 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t
, 0))) == RESULT_DECL
203 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
205 bitmap_bit_p ((bitmap
)data
,
206 DECL_UID (DECL_RESULT (current_function_decl
)));
211 /* Dump split point CURRENT. */
214 dump_split_point (FILE * file
, struct split_point
*current
)
217 "Split point at BB %i\n"
218 " header time: %i header size: %i\n"
219 " split time: %i split size: %i\n bbs: ",
220 current
->entry_bb
->index
, current
->header_time
,
221 current
->header_size
, current
->split_time
, current
->split_size
);
222 dump_bitmap (file
, current
->split_bbs
);
223 fprintf (file
, " SSA names to pass: ");
224 dump_bitmap (file
, current
->ssa_names_to_pass
);
227 /* Look for all BBs in header that might lead to the split part and verify
228 that they are not defining any non-SSA var used by the split part.
229 Parameters are the same as for consider_split. */
232 verify_non_ssa_vars (struct split_point
*current
, bitmap non_ssa_vars
,
233 basic_block return_bb
)
235 bitmap seen
= BITMAP_ALLOC (NULL
);
236 vec
<basic_block
> worklist
= vNULL
;
242 FOR_EACH_EDGE (e
, ei
, current
->entry_bb
->preds
)
243 if (e
->src
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
)
244 && !bitmap_bit_p (current
->split_bbs
, e
->src
->index
))
246 worklist
.safe_push (e
->src
);
247 bitmap_set_bit (seen
, e
->src
->index
);
250 while (!worklist
.is_empty ())
252 bb
= worklist
.pop ();
253 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
254 if (e
->src
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
)
255 && bitmap_set_bit (seen
, e
->src
->index
))
257 gcc_checking_assert (!bitmap_bit_p (current
->split_bbs
,
259 worklist
.safe_push (e
->src
);
261 for (gimple_stmt_iterator bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
);
264 gimple stmt
= gsi_stmt (bsi
);
265 if (is_gimple_debug (stmt
))
267 if (walk_stmt_load_store_addr_ops
268 (stmt
, non_ssa_vars
, test_nonssa_use
, test_nonssa_use
,
274 if (glabel
*label_stmt
= dyn_cast
<glabel
*> (stmt
))
275 if (test_nonssa_use (stmt
, gimple_label_label (label_stmt
),
276 NULL_TREE
, non_ssa_vars
))
282 for (gphi_iterator bsi
= gsi_start_phis (bb
); !gsi_end_p (bsi
);
285 if (walk_stmt_load_store_addr_ops
286 (gsi_stmt (bsi
), non_ssa_vars
, test_nonssa_use
, test_nonssa_use
,
293 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
295 if (e
->dest
!= return_bb
)
297 for (gphi_iterator bsi
= gsi_start_phis (return_bb
);
301 gphi
*stmt
= bsi
.phi ();
302 tree op
= gimple_phi_arg_def (stmt
, e
->dest_idx
);
304 if (virtual_operand_p (gimple_phi_result (stmt
)))
306 if (TREE_CODE (op
) != SSA_NAME
307 && test_nonssa_use (stmt
, op
, op
, non_ssa_vars
))
316 /* Verify that the rest of function does not define any label
317 used by the split part. */
318 FOR_EACH_BB_FN (bb
, cfun
)
319 if (!bitmap_bit_p (current
->split_bbs
, bb
->index
)
320 && !bitmap_bit_p (seen
, bb
->index
))
322 gimple_stmt_iterator bsi
;
323 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
324 if (glabel
*label_stmt
= dyn_cast
<glabel
*> (gsi_stmt (bsi
)))
326 if (test_nonssa_use (label_stmt
,
327 gimple_label_label (label_stmt
),
328 NULL_TREE
, non_ssa_vars
))
344 /* If STMT is a call, check the callee against a list of forbidden
345 predicate functions. If a match is found, look for uses of the
346 call result in condition statements that compare against zero.
347 For each such use, find the block targeted by the condition
348 statement for the nonzero result, and set the bit for this block
349 in the forbidden dominators bitmap. The purpose of this is to avoid
350 selecting a split point where we are likely to lose the chance
351 to optimize away an unused function call. */
354 check_forbidden_calls (gimple stmt
)
356 imm_use_iterator use_iter
;
360 /* At the moment, __builtin_constant_p is the only forbidden
361 predicate function call (see PR49642). */
362 if (!gimple_call_builtin_p (stmt
, BUILT_IN_CONSTANT_P
))
365 lhs
= gimple_call_lhs (stmt
);
367 if (!lhs
|| TREE_CODE (lhs
) != SSA_NAME
)
370 FOR_EACH_IMM_USE_FAST (use_p
, use_iter
, lhs
)
373 basic_block use_bb
, forbidden_bb
;
375 edge true_edge
, false_edge
;
378 use_stmt
= dyn_cast
<gcond
*> (USE_STMT (use_p
));
382 /* Assuming canonical form for GIMPLE_COND here, with constant
383 in second position. */
384 op1
= gimple_cond_rhs (use_stmt
);
385 code
= gimple_cond_code (use_stmt
);
386 use_bb
= gimple_bb (use_stmt
);
388 extract_true_false_edges_from_block (use_bb
, &true_edge
, &false_edge
);
390 /* We're only interested in comparisons that distinguish
391 unambiguously from zero. */
392 if (!integer_zerop (op1
) || code
== LE_EXPR
|| code
== GE_EXPR
)
396 forbidden_bb
= false_edge
->dest
;
398 forbidden_bb
= true_edge
->dest
;
400 bitmap_set_bit (forbidden_dominators
, forbidden_bb
->index
);
404 /* If BB is dominated by any block in the forbidden dominators set,
405 return TRUE; else FALSE. */
408 dominated_by_forbidden (basic_block bb
)
413 EXECUTE_IF_SET_IN_BITMAP (forbidden_dominators
, 1, dom_bb
, bi
)
415 if (dominated_by_p (CDI_DOMINATORS
, bb
,
416 BASIC_BLOCK_FOR_FN (cfun
, dom_bb
)))
423 /* For give split point CURRENT and return block RETURN_BB return 1
424 if ssa name VAL is set by split part and 0 otherwise. */
426 split_part_set_ssa_name_p (tree val
, struct split_point
*current
,
427 basic_block return_bb
)
429 if (TREE_CODE (val
) != SSA_NAME
)
432 return (!SSA_NAME_IS_DEFAULT_DEF (val
)
433 && (bitmap_bit_p (current
->split_bbs
,
434 gimple_bb (SSA_NAME_DEF_STMT (val
))->index
)
435 || gimple_bb (SSA_NAME_DEF_STMT (val
)) == return_bb
));
438 /* We found an split_point CURRENT. NON_SSA_VARS is bitmap of all non ssa
439 variables used and RETURN_BB is return basic block.
440 See if we can split function here. */
443 consider_split (struct split_point
*current
, bitmap non_ssa_vars
,
444 basic_block return_bb
)
447 unsigned int num_args
= 0;
448 unsigned int call_overhead
;
453 int incoming_freq
= 0;
456 bool back_edge
= false;
458 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
459 dump_split_point (dump_file
, current
);
461 FOR_EACH_EDGE (e
, ei
, current
->entry_bb
->preds
)
463 if (e
->flags
& EDGE_DFS_BACK
)
465 if (!bitmap_bit_p (current
->split_bbs
, e
->src
->index
))
466 incoming_freq
+= EDGE_FREQUENCY (e
);
469 /* Do not split when we would end up calling function anyway. */
471 >= (ENTRY_BLOCK_PTR_FOR_FN (cfun
)->frequency
472 * PARAM_VALUE (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY
) / 100))
474 /* When profile is guessed, we can not expect it to give us
475 realistic estimate on likelyness of function taking the
476 complex path. As a special case, when tail of the function is
477 a loop, enable splitting since inlining code skipping the loop
478 is likely noticeable win. */
480 && profile_status_for_fn (cfun
) != PROFILE_READ
481 && incoming_freq
< ENTRY_BLOCK_PTR_FOR_FN (cfun
)->frequency
)
483 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
485 " Split before loop, accepting despite low frequencies %i %i.\n",
487 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->frequency
);
491 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
493 " Refused: incoming frequency is too large.\n");
498 if (!current
->header_size
)
500 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
501 fprintf (dump_file
, " Refused: header empty\n");
505 /* Verify that PHI args on entry are either virtual or all their operands
506 incoming from header are the same. */
507 for (bsi
= gsi_start_phis (current
->entry_bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
509 gphi
*stmt
= bsi
.phi ();
512 if (virtual_operand_p (gimple_phi_result (stmt
)))
514 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
516 edge e
= gimple_phi_arg_edge (stmt
, i
);
517 if (!bitmap_bit_p (current
->split_bbs
, e
->src
->index
))
519 tree edge_val
= gimple_phi_arg_def (stmt
, i
);
520 if (val
&& edge_val
!= val
)
522 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
524 " Refused: entry BB has PHI with multiple variants\n");
533 /* See what argument we will pass to the split function and compute
535 call_overhead
= eni_size_weights
.call_cost
;
536 for (parm
= DECL_ARGUMENTS (current_function_decl
); parm
;
537 parm
= DECL_CHAIN (parm
))
539 if (!is_gimple_reg (parm
))
541 if (bitmap_bit_p (non_ssa_vars
, DECL_UID (parm
)))
543 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
545 " Refused: need to pass non-ssa param values\n");
551 tree ddef
= ssa_default_def (cfun
, parm
);
553 && bitmap_bit_p (current
->ssa_names_to_pass
,
554 SSA_NAME_VERSION (ddef
)))
556 if (!VOID_TYPE_P (TREE_TYPE (parm
)))
557 call_overhead
+= estimate_move_cost (TREE_TYPE (parm
), false);
562 if (!VOID_TYPE_P (TREE_TYPE (current_function_decl
)))
563 call_overhead
+= estimate_move_cost (TREE_TYPE (current_function_decl
),
566 if (current
->split_size
<= call_overhead
)
568 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
570 " Refused: split size is smaller than call overhead\n");
573 if (current
->header_size
+ call_overhead
574 >= (unsigned int)(DECL_DECLARED_INLINE_P (current_function_decl
)
575 ? MAX_INLINE_INSNS_SINGLE
576 : MAX_INLINE_INSNS_AUTO
))
578 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
580 " Refused: header size is too large for inline candidate\n");
584 /* FIXME: we currently can pass only SSA function parameters to the split
585 arguments. Once parm_adjustment infrastructure is supported by cloning,
586 we can pass more than that. */
587 if (num_args
!= bitmap_count_bits (current
->ssa_names_to_pass
))
590 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
592 " Refused: need to pass non-param values\n");
596 /* When there are non-ssa vars used in the split region, see if they
597 are used in the header region. If so, reject the split.
598 FIXME: we can use nested function support to access both. */
599 if (!bitmap_empty_p (non_ssa_vars
)
600 && !verify_non_ssa_vars (current
, non_ssa_vars
, return_bb
))
602 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
604 " Refused: split part has non-ssa uses\n");
608 /* If the split point is dominated by a forbidden block, reject
610 if (!bitmap_empty_p (forbidden_dominators
)
611 && dominated_by_forbidden (current
->entry_bb
))
613 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
615 " Refused: split point dominated by forbidden block\n");
619 /* See if retval used by return bb is computed by header or split part.
620 When it is computed by split part, we need to produce return statement
621 in the split part and add code to header to pass it around.
623 This is bit tricky to test:
624 1) When there is no return_bb or no return value, we always pass
626 2) Invariants are always computed by caller.
627 3) For SSA we need to look if defining statement is in header or split part
628 4) For non-SSA we need to look where the var is computed. */
629 retval
= find_retval (return_bb
);
631 current
->split_part_set_retval
= true;
632 else if (is_gimple_min_invariant (retval
))
633 current
->split_part_set_retval
= false;
634 /* Special case is value returned by reference we record as if it was non-ssa
635 set to result_decl. */
636 else if (TREE_CODE (retval
) == SSA_NAME
637 && SSA_NAME_VAR (retval
)
638 && TREE_CODE (SSA_NAME_VAR (retval
)) == RESULT_DECL
639 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
640 current
->split_part_set_retval
641 = bitmap_bit_p (non_ssa_vars
, DECL_UID (SSA_NAME_VAR (retval
)));
642 else if (TREE_CODE (retval
) == SSA_NAME
)
643 current
->split_part_set_retval
644 = split_part_set_ssa_name_p (retval
, current
, return_bb
);
645 else if (TREE_CODE (retval
) == PARM_DECL
)
646 current
->split_part_set_retval
= false;
647 else if (TREE_CODE (retval
) == VAR_DECL
648 || TREE_CODE (retval
) == RESULT_DECL
)
649 current
->split_part_set_retval
650 = bitmap_bit_p (non_ssa_vars
, DECL_UID (retval
));
652 current
->split_part_set_retval
= true;
654 /* See if retbnd used by return bb is computed by header or split part. */
655 retbnd
= find_retbnd (return_bb
);
658 bool split_part_set_retbnd
659 = split_part_set_ssa_name_p (retbnd
, current
, return_bb
);
661 /* If we have both return value and bounds then keep their definitions
662 in a single function. We use SSA names to link returned bounds and
663 value and therefore do not handle cases when result is passed by
664 reference (which should not be our case anyway since bounds are
665 returned for pointers only). */
666 if ((DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
))
667 && current
->split_part_set_retval
)
668 || split_part_set_retbnd
!= current
->split_part_set_retval
)
670 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
672 " Refused: split point splits return value and bounds\n");
677 /* split_function fixes up at most one PHI non-virtual PHI node in return_bb,
678 for the return value. If there are other PHIs, give up. */
679 if (return_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
683 for (psi
= gsi_start_phis (return_bb
); !gsi_end_p (psi
); gsi_next (&psi
))
684 if (!virtual_operand_p (gimple_phi_result (psi
.phi ()))
686 && current
->split_part_set_retval
687 && TREE_CODE (retval
) == SSA_NAME
688 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
))
689 && SSA_NAME_DEF_STMT (retval
) == psi
.phi ()))
691 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
693 " Refused: return bb has extra PHIs\n");
698 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
699 fprintf (dump_file
, " Accepted!\n");
701 /* At the moment chose split point with lowest frequency and that leaves
702 out smallest size of header.
703 In future we might re-consider this heuristics. */
704 if (!best_split_point
.split_bbs
705 || best_split_point
.entry_bb
->frequency
> current
->entry_bb
->frequency
706 || (best_split_point
.entry_bb
->frequency
== current
->entry_bb
->frequency
707 && best_split_point
.split_size
< current
->split_size
))
710 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
711 fprintf (dump_file
, " New best split point!\n");
712 if (best_split_point
.ssa_names_to_pass
)
714 BITMAP_FREE (best_split_point
.ssa_names_to_pass
);
715 BITMAP_FREE (best_split_point
.split_bbs
);
717 best_split_point
= *current
;
718 best_split_point
.ssa_names_to_pass
= BITMAP_ALLOC (NULL
);
719 bitmap_copy (best_split_point
.ssa_names_to_pass
,
720 current
->ssa_names_to_pass
);
721 best_split_point
.split_bbs
= BITMAP_ALLOC (NULL
);
722 bitmap_copy (best_split_point
.split_bbs
, current
->split_bbs
);
726 /* Return basic block containing RETURN statement. We allow basic blocks
730 but return_bb can not be more complex than this.
731 If nothing is found, return the exit block.
733 When there are multiple RETURN statement, chose one with return value,
734 since that one is more likely shared by multiple code paths.
736 Return BB is special, because for function splitting it is the only
737 basic block that is duplicated in between header and split part of the
740 TODO: We might support multiple return blocks. */
743 find_return_bb (void)
746 basic_block return_bb
= EXIT_BLOCK_PTR_FOR_FN (cfun
);
747 gimple_stmt_iterator bsi
;
748 bool found_return
= false;
749 tree retval
= NULL_TREE
;
751 if (!single_pred_p (EXIT_BLOCK_PTR_FOR_FN (cfun
)))
754 e
= single_pred_edge (EXIT_BLOCK_PTR_FOR_FN (cfun
));
755 for (bsi
= gsi_last_bb (e
->src
); !gsi_end_p (bsi
); gsi_prev (&bsi
))
757 gimple stmt
= gsi_stmt (bsi
);
758 if (gimple_code (stmt
) == GIMPLE_LABEL
759 || is_gimple_debug (stmt
)
760 || gimple_clobber_p (stmt
))
762 else if (gimple_code (stmt
) == GIMPLE_ASSIGN
764 && gimple_assign_single_p (stmt
)
765 && (auto_var_in_fn_p (gimple_assign_rhs1 (stmt
),
766 current_function_decl
)
767 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
768 && retval
== gimple_assign_lhs (stmt
))
770 else if (greturn
*return_stmt
= dyn_cast
<greturn
*> (stmt
))
773 retval
= gimple_return_retval (return_stmt
);
778 if (gsi_end_p (bsi
) && found_return
)
784 /* Given return basic block RETURN_BB, see where return value is really
787 find_retval (basic_block return_bb
)
789 gimple_stmt_iterator bsi
;
790 for (bsi
= gsi_start_bb (return_bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
791 if (greturn
*return_stmt
= dyn_cast
<greturn
*> (gsi_stmt (bsi
)))
792 return gimple_return_retval (return_stmt
);
793 else if (gimple_code (gsi_stmt (bsi
)) == GIMPLE_ASSIGN
794 && !gimple_clobber_p (gsi_stmt (bsi
)))
795 return gimple_assign_rhs1 (gsi_stmt (bsi
));
799 /* Given return basic block RETURN_BB, see where return bounds are really
802 find_retbnd (basic_block return_bb
)
804 gimple_stmt_iterator bsi
;
805 for (bsi
= gsi_last_bb (return_bb
); !gsi_end_p (bsi
); gsi_prev (&bsi
))
806 if (gimple_code (gsi_stmt (bsi
)) == GIMPLE_RETURN
)
807 return gimple_return_retbnd (gsi_stmt (bsi
));
811 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
812 variable, mark it as used in bitmap passed via DATA.
813 Return true when access to T prevents splitting the function. */
816 mark_nonssa_use (gimple
, tree t
, tree
, void *data
)
818 t
= get_base_address (t
);
820 if (!t
|| is_gimple_reg (t
))
823 /* At present we can't pass non-SSA arguments to split function.
824 FIXME: this can be relaxed by passing references to arguments. */
825 if (TREE_CODE (t
) == PARM_DECL
)
827 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
829 "Cannot split: use of non-ssa function parameter.\n");
833 if ((TREE_CODE (t
) == VAR_DECL
834 && auto_var_in_fn_p (t
, current_function_decl
))
835 || TREE_CODE (t
) == RESULT_DECL
836 || (TREE_CODE (t
) == LABEL_DECL
837 && FORCED_LABEL (t
)))
838 bitmap_set_bit ((bitmap
)data
, DECL_UID (t
));
840 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
841 to pretend that the value pointed to is actual result decl. */
842 if ((TREE_CODE (t
) == MEM_REF
|| INDIRECT_REF_P (t
))
843 && TREE_CODE (TREE_OPERAND (t
, 0)) == SSA_NAME
844 && SSA_NAME_VAR (TREE_OPERAND (t
, 0))
845 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t
, 0))) == RESULT_DECL
846 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
848 bitmap_bit_p ((bitmap
)data
,
849 DECL_UID (DECL_RESULT (current_function_decl
)));
854 /* Compute local properties of basic block BB we collect when looking for
855 split points. We look for ssa defs and store them in SET_SSA_NAMES,
856 for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic
857 vars stored in NON_SSA_VARS.
859 When BB has edge to RETURN_BB, collect uses in RETURN_BB too.
861 Return false when BB contains something that prevents it from being put into
865 visit_bb (basic_block bb
, basic_block return_bb
,
866 bitmap set_ssa_names
, bitmap used_ssa_names
,
871 bool can_split
= true;
873 for (gimple_stmt_iterator bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
);
876 gimple stmt
= gsi_stmt (bsi
);
881 if (is_gimple_debug (stmt
))
884 if (gimple_clobber_p (stmt
))
887 /* FIXME: We can split regions containing EH. We can not however
888 split RESX, EH_DISPATCH and EH_POINTER referring to same region
889 into different partitions. This would require tracking of
890 EH regions and checking in consider_split_point if they
891 are not used elsewhere. */
892 if (gimple_code (stmt
) == GIMPLE_RESX
)
894 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
895 fprintf (dump_file
, "Cannot split: resx.\n");
898 if (gimple_code (stmt
) == GIMPLE_EH_DISPATCH
)
900 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
901 fprintf (dump_file
, "Cannot split: eh dispatch.\n");
905 /* Check builtins that prevent splitting. */
906 if (gimple_code (stmt
) == GIMPLE_CALL
907 && (decl
= gimple_call_fndecl (stmt
)) != NULL_TREE
908 && DECL_BUILT_IN (decl
)
909 && DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
910 switch (DECL_FUNCTION_CODE (decl
))
912 /* FIXME: once we will allow passing non-parm values to split part,
913 we need to be sure to handle correct builtin_stack_save and
914 builtin_stack_restore. At the moment we are safe; there is no
915 way to store builtin_stack_save result in non-SSA variable
916 since all calls to those are compiler generated. */
918 case BUILT_IN_APPLY_ARGS
:
919 case BUILT_IN_VA_START
:
920 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
922 "Cannot split: builtin_apply and va_start.\n");
925 case BUILT_IN_EH_POINTER
:
926 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
927 fprintf (dump_file
, "Cannot split: builtin_eh_pointer.\n");
934 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_DEF
)
935 bitmap_set_bit (set_ssa_names
, SSA_NAME_VERSION (op
));
936 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
937 bitmap_set_bit (used_ssa_names
, SSA_NAME_VERSION (op
));
938 can_split
&= !walk_stmt_load_store_addr_ops (stmt
, non_ssa_vars
,
943 for (gphi_iterator bsi
= gsi_start_phis (bb
); !gsi_end_p (bsi
);
946 gphi
*stmt
= bsi
.phi ();
949 if (virtual_operand_p (gimple_phi_result (stmt
)))
951 bitmap_set_bit (set_ssa_names
,
952 SSA_NAME_VERSION (gimple_phi_result (stmt
)));
953 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
955 tree op
= gimple_phi_arg_def (stmt
, i
);
956 if (TREE_CODE (op
) == SSA_NAME
)
957 bitmap_set_bit (used_ssa_names
, SSA_NAME_VERSION (op
));
959 can_split
&= !walk_stmt_load_store_addr_ops (stmt
, non_ssa_vars
,
964 /* Record also uses coming from PHI operand in return BB. */
965 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
966 if (e
->dest
== return_bb
)
968 for (gphi_iterator bsi
= gsi_start_phis (return_bb
);
972 gphi
*stmt
= bsi
.phi ();
973 tree op
= gimple_phi_arg_def (stmt
, e
->dest_idx
);
975 if (virtual_operand_p (gimple_phi_result (stmt
)))
977 if (TREE_CODE (op
) == SSA_NAME
)
978 bitmap_set_bit (used_ssa_names
, SSA_NAME_VERSION (op
));
980 can_split
&= !mark_nonssa_use (stmt
, op
, op
, non_ssa_vars
);
986 /* Stack entry for recursive DFS walk in find_split_point. */
990 /* Basic block we are examining. */
993 /* SSA names set and used by the BB and all BBs reachable
994 from it via DFS walk. */
995 bitmap set_ssa_names
, used_ssa_names
;
998 /* All BBS visited from this BB via DFS walk. */
1001 /* Last examined edge in DFS walk. Since we walk unoriented graph,
1002 the value is up to sum of incoming and outgoing edges of BB. */
1003 unsigned int edge_num
;
1005 /* Stack entry index of earliest BB reachable from current BB
1006 or any BB visited later in DFS walk. */
1009 /* Overall time and size of all BBs reached from this BB in DFS walk. */
1010 int overall_time
, overall_size
;
1012 /* When false we can not split on this BB. */
1017 /* Find all articulations and call consider_split on them.
1018 OVERALL_TIME and OVERALL_SIZE is time and size of the function.
1020 We perform basic algorithm for finding an articulation in a graph
1021 created from CFG by considering it to be an unoriented graph.
1023 The articulation is discovered via DFS walk. We collect earliest
1024 basic block on stack that is reachable via backward edge. Articulation
1025 is any basic block such that there is no backward edge bypassing it.
1026 To reduce stack usage we maintain heap allocated stack in STACK vector.
1027 AUX pointer of BB is set to index it appears in the stack or -1 once
1028 it is visited and popped off the stack.
1030 The algorithm finds articulation after visiting the whole component
1031 reachable by it. This makes it convenient to collect information about
1032 the component used by consider_split. */
1035 find_split_points (int overall_time
, int overall_size
)
1038 vec
<stack_entry
> stack
= vNULL
;
1040 basic_block return_bb
= find_return_bb ();
1041 struct split_point current
;
1043 current
.header_time
= overall_time
;
1044 current
.header_size
= overall_size
;
1045 current
.split_time
= 0;
1046 current
.split_size
= 0;
1047 current
.ssa_names_to_pass
= BITMAP_ALLOC (NULL
);
1049 first
.bb
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
1051 first
.overall_time
= 0;
1052 first
.overall_size
= 0;
1053 first
.earliest
= INT_MAX
;
1054 first
.set_ssa_names
= 0;
1055 first
.used_ssa_names
= 0;
1056 first
.non_ssa_vars
= 0;
1057 first
.bbs_visited
= 0;
1058 first
.can_split
= false;
1059 stack
.safe_push (first
);
1060 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->aux
= (void *)(intptr_t)-1;
1062 while (!stack
.is_empty ())
1064 stack_entry
*entry
= &stack
.last ();
1066 /* We are walking an acyclic graph, so edge_num counts
1067 succ and pred edges together. However when considering
1068 articulation, we want to have processed everything reachable
1069 from articulation but nothing that reaches into it. */
1070 if (entry
->edge_num
== EDGE_COUNT (entry
->bb
->succs
)
1071 && entry
->bb
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
))
1073 int pos
= stack
.length ();
1074 entry
->can_split
&= visit_bb (entry
->bb
, return_bb
,
1075 entry
->set_ssa_names
,
1076 entry
->used_ssa_names
,
1077 entry
->non_ssa_vars
);
1078 if (pos
<= entry
->earliest
&& !entry
->can_split
1079 && dump_file
&& (dump_flags
& TDF_DETAILS
))
1081 "found articulation at bb %i but can not split\n",
1083 if (pos
<= entry
->earliest
&& entry
->can_split
)
1085 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1086 fprintf (dump_file
, "found articulation at bb %i\n",
1088 current
.entry_bb
= entry
->bb
;
1089 current
.ssa_names_to_pass
= BITMAP_ALLOC (NULL
);
1090 bitmap_and_compl (current
.ssa_names_to_pass
,
1091 entry
->used_ssa_names
, entry
->set_ssa_names
);
1092 current
.header_time
= overall_time
- entry
->overall_time
;
1093 current
.header_size
= overall_size
- entry
->overall_size
;
1094 current
.split_time
= entry
->overall_time
;
1095 current
.split_size
= entry
->overall_size
;
1096 current
.split_bbs
= entry
->bbs_visited
;
1097 consider_split (¤t
, entry
->non_ssa_vars
, return_bb
);
1098 BITMAP_FREE (current
.ssa_names_to_pass
);
1101 /* Do actual DFS walk. */
1103 < (EDGE_COUNT (entry
->bb
->succs
)
1104 + EDGE_COUNT (entry
->bb
->preds
)))
1108 if (entry
->edge_num
< EDGE_COUNT (entry
->bb
->succs
))
1110 e
= EDGE_SUCC (entry
->bb
, entry
->edge_num
);
1115 e
= EDGE_PRED (entry
->bb
, entry
->edge_num
1116 - EDGE_COUNT (entry
->bb
->succs
));
1122 /* New BB to visit, push it to the stack. */
1123 if (dest
!= return_bb
&& dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
)
1126 stack_entry new_entry
;
1128 new_entry
.bb
= dest
;
1129 new_entry
.edge_num
= 0;
1130 new_entry
.overall_time
1131 = bb_info_vec
[dest
->index
].time
;
1132 new_entry
.overall_size
1133 = bb_info_vec
[dest
->index
].size
;
1134 new_entry
.earliest
= INT_MAX
;
1135 new_entry
.set_ssa_names
= BITMAP_ALLOC (NULL
);
1136 new_entry
.used_ssa_names
= BITMAP_ALLOC (NULL
);
1137 new_entry
.bbs_visited
= BITMAP_ALLOC (NULL
);
1138 new_entry
.non_ssa_vars
= BITMAP_ALLOC (NULL
);
1139 new_entry
.can_split
= true;
1140 bitmap_set_bit (new_entry
.bbs_visited
, dest
->index
);
1141 stack
.safe_push (new_entry
);
1142 dest
->aux
= (void *)(intptr_t)stack
.length ();
1144 /* Back edge found, record the earliest point. */
1145 else if ((intptr_t)dest
->aux
> 0
1146 && (intptr_t)dest
->aux
< entry
->earliest
)
1147 entry
->earliest
= (intptr_t)dest
->aux
;
1149 /* We are done with examining the edges. Pop off the value from stack
1150 and merge stuff we accumulate during the walk. */
1151 else if (entry
->bb
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
))
1153 stack_entry
*prev
= &stack
[stack
.length () - 2];
1155 entry
->bb
->aux
= (void *)(intptr_t)-1;
1156 prev
->can_split
&= entry
->can_split
;
1157 if (prev
->set_ssa_names
)
1159 bitmap_ior_into (prev
->set_ssa_names
, entry
->set_ssa_names
);
1160 bitmap_ior_into (prev
->used_ssa_names
, entry
->used_ssa_names
);
1161 bitmap_ior_into (prev
->bbs_visited
, entry
->bbs_visited
);
1162 bitmap_ior_into (prev
->non_ssa_vars
, entry
->non_ssa_vars
);
1164 if (prev
->earliest
> entry
->earliest
)
1165 prev
->earliest
= entry
->earliest
;
1166 prev
->overall_time
+= entry
->overall_time
;
1167 prev
->overall_size
+= entry
->overall_size
;
1168 BITMAP_FREE (entry
->set_ssa_names
);
1169 BITMAP_FREE (entry
->used_ssa_names
);
1170 BITMAP_FREE (entry
->bbs_visited
);
1171 BITMAP_FREE (entry
->non_ssa_vars
);
1177 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->aux
= NULL
;
1178 FOR_EACH_BB_FN (bb
, cfun
)
1181 BITMAP_FREE (current
.ssa_names_to_pass
);
1184 /* Build and insert initialization of returned bounds RETBND
1185 for returned value RETVAL. Statements are inserted after
1186 a statement pointed by GSI and GSI is modified to point to
1187 the last inserted statement. */
1190 insert_bndret_call_after (tree retbnd
, tree retval
, gimple_stmt_iterator
*gsi
)
1192 tree fndecl
= targetm
.builtin_chkp_function (BUILT_IN_CHKP_BNDRET
);
1193 gimple bndret
= gimple_build_call (fndecl
, 1, retval
);
1194 gimple_call_set_lhs (bndret
, retbnd
);
1195 gsi_insert_after (gsi
, bndret
, GSI_CONTINUE_LINKING
);
1197 /* Split function at SPLIT_POINT. */
1200 split_function (struct split_point
*split_point
)
1202 vec
<tree
> args_to_pass
= vNULL
;
1203 bitmap args_to_skip
;
1206 cgraph_node
*node
, *cur_node
= cgraph_node::get (current_function_decl
);
1207 basic_block return_bb
= find_return_bb ();
1208 basic_block call_bb
;
1212 tree retval
= NULL
, real_retval
= NULL
, retbnd
= NULL
;
1213 bool split_part_return_p
= false;
1214 bool with_bounds
= chkp_function_instrumented_p (current_function_decl
);
1215 gimple last_stmt
= NULL
;
1218 vec
<tree
, va_gc
> **debug_args
= NULL
;
1222 fprintf (dump_file
, "\n\nSplitting function at:\n");
1223 dump_split_point (dump_file
, split_point
);
1226 if (cur_node
->local
.can_change_signature
)
1227 args_to_skip
= BITMAP_ALLOC (NULL
);
1229 args_to_skip
= NULL
;
1231 /* Collect the parameters of new function and args_to_skip bitmap. */
1232 for (parm
= DECL_ARGUMENTS (current_function_decl
);
1233 parm
; parm
= DECL_CHAIN (parm
), num
++)
1235 && (!is_gimple_reg (parm
)
1236 || (ddef
= ssa_default_def (cfun
, parm
)) == NULL_TREE
1237 || !bitmap_bit_p (split_point
->ssa_names_to_pass
,
1238 SSA_NAME_VERSION (ddef
))))
1239 bitmap_set_bit (args_to_skip
, num
);
1242 /* This parm might not have been used up to now, but is going to be
1243 used, hence register it. */
1244 if (is_gimple_reg (parm
))
1245 arg
= get_or_create_ssa_default_def (cfun
, parm
);
1249 if (!useless_type_conversion_p (DECL_ARG_TYPE (parm
), TREE_TYPE (arg
)))
1250 arg
= fold_convert (DECL_ARG_TYPE (parm
), arg
);
1251 args_to_pass
.safe_push (arg
);
1254 /* See if the split function will return. */
1255 FOR_EACH_EDGE (e
, ei
, return_bb
->preds
)
1256 if (bitmap_bit_p (split_point
->split_bbs
, e
->src
->index
))
1259 split_part_return_p
= true;
1261 /* Add return block to what will become the split function.
1262 We do not return; no return block is needed. */
1263 if (!split_part_return_p
)
1265 /* We have no return block, so nothing is needed. */
1266 else if (return_bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
1268 /* When we do not want to return value, we need to construct
1269 new return block with empty return statement.
1270 FIXME: Once we are able to change return type, we should change function
1271 to return void instead of just outputting function with undefined return
1272 value. For structures this affects quality of codegen. */
1273 else if (!split_point
->split_part_set_retval
1274 && find_retval (return_bb
))
1276 bool redirected
= true;
1277 basic_block new_return_bb
= create_basic_block (NULL
, 0, return_bb
);
1278 gimple_stmt_iterator gsi
= gsi_start_bb (new_return_bb
);
1279 gsi_insert_after (&gsi
, gimple_build_return (NULL
), GSI_NEW_STMT
);
1283 FOR_EACH_EDGE (e
, ei
, return_bb
->preds
)
1284 if (bitmap_bit_p (split_point
->split_bbs
, e
->src
->index
))
1286 new_return_bb
->count
+= e
->count
;
1287 new_return_bb
->frequency
+= EDGE_FREQUENCY (e
);
1288 redirect_edge_and_branch (e
, new_return_bb
);
1293 e
= make_edge (new_return_bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
), 0);
1294 e
->probability
= REG_BR_PROB_BASE
;
1295 e
->count
= new_return_bb
->count
;
1296 add_bb_to_loop (new_return_bb
, current_loops
->tree_root
);
1297 bitmap_set_bit (split_point
->split_bbs
, new_return_bb
->index
);
1299 /* When we pass around the value, use existing return block. */
1301 bitmap_set_bit (split_point
->split_bbs
, return_bb
->index
);
1303 /* If RETURN_BB has virtual operand PHIs, they must be removed and the
1304 virtual operand marked for renaming as we change the CFG in a way that
1305 tree-inline is not able to compensate for.
1307 Note this can happen whether or not we have a return value. If we have
1308 a return value, then RETURN_BB may have PHIs for real operands too. */
1309 if (return_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
1312 for (gphi_iterator gsi
= gsi_start_phis (return_bb
);
1315 gphi
*stmt
= gsi
.phi ();
1316 if (!virtual_operand_p (gimple_phi_result (stmt
)))
1321 mark_virtual_phi_result_for_renaming (stmt
);
1322 remove_phi_node (&gsi
, true);
1325 /* In reality we have to rename the reaching definition of the
1326 virtual operand at return_bb as we will eventually release it
1327 when we remove the code region we outlined.
1328 So we have to rename all immediate virtual uses of that region
1329 if we didn't see a PHI definition yet. */
1330 /* ??? In real reality we want to set the reaching vdef of the
1331 entry of the SESE region as the vuse of the call and the reaching
1332 vdef of the exit of the SESE region as the vdef of the call. */
1334 for (gimple_stmt_iterator gsi
= gsi_start_bb (return_bb
);
1338 gimple stmt
= gsi_stmt (gsi
);
1339 if (gimple_vuse (stmt
))
1341 gimple_set_vuse (stmt
, NULL_TREE
);
1344 if (gimple_vdef (stmt
))
1349 /* Now create the actual clone. */
1350 cgraph_edge::rebuild_edges ();
1351 node
= cur_node
->create_version_clone_with_body
1352 (vNULL
, NULL
, args_to_skip
, !split_part_return_p
, split_point
->split_bbs
,
1353 split_point
->entry_bb
, "part");
1355 /* Let's take a time profile for splitted function. */
1356 node
->tp_first_run
= cur_node
->tp_first_run
+ 1;
1358 /* For usual cloning it is enough to clear builtin only when signature
1359 changes. For partial inlining we however can not expect the part
1360 of builtin implementation to have same semantic as the whole. */
1361 if (DECL_BUILT_IN (node
->decl
))
1363 DECL_BUILT_IN_CLASS (node
->decl
) = NOT_BUILT_IN
;
1364 DECL_FUNCTION_CODE (node
->decl
) = (enum built_in_function
) 0;
1367 /* If the original function is instrumented then it's
1368 part is also instrumented. */
1370 chkp_function_mark_instrumented (node
->decl
);
1372 /* If the original function is declared inline, there is no point in issuing
1373 a warning for the non-inlinable part. */
1374 DECL_NO_INLINE_WARNING_P (node
->decl
) = 1;
1375 cur_node
->remove_callees ();
1376 cur_node
->remove_all_references ();
1377 if (!split_part_return_p
)
1378 TREE_THIS_VOLATILE (node
->decl
) = 1;
1380 dump_function_to_file (node
->decl
, dump_file
, dump_flags
);
1382 /* Create the basic block we place call into. It is the entry basic block
1383 split after last label. */
1384 call_bb
= split_point
->entry_bb
;
1385 for (gimple_stmt_iterator gsi
= gsi_start_bb (call_bb
); !gsi_end_p (gsi
);)
1386 if (gimple_code (gsi_stmt (gsi
)) == GIMPLE_LABEL
)
1388 last_stmt
= gsi_stmt (gsi
);
1393 e
= split_block (split_point
->entry_bb
, last_stmt
);
1396 /* Produce the call statement. */
1397 gimple_stmt_iterator gsi
= gsi_last_bb (call_bb
);
1398 FOR_EACH_VEC_ELT (args_to_pass
, i
, arg
)
1399 if (!is_gimple_val (arg
))
1401 arg
= force_gimple_operand_gsi (&gsi
, arg
, true, NULL_TREE
,
1402 false, GSI_CONTINUE_LINKING
);
1403 args_to_pass
[i
] = arg
;
1405 call
= gimple_build_call_vec (node
->decl
, args_to_pass
);
1406 gimple_call_set_with_bounds (call
, with_bounds
);
1407 gimple_set_block (call
, DECL_INITIAL (current_function_decl
));
1408 args_to_pass
.release ();
1410 /* For optimized away parameters, add on the caller side
1412 DEBUG D#X => parm_Y(D)
1413 stmts and associate D#X with parm in decl_debug_args_lookup
1414 vector to say for debug info that if parameter parm had been passed,
1415 it would have value parm_Y(D). */
1417 for (parm
= DECL_ARGUMENTS (current_function_decl
), num
= 0;
1418 parm
; parm
= DECL_CHAIN (parm
), num
++)
1419 if (bitmap_bit_p (args_to_skip
, num
)
1420 && is_gimple_reg (parm
))
1425 /* This needs to be done even without MAY_HAVE_DEBUG_STMTS,
1426 otherwise if it didn't exist before, we'd end up with
1427 different SSA_NAME_VERSIONs between -g and -g0. */
1428 arg
= get_or_create_ssa_default_def (cfun
, parm
);
1429 if (!MAY_HAVE_DEBUG_STMTS
)
1432 if (debug_args
== NULL
)
1433 debug_args
= decl_debug_args_insert (node
->decl
);
1434 ddecl
= make_node (DEBUG_EXPR_DECL
);
1435 DECL_ARTIFICIAL (ddecl
) = 1;
1436 TREE_TYPE (ddecl
) = TREE_TYPE (parm
);
1437 DECL_MODE (ddecl
) = DECL_MODE (parm
);
1438 vec_safe_push (*debug_args
, DECL_ORIGIN (parm
));
1439 vec_safe_push (*debug_args
, ddecl
);
1440 def_temp
= gimple_build_debug_bind (ddecl
, unshare_expr (arg
),
1442 gsi_insert_after (&gsi
, def_temp
, GSI_NEW_STMT
);
1444 /* And on the callee side, add
1447 stmts to the first bb where var is a VAR_DECL created for the
1448 optimized away parameter in DECL_INITIAL block. This hints
1449 in the debug info that var (whole DECL_ORIGIN is the parm PARM_DECL)
1450 is optimized away, but could be looked up at the call site
1451 as value of D#X there. */
1452 if (debug_args
!= NULL
)
1456 gimple_stmt_iterator cgsi
;
1459 push_cfun (DECL_STRUCT_FUNCTION (node
->decl
));
1460 var
= BLOCK_VARS (DECL_INITIAL (node
->decl
));
1461 i
= vec_safe_length (*debug_args
);
1462 cgsi
= gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
)));
1466 while (var
!= NULL_TREE
1467 && DECL_ABSTRACT_ORIGIN (var
) != (**debug_args
)[i
])
1468 var
= TREE_CHAIN (var
);
1469 if (var
== NULL_TREE
)
1471 vexpr
= make_node (DEBUG_EXPR_DECL
);
1472 parm
= (**debug_args
)[i
];
1473 DECL_ARTIFICIAL (vexpr
) = 1;
1474 TREE_TYPE (vexpr
) = TREE_TYPE (parm
);
1475 DECL_MODE (vexpr
) = DECL_MODE (parm
);
1476 def_temp
= gimple_build_debug_source_bind (vexpr
, parm
,
1478 gsi_insert_before (&cgsi
, def_temp
, GSI_SAME_STMT
);
1479 def_temp
= gimple_build_debug_bind (var
, vexpr
, NULL
);
1480 gsi_insert_before (&cgsi
, def_temp
, GSI_SAME_STMT
);
1486 /* We avoid address being taken on any variable used by split part,
1487 so return slot optimization is always possible. Moreover this is
1488 required to make DECL_BY_REFERENCE work. */
1489 if (aggregate_value_p (DECL_RESULT (current_function_decl
),
1490 TREE_TYPE (current_function_decl
))
1491 && (!is_gimple_reg_type (TREE_TYPE (DECL_RESULT (current_function_decl
)))
1492 || DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
))))
1493 gimple_call_set_return_slot_opt (call
, true);
1495 /* Update return value. This is bit tricky. When we do not return,
1496 do nothing. When we return we might need to update return_bb
1497 or produce a new return statement. */
1498 if (!split_part_return_p
)
1499 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1502 e
= make_edge (call_bb
, return_bb
,
1503 return_bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
)
1504 ? 0 : EDGE_FALLTHRU
);
1505 e
->count
= call_bb
->count
;
1506 e
->probability
= REG_BR_PROB_BASE
;
1508 /* If there is return basic block, see what value we need to store
1509 return value into and put call just before it. */
1510 if (return_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
1512 real_retval
= retval
= find_retval (return_bb
);
1513 retbnd
= find_retbnd (return_bb
);
1515 if (real_retval
&& split_point
->split_part_set_retval
)
1519 /* See if we need new SSA_NAME for the result.
1520 When DECL_BY_REFERENCE is true, retval is actually pointer to
1521 return value and it is constant in whole function. */
1522 if (TREE_CODE (retval
) == SSA_NAME
1523 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
1525 retval
= copy_ssa_name (retval
, call
);
1527 /* See if there is PHI defining return value. */
1528 for (psi
= gsi_start_phis (return_bb
);
1529 !gsi_end_p (psi
); gsi_next (&psi
))
1530 if (!virtual_operand_p (gimple_phi_result (psi
.phi ())))
1533 /* When there is PHI, just update its value. */
1534 if (TREE_CODE (retval
) == SSA_NAME
1535 && !gsi_end_p (psi
))
1536 add_phi_arg (psi
.phi (), retval
, e
, UNKNOWN_LOCATION
);
1537 /* Otherwise update the return BB itself.
1538 find_return_bb allows at most one assignment to return value,
1539 so update first statement. */
1542 gimple_stmt_iterator bsi
;
1543 for (bsi
= gsi_start_bb (return_bb
); !gsi_end_p (bsi
);
1545 if (greturn
*return_stmt
1546 = dyn_cast
<greturn
*> (gsi_stmt (bsi
)))
1548 gimple_return_set_retval (return_stmt
, retval
);
1551 else if (gimple_code (gsi_stmt (bsi
)) == GIMPLE_ASSIGN
1552 && !gimple_clobber_p (gsi_stmt (bsi
)))
1554 gimple_assign_set_rhs1 (gsi_stmt (bsi
), retval
);
1557 update_stmt (gsi_stmt (bsi
));
1560 /* Replace retbnd with new one. */
1563 gimple_stmt_iterator bsi
;
1564 for (bsi
= gsi_last_bb (return_bb
); !gsi_end_p (bsi
);
1566 if (gimple_code (gsi_stmt (bsi
)) == GIMPLE_RETURN
)
1568 retbnd
= copy_ssa_name (retbnd
, call
);
1569 gimple_return_set_retbnd (gsi_stmt (bsi
), retbnd
);
1570 update_stmt (gsi_stmt (bsi
));
1575 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
1577 gimple_call_set_lhs (call
, build_simple_mem_ref (retval
));
1578 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1583 restype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
1584 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1585 if (!useless_type_conversion_p (TREE_TYPE (retval
), restype
))
1588 tree tem
= create_tmp_reg (restype
);
1589 tem
= make_ssa_name (tem
, call
);
1590 cpy
= gimple_build_assign (retval
, NOP_EXPR
, tem
);
1591 gsi_insert_after (&gsi
, cpy
, GSI_NEW_STMT
);
1594 /* Build bndret call to obtain returned bounds. */
1596 insert_bndret_call_after (retbnd
, retval
, &gsi
);
1597 gimple_call_set_lhs (call
, retval
);
1602 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1604 /* We don't use return block (there is either no return in function or
1605 multiple of them). So create new basic block with return statement.
1610 if (split_point
->split_part_set_retval
1611 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl
))))
1613 retval
= DECL_RESULT (current_function_decl
);
1615 if (chkp_function_instrumented_p (current_function_decl
)
1616 && BOUNDED_P (retval
))
1617 retbnd
= create_tmp_reg (pointer_bounds_type_node
);
1619 /* We use temporary register to hold value when aggregate_value_p
1620 is false. Similarly for DECL_BY_REFERENCE we must avoid extra
1622 if (!aggregate_value_p (retval
, TREE_TYPE (current_function_decl
))
1623 && !DECL_BY_REFERENCE (retval
))
1624 retval
= create_tmp_reg (TREE_TYPE (retval
));
1625 if (is_gimple_reg (retval
))
1627 /* When returning by reference, there is only one SSA name
1628 assigned to RESULT_DECL (that is pointer to return value).
1629 Look it up or create new one if it is missing. */
1630 if (DECL_BY_REFERENCE (retval
))
1631 retval
= get_or_create_ssa_default_def (cfun
, retval
);
1632 /* Otherwise produce new SSA name for return value. */
1634 retval
= make_ssa_name (retval
, call
);
1636 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl
)))
1637 gimple_call_set_lhs (call
, build_simple_mem_ref (retval
));
1639 gimple_call_set_lhs (call
, retval
);
1641 gsi_insert_after (&gsi
, call
, GSI_NEW_STMT
);
1642 /* Build bndret call to obtain returned bounds. */
1644 insert_bndret_call_after (retbnd
, retval
, &gsi
);
1645 ret
= gimple_build_return (retval
);
1646 gsi_insert_after (&gsi
, ret
, GSI_NEW_STMT
);
1649 free_dominance_info (CDI_DOMINATORS
);
1650 free_dominance_info (CDI_POST_DOMINATORS
);
1651 compute_inline_parameters (node
, true);
1654 /* Execute function splitting pass. */
1657 execute_split_functions (void)
1659 gimple_stmt_iterator bsi
;
1661 int overall_time
= 0, overall_size
= 0;
1663 struct cgraph_node
*node
= cgraph_node::get (current_function_decl
);
1665 if (flags_from_decl_or_type (current_function_decl
)
1666 & (ECF_NORETURN
|ECF_MALLOC
))
1669 fprintf (dump_file
, "Not splitting: noreturn/malloc function.\n");
1672 if (MAIN_NAME_P (DECL_NAME (current_function_decl
)))
1675 fprintf (dump_file
, "Not splitting: main function.\n");
1678 /* This can be relaxed; function might become inlinable after splitting
1679 away the uninlinable part. */
1680 if (inline_edge_summary_vec
.exists ()
1681 && !inline_summary (node
)->inlinable
)
1684 fprintf (dump_file
, "Not splitting: not inlinable.\n");
1687 if (DECL_DISREGARD_INLINE_LIMITS (node
->decl
))
1690 fprintf (dump_file
, "Not splitting: disregarding inline limits.\n");
1693 /* This can be relaxed; most of versioning tests actually prevents
1695 if (!tree_versionable_function_p (current_function_decl
))
1698 fprintf (dump_file
, "Not splitting: not versionable.\n");
1701 /* FIXME: we could support this. */
1702 if (DECL_STRUCT_FUNCTION (current_function_decl
)->static_chain_decl
)
1705 fprintf (dump_file
, "Not splitting: nested function.\n");
1709 /* See if it makes sense to try to split.
1710 It makes sense to split if we inline, that is if we have direct calls to
1711 handle or direct calls are possibly going to appear as result of indirect
1712 inlining or LTO. Also handle -fprofile-generate as LTO to allow non-LTO
1713 training for LTO -fprofile-use build.
1715 Note that we are not completely conservative about disqualifying functions
1716 called once. It is possible that the caller is called more then once and
1717 then inlining would still benefit. */
1719 /* Local functions called once will be completely inlined most of time. */
1720 || (!node
->callers
->next_caller
&& node
->local
.local
))
1721 && !node
->address_taken
1722 && (!flag_lto
|| !node
->externally_visible
))
1725 fprintf (dump_file
, "Not splitting: not called directly "
1726 "or called once.\n");
1730 /* FIXME: We can actually split if splitting reduces call overhead. */
1731 if (!flag_inline_small_functions
1732 && !DECL_DECLARED_INLINE_P (current_function_decl
))
1735 fprintf (dump_file
, "Not splitting: not autoinlining and function"
1736 " is not inline.\n");
1740 /* We enforce splitting after loop headers when profile info is not
1742 if (profile_status_for_fn (cfun
) != PROFILE_READ
)
1743 mark_dfs_back_edges ();
1745 /* Initialize bitmap to track forbidden calls. */
1746 forbidden_dominators
= BITMAP_ALLOC (NULL
);
1747 calculate_dominance_info (CDI_DOMINATORS
);
1749 /* Compute local info about basic blocks and determine function size/time. */
1750 bb_info_vec
.safe_grow_cleared (last_basic_block_for_fn (cfun
) + 1);
1751 memset (&best_split_point
, 0, sizeof (best_split_point
));
1752 FOR_EACH_BB_FN (bb
, cfun
)
1756 int freq
= compute_call_stmt_bb_frequency (current_function_decl
, bb
);
1758 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1759 fprintf (dump_file
, "Basic block %i\n", bb
->index
);
1761 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
1763 int this_time
, this_size
;
1764 gimple stmt
= gsi_stmt (bsi
);
1766 this_size
= estimate_num_insns (stmt
, &eni_size_weights
);
1767 this_time
= estimate_num_insns (stmt
, &eni_time_weights
) * freq
;
1770 check_forbidden_calls (stmt
);
1772 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1774 fprintf (dump_file
, " freq:%6i size:%3i time:%3i ",
1775 freq
, this_size
, this_time
);
1776 print_gimple_stmt (dump_file
, stmt
, 0, 0);
1779 overall_time
+= time
;
1780 overall_size
+= size
;
1781 bb_info_vec
[bb
->index
].time
= time
;
1782 bb_info_vec
[bb
->index
].size
= size
;
1784 find_split_points (overall_time
, overall_size
);
1785 if (best_split_point
.split_bbs
)
1787 split_function (&best_split_point
);
1788 BITMAP_FREE (best_split_point
.ssa_names_to_pass
);
1789 BITMAP_FREE (best_split_point
.split_bbs
);
1790 todo
= TODO_update_ssa
| TODO_cleanup_cfg
;
1792 BITMAP_FREE (forbidden_dominators
);
1793 bb_info_vec
.release ();
1799 const pass_data pass_data_split_functions
=
1801 GIMPLE_PASS
, /* type */
1802 "fnsplit", /* name */
1803 OPTGROUP_NONE
, /* optinfo_flags */
1804 TV_IPA_FNSPLIT
, /* tv_id */
1805 PROP_cfg
, /* properties_required */
1806 0, /* properties_provided */
1807 0, /* properties_destroyed */
1808 0, /* todo_flags_start */
1809 0, /* todo_flags_finish */
1812 class pass_split_functions
: public gimple_opt_pass
1815 pass_split_functions (gcc::context
*ctxt
)
1816 : gimple_opt_pass (pass_data_split_functions
, ctxt
)
1819 /* opt_pass methods: */
1820 virtual bool gate (function
*);
1821 virtual unsigned int execute (function
*)
1823 return execute_split_functions ();
1826 }; // class pass_split_functions
1829 pass_split_functions::gate (function
*)
1831 /* When doing profile feedback, we want to execute the pass after profiling
1832 is read. So disable one in early optimization. */
1833 return (flag_partial_inlining
1834 && !profile_arc_flag
&& !flag_branch_probabilities
);
1840 make_pass_split_functions (gcc::context
*ctxt
)
1842 return new pass_split_functions (ctxt
);
1845 /* Execute function splitting pass. */
1848 execute_feedback_split_functions (void)
1850 unsigned int retval
= execute_split_functions ();
1852 retval
|= TODO_rebuild_cgraph_edges
;
1858 const pass_data pass_data_feedback_split_functions
=
1860 GIMPLE_PASS
, /* type */
1861 "feedback_fnsplit", /* name */
1862 OPTGROUP_NONE
, /* optinfo_flags */
1863 TV_IPA_FNSPLIT
, /* tv_id */
1864 PROP_cfg
, /* properties_required */
1865 0, /* properties_provided */
1866 0, /* properties_destroyed */
1867 0, /* todo_flags_start */
1868 0, /* todo_flags_finish */
1871 class pass_feedback_split_functions
: public gimple_opt_pass
1874 pass_feedback_split_functions (gcc::context
*ctxt
)
1875 : gimple_opt_pass (pass_data_feedback_split_functions
, ctxt
)
1878 /* opt_pass methods: */
1879 virtual bool gate (function
*);
1880 virtual unsigned int execute (function
*)
1882 return execute_feedback_split_functions ();
1885 }; // class pass_feedback_split_functions
1888 pass_feedback_split_functions::gate (function
*)
1890 /* We don't need to split when profiling at all, we are producing
1891 lousy code anyway. */
1892 return (flag_partial_inlining
1893 && flag_branch_probabilities
);
1899 make_pass_feedback_split_functions (gcc::context
*ctxt
)
1901 return new pass_feedback_split_functions (ctxt
);