* fold-const.c (fold_binary_loc): Don't fold if the result
[official-gcc.git] / gcc / ipa-split.c
blob98dbc63e2fb08e6738a864f847448ac2ef05fc34
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
10 version.
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
15 for more details.
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:
24 func (...)
26 if (cheap_test)
27 something_small
28 else
29 something_big
32 Produce:
34 func.part (...)
36 something_big
39 func (...)
41 if (cheap_test)
42 something_small
43 else
44 func.part (...);
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
55 and chose best one.
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
60 and consider_split.
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. */
77 #include "config.h"
78 #include "system.h"
79 #include "coretypes.h"
80 #include "tree.h"
81 #include "predict.h"
82 #include "vec.h"
83 #include "hashtab.h"
84 #include "hash-set.h"
85 #include "machmode.h"
86 #include "tm.h"
87 #include "hard-reg-set.h"
88 #include "input.h"
89 #include "function.h"
90 #include "dominance.h"
91 #include "cfg.h"
92 #include "cfganal.h"
93 #include "basic-block.h"
94 #include "tree-ssa-alias.h"
95 #include "internal-fn.h"
96 #include "gimple-expr.h"
97 #include "is-a.h"
98 #include "gimple.h"
99 #include "stringpool.h"
100 #include "expr.h"
101 #include "calls.h"
102 #include "gimplify.h"
103 #include "gimple-iterator.h"
104 #include "gimplify-me.h"
105 #include "gimple-walk.h"
106 #include "target.h"
107 #include "hash-map.h"
108 #include "plugin-api.h"
109 #include "ipa-ref.h"
110 #include "cgraph.h"
111 #include "alloc-pool.h"
112 #include "ipa-prop.h"
113 #include "gimple-ssa.h"
114 #include "tree-cfg.h"
115 #include "tree-phinodes.h"
116 #include "ssa-iterators.h"
117 #include "stringpool.h"
118 #include "tree-ssanames.h"
119 #include "tree-into-ssa.h"
120 #include "tree-dfa.h"
121 #include "tree-pass.h"
122 #include "flags.h"
123 #include "diagnostic.h"
124 #include "tree-dump.h"
125 #include "tree-inline.h"
126 #include "params.h"
127 #include "gimple-pretty-print.h"
128 #include "ipa-inline.h"
129 #include "cfgloop.h"
130 #include "tree-chkp.h"
132 /* Per basic block info. */
134 typedef struct
136 unsigned int size;
137 unsigned int time;
138 } split_bb_info;
140 static vec<split_bb_info> bb_info_vec;
142 /* Description of split point. */
144 struct split_point
146 /* Size of the partitions. */
147 unsigned int header_time, header_size, split_time, split_size;
149 /* SSA names that need to be passed into spit function. */
150 bitmap ssa_names_to_pass;
152 /* Basic block where we split (that will become entry point of new function. */
153 basic_block entry_bb;
155 /* Basic blocks we are splitting away. */
156 bitmap split_bbs;
158 /* True when return value is computed on split part and thus it needs
159 to be returned. */
160 bool split_part_set_retval;
163 /* Best split point found. */
165 struct split_point best_split_point;
167 /* Set of basic blocks that are not allowed to dominate a split point. */
169 static bitmap forbidden_dominators;
171 static tree find_retval (basic_block return_bb);
172 static tree find_retbnd (basic_block return_bb);
174 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
175 variable, check it if it is present in bitmap passed via DATA. */
177 static bool
178 test_nonssa_use (gimple, tree t, tree, void *data)
180 t = get_base_address (t);
182 if (!t || is_gimple_reg (t))
183 return false;
185 if (TREE_CODE (t) == PARM_DECL
186 || (TREE_CODE (t) == VAR_DECL
187 && auto_var_in_fn_p (t, current_function_decl))
188 || TREE_CODE (t) == RESULT_DECL
189 /* Normal labels are part of CFG and will be handled gratefuly.
190 Forced labels however can be used directly by statements and
191 need to stay in one partition along with their uses. */
192 || (TREE_CODE (t) == LABEL_DECL
193 && FORCED_LABEL (t)))
194 return bitmap_bit_p ((bitmap)data, DECL_UID (t));
196 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
197 to pretend that the value pointed to is actual result decl. */
198 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
199 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
200 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
201 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
202 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
203 return
204 bitmap_bit_p ((bitmap)data,
205 DECL_UID (DECL_RESULT (current_function_decl)));
207 return false;
210 /* Dump split point CURRENT. */
212 static void
213 dump_split_point (FILE * file, struct split_point *current)
215 fprintf (file,
216 "Split point at BB %i\n"
217 " header time: %i header size: %i\n"
218 " split time: %i split size: %i\n bbs: ",
219 current->entry_bb->index, current->header_time,
220 current->header_size, current->split_time, current->split_size);
221 dump_bitmap (file, current->split_bbs);
222 fprintf (file, " SSA names to pass: ");
223 dump_bitmap (file, current->ssa_names_to_pass);
226 /* Look for all BBs in header that might lead to the split part and verify
227 that they are not defining any non-SSA var used by the split part.
228 Parameters are the same as for consider_split. */
230 static bool
231 verify_non_ssa_vars (struct split_point *current, bitmap non_ssa_vars,
232 basic_block return_bb)
234 bitmap seen = BITMAP_ALLOC (NULL);
235 vec<basic_block> worklist = vNULL;
236 edge e;
237 edge_iterator ei;
238 bool ok = true;
239 basic_block bb;
241 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
242 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
243 && !bitmap_bit_p (current->split_bbs, e->src->index))
245 worklist.safe_push (e->src);
246 bitmap_set_bit (seen, e->src->index);
249 while (!worklist.is_empty ())
251 gimple_stmt_iterator bsi;
253 bb = worklist.pop ();
254 FOR_EACH_EDGE (e, ei, bb->preds)
255 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
256 && bitmap_set_bit (seen, e->src->index))
258 gcc_checking_assert (!bitmap_bit_p (current->split_bbs,
259 e->src->index));
260 worklist.safe_push (e->src);
262 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
264 gimple stmt = gsi_stmt (bsi);
265 if (is_gimple_debug (stmt))
266 continue;
267 if (walk_stmt_load_store_addr_ops
268 (stmt, non_ssa_vars, test_nonssa_use, test_nonssa_use,
269 test_nonssa_use))
271 ok = false;
272 goto done;
274 if (gimple_code (stmt) == GIMPLE_LABEL
275 && test_nonssa_use (stmt, gimple_label_label (stmt),
276 NULL_TREE, non_ssa_vars))
278 ok = false;
279 goto done;
282 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
284 if (walk_stmt_load_store_addr_ops
285 (gsi_stmt (bsi), non_ssa_vars, test_nonssa_use, test_nonssa_use,
286 test_nonssa_use))
288 ok = false;
289 goto done;
292 FOR_EACH_EDGE (e, ei, bb->succs)
294 if (e->dest != return_bb)
295 continue;
296 for (bsi = gsi_start_phis (return_bb); !gsi_end_p (bsi);
297 gsi_next (&bsi))
299 gimple stmt = gsi_stmt (bsi);
300 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
302 if (virtual_operand_p (gimple_phi_result (stmt)))
303 continue;
304 if (TREE_CODE (op) != SSA_NAME
305 && test_nonssa_use (stmt, op, op, non_ssa_vars))
307 ok = false;
308 goto done;
314 /* Verify that the rest of function does not define any label
315 used by the split part. */
316 FOR_EACH_BB_FN (bb, cfun)
317 if (!bitmap_bit_p (current->split_bbs, bb->index)
318 && !bitmap_bit_p (seen, bb->index))
320 gimple_stmt_iterator bsi;
321 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
322 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_LABEL
323 && test_nonssa_use (gsi_stmt (bsi),
324 gimple_label_label (gsi_stmt (bsi)),
325 NULL_TREE, non_ssa_vars))
327 ok = false;
328 goto done;
330 else if (gimple_code (gsi_stmt (bsi)) != GIMPLE_LABEL)
331 break;
334 done:
335 BITMAP_FREE (seen);
336 worklist.release ();
337 return ok;
340 /* If STMT is a call, check the callee against a list of forbidden
341 predicate functions. If a match is found, look for uses of the
342 call result in condition statements that compare against zero.
343 For each such use, find the block targeted by the condition
344 statement for the nonzero result, and set the bit for this block
345 in the forbidden dominators bitmap. The purpose of this is to avoid
346 selecting a split point where we are likely to lose the chance
347 to optimize away an unused function call. */
349 static void
350 check_forbidden_calls (gimple stmt)
352 imm_use_iterator use_iter;
353 use_operand_p use_p;
354 tree lhs;
356 /* At the moment, __builtin_constant_p is the only forbidden
357 predicate function call (see PR49642). */
358 if (!gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P))
359 return;
361 lhs = gimple_call_lhs (stmt);
363 if (!lhs || TREE_CODE (lhs) != SSA_NAME)
364 return;
366 FOR_EACH_IMM_USE_FAST (use_p, use_iter, lhs)
368 tree op1;
369 basic_block use_bb, forbidden_bb;
370 enum tree_code code;
371 edge true_edge, false_edge;
372 gimple use_stmt = USE_STMT (use_p);
374 if (gimple_code (use_stmt) != GIMPLE_COND)
375 continue;
377 /* Assuming canonical form for GIMPLE_COND here, with constant
378 in second position. */
379 op1 = gimple_cond_rhs (use_stmt);
380 code = gimple_cond_code (use_stmt);
381 use_bb = gimple_bb (use_stmt);
383 extract_true_false_edges_from_block (use_bb, &true_edge, &false_edge);
385 /* We're only interested in comparisons that distinguish
386 unambiguously from zero. */
387 if (!integer_zerop (op1) || code == LE_EXPR || code == GE_EXPR)
388 continue;
390 if (code == EQ_EXPR)
391 forbidden_bb = false_edge->dest;
392 else
393 forbidden_bb = true_edge->dest;
395 bitmap_set_bit (forbidden_dominators, forbidden_bb->index);
399 /* If BB is dominated by any block in the forbidden dominators set,
400 return TRUE; else FALSE. */
402 static bool
403 dominated_by_forbidden (basic_block bb)
405 unsigned dom_bb;
406 bitmap_iterator bi;
408 EXECUTE_IF_SET_IN_BITMAP (forbidden_dominators, 1, dom_bb, bi)
410 if (dominated_by_p (CDI_DOMINATORS, bb,
411 BASIC_BLOCK_FOR_FN (cfun, dom_bb)))
412 return true;
415 return false;
418 /* For give split point CURRENT and return block RETURN_BB return 1
419 if ssa name VAL is set by split part and 0 otherwise. */
420 static bool
421 split_part_set_ssa_name_p (tree val, struct split_point *current,
422 basic_block return_bb)
424 if (TREE_CODE (val) != SSA_NAME)
425 return false;
427 return (!SSA_NAME_IS_DEFAULT_DEF (val)
428 && (bitmap_bit_p (current->split_bbs,
429 gimple_bb (SSA_NAME_DEF_STMT (val))->index)
430 || gimple_bb (SSA_NAME_DEF_STMT (val)) == return_bb));
433 /* We found an split_point CURRENT. NON_SSA_VARS is bitmap of all non ssa
434 variables used and RETURN_BB is return basic block.
435 See if we can split function here. */
437 static void
438 consider_split (struct split_point *current, bitmap non_ssa_vars,
439 basic_block return_bb)
441 tree parm;
442 unsigned int num_args = 0;
443 unsigned int call_overhead;
444 edge e;
445 edge_iterator ei;
446 gimple_stmt_iterator bsi;
447 unsigned int i;
448 int incoming_freq = 0;
449 tree retval;
450 tree retbnd;
451 bool back_edge = false;
453 if (dump_file && (dump_flags & TDF_DETAILS))
454 dump_split_point (dump_file, current);
456 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
458 if (e->flags & EDGE_DFS_BACK)
459 back_edge = true;
460 if (!bitmap_bit_p (current->split_bbs, e->src->index))
461 incoming_freq += EDGE_FREQUENCY (e);
464 /* Do not split when we would end up calling function anyway. */
465 if (incoming_freq
466 >= (ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency
467 * PARAM_VALUE (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY) / 100))
469 /* When profile is guessed, we can not expect it to give us
470 realistic estimate on likelyness of function taking the
471 complex path. As a special case, when tail of the function is
472 a loop, enable splitting since inlining code skipping the loop
473 is likely noticeable win. */
474 if (back_edge
475 && profile_status_for_fn (cfun) != PROFILE_READ
476 && incoming_freq < ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency)
478 if (dump_file && (dump_flags & TDF_DETAILS))
479 fprintf (dump_file,
480 " Split before loop, accepting despite low frequencies %i %i.\n",
481 incoming_freq,
482 ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency);
484 else
486 if (dump_file && (dump_flags & TDF_DETAILS))
487 fprintf (dump_file,
488 " Refused: incoming frequency is too large.\n");
489 return;
493 if (!current->header_size)
495 if (dump_file && (dump_flags & TDF_DETAILS))
496 fprintf (dump_file, " Refused: header empty\n");
497 return;
500 /* Verify that PHI args on entry are either virtual or all their operands
501 incoming from header are the same. */
502 for (bsi = gsi_start_phis (current->entry_bb); !gsi_end_p (bsi); gsi_next (&bsi))
504 gimple stmt = gsi_stmt (bsi);
505 tree val = NULL;
507 if (virtual_operand_p (gimple_phi_result (stmt)))
508 continue;
509 for (i = 0; i < gimple_phi_num_args (stmt); i++)
511 edge e = gimple_phi_arg_edge (stmt, i);
512 if (!bitmap_bit_p (current->split_bbs, e->src->index))
514 tree edge_val = gimple_phi_arg_def (stmt, i);
515 if (val && edge_val != val)
517 if (dump_file && (dump_flags & TDF_DETAILS))
518 fprintf (dump_file,
519 " Refused: entry BB has PHI with multiple variants\n");
520 return;
522 val = edge_val;
528 /* See what argument we will pass to the split function and compute
529 call overhead. */
530 call_overhead = eni_size_weights.call_cost;
531 for (parm = DECL_ARGUMENTS (current_function_decl); parm;
532 parm = DECL_CHAIN (parm))
534 if (!is_gimple_reg (parm))
536 if (bitmap_bit_p (non_ssa_vars, DECL_UID (parm)))
538 if (dump_file && (dump_flags & TDF_DETAILS))
539 fprintf (dump_file,
540 " Refused: need to pass non-ssa param values\n");
541 return;
544 else
546 tree ddef = ssa_default_def (cfun, parm);
547 if (ddef
548 && bitmap_bit_p (current->ssa_names_to_pass,
549 SSA_NAME_VERSION (ddef)))
551 if (!VOID_TYPE_P (TREE_TYPE (parm)))
552 call_overhead += estimate_move_cost (TREE_TYPE (parm), false);
553 num_args++;
557 if (!VOID_TYPE_P (TREE_TYPE (current_function_decl)))
558 call_overhead += estimate_move_cost (TREE_TYPE (current_function_decl),
559 false);
561 if (current->split_size <= call_overhead)
563 if (dump_file && (dump_flags & TDF_DETAILS))
564 fprintf (dump_file,
565 " Refused: split size is smaller than call overhead\n");
566 return;
568 if (current->header_size + call_overhead
569 >= (unsigned int)(DECL_DECLARED_INLINE_P (current_function_decl)
570 ? MAX_INLINE_INSNS_SINGLE
571 : MAX_INLINE_INSNS_AUTO))
573 if (dump_file && (dump_flags & TDF_DETAILS))
574 fprintf (dump_file,
575 " Refused: header size is too large for inline candidate\n");
576 return;
579 /* FIXME: we currently can pass only SSA function parameters to the split
580 arguments. Once parm_adjustment infrastructure is supported by cloning,
581 we can pass more than that. */
582 if (num_args != bitmap_count_bits (current->ssa_names_to_pass))
585 if (dump_file && (dump_flags & TDF_DETAILS))
586 fprintf (dump_file,
587 " Refused: need to pass non-param values\n");
588 return;
591 /* When there are non-ssa vars used in the split region, see if they
592 are used in the header region. If so, reject the split.
593 FIXME: we can use nested function support to access both. */
594 if (!bitmap_empty_p (non_ssa_vars)
595 && !verify_non_ssa_vars (current, non_ssa_vars, return_bb))
597 if (dump_file && (dump_flags & TDF_DETAILS))
598 fprintf (dump_file,
599 " Refused: split part has non-ssa uses\n");
600 return;
603 /* If the split point is dominated by a forbidden block, reject
604 the split. */
605 if (!bitmap_empty_p (forbidden_dominators)
606 && dominated_by_forbidden (current->entry_bb))
608 if (dump_file && (dump_flags & TDF_DETAILS))
609 fprintf (dump_file,
610 " Refused: split point dominated by forbidden block\n");
611 return;
614 /* See if retval used by return bb is computed by header or split part.
615 When it is computed by split part, we need to produce return statement
616 in the split part and add code to header to pass it around.
618 This is bit tricky to test:
619 1) When there is no return_bb or no return value, we always pass
620 value around.
621 2) Invariants are always computed by caller.
622 3) For SSA we need to look if defining statement is in header or split part
623 4) For non-SSA we need to look where the var is computed. */
624 retval = find_retval (return_bb);
625 if (!retval)
626 current->split_part_set_retval = true;
627 else if (is_gimple_min_invariant (retval))
628 current->split_part_set_retval = false;
629 /* Special case is value returned by reference we record as if it was non-ssa
630 set to result_decl. */
631 else if (TREE_CODE (retval) == SSA_NAME
632 && SSA_NAME_VAR (retval)
633 && TREE_CODE (SSA_NAME_VAR (retval)) == RESULT_DECL
634 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
635 current->split_part_set_retval
636 = bitmap_bit_p (non_ssa_vars, DECL_UID (SSA_NAME_VAR (retval)));
637 else if (TREE_CODE (retval) == SSA_NAME)
638 current->split_part_set_retval
639 = split_part_set_ssa_name_p (retval, current, return_bb);
640 else if (TREE_CODE (retval) == PARM_DECL)
641 current->split_part_set_retval = false;
642 else if (TREE_CODE (retval) == VAR_DECL
643 || TREE_CODE (retval) == RESULT_DECL)
644 current->split_part_set_retval
645 = bitmap_bit_p (non_ssa_vars, DECL_UID (retval));
646 else
647 current->split_part_set_retval = true;
649 /* See if retbnd used by return bb is computed by header or split part. */
650 retbnd = find_retbnd (return_bb);
651 if (retbnd)
653 bool split_part_set_retbnd
654 = split_part_set_ssa_name_p (retbnd, current, return_bb);
656 /* If we have both return value and bounds then keep their definitions
657 in a single function. We use SSA names to link returned bounds and
658 value and therefore do not handle cases when result is passed by
659 reference (which should not be our case anyway since bounds are
660 returned for pointers only). */
661 if ((DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
662 && current->split_part_set_retval)
663 || split_part_set_retbnd != current->split_part_set_retval)
665 if (dump_file && (dump_flags & TDF_DETAILS))
666 fprintf (dump_file,
667 " Refused: split point splits return value and bounds\n");
668 return;
672 /* split_function fixes up at most one PHI non-virtual PHI node in return_bb,
673 for the return value. If there are other PHIs, give up. */
674 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
676 gimple_stmt_iterator psi;
678 for (psi = gsi_start_phis (return_bb); !gsi_end_p (psi); gsi_next (&psi))
679 if (!virtual_operand_p (gimple_phi_result (gsi_stmt (psi)))
680 && !(retval
681 && current->split_part_set_retval
682 && TREE_CODE (retval) == SSA_NAME
683 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
684 && SSA_NAME_DEF_STMT (retval) == gsi_stmt (psi)))
686 if (dump_file && (dump_flags & TDF_DETAILS))
687 fprintf (dump_file,
688 " Refused: return bb has extra PHIs\n");
689 return;
693 if (dump_file && (dump_flags & TDF_DETAILS))
694 fprintf (dump_file, " Accepted!\n");
696 /* At the moment chose split point with lowest frequency and that leaves
697 out smallest size of header.
698 In future we might re-consider this heuristics. */
699 if (!best_split_point.split_bbs
700 || best_split_point.entry_bb->frequency > current->entry_bb->frequency
701 || (best_split_point.entry_bb->frequency == current->entry_bb->frequency
702 && best_split_point.split_size < current->split_size))
705 if (dump_file && (dump_flags & TDF_DETAILS))
706 fprintf (dump_file, " New best split point!\n");
707 if (best_split_point.ssa_names_to_pass)
709 BITMAP_FREE (best_split_point.ssa_names_to_pass);
710 BITMAP_FREE (best_split_point.split_bbs);
712 best_split_point = *current;
713 best_split_point.ssa_names_to_pass = BITMAP_ALLOC (NULL);
714 bitmap_copy (best_split_point.ssa_names_to_pass,
715 current->ssa_names_to_pass);
716 best_split_point.split_bbs = BITMAP_ALLOC (NULL);
717 bitmap_copy (best_split_point.split_bbs, current->split_bbs);
721 /* Return basic block containing RETURN statement. We allow basic blocks
722 of the form:
723 <retval> = tmp_var;
724 return <retval>
725 but return_bb can not be more complex than this.
726 If nothing is found, return the exit block.
728 When there are multiple RETURN statement, chose one with return value,
729 since that one is more likely shared by multiple code paths.
731 Return BB is special, because for function splitting it is the only
732 basic block that is duplicated in between header and split part of the
733 function.
735 TODO: We might support multiple return blocks. */
737 static basic_block
738 find_return_bb (void)
740 edge e;
741 basic_block return_bb = EXIT_BLOCK_PTR_FOR_FN (cfun);
742 gimple_stmt_iterator bsi;
743 bool found_return = false;
744 tree retval = NULL_TREE;
746 if (!single_pred_p (EXIT_BLOCK_PTR_FOR_FN (cfun)))
747 return return_bb;
749 e = single_pred_edge (EXIT_BLOCK_PTR_FOR_FN (cfun));
750 for (bsi = gsi_last_bb (e->src); !gsi_end_p (bsi); gsi_prev (&bsi))
752 gimple stmt = gsi_stmt (bsi);
753 if (gimple_code (stmt) == GIMPLE_LABEL
754 || is_gimple_debug (stmt)
755 || gimple_clobber_p (stmt))
757 else if (gimple_code (stmt) == GIMPLE_ASSIGN
758 && found_return
759 && gimple_assign_single_p (stmt)
760 && (auto_var_in_fn_p (gimple_assign_rhs1 (stmt),
761 current_function_decl)
762 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
763 && retval == gimple_assign_lhs (stmt))
765 else if (gimple_code (stmt) == GIMPLE_RETURN)
767 found_return = true;
768 retval = gimple_return_retval (stmt);
770 else
771 break;
773 if (gsi_end_p (bsi) && found_return)
774 return_bb = e->src;
776 return return_bb;
779 /* Given return basic block RETURN_BB, see where return value is really
780 stored. */
781 static tree
782 find_retval (basic_block return_bb)
784 gimple_stmt_iterator bsi;
785 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
786 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
787 return gimple_return_retval (gsi_stmt (bsi));
788 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
789 && !gimple_clobber_p (gsi_stmt (bsi)))
790 return gimple_assign_rhs1 (gsi_stmt (bsi));
791 return NULL;
794 /* Given return basic block RETURN_BB, see where return bounds are really
795 stored. */
796 static tree
797 find_retbnd (basic_block return_bb)
799 gimple_stmt_iterator bsi;
800 for (bsi = gsi_last_bb (return_bb); !gsi_end_p (bsi); gsi_prev (&bsi))
801 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
802 return gimple_return_retbnd (gsi_stmt (bsi));
803 return NULL;
806 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
807 variable, mark it as used in bitmap passed via DATA.
808 Return true when access to T prevents splitting the function. */
810 static bool
811 mark_nonssa_use (gimple, tree t, tree, void *data)
813 t = get_base_address (t);
815 if (!t || is_gimple_reg (t))
816 return false;
818 /* At present we can't pass non-SSA arguments to split function.
819 FIXME: this can be relaxed by passing references to arguments. */
820 if (TREE_CODE (t) == PARM_DECL)
822 if (dump_file && (dump_flags & TDF_DETAILS))
823 fprintf (dump_file,
824 "Cannot split: use of non-ssa function parameter.\n");
825 return true;
828 if ((TREE_CODE (t) == VAR_DECL
829 && auto_var_in_fn_p (t, current_function_decl))
830 || TREE_CODE (t) == RESULT_DECL
831 || (TREE_CODE (t) == LABEL_DECL
832 && FORCED_LABEL (t)))
833 bitmap_set_bit ((bitmap)data, DECL_UID (t));
835 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
836 to pretend that the value pointed to is actual result decl. */
837 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
838 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
839 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
840 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
841 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
842 return
843 bitmap_bit_p ((bitmap)data,
844 DECL_UID (DECL_RESULT (current_function_decl)));
846 return false;
849 /* Compute local properties of basic block BB we collect when looking for
850 split points. We look for ssa defs and store them in SET_SSA_NAMES,
851 for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic
852 vars stored in NON_SSA_VARS.
854 When BB has edge to RETURN_BB, collect uses in RETURN_BB too.
856 Return false when BB contains something that prevents it from being put into
857 split function. */
859 static bool
860 visit_bb (basic_block bb, basic_block return_bb,
861 bitmap set_ssa_names, bitmap used_ssa_names,
862 bitmap non_ssa_vars)
864 gimple_stmt_iterator bsi;
865 edge e;
866 edge_iterator ei;
867 bool can_split = true;
869 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
871 gimple stmt = gsi_stmt (bsi);
872 tree op;
873 ssa_op_iter iter;
874 tree decl;
876 if (is_gimple_debug (stmt))
877 continue;
879 if (gimple_clobber_p (stmt))
880 continue;
882 /* FIXME: We can split regions containing EH. We can not however
883 split RESX, EH_DISPATCH and EH_POINTER referring to same region
884 into different partitions. This would require tracking of
885 EH regions and checking in consider_split_point if they
886 are not used elsewhere. */
887 if (gimple_code (stmt) == GIMPLE_RESX)
889 if (dump_file && (dump_flags & TDF_DETAILS))
890 fprintf (dump_file, "Cannot split: resx.\n");
891 can_split = false;
893 if (gimple_code (stmt) == GIMPLE_EH_DISPATCH)
895 if (dump_file && (dump_flags & TDF_DETAILS))
896 fprintf (dump_file, "Cannot split: eh dispatch.\n");
897 can_split = false;
900 /* Check builtins that prevent splitting. */
901 if (gimple_code (stmt) == GIMPLE_CALL
902 && (decl = gimple_call_fndecl (stmt)) != NULL_TREE
903 && DECL_BUILT_IN (decl)
904 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
905 switch (DECL_FUNCTION_CODE (decl))
907 /* FIXME: once we will allow passing non-parm values to split part,
908 we need to be sure to handle correct builtin_stack_save and
909 builtin_stack_restore. At the moment we are safe; there is no
910 way to store builtin_stack_save result in non-SSA variable
911 since all calls to those are compiler generated. */
912 case BUILT_IN_APPLY:
913 case BUILT_IN_APPLY_ARGS:
914 case BUILT_IN_VA_START:
915 if (dump_file && (dump_flags & TDF_DETAILS))
916 fprintf (dump_file,
917 "Cannot split: builtin_apply and va_start.\n");
918 can_split = false;
919 break;
920 case BUILT_IN_EH_POINTER:
921 if (dump_file && (dump_flags & TDF_DETAILS))
922 fprintf (dump_file, "Cannot split: builtin_eh_pointer.\n");
923 can_split = false;
924 break;
925 default:
926 break;
929 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
930 bitmap_set_bit (set_ssa_names, SSA_NAME_VERSION (op));
931 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
932 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
933 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
934 mark_nonssa_use,
935 mark_nonssa_use,
936 mark_nonssa_use);
938 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
940 gimple stmt = gsi_stmt (bsi);
941 unsigned int i;
943 if (virtual_operand_p (gimple_phi_result (stmt)))
944 continue;
945 bitmap_set_bit (set_ssa_names,
946 SSA_NAME_VERSION (gimple_phi_result (stmt)));
947 for (i = 0; i < gimple_phi_num_args (stmt); i++)
949 tree op = gimple_phi_arg_def (stmt, i);
950 if (TREE_CODE (op) == SSA_NAME)
951 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
953 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
954 mark_nonssa_use,
955 mark_nonssa_use,
956 mark_nonssa_use);
958 /* Record also uses coming from PHI operand in return BB. */
959 FOR_EACH_EDGE (e, ei, bb->succs)
960 if (e->dest == return_bb)
962 for (bsi = gsi_start_phis (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
964 gimple stmt = gsi_stmt (bsi);
965 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
967 if (virtual_operand_p (gimple_phi_result (stmt)))
968 continue;
969 if (TREE_CODE (op) == SSA_NAME)
970 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
971 else
972 can_split &= !mark_nonssa_use (stmt, op, op, non_ssa_vars);
975 return can_split;
978 /* Stack entry for recursive DFS walk in find_split_point. */
980 typedef struct
982 /* Basic block we are examining. */
983 basic_block bb;
985 /* SSA names set and used by the BB and all BBs reachable
986 from it via DFS walk. */
987 bitmap set_ssa_names, used_ssa_names;
988 bitmap non_ssa_vars;
990 /* All BBS visited from this BB via DFS walk. */
991 bitmap bbs_visited;
993 /* Last examined edge in DFS walk. Since we walk unoriented graph,
994 the value is up to sum of incoming and outgoing edges of BB. */
995 unsigned int edge_num;
997 /* Stack entry index of earliest BB reachable from current BB
998 or any BB visited later in DFS walk. */
999 int earliest;
1001 /* Overall time and size of all BBs reached from this BB in DFS walk. */
1002 int overall_time, overall_size;
1004 /* When false we can not split on this BB. */
1005 bool can_split;
1006 } stack_entry;
1009 /* Find all articulations and call consider_split on them.
1010 OVERALL_TIME and OVERALL_SIZE is time and size of the function.
1012 We perform basic algorithm for finding an articulation in a graph
1013 created from CFG by considering it to be an unoriented graph.
1015 The articulation is discovered via DFS walk. We collect earliest
1016 basic block on stack that is reachable via backward edge. Articulation
1017 is any basic block such that there is no backward edge bypassing it.
1018 To reduce stack usage we maintain heap allocated stack in STACK vector.
1019 AUX pointer of BB is set to index it appears in the stack or -1 once
1020 it is visited and popped off the stack.
1022 The algorithm finds articulation after visiting the whole component
1023 reachable by it. This makes it convenient to collect information about
1024 the component used by consider_split. */
1026 static void
1027 find_split_points (int overall_time, int overall_size)
1029 stack_entry first;
1030 vec<stack_entry> stack = vNULL;
1031 basic_block bb;
1032 basic_block return_bb = find_return_bb ();
1033 struct split_point current;
1035 current.header_time = overall_time;
1036 current.header_size = overall_size;
1037 current.split_time = 0;
1038 current.split_size = 0;
1039 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
1041 first.bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1042 first.edge_num = 0;
1043 first.overall_time = 0;
1044 first.overall_size = 0;
1045 first.earliest = INT_MAX;
1046 first.set_ssa_names = 0;
1047 first.used_ssa_names = 0;
1048 first.non_ssa_vars = 0;
1049 first.bbs_visited = 0;
1050 first.can_split = false;
1051 stack.safe_push (first);
1052 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = (void *)(intptr_t)-1;
1054 while (!stack.is_empty ())
1056 stack_entry *entry = &stack.last ();
1058 /* We are walking an acyclic graph, so edge_num counts
1059 succ and pred edges together. However when considering
1060 articulation, we want to have processed everything reachable
1061 from articulation but nothing that reaches into it. */
1062 if (entry->edge_num == EDGE_COUNT (entry->bb->succs)
1063 && entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1065 int pos = stack.length ();
1066 entry->can_split &= visit_bb (entry->bb, return_bb,
1067 entry->set_ssa_names,
1068 entry->used_ssa_names,
1069 entry->non_ssa_vars);
1070 if (pos <= entry->earliest && !entry->can_split
1071 && dump_file && (dump_flags & TDF_DETAILS))
1072 fprintf (dump_file,
1073 "found articulation at bb %i but can not split\n",
1074 entry->bb->index);
1075 if (pos <= entry->earliest && entry->can_split)
1077 if (dump_file && (dump_flags & TDF_DETAILS))
1078 fprintf (dump_file, "found articulation at bb %i\n",
1079 entry->bb->index);
1080 current.entry_bb = entry->bb;
1081 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
1082 bitmap_and_compl (current.ssa_names_to_pass,
1083 entry->used_ssa_names, entry->set_ssa_names);
1084 current.header_time = overall_time - entry->overall_time;
1085 current.header_size = overall_size - entry->overall_size;
1086 current.split_time = entry->overall_time;
1087 current.split_size = entry->overall_size;
1088 current.split_bbs = entry->bbs_visited;
1089 consider_split (&current, entry->non_ssa_vars, return_bb);
1090 BITMAP_FREE (current.ssa_names_to_pass);
1093 /* Do actual DFS walk. */
1094 if (entry->edge_num
1095 < (EDGE_COUNT (entry->bb->succs)
1096 + EDGE_COUNT (entry->bb->preds)))
1098 edge e;
1099 basic_block dest;
1100 if (entry->edge_num < EDGE_COUNT (entry->bb->succs))
1102 e = EDGE_SUCC (entry->bb, entry->edge_num);
1103 dest = e->dest;
1105 else
1107 e = EDGE_PRED (entry->bb, entry->edge_num
1108 - EDGE_COUNT (entry->bb->succs));
1109 dest = e->src;
1112 entry->edge_num++;
1114 /* New BB to visit, push it to the stack. */
1115 if (dest != return_bb && dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
1116 && !dest->aux)
1118 stack_entry new_entry;
1120 new_entry.bb = dest;
1121 new_entry.edge_num = 0;
1122 new_entry.overall_time
1123 = bb_info_vec[dest->index].time;
1124 new_entry.overall_size
1125 = bb_info_vec[dest->index].size;
1126 new_entry.earliest = INT_MAX;
1127 new_entry.set_ssa_names = BITMAP_ALLOC (NULL);
1128 new_entry.used_ssa_names = BITMAP_ALLOC (NULL);
1129 new_entry.bbs_visited = BITMAP_ALLOC (NULL);
1130 new_entry.non_ssa_vars = BITMAP_ALLOC (NULL);
1131 new_entry.can_split = true;
1132 bitmap_set_bit (new_entry.bbs_visited, dest->index);
1133 stack.safe_push (new_entry);
1134 dest->aux = (void *)(intptr_t)stack.length ();
1136 /* Back edge found, record the earliest point. */
1137 else if ((intptr_t)dest->aux > 0
1138 && (intptr_t)dest->aux < entry->earliest)
1139 entry->earliest = (intptr_t)dest->aux;
1141 /* We are done with examining the edges. Pop off the value from stack
1142 and merge stuff we accumulate during the walk. */
1143 else if (entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1145 stack_entry *prev = &stack[stack.length () - 2];
1147 entry->bb->aux = (void *)(intptr_t)-1;
1148 prev->can_split &= entry->can_split;
1149 if (prev->set_ssa_names)
1151 bitmap_ior_into (prev->set_ssa_names, entry->set_ssa_names);
1152 bitmap_ior_into (prev->used_ssa_names, entry->used_ssa_names);
1153 bitmap_ior_into (prev->bbs_visited, entry->bbs_visited);
1154 bitmap_ior_into (prev->non_ssa_vars, entry->non_ssa_vars);
1156 if (prev->earliest > entry->earliest)
1157 prev->earliest = entry->earliest;
1158 prev->overall_time += entry->overall_time;
1159 prev->overall_size += entry->overall_size;
1160 BITMAP_FREE (entry->set_ssa_names);
1161 BITMAP_FREE (entry->used_ssa_names);
1162 BITMAP_FREE (entry->bbs_visited);
1163 BITMAP_FREE (entry->non_ssa_vars);
1164 stack.pop ();
1166 else
1167 stack.pop ();
1169 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = NULL;
1170 FOR_EACH_BB_FN (bb, cfun)
1171 bb->aux = NULL;
1172 stack.release ();
1173 BITMAP_FREE (current.ssa_names_to_pass);
1176 /* Build and insert initialization of returned bounds RETBND
1177 for returned value RETVAL. Statements are inserted after
1178 a statement pointed by GSI and GSI is modified to point to
1179 the last inserted statement. */
1181 static void
1182 insert_bndret_call_after (tree retbnd, tree retval, gimple_stmt_iterator *gsi)
1184 tree fndecl = targetm.builtin_chkp_function (BUILT_IN_CHKP_BNDRET);
1185 gimple bndret = gimple_build_call (fndecl, 1, retval);
1186 gimple_call_set_lhs (bndret, retbnd);
1187 gsi_insert_after (gsi, bndret, GSI_CONTINUE_LINKING);
1189 /* Split function at SPLIT_POINT. */
1191 static void
1192 split_function (struct split_point *split_point)
1194 vec<tree> args_to_pass = vNULL;
1195 bitmap args_to_skip;
1196 tree parm;
1197 int num = 0;
1198 cgraph_node *node, *cur_node = cgraph_node::get (current_function_decl);
1199 basic_block return_bb = find_return_bb ();
1200 basic_block call_bb;
1201 gimple_stmt_iterator gsi;
1202 gimple call;
1203 edge e;
1204 edge_iterator ei;
1205 tree retval = NULL, real_retval = NULL, retbnd = NULL;
1206 bool split_part_return_p = false;
1207 bool with_bounds = chkp_function_instrumented_p (current_function_decl);
1208 gimple last_stmt = NULL;
1209 unsigned int i;
1210 tree arg, ddef;
1211 vec<tree, va_gc> **debug_args = NULL;
1213 if (dump_file)
1215 fprintf (dump_file, "\n\nSplitting function at:\n");
1216 dump_split_point (dump_file, split_point);
1219 if (cur_node->local.can_change_signature)
1220 args_to_skip = BITMAP_ALLOC (NULL);
1221 else
1222 args_to_skip = NULL;
1224 /* Collect the parameters of new function and args_to_skip bitmap. */
1225 for (parm = DECL_ARGUMENTS (current_function_decl);
1226 parm; parm = DECL_CHAIN (parm), num++)
1227 if (args_to_skip
1228 && (!is_gimple_reg (parm)
1229 || (ddef = ssa_default_def (cfun, parm)) == NULL_TREE
1230 || !bitmap_bit_p (split_point->ssa_names_to_pass,
1231 SSA_NAME_VERSION (ddef))))
1232 bitmap_set_bit (args_to_skip, num);
1233 else
1235 /* This parm might not have been used up to now, but is going to be
1236 used, hence register it. */
1237 if (is_gimple_reg (parm))
1238 arg = get_or_create_ssa_default_def (cfun, parm);
1239 else
1240 arg = parm;
1242 if (!useless_type_conversion_p (DECL_ARG_TYPE (parm), TREE_TYPE (arg)))
1243 arg = fold_convert (DECL_ARG_TYPE (parm), arg);
1244 args_to_pass.safe_push (arg);
1247 /* See if the split function will return. */
1248 FOR_EACH_EDGE (e, ei, return_bb->preds)
1249 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1250 break;
1251 if (e)
1252 split_part_return_p = true;
1254 /* Add return block to what will become the split function.
1255 We do not return; no return block is needed. */
1256 if (!split_part_return_p)
1258 /* We have no return block, so nothing is needed. */
1259 else if (return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1261 /* When we do not want to return value, we need to construct
1262 new return block with empty return statement.
1263 FIXME: Once we are able to change return type, we should change function
1264 to return void instead of just outputting function with undefined return
1265 value. For structures this affects quality of codegen. */
1266 else if (!split_point->split_part_set_retval
1267 && find_retval (return_bb))
1269 bool redirected = true;
1270 basic_block new_return_bb = create_basic_block (NULL, 0, return_bb);
1271 gimple_stmt_iterator gsi = gsi_start_bb (new_return_bb);
1272 gsi_insert_after (&gsi, gimple_build_return (NULL), GSI_NEW_STMT);
1273 while (redirected)
1275 redirected = false;
1276 FOR_EACH_EDGE (e, ei, return_bb->preds)
1277 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1279 new_return_bb->count += e->count;
1280 new_return_bb->frequency += EDGE_FREQUENCY (e);
1281 redirect_edge_and_branch (e, new_return_bb);
1282 redirected = true;
1283 break;
1286 e = make_edge (new_return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1287 e->probability = REG_BR_PROB_BASE;
1288 e->count = new_return_bb->count;
1289 add_bb_to_loop (new_return_bb, current_loops->tree_root);
1290 bitmap_set_bit (split_point->split_bbs, new_return_bb->index);
1292 /* When we pass around the value, use existing return block. */
1293 else
1294 bitmap_set_bit (split_point->split_bbs, return_bb->index);
1296 /* If RETURN_BB has virtual operand PHIs, they must be removed and the
1297 virtual operand marked for renaming as we change the CFG in a way that
1298 tree-inline is not able to compensate for.
1300 Note this can happen whether or not we have a return value. If we have
1301 a return value, then RETURN_BB may have PHIs for real operands too. */
1302 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1304 bool phi_p = false;
1305 for (gsi = gsi_start_phis (return_bb); !gsi_end_p (gsi);)
1307 gimple stmt = gsi_stmt (gsi);
1308 if (!virtual_operand_p (gimple_phi_result (stmt)))
1310 gsi_next (&gsi);
1311 continue;
1313 mark_virtual_phi_result_for_renaming (stmt);
1314 remove_phi_node (&gsi, true);
1315 phi_p = true;
1317 /* In reality we have to rename the reaching definition of the
1318 virtual operand at return_bb as we will eventually release it
1319 when we remove the code region we outlined.
1320 So we have to rename all immediate virtual uses of that region
1321 if we didn't see a PHI definition yet. */
1322 /* ??? In real reality we want to set the reaching vdef of the
1323 entry of the SESE region as the vuse of the call and the reaching
1324 vdef of the exit of the SESE region as the vdef of the call. */
1325 if (!phi_p)
1326 for (gsi = gsi_start_bb (return_bb); !gsi_end_p (gsi); gsi_next (&gsi))
1328 gimple stmt = gsi_stmt (gsi);
1329 if (gimple_vuse (stmt))
1331 gimple_set_vuse (stmt, NULL_TREE);
1332 update_stmt (stmt);
1334 if (gimple_vdef (stmt))
1335 break;
1339 /* Now create the actual clone. */
1340 cgraph_edge::rebuild_edges ();
1341 node = cur_node->create_version_clone_with_body
1342 (vNULL, NULL, args_to_skip, !split_part_return_p, split_point->split_bbs,
1343 split_point->entry_bb, "part");
1345 /* Let's take a time profile for splitted function. */
1346 node->tp_first_run = cur_node->tp_first_run + 1;
1348 /* For usual cloning it is enough to clear builtin only when signature
1349 changes. For partial inlining we however can not expect the part
1350 of builtin implementation to have same semantic as the whole. */
1351 if (DECL_BUILT_IN (node->decl))
1353 DECL_BUILT_IN_CLASS (node->decl) = NOT_BUILT_IN;
1354 DECL_FUNCTION_CODE (node->decl) = (enum built_in_function) 0;
1357 /* If the original function is instrumented then it's
1358 part is also instrumented. */
1359 if (with_bounds)
1360 chkp_function_mark_instrumented (node->decl);
1362 /* If the original function is declared inline, there is no point in issuing
1363 a warning for the non-inlinable part. */
1364 DECL_NO_INLINE_WARNING_P (node->decl) = 1;
1365 cur_node->remove_callees ();
1366 cur_node->remove_all_references ();
1367 if (!split_part_return_p)
1368 TREE_THIS_VOLATILE (node->decl) = 1;
1369 if (dump_file)
1370 dump_function_to_file (node->decl, dump_file, dump_flags);
1372 /* Create the basic block we place call into. It is the entry basic block
1373 split after last label. */
1374 call_bb = split_point->entry_bb;
1375 for (gsi = gsi_start_bb (call_bb); !gsi_end_p (gsi);)
1376 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
1378 last_stmt = gsi_stmt (gsi);
1379 gsi_next (&gsi);
1381 else
1382 break;
1383 e = split_block (split_point->entry_bb, last_stmt);
1384 remove_edge (e);
1386 /* Produce the call statement. */
1387 gsi = gsi_last_bb (call_bb);
1388 FOR_EACH_VEC_ELT (args_to_pass, i, arg)
1389 if (!is_gimple_val (arg))
1391 arg = force_gimple_operand_gsi (&gsi, arg, true, NULL_TREE,
1392 false, GSI_CONTINUE_LINKING);
1393 args_to_pass[i] = arg;
1395 call = gimple_build_call_vec (node->decl, args_to_pass);
1396 gimple_call_set_with_bounds (call, with_bounds);
1397 gimple_set_block (call, DECL_INITIAL (current_function_decl));
1398 args_to_pass.release ();
1400 /* For optimized away parameters, add on the caller side
1401 before the call
1402 DEBUG D#X => parm_Y(D)
1403 stmts and associate D#X with parm in decl_debug_args_lookup
1404 vector to say for debug info that if parameter parm had been passed,
1405 it would have value parm_Y(D). */
1406 if (args_to_skip)
1407 for (parm = DECL_ARGUMENTS (current_function_decl), num = 0;
1408 parm; parm = DECL_CHAIN (parm), num++)
1409 if (bitmap_bit_p (args_to_skip, num)
1410 && is_gimple_reg (parm))
1412 tree ddecl;
1413 gimple def_temp;
1415 /* This needs to be done even without MAY_HAVE_DEBUG_STMTS,
1416 otherwise if it didn't exist before, we'd end up with
1417 different SSA_NAME_VERSIONs between -g and -g0. */
1418 arg = get_or_create_ssa_default_def (cfun, parm);
1419 if (!MAY_HAVE_DEBUG_STMTS)
1420 continue;
1422 if (debug_args == NULL)
1423 debug_args = decl_debug_args_insert (node->decl);
1424 ddecl = make_node (DEBUG_EXPR_DECL);
1425 DECL_ARTIFICIAL (ddecl) = 1;
1426 TREE_TYPE (ddecl) = TREE_TYPE (parm);
1427 DECL_MODE (ddecl) = DECL_MODE (parm);
1428 vec_safe_push (*debug_args, DECL_ORIGIN (parm));
1429 vec_safe_push (*debug_args, ddecl);
1430 def_temp = gimple_build_debug_bind (ddecl, unshare_expr (arg),
1431 call);
1432 gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
1434 /* And on the callee side, add
1435 DEBUG D#Y s=> parm
1436 DEBUG var => D#Y
1437 stmts to the first bb where var is a VAR_DECL created for the
1438 optimized away parameter in DECL_INITIAL block. This hints
1439 in the debug info that var (whole DECL_ORIGIN is the parm PARM_DECL)
1440 is optimized away, but could be looked up at the call site
1441 as value of D#X there. */
1442 if (debug_args != NULL)
1444 unsigned int i;
1445 tree var, vexpr;
1446 gimple_stmt_iterator cgsi;
1447 gimple def_temp;
1449 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1450 var = BLOCK_VARS (DECL_INITIAL (node->decl));
1451 i = vec_safe_length (*debug_args);
1452 cgsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1455 i -= 2;
1456 while (var != NULL_TREE
1457 && DECL_ABSTRACT_ORIGIN (var) != (**debug_args)[i])
1458 var = TREE_CHAIN (var);
1459 if (var == NULL_TREE)
1460 break;
1461 vexpr = make_node (DEBUG_EXPR_DECL);
1462 parm = (**debug_args)[i];
1463 DECL_ARTIFICIAL (vexpr) = 1;
1464 TREE_TYPE (vexpr) = TREE_TYPE (parm);
1465 DECL_MODE (vexpr) = DECL_MODE (parm);
1466 def_temp = gimple_build_debug_source_bind (vexpr, parm,
1467 NULL);
1468 gsi_insert_before (&cgsi, def_temp, GSI_SAME_STMT);
1469 def_temp = gimple_build_debug_bind (var, vexpr, NULL);
1470 gsi_insert_before (&cgsi, def_temp, GSI_SAME_STMT);
1472 while (i);
1473 pop_cfun ();
1476 /* We avoid address being taken on any variable used by split part,
1477 so return slot optimization is always possible. Moreover this is
1478 required to make DECL_BY_REFERENCE work. */
1479 if (aggregate_value_p (DECL_RESULT (current_function_decl),
1480 TREE_TYPE (current_function_decl))
1481 && (!is_gimple_reg_type (TREE_TYPE (DECL_RESULT (current_function_decl)))
1482 || DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))))
1483 gimple_call_set_return_slot_opt (call, true);
1485 /* Update return value. This is bit tricky. When we do not return,
1486 do nothing. When we return we might need to update return_bb
1487 or produce a new return statement. */
1488 if (!split_part_return_p)
1489 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1490 else
1492 e = make_edge (call_bb, return_bb,
1493 return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
1494 ? 0 : EDGE_FALLTHRU);
1495 e->count = call_bb->count;
1496 e->probability = REG_BR_PROB_BASE;
1498 /* If there is return basic block, see what value we need to store
1499 return value into and put call just before it. */
1500 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1502 real_retval = retval = find_retval (return_bb);
1503 retbnd = find_retbnd (return_bb);
1505 if (real_retval && split_point->split_part_set_retval)
1507 gimple_stmt_iterator psi;
1509 /* See if we need new SSA_NAME for the result.
1510 When DECL_BY_REFERENCE is true, retval is actually pointer to
1511 return value and it is constant in whole function. */
1512 if (TREE_CODE (retval) == SSA_NAME
1513 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1515 retval = copy_ssa_name (retval, call);
1517 /* See if there is PHI defining return value. */
1518 for (psi = gsi_start_phis (return_bb);
1519 !gsi_end_p (psi); gsi_next (&psi))
1520 if (!virtual_operand_p (gimple_phi_result (gsi_stmt (psi))))
1521 break;
1523 /* When there is PHI, just update its value. */
1524 if (TREE_CODE (retval) == SSA_NAME
1525 && !gsi_end_p (psi))
1526 add_phi_arg (gsi_stmt (psi), retval, e, UNKNOWN_LOCATION);
1527 /* Otherwise update the return BB itself.
1528 find_return_bb allows at most one assignment to return value,
1529 so update first statement. */
1530 else
1532 gimple_stmt_iterator bsi;
1533 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi);
1534 gsi_next (&bsi))
1535 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
1537 gimple_return_set_retval (gsi_stmt (bsi), retval);
1538 break;
1540 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
1541 && !gimple_clobber_p (gsi_stmt (bsi)))
1543 gimple_assign_set_rhs1 (gsi_stmt (bsi), retval);
1544 break;
1546 update_stmt (gsi_stmt (bsi));
1549 /* Replace retbnd with new one. */
1550 if (retbnd)
1552 gimple_stmt_iterator bsi;
1553 for (bsi = gsi_last_bb (return_bb); !gsi_end_p (bsi);
1554 gsi_prev (&bsi))
1555 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
1557 retbnd = copy_ssa_name (retbnd, call);
1558 gimple_return_set_retbnd (gsi_stmt (bsi), retbnd);
1559 update_stmt (gsi_stmt (bsi));
1560 break;
1564 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1566 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1567 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1569 else
1571 tree restype;
1572 restype = TREE_TYPE (DECL_RESULT (current_function_decl));
1573 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1574 if (!useless_type_conversion_p (TREE_TYPE (retval), restype))
1576 gimple cpy;
1577 tree tem = create_tmp_reg (restype, NULL);
1578 tem = make_ssa_name (tem, call);
1579 cpy = gimple_build_assign_with_ops (NOP_EXPR, retval,
1580 tem, NULL_TREE);
1581 gsi_insert_after (&gsi, cpy, GSI_NEW_STMT);
1582 retval = tem;
1584 /* Build bndret call to obtain returned bounds. */
1585 if (retbnd)
1586 insert_bndret_call_after (retbnd, retval, &gsi);
1587 gimple_call_set_lhs (call, retval);
1588 update_stmt (call);
1591 else
1592 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1594 /* We don't use return block (there is either no return in function or
1595 multiple of them). So create new basic block with return statement.
1597 else
1599 gimple ret;
1600 if (split_point->split_part_set_retval
1601 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1603 retval = DECL_RESULT (current_function_decl);
1605 if (chkp_function_instrumented_p (current_function_decl)
1606 && BOUNDED_P (retval))
1607 retbnd = create_tmp_reg (pointer_bounds_type_node, NULL);
1609 /* We use temporary register to hold value when aggregate_value_p
1610 is false. Similarly for DECL_BY_REFERENCE we must avoid extra
1611 copy. */
1612 if (!aggregate_value_p (retval, TREE_TYPE (current_function_decl))
1613 && !DECL_BY_REFERENCE (retval))
1614 retval = create_tmp_reg (TREE_TYPE (retval), NULL);
1615 if (is_gimple_reg (retval))
1617 /* When returning by reference, there is only one SSA name
1618 assigned to RESULT_DECL (that is pointer to return value).
1619 Look it up or create new one if it is missing. */
1620 if (DECL_BY_REFERENCE (retval))
1621 retval = get_or_create_ssa_default_def (cfun, retval);
1622 /* Otherwise produce new SSA name for return value. */
1623 else
1624 retval = make_ssa_name (retval, call);
1626 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1627 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1628 else
1629 gimple_call_set_lhs (call, retval);
1631 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1632 /* Build bndret call to obtain returned bounds. */
1633 if (retbnd)
1634 insert_bndret_call_after (retbnd, retval, &gsi);
1635 ret = gimple_build_return (retval);
1636 gsi_insert_after (&gsi, ret, GSI_NEW_STMT);
1639 free_dominance_info (CDI_DOMINATORS);
1640 free_dominance_info (CDI_POST_DOMINATORS);
1641 compute_inline_parameters (node, true);
1644 /* Execute function splitting pass. */
1646 static unsigned int
1647 execute_split_functions (void)
1649 gimple_stmt_iterator bsi;
1650 basic_block bb;
1651 int overall_time = 0, overall_size = 0;
1652 int todo = 0;
1653 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1655 if (flags_from_decl_or_type (current_function_decl)
1656 & (ECF_NORETURN|ECF_MALLOC))
1658 if (dump_file)
1659 fprintf (dump_file, "Not splitting: noreturn/malloc function.\n");
1660 return 0;
1662 if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
1664 if (dump_file)
1665 fprintf (dump_file, "Not splitting: main function.\n");
1666 return 0;
1668 /* This can be relaxed; function might become inlinable after splitting
1669 away the uninlinable part. */
1670 if (inline_edge_summary_vec.exists ()
1671 && !inline_summary (node)->inlinable)
1673 if (dump_file)
1674 fprintf (dump_file, "Not splitting: not inlinable.\n");
1675 return 0;
1677 if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1679 if (dump_file)
1680 fprintf (dump_file, "Not splitting: disregarding inline limits.\n");
1681 return 0;
1683 /* This can be relaxed; most of versioning tests actually prevents
1684 a duplication. */
1685 if (!tree_versionable_function_p (current_function_decl))
1687 if (dump_file)
1688 fprintf (dump_file, "Not splitting: not versionable.\n");
1689 return 0;
1691 /* FIXME: we could support this. */
1692 if (DECL_STRUCT_FUNCTION (current_function_decl)->static_chain_decl)
1694 if (dump_file)
1695 fprintf (dump_file, "Not splitting: nested function.\n");
1696 return 0;
1699 /* See if it makes sense to try to split.
1700 It makes sense to split if we inline, that is if we have direct calls to
1701 handle or direct calls are possibly going to appear as result of indirect
1702 inlining or LTO. Also handle -fprofile-generate as LTO to allow non-LTO
1703 training for LTO -fprofile-use build.
1705 Note that we are not completely conservative about disqualifying functions
1706 called once. It is possible that the caller is called more then once and
1707 then inlining would still benefit. */
1708 if ((!node->callers
1709 /* Local functions called once will be completely inlined most of time. */
1710 || (!node->callers->next_caller && node->local.local))
1711 && !node->address_taken
1712 && (!flag_lto || !node->externally_visible))
1714 if (dump_file)
1715 fprintf (dump_file, "Not splitting: not called directly "
1716 "or called once.\n");
1717 return 0;
1720 /* FIXME: We can actually split if splitting reduces call overhead. */
1721 if (!flag_inline_small_functions
1722 && !DECL_DECLARED_INLINE_P (current_function_decl))
1724 if (dump_file)
1725 fprintf (dump_file, "Not splitting: not autoinlining and function"
1726 " is not inline.\n");
1727 return 0;
1730 /* We enforce splitting after loop headers when profile info is not
1731 available. */
1732 if (profile_status_for_fn (cfun) != PROFILE_READ)
1733 mark_dfs_back_edges ();
1735 /* Initialize bitmap to track forbidden calls. */
1736 forbidden_dominators = BITMAP_ALLOC (NULL);
1737 calculate_dominance_info (CDI_DOMINATORS);
1739 /* Compute local info about basic blocks and determine function size/time. */
1740 bb_info_vec.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
1741 memset (&best_split_point, 0, sizeof (best_split_point));
1742 FOR_EACH_BB_FN (bb, cfun)
1744 int time = 0;
1745 int size = 0;
1746 int freq = compute_call_stmt_bb_frequency (current_function_decl, bb);
1748 if (dump_file && (dump_flags & TDF_DETAILS))
1749 fprintf (dump_file, "Basic block %i\n", bb->index);
1751 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1753 int this_time, this_size;
1754 gimple stmt = gsi_stmt (bsi);
1756 this_size = estimate_num_insns (stmt, &eni_size_weights);
1757 this_time = estimate_num_insns (stmt, &eni_time_weights) * freq;
1758 size += this_size;
1759 time += this_time;
1760 check_forbidden_calls (stmt);
1762 if (dump_file && (dump_flags & TDF_DETAILS))
1764 fprintf (dump_file, " freq:%6i size:%3i time:%3i ",
1765 freq, this_size, this_time);
1766 print_gimple_stmt (dump_file, stmt, 0, 0);
1769 overall_time += time;
1770 overall_size += size;
1771 bb_info_vec[bb->index].time = time;
1772 bb_info_vec[bb->index].size = size;
1774 find_split_points (overall_time, overall_size);
1775 if (best_split_point.split_bbs)
1777 split_function (&best_split_point);
1778 BITMAP_FREE (best_split_point.ssa_names_to_pass);
1779 BITMAP_FREE (best_split_point.split_bbs);
1780 todo = TODO_update_ssa | TODO_cleanup_cfg;
1782 BITMAP_FREE (forbidden_dominators);
1783 bb_info_vec.release ();
1784 return todo;
1787 namespace {
1789 const pass_data pass_data_split_functions =
1791 GIMPLE_PASS, /* type */
1792 "fnsplit", /* name */
1793 OPTGROUP_NONE, /* optinfo_flags */
1794 TV_IPA_FNSPLIT, /* tv_id */
1795 PROP_cfg, /* properties_required */
1796 0, /* properties_provided */
1797 0, /* properties_destroyed */
1798 0, /* todo_flags_start */
1799 0, /* todo_flags_finish */
1802 class pass_split_functions : public gimple_opt_pass
1804 public:
1805 pass_split_functions (gcc::context *ctxt)
1806 : gimple_opt_pass (pass_data_split_functions, ctxt)
1809 /* opt_pass methods: */
1810 virtual bool gate (function *);
1811 virtual unsigned int execute (function *)
1813 return execute_split_functions ();
1816 }; // class pass_split_functions
1818 bool
1819 pass_split_functions::gate (function *)
1821 /* When doing profile feedback, we want to execute the pass after profiling
1822 is read. So disable one in early optimization. */
1823 return (flag_partial_inlining
1824 && !profile_arc_flag && !flag_branch_probabilities);
1827 } // anon namespace
1829 gimple_opt_pass *
1830 make_pass_split_functions (gcc::context *ctxt)
1832 return new pass_split_functions (ctxt);
1835 /* Execute function splitting pass. */
1837 static unsigned int
1838 execute_feedback_split_functions (void)
1840 unsigned int retval = execute_split_functions ();
1841 if (retval)
1842 retval |= TODO_rebuild_cgraph_edges;
1843 return retval;
1846 namespace {
1848 const pass_data pass_data_feedback_split_functions =
1850 GIMPLE_PASS, /* type */
1851 "feedback_fnsplit", /* name */
1852 OPTGROUP_NONE, /* optinfo_flags */
1853 TV_IPA_FNSPLIT, /* tv_id */
1854 PROP_cfg, /* properties_required */
1855 0, /* properties_provided */
1856 0, /* properties_destroyed */
1857 0, /* todo_flags_start */
1858 0, /* todo_flags_finish */
1861 class pass_feedback_split_functions : public gimple_opt_pass
1863 public:
1864 pass_feedback_split_functions (gcc::context *ctxt)
1865 : gimple_opt_pass (pass_data_feedback_split_functions, ctxt)
1868 /* opt_pass methods: */
1869 virtual bool gate (function *);
1870 virtual unsigned int execute (function *)
1872 return execute_feedback_split_functions ();
1875 }; // class pass_feedback_split_functions
1877 bool
1878 pass_feedback_split_functions::gate (function *)
1880 /* We don't need to split when profiling at all, we are producing
1881 lousy code anyway. */
1882 return (flag_partial_inlining
1883 && flag_branch_probabilities);
1886 } // anon namespace
1888 gimple_opt_pass *
1889 make_pass_feedback_split_functions (gcc::context *ctxt)
1891 return new pass_feedback_split_functions (ctxt);