2014-12-20 Martin Uecker <uecker@eecs.berkeley.edu>
[official-gcc.git] / gcc / ipa-split.c
blob276de3c5dfb164a86c68c14cf3946aa6f2d3de6a
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 bb = worklist.pop ();
252 FOR_EACH_EDGE (e, ei, bb->preds)
253 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
254 && bitmap_set_bit (seen, e->src->index))
256 gcc_checking_assert (!bitmap_bit_p (current->split_bbs,
257 e->src->index));
258 worklist.safe_push (e->src);
260 for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
261 gsi_next (&bsi))
263 gimple stmt = gsi_stmt (bsi);
264 if (is_gimple_debug (stmt))
265 continue;
266 if (walk_stmt_load_store_addr_ops
267 (stmt, non_ssa_vars, test_nonssa_use, test_nonssa_use,
268 test_nonssa_use))
270 ok = false;
271 goto done;
273 if (glabel *label_stmt = dyn_cast <glabel *> (stmt))
274 if (test_nonssa_use (stmt, gimple_label_label (label_stmt),
275 NULL_TREE, non_ssa_vars))
277 ok = false;
278 goto done;
281 for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
282 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 (gphi_iterator bsi = gsi_start_phis (return_bb);
297 !gsi_end_p (bsi);
298 gsi_next (&bsi))
300 gphi *stmt = bsi.phi ();
301 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
303 if (virtual_operand_p (gimple_phi_result (stmt)))
304 continue;
305 if (TREE_CODE (op) != SSA_NAME
306 && test_nonssa_use (stmt, op, op, non_ssa_vars))
308 ok = false;
309 goto done;
315 /* Verify that the rest of function does not define any label
316 used by the split part. */
317 FOR_EACH_BB_FN (bb, cfun)
318 if (!bitmap_bit_p (current->split_bbs, bb->index)
319 && !bitmap_bit_p (seen, bb->index))
321 gimple_stmt_iterator bsi;
322 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
323 if (glabel *label_stmt = dyn_cast <glabel *> (gsi_stmt (bsi)))
325 if (test_nonssa_use (label_stmt,
326 gimple_label_label (label_stmt),
327 NULL_TREE, non_ssa_vars))
329 ok = false;
330 goto done;
333 else
334 break;
337 done:
338 BITMAP_FREE (seen);
339 worklist.release ();
340 return ok;
343 /* If STMT is a call, check the callee against a list of forbidden
344 predicate functions. If a match is found, look for uses of the
345 call result in condition statements that compare against zero.
346 For each such use, find the block targeted by the condition
347 statement for the nonzero result, and set the bit for this block
348 in the forbidden dominators bitmap. The purpose of this is to avoid
349 selecting a split point where we are likely to lose the chance
350 to optimize away an unused function call. */
352 static void
353 check_forbidden_calls (gimple stmt)
355 imm_use_iterator use_iter;
356 use_operand_p use_p;
357 tree lhs;
359 /* At the moment, __builtin_constant_p is the only forbidden
360 predicate function call (see PR49642). */
361 if (!gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P))
362 return;
364 lhs = gimple_call_lhs (stmt);
366 if (!lhs || TREE_CODE (lhs) != SSA_NAME)
367 return;
369 FOR_EACH_IMM_USE_FAST (use_p, use_iter, lhs)
371 tree op1;
372 basic_block use_bb, forbidden_bb;
373 enum tree_code code;
374 edge true_edge, false_edge;
375 gcond *use_stmt;
377 use_stmt = dyn_cast <gcond *> (USE_STMT (use_p));
378 if (!use_stmt)
379 continue;
381 /* Assuming canonical form for GIMPLE_COND here, with constant
382 in second position. */
383 op1 = gimple_cond_rhs (use_stmt);
384 code = gimple_cond_code (use_stmt);
385 use_bb = gimple_bb (use_stmt);
387 extract_true_false_edges_from_block (use_bb, &true_edge, &false_edge);
389 /* We're only interested in comparisons that distinguish
390 unambiguously from zero. */
391 if (!integer_zerop (op1) || code == LE_EXPR || code == GE_EXPR)
392 continue;
394 if (code == EQ_EXPR)
395 forbidden_bb = false_edge->dest;
396 else
397 forbidden_bb = true_edge->dest;
399 bitmap_set_bit (forbidden_dominators, forbidden_bb->index);
403 /* If BB is dominated by any block in the forbidden dominators set,
404 return TRUE; else FALSE. */
406 static bool
407 dominated_by_forbidden (basic_block bb)
409 unsigned dom_bb;
410 bitmap_iterator bi;
412 EXECUTE_IF_SET_IN_BITMAP (forbidden_dominators, 1, dom_bb, bi)
414 if (dominated_by_p (CDI_DOMINATORS, bb,
415 BASIC_BLOCK_FOR_FN (cfun, dom_bb)))
416 return true;
419 return false;
422 /* For give split point CURRENT and return block RETURN_BB return 1
423 if ssa name VAL is set by split part and 0 otherwise. */
424 static bool
425 split_part_set_ssa_name_p (tree val, struct split_point *current,
426 basic_block return_bb)
428 if (TREE_CODE (val) != SSA_NAME)
429 return false;
431 return (!SSA_NAME_IS_DEFAULT_DEF (val)
432 && (bitmap_bit_p (current->split_bbs,
433 gimple_bb (SSA_NAME_DEF_STMT (val))->index)
434 || gimple_bb (SSA_NAME_DEF_STMT (val)) == return_bb));
437 /* We found an split_point CURRENT. NON_SSA_VARS is bitmap of all non ssa
438 variables used and RETURN_BB is return basic block.
439 See if we can split function here. */
441 static void
442 consider_split (struct split_point *current, bitmap non_ssa_vars,
443 basic_block return_bb)
445 tree parm;
446 unsigned int num_args = 0;
447 unsigned int call_overhead;
448 edge e;
449 edge_iterator ei;
450 gphi_iterator bsi;
451 unsigned int i;
452 int incoming_freq = 0;
453 tree retval;
454 tree retbnd;
455 bool back_edge = false;
457 if (dump_file && (dump_flags & TDF_DETAILS))
458 dump_split_point (dump_file, current);
460 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
462 if (e->flags & EDGE_DFS_BACK)
463 back_edge = true;
464 if (!bitmap_bit_p (current->split_bbs, e->src->index))
465 incoming_freq += EDGE_FREQUENCY (e);
468 /* Do not split when we would end up calling function anyway. */
469 if (incoming_freq
470 >= (ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency
471 * PARAM_VALUE (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY) / 100))
473 /* When profile is guessed, we can not expect it to give us
474 realistic estimate on likelyness of function taking the
475 complex path. As a special case, when tail of the function is
476 a loop, enable splitting since inlining code skipping the loop
477 is likely noticeable win. */
478 if (back_edge
479 && profile_status_for_fn (cfun) != PROFILE_READ
480 && incoming_freq < ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency)
482 if (dump_file && (dump_flags & TDF_DETAILS))
483 fprintf (dump_file,
484 " Split before loop, accepting despite low frequencies %i %i.\n",
485 incoming_freq,
486 ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency);
488 else
490 if (dump_file && (dump_flags & TDF_DETAILS))
491 fprintf (dump_file,
492 " Refused: incoming frequency is too large.\n");
493 return;
497 if (!current->header_size)
499 if (dump_file && (dump_flags & TDF_DETAILS))
500 fprintf (dump_file, " Refused: header empty\n");
501 return;
504 /* Verify that PHI args on entry are either virtual or all their operands
505 incoming from header are the same. */
506 for (bsi = gsi_start_phis (current->entry_bb); !gsi_end_p (bsi); gsi_next (&bsi))
508 gphi *stmt = bsi.phi ();
509 tree val = NULL;
511 if (virtual_operand_p (gimple_phi_result (stmt)))
512 continue;
513 for (i = 0; i < gimple_phi_num_args (stmt); i++)
515 edge e = gimple_phi_arg_edge (stmt, i);
516 if (!bitmap_bit_p (current->split_bbs, e->src->index))
518 tree edge_val = gimple_phi_arg_def (stmt, i);
519 if (val && edge_val != val)
521 if (dump_file && (dump_flags & TDF_DETAILS))
522 fprintf (dump_file,
523 " Refused: entry BB has PHI with multiple variants\n");
524 return;
526 val = edge_val;
532 /* See what argument we will pass to the split function and compute
533 call overhead. */
534 call_overhead = eni_size_weights.call_cost;
535 for (parm = DECL_ARGUMENTS (current_function_decl); parm;
536 parm = DECL_CHAIN (parm))
538 if (!is_gimple_reg (parm))
540 if (bitmap_bit_p (non_ssa_vars, DECL_UID (parm)))
542 if (dump_file && (dump_flags & TDF_DETAILS))
543 fprintf (dump_file,
544 " Refused: need to pass non-ssa param values\n");
545 return;
548 else
550 tree ddef = ssa_default_def (cfun, parm);
551 if (ddef
552 && bitmap_bit_p (current->ssa_names_to_pass,
553 SSA_NAME_VERSION (ddef)))
555 if (!VOID_TYPE_P (TREE_TYPE (parm)))
556 call_overhead += estimate_move_cost (TREE_TYPE (parm), false);
557 num_args++;
561 if (!VOID_TYPE_P (TREE_TYPE (current_function_decl)))
562 call_overhead += estimate_move_cost (TREE_TYPE (current_function_decl),
563 false);
565 if (current->split_size <= call_overhead)
567 if (dump_file && (dump_flags & TDF_DETAILS))
568 fprintf (dump_file,
569 " Refused: split size is smaller than call overhead\n");
570 return;
572 if (current->header_size + call_overhead
573 >= (unsigned int)(DECL_DECLARED_INLINE_P (current_function_decl)
574 ? MAX_INLINE_INSNS_SINGLE
575 : MAX_INLINE_INSNS_AUTO))
577 if (dump_file && (dump_flags & TDF_DETAILS))
578 fprintf (dump_file,
579 " Refused: header size is too large for inline candidate\n");
580 return;
583 /* FIXME: we currently can pass only SSA function parameters to the split
584 arguments. Once parm_adjustment infrastructure is supported by cloning,
585 we can pass more than that. */
586 if (num_args != bitmap_count_bits (current->ssa_names_to_pass))
589 if (dump_file && (dump_flags & TDF_DETAILS))
590 fprintf (dump_file,
591 " Refused: need to pass non-param values\n");
592 return;
595 /* When there are non-ssa vars used in the split region, see if they
596 are used in the header region. If so, reject the split.
597 FIXME: we can use nested function support to access both. */
598 if (!bitmap_empty_p (non_ssa_vars)
599 && !verify_non_ssa_vars (current, non_ssa_vars, return_bb))
601 if (dump_file && (dump_flags & TDF_DETAILS))
602 fprintf (dump_file,
603 " Refused: split part has non-ssa uses\n");
604 return;
607 /* If the split point is dominated by a forbidden block, reject
608 the split. */
609 if (!bitmap_empty_p (forbidden_dominators)
610 && dominated_by_forbidden (current->entry_bb))
612 if (dump_file && (dump_flags & TDF_DETAILS))
613 fprintf (dump_file,
614 " Refused: split point dominated by forbidden block\n");
615 return;
618 /* See if retval used by return bb is computed by header or split part.
619 When it is computed by split part, we need to produce return statement
620 in the split part and add code to header to pass it around.
622 This is bit tricky to test:
623 1) When there is no return_bb or no return value, we always pass
624 value around.
625 2) Invariants are always computed by caller.
626 3) For SSA we need to look if defining statement is in header or split part
627 4) For non-SSA we need to look where the var is computed. */
628 retval = find_retval (return_bb);
629 if (!retval)
630 current->split_part_set_retval = true;
631 else if (is_gimple_min_invariant (retval))
632 current->split_part_set_retval = false;
633 /* Special case is value returned by reference we record as if it was non-ssa
634 set to result_decl. */
635 else if (TREE_CODE (retval) == SSA_NAME
636 && SSA_NAME_VAR (retval)
637 && TREE_CODE (SSA_NAME_VAR (retval)) == RESULT_DECL
638 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
639 current->split_part_set_retval
640 = bitmap_bit_p (non_ssa_vars, DECL_UID (SSA_NAME_VAR (retval)));
641 else if (TREE_CODE (retval) == SSA_NAME)
642 current->split_part_set_retval
643 = split_part_set_ssa_name_p (retval, current, return_bb);
644 else if (TREE_CODE (retval) == PARM_DECL)
645 current->split_part_set_retval = false;
646 else if (TREE_CODE (retval) == VAR_DECL
647 || TREE_CODE (retval) == RESULT_DECL)
648 current->split_part_set_retval
649 = bitmap_bit_p (non_ssa_vars, DECL_UID (retval));
650 else
651 current->split_part_set_retval = true;
653 /* See if retbnd used by return bb is computed by header or split part. */
654 retbnd = find_retbnd (return_bb);
655 if (retbnd)
657 bool split_part_set_retbnd
658 = split_part_set_ssa_name_p (retbnd, current, return_bb);
660 /* If we have both return value and bounds then keep their definitions
661 in a single function. We use SSA names to link returned bounds and
662 value and therefore do not handle cases when result is passed by
663 reference (which should not be our case anyway since bounds are
664 returned for pointers only). */
665 if ((DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
666 && current->split_part_set_retval)
667 || split_part_set_retbnd != current->split_part_set_retval)
669 if (dump_file && (dump_flags & TDF_DETAILS))
670 fprintf (dump_file,
671 " Refused: split point splits return value and bounds\n");
672 return;
676 /* split_function fixes up at most one PHI non-virtual PHI node in return_bb,
677 for the return value. If there are other PHIs, give up. */
678 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
680 gphi_iterator psi;
682 for (psi = gsi_start_phis (return_bb); !gsi_end_p (psi); gsi_next (&psi))
683 if (!virtual_operand_p (gimple_phi_result (psi.phi ()))
684 && !(retval
685 && current->split_part_set_retval
686 && TREE_CODE (retval) == SSA_NAME
687 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
688 && SSA_NAME_DEF_STMT (retval) == psi.phi ()))
690 if (dump_file && (dump_flags & TDF_DETAILS))
691 fprintf (dump_file,
692 " Refused: return bb has extra PHIs\n");
693 return;
697 if (dump_file && (dump_flags & TDF_DETAILS))
698 fprintf (dump_file, " Accepted!\n");
700 /* At the moment chose split point with lowest frequency and that leaves
701 out smallest size of header.
702 In future we might re-consider this heuristics. */
703 if (!best_split_point.split_bbs
704 || best_split_point.entry_bb->frequency > current->entry_bb->frequency
705 || (best_split_point.entry_bb->frequency == current->entry_bb->frequency
706 && best_split_point.split_size < current->split_size))
709 if (dump_file && (dump_flags & TDF_DETAILS))
710 fprintf (dump_file, " New best split point!\n");
711 if (best_split_point.ssa_names_to_pass)
713 BITMAP_FREE (best_split_point.ssa_names_to_pass);
714 BITMAP_FREE (best_split_point.split_bbs);
716 best_split_point = *current;
717 best_split_point.ssa_names_to_pass = BITMAP_ALLOC (NULL);
718 bitmap_copy (best_split_point.ssa_names_to_pass,
719 current->ssa_names_to_pass);
720 best_split_point.split_bbs = BITMAP_ALLOC (NULL);
721 bitmap_copy (best_split_point.split_bbs, current->split_bbs);
725 /* Return basic block containing RETURN statement. We allow basic blocks
726 of the form:
727 <retval> = tmp_var;
728 return <retval>
729 but return_bb can not be more complex than this.
730 If nothing is found, return the exit block.
732 When there are multiple RETURN statement, chose one with return value,
733 since that one is more likely shared by multiple code paths.
735 Return BB is special, because for function splitting it is the only
736 basic block that is duplicated in between header and split part of the
737 function.
739 TODO: We might support multiple return blocks. */
741 static basic_block
742 find_return_bb (void)
744 edge e;
745 basic_block return_bb = EXIT_BLOCK_PTR_FOR_FN (cfun);
746 gimple_stmt_iterator bsi;
747 bool found_return = false;
748 tree retval = NULL_TREE;
750 if (!single_pred_p (EXIT_BLOCK_PTR_FOR_FN (cfun)))
751 return return_bb;
753 e = single_pred_edge (EXIT_BLOCK_PTR_FOR_FN (cfun));
754 for (bsi = gsi_last_bb (e->src); !gsi_end_p (bsi); gsi_prev (&bsi))
756 gimple stmt = gsi_stmt (bsi);
757 if (gimple_code (stmt) == GIMPLE_LABEL
758 || is_gimple_debug (stmt)
759 || gimple_clobber_p (stmt))
761 else if (gimple_code (stmt) == GIMPLE_ASSIGN
762 && found_return
763 && gimple_assign_single_p (stmt)
764 && (auto_var_in_fn_p (gimple_assign_rhs1 (stmt),
765 current_function_decl)
766 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
767 && retval == gimple_assign_lhs (stmt))
769 else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
771 found_return = true;
772 retval = gimple_return_retval (return_stmt);
774 else
775 break;
777 if (gsi_end_p (bsi) && found_return)
778 return_bb = e->src;
780 return return_bb;
783 /* Given return basic block RETURN_BB, see where return value is really
784 stored. */
785 static tree
786 find_retval (basic_block return_bb)
788 gimple_stmt_iterator bsi;
789 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
790 if (greturn *return_stmt = dyn_cast <greturn *> (gsi_stmt (bsi)))
791 return gimple_return_retval (return_stmt);
792 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
793 && !gimple_clobber_p (gsi_stmt (bsi)))
794 return gimple_assign_rhs1 (gsi_stmt (bsi));
795 return NULL;
798 /* Given return basic block RETURN_BB, see where return bounds are really
799 stored. */
800 static tree
801 find_retbnd (basic_block return_bb)
803 gimple_stmt_iterator bsi;
804 for (bsi = gsi_last_bb (return_bb); !gsi_end_p (bsi); gsi_prev (&bsi))
805 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
806 return gimple_return_retbnd (gsi_stmt (bsi));
807 return NULL;
810 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
811 variable, mark it as used in bitmap passed via DATA.
812 Return true when access to T prevents splitting the function. */
814 static bool
815 mark_nonssa_use (gimple, tree t, tree, void *data)
817 t = get_base_address (t);
819 if (!t || is_gimple_reg (t))
820 return false;
822 /* At present we can't pass non-SSA arguments to split function.
823 FIXME: this can be relaxed by passing references to arguments. */
824 if (TREE_CODE (t) == PARM_DECL)
826 if (dump_file && (dump_flags & TDF_DETAILS))
827 fprintf (dump_file,
828 "Cannot split: use of non-ssa function parameter.\n");
829 return true;
832 if ((TREE_CODE (t) == VAR_DECL
833 && auto_var_in_fn_p (t, current_function_decl))
834 || TREE_CODE (t) == RESULT_DECL
835 || (TREE_CODE (t) == LABEL_DECL
836 && FORCED_LABEL (t)))
837 bitmap_set_bit ((bitmap)data, DECL_UID (t));
839 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
840 to pretend that the value pointed to is actual result decl. */
841 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
842 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
843 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
844 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
845 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
846 return
847 bitmap_bit_p ((bitmap)data,
848 DECL_UID (DECL_RESULT (current_function_decl)));
850 return false;
853 /* Compute local properties of basic block BB we collect when looking for
854 split points. We look for ssa defs and store them in SET_SSA_NAMES,
855 for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic
856 vars stored in NON_SSA_VARS.
858 When BB has edge to RETURN_BB, collect uses in RETURN_BB too.
860 Return false when BB contains something that prevents it from being put into
861 split function. */
863 static bool
864 visit_bb (basic_block bb, basic_block return_bb,
865 bitmap set_ssa_names, bitmap used_ssa_names,
866 bitmap non_ssa_vars)
868 edge e;
869 edge_iterator ei;
870 bool can_split = true;
872 for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
873 gsi_next (&bsi))
875 gimple stmt = gsi_stmt (bsi);
876 tree op;
877 ssa_op_iter iter;
878 tree decl;
880 if (is_gimple_debug (stmt))
881 continue;
883 if (gimple_clobber_p (stmt))
884 continue;
886 /* FIXME: We can split regions containing EH. We can not however
887 split RESX, EH_DISPATCH and EH_POINTER referring to same region
888 into different partitions. This would require tracking of
889 EH regions and checking in consider_split_point if they
890 are not used elsewhere. */
891 if (gimple_code (stmt) == GIMPLE_RESX)
893 if (dump_file && (dump_flags & TDF_DETAILS))
894 fprintf (dump_file, "Cannot split: resx.\n");
895 can_split = false;
897 if (gimple_code (stmt) == GIMPLE_EH_DISPATCH)
899 if (dump_file && (dump_flags & TDF_DETAILS))
900 fprintf (dump_file, "Cannot split: eh dispatch.\n");
901 can_split = false;
904 /* Check builtins that prevent splitting. */
905 if (gimple_code (stmt) == GIMPLE_CALL
906 && (decl = gimple_call_fndecl (stmt)) != NULL_TREE
907 && DECL_BUILT_IN (decl)
908 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
909 switch (DECL_FUNCTION_CODE (decl))
911 /* FIXME: once we will allow passing non-parm values to split part,
912 we need to be sure to handle correct builtin_stack_save and
913 builtin_stack_restore. At the moment we are safe; there is no
914 way to store builtin_stack_save result in non-SSA variable
915 since all calls to those are compiler generated. */
916 case BUILT_IN_APPLY:
917 case BUILT_IN_APPLY_ARGS:
918 case BUILT_IN_VA_START:
919 if (dump_file && (dump_flags & TDF_DETAILS))
920 fprintf (dump_file,
921 "Cannot split: builtin_apply and va_start.\n");
922 can_split = false;
923 break;
924 case BUILT_IN_EH_POINTER:
925 if (dump_file && (dump_flags & TDF_DETAILS))
926 fprintf (dump_file, "Cannot split: builtin_eh_pointer.\n");
927 can_split = false;
928 break;
929 default:
930 break;
933 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
934 bitmap_set_bit (set_ssa_names, SSA_NAME_VERSION (op));
935 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
936 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
937 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
938 mark_nonssa_use,
939 mark_nonssa_use,
940 mark_nonssa_use);
942 for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
943 gsi_next (&bsi))
945 gphi *stmt = bsi.phi ();
946 unsigned int i;
948 if (virtual_operand_p (gimple_phi_result (stmt)))
949 continue;
950 bitmap_set_bit (set_ssa_names,
951 SSA_NAME_VERSION (gimple_phi_result (stmt)));
952 for (i = 0; i < gimple_phi_num_args (stmt); i++)
954 tree op = gimple_phi_arg_def (stmt, i);
955 if (TREE_CODE (op) == SSA_NAME)
956 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
958 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
959 mark_nonssa_use,
960 mark_nonssa_use,
961 mark_nonssa_use);
963 /* Record also uses coming from PHI operand in return BB. */
964 FOR_EACH_EDGE (e, ei, bb->succs)
965 if (e->dest == return_bb)
967 for (gphi_iterator bsi = gsi_start_phis (return_bb);
968 !gsi_end_p (bsi);
969 gsi_next (&bsi))
971 gphi *stmt = bsi.phi ();
972 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
974 if (virtual_operand_p (gimple_phi_result (stmt)))
975 continue;
976 if (TREE_CODE (op) == SSA_NAME)
977 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
978 else
979 can_split &= !mark_nonssa_use (stmt, op, op, non_ssa_vars);
982 return can_split;
985 /* Stack entry for recursive DFS walk in find_split_point. */
987 typedef struct
989 /* Basic block we are examining. */
990 basic_block bb;
992 /* SSA names set and used by the BB and all BBs reachable
993 from it via DFS walk. */
994 bitmap set_ssa_names, used_ssa_names;
995 bitmap non_ssa_vars;
997 /* All BBS visited from this BB via DFS walk. */
998 bitmap bbs_visited;
1000 /* Last examined edge in DFS walk. Since we walk unoriented graph,
1001 the value is up to sum of incoming and outgoing edges of BB. */
1002 unsigned int edge_num;
1004 /* Stack entry index of earliest BB reachable from current BB
1005 or any BB visited later in DFS walk. */
1006 int earliest;
1008 /* Overall time and size of all BBs reached from this BB in DFS walk. */
1009 int overall_time, overall_size;
1011 /* When false we can not split on this BB. */
1012 bool can_split;
1013 } stack_entry;
1016 /* Find all articulations and call consider_split on them.
1017 OVERALL_TIME and OVERALL_SIZE is time and size of the function.
1019 We perform basic algorithm for finding an articulation in a graph
1020 created from CFG by considering it to be an unoriented graph.
1022 The articulation is discovered via DFS walk. We collect earliest
1023 basic block on stack that is reachable via backward edge. Articulation
1024 is any basic block such that there is no backward edge bypassing it.
1025 To reduce stack usage we maintain heap allocated stack in STACK vector.
1026 AUX pointer of BB is set to index it appears in the stack or -1 once
1027 it is visited and popped off the stack.
1029 The algorithm finds articulation after visiting the whole component
1030 reachable by it. This makes it convenient to collect information about
1031 the component used by consider_split. */
1033 static void
1034 find_split_points (int overall_time, int overall_size)
1036 stack_entry first;
1037 vec<stack_entry> stack = vNULL;
1038 basic_block bb;
1039 basic_block return_bb = find_return_bb ();
1040 struct split_point current;
1042 current.header_time = overall_time;
1043 current.header_size = overall_size;
1044 current.split_time = 0;
1045 current.split_size = 0;
1046 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
1048 first.bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1049 first.edge_num = 0;
1050 first.overall_time = 0;
1051 first.overall_size = 0;
1052 first.earliest = INT_MAX;
1053 first.set_ssa_names = 0;
1054 first.used_ssa_names = 0;
1055 first.non_ssa_vars = 0;
1056 first.bbs_visited = 0;
1057 first.can_split = false;
1058 stack.safe_push (first);
1059 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = (void *)(intptr_t)-1;
1061 while (!stack.is_empty ())
1063 stack_entry *entry = &stack.last ();
1065 /* We are walking an acyclic graph, so edge_num counts
1066 succ and pred edges together. However when considering
1067 articulation, we want to have processed everything reachable
1068 from articulation but nothing that reaches into it. */
1069 if (entry->edge_num == EDGE_COUNT (entry->bb->succs)
1070 && entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1072 int pos = stack.length ();
1073 entry->can_split &= visit_bb (entry->bb, return_bb,
1074 entry->set_ssa_names,
1075 entry->used_ssa_names,
1076 entry->non_ssa_vars);
1077 if (pos <= entry->earliest && !entry->can_split
1078 && dump_file && (dump_flags & TDF_DETAILS))
1079 fprintf (dump_file,
1080 "found articulation at bb %i but can not split\n",
1081 entry->bb->index);
1082 if (pos <= entry->earliest && entry->can_split)
1084 if (dump_file && (dump_flags & TDF_DETAILS))
1085 fprintf (dump_file, "found articulation at bb %i\n",
1086 entry->bb->index);
1087 current.entry_bb = entry->bb;
1088 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
1089 bitmap_and_compl (current.ssa_names_to_pass,
1090 entry->used_ssa_names, entry->set_ssa_names);
1091 current.header_time = overall_time - entry->overall_time;
1092 current.header_size = overall_size - entry->overall_size;
1093 current.split_time = entry->overall_time;
1094 current.split_size = entry->overall_size;
1095 current.split_bbs = entry->bbs_visited;
1096 consider_split (&current, entry->non_ssa_vars, return_bb);
1097 BITMAP_FREE (current.ssa_names_to_pass);
1100 /* Do actual DFS walk. */
1101 if (entry->edge_num
1102 < (EDGE_COUNT (entry->bb->succs)
1103 + EDGE_COUNT (entry->bb->preds)))
1105 edge e;
1106 basic_block dest;
1107 if (entry->edge_num < EDGE_COUNT (entry->bb->succs))
1109 e = EDGE_SUCC (entry->bb, entry->edge_num);
1110 dest = e->dest;
1112 else
1114 e = EDGE_PRED (entry->bb, entry->edge_num
1115 - EDGE_COUNT (entry->bb->succs));
1116 dest = e->src;
1119 entry->edge_num++;
1121 /* New BB to visit, push it to the stack. */
1122 if (dest != return_bb && dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
1123 && !dest->aux)
1125 stack_entry new_entry;
1127 new_entry.bb = dest;
1128 new_entry.edge_num = 0;
1129 new_entry.overall_time
1130 = bb_info_vec[dest->index].time;
1131 new_entry.overall_size
1132 = bb_info_vec[dest->index].size;
1133 new_entry.earliest = INT_MAX;
1134 new_entry.set_ssa_names = BITMAP_ALLOC (NULL);
1135 new_entry.used_ssa_names = BITMAP_ALLOC (NULL);
1136 new_entry.bbs_visited = BITMAP_ALLOC (NULL);
1137 new_entry.non_ssa_vars = BITMAP_ALLOC (NULL);
1138 new_entry.can_split = true;
1139 bitmap_set_bit (new_entry.bbs_visited, dest->index);
1140 stack.safe_push (new_entry);
1141 dest->aux = (void *)(intptr_t)stack.length ();
1143 /* Back edge found, record the earliest point. */
1144 else if ((intptr_t)dest->aux > 0
1145 && (intptr_t)dest->aux < entry->earliest)
1146 entry->earliest = (intptr_t)dest->aux;
1148 /* We are done with examining the edges. Pop off the value from stack
1149 and merge stuff we accumulate during the walk. */
1150 else if (entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1152 stack_entry *prev = &stack[stack.length () - 2];
1154 entry->bb->aux = (void *)(intptr_t)-1;
1155 prev->can_split &= entry->can_split;
1156 if (prev->set_ssa_names)
1158 bitmap_ior_into (prev->set_ssa_names, entry->set_ssa_names);
1159 bitmap_ior_into (prev->used_ssa_names, entry->used_ssa_names);
1160 bitmap_ior_into (prev->bbs_visited, entry->bbs_visited);
1161 bitmap_ior_into (prev->non_ssa_vars, entry->non_ssa_vars);
1163 if (prev->earliest > entry->earliest)
1164 prev->earliest = entry->earliest;
1165 prev->overall_time += entry->overall_time;
1166 prev->overall_size += entry->overall_size;
1167 BITMAP_FREE (entry->set_ssa_names);
1168 BITMAP_FREE (entry->used_ssa_names);
1169 BITMAP_FREE (entry->bbs_visited);
1170 BITMAP_FREE (entry->non_ssa_vars);
1171 stack.pop ();
1173 else
1174 stack.pop ();
1176 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = NULL;
1177 FOR_EACH_BB_FN (bb, cfun)
1178 bb->aux = NULL;
1179 stack.release ();
1180 BITMAP_FREE (current.ssa_names_to_pass);
1183 /* Build and insert initialization of returned bounds RETBND
1184 for returned value RETVAL. Statements are inserted after
1185 a statement pointed by GSI and GSI is modified to point to
1186 the last inserted statement. */
1188 static void
1189 insert_bndret_call_after (tree retbnd, tree retval, gimple_stmt_iterator *gsi)
1191 tree fndecl = targetm.builtin_chkp_function (BUILT_IN_CHKP_BNDRET);
1192 gimple bndret = gimple_build_call (fndecl, 1, retval);
1193 gimple_call_set_lhs (bndret, retbnd);
1194 gsi_insert_after (gsi, bndret, GSI_CONTINUE_LINKING);
1196 /* Split function at SPLIT_POINT. */
1198 static void
1199 split_function (struct split_point *split_point)
1201 vec<tree> args_to_pass = vNULL;
1202 bitmap args_to_skip;
1203 tree parm;
1204 int num = 0;
1205 cgraph_node *node, *cur_node = cgraph_node::get (current_function_decl);
1206 basic_block return_bb = find_return_bb ();
1207 basic_block call_bb;
1208 gcall *call;
1209 edge e;
1210 edge_iterator ei;
1211 tree retval = NULL, real_retval = NULL, retbnd = NULL;
1212 bool split_part_return_p = false;
1213 bool with_bounds = chkp_function_instrumented_p (current_function_decl);
1214 gimple last_stmt = NULL;
1215 unsigned int i;
1216 tree arg, ddef;
1217 vec<tree, va_gc> **debug_args = NULL;
1219 if (dump_file)
1221 fprintf (dump_file, "\n\nSplitting function at:\n");
1222 dump_split_point (dump_file, split_point);
1225 if (cur_node->local.can_change_signature)
1226 args_to_skip = BITMAP_ALLOC (NULL);
1227 else
1228 args_to_skip = NULL;
1230 /* Collect the parameters of new function and args_to_skip bitmap. */
1231 for (parm = DECL_ARGUMENTS (current_function_decl);
1232 parm; parm = DECL_CHAIN (parm), num++)
1233 if (args_to_skip
1234 && (!is_gimple_reg (parm)
1235 || (ddef = ssa_default_def (cfun, parm)) == NULL_TREE
1236 || !bitmap_bit_p (split_point->ssa_names_to_pass,
1237 SSA_NAME_VERSION (ddef))))
1238 bitmap_set_bit (args_to_skip, num);
1239 else
1241 /* This parm might not have been used up to now, but is going to be
1242 used, hence register it. */
1243 if (is_gimple_reg (parm))
1244 arg = get_or_create_ssa_default_def (cfun, parm);
1245 else
1246 arg = parm;
1248 if (!useless_type_conversion_p (DECL_ARG_TYPE (parm), TREE_TYPE (arg)))
1249 arg = fold_convert (DECL_ARG_TYPE (parm), arg);
1250 args_to_pass.safe_push (arg);
1253 /* See if the split function will return. */
1254 FOR_EACH_EDGE (e, ei, return_bb->preds)
1255 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1256 break;
1257 if (e)
1258 split_part_return_p = true;
1260 /* Add return block to what will become the split function.
1261 We do not return; no return block is needed. */
1262 if (!split_part_return_p)
1264 /* We have no return block, so nothing is needed. */
1265 else if (return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1267 /* When we do not want to return value, we need to construct
1268 new return block with empty return statement.
1269 FIXME: Once we are able to change return type, we should change function
1270 to return void instead of just outputting function with undefined return
1271 value. For structures this affects quality of codegen. */
1272 else if (!split_point->split_part_set_retval
1273 && find_retval (return_bb))
1275 bool redirected = true;
1276 basic_block new_return_bb = create_basic_block (NULL, 0, return_bb);
1277 gimple_stmt_iterator gsi = gsi_start_bb (new_return_bb);
1278 gsi_insert_after (&gsi, gimple_build_return (NULL), GSI_NEW_STMT);
1279 while (redirected)
1281 redirected = false;
1282 FOR_EACH_EDGE (e, ei, return_bb->preds)
1283 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1285 new_return_bb->count += e->count;
1286 new_return_bb->frequency += EDGE_FREQUENCY (e);
1287 redirect_edge_and_branch (e, new_return_bb);
1288 redirected = true;
1289 break;
1292 e = make_edge (new_return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1293 e->probability = REG_BR_PROB_BASE;
1294 e->count = new_return_bb->count;
1295 add_bb_to_loop (new_return_bb, current_loops->tree_root);
1296 bitmap_set_bit (split_point->split_bbs, new_return_bb->index);
1298 /* When we pass around the value, use existing return block. */
1299 else
1300 bitmap_set_bit (split_point->split_bbs, return_bb->index);
1302 /* If RETURN_BB has virtual operand PHIs, they must be removed and the
1303 virtual operand marked for renaming as we change the CFG in a way that
1304 tree-inline is not able to compensate for.
1306 Note this can happen whether or not we have a return value. If we have
1307 a return value, then RETURN_BB may have PHIs for real operands too. */
1308 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1310 bool phi_p = false;
1311 for (gphi_iterator gsi = gsi_start_phis (return_bb);
1312 !gsi_end_p (gsi);)
1314 gphi *stmt = gsi.phi ();
1315 if (!virtual_operand_p (gimple_phi_result (stmt)))
1317 gsi_next (&gsi);
1318 continue;
1320 mark_virtual_phi_result_for_renaming (stmt);
1321 remove_phi_node (&gsi, true);
1322 phi_p = true;
1324 /* In reality we have to rename the reaching definition of the
1325 virtual operand at return_bb as we will eventually release it
1326 when we remove the code region we outlined.
1327 So we have to rename all immediate virtual uses of that region
1328 if we didn't see a PHI definition yet. */
1329 /* ??? In real reality we want to set the reaching vdef of the
1330 entry of the SESE region as the vuse of the call and the reaching
1331 vdef of the exit of the SESE region as the vdef of the call. */
1332 if (!phi_p)
1333 for (gimple_stmt_iterator gsi = gsi_start_bb (return_bb);
1334 !gsi_end_p (gsi);
1335 gsi_next (&gsi))
1337 gimple stmt = gsi_stmt (gsi);
1338 if (gimple_vuse (stmt))
1340 gimple_set_vuse (stmt, NULL_TREE);
1341 update_stmt (stmt);
1343 if (gimple_vdef (stmt))
1344 break;
1348 /* Now create the actual clone. */
1349 cgraph_edge::rebuild_edges ();
1350 node = cur_node->create_version_clone_with_body
1351 (vNULL, NULL, args_to_skip, !split_part_return_p, split_point->split_bbs,
1352 split_point->entry_bb, "part");
1354 /* Let's take a time profile for splitted function. */
1355 node->tp_first_run = cur_node->tp_first_run + 1;
1357 /* For usual cloning it is enough to clear builtin only when signature
1358 changes. For partial inlining we however can not expect the part
1359 of builtin implementation to have same semantic as the whole. */
1360 if (DECL_BUILT_IN (node->decl))
1362 DECL_BUILT_IN_CLASS (node->decl) = NOT_BUILT_IN;
1363 DECL_FUNCTION_CODE (node->decl) = (enum built_in_function) 0;
1366 /* If the original function is instrumented then it's
1367 part is also instrumented. */
1368 if (with_bounds)
1369 chkp_function_mark_instrumented (node->decl);
1371 /* If the original function is declared inline, there is no point in issuing
1372 a warning for the non-inlinable part. */
1373 DECL_NO_INLINE_WARNING_P (node->decl) = 1;
1374 cur_node->remove_callees ();
1375 cur_node->remove_all_references ();
1376 if (!split_part_return_p)
1377 TREE_THIS_VOLATILE (node->decl) = 1;
1378 if (dump_file)
1379 dump_function_to_file (node->decl, dump_file, dump_flags);
1381 /* Create the basic block we place call into. It is the entry basic block
1382 split after last label. */
1383 call_bb = split_point->entry_bb;
1384 for (gimple_stmt_iterator gsi = gsi_start_bb (call_bb); !gsi_end_p (gsi);)
1385 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
1387 last_stmt = gsi_stmt (gsi);
1388 gsi_next (&gsi);
1390 else
1391 break;
1392 e = split_block (split_point->entry_bb, last_stmt);
1393 remove_edge (e);
1395 /* Produce the call statement. */
1396 gimple_stmt_iterator gsi = gsi_last_bb (call_bb);
1397 FOR_EACH_VEC_ELT (args_to_pass, i, arg)
1398 if (!is_gimple_val (arg))
1400 arg = force_gimple_operand_gsi (&gsi, arg, true, NULL_TREE,
1401 false, GSI_CONTINUE_LINKING);
1402 args_to_pass[i] = arg;
1404 call = gimple_build_call_vec (node->decl, args_to_pass);
1405 gimple_call_set_with_bounds (call, with_bounds);
1406 gimple_set_block (call, DECL_INITIAL (current_function_decl));
1407 args_to_pass.release ();
1409 /* For optimized away parameters, add on the caller side
1410 before the call
1411 DEBUG D#X => parm_Y(D)
1412 stmts and associate D#X with parm in decl_debug_args_lookup
1413 vector to say for debug info that if parameter parm had been passed,
1414 it would have value parm_Y(D). */
1415 if (args_to_skip)
1416 for (parm = DECL_ARGUMENTS (current_function_decl), num = 0;
1417 parm; parm = DECL_CHAIN (parm), num++)
1418 if (bitmap_bit_p (args_to_skip, num)
1419 && is_gimple_reg (parm))
1421 tree ddecl;
1422 gimple def_temp;
1424 /* This needs to be done even without MAY_HAVE_DEBUG_STMTS,
1425 otherwise if it didn't exist before, we'd end up with
1426 different SSA_NAME_VERSIONs between -g and -g0. */
1427 arg = get_or_create_ssa_default_def (cfun, parm);
1428 if (!MAY_HAVE_DEBUG_STMTS)
1429 continue;
1431 if (debug_args == NULL)
1432 debug_args = decl_debug_args_insert (node->decl);
1433 ddecl = make_node (DEBUG_EXPR_DECL);
1434 DECL_ARTIFICIAL (ddecl) = 1;
1435 TREE_TYPE (ddecl) = TREE_TYPE (parm);
1436 DECL_MODE (ddecl) = DECL_MODE (parm);
1437 vec_safe_push (*debug_args, DECL_ORIGIN (parm));
1438 vec_safe_push (*debug_args, ddecl);
1439 def_temp = gimple_build_debug_bind (ddecl, unshare_expr (arg),
1440 call);
1441 gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
1443 /* And on the callee side, add
1444 DEBUG D#Y s=> parm
1445 DEBUG var => D#Y
1446 stmts to the first bb where var is a VAR_DECL created for the
1447 optimized away parameter in DECL_INITIAL block. This hints
1448 in the debug info that var (whole DECL_ORIGIN is the parm PARM_DECL)
1449 is optimized away, but could be looked up at the call site
1450 as value of D#X there. */
1451 if (debug_args != NULL)
1453 unsigned int i;
1454 tree var, vexpr;
1455 gimple_stmt_iterator cgsi;
1456 gimple def_temp;
1458 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1459 var = BLOCK_VARS (DECL_INITIAL (node->decl));
1460 i = vec_safe_length (*debug_args);
1461 cgsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1464 i -= 2;
1465 while (var != NULL_TREE
1466 && DECL_ABSTRACT_ORIGIN (var) != (**debug_args)[i])
1467 var = TREE_CHAIN (var);
1468 if (var == NULL_TREE)
1469 break;
1470 vexpr = make_node (DEBUG_EXPR_DECL);
1471 parm = (**debug_args)[i];
1472 DECL_ARTIFICIAL (vexpr) = 1;
1473 TREE_TYPE (vexpr) = TREE_TYPE (parm);
1474 DECL_MODE (vexpr) = DECL_MODE (parm);
1475 def_temp = gimple_build_debug_source_bind (vexpr, parm,
1476 NULL);
1477 gsi_insert_before (&cgsi, def_temp, GSI_SAME_STMT);
1478 def_temp = gimple_build_debug_bind (var, vexpr, NULL);
1479 gsi_insert_before (&cgsi, def_temp, GSI_SAME_STMT);
1481 while (i);
1482 pop_cfun ();
1485 /* We avoid address being taken on any variable used by split part,
1486 so return slot optimization is always possible. Moreover this is
1487 required to make DECL_BY_REFERENCE work. */
1488 if (aggregate_value_p (DECL_RESULT (current_function_decl),
1489 TREE_TYPE (current_function_decl))
1490 && (!is_gimple_reg_type (TREE_TYPE (DECL_RESULT (current_function_decl)))
1491 || DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))))
1492 gimple_call_set_return_slot_opt (call, true);
1494 /* Update return value. This is bit tricky. When we do not return,
1495 do nothing. When we return we might need to update return_bb
1496 or produce a new return statement. */
1497 if (!split_part_return_p)
1498 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1499 else
1501 e = make_edge (call_bb, return_bb,
1502 return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
1503 ? 0 : EDGE_FALLTHRU);
1504 e->count = call_bb->count;
1505 e->probability = REG_BR_PROB_BASE;
1507 /* If there is return basic block, see what value we need to store
1508 return value into and put call just before it. */
1509 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1511 real_retval = retval = find_retval (return_bb);
1512 retbnd = find_retbnd (return_bb);
1514 if (real_retval && split_point->split_part_set_retval)
1516 gphi_iterator psi;
1518 /* See if we need new SSA_NAME for the result.
1519 When DECL_BY_REFERENCE is true, retval is actually pointer to
1520 return value and it is constant in whole function. */
1521 if (TREE_CODE (retval) == SSA_NAME
1522 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1524 retval = copy_ssa_name (retval, call);
1526 /* See if there is PHI defining return value. */
1527 for (psi = gsi_start_phis (return_bb);
1528 !gsi_end_p (psi); gsi_next (&psi))
1529 if (!virtual_operand_p (gimple_phi_result (psi.phi ())))
1530 break;
1532 /* When there is PHI, just update its value. */
1533 if (TREE_CODE (retval) == SSA_NAME
1534 && !gsi_end_p (psi))
1535 add_phi_arg (psi.phi (), retval, e, UNKNOWN_LOCATION);
1536 /* Otherwise update the return BB itself.
1537 find_return_bb allows at most one assignment to return value,
1538 so update first statement. */
1539 else
1541 gimple_stmt_iterator bsi;
1542 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi);
1543 gsi_next (&bsi))
1544 if (greturn *return_stmt
1545 = dyn_cast <greturn *> (gsi_stmt (bsi)))
1547 gimple_return_set_retval (return_stmt, retval);
1548 break;
1550 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
1551 && !gimple_clobber_p (gsi_stmt (bsi)))
1553 gimple_assign_set_rhs1 (gsi_stmt (bsi), retval);
1554 break;
1556 update_stmt (gsi_stmt (bsi));
1559 /* Replace retbnd with new one. */
1560 if (retbnd)
1562 gimple_stmt_iterator bsi;
1563 for (bsi = gsi_last_bb (return_bb); !gsi_end_p (bsi);
1564 gsi_prev (&bsi))
1565 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
1567 retbnd = copy_ssa_name (retbnd, call);
1568 gimple_return_set_retbnd (gsi_stmt (bsi), retbnd);
1569 update_stmt (gsi_stmt (bsi));
1570 break;
1574 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1576 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1577 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1579 else
1581 tree restype;
1582 restype = TREE_TYPE (DECL_RESULT (current_function_decl));
1583 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1584 if (!useless_type_conversion_p (TREE_TYPE (retval), restype))
1586 gimple cpy;
1587 tree tem = create_tmp_reg (restype);
1588 tem = make_ssa_name (tem, call);
1589 cpy = gimple_build_assign (retval, NOP_EXPR, tem);
1590 gsi_insert_after (&gsi, cpy, GSI_NEW_STMT);
1591 retval = tem;
1593 /* Build bndret call to obtain returned bounds. */
1594 if (retbnd)
1595 insert_bndret_call_after (retbnd, retval, &gsi);
1596 gimple_call_set_lhs (call, retval);
1597 update_stmt (call);
1600 else
1601 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1603 /* We don't use return block (there is either no return in function or
1604 multiple of them). So create new basic block with return statement.
1606 else
1608 greturn *ret;
1609 if (split_point->split_part_set_retval
1610 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1612 retval = DECL_RESULT (current_function_decl);
1614 if (chkp_function_instrumented_p (current_function_decl)
1615 && BOUNDED_P (retval))
1616 retbnd = create_tmp_reg (pointer_bounds_type_node);
1618 /* We use temporary register to hold value when aggregate_value_p
1619 is false. Similarly for DECL_BY_REFERENCE we must avoid extra
1620 copy. */
1621 if (!aggregate_value_p (retval, TREE_TYPE (current_function_decl))
1622 && !DECL_BY_REFERENCE (retval))
1623 retval = create_tmp_reg (TREE_TYPE (retval));
1624 if (is_gimple_reg (retval))
1626 /* When returning by reference, there is only one SSA name
1627 assigned to RESULT_DECL (that is pointer to return value).
1628 Look it up or create new one if it is missing. */
1629 if (DECL_BY_REFERENCE (retval))
1630 retval = get_or_create_ssa_default_def (cfun, retval);
1631 /* Otherwise produce new SSA name for return value. */
1632 else
1633 retval = make_ssa_name (retval, call);
1635 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1636 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1637 else
1638 gimple_call_set_lhs (call, retval);
1640 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1641 /* Build bndret call to obtain returned bounds. */
1642 if (retbnd)
1643 insert_bndret_call_after (retbnd, retval, &gsi);
1644 ret = gimple_build_return (retval);
1645 gsi_insert_after (&gsi, ret, GSI_NEW_STMT);
1648 free_dominance_info (CDI_DOMINATORS);
1649 free_dominance_info (CDI_POST_DOMINATORS);
1650 compute_inline_parameters (node, true);
1653 /* Execute function splitting pass. */
1655 static unsigned int
1656 execute_split_functions (void)
1658 gimple_stmt_iterator bsi;
1659 basic_block bb;
1660 int overall_time = 0, overall_size = 0;
1661 int todo = 0;
1662 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1664 if (flags_from_decl_or_type (current_function_decl)
1665 & (ECF_NORETURN|ECF_MALLOC))
1667 if (dump_file)
1668 fprintf (dump_file, "Not splitting: noreturn/malloc function.\n");
1669 return 0;
1671 if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
1673 if (dump_file)
1674 fprintf (dump_file, "Not splitting: main function.\n");
1675 return 0;
1677 /* This can be relaxed; function might become inlinable after splitting
1678 away the uninlinable part. */
1679 if (inline_edge_summary_vec.exists ()
1680 && !inline_summary (node)->inlinable)
1682 if (dump_file)
1683 fprintf (dump_file, "Not splitting: not inlinable.\n");
1684 return 0;
1686 if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1688 if (dump_file)
1689 fprintf (dump_file, "Not splitting: disregarding inline limits.\n");
1690 return 0;
1692 /* This can be relaxed; most of versioning tests actually prevents
1693 a duplication. */
1694 if (!tree_versionable_function_p (current_function_decl))
1696 if (dump_file)
1697 fprintf (dump_file, "Not splitting: not versionable.\n");
1698 return 0;
1700 /* FIXME: we could support this. */
1701 if (DECL_STRUCT_FUNCTION (current_function_decl)->static_chain_decl)
1703 if (dump_file)
1704 fprintf (dump_file, "Not splitting: nested function.\n");
1705 return 0;
1708 /* See if it makes sense to try to split.
1709 It makes sense to split if we inline, that is if we have direct calls to
1710 handle or direct calls are possibly going to appear as result of indirect
1711 inlining or LTO. Also handle -fprofile-generate as LTO to allow non-LTO
1712 training for LTO -fprofile-use build.
1714 Note that we are not completely conservative about disqualifying functions
1715 called once. It is possible that the caller is called more then once and
1716 then inlining would still benefit. */
1717 if ((!node->callers
1718 /* Local functions called once will be completely inlined most of time. */
1719 || (!node->callers->next_caller && node->local.local))
1720 && !node->address_taken
1721 && (!flag_lto || !node->externally_visible))
1723 if (dump_file)
1724 fprintf (dump_file, "Not splitting: not called directly "
1725 "or called once.\n");
1726 return 0;
1729 /* FIXME: We can actually split if splitting reduces call overhead. */
1730 if (!flag_inline_small_functions
1731 && !DECL_DECLARED_INLINE_P (current_function_decl))
1733 if (dump_file)
1734 fprintf (dump_file, "Not splitting: not autoinlining and function"
1735 " is not inline.\n");
1736 return 0;
1739 /* We enforce splitting after loop headers when profile info is not
1740 available. */
1741 if (profile_status_for_fn (cfun) != PROFILE_READ)
1742 mark_dfs_back_edges ();
1744 /* Initialize bitmap to track forbidden calls. */
1745 forbidden_dominators = BITMAP_ALLOC (NULL);
1746 calculate_dominance_info (CDI_DOMINATORS);
1748 /* Compute local info about basic blocks and determine function size/time. */
1749 bb_info_vec.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
1750 memset (&best_split_point, 0, sizeof (best_split_point));
1751 FOR_EACH_BB_FN (bb, cfun)
1753 int time = 0;
1754 int size = 0;
1755 int freq = compute_call_stmt_bb_frequency (current_function_decl, bb);
1757 if (dump_file && (dump_flags & TDF_DETAILS))
1758 fprintf (dump_file, "Basic block %i\n", bb->index);
1760 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1762 int this_time, this_size;
1763 gimple stmt = gsi_stmt (bsi);
1765 this_size = estimate_num_insns (stmt, &eni_size_weights);
1766 this_time = estimate_num_insns (stmt, &eni_time_weights) * freq;
1767 size += this_size;
1768 time += this_time;
1769 check_forbidden_calls (stmt);
1771 if (dump_file && (dump_flags & TDF_DETAILS))
1773 fprintf (dump_file, " freq:%6i size:%3i time:%3i ",
1774 freq, this_size, this_time);
1775 print_gimple_stmt (dump_file, stmt, 0, 0);
1778 overall_time += time;
1779 overall_size += size;
1780 bb_info_vec[bb->index].time = time;
1781 bb_info_vec[bb->index].size = size;
1783 find_split_points (overall_time, overall_size);
1784 if (best_split_point.split_bbs)
1786 split_function (&best_split_point);
1787 BITMAP_FREE (best_split_point.ssa_names_to_pass);
1788 BITMAP_FREE (best_split_point.split_bbs);
1789 todo = TODO_update_ssa | TODO_cleanup_cfg;
1791 BITMAP_FREE (forbidden_dominators);
1792 bb_info_vec.release ();
1793 return todo;
1796 namespace {
1798 const pass_data pass_data_split_functions =
1800 GIMPLE_PASS, /* type */
1801 "fnsplit", /* name */
1802 OPTGROUP_NONE, /* optinfo_flags */
1803 TV_IPA_FNSPLIT, /* tv_id */
1804 PROP_cfg, /* properties_required */
1805 0, /* properties_provided */
1806 0, /* properties_destroyed */
1807 0, /* todo_flags_start */
1808 0, /* todo_flags_finish */
1811 class pass_split_functions : public gimple_opt_pass
1813 public:
1814 pass_split_functions (gcc::context *ctxt)
1815 : gimple_opt_pass (pass_data_split_functions, ctxt)
1818 /* opt_pass methods: */
1819 virtual bool gate (function *);
1820 virtual unsigned int execute (function *)
1822 return execute_split_functions ();
1825 }; // class pass_split_functions
1827 bool
1828 pass_split_functions::gate (function *)
1830 /* When doing profile feedback, we want to execute the pass after profiling
1831 is read. So disable one in early optimization. */
1832 return (flag_partial_inlining
1833 && !profile_arc_flag && !flag_branch_probabilities);
1836 } // anon namespace
1838 gimple_opt_pass *
1839 make_pass_split_functions (gcc::context *ctxt)
1841 return new pass_split_functions (ctxt);
1844 /* Execute function splitting pass. */
1846 static unsigned int
1847 execute_feedback_split_functions (void)
1849 unsigned int retval = execute_split_functions ();
1850 if (retval)
1851 retval |= TODO_rebuild_cgraph_edges;
1852 return retval;
1855 namespace {
1857 const pass_data pass_data_feedback_split_functions =
1859 GIMPLE_PASS, /* type */
1860 "feedback_fnsplit", /* name */
1861 OPTGROUP_NONE, /* optinfo_flags */
1862 TV_IPA_FNSPLIT, /* tv_id */
1863 PROP_cfg, /* properties_required */
1864 0, /* properties_provided */
1865 0, /* properties_destroyed */
1866 0, /* todo_flags_start */
1867 0, /* todo_flags_finish */
1870 class pass_feedback_split_functions : public gimple_opt_pass
1872 public:
1873 pass_feedback_split_functions (gcc::context *ctxt)
1874 : gimple_opt_pass (pass_data_feedback_split_functions, ctxt)
1877 /* opt_pass methods: */
1878 virtual bool gate (function *);
1879 virtual unsigned int execute (function *)
1881 return execute_feedback_split_functions ();
1884 }; // class pass_feedback_split_functions
1886 bool
1887 pass_feedback_split_functions::gate (function *)
1889 /* We don't need to split when profiling at all, we are producing
1890 lousy code anyway. */
1891 return (flag_partial_inlining
1892 && flag_branch_probabilities);
1895 } // anon namespace
1897 gimple_opt_pass *
1898 make_pass_feedback_split_functions (gcc::context *ctxt)
1900 return new pass_feedback_split_functions (ctxt);