Implement TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS hook.
[official-gcc.git] / gcc / ipa-split.c
bloba4ff47765a2cca666e47106984f50d2ec501c77c
1 /* Function splitting pass
2 Copyright (C) 2010-2015 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 "input.h"
81 #include "alias.h"
82 #include "symtab.h"
83 #include "options.h"
84 #include "tree.h"
85 #include "fold-const.h"
86 #include "predict.h"
87 #include "tm.h"
88 #include "hard-reg-set.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 "rtl.h"
101 #include "flags.h"
102 #include "insn-config.h"
103 #include "expmed.h"
104 #include "dojump.h"
105 #include "explow.h"
106 #include "calls.h"
107 #include "emit-rtl.h"
108 #include "varasm.h"
109 #include "stmt.h"
110 #include "expr.h"
111 #include "gimplify.h"
112 #include "gimple-iterator.h"
113 #include "gimplify-me.h"
114 #include "gimple-walk.h"
115 #include "target.h"
116 #include "plugin-api.h"
117 #include "ipa-ref.h"
118 #include "cgraph.h"
119 #include "alloc-pool.h"
120 #include "symbol-summary.h"
121 #include "ipa-prop.h"
122 #include "gimple-ssa.h"
123 #include "tree-cfg.h"
124 #include "tree-phinodes.h"
125 #include "ssa-iterators.h"
126 #include "tree-ssanames.h"
127 #include "tree-into-ssa.h"
128 #include "tree-dfa.h"
129 #include "tree-pass.h"
130 #include "diagnostic.h"
131 #include "tree-dump.h"
132 #include "tree-inline.h"
133 #include "params.h"
134 #include "gimple-pretty-print.h"
135 #include "ipa-inline.h"
136 #include "cfgloop.h"
137 #include "tree-chkp.h"
139 /* Per basic block info. */
141 typedef struct
143 unsigned int size;
144 unsigned int time;
145 } split_bb_info;
147 static vec<split_bb_info> bb_info_vec;
149 /* Description of split point. */
151 struct split_point
153 /* Size of the partitions. */
154 unsigned int header_time, header_size, split_time, split_size;
156 /* SSA names that need to be passed into spit function. */
157 bitmap ssa_names_to_pass;
159 /* Basic block where we split (that will become entry point of new function. */
160 basic_block entry_bb;
162 /* Basic blocks we are splitting away. */
163 bitmap split_bbs;
165 /* True when return value is computed on split part and thus it needs
166 to be returned. */
167 bool split_part_set_retval;
170 /* Best split point found. */
172 struct split_point best_split_point;
174 /* Set of basic blocks that are not allowed to dominate a split point. */
176 static bitmap forbidden_dominators;
178 static tree find_retval (basic_block return_bb);
179 static tree find_retbnd (basic_block return_bb);
181 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
182 variable, check it if it is present in bitmap passed via DATA. */
184 static bool
185 test_nonssa_use (gimple, tree t, tree, void *data)
187 t = get_base_address (t);
189 if (!t || is_gimple_reg (t))
190 return false;
192 if (TREE_CODE (t) == PARM_DECL
193 || (TREE_CODE (t) == VAR_DECL
194 && auto_var_in_fn_p (t, current_function_decl))
195 || TREE_CODE (t) == RESULT_DECL
196 /* Normal labels are part of CFG and will be handled gratefuly.
197 Forced labels however can be used directly by statements and
198 need to stay in one partition along with their uses. */
199 || (TREE_CODE (t) == LABEL_DECL
200 && FORCED_LABEL (t)))
201 return bitmap_bit_p ((bitmap)data, DECL_UID (t));
203 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
204 to pretend that the value pointed to is actual result decl. */
205 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
206 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
207 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
208 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
209 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
210 return
211 bitmap_bit_p ((bitmap)data,
212 DECL_UID (DECL_RESULT (current_function_decl)));
214 return false;
217 /* Dump split point CURRENT. */
219 static void
220 dump_split_point (FILE * file, struct split_point *current)
222 fprintf (file,
223 "Split point at BB %i\n"
224 " header time: %i header size: %i\n"
225 " split time: %i split size: %i\n bbs: ",
226 current->entry_bb->index, current->header_time,
227 current->header_size, current->split_time, current->split_size);
228 dump_bitmap (file, current->split_bbs);
229 fprintf (file, " SSA names to pass: ");
230 dump_bitmap (file, current->ssa_names_to_pass);
233 /* Look for all BBs in header that might lead to the split part and verify
234 that they are not defining any non-SSA var used by the split part.
235 Parameters are the same as for consider_split. */
237 static bool
238 verify_non_ssa_vars (struct split_point *current, bitmap non_ssa_vars,
239 basic_block return_bb)
241 bitmap seen = BITMAP_ALLOC (NULL);
242 vec<basic_block> worklist = vNULL;
243 edge e;
244 edge_iterator ei;
245 bool ok = true;
246 basic_block bb;
248 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
249 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
250 && !bitmap_bit_p (current->split_bbs, e->src->index))
252 worklist.safe_push (e->src);
253 bitmap_set_bit (seen, e->src->index);
256 while (!worklist.is_empty ())
258 bb = worklist.pop ();
259 FOR_EACH_EDGE (e, ei, bb->preds)
260 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
261 && bitmap_set_bit (seen, e->src->index))
263 gcc_checking_assert (!bitmap_bit_p (current->split_bbs,
264 e->src->index));
265 worklist.safe_push (e->src);
267 for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
268 gsi_next (&bsi))
270 gimple stmt = gsi_stmt (bsi);
271 if (is_gimple_debug (stmt))
272 continue;
273 if (walk_stmt_load_store_addr_ops
274 (stmt, non_ssa_vars, test_nonssa_use, test_nonssa_use,
275 test_nonssa_use))
277 ok = false;
278 goto done;
280 if (glabel *label_stmt = dyn_cast <glabel *> (stmt))
281 if (test_nonssa_use (stmt, gimple_label_label (label_stmt),
282 NULL_TREE, non_ssa_vars))
284 ok = false;
285 goto done;
288 for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
289 gsi_next (&bsi))
291 if (walk_stmt_load_store_addr_ops
292 (gsi_stmt (bsi), non_ssa_vars, test_nonssa_use, test_nonssa_use,
293 test_nonssa_use))
295 ok = false;
296 goto done;
299 FOR_EACH_EDGE (e, ei, bb->succs)
301 if (e->dest != return_bb)
302 continue;
303 for (gphi_iterator bsi = gsi_start_phis (return_bb);
304 !gsi_end_p (bsi);
305 gsi_next (&bsi))
307 gphi *stmt = bsi.phi ();
308 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
310 if (virtual_operand_p (gimple_phi_result (stmt)))
311 continue;
312 if (TREE_CODE (op) != SSA_NAME
313 && test_nonssa_use (stmt, op, op, non_ssa_vars))
315 ok = false;
316 goto done;
322 /* Verify that the rest of function does not define any label
323 used by the split part. */
324 FOR_EACH_BB_FN (bb, cfun)
325 if (!bitmap_bit_p (current->split_bbs, bb->index)
326 && !bitmap_bit_p (seen, bb->index))
328 gimple_stmt_iterator bsi;
329 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
330 if (glabel *label_stmt = dyn_cast <glabel *> (gsi_stmt (bsi)))
332 if (test_nonssa_use (label_stmt,
333 gimple_label_label (label_stmt),
334 NULL_TREE, non_ssa_vars))
336 ok = false;
337 goto done;
340 else
341 break;
344 done:
345 BITMAP_FREE (seen);
346 worklist.release ();
347 return ok;
350 /* If STMT is a call, check the callee against a list of forbidden
351 predicate functions. If a match is found, look for uses of the
352 call result in condition statements that compare against zero.
353 For each such use, find the block targeted by the condition
354 statement for the nonzero result, and set the bit for this block
355 in the forbidden dominators bitmap. The purpose of this is to avoid
356 selecting a split point where we are likely to lose the chance
357 to optimize away an unused function call. */
359 static void
360 check_forbidden_calls (gimple stmt)
362 imm_use_iterator use_iter;
363 use_operand_p use_p;
364 tree lhs;
366 /* At the moment, __builtin_constant_p is the only forbidden
367 predicate function call (see PR49642). */
368 if (!gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P))
369 return;
371 lhs = gimple_call_lhs (stmt);
373 if (!lhs || TREE_CODE (lhs) != SSA_NAME)
374 return;
376 FOR_EACH_IMM_USE_FAST (use_p, use_iter, lhs)
378 tree op1;
379 basic_block use_bb, forbidden_bb;
380 enum tree_code code;
381 edge true_edge, false_edge;
382 gcond *use_stmt;
384 use_stmt = dyn_cast <gcond *> (USE_STMT (use_p));
385 if (!use_stmt)
386 continue;
388 /* Assuming canonical form for GIMPLE_COND here, with constant
389 in second position. */
390 op1 = gimple_cond_rhs (use_stmt);
391 code = gimple_cond_code (use_stmt);
392 use_bb = gimple_bb (use_stmt);
394 extract_true_false_edges_from_block (use_bb, &true_edge, &false_edge);
396 /* We're only interested in comparisons that distinguish
397 unambiguously from zero. */
398 if (!integer_zerop (op1) || code == LE_EXPR || code == GE_EXPR)
399 continue;
401 if (code == EQ_EXPR)
402 forbidden_bb = false_edge->dest;
403 else
404 forbidden_bb = true_edge->dest;
406 bitmap_set_bit (forbidden_dominators, forbidden_bb->index);
410 /* If BB is dominated by any block in the forbidden dominators set,
411 return TRUE; else FALSE. */
413 static bool
414 dominated_by_forbidden (basic_block bb)
416 unsigned dom_bb;
417 bitmap_iterator bi;
419 EXECUTE_IF_SET_IN_BITMAP (forbidden_dominators, 1, dom_bb, bi)
421 if (dominated_by_p (CDI_DOMINATORS, bb,
422 BASIC_BLOCK_FOR_FN (cfun, dom_bb)))
423 return true;
426 return false;
429 /* For give split point CURRENT and return block RETURN_BB return 1
430 if ssa name VAL is set by split part and 0 otherwise. */
431 static bool
432 split_part_set_ssa_name_p (tree val, struct split_point *current,
433 basic_block return_bb)
435 if (TREE_CODE (val) != SSA_NAME)
436 return false;
438 return (!SSA_NAME_IS_DEFAULT_DEF (val)
439 && (bitmap_bit_p (current->split_bbs,
440 gimple_bb (SSA_NAME_DEF_STMT (val))->index)
441 || gimple_bb (SSA_NAME_DEF_STMT (val)) == return_bb));
444 /* We found an split_point CURRENT. NON_SSA_VARS is bitmap of all non ssa
445 variables used and RETURN_BB is return basic block.
446 See if we can split function here. */
448 static void
449 consider_split (struct split_point *current, bitmap non_ssa_vars,
450 basic_block return_bb)
452 tree parm;
453 unsigned int num_args = 0;
454 unsigned int call_overhead;
455 edge e;
456 edge_iterator ei;
457 gphi_iterator bsi;
458 unsigned int i;
459 int incoming_freq = 0;
460 tree retval;
461 tree retbnd;
462 bool back_edge = false;
464 if (dump_file && (dump_flags & TDF_DETAILS))
465 dump_split_point (dump_file, current);
467 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
469 if (e->flags & EDGE_DFS_BACK)
470 back_edge = true;
471 if (!bitmap_bit_p (current->split_bbs, e->src->index))
472 incoming_freq += EDGE_FREQUENCY (e);
475 /* Do not split when we would end up calling function anyway. */
476 if (incoming_freq
477 >= (ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency
478 * PARAM_VALUE (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY) / 100))
480 /* When profile is guessed, we can not expect it to give us
481 realistic estimate on likelyness of function taking the
482 complex path. As a special case, when tail of the function is
483 a loop, enable splitting since inlining code skipping the loop
484 is likely noticeable win. */
485 if (back_edge
486 && profile_status_for_fn (cfun) != PROFILE_READ
487 && incoming_freq < ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency)
489 if (dump_file && (dump_flags & TDF_DETAILS))
490 fprintf (dump_file,
491 " Split before loop, accepting despite low frequencies %i %i.\n",
492 incoming_freq,
493 ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency);
495 else
497 if (dump_file && (dump_flags & TDF_DETAILS))
498 fprintf (dump_file,
499 " Refused: incoming frequency is too large.\n");
500 return;
504 if (!current->header_size)
506 if (dump_file && (dump_flags & TDF_DETAILS))
507 fprintf (dump_file, " Refused: header empty\n");
508 return;
511 /* Verify that PHI args on entry are either virtual or all their operands
512 incoming from header are the same. */
513 for (bsi = gsi_start_phis (current->entry_bb); !gsi_end_p (bsi); gsi_next (&bsi))
515 gphi *stmt = bsi.phi ();
516 tree val = NULL;
518 if (virtual_operand_p (gimple_phi_result (stmt)))
519 continue;
520 for (i = 0; i < gimple_phi_num_args (stmt); i++)
522 edge e = gimple_phi_arg_edge (stmt, i);
523 if (!bitmap_bit_p (current->split_bbs, e->src->index))
525 tree edge_val = gimple_phi_arg_def (stmt, i);
526 if (val && edge_val != val)
528 if (dump_file && (dump_flags & TDF_DETAILS))
529 fprintf (dump_file,
530 " Refused: entry BB has PHI with multiple variants\n");
531 return;
533 val = edge_val;
539 /* See what argument we will pass to the split function and compute
540 call overhead. */
541 call_overhead = eni_size_weights.call_cost;
542 for (parm = DECL_ARGUMENTS (current_function_decl); parm;
543 parm = DECL_CHAIN (parm))
545 if (!is_gimple_reg (parm))
547 if (bitmap_bit_p (non_ssa_vars, DECL_UID (parm)))
549 if (dump_file && (dump_flags & TDF_DETAILS))
550 fprintf (dump_file,
551 " Refused: need to pass non-ssa param values\n");
552 return;
555 else
557 tree ddef = ssa_default_def (cfun, parm);
558 if (ddef
559 && bitmap_bit_p (current->ssa_names_to_pass,
560 SSA_NAME_VERSION (ddef)))
562 if (!VOID_TYPE_P (TREE_TYPE (parm)))
563 call_overhead += estimate_move_cost (TREE_TYPE (parm), false);
564 num_args++;
568 if (!VOID_TYPE_P (TREE_TYPE (current_function_decl)))
569 call_overhead += estimate_move_cost (TREE_TYPE (current_function_decl),
570 false);
572 if (current->split_size <= call_overhead)
574 if (dump_file && (dump_flags & TDF_DETAILS))
575 fprintf (dump_file,
576 " Refused: split size is smaller than call overhead\n");
577 return;
579 if (current->header_size + call_overhead
580 >= (unsigned int)(DECL_DECLARED_INLINE_P (current_function_decl)
581 ? MAX_INLINE_INSNS_SINGLE
582 : MAX_INLINE_INSNS_AUTO))
584 if (dump_file && (dump_flags & TDF_DETAILS))
585 fprintf (dump_file,
586 " Refused: header size is too large for inline candidate\n");
587 return;
590 /* Splitting functions brings the target out of comdat group; this will
591 lead to code duplication if the function is reused by other unit.
592 Limit this duplication. This is consistent with limit in tree-sra.c
593 FIXME: with LTO we ought to be able to do better! */
594 if (DECL_ONE_ONLY (current_function_decl)
595 && current->split_size >= (unsigned int) MAX_INLINE_INSNS_AUTO)
597 if (dump_file && (dump_flags & TDF_DETAILS))
598 fprintf (dump_file,
599 " Refused: function is COMDAT and tail is too large\n");
600 return;
602 /* For comdat functions also reject very small tails; those will likely get
603 inlined back and we do not want to risk the duplication overhead.
604 FIXME: with LTO we ought to be able to do better! */
605 if (DECL_ONE_ONLY (current_function_decl)
606 && current->split_size
607 <= (unsigned int) PARAM_VALUE (PARAM_EARLY_INLINING_INSNS) / 2)
609 if (dump_file && (dump_flags & TDF_DETAILS))
610 fprintf (dump_file,
611 " Refused: function is COMDAT and tail is too small\n");
612 return;
615 /* FIXME: we currently can pass only SSA function parameters to the split
616 arguments. Once parm_adjustment infrastructure is supported by cloning,
617 we can pass more than that. */
618 if (num_args != bitmap_count_bits (current->ssa_names_to_pass))
621 if (dump_file && (dump_flags & TDF_DETAILS))
622 fprintf (dump_file,
623 " Refused: need to pass non-param values\n");
624 return;
627 /* When there are non-ssa vars used in the split region, see if they
628 are used in the header region. If so, reject the split.
629 FIXME: we can use nested function support to access both. */
630 if (!bitmap_empty_p (non_ssa_vars)
631 && !verify_non_ssa_vars (current, non_ssa_vars, return_bb))
633 if (dump_file && (dump_flags & TDF_DETAILS))
634 fprintf (dump_file,
635 " Refused: split part has non-ssa uses\n");
636 return;
639 /* If the split point is dominated by a forbidden block, reject
640 the split. */
641 if (!bitmap_empty_p (forbidden_dominators)
642 && dominated_by_forbidden (current->entry_bb))
644 if (dump_file && (dump_flags & TDF_DETAILS))
645 fprintf (dump_file,
646 " Refused: split point dominated by forbidden block\n");
647 return;
650 /* See if retval used by return bb is computed by header or split part.
651 When it is computed by split part, we need to produce return statement
652 in the split part and add code to header to pass it around.
654 This is bit tricky to test:
655 1) When there is no return_bb or no return value, we always pass
656 value around.
657 2) Invariants are always computed by caller.
658 3) For SSA we need to look if defining statement is in header or split part
659 4) For non-SSA we need to look where the var is computed. */
660 retval = find_retval (return_bb);
661 if (!retval)
662 current->split_part_set_retval = true;
663 else if (is_gimple_min_invariant (retval))
664 current->split_part_set_retval = false;
665 /* Special case is value returned by reference we record as if it was non-ssa
666 set to result_decl. */
667 else if (TREE_CODE (retval) == SSA_NAME
668 && SSA_NAME_VAR (retval)
669 && TREE_CODE (SSA_NAME_VAR (retval)) == RESULT_DECL
670 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
671 current->split_part_set_retval
672 = bitmap_bit_p (non_ssa_vars, DECL_UID (SSA_NAME_VAR (retval)));
673 else if (TREE_CODE (retval) == SSA_NAME)
674 current->split_part_set_retval
675 = split_part_set_ssa_name_p (retval, current, return_bb);
676 else if (TREE_CODE (retval) == PARM_DECL)
677 current->split_part_set_retval = false;
678 else if (TREE_CODE (retval) == VAR_DECL
679 || TREE_CODE (retval) == RESULT_DECL)
680 current->split_part_set_retval
681 = bitmap_bit_p (non_ssa_vars, DECL_UID (retval));
682 else
683 current->split_part_set_retval = true;
685 /* See if retbnd used by return bb is computed by header or split part. */
686 retbnd = find_retbnd (return_bb);
687 if (retbnd)
689 bool split_part_set_retbnd
690 = split_part_set_ssa_name_p (retbnd, current, return_bb);
692 /* If we have both return value and bounds then keep their definitions
693 in a single function. We use SSA names to link returned bounds and
694 value and therefore do not handle cases when result is passed by
695 reference (which should not be our case anyway since bounds are
696 returned for pointers only). */
697 if ((DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
698 && current->split_part_set_retval)
699 || split_part_set_retbnd != current->split_part_set_retval)
701 if (dump_file && (dump_flags & TDF_DETAILS))
702 fprintf (dump_file,
703 " Refused: split point splits return value and bounds\n");
704 return;
708 /* split_function fixes up at most one PHI non-virtual PHI node in return_bb,
709 for the return value. If there are other PHIs, give up. */
710 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
712 gphi_iterator psi;
714 for (psi = gsi_start_phis (return_bb); !gsi_end_p (psi); gsi_next (&psi))
715 if (!virtual_operand_p (gimple_phi_result (psi.phi ()))
716 && !(retval
717 && current->split_part_set_retval
718 && TREE_CODE (retval) == SSA_NAME
719 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
720 && SSA_NAME_DEF_STMT (retval) == psi.phi ()))
722 if (dump_file && (dump_flags & TDF_DETAILS))
723 fprintf (dump_file,
724 " Refused: return bb has extra PHIs\n");
725 return;
729 if (dump_file && (dump_flags & TDF_DETAILS))
730 fprintf (dump_file, " Accepted!\n");
732 /* At the moment chose split point with lowest frequency and that leaves
733 out smallest size of header.
734 In future we might re-consider this heuristics. */
735 if (!best_split_point.split_bbs
736 || best_split_point.entry_bb->frequency > current->entry_bb->frequency
737 || (best_split_point.entry_bb->frequency == current->entry_bb->frequency
738 && best_split_point.split_size < current->split_size))
741 if (dump_file && (dump_flags & TDF_DETAILS))
742 fprintf (dump_file, " New best split point!\n");
743 if (best_split_point.ssa_names_to_pass)
745 BITMAP_FREE (best_split_point.ssa_names_to_pass);
746 BITMAP_FREE (best_split_point.split_bbs);
748 best_split_point = *current;
749 best_split_point.ssa_names_to_pass = BITMAP_ALLOC (NULL);
750 bitmap_copy (best_split_point.ssa_names_to_pass,
751 current->ssa_names_to_pass);
752 best_split_point.split_bbs = BITMAP_ALLOC (NULL);
753 bitmap_copy (best_split_point.split_bbs, current->split_bbs);
757 /* Return basic block containing RETURN statement. We allow basic blocks
758 of the form:
759 <retval> = tmp_var;
760 return <retval>
761 but return_bb can not be more complex than this (except for
762 -fsanitize=thread we allow TSAN_FUNC_EXIT () internal call in there).
763 If nothing is found, return the exit block.
765 When there are multiple RETURN statement, chose one with return value,
766 since that one is more likely shared by multiple code paths.
768 Return BB is special, because for function splitting it is the only
769 basic block that is duplicated in between header and split part of the
770 function.
772 TODO: We might support multiple return blocks. */
774 static basic_block
775 find_return_bb (void)
777 edge e;
778 basic_block return_bb = EXIT_BLOCK_PTR_FOR_FN (cfun);
779 gimple_stmt_iterator bsi;
780 bool found_return = false;
781 tree retval = NULL_TREE;
783 if (!single_pred_p (EXIT_BLOCK_PTR_FOR_FN (cfun)))
784 return return_bb;
786 e = single_pred_edge (EXIT_BLOCK_PTR_FOR_FN (cfun));
787 for (bsi = gsi_last_bb (e->src); !gsi_end_p (bsi); gsi_prev (&bsi))
789 gimple stmt = gsi_stmt (bsi);
790 if (gimple_code (stmt) == GIMPLE_LABEL
791 || is_gimple_debug (stmt)
792 || gimple_clobber_p (stmt))
794 else if (gimple_code (stmt) == GIMPLE_ASSIGN
795 && found_return
796 && gimple_assign_single_p (stmt)
797 && (auto_var_in_fn_p (gimple_assign_rhs1 (stmt),
798 current_function_decl)
799 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
800 && retval == gimple_assign_lhs (stmt))
802 else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
804 found_return = true;
805 retval = gimple_return_retval (return_stmt);
807 /* For -fsanitize=thread, allow also TSAN_FUNC_EXIT () in the return
808 bb. */
809 else if ((flag_sanitize & SANITIZE_THREAD)
810 && is_gimple_call (stmt)
811 && gimple_call_internal_p (stmt)
812 && gimple_call_internal_fn (stmt) == IFN_TSAN_FUNC_EXIT)
814 else
815 break;
817 if (gsi_end_p (bsi) && found_return)
818 return_bb = e->src;
820 return return_bb;
823 /* Given return basic block RETURN_BB, see where return value is really
824 stored. */
825 static tree
826 find_retval (basic_block return_bb)
828 gimple_stmt_iterator bsi;
829 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
830 if (greturn *return_stmt = dyn_cast <greturn *> (gsi_stmt (bsi)))
831 return gimple_return_retval (return_stmt);
832 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
833 && !gimple_clobber_p (gsi_stmt (bsi)))
834 return gimple_assign_rhs1 (gsi_stmt (bsi));
835 return NULL;
838 /* Given return basic block RETURN_BB, see where return bounds are really
839 stored. */
840 static tree
841 find_retbnd (basic_block return_bb)
843 gimple_stmt_iterator bsi;
844 for (bsi = gsi_last_bb (return_bb); !gsi_end_p (bsi); gsi_prev (&bsi))
845 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
846 return gimple_return_retbnd (gsi_stmt (bsi));
847 return NULL;
850 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
851 variable, mark it as used in bitmap passed via DATA.
852 Return true when access to T prevents splitting the function. */
854 static bool
855 mark_nonssa_use (gimple, tree t, tree, void *data)
857 t = get_base_address (t);
859 if (!t || is_gimple_reg (t))
860 return false;
862 /* At present we can't pass non-SSA arguments to split function.
863 FIXME: this can be relaxed by passing references to arguments. */
864 if (TREE_CODE (t) == PARM_DECL)
866 if (dump_file && (dump_flags & TDF_DETAILS))
867 fprintf (dump_file,
868 "Cannot split: use of non-ssa function parameter.\n");
869 return true;
872 if ((TREE_CODE (t) == VAR_DECL
873 && auto_var_in_fn_p (t, current_function_decl))
874 || TREE_CODE (t) == RESULT_DECL
875 || (TREE_CODE (t) == LABEL_DECL
876 && FORCED_LABEL (t)))
877 bitmap_set_bit ((bitmap)data, DECL_UID (t));
879 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
880 to pretend that the value pointed to is actual result decl. */
881 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
882 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
883 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
884 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
885 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
886 return
887 bitmap_bit_p ((bitmap)data,
888 DECL_UID (DECL_RESULT (current_function_decl)));
890 return false;
893 /* Compute local properties of basic block BB we collect when looking for
894 split points. We look for ssa defs and store them in SET_SSA_NAMES,
895 for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic
896 vars stored in NON_SSA_VARS.
898 When BB has edge to RETURN_BB, collect uses in RETURN_BB too.
900 Return false when BB contains something that prevents it from being put into
901 split function. */
903 static bool
904 visit_bb (basic_block bb, basic_block return_bb,
905 bitmap set_ssa_names, bitmap used_ssa_names,
906 bitmap non_ssa_vars)
908 edge e;
909 edge_iterator ei;
910 bool can_split = true;
912 for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
913 gsi_next (&bsi))
915 gimple stmt = gsi_stmt (bsi);
916 tree op;
917 ssa_op_iter iter;
918 tree decl;
920 if (is_gimple_debug (stmt))
921 continue;
923 if (gimple_clobber_p (stmt))
924 continue;
926 /* FIXME: We can split regions containing EH. We can not however
927 split RESX, EH_DISPATCH and EH_POINTER referring to same region
928 into different partitions. This would require tracking of
929 EH regions and checking in consider_split_point if they
930 are not used elsewhere. */
931 if (gimple_code (stmt) == GIMPLE_RESX)
933 if (dump_file && (dump_flags & TDF_DETAILS))
934 fprintf (dump_file, "Cannot split: resx.\n");
935 can_split = false;
937 if (gimple_code (stmt) == GIMPLE_EH_DISPATCH)
939 if (dump_file && (dump_flags & TDF_DETAILS))
940 fprintf (dump_file, "Cannot split: eh dispatch.\n");
941 can_split = false;
944 /* Check builtins that prevent splitting. */
945 if (gimple_code (stmt) == GIMPLE_CALL
946 && (decl = gimple_call_fndecl (stmt)) != NULL_TREE
947 && DECL_BUILT_IN (decl)
948 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
949 switch (DECL_FUNCTION_CODE (decl))
951 /* FIXME: once we will allow passing non-parm values to split part,
952 we need to be sure to handle correct builtin_stack_save and
953 builtin_stack_restore. At the moment we are safe; there is no
954 way to store builtin_stack_save result in non-SSA variable
955 since all calls to those are compiler generated. */
956 case BUILT_IN_APPLY:
957 case BUILT_IN_APPLY_ARGS:
958 case BUILT_IN_VA_START:
959 if (dump_file && (dump_flags & TDF_DETAILS))
960 fprintf (dump_file,
961 "Cannot split: builtin_apply and va_start.\n");
962 can_split = false;
963 break;
964 case BUILT_IN_EH_POINTER:
965 if (dump_file && (dump_flags & TDF_DETAILS))
966 fprintf (dump_file, "Cannot split: builtin_eh_pointer.\n");
967 can_split = false;
968 break;
969 default:
970 break;
973 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
974 bitmap_set_bit (set_ssa_names, SSA_NAME_VERSION (op));
975 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
976 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
977 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
978 mark_nonssa_use,
979 mark_nonssa_use,
980 mark_nonssa_use);
982 for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
983 gsi_next (&bsi))
985 gphi *stmt = bsi.phi ();
986 unsigned int i;
988 if (virtual_operand_p (gimple_phi_result (stmt)))
989 continue;
990 bitmap_set_bit (set_ssa_names,
991 SSA_NAME_VERSION (gimple_phi_result (stmt)));
992 for (i = 0; i < gimple_phi_num_args (stmt); i++)
994 tree op = gimple_phi_arg_def (stmt, i);
995 if (TREE_CODE (op) == SSA_NAME)
996 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
998 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
999 mark_nonssa_use,
1000 mark_nonssa_use,
1001 mark_nonssa_use);
1003 /* Record also uses coming from PHI operand in return BB. */
1004 FOR_EACH_EDGE (e, ei, bb->succs)
1005 if (e->dest == return_bb)
1007 for (gphi_iterator bsi = gsi_start_phis (return_bb);
1008 !gsi_end_p (bsi);
1009 gsi_next (&bsi))
1011 gphi *stmt = bsi.phi ();
1012 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
1014 if (virtual_operand_p (gimple_phi_result (stmt)))
1015 continue;
1016 if (TREE_CODE (op) == SSA_NAME)
1017 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
1018 else
1019 can_split &= !mark_nonssa_use (stmt, op, op, non_ssa_vars);
1022 return can_split;
1025 /* Stack entry for recursive DFS walk in find_split_point. */
1027 typedef struct
1029 /* Basic block we are examining. */
1030 basic_block bb;
1032 /* SSA names set and used by the BB and all BBs reachable
1033 from it via DFS walk. */
1034 bitmap set_ssa_names, used_ssa_names;
1035 bitmap non_ssa_vars;
1037 /* All BBS visited from this BB via DFS walk. */
1038 bitmap bbs_visited;
1040 /* Last examined edge in DFS walk. Since we walk unoriented graph,
1041 the value is up to sum of incoming and outgoing edges of BB. */
1042 unsigned int edge_num;
1044 /* Stack entry index of earliest BB reachable from current BB
1045 or any BB visited later in DFS walk. */
1046 int earliest;
1048 /* Overall time and size of all BBs reached from this BB in DFS walk. */
1049 int overall_time, overall_size;
1051 /* When false we can not split on this BB. */
1052 bool can_split;
1053 } stack_entry;
1056 /* Find all articulations and call consider_split on them.
1057 OVERALL_TIME and OVERALL_SIZE is time and size of the function.
1059 We perform basic algorithm for finding an articulation in a graph
1060 created from CFG by considering it to be an unoriented graph.
1062 The articulation is discovered via DFS walk. We collect earliest
1063 basic block on stack that is reachable via backward edge. Articulation
1064 is any basic block such that there is no backward edge bypassing it.
1065 To reduce stack usage we maintain heap allocated stack in STACK vector.
1066 AUX pointer of BB is set to index it appears in the stack or -1 once
1067 it is visited and popped off the stack.
1069 The algorithm finds articulation after visiting the whole component
1070 reachable by it. This makes it convenient to collect information about
1071 the component used by consider_split. */
1073 static void
1074 find_split_points (basic_block return_bb, int overall_time, int overall_size)
1076 stack_entry first;
1077 vec<stack_entry> stack = vNULL;
1078 basic_block bb;
1079 struct split_point current;
1081 current.header_time = overall_time;
1082 current.header_size = overall_size;
1083 current.split_time = 0;
1084 current.split_size = 0;
1085 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
1087 first.bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1088 first.edge_num = 0;
1089 first.overall_time = 0;
1090 first.overall_size = 0;
1091 first.earliest = INT_MAX;
1092 first.set_ssa_names = 0;
1093 first.used_ssa_names = 0;
1094 first.non_ssa_vars = 0;
1095 first.bbs_visited = 0;
1096 first.can_split = false;
1097 stack.safe_push (first);
1098 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = (void *)(intptr_t)-1;
1100 while (!stack.is_empty ())
1102 stack_entry *entry = &stack.last ();
1104 /* We are walking an acyclic graph, so edge_num counts
1105 succ and pred edges together. However when considering
1106 articulation, we want to have processed everything reachable
1107 from articulation but nothing that reaches into it. */
1108 if (entry->edge_num == EDGE_COUNT (entry->bb->succs)
1109 && entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1111 int pos = stack.length ();
1112 entry->can_split &= visit_bb (entry->bb, return_bb,
1113 entry->set_ssa_names,
1114 entry->used_ssa_names,
1115 entry->non_ssa_vars);
1116 if (pos <= entry->earliest && !entry->can_split
1117 && dump_file && (dump_flags & TDF_DETAILS))
1118 fprintf (dump_file,
1119 "found articulation at bb %i but can not split\n",
1120 entry->bb->index);
1121 if (pos <= entry->earliest && entry->can_split)
1123 if (dump_file && (dump_flags & TDF_DETAILS))
1124 fprintf (dump_file, "found articulation at bb %i\n",
1125 entry->bb->index);
1126 current.entry_bb = entry->bb;
1127 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
1128 bitmap_and_compl (current.ssa_names_to_pass,
1129 entry->used_ssa_names, entry->set_ssa_names);
1130 current.header_time = overall_time - entry->overall_time;
1131 current.header_size = overall_size - entry->overall_size;
1132 current.split_time = entry->overall_time;
1133 current.split_size = entry->overall_size;
1134 current.split_bbs = entry->bbs_visited;
1135 consider_split (&current, entry->non_ssa_vars, return_bb);
1136 BITMAP_FREE (current.ssa_names_to_pass);
1139 /* Do actual DFS walk. */
1140 if (entry->edge_num
1141 < (EDGE_COUNT (entry->bb->succs)
1142 + EDGE_COUNT (entry->bb->preds)))
1144 edge e;
1145 basic_block dest;
1146 if (entry->edge_num < EDGE_COUNT (entry->bb->succs))
1148 e = EDGE_SUCC (entry->bb, entry->edge_num);
1149 dest = e->dest;
1151 else
1153 e = EDGE_PRED (entry->bb, entry->edge_num
1154 - EDGE_COUNT (entry->bb->succs));
1155 dest = e->src;
1158 entry->edge_num++;
1160 /* New BB to visit, push it to the stack. */
1161 if (dest != return_bb && dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
1162 && !dest->aux)
1164 stack_entry new_entry;
1166 new_entry.bb = dest;
1167 new_entry.edge_num = 0;
1168 new_entry.overall_time
1169 = bb_info_vec[dest->index].time;
1170 new_entry.overall_size
1171 = bb_info_vec[dest->index].size;
1172 new_entry.earliest = INT_MAX;
1173 new_entry.set_ssa_names = BITMAP_ALLOC (NULL);
1174 new_entry.used_ssa_names = BITMAP_ALLOC (NULL);
1175 new_entry.bbs_visited = BITMAP_ALLOC (NULL);
1176 new_entry.non_ssa_vars = BITMAP_ALLOC (NULL);
1177 new_entry.can_split = true;
1178 bitmap_set_bit (new_entry.bbs_visited, dest->index);
1179 stack.safe_push (new_entry);
1180 dest->aux = (void *)(intptr_t)stack.length ();
1182 /* Back edge found, record the earliest point. */
1183 else if ((intptr_t)dest->aux > 0
1184 && (intptr_t)dest->aux < entry->earliest)
1185 entry->earliest = (intptr_t)dest->aux;
1187 /* We are done with examining the edges. Pop off the value from stack
1188 and merge stuff we accumulate during the walk. */
1189 else if (entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1191 stack_entry *prev = &stack[stack.length () - 2];
1193 entry->bb->aux = (void *)(intptr_t)-1;
1194 prev->can_split &= entry->can_split;
1195 if (prev->set_ssa_names)
1197 bitmap_ior_into (prev->set_ssa_names, entry->set_ssa_names);
1198 bitmap_ior_into (prev->used_ssa_names, entry->used_ssa_names);
1199 bitmap_ior_into (prev->bbs_visited, entry->bbs_visited);
1200 bitmap_ior_into (prev->non_ssa_vars, entry->non_ssa_vars);
1202 if (prev->earliest > entry->earliest)
1203 prev->earliest = entry->earliest;
1204 prev->overall_time += entry->overall_time;
1205 prev->overall_size += entry->overall_size;
1206 BITMAP_FREE (entry->set_ssa_names);
1207 BITMAP_FREE (entry->used_ssa_names);
1208 BITMAP_FREE (entry->bbs_visited);
1209 BITMAP_FREE (entry->non_ssa_vars);
1210 stack.pop ();
1212 else
1213 stack.pop ();
1215 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = NULL;
1216 FOR_EACH_BB_FN (bb, cfun)
1217 bb->aux = NULL;
1218 stack.release ();
1219 BITMAP_FREE (current.ssa_names_to_pass);
1222 /* Split function at SPLIT_POINT. */
1224 static void
1225 split_function (basic_block return_bb, struct split_point *split_point,
1226 bool add_tsan_func_exit)
1228 vec<tree> args_to_pass = vNULL;
1229 bitmap args_to_skip;
1230 tree parm;
1231 int num = 0;
1232 cgraph_node *node, *cur_node = cgraph_node::get (current_function_decl);
1233 basic_block call_bb;
1234 gcall *call, *tsan_func_exit_call = NULL;
1235 edge e;
1236 edge_iterator ei;
1237 tree retval = NULL, real_retval = NULL, retbnd = NULL;
1238 bool split_part_return_p = false;
1239 bool with_bounds = chkp_function_instrumented_p (current_function_decl);
1240 gimple last_stmt = NULL;
1241 unsigned int i;
1242 tree arg, ddef;
1243 vec<tree, va_gc> **debug_args = NULL;
1245 if (dump_file)
1247 fprintf (dump_file, "\n\nSplitting function at:\n");
1248 dump_split_point (dump_file, split_point);
1251 if (cur_node->local.can_change_signature)
1252 args_to_skip = BITMAP_ALLOC (NULL);
1253 else
1254 args_to_skip = NULL;
1256 /* Collect the parameters of new function and args_to_skip bitmap. */
1257 for (parm = DECL_ARGUMENTS (current_function_decl);
1258 parm; parm = DECL_CHAIN (parm), num++)
1259 if (args_to_skip
1260 && (!is_gimple_reg (parm)
1261 || (ddef = ssa_default_def (cfun, parm)) == NULL_TREE
1262 || !bitmap_bit_p (split_point->ssa_names_to_pass,
1263 SSA_NAME_VERSION (ddef))))
1264 bitmap_set_bit (args_to_skip, num);
1265 else
1267 /* This parm might not have been used up to now, but is going to be
1268 used, hence register it. */
1269 if (is_gimple_reg (parm))
1270 arg = get_or_create_ssa_default_def (cfun, parm);
1271 else
1272 arg = parm;
1274 if (!useless_type_conversion_p (DECL_ARG_TYPE (parm), TREE_TYPE (arg)))
1275 arg = fold_convert (DECL_ARG_TYPE (parm), arg);
1276 args_to_pass.safe_push (arg);
1279 /* See if the split function will return. */
1280 FOR_EACH_EDGE (e, ei, return_bb->preds)
1281 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1282 break;
1283 if (e)
1284 split_part_return_p = true;
1286 /* Add return block to what will become the split function.
1287 We do not return; no return block is needed. */
1288 if (!split_part_return_p)
1290 /* We have no return block, so nothing is needed. */
1291 else if (return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1293 /* When we do not want to return value, we need to construct
1294 new return block with empty return statement.
1295 FIXME: Once we are able to change return type, we should change function
1296 to return void instead of just outputting function with undefined return
1297 value. For structures this affects quality of codegen. */
1298 else if (!split_point->split_part_set_retval
1299 && find_retval (return_bb))
1301 bool redirected = true;
1302 basic_block new_return_bb = create_basic_block (NULL, 0, return_bb);
1303 gimple_stmt_iterator gsi = gsi_start_bb (new_return_bb);
1304 gsi_insert_after (&gsi, gimple_build_return (NULL), GSI_NEW_STMT);
1305 while (redirected)
1307 redirected = false;
1308 FOR_EACH_EDGE (e, ei, return_bb->preds)
1309 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1311 new_return_bb->count += e->count;
1312 new_return_bb->frequency += EDGE_FREQUENCY (e);
1313 redirect_edge_and_branch (e, new_return_bb);
1314 redirected = true;
1315 break;
1318 e = make_edge (new_return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1319 e->probability = REG_BR_PROB_BASE;
1320 e->count = new_return_bb->count;
1321 add_bb_to_loop (new_return_bb, current_loops->tree_root);
1322 bitmap_set_bit (split_point->split_bbs, new_return_bb->index);
1324 /* When we pass around the value, use existing return block. */
1325 else
1326 bitmap_set_bit (split_point->split_bbs, return_bb->index);
1328 /* If RETURN_BB has virtual operand PHIs, they must be removed and the
1329 virtual operand marked for renaming as we change the CFG in a way that
1330 tree-inline is not able to compensate for.
1332 Note this can happen whether or not we have a return value. If we have
1333 a return value, then RETURN_BB may have PHIs for real operands too. */
1334 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1336 bool phi_p = false;
1337 for (gphi_iterator gsi = gsi_start_phis (return_bb);
1338 !gsi_end_p (gsi);)
1340 gphi *stmt = gsi.phi ();
1341 if (!virtual_operand_p (gimple_phi_result (stmt)))
1343 gsi_next (&gsi);
1344 continue;
1346 mark_virtual_phi_result_for_renaming (stmt);
1347 remove_phi_node (&gsi, true);
1348 phi_p = true;
1350 /* In reality we have to rename the reaching definition of the
1351 virtual operand at return_bb as we will eventually release it
1352 when we remove the code region we outlined.
1353 So we have to rename all immediate virtual uses of that region
1354 if we didn't see a PHI definition yet. */
1355 /* ??? In real reality we want to set the reaching vdef of the
1356 entry of the SESE region as the vuse of the call and the reaching
1357 vdef of the exit of the SESE region as the vdef of the call. */
1358 if (!phi_p)
1359 for (gimple_stmt_iterator gsi = gsi_start_bb (return_bb);
1360 !gsi_end_p (gsi);
1361 gsi_next (&gsi))
1363 gimple stmt = gsi_stmt (gsi);
1364 if (gimple_vuse (stmt))
1366 gimple_set_vuse (stmt, NULL_TREE);
1367 update_stmt (stmt);
1369 if (gimple_vdef (stmt))
1370 break;
1374 /* Now create the actual clone. */
1375 cgraph_edge::rebuild_edges ();
1376 node = cur_node->create_version_clone_with_body
1377 (vNULL, NULL, args_to_skip, !split_part_return_p, split_point->split_bbs,
1378 split_point->entry_bb, "part");
1380 node->split_part = true;
1382 /* Let's take a time profile for splitted function. */
1383 node->tp_first_run = cur_node->tp_first_run + 1;
1385 /* For usual cloning it is enough to clear builtin only when signature
1386 changes. For partial inlining we however can not expect the part
1387 of builtin implementation to have same semantic as the whole. */
1388 if (DECL_BUILT_IN (node->decl))
1390 DECL_BUILT_IN_CLASS (node->decl) = NOT_BUILT_IN;
1391 DECL_FUNCTION_CODE (node->decl) = (enum built_in_function) 0;
1394 /* If the original function is instrumented then it's
1395 part is also instrumented. */
1396 if (with_bounds)
1397 chkp_function_mark_instrumented (node->decl);
1399 /* If the original function is declared inline, there is no point in issuing
1400 a warning for the non-inlinable part. */
1401 DECL_NO_INLINE_WARNING_P (node->decl) = 1;
1402 cur_node->remove_callees ();
1403 cur_node->remove_all_references ();
1404 if (!split_part_return_p)
1405 TREE_THIS_VOLATILE (node->decl) = 1;
1406 if (dump_file)
1407 dump_function_to_file (node->decl, dump_file, dump_flags);
1409 /* Create the basic block we place call into. It is the entry basic block
1410 split after last label. */
1411 call_bb = split_point->entry_bb;
1412 for (gimple_stmt_iterator gsi = gsi_start_bb (call_bb); !gsi_end_p (gsi);)
1413 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
1415 last_stmt = gsi_stmt (gsi);
1416 gsi_next (&gsi);
1418 else
1419 break;
1420 e = split_block (split_point->entry_bb, last_stmt);
1421 remove_edge (e);
1423 /* Produce the call statement. */
1424 gimple_stmt_iterator gsi = gsi_last_bb (call_bb);
1425 FOR_EACH_VEC_ELT (args_to_pass, i, arg)
1426 if (!is_gimple_val (arg))
1428 arg = force_gimple_operand_gsi (&gsi, arg, true, NULL_TREE,
1429 false, GSI_CONTINUE_LINKING);
1430 args_to_pass[i] = arg;
1432 call = gimple_build_call_vec (node->decl, args_to_pass);
1433 gimple_call_set_with_bounds (call, with_bounds);
1434 gimple_set_block (call, DECL_INITIAL (current_function_decl));
1435 args_to_pass.release ();
1437 /* For optimized away parameters, add on the caller side
1438 before the call
1439 DEBUG D#X => parm_Y(D)
1440 stmts and associate D#X with parm in decl_debug_args_lookup
1441 vector to say for debug info that if parameter parm had been passed,
1442 it would have value parm_Y(D). */
1443 if (args_to_skip)
1444 for (parm = DECL_ARGUMENTS (current_function_decl), num = 0;
1445 parm; parm = DECL_CHAIN (parm), num++)
1446 if (bitmap_bit_p (args_to_skip, num)
1447 && is_gimple_reg (parm))
1449 tree ddecl;
1450 gimple def_temp;
1452 /* This needs to be done even without MAY_HAVE_DEBUG_STMTS,
1453 otherwise if it didn't exist before, we'd end up with
1454 different SSA_NAME_VERSIONs between -g and -g0. */
1455 arg = get_or_create_ssa_default_def (cfun, parm);
1456 if (!MAY_HAVE_DEBUG_STMTS)
1457 continue;
1459 if (debug_args == NULL)
1460 debug_args = decl_debug_args_insert (node->decl);
1461 ddecl = make_node (DEBUG_EXPR_DECL);
1462 DECL_ARTIFICIAL (ddecl) = 1;
1463 TREE_TYPE (ddecl) = TREE_TYPE (parm);
1464 DECL_MODE (ddecl) = DECL_MODE (parm);
1465 vec_safe_push (*debug_args, DECL_ORIGIN (parm));
1466 vec_safe_push (*debug_args, ddecl);
1467 def_temp = gimple_build_debug_bind (ddecl, unshare_expr (arg),
1468 call);
1469 gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
1471 /* And on the callee side, add
1472 DEBUG D#Y s=> parm
1473 DEBUG var => D#Y
1474 stmts to the first bb where var is a VAR_DECL created for the
1475 optimized away parameter in DECL_INITIAL block. This hints
1476 in the debug info that var (whole DECL_ORIGIN is the parm PARM_DECL)
1477 is optimized away, but could be looked up at the call site
1478 as value of D#X there. */
1479 if (debug_args != NULL)
1481 unsigned int i;
1482 tree var, vexpr;
1483 gimple_stmt_iterator cgsi;
1484 gimple def_temp;
1486 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1487 var = BLOCK_VARS (DECL_INITIAL (node->decl));
1488 i = vec_safe_length (*debug_args);
1489 cgsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1492 i -= 2;
1493 while (var != NULL_TREE
1494 && DECL_ABSTRACT_ORIGIN (var) != (**debug_args)[i])
1495 var = TREE_CHAIN (var);
1496 if (var == NULL_TREE)
1497 break;
1498 vexpr = make_node (DEBUG_EXPR_DECL);
1499 parm = (**debug_args)[i];
1500 DECL_ARTIFICIAL (vexpr) = 1;
1501 TREE_TYPE (vexpr) = TREE_TYPE (parm);
1502 DECL_MODE (vexpr) = DECL_MODE (parm);
1503 def_temp = gimple_build_debug_source_bind (vexpr, parm,
1504 NULL);
1505 gsi_insert_before (&cgsi, def_temp, GSI_SAME_STMT);
1506 def_temp = gimple_build_debug_bind (var, vexpr, NULL);
1507 gsi_insert_before (&cgsi, def_temp, GSI_SAME_STMT);
1509 while (i);
1510 pop_cfun ();
1513 /* We avoid address being taken on any variable used by split part,
1514 so return slot optimization is always possible. Moreover this is
1515 required to make DECL_BY_REFERENCE work. */
1516 if (aggregate_value_p (DECL_RESULT (current_function_decl),
1517 TREE_TYPE (current_function_decl))
1518 && (!is_gimple_reg_type (TREE_TYPE (DECL_RESULT (current_function_decl)))
1519 || DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))))
1520 gimple_call_set_return_slot_opt (call, true);
1522 if (add_tsan_func_exit)
1523 tsan_func_exit_call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
1525 /* Update return value. This is bit tricky. When we do not return,
1526 do nothing. When we return we might need to update return_bb
1527 or produce a new return statement. */
1528 if (!split_part_return_p)
1530 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1531 if (tsan_func_exit_call)
1532 gsi_insert_after (&gsi, tsan_func_exit_call, GSI_NEW_STMT);
1534 else
1536 e = make_edge (call_bb, return_bb,
1537 return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
1538 ? 0 : EDGE_FALLTHRU);
1539 e->count = call_bb->count;
1540 e->probability = REG_BR_PROB_BASE;
1542 /* If there is return basic block, see what value we need to store
1543 return value into and put call just before it. */
1544 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1546 real_retval = retval = find_retval (return_bb);
1547 retbnd = find_retbnd (return_bb);
1549 if (real_retval && split_point->split_part_set_retval)
1551 gphi_iterator psi;
1553 /* See if we need new SSA_NAME for the result.
1554 When DECL_BY_REFERENCE is true, retval is actually pointer to
1555 return value and it is constant in whole function. */
1556 if (TREE_CODE (retval) == SSA_NAME
1557 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1559 retval = copy_ssa_name (retval, call);
1561 /* See if there is PHI defining return value. */
1562 for (psi = gsi_start_phis (return_bb);
1563 !gsi_end_p (psi); gsi_next (&psi))
1564 if (!virtual_operand_p (gimple_phi_result (psi.phi ())))
1565 break;
1567 /* When there is PHI, just update its value. */
1568 if (TREE_CODE (retval) == SSA_NAME
1569 && !gsi_end_p (psi))
1570 add_phi_arg (psi.phi (), retval, e, UNKNOWN_LOCATION);
1571 /* Otherwise update the return BB itself.
1572 find_return_bb allows at most one assignment to return value,
1573 so update first statement. */
1574 else
1576 gimple_stmt_iterator bsi;
1577 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi);
1578 gsi_next (&bsi))
1579 if (greturn *return_stmt
1580 = dyn_cast <greturn *> (gsi_stmt (bsi)))
1582 gimple_return_set_retval (return_stmt, retval);
1583 break;
1585 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
1586 && !gimple_clobber_p (gsi_stmt (bsi)))
1588 gimple_assign_set_rhs1 (gsi_stmt (bsi), retval);
1589 break;
1591 update_stmt (gsi_stmt (bsi));
1594 /* Replace retbnd with new one. */
1595 if (retbnd)
1597 gimple_stmt_iterator bsi;
1598 for (bsi = gsi_last_bb (return_bb); !gsi_end_p (bsi);
1599 gsi_prev (&bsi))
1600 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
1602 retbnd = copy_ssa_name (retbnd, call);
1603 gimple_return_set_retbnd (gsi_stmt (bsi), retbnd);
1604 update_stmt (gsi_stmt (bsi));
1605 break;
1609 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1611 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1612 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1614 else
1616 tree restype;
1617 restype = TREE_TYPE (DECL_RESULT (current_function_decl));
1618 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1619 if (!useless_type_conversion_p (TREE_TYPE (retval), restype))
1621 gimple cpy;
1622 tree tem = create_tmp_reg (restype);
1623 tem = make_ssa_name (tem, call);
1624 cpy = gimple_build_assign (retval, NOP_EXPR, tem);
1625 gsi_insert_after (&gsi, cpy, GSI_NEW_STMT);
1626 retval = tem;
1628 /* Build bndret call to obtain returned bounds. */
1629 if (retbnd)
1630 chkp_insert_retbnd_call (retbnd, retval, &gsi);
1631 gimple_call_set_lhs (call, retval);
1632 update_stmt (call);
1635 else
1636 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1637 if (tsan_func_exit_call)
1638 gsi_insert_after (&gsi, tsan_func_exit_call, GSI_NEW_STMT);
1640 /* We don't use return block (there is either no return in function or
1641 multiple of them). So create new basic block with return statement.
1643 else
1645 greturn *ret;
1646 if (split_point->split_part_set_retval
1647 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1649 retval = DECL_RESULT (current_function_decl);
1651 if (chkp_function_instrumented_p (current_function_decl)
1652 && BOUNDED_P (retval))
1653 retbnd = create_tmp_reg (pointer_bounds_type_node);
1655 /* We use temporary register to hold value when aggregate_value_p
1656 is false. Similarly for DECL_BY_REFERENCE we must avoid extra
1657 copy. */
1658 if (!aggregate_value_p (retval, TREE_TYPE (current_function_decl))
1659 && !DECL_BY_REFERENCE (retval))
1660 retval = create_tmp_reg (TREE_TYPE (retval));
1661 if (is_gimple_reg (retval))
1663 /* When returning by reference, there is only one SSA name
1664 assigned to RESULT_DECL (that is pointer to return value).
1665 Look it up or create new one if it is missing. */
1666 if (DECL_BY_REFERENCE (retval))
1667 retval = get_or_create_ssa_default_def (cfun, retval);
1668 /* Otherwise produce new SSA name for return value. */
1669 else
1670 retval = make_ssa_name (retval, call);
1672 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1673 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1674 else
1675 gimple_call_set_lhs (call, retval);
1677 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1678 /* Build bndret call to obtain returned bounds. */
1679 if (retbnd)
1680 chkp_insert_retbnd_call (retbnd, retval, &gsi);
1681 if (tsan_func_exit_call)
1682 gsi_insert_after (&gsi, tsan_func_exit_call, GSI_NEW_STMT);
1683 ret = gimple_build_return (retval);
1684 gsi_insert_after (&gsi, ret, GSI_NEW_STMT);
1687 free_dominance_info (CDI_DOMINATORS);
1688 free_dominance_info (CDI_POST_DOMINATORS);
1689 compute_inline_parameters (node, true);
1692 /* Execute function splitting pass. */
1694 static unsigned int
1695 execute_split_functions (void)
1697 gimple_stmt_iterator bsi;
1698 basic_block bb;
1699 int overall_time = 0, overall_size = 0;
1700 int todo = 0;
1701 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1703 if (flags_from_decl_or_type (current_function_decl)
1704 & (ECF_NORETURN|ECF_MALLOC))
1706 if (dump_file)
1707 fprintf (dump_file, "Not splitting: noreturn/malloc function.\n");
1708 return 0;
1710 if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
1712 if (dump_file)
1713 fprintf (dump_file, "Not splitting: main function.\n");
1714 return 0;
1716 /* This can be relaxed; function might become inlinable after splitting
1717 away the uninlinable part. */
1718 if (inline_edge_summary_vec.exists ()
1719 && !inline_summaries->get (node)->inlinable)
1721 if (dump_file)
1722 fprintf (dump_file, "Not splitting: not inlinable.\n");
1723 return 0;
1725 if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1727 if (dump_file)
1728 fprintf (dump_file, "Not splitting: disregarding inline limits.\n");
1729 return 0;
1731 /* This can be relaxed; most of versioning tests actually prevents
1732 a duplication. */
1733 if (!tree_versionable_function_p (current_function_decl))
1735 if (dump_file)
1736 fprintf (dump_file, "Not splitting: not versionable.\n");
1737 return 0;
1739 /* FIXME: we could support this. */
1740 if (DECL_STRUCT_FUNCTION (current_function_decl)->static_chain_decl)
1742 if (dump_file)
1743 fprintf (dump_file, "Not splitting: nested function.\n");
1744 return 0;
1747 /* See if it makes sense to try to split.
1748 It makes sense to split if we inline, that is if we have direct calls to
1749 handle or direct calls are possibly going to appear as result of indirect
1750 inlining or LTO. Also handle -fprofile-generate as LTO to allow non-LTO
1751 training for LTO -fprofile-use build.
1753 Note that we are not completely conservative about disqualifying functions
1754 called once. It is possible that the caller is called more then once and
1755 then inlining would still benefit. */
1756 if ((!node->callers
1757 /* Local functions called once will be completely inlined most of time. */
1758 || (!node->callers->next_caller && node->local.local))
1759 && !node->address_taken
1760 && !node->has_aliases_p ()
1761 && (!flag_lto || !node->externally_visible))
1763 if (dump_file)
1764 fprintf (dump_file, "Not splitting: not called directly "
1765 "or called once.\n");
1766 return 0;
1769 /* FIXME: We can actually split if splitting reduces call overhead. */
1770 if (!flag_inline_small_functions
1771 && !DECL_DECLARED_INLINE_P (current_function_decl))
1773 if (dump_file)
1774 fprintf (dump_file, "Not splitting: not autoinlining and function"
1775 " is not inline.\n");
1776 return 0;
1779 /* We enforce splitting after loop headers when profile info is not
1780 available. */
1781 if (profile_status_for_fn (cfun) != PROFILE_READ)
1782 mark_dfs_back_edges ();
1784 /* Initialize bitmap to track forbidden calls. */
1785 forbidden_dominators = BITMAP_ALLOC (NULL);
1786 calculate_dominance_info (CDI_DOMINATORS);
1788 /* Compute local info about basic blocks and determine function size/time. */
1789 bb_info_vec.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
1790 memset (&best_split_point, 0, sizeof (best_split_point));
1791 basic_block return_bb = find_return_bb ();
1792 int tsan_exit_found = -1;
1793 FOR_EACH_BB_FN (bb, cfun)
1795 int time = 0;
1796 int size = 0;
1797 int freq = compute_call_stmt_bb_frequency (current_function_decl, bb);
1799 if (dump_file && (dump_flags & TDF_DETAILS))
1800 fprintf (dump_file, "Basic block %i\n", bb->index);
1802 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1804 int this_time, this_size;
1805 gimple stmt = gsi_stmt (bsi);
1807 this_size = estimate_num_insns (stmt, &eni_size_weights);
1808 this_time = estimate_num_insns (stmt, &eni_time_weights) * freq;
1809 size += this_size;
1810 time += this_time;
1811 check_forbidden_calls (stmt);
1813 if (dump_file && (dump_flags & TDF_DETAILS))
1815 fprintf (dump_file, " freq:%6i size:%3i time:%3i ",
1816 freq, this_size, this_time);
1817 print_gimple_stmt (dump_file, stmt, 0, 0);
1820 if ((flag_sanitize & SANITIZE_THREAD)
1821 && is_gimple_call (stmt)
1822 && gimple_call_internal_p (stmt)
1823 && gimple_call_internal_fn (stmt) == IFN_TSAN_FUNC_EXIT)
1825 /* We handle TSAN_FUNC_EXIT for splitting either in the
1826 return_bb, or in its immediate predecessors. */
1827 if ((bb != return_bb && !find_edge (bb, return_bb))
1828 || (tsan_exit_found != -1
1829 && tsan_exit_found != (bb != return_bb)))
1831 if (dump_file)
1832 fprintf (dump_file, "Not splitting: TSAN_FUNC_EXIT"
1833 " in unexpected basic block.\n");
1834 BITMAP_FREE (forbidden_dominators);
1835 bb_info_vec.release ();
1836 return 0;
1838 tsan_exit_found = bb != return_bb;
1841 overall_time += time;
1842 overall_size += size;
1843 bb_info_vec[bb->index].time = time;
1844 bb_info_vec[bb->index].size = size;
1846 find_split_points (return_bb, overall_time, overall_size);
1847 if (best_split_point.split_bbs)
1849 split_function (return_bb, &best_split_point, tsan_exit_found == 1);
1850 BITMAP_FREE (best_split_point.ssa_names_to_pass);
1851 BITMAP_FREE (best_split_point.split_bbs);
1852 todo = TODO_update_ssa | TODO_cleanup_cfg;
1854 BITMAP_FREE (forbidden_dominators);
1855 bb_info_vec.release ();
1856 return todo;
1859 namespace {
1861 const pass_data pass_data_split_functions =
1863 GIMPLE_PASS, /* type */
1864 "fnsplit", /* name */
1865 OPTGROUP_NONE, /* optinfo_flags */
1866 TV_IPA_FNSPLIT, /* tv_id */
1867 PROP_cfg, /* properties_required */
1868 0, /* properties_provided */
1869 0, /* properties_destroyed */
1870 0, /* todo_flags_start */
1871 0, /* todo_flags_finish */
1874 class pass_split_functions : public gimple_opt_pass
1876 public:
1877 pass_split_functions (gcc::context *ctxt)
1878 : gimple_opt_pass (pass_data_split_functions, ctxt)
1881 /* opt_pass methods: */
1882 virtual bool gate (function *);
1883 virtual unsigned int execute (function *)
1885 return execute_split_functions ();
1888 }; // class pass_split_functions
1890 bool
1891 pass_split_functions::gate (function *)
1893 /* When doing profile feedback, we want to execute the pass after profiling
1894 is read. So disable one in early optimization. */
1895 return (flag_partial_inlining
1896 && !profile_arc_flag && !flag_branch_probabilities);
1899 } // anon namespace
1901 gimple_opt_pass *
1902 make_pass_split_functions (gcc::context *ctxt)
1904 return new pass_split_functions (ctxt);
1907 /* Execute function splitting pass. */
1909 static unsigned int
1910 execute_feedback_split_functions (void)
1912 unsigned int retval = execute_split_functions ();
1913 if (retval)
1914 retval |= TODO_rebuild_cgraph_edges;
1915 return retval;
1918 namespace {
1920 const pass_data pass_data_feedback_split_functions =
1922 GIMPLE_PASS, /* type */
1923 "feedback_fnsplit", /* name */
1924 OPTGROUP_NONE, /* optinfo_flags */
1925 TV_IPA_FNSPLIT, /* tv_id */
1926 PROP_cfg, /* properties_required */
1927 0, /* properties_provided */
1928 0, /* properties_destroyed */
1929 0, /* todo_flags_start */
1930 0, /* todo_flags_finish */
1933 class pass_feedback_split_functions : public gimple_opt_pass
1935 public:
1936 pass_feedback_split_functions (gcc::context *ctxt)
1937 : gimple_opt_pass (pass_data_feedback_split_functions, ctxt)
1940 /* opt_pass methods: */
1941 virtual bool gate (function *);
1942 virtual unsigned int execute (function *)
1944 return execute_feedback_split_functions ();
1947 }; // class pass_feedback_split_functions
1949 bool
1950 pass_feedback_split_functions::gate (function *)
1952 /* We don't need to split when profiling at all, we are producing
1953 lousy code anyway. */
1954 return (flag_partial_inlining
1955 && flag_branch_probabilities);
1958 } // anon namespace
1960 gimple_opt_pass *
1961 make_pass_feedback_split_functions (gcc::context *ctxt)
1963 return new pass_feedback_split_functions (ctxt);