RISC-V: Make dynamic LMUL cost model more accurate for conversion codes
[official-gcc.git] / gcc / ipa-split.cc
blob1a7285ff5dcf80dff1eed0577a6d4b96e3651e40
1 /* Function splitting pass
2 Copyright (C) 2010-2023 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 "backend.h"
81 #include "rtl.h"
82 #include "tree.h"
83 #include "gimple.h"
84 #include "cfghooks.h"
85 #include "alloc-pool.h"
86 #include "tree-pass.h"
87 #include "ssa.h"
88 #include "cgraph.h"
89 #include "diagnostic.h"
90 #include "fold-const.h"
91 #include "cfganal.h"
92 #include "calls.h"
93 #include "gimplify.h"
94 #include "gimple-iterator.h"
95 #include "gimplify-me.h"
96 #include "gimple-walk.h"
97 #include "symbol-summary.h"
98 #include "ipa-prop.h"
99 #include "tree-cfg.h"
100 #include "tree-into-ssa.h"
101 #include "tree-dfa.h"
102 #include "tree-inline.h"
103 #include "gimple-pretty-print.h"
104 #include "ipa-fnsummary.h"
105 #include "cfgloop.h"
106 #include "attribs.h"
107 #include "ipa-strub.h"
109 /* Per basic block info. */
111 class split_bb_info
113 public:
114 unsigned int size;
115 sreal time;
118 static vec<split_bb_info> bb_info_vec;
120 /* Description of split point. */
122 class split_point
124 public:
125 /* Size of the partitions. */
126 sreal header_time, split_time;
127 unsigned int header_size, split_size;
129 /* SSA names that need to be passed into spit function. */
130 bitmap ssa_names_to_pass;
132 /* Basic block where we split (that will become entry point of new function. */
133 basic_block entry_bb;
135 /* Count for entering the split part.
136 This is not count of the entry_bb because it may be in loop. */
137 profile_count count;
139 /* Basic blocks we are splitting away. */
140 bitmap split_bbs;
142 /* True when return value is computed on split part and thus it needs
143 to be returned. */
144 bool split_part_set_retval;
147 /* Best split point found. */
149 class split_point best_split_point;
151 /* Set of basic blocks that are not allowed to dominate a split point. */
153 static bitmap forbidden_dominators;
155 static tree find_retval (basic_block return_bb);
157 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
158 variable, check it if it is present in bitmap passed via DATA. */
160 static bool
161 test_nonssa_use (gimple *, tree t, tree, void *data)
163 t = get_base_address (t);
165 if (!t || is_gimple_reg (t))
166 return false;
168 if (TREE_CODE (t) == PARM_DECL
169 || (VAR_P (t)
170 && auto_var_in_fn_p (t, current_function_decl))
171 || TREE_CODE (t) == RESULT_DECL
172 /* Normal labels are part of CFG and will be handled gratefully.
173 Forced labels however can be used directly by statements and
174 need to stay in one partition along with their uses. */
175 || (TREE_CODE (t) == LABEL_DECL
176 && FORCED_LABEL (t)))
177 return bitmap_bit_p ((bitmap)data, DECL_UID (t));
179 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
180 to pretend that the value pointed to is actual result decl. */
181 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
182 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
183 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
184 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
185 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
186 return
187 bitmap_bit_p ((bitmap)data,
188 DECL_UID (DECL_RESULT (current_function_decl)));
190 return false;
193 /* Dump split point CURRENT. */
195 static void
196 dump_split_point (FILE * file, class split_point *current)
198 fprintf (file,
199 "Split point at BB %i\n"
200 " header time: %f header size: %i\n"
201 " split time: %f split size: %i\n bbs: ",
202 current->entry_bb->index, current->header_time.to_double (),
203 current->header_size, current->split_time.to_double (),
204 current->split_size);
205 dump_bitmap (file, current->split_bbs);
206 fprintf (file, " SSA names to pass: ");
207 dump_bitmap (file, current->ssa_names_to_pass);
210 /* Look for all BBs in header that might lead to the split part and verify
211 that they are not defining any non-SSA var used by the split part.
212 Parameters are the same as for consider_split. */
214 static bool
215 verify_non_ssa_vars (class split_point *current, bitmap non_ssa_vars,
216 basic_block return_bb)
218 bitmap seen = BITMAP_ALLOC (NULL);
219 vec<basic_block> worklist = vNULL;
220 edge e;
221 edge_iterator ei;
222 bool ok = true;
223 basic_block bb;
225 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
226 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
227 && !bitmap_bit_p (current->split_bbs, e->src->index))
229 worklist.safe_push (e->src);
230 bitmap_set_bit (seen, e->src->index);
233 while (!worklist.is_empty ())
235 bb = worklist.pop ();
236 FOR_EACH_EDGE (e, ei, bb->preds)
237 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
238 && bitmap_set_bit (seen, e->src->index))
240 gcc_checking_assert (!bitmap_bit_p (current->split_bbs,
241 e->src->index));
242 worklist.safe_push (e->src);
244 for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
245 gsi_next (&bsi))
247 gimple *stmt = gsi_stmt (bsi);
248 if (is_gimple_debug (stmt))
249 continue;
250 if (walk_stmt_load_store_addr_ops
251 (stmt, non_ssa_vars, test_nonssa_use, test_nonssa_use,
252 test_nonssa_use))
254 ok = false;
255 goto done;
257 if (glabel *label_stmt = dyn_cast <glabel *> (stmt))
258 if (test_nonssa_use (stmt, gimple_label_label (label_stmt),
259 NULL_TREE, non_ssa_vars))
261 ok = false;
262 goto done;
265 for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
266 gsi_next (&bsi))
268 if (walk_stmt_load_store_addr_ops
269 (gsi_stmt (bsi), non_ssa_vars, test_nonssa_use, test_nonssa_use,
270 test_nonssa_use))
272 ok = false;
273 goto done;
276 FOR_EACH_EDGE (e, ei, bb->succs)
278 if (e->dest != return_bb)
279 continue;
280 for (gphi_iterator bsi = gsi_start_phis (return_bb);
281 !gsi_end_p (bsi);
282 gsi_next (&bsi))
284 gphi *stmt = bsi.phi ();
285 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
287 if (virtual_operand_p (gimple_phi_result (stmt)))
288 continue;
289 if (TREE_CODE (op) != SSA_NAME
290 && test_nonssa_use (stmt, op, op, non_ssa_vars))
292 ok = false;
293 goto done;
299 /* Verify that the rest of function does not define any label
300 used by the split part. */
301 FOR_EACH_BB_FN (bb, cfun)
302 if (!bitmap_bit_p (current->split_bbs, bb->index)
303 && !bitmap_bit_p (seen, bb->index))
305 gimple_stmt_iterator bsi;
306 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
307 if (glabel *label_stmt = dyn_cast <glabel *> (gsi_stmt (bsi)))
309 if (test_nonssa_use (label_stmt,
310 gimple_label_label (label_stmt),
311 NULL_TREE, non_ssa_vars))
313 ok = false;
314 goto done;
317 else
318 break;
321 done:
322 BITMAP_FREE (seen);
323 worklist.release ();
324 return ok;
327 /* If STMT is a call, check the callee against a list of forbidden
328 predicate functions. If a match is found, look for uses of the
329 call result in condition statements that compare against zero.
330 For each such use, find the block targeted by the condition
331 statement for the nonzero result, and set the bit for this block
332 in the forbidden dominators bitmap. The purpose of this is to avoid
333 selecting a split point where we are likely to lose the chance
334 to optimize away an unused function call. */
336 static void
337 check_forbidden_calls (gimple *stmt)
339 imm_use_iterator use_iter;
340 use_operand_p use_p;
341 tree lhs;
343 /* At the moment, __builtin_constant_p is the only forbidden
344 predicate function call (see PR49642). */
345 if (!gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P))
346 return;
348 lhs = gimple_call_lhs (stmt);
350 if (!lhs || TREE_CODE (lhs) != SSA_NAME)
351 return;
353 FOR_EACH_IMM_USE_FAST (use_p, use_iter, lhs)
355 tree op1;
356 basic_block use_bb, forbidden_bb;
357 enum tree_code code;
358 edge true_edge, false_edge;
359 gcond *use_stmt;
361 use_stmt = dyn_cast <gcond *> (USE_STMT (use_p));
362 if (!use_stmt)
363 continue;
365 /* Assuming canonical form for GIMPLE_COND here, with constant
366 in second position. */
367 op1 = gimple_cond_rhs (use_stmt);
368 code = gimple_cond_code (use_stmt);
369 use_bb = gimple_bb (use_stmt);
371 extract_true_false_edges_from_block (use_bb, &true_edge, &false_edge);
373 /* We're only interested in comparisons that distinguish
374 unambiguously from zero. */
375 if (!integer_zerop (op1) || code == LE_EXPR || code == GE_EXPR)
376 continue;
378 if (code == EQ_EXPR)
379 forbidden_bb = false_edge->dest;
380 else
381 forbidden_bb = true_edge->dest;
383 bitmap_set_bit (forbidden_dominators, forbidden_bb->index);
387 /* If BB is dominated by any block in the forbidden dominators set,
388 return TRUE; else FALSE. */
390 static bool
391 dominated_by_forbidden (basic_block bb)
393 unsigned dom_bb;
394 bitmap_iterator bi;
396 EXECUTE_IF_SET_IN_BITMAP (forbidden_dominators, 1, dom_bb, bi)
398 if (dominated_by_p (CDI_DOMINATORS, bb,
399 BASIC_BLOCK_FOR_FN (cfun, dom_bb)))
400 return true;
403 return false;
406 /* For give split point CURRENT and return block RETURN_BB return 1
407 if ssa name VAL is set by split part and 0 otherwise. */
408 static bool
409 split_part_set_ssa_name_p (tree val, class split_point *current,
410 basic_block return_bb)
412 if (TREE_CODE (val) != SSA_NAME)
413 return false;
415 return (!SSA_NAME_IS_DEFAULT_DEF (val)
416 && (bitmap_bit_p (current->split_bbs,
417 gimple_bb (SSA_NAME_DEF_STMT (val))->index)
418 || gimple_bb (SSA_NAME_DEF_STMT (val)) == return_bb));
421 /* We found an split_point CURRENT. NON_SSA_VARS is bitmap of all non ssa
422 variables used and RETURN_BB is return basic block.
423 See if we can split function here. */
425 static void
426 consider_split (class split_point *current, bitmap non_ssa_vars,
427 basic_block return_bb)
429 tree parm;
430 unsigned int num_args = 0;
431 unsigned int call_overhead;
432 edge e;
433 edge_iterator ei;
434 gphi_iterator bsi;
435 unsigned int i;
436 tree retval;
437 bool back_edge = false;
439 if (dump_file && (dump_flags & TDF_DETAILS))
440 dump_split_point (dump_file, current);
442 current->count = profile_count::zero ();
443 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
445 if (e->flags & EDGE_DFS_BACK)
446 back_edge = true;
447 if (!bitmap_bit_p (current->split_bbs, e->src->index))
448 current->count += e->count ();
451 /* Do not split when we would end up calling function anyway.
452 Compares are three state, use !(...<...) to also give up when outcome
453 is unknown. */
454 if (!(current->count
455 < (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.apply_scale
456 (param_partial_inlining_entry_probability, 100))))
458 /* When profile is guessed, we cannot expect it to give us
459 realistic estimate on likeliness of function taking the
460 complex path. As a special case, when tail of the function is
461 a loop, enable splitting since inlining code skipping the loop
462 is likely noticeable win. */
463 if (back_edge
464 && profile_status_for_fn (cfun) != PROFILE_READ
465 && current->count
466 < ENTRY_BLOCK_PTR_FOR_FN (cfun)->count)
468 if (dump_file && (dump_flags & TDF_DETAILS))
470 fprintf (dump_file,
471 " Split before loop, accepting despite low counts");
472 current->count.dump (dump_file);
473 fprintf (dump_file, " ");
474 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.dump (dump_file);
477 else
479 if (dump_file && (dump_flags & TDF_DETAILS))
480 fprintf (dump_file,
481 " Refused: incoming frequency is too large.\n");
482 return;
486 if (!current->header_size)
488 if (dump_file && (dump_flags & TDF_DETAILS))
489 fprintf (dump_file, " Refused: header empty\n");
490 return;
493 /* Verify that PHI args on entry are either virtual or all their operands
494 incoming from header are the same. */
495 for (bsi = gsi_start_phis (current->entry_bb); !gsi_end_p (bsi); gsi_next (&bsi))
497 gphi *stmt = bsi.phi ();
498 tree val = NULL;
500 if (virtual_operand_p (gimple_phi_result (stmt)))
501 continue;
502 for (i = 0; i < gimple_phi_num_args (stmt); i++)
504 edge e = gimple_phi_arg_edge (stmt, i);
505 if (!bitmap_bit_p (current->split_bbs, e->src->index))
507 tree edge_val = gimple_phi_arg_def (stmt, i);
508 if (val && edge_val != val)
510 if (dump_file && (dump_flags & TDF_DETAILS))
511 fprintf (dump_file,
512 " Refused: entry BB has PHI with multiple variants\n");
513 return;
515 val = edge_val;
521 /* See what argument we will pass to the split function and compute
522 call overhead. */
523 call_overhead = eni_size_weights.call_cost;
524 for (parm = DECL_ARGUMENTS (current_function_decl); parm;
525 parm = DECL_CHAIN (parm))
527 if (!is_gimple_reg (parm))
529 if (bitmap_bit_p (non_ssa_vars, DECL_UID (parm)))
531 if (dump_file && (dump_flags & TDF_DETAILS))
532 fprintf (dump_file,
533 " Refused: need to pass non-ssa param values\n");
534 return;
537 else
539 tree ddef = ssa_default_def (cfun, parm);
540 if (ddef
541 && bitmap_bit_p (current->ssa_names_to_pass,
542 SSA_NAME_VERSION (ddef)))
544 if (!VOID_TYPE_P (TREE_TYPE (parm)))
545 call_overhead += estimate_move_cost (TREE_TYPE (parm), false);
546 num_args++;
550 if (!VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
551 call_overhead += estimate_move_cost (TREE_TYPE (TREE_TYPE
552 (current_function_decl)),
553 false);
555 if (current->split_size <= call_overhead)
557 if (dump_file && (dump_flags & TDF_DETAILS))
558 fprintf (dump_file,
559 " Refused: split size is smaller than call overhead\n");
560 return;
562 /* FIXME: The logic here is not very precise, because inliner does use
563 inline predicates to reduce function body size. We add 10 to anticipate
564 that. Next stage1 we should try to be more meaningful here. */
565 if (current->header_size + call_overhead
566 >= (unsigned int)(DECL_DECLARED_INLINE_P (current_function_decl)
567 ? param_max_inline_insns_single
568 : param_max_inline_insns_auto) + 10)
570 if (dump_file && (dump_flags & TDF_DETAILS))
571 fprintf (dump_file,
572 " Refused: header size is too large for inline candidate\n");
573 return;
576 /* Splitting functions brings the target out of comdat group; this will
577 lead to code duplication if the function is reused by other unit.
578 Limit this duplication. This is consistent with limit in tree-sra.cc
579 FIXME: with LTO we ought to be able to do better! */
580 if (DECL_ONE_ONLY (current_function_decl)
581 && current->split_size >= (unsigned int) param_max_inline_insns_auto + 10)
583 if (dump_file && (dump_flags & TDF_DETAILS))
584 fprintf (dump_file,
585 " Refused: function is COMDAT and tail is too large\n");
586 return;
588 /* For comdat functions also reject very small tails; those will likely get
589 inlined back and we do not want to risk the duplication overhead.
590 FIXME: with LTO we ought to be able to do better! */
591 if (DECL_ONE_ONLY (current_function_decl)
592 && current->split_size
593 <= (unsigned int) param_early_inlining_insns / 2)
595 if (dump_file && (dump_flags & TDF_DETAILS))
596 fprintf (dump_file,
597 " Refused: function is COMDAT and tail is too small\n");
598 return;
601 /* FIXME: we currently can pass only SSA function parameters to the split
602 arguments. Once parm_adjustment infrastructure is supported by cloning,
603 we can pass more than that. */
604 if (num_args != bitmap_count_bits (current->ssa_names_to_pass))
607 if (dump_file && (dump_flags & TDF_DETAILS))
608 fprintf (dump_file,
609 " Refused: need to pass non-param values\n");
610 return;
613 /* When there are non-ssa vars used in the split region, see if they
614 are used in the header region. If so, reject the split.
615 FIXME: we can use nested function support to access both. */
616 if (!bitmap_empty_p (non_ssa_vars)
617 && !verify_non_ssa_vars (current, non_ssa_vars, return_bb))
619 if (dump_file && (dump_flags & TDF_DETAILS))
620 fprintf (dump_file,
621 " Refused: split part has non-ssa uses\n");
622 return;
625 /* If the split point is dominated by a forbidden block, reject
626 the split. */
627 if (!bitmap_empty_p (forbidden_dominators)
628 && dominated_by_forbidden (current->entry_bb))
630 if (dump_file && (dump_flags & TDF_DETAILS))
631 fprintf (dump_file,
632 " Refused: split point dominated by forbidden block\n");
633 return;
636 /* See if retval used by return bb is computed by header or split part.
637 When it is computed by split part, we need to produce return statement
638 in the split part and add code to header to pass it around.
640 This is bit tricky to test:
641 1) When there is no return_bb or no return value, we always pass
642 value around.
643 2) Invariants are always computed by caller.
644 3) For SSA we need to look if defining statement is in header or split part
645 4) For non-SSA we need to look where the var is computed. */
646 retval = find_retval (return_bb);
647 if (!retval)
649 /* If there is a return_bb with no return value in function returning
650 value by reference, also make the split part return void, otherwise
651 we expansion would try to create a non-POD temporary, which is
652 invalid. */
653 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
654 && DECL_RESULT (current_function_decl)
655 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
656 current->split_part_set_retval = false;
657 else
658 current->split_part_set_retval = true;
660 else if (is_gimple_min_invariant (retval))
661 current->split_part_set_retval = false;
662 /* Special case is value returned by reference we record as if it was non-ssa
663 set to result_decl. */
664 else if (TREE_CODE (retval) == SSA_NAME
665 && SSA_NAME_VAR (retval)
666 && TREE_CODE (SSA_NAME_VAR (retval)) == RESULT_DECL
667 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
668 current->split_part_set_retval
669 = bitmap_bit_p (non_ssa_vars, DECL_UID (SSA_NAME_VAR (retval)));
670 else if (TREE_CODE (retval) == SSA_NAME)
671 current->split_part_set_retval
672 = split_part_set_ssa_name_p (retval, current, return_bb);
673 else if (TREE_CODE (retval) == PARM_DECL)
674 current->split_part_set_retval = false;
675 else if (VAR_P (retval)
676 || TREE_CODE (retval) == RESULT_DECL)
677 current->split_part_set_retval
678 = bitmap_bit_p (non_ssa_vars, DECL_UID (retval));
679 else
680 current->split_part_set_retval = true;
682 /* split_function fixes up at most one PHI non-virtual PHI node in return_bb,
683 for the return value. If there are other PHIs, give up. */
684 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
686 gphi_iterator psi;
688 for (psi = gsi_start_phis (return_bb); !gsi_end_p (psi); gsi_next (&psi))
689 if (!virtual_operand_p (gimple_phi_result (psi.phi ()))
690 && !(retval
691 && current->split_part_set_retval
692 && TREE_CODE (retval) == SSA_NAME
693 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
694 && SSA_NAME_DEF_STMT (retval) == psi.phi ()))
696 if (dump_file && (dump_flags & TDF_DETAILS))
697 fprintf (dump_file,
698 " Refused: return bb has extra PHIs\n");
699 return;
703 if (dump_file && (dump_flags & TDF_DETAILS))
704 fprintf (dump_file, " Accepted!\n");
706 /* At the moment chose split point with lowest count and that leaves
707 out smallest size of header.
708 In future we might re-consider this heuristics. */
709 if (!best_split_point.split_bbs
710 || best_split_point.count
711 > current->count
712 || (best_split_point.count == current->count
713 && best_split_point.split_size < current->split_size))
716 if (dump_file && (dump_flags & TDF_DETAILS))
717 fprintf (dump_file, " New best split point!\n");
718 if (best_split_point.ssa_names_to_pass)
720 BITMAP_FREE (best_split_point.ssa_names_to_pass);
721 BITMAP_FREE (best_split_point.split_bbs);
723 best_split_point = *current;
724 best_split_point.ssa_names_to_pass = BITMAP_ALLOC (NULL);
725 bitmap_copy (best_split_point.ssa_names_to_pass,
726 current->ssa_names_to_pass);
727 best_split_point.split_bbs = BITMAP_ALLOC (NULL);
728 bitmap_copy (best_split_point.split_bbs, current->split_bbs);
732 /* Return basic block containing RETURN statement. We allow basic blocks
733 of the form:
734 <retval> = tmp_var;
735 return <retval>
736 but return_bb cannot be more complex than this (except for
737 -fsanitize=thread we allow TSAN_FUNC_EXIT () internal call in there).
738 If nothing is found, return the exit block.
740 When there are multiple RETURN statement, chose one with return value,
741 since that one is more likely shared by multiple code paths.
743 Return BB is special, because for function splitting it is the only
744 basic block that is duplicated in between header and split part of the
745 function.
747 TODO: We might support multiple return blocks. */
749 static basic_block
750 find_return_bb (void)
752 edge e;
753 basic_block return_bb = EXIT_BLOCK_PTR_FOR_FN (cfun);
754 gimple_stmt_iterator bsi;
755 bool found_return = false;
756 tree retval = NULL_TREE;
758 if (!single_pred_p (EXIT_BLOCK_PTR_FOR_FN (cfun)))
759 return return_bb;
761 e = single_pred_edge (EXIT_BLOCK_PTR_FOR_FN (cfun));
762 for (bsi = gsi_last_bb (e->src); !gsi_end_p (bsi); gsi_prev (&bsi))
764 gimple *stmt = gsi_stmt (bsi);
765 if (gimple_code (stmt) == GIMPLE_LABEL
766 || is_gimple_debug (stmt)
767 || gimple_clobber_p (stmt))
769 else if (gimple_code (stmt) == GIMPLE_ASSIGN
770 && found_return
771 && gimple_assign_single_p (stmt)
772 && (auto_var_in_fn_p (gimple_assign_rhs1 (stmt),
773 current_function_decl)
774 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
775 && retval == gimple_assign_lhs (stmt))
777 else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
779 found_return = true;
780 retval = gimple_return_retval (return_stmt);
782 /* For -fsanitize=thread, allow also TSAN_FUNC_EXIT () in the return
783 bb. */
784 else if ((flag_sanitize & SANITIZE_THREAD)
785 && gimple_call_internal_p (stmt, IFN_TSAN_FUNC_EXIT))
787 else
788 break;
790 if (gsi_end_p (bsi) && found_return)
791 return_bb = e->src;
793 return return_bb;
796 /* Given return basic block RETURN_BB, see where return value is really
797 stored. */
798 static tree
799 find_retval (basic_block return_bb)
801 gimple_stmt_iterator bsi;
802 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
803 if (greturn *return_stmt = dyn_cast <greturn *> (gsi_stmt (bsi)))
804 return gimple_return_retval (return_stmt);
805 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
806 && !gimple_clobber_p (gsi_stmt (bsi)))
807 return gimple_assign_rhs1 (gsi_stmt (bsi));
808 return NULL;
811 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
812 variable, mark it as used in bitmap passed via DATA.
813 Return true when access to T prevents splitting the function. */
815 static bool
816 mark_nonssa_use (gimple *, tree t, tree, void *data)
818 t = get_base_address (t);
820 if (!t || is_gimple_reg (t))
821 return false;
823 /* At present we can't pass non-SSA arguments to split function.
824 FIXME: this can be relaxed by passing references to arguments. */
825 if (TREE_CODE (t) == PARM_DECL)
827 if (dump_file && (dump_flags & TDF_DETAILS))
828 fprintf (dump_file,
829 "Cannot split: use of non-ssa function parameter.\n");
830 return true;
833 if ((VAR_P (t) && auto_var_in_fn_p (t, current_function_decl))
834 || TREE_CODE (t) == RESULT_DECL
835 || (TREE_CODE (t) == LABEL_DECL && FORCED_LABEL (t)))
836 bitmap_set_bit ((bitmap)data, DECL_UID (t));
838 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
839 to pretend that the value pointed to is actual result decl. */
840 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
841 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
842 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
843 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
844 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
845 return
846 bitmap_bit_p ((bitmap)data,
847 DECL_UID (DECL_RESULT (current_function_decl)));
849 return false;
852 /* Compute local properties of basic block BB we collect when looking for
853 split points. We look for ssa defs and store them in SET_SSA_NAMES,
854 for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic
855 vars stored in NON_SSA_VARS.
857 When BB has edge to RETURN_BB, collect uses in RETURN_BB too.
859 Return false when BB contains something that prevents it from being put into
860 split function. */
862 static bool
863 visit_bb (basic_block bb, basic_block return_bb,
864 bitmap set_ssa_names, bitmap used_ssa_names,
865 bitmap non_ssa_vars)
867 edge e;
868 edge_iterator ei;
869 bool can_split = true;
871 for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
872 gsi_next (&bsi))
874 gimple *stmt = gsi_stmt (bsi);
875 tree op;
876 ssa_op_iter iter;
878 if (is_gimple_debug (stmt))
879 continue;
881 if (gimple_clobber_p (stmt))
882 continue;
884 /* FIXME: We can split regions containing EH. We cannot however
885 split RESX, EH_DISPATCH and EH_POINTER referring to same region
886 into different partitions. This would require tracking of
887 EH regions and checking in consider_split_point if they
888 are not used elsewhere. */
889 if (gimple_code (stmt) == GIMPLE_RESX)
891 if (dump_file && (dump_flags & TDF_DETAILS))
892 fprintf (dump_file, "Cannot split: resx.\n");
893 can_split = false;
895 if (gimple_code (stmt) == GIMPLE_EH_DISPATCH)
897 if (dump_file && (dump_flags & TDF_DETAILS))
898 fprintf (dump_file, "Cannot split: eh dispatch.\n");
899 can_split = false;
902 /* Check calls that would prevent splitting. */
903 if (gimple_code (stmt) == GIMPLE_CALL)
905 if (tree decl = gimple_call_fndecl (stmt))
907 /* Check builtins that would prevent splitting. */
908 if (fndecl_built_in_p (decl, BUILT_IN_NORMAL))
909 switch (DECL_FUNCTION_CODE (decl))
911 /* FIXME: once we will allow passing non-parm values to
912 split part, we need to be sure to handle correct
913 builtin_stack_save and builtin_stack_restore. At the
914 moment we are safe; there is no way to store
915 builtin_stack_save result in non-SSA variable since all
916 calls to those are compiler generated. */
917 case BUILT_IN_APPLY:
918 case BUILT_IN_APPLY_ARGS:
919 case BUILT_IN_VA_START:
920 if (dump_file && (dump_flags & TDF_DETAILS))
921 fprintf (dump_file,
922 "Cannot split: builtin_apply and va_start.\n");
923 can_split = false;
924 break;
925 case BUILT_IN_EH_POINTER:
926 if (dump_file && (dump_flags & TDF_DETAILS))
927 fprintf (dump_file,
928 "Cannot split: builtin_eh_pointer.\n");
929 can_split = false;
930 break;
931 default:
932 break;
935 /* Calls to functions (which have the warning or error
936 attribute on them) should not be split off into another
937 function. */
938 if (lookup_attribute ("warning", DECL_ATTRIBUTES (decl))
939 || lookup_attribute ("error", DECL_ATTRIBUTES (decl)))
941 if (dump_file && (dump_flags & TDF_DETAILS))
942 fprintf (dump_file,
943 "Cannot split: warning or error attribute.\n");
944 can_split = false;
949 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
950 bitmap_set_bit (set_ssa_names, SSA_NAME_VERSION (op));
951 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
952 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
953 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
954 mark_nonssa_use,
955 mark_nonssa_use,
956 mark_nonssa_use);
958 for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
959 gsi_next (&bsi))
961 gphi *stmt = bsi.phi ();
962 unsigned int i;
964 if (virtual_operand_p (gimple_phi_result (stmt)))
965 continue;
966 bitmap_set_bit (set_ssa_names,
967 SSA_NAME_VERSION (gimple_phi_result (stmt)));
968 for (i = 0; i < gimple_phi_num_args (stmt); i++)
970 tree op = gimple_phi_arg_def (stmt, i);
971 if (TREE_CODE (op) == SSA_NAME)
972 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
974 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
975 mark_nonssa_use,
976 mark_nonssa_use,
977 mark_nonssa_use);
979 /* Record also uses coming from PHI operand in return BB. */
980 FOR_EACH_EDGE (e, ei, bb->succs)
981 if (e->dest == return_bb)
983 for (gphi_iterator bsi = gsi_start_phis (return_bb);
984 !gsi_end_p (bsi);
985 gsi_next (&bsi))
987 gphi *stmt = bsi.phi ();
988 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
990 if (virtual_operand_p (gimple_phi_result (stmt)))
991 continue;
992 if (TREE_CODE (op) == SSA_NAME)
993 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
994 else
995 can_split &= !mark_nonssa_use (stmt, op, op, non_ssa_vars);
998 return can_split;
1001 /* Stack entry for recursive DFS walk in find_split_point. */
1003 class stack_entry
1005 public:
1006 /* Basic block we are examining. */
1007 basic_block bb;
1009 /* SSA names set and used by the BB and all BBs reachable
1010 from it via DFS walk. */
1011 bitmap set_ssa_names, used_ssa_names;
1012 bitmap non_ssa_vars;
1014 /* All BBS visited from this BB via DFS walk. */
1015 bitmap bbs_visited;
1017 /* Last examined edge in DFS walk. Since we walk unoriented graph,
1018 the value is up to sum of incoming and outgoing edges of BB. */
1019 unsigned int edge_num;
1021 /* Stack entry index of earliest BB reachable from current BB
1022 or any BB visited later in DFS walk. */
1023 int earliest;
1025 /* Overall time and size of all BBs reached from this BB in DFS walk. */
1026 sreal overall_time;
1027 int overall_size;
1029 /* When false we cannot split on this BB. */
1030 bool can_split;
1034 /* Find all articulations and call consider_split on them.
1035 OVERALL_TIME and OVERALL_SIZE is time and size of the function.
1037 We perform basic algorithm for finding an articulation in a graph
1038 created from CFG by considering it to be an unoriented graph.
1040 The articulation is discovered via DFS walk. We collect earliest
1041 basic block on stack that is reachable via backward edge. Articulation
1042 is any basic block such that there is no backward edge bypassing it.
1043 To reduce stack usage we maintain heap allocated stack in STACK vector.
1044 AUX pointer of BB is set to index it appears in the stack or -1 once
1045 it is visited and popped off the stack.
1047 The algorithm finds articulation after visiting the whole component
1048 reachable by it. This makes it convenient to collect information about
1049 the component used by consider_split. */
1051 static void
1052 find_split_points (basic_block return_bb, sreal overall_time, int overall_size)
1054 stack_entry first;
1055 vec<stack_entry> stack = vNULL;
1056 basic_block bb;
1057 class split_point current;
1059 current.header_time = overall_time;
1060 current.header_size = overall_size;
1061 current.split_time = 0;
1062 current.split_size = 0;
1063 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
1065 first.bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1066 first.edge_num = 0;
1067 first.overall_time = 0;
1068 first.overall_size = 0;
1069 first.earliest = INT_MAX;
1070 first.set_ssa_names = 0;
1071 first.used_ssa_names = 0;
1072 first.non_ssa_vars = 0;
1073 first.bbs_visited = 0;
1074 first.can_split = false;
1075 stack.safe_push (first);
1076 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = (void *)(intptr_t)-1;
1078 while (!stack.is_empty ())
1080 stack_entry *entry = &stack.last ();
1082 /* We are walking an acyclic graph, so edge_num counts
1083 succ and pred edges together. However when considering
1084 articulation, we want to have processed everything reachable
1085 from articulation but nothing that reaches into it. */
1086 if (entry->edge_num == EDGE_COUNT (entry->bb->succs)
1087 && entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1089 int pos = stack.length ();
1090 entry->can_split &= visit_bb (entry->bb, return_bb,
1091 entry->set_ssa_names,
1092 entry->used_ssa_names,
1093 entry->non_ssa_vars);
1094 if (pos <= entry->earliest && !entry->can_split
1095 && dump_file && (dump_flags & TDF_DETAILS))
1096 fprintf (dump_file,
1097 "found articulation at bb %i but cannot split\n",
1098 entry->bb->index);
1099 if (pos <= entry->earliest && entry->can_split)
1101 if (dump_file && (dump_flags & TDF_DETAILS))
1102 fprintf (dump_file, "found articulation at bb %i\n",
1103 entry->bb->index);
1104 current.entry_bb = entry->bb;
1105 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
1106 bitmap_and_compl (current.ssa_names_to_pass,
1107 entry->used_ssa_names, entry->set_ssa_names);
1108 current.header_time = overall_time - entry->overall_time;
1109 current.header_size = overall_size - entry->overall_size;
1110 current.split_time = entry->overall_time;
1111 current.split_size = entry->overall_size;
1112 current.split_bbs = entry->bbs_visited;
1113 consider_split (&current, entry->non_ssa_vars, return_bb);
1114 BITMAP_FREE (current.ssa_names_to_pass);
1117 /* Do actual DFS walk. */
1118 if (entry->edge_num
1119 < (EDGE_COUNT (entry->bb->succs)
1120 + EDGE_COUNT (entry->bb->preds)))
1122 edge e;
1123 basic_block dest;
1124 if (entry->edge_num < EDGE_COUNT (entry->bb->succs))
1126 e = EDGE_SUCC (entry->bb, entry->edge_num);
1127 dest = e->dest;
1129 else
1131 e = EDGE_PRED (entry->bb, entry->edge_num
1132 - EDGE_COUNT (entry->bb->succs));
1133 dest = e->src;
1136 entry->edge_num++;
1138 /* New BB to visit, push it to the stack. */
1139 if (dest != return_bb && dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
1140 && !dest->aux)
1142 stack_entry new_entry;
1144 new_entry.bb = dest;
1145 new_entry.edge_num = 0;
1146 new_entry.overall_time
1147 = bb_info_vec[dest->index].time;
1148 new_entry.overall_size
1149 = bb_info_vec[dest->index].size;
1150 new_entry.earliest = INT_MAX;
1151 new_entry.set_ssa_names = BITMAP_ALLOC (NULL);
1152 new_entry.used_ssa_names = BITMAP_ALLOC (NULL);
1153 new_entry.bbs_visited = BITMAP_ALLOC (NULL);
1154 new_entry.non_ssa_vars = BITMAP_ALLOC (NULL);
1155 new_entry.can_split = true;
1156 bitmap_set_bit (new_entry.bbs_visited, dest->index);
1157 stack.safe_push (new_entry);
1158 dest->aux = (void *)(intptr_t)stack.length ();
1160 /* Back edge found, record the earliest point. */
1161 else if ((intptr_t)dest->aux > 0
1162 && (intptr_t)dest->aux < entry->earliest)
1163 entry->earliest = (intptr_t)dest->aux;
1165 /* We are done with examining the edges. Pop off the value from stack
1166 and merge stuff we accumulate during the walk. */
1167 else if (entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1169 stack_entry *prev = &stack[stack.length () - 2];
1171 entry->bb->aux = (void *)(intptr_t)-1;
1172 prev->can_split &= entry->can_split;
1173 if (prev->set_ssa_names)
1175 bitmap_ior_into (prev->set_ssa_names, entry->set_ssa_names);
1176 bitmap_ior_into (prev->used_ssa_names, entry->used_ssa_names);
1177 bitmap_ior_into (prev->bbs_visited, entry->bbs_visited);
1178 bitmap_ior_into (prev->non_ssa_vars, entry->non_ssa_vars);
1180 if (prev->earliest > entry->earliest)
1181 prev->earliest = entry->earliest;
1182 prev->overall_time += entry->overall_time;
1183 prev->overall_size += entry->overall_size;
1184 BITMAP_FREE (entry->set_ssa_names);
1185 BITMAP_FREE (entry->used_ssa_names);
1186 BITMAP_FREE (entry->bbs_visited);
1187 BITMAP_FREE (entry->non_ssa_vars);
1188 stack.pop ();
1190 else
1191 stack.pop ();
1193 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = NULL;
1194 FOR_EACH_BB_FN (bb, cfun)
1195 bb->aux = NULL;
1196 stack.release ();
1197 BITMAP_FREE (current.ssa_names_to_pass);
1200 /* Split function at SPLIT_POINT. */
1202 static void
1203 split_function (basic_block return_bb, class split_point *split_point,
1204 bool add_tsan_func_exit)
1206 vec<tree> args_to_pass = vNULL;
1207 bitmap args_to_skip;
1208 tree parm;
1209 int num = 0;
1210 cgraph_node *node, *cur_node = cgraph_node::get (current_function_decl);
1211 basic_block call_bb;
1212 gcall *call, *tsan_func_exit_call = NULL;
1213 edge e;
1214 edge_iterator ei;
1215 tree retval = NULL, real_retval = NULL;
1216 gimple *last_stmt = NULL;
1217 unsigned int i;
1218 tree arg, ddef;
1220 if (dump_file)
1222 fprintf (dump_file, "\n\nSplitting function at:\n");
1223 dump_split_point (dump_file, split_point);
1226 if (cur_node->can_change_signature)
1227 args_to_skip = BITMAP_ALLOC (NULL);
1228 else
1229 args_to_skip = NULL;
1231 /* Collect the parameters of new function and args_to_skip bitmap. */
1232 for (parm = DECL_ARGUMENTS (current_function_decl);
1233 parm; parm = DECL_CHAIN (parm), num++)
1234 if (args_to_skip
1235 && (!is_gimple_reg (parm)
1236 || (ddef = ssa_default_def (cfun, parm)) == NULL_TREE
1237 || !bitmap_bit_p (split_point->ssa_names_to_pass,
1238 SSA_NAME_VERSION (ddef))))
1239 bitmap_set_bit (args_to_skip, num);
1240 else
1242 /* This parm might not have been used up to now, but is going to be
1243 used, hence register it. */
1244 if (is_gimple_reg (parm))
1245 arg = get_or_create_ssa_default_def (cfun, parm);
1246 else
1247 arg = parm;
1249 if (!useless_type_conversion_p (DECL_ARG_TYPE (parm), TREE_TYPE (arg)))
1250 arg = fold_convert (DECL_ARG_TYPE (parm), arg);
1251 args_to_pass.safe_push (arg);
1254 /* See if the split function will return. */
1255 bool split_part_return_p = false;
1256 FOR_EACH_EDGE (e, ei, return_bb->preds)
1258 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1259 split_part_return_p = true;
1262 /* Add return block to what will become the split function.
1263 We do not return; no return block is needed. */
1264 if (!split_part_return_p)
1266 /* We have no return block, so nothing is needed. */
1267 else if (return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1269 /* When we do not want to return value, we need to construct
1270 new return block with empty return statement.
1271 FIXME: Once we are able to change return type, we should change function
1272 to return void instead of just outputting function with undefined return
1273 value. For structures this affects quality of codegen. */
1274 else if ((retval = find_retval (return_bb))
1275 && !split_point->split_part_set_retval)
1277 bool redirected = true;
1278 basic_block new_return_bb = create_basic_block (NULL, 0, return_bb);
1279 gimple_stmt_iterator gsi = gsi_start_bb (new_return_bb);
1280 gsi_insert_after (&gsi, gimple_build_return (NULL), GSI_NEW_STMT);
1281 new_return_bb->count = profile_count::zero ();
1282 while (redirected)
1284 redirected = false;
1285 FOR_EACH_EDGE (e, ei, return_bb->preds)
1286 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1288 new_return_bb->count += e->count ();
1289 redirect_edge_and_branch (e, new_return_bb);
1290 redirected = true;
1291 break;
1294 e = make_single_succ_edge (new_return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
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 ipa_param_adjustments *adjustments;
1349 bool skip_return = (!split_part_return_p
1350 || !split_point->split_part_set_retval);
1351 /* TODO: Perhaps get rid of args_to_skip entirely, after we make sure the
1352 debug info generation and discrepancy avoiding works well too. */
1353 if ((args_to_skip && !bitmap_empty_p (args_to_skip))
1354 || skip_return)
1356 vec<ipa_adjusted_param, va_gc> *new_params = NULL;
1357 unsigned j;
1358 for (parm = DECL_ARGUMENTS (current_function_decl), j = 0;
1359 parm; parm = DECL_CHAIN (parm), j++)
1360 if (!args_to_skip || !bitmap_bit_p (args_to_skip, j))
1362 ipa_adjusted_param adj;
1363 memset (&adj, 0, sizeof (adj));
1364 adj.op = IPA_PARAM_OP_COPY;
1365 adj.base_index = j;
1366 adj.prev_clone_index = j;
1367 vec_safe_push (new_params, adj);
1369 adjustments = new ipa_param_adjustments (new_params, j, skip_return);
1371 else
1372 adjustments = NULL;
1374 /* Now create the actual clone. */
1375 cgraph_edge::rebuild_edges ();
1376 node = cur_node->create_version_clone_with_body
1377 (vNULL, NULL, adjustments,
1378 split_point->split_bbs, split_point->entry_bb, "part");
1379 delete adjustments;
1380 node->split_part = true;
1382 if (cur_node->same_comdat_group)
1384 /* TODO: call is versionable if we make sure that all
1385 callers are inside of a comdat group. */
1386 cur_node->calls_comdat_local = true;
1387 node->add_to_same_comdat_group (cur_node);
1391 /* Let's take a time profile for splitted function. */
1392 if (cur_node->tp_first_run)
1393 node->tp_first_run = cur_node->tp_first_run + 1;
1395 /* For usual cloning it is enough to clear builtin only when signature
1396 changes. For partial inlining we however cannot expect the part
1397 of builtin implementation to have same semantic as the whole. */
1398 if (fndecl_built_in_p (node->decl))
1399 set_decl_built_in_function (node->decl, NOT_BUILT_IN, 0);
1401 /* If return_bb contains any clobbers that refer to SSA_NAMEs
1402 set in the split part, remove them. Also reset debug stmts that
1403 refer to SSA_NAMEs set in the split part. */
1404 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1406 gimple_stmt_iterator gsi = gsi_start_bb (return_bb);
1407 while (!gsi_end_p (gsi))
1409 tree op;
1410 ssa_op_iter iter;
1411 gimple *stmt = gsi_stmt (gsi);
1412 bool remove = false;
1413 if (gimple_clobber_p (stmt) || is_gimple_debug (stmt))
1414 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
1416 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (op));
1417 if (op != retval
1418 && bb
1419 && bb != return_bb
1420 && bitmap_bit_p (split_point->split_bbs, bb->index))
1422 if (is_gimple_debug (stmt))
1424 gimple_debug_bind_reset_value (stmt);
1425 update_stmt (stmt);
1427 else
1428 remove = true;
1429 break;
1432 if (remove)
1433 gsi_remove (&gsi, true);
1434 else
1435 gsi_next (&gsi);
1439 /* If the original function is declared inline, there is no point in issuing
1440 a warning for the non-inlinable part. */
1441 DECL_NO_INLINE_WARNING_P (node->decl) = 1;
1442 cur_node->remove_callees ();
1443 cur_node->remove_all_references ();
1444 if (!split_part_return_p)
1445 TREE_THIS_VOLATILE (node->decl) = 1;
1446 if (dump_file)
1447 dump_function_to_file (node->decl, dump_file, dump_flags);
1449 /* Create the basic block we place call into. It is the entry basic block
1450 split after last label. */
1451 call_bb = split_point->entry_bb;
1452 for (gimple_stmt_iterator gsi = gsi_start_bb (call_bb); !gsi_end_p (gsi);)
1453 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
1455 last_stmt = gsi_stmt (gsi);
1456 gsi_next (&gsi);
1458 else
1459 break;
1460 call_bb->count = split_point->count;
1461 e = split_block (split_point->entry_bb, last_stmt);
1462 remove_edge (e);
1464 /* Produce the call statement. */
1465 gimple_stmt_iterator gsi = gsi_last_bb (call_bb);
1466 FOR_EACH_VEC_ELT (args_to_pass, i, arg)
1467 if (!is_gimple_val (arg))
1469 arg = force_gimple_operand_gsi (&gsi, arg, true, NULL_TREE,
1470 false, GSI_CONTINUE_LINKING);
1471 args_to_pass[i] = arg;
1473 call = gimple_build_call_vec (node->decl, args_to_pass);
1474 gimple_set_block (call, DECL_INITIAL (current_function_decl));
1475 args_to_pass.release ();
1477 /* For optimized away parameters, add on the caller side
1478 before the call
1479 DEBUG D#X => parm_Y(D)
1480 stmts and associate D#X with parm in decl_debug_args_lookup
1481 vector to say for debug info that if parameter parm had been passed,
1482 it would have value parm_Y(D). */
1483 if (args_to_skip)
1485 vec<tree, va_gc> **debug_args = NULL;
1486 unsigned i = 0, len = 0;
1487 if (MAY_HAVE_DEBUG_BIND_STMTS)
1489 debug_args = decl_debug_args_lookup (node->decl);
1490 if (debug_args)
1491 len = vec_safe_length (*debug_args);
1493 for (parm = DECL_ARGUMENTS (current_function_decl), num = 0;
1494 parm; parm = DECL_CHAIN (parm), num++)
1495 if (bitmap_bit_p (args_to_skip, num) && is_gimple_reg (parm))
1497 tree ddecl;
1498 gimple *def_temp;
1500 /* This needs to be done even without
1501 MAY_HAVE_DEBUG_BIND_STMTS, otherwise if it didn't exist
1502 before, we'd end up with different SSA_NAME_VERSIONs
1503 between -g and -g0. */
1504 arg = get_or_create_ssa_default_def (cfun, parm);
1505 if (!MAY_HAVE_DEBUG_BIND_STMTS || debug_args == NULL)
1506 continue;
1508 while (i < len && (**debug_args)[i] != DECL_ORIGIN (parm))
1509 i += 2;
1510 if (i >= len)
1511 continue;
1512 ddecl = (**debug_args)[i + 1];
1513 def_temp
1514 = gimple_build_debug_bind (ddecl, unshare_expr (arg), call);
1515 gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
1517 BITMAP_FREE (args_to_skip);
1520 /* We avoid address being taken on any variable used by split part,
1521 so return slot optimization is always possible. Moreover this is
1522 required to make DECL_BY_REFERENCE work. */
1523 if (aggregate_value_p (DECL_RESULT (current_function_decl),
1524 TREE_TYPE (current_function_decl))
1525 && (!is_gimple_reg_type (TREE_TYPE (DECL_RESULT (current_function_decl)))
1526 || DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))))
1527 gimple_call_set_return_slot_opt (call, true);
1529 if (add_tsan_func_exit)
1530 tsan_func_exit_call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
1532 /* Update return value. This is bit tricky. When we do not return,
1533 do nothing. When we return we might need to update return_bb
1534 or produce a new return statement. */
1535 if (!split_part_return_p)
1537 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1538 if (tsan_func_exit_call)
1539 gsi_insert_after (&gsi, tsan_func_exit_call, GSI_NEW_STMT);
1541 else
1543 e = make_single_succ_edge (call_bb, return_bb,
1544 return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
1545 ? 0 : EDGE_FALLTHRU);
1547 /* If there is return basic block, see what value we need to store
1548 return value into and put call just before it. */
1549 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1551 real_retval = retval;
1552 if (real_retval && split_point->split_part_set_retval)
1554 gphi_iterator psi;
1556 /* See if we need new SSA_NAME for the result.
1557 When DECL_BY_REFERENCE is true, retval is actually pointer to
1558 return value and it is constant in whole function. */
1559 if (TREE_CODE (retval) == SSA_NAME
1560 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1562 retval = copy_ssa_name (retval, call);
1564 /* See if there is PHI defining return value. */
1565 for (psi = gsi_start_phis (return_bb);
1566 !gsi_end_p (psi); gsi_next (&psi))
1567 if (!virtual_operand_p (gimple_phi_result (psi.phi ())))
1568 break;
1570 /* When there is PHI, just update its value. */
1571 if (TREE_CODE (retval) == SSA_NAME
1572 && !gsi_end_p (psi))
1573 add_phi_arg (psi.phi (), retval, e, UNKNOWN_LOCATION);
1574 /* Otherwise update the return BB itself.
1575 find_return_bb allows at most one assignment to return value,
1576 so update first statement. */
1577 else
1579 gimple_stmt_iterator bsi;
1580 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi);
1581 gsi_next (&bsi))
1582 if (greturn *return_stmt
1583 = dyn_cast <greturn *> (gsi_stmt (bsi)))
1585 gimple_return_set_retval (return_stmt, retval);
1586 break;
1588 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
1589 && !gimple_clobber_p (gsi_stmt (bsi)))
1591 gimple_assign_set_rhs1 (gsi_stmt (bsi), retval);
1592 break;
1594 update_stmt (gsi_stmt (bsi));
1595 /* Also adjust clobbers and debug stmts in return_bb. */
1596 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi);
1597 gsi_next (&bsi))
1599 gimple *stmt = gsi_stmt (bsi);
1600 if (gimple_clobber_p (stmt)
1601 || is_gimple_debug (stmt))
1603 ssa_op_iter iter;
1604 use_operand_p use_p;
1605 bool update = false;
1606 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter,
1607 SSA_OP_USE)
1608 if (USE_FROM_PTR (use_p) == real_retval)
1610 SET_USE (use_p, retval);
1611 update = true;
1613 if (update)
1614 update_stmt (stmt);
1619 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1621 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1622 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1624 else
1626 tree restype;
1627 restype = TREE_TYPE (DECL_RESULT (current_function_decl));
1628 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1629 if (!useless_type_conversion_p (TREE_TYPE (retval), restype))
1631 gimple *cpy;
1632 tree tem = create_tmp_reg (restype);
1633 tem = make_ssa_name (tem, call);
1634 cpy = gimple_build_assign (retval, NOP_EXPR, tem);
1635 gsi_insert_after (&gsi, cpy, GSI_NEW_STMT);
1636 retval = tem;
1638 gimple_call_set_lhs (call, retval);
1639 update_stmt (call);
1642 else
1643 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1644 if (tsan_func_exit_call)
1645 gsi_insert_after (&gsi, tsan_func_exit_call, GSI_NEW_STMT);
1647 /* We don't use return block (there is either no return in function or
1648 multiple of them). So create new basic block with return statement.
1650 else
1652 greturn *ret;
1653 if (split_point->split_part_set_retval
1654 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1656 retval = DECL_RESULT (current_function_decl);
1658 /* We use temporary register to hold value when aggregate_value_p
1659 is false. Similarly for DECL_BY_REFERENCE we must avoid extra
1660 copy. */
1661 if (!aggregate_value_p (retval, TREE_TYPE (current_function_decl))
1662 && !DECL_BY_REFERENCE (retval))
1663 retval = create_tmp_reg (TREE_TYPE (retval));
1664 if (is_gimple_reg (retval))
1666 /* When returning by reference, there is only one SSA name
1667 assigned to RESULT_DECL (that is pointer to return value).
1668 Look it up or create new one if it is missing. */
1669 if (DECL_BY_REFERENCE (retval))
1670 retval = get_or_create_ssa_default_def (cfun, retval);
1671 /* Otherwise produce new SSA name for return value. */
1672 else
1673 retval = make_ssa_name (retval, call);
1675 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1676 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1677 else
1678 gimple_call_set_lhs (call, retval);
1679 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1681 else
1683 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1684 if (retval
1685 && is_gimple_reg_type (TREE_TYPE (retval))
1686 && !is_gimple_val (retval))
1688 gassign *g
1689 = gimple_build_assign (make_ssa_name (TREE_TYPE (retval)),
1690 retval);
1691 retval = gimple_assign_lhs (g);
1692 gsi_insert_after (&gsi, g, GSI_NEW_STMT);
1695 if (tsan_func_exit_call)
1696 gsi_insert_after (&gsi, tsan_func_exit_call, GSI_NEW_STMT);
1697 ret = gimple_build_return (retval);
1698 gsi_insert_after (&gsi, ret, GSI_NEW_STMT);
1701 free_dominance_info (CDI_DOMINATORS);
1702 free_dominance_info (CDI_POST_DOMINATORS);
1703 compute_fn_summary (node, true);
1706 /* Execute function splitting pass. */
1708 static unsigned int
1709 execute_split_functions (void)
1711 gimple_stmt_iterator bsi;
1712 basic_block bb;
1713 sreal overall_time = 0;
1714 int overall_size = 0;
1715 int todo = 0;
1716 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1718 if (flags_from_decl_or_type (current_function_decl)
1719 & (ECF_NORETURN|ECF_MALLOC|ECF_RETURNS_TWICE))
1721 if (dump_file)
1722 fprintf (dump_file, "Not splitting: noreturn/malloc/returns_twice "
1723 "function.\n");
1724 return 0;
1726 if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
1728 if (dump_file)
1729 fprintf (dump_file, "Not splitting: main function.\n");
1730 return 0;
1732 if (node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
1734 if (dump_file)
1735 fprintf (dump_file, "Not splitting: function is unlikely executed.\n");
1736 return 0;
1738 /* This can be relaxed; function might become inlinable after splitting
1739 away the uninlinable part. */
1740 if (ipa_fn_summaries
1741 && ipa_fn_summaries->get (node)
1742 && !ipa_fn_summaries->get (node)->inlinable)
1744 if (dump_file)
1745 fprintf (dump_file, "Not splitting: not inlinable.\n");
1746 return 0;
1748 if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1750 if (dump_file)
1751 fprintf (dump_file, "Not splitting: disregarding inline limits.\n");
1752 return 0;
1754 /* This can be relaxed; most of versioning tests actually prevents
1755 a duplication. */
1756 if (!tree_versionable_function_p (current_function_decl))
1758 if (dump_file)
1759 fprintf (dump_file, "Not splitting: not versionable.\n");
1760 return 0;
1762 /* FIXME: we could support this. */
1763 if (DECL_STRUCT_FUNCTION (current_function_decl)->static_chain_decl)
1765 if (dump_file)
1766 fprintf (dump_file, "Not splitting: nested function.\n");
1767 return 0;
1770 /* See if it makes sense to try to split.
1771 It makes sense to split if we inline, that is if we have direct calls to
1772 handle or direct calls are possibly going to appear as result of indirect
1773 inlining or LTO. Also handle -fprofile-generate as LTO to allow non-LTO
1774 training for LTO -fprofile-use build.
1776 Note that we are not completely conservative about disqualifying functions
1777 called once. It is possible that the caller is called more then once and
1778 then inlining would still benefit. */
1779 if ((!node->callers
1780 /* Local functions called once will be completely inlined most of time. */
1781 || (!node->callers->next_caller && node->local))
1782 && !node->address_taken
1783 && !node->has_aliases_p ()
1784 && (!flag_lto || !node->externally_visible))
1786 if (dump_file)
1787 fprintf (dump_file, "Not splitting: not called directly "
1788 "or called once.\n");
1789 return 0;
1792 /* FIXME: We can actually split if splitting reduces call overhead. */
1793 if (!flag_inline_small_functions
1794 && !DECL_DECLARED_INLINE_P (current_function_decl))
1796 if (dump_file)
1797 fprintf (dump_file, "Not splitting: not autoinlining and function"
1798 " is not inline.\n");
1799 return 0;
1802 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (current_function_decl)))
1804 if (dump_file)
1805 fprintf (dump_file, "Not splitting: function is noinline.\n");
1806 return 0;
1808 if (lookup_attribute ("section", DECL_ATTRIBUTES (current_function_decl)))
1810 if (dump_file)
1811 fprintf (dump_file, "Not splitting: function is in user defined "
1812 "section.\n");
1813 return 0;
1815 if (!strub_splittable_p (node))
1817 if (dump_file)
1818 fprintf (dump_file, "Not splitting: function is a strub context.\n");
1819 return 0;
1822 /* We enforce splitting after loop headers when profile info is not
1823 available. */
1824 if (profile_status_for_fn (cfun) != PROFILE_READ)
1825 mark_dfs_back_edges ();
1827 /* Initialize bitmap to track forbidden calls. */
1828 forbidden_dominators = BITMAP_ALLOC (NULL);
1829 calculate_dominance_info (CDI_DOMINATORS);
1831 /* Compute local info about basic blocks and determine function size/time. */
1832 bb_info_vec.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1, true);
1833 best_split_point.split_bbs = NULL;
1834 basic_block return_bb = find_return_bb ();
1835 int tsan_exit_found = -1;
1836 FOR_EACH_BB_FN (bb, cfun)
1838 sreal time = 0;
1839 int size = 0;
1840 sreal freq = bb->count.to_sreal_scale
1841 (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count);
1843 if (dump_file && (dump_flags & TDF_DETAILS))
1844 fprintf (dump_file, "Basic block %i\n", bb->index);
1846 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1848 sreal this_time;
1849 int this_size;
1850 gimple *stmt = gsi_stmt (bsi);
1852 this_size = estimate_num_insns (stmt, &eni_size_weights);
1853 this_time = (sreal)estimate_num_insns (stmt, &eni_time_weights)
1854 * freq;
1855 size += this_size;
1856 time += this_time;
1857 check_forbidden_calls (stmt);
1859 if (dump_file && (dump_flags & TDF_DETAILS))
1861 fprintf (dump_file, " freq:%4.2f size:%3i time:%4.2f ",
1862 freq.to_double (), this_size, this_time.to_double ());
1863 print_gimple_stmt (dump_file, stmt, 0);
1866 if ((flag_sanitize & SANITIZE_THREAD)
1867 && gimple_call_internal_p (stmt, IFN_TSAN_FUNC_EXIT))
1869 /* We handle TSAN_FUNC_EXIT for splitting either in the
1870 return_bb, or in its immediate predecessors. */
1871 if ((bb != return_bb && !find_edge (bb, return_bb))
1872 || (tsan_exit_found != -1
1873 && tsan_exit_found != (bb != return_bb)))
1875 if (dump_file)
1876 fprintf (dump_file, "Not splitting: TSAN_FUNC_EXIT"
1877 " in unexpected basic block.\n");
1878 BITMAP_FREE (forbidden_dominators);
1879 bb_info_vec.release ();
1880 return 0;
1882 tsan_exit_found = bb != return_bb;
1885 overall_time += time;
1886 overall_size += size;
1887 bb_info_vec[bb->index].time = time;
1888 bb_info_vec[bb->index].size = size;
1890 find_split_points (return_bb, overall_time, overall_size);
1891 if (best_split_point.split_bbs)
1893 split_function (return_bb, &best_split_point, tsan_exit_found == 1);
1894 BITMAP_FREE (best_split_point.ssa_names_to_pass);
1895 BITMAP_FREE (best_split_point.split_bbs);
1896 todo = TODO_update_ssa | TODO_cleanup_cfg;
1898 BITMAP_FREE (forbidden_dominators);
1899 bb_info_vec.release ();
1900 return todo;
1903 namespace {
1905 const pass_data pass_data_split_functions =
1907 GIMPLE_PASS, /* type */
1908 "fnsplit", /* name */
1909 OPTGROUP_NONE, /* optinfo_flags */
1910 TV_IPA_FNSPLIT, /* tv_id */
1911 PROP_cfg, /* properties_required */
1912 0, /* properties_provided */
1913 0, /* properties_destroyed */
1914 0, /* todo_flags_start */
1915 0, /* todo_flags_finish */
1918 class pass_split_functions : public gimple_opt_pass
1920 public:
1921 pass_split_functions (gcc::context *ctxt)
1922 : gimple_opt_pass (pass_data_split_functions, ctxt)
1925 /* opt_pass methods: */
1926 bool gate (function *) final override;
1927 unsigned int execute (function *) final override
1929 return execute_split_functions ();
1932 }; // class pass_split_functions
1934 bool
1935 pass_split_functions::gate (function *)
1937 /* When doing profile feedback, we want to execute the pass after profiling
1938 is read. So disable one in early optimization. */
1939 return (flag_partial_inlining
1940 && !profile_arc_flag && !flag_branch_probabilities);
1943 } // anon namespace
1945 gimple_opt_pass *
1946 make_pass_split_functions (gcc::context *ctxt)
1948 return new pass_split_functions (ctxt);
1951 /* Execute function splitting pass. */
1953 static unsigned int
1954 execute_feedback_split_functions (void)
1956 unsigned int retval = execute_split_functions ();
1957 if (retval)
1958 retval |= TODO_rebuild_cgraph_edges;
1959 return retval;
1962 namespace {
1964 const pass_data pass_data_feedback_split_functions =
1966 GIMPLE_PASS, /* type */
1967 "feedback_fnsplit", /* name */
1968 OPTGROUP_NONE, /* optinfo_flags */
1969 TV_IPA_FNSPLIT, /* tv_id */
1970 PROP_cfg, /* properties_required */
1971 0, /* properties_provided */
1972 0, /* properties_destroyed */
1973 0, /* todo_flags_start */
1974 0, /* todo_flags_finish */
1977 class pass_feedback_split_functions : public gimple_opt_pass
1979 public:
1980 pass_feedback_split_functions (gcc::context *ctxt)
1981 : gimple_opt_pass (pass_data_feedback_split_functions, ctxt)
1984 /* opt_pass methods: */
1985 bool gate (function *) final override;
1986 unsigned int execute (function *) final override
1988 return execute_feedback_split_functions ();
1991 }; // class pass_feedback_split_functions
1993 bool
1994 pass_feedback_split_functions::gate (function *)
1996 /* We don't need to split when profiling at all, we are producing
1997 lousy code anyway. */
1998 return (flag_partial_inlining
1999 && flag_branch_probabilities);
2002 } // anon namespace
2004 gimple_opt_pass *
2005 make_pass_feedback_split_functions (gcc::context *ctxt)
2007 return new pass_feedback_split_functions (ctxt);