missing Changelog
[official-gcc.git] / gcc / ipa-split.c
blobf6b469a776deed2b2364d25ee156d0129bad06ff
1 /* Function splitting pass
2 Copyright (C) 2010, 2011, 2012
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka <jh@suse.cz>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* The purpose of this pass is to split function bodies to improve
23 inlining. I.e. for function of the form:
25 func (...)
27 if (cheap_test)
28 something_small
29 else
30 something_big
33 Produce:
35 func.part (...)
37 something_big
40 func (...)
42 if (cheap_test)
43 something_small
44 else
45 func.part (...);
48 When func becomes inlinable and when cheap_test is often true, inlining func,
49 but not fund.part leads to performance improvement similar as inlining
50 original func while the code size growth is smaller.
52 The pass is organized in three stages:
53 1) Collect local info about basic block into BB_INFO structure and
54 compute function body estimated size and time.
55 2) Via DFS walk find all possible basic blocks where we can split
56 and chose best one.
57 3) If split point is found, split at the specified BB by creating a clone
58 and updating function to call it.
60 The decisions what functions to split are in execute_split_functions
61 and consider_split.
63 There are several possible future improvements for this pass including:
65 1) Splitting to break up large functions
66 2) Splitting to reduce stack frame usage
67 3) Allow split part of function to use values computed in the header part.
68 The values needs to be passed to split function, perhaps via same
69 interface as for nested functions or as argument.
70 4) Support for simple rematerialization. I.e. when split part use
71 value computed in header from function parameter in very cheap way, we
72 can just recompute it.
73 5) Support splitting of nested functions.
74 6) Support non-SSA arguments.
75 7) There is nothing preventing us from producing multiple parts of single function
76 when needed or splitting also the parts. */
78 #include "config.h"
79 #include "system.h"
80 #include "coretypes.h"
81 #include "tree.h"
82 #include "target.h"
83 #include "cgraph.h"
84 #include "ipa-prop.h"
85 #include "tree-flow.h"
86 #include "tree-pass.h"
87 #include "flags.h"
88 #include "diagnostic.h"
89 #include "tree-dump.h"
90 #include "tree-inline.h"
91 #include "params.h"
92 #include "gimple-pretty-print.h"
93 #include "ipa-inline.h"
95 /* Per basic block info. */
97 typedef struct
99 unsigned int size;
100 unsigned int time;
101 } bb_info;
103 static vec<bb_info> bb_info_vec;
105 /* Description of split point. */
107 struct split_point
109 /* Size of the partitions. */
110 unsigned int header_time, header_size, split_time, split_size;
112 /* SSA names that need to be passed into spit function. */
113 bitmap ssa_names_to_pass;
115 /* Basic block where we split (that will become entry point of new function. */
116 basic_block entry_bb;
118 /* Basic blocks we are splitting away. */
119 bitmap split_bbs;
121 /* True when return value is computed on split part and thus it needs
122 to be returned. */
123 bool split_part_set_retval;
126 /* Best split point found. */
128 struct split_point best_split_point;
130 /* Set of basic blocks that are not allowed to dominate a split point. */
132 static bitmap forbidden_dominators;
134 static tree find_retval (basic_block return_bb);
136 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
137 variable, check it if it is present in bitmap passed via DATA. */
139 static bool
140 test_nonssa_use (gimple stmt ATTRIBUTE_UNUSED, tree t, void *data)
142 t = get_base_address (t);
144 if (!t || is_gimple_reg (t))
145 return false;
147 if (TREE_CODE (t) == PARM_DECL
148 || (TREE_CODE (t) == VAR_DECL
149 && auto_var_in_fn_p (t, current_function_decl))
150 || TREE_CODE (t) == RESULT_DECL
151 || TREE_CODE (t) == LABEL_DECL)
152 return bitmap_bit_p ((bitmap)data, DECL_UID (t));
154 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
155 to pretend that the value pointed to is actual result decl. */
156 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
157 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
158 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
159 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
160 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
161 return
162 bitmap_bit_p ((bitmap)data,
163 DECL_UID (DECL_RESULT (current_function_decl)));
165 return false;
168 /* Dump split point CURRENT. */
170 static void
171 dump_split_point (FILE * file, struct split_point *current)
173 fprintf (file,
174 "Split point at BB %i\n"
175 " header time: %i header size: %i\n"
176 " split time: %i split size: %i\n bbs: ",
177 current->entry_bb->index, current->header_time,
178 current->header_size, current->split_time, current->split_size);
179 dump_bitmap (file, current->split_bbs);
180 fprintf (file, " SSA names to pass: ");
181 dump_bitmap (file, current->ssa_names_to_pass);
184 /* Look for all BBs in header that might lead to the split part and verify
185 that they are not defining any non-SSA var used by the split part.
186 Parameters are the same as for consider_split. */
188 static bool
189 verify_non_ssa_vars (struct split_point *current, bitmap non_ssa_vars,
190 basic_block return_bb)
192 bitmap seen = BITMAP_ALLOC (NULL);
193 vec<basic_block> worklist = vNULL;
194 edge e;
195 edge_iterator ei;
196 bool ok = true;
198 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
199 if (e->src != ENTRY_BLOCK_PTR
200 && !bitmap_bit_p (current->split_bbs, e->src->index))
202 worklist.safe_push (e->src);
203 bitmap_set_bit (seen, e->src->index);
206 while (!worklist.is_empty ())
208 gimple_stmt_iterator bsi;
209 basic_block bb = worklist.pop ();
211 FOR_EACH_EDGE (e, ei, bb->preds)
212 if (e->src != ENTRY_BLOCK_PTR
213 && bitmap_set_bit (seen, e->src->index))
215 gcc_checking_assert (!bitmap_bit_p (current->split_bbs,
216 e->src->index));
217 worklist.safe_push (e->src);
219 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
221 gimple stmt = gsi_stmt (bsi);
222 if (is_gimple_debug (stmt))
223 continue;
224 if (walk_stmt_load_store_addr_ops
225 (stmt, non_ssa_vars, test_nonssa_use, test_nonssa_use,
226 test_nonssa_use))
228 ok = false;
229 goto done;
231 if (gimple_code (stmt) == GIMPLE_LABEL
232 && test_nonssa_use (stmt, gimple_label_label (stmt),
233 non_ssa_vars))
235 ok = false;
236 goto done;
239 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
241 if (walk_stmt_load_store_addr_ops
242 (gsi_stmt (bsi), non_ssa_vars, test_nonssa_use, test_nonssa_use,
243 test_nonssa_use))
245 ok = false;
246 goto done;
249 FOR_EACH_EDGE (e, ei, bb->succs)
251 if (e->dest != return_bb)
252 continue;
253 for (bsi = gsi_start_phis (return_bb); !gsi_end_p (bsi);
254 gsi_next (&bsi))
256 gimple stmt = gsi_stmt (bsi);
257 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
259 if (virtual_operand_p (gimple_phi_result (stmt)))
260 continue;
261 if (TREE_CODE (op) != SSA_NAME
262 && test_nonssa_use (stmt, op, non_ssa_vars))
264 ok = false;
265 goto done;
270 done:
271 BITMAP_FREE (seen);
272 worklist.release ();
273 return ok;
276 /* If STMT is a call, check the callee against a list of forbidden
277 predicate functions. If a match is found, look for uses of the
278 call result in condition statements that compare against zero.
279 For each such use, find the block targeted by the condition
280 statement for the nonzero result, and set the bit for this block
281 in the forbidden dominators bitmap. The purpose of this is to avoid
282 selecting a split point where we are likely to lose the chance
283 to optimize away an unused function call. */
285 static void
286 check_forbidden_calls (gimple stmt)
288 imm_use_iterator use_iter;
289 use_operand_p use_p;
290 tree lhs;
292 /* At the moment, __builtin_constant_p is the only forbidden
293 predicate function call (see PR49642). */
294 if (!gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P))
295 return;
297 lhs = gimple_call_lhs (stmt);
299 if (!lhs || TREE_CODE (lhs) != SSA_NAME)
300 return;
302 FOR_EACH_IMM_USE_FAST (use_p, use_iter, lhs)
304 tree op1;
305 basic_block use_bb, forbidden_bb;
306 enum tree_code code;
307 edge true_edge, false_edge;
308 gimple use_stmt = USE_STMT (use_p);
310 if (gimple_code (use_stmt) != GIMPLE_COND)
311 continue;
313 /* Assuming canonical form for GIMPLE_COND here, with constant
314 in second position. */
315 op1 = gimple_cond_rhs (use_stmt);
316 code = gimple_cond_code (use_stmt);
317 use_bb = gimple_bb (use_stmt);
319 extract_true_false_edges_from_block (use_bb, &true_edge, &false_edge);
321 /* We're only interested in comparisons that distinguish
322 unambiguously from zero. */
323 if (!integer_zerop (op1) || code == LE_EXPR || code == GE_EXPR)
324 continue;
326 if (code == EQ_EXPR)
327 forbidden_bb = false_edge->dest;
328 else
329 forbidden_bb = true_edge->dest;
331 bitmap_set_bit (forbidden_dominators, forbidden_bb->index);
335 /* If BB is dominated by any block in the forbidden dominators set,
336 return TRUE; else FALSE. */
338 static bool
339 dominated_by_forbidden (basic_block bb)
341 unsigned dom_bb;
342 bitmap_iterator bi;
344 EXECUTE_IF_SET_IN_BITMAP (forbidden_dominators, 1, dom_bb, bi)
346 if (dominated_by_p (CDI_DOMINATORS, bb, BASIC_BLOCK (dom_bb)))
347 return true;
350 return false;
353 /* We found an split_point CURRENT. NON_SSA_VARS is bitmap of all non ssa
354 variables used and RETURN_BB is return basic block.
355 See if we can split function here. */
357 static void
358 consider_split (struct split_point *current, bitmap non_ssa_vars,
359 basic_block return_bb)
361 tree parm;
362 unsigned int num_args = 0;
363 unsigned int call_overhead;
364 edge e;
365 edge_iterator ei;
366 gimple_stmt_iterator bsi;
367 unsigned int i;
368 int incoming_freq = 0;
369 tree retval;
371 if (dump_file && (dump_flags & TDF_DETAILS))
372 dump_split_point (dump_file, current);
374 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
375 if (!bitmap_bit_p (current->split_bbs, e->src->index))
376 incoming_freq += EDGE_FREQUENCY (e);
378 /* Do not split when we would end up calling function anyway. */
379 if (incoming_freq
380 >= (ENTRY_BLOCK_PTR->frequency
381 * PARAM_VALUE (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY) / 100))
383 if (dump_file && (dump_flags & TDF_DETAILS))
384 fprintf (dump_file,
385 " Refused: incoming frequency is too large.\n");
386 return;
389 if (!current->header_size)
391 if (dump_file && (dump_flags & TDF_DETAILS))
392 fprintf (dump_file, " Refused: header empty\n");
393 return;
396 /* Verify that PHI args on entry are either virtual or all their operands
397 incoming from header are the same. */
398 for (bsi = gsi_start_phis (current->entry_bb); !gsi_end_p (bsi); gsi_next (&bsi))
400 gimple stmt = gsi_stmt (bsi);
401 tree val = NULL;
403 if (virtual_operand_p (gimple_phi_result (stmt)))
404 continue;
405 for (i = 0; i < gimple_phi_num_args (stmt); i++)
407 edge e = gimple_phi_arg_edge (stmt, i);
408 if (!bitmap_bit_p (current->split_bbs, e->src->index))
410 tree edge_val = gimple_phi_arg_def (stmt, i);
411 if (val && edge_val != val)
413 if (dump_file && (dump_flags & TDF_DETAILS))
414 fprintf (dump_file,
415 " Refused: entry BB has PHI with multiple variants\n");
416 return;
418 val = edge_val;
424 /* See what argument we will pass to the split function and compute
425 call overhead. */
426 call_overhead = eni_size_weights.call_cost;
427 for (parm = DECL_ARGUMENTS (current_function_decl); parm;
428 parm = DECL_CHAIN (parm))
430 if (!is_gimple_reg (parm))
432 if (bitmap_bit_p (non_ssa_vars, DECL_UID (parm)))
434 if (dump_file && (dump_flags & TDF_DETAILS))
435 fprintf (dump_file,
436 " Refused: need to pass non-ssa param values\n");
437 return;
440 else
442 tree ddef = ssa_default_def (cfun, parm);
443 if (ddef
444 && bitmap_bit_p (current->ssa_names_to_pass,
445 SSA_NAME_VERSION (ddef)))
447 if (!VOID_TYPE_P (TREE_TYPE (parm)))
448 call_overhead += estimate_move_cost (TREE_TYPE (parm));
449 num_args++;
453 if (!VOID_TYPE_P (TREE_TYPE (current_function_decl)))
454 call_overhead += estimate_move_cost (TREE_TYPE (current_function_decl));
456 if (current->split_size <= call_overhead)
458 if (dump_file && (dump_flags & TDF_DETAILS))
459 fprintf (dump_file,
460 " Refused: split size is smaller than call overhead\n");
461 return;
463 if (current->header_size + call_overhead
464 >= (unsigned int)(DECL_DECLARED_INLINE_P (current_function_decl)
465 ? MAX_INLINE_INSNS_SINGLE
466 : MAX_INLINE_INSNS_AUTO))
468 if (dump_file && (dump_flags & TDF_DETAILS))
469 fprintf (dump_file,
470 " Refused: header size is too large for inline candidate\n");
471 return;
474 /* FIXME: we currently can pass only SSA function parameters to the split
475 arguments. Once parm_adjustment infrastructure is supported by cloning,
476 we can pass more than that. */
477 if (num_args != bitmap_count_bits (current->ssa_names_to_pass))
480 if (dump_file && (dump_flags & TDF_DETAILS))
481 fprintf (dump_file,
482 " Refused: need to pass non-param values\n");
483 return;
486 /* When there are non-ssa vars used in the split region, see if they
487 are used in the header region. If so, reject the split.
488 FIXME: we can use nested function support to access both. */
489 if (!bitmap_empty_p (non_ssa_vars)
490 && !verify_non_ssa_vars (current, non_ssa_vars, return_bb))
492 if (dump_file && (dump_flags & TDF_DETAILS))
493 fprintf (dump_file,
494 " Refused: split part has non-ssa uses\n");
495 return;
498 /* If the split point is dominated by a forbidden block, reject
499 the split. */
500 if (!bitmap_empty_p (forbidden_dominators)
501 && dominated_by_forbidden (current->entry_bb))
503 if (dump_file && (dump_flags & TDF_DETAILS))
504 fprintf (dump_file,
505 " Refused: split point dominated by forbidden block\n");
506 return;
509 /* See if retval used by return bb is computed by header or split part.
510 When it is computed by split part, we need to produce return statement
511 in the split part and add code to header to pass it around.
513 This is bit tricky to test:
514 1) When there is no return_bb or no return value, we always pass
515 value around.
516 2) Invariants are always computed by caller.
517 3) For SSA we need to look if defining statement is in header or split part
518 4) For non-SSA we need to look where the var is computed. */
519 retval = find_retval (return_bb);
520 if (!retval)
521 current->split_part_set_retval = true;
522 else if (is_gimple_min_invariant (retval))
523 current->split_part_set_retval = false;
524 /* Special case is value returned by reference we record as if it was non-ssa
525 set to result_decl. */
526 else if (TREE_CODE (retval) == SSA_NAME
527 && SSA_NAME_VAR (retval)
528 && TREE_CODE (SSA_NAME_VAR (retval)) == RESULT_DECL
529 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
530 current->split_part_set_retval
531 = bitmap_bit_p (non_ssa_vars, DECL_UID (SSA_NAME_VAR (retval)));
532 else if (TREE_CODE (retval) == SSA_NAME)
533 current->split_part_set_retval
534 = (!SSA_NAME_IS_DEFAULT_DEF (retval)
535 && (bitmap_bit_p (current->split_bbs,
536 gimple_bb (SSA_NAME_DEF_STMT (retval))->index)
537 || gimple_bb (SSA_NAME_DEF_STMT (retval)) == return_bb));
538 else if (TREE_CODE (retval) == PARM_DECL)
539 current->split_part_set_retval = false;
540 else if (TREE_CODE (retval) == VAR_DECL
541 || TREE_CODE (retval) == RESULT_DECL)
542 current->split_part_set_retval
543 = bitmap_bit_p (non_ssa_vars, DECL_UID (retval));
544 else
545 current->split_part_set_retval = true;
547 /* split_function fixes up at most one PHI non-virtual PHI node in return_bb,
548 for the return value. If there are other PHIs, give up. */
549 if (return_bb != EXIT_BLOCK_PTR)
551 gimple_stmt_iterator psi;
553 for (psi = gsi_start_phis (return_bb); !gsi_end_p (psi); gsi_next (&psi))
554 if (!virtual_operand_p (gimple_phi_result (gsi_stmt (psi)))
555 && !(retval
556 && current->split_part_set_retval
557 && TREE_CODE (retval) == SSA_NAME
558 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
559 && SSA_NAME_DEF_STMT (retval) == gsi_stmt (psi)))
561 if (dump_file && (dump_flags & TDF_DETAILS))
562 fprintf (dump_file,
563 " Refused: return bb has extra PHIs\n");
564 return;
568 if (dump_file && (dump_flags & TDF_DETAILS))
569 fprintf (dump_file, " Accepted!\n");
571 /* At the moment chose split point with lowest frequency and that leaves
572 out smallest size of header.
573 In future we might re-consider this heuristics. */
574 if (!best_split_point.split_bbs
575 || best_split_point.entry_bb->frequency > current->entry_bb->frequency
576 || (best_split_point.entry_bb->frequency == current->entry_bb->frequency
577 && best_split_point.split_size < current->split_size))
580 if (dump_file && (dump_flags & TDF_DETAILS))
581 fprintf (dump_file, " New best split point!\n");
582 if (best_split_point.ssa_names_to_pass)
584 BITMAP_FREE (best_split_point.ssa_names_to_pass);
585 BITMAP_FREE (best_split_point.split_bbs);
587 best_split_point = *current;
588 best_split_point.ssa_names_to_pass = BITMAP_ALLOC (NULL);
589 bitmap_copy (best_split_point.ssa_names_to_pass,
590 current->ssa_names_to_pass);
591 best_split_point.split_bbs = BITMAP_ALLOC (NULL);
592 bitmap_copy (best_split_point.split_bbs, current->split_bbs);
596 /* Return basic block containing RETURN statement. We allow basic blocks
597 of the form:
598 <retval> = tmp_var;
599 return <retval>
600 but return_bb can not be more complex than this.
601 If nothing is found, return EXIT_BLOCK_PTR.
603 When there are multiple RETURN statement, chose one with return value,
604 since that one is more likely shared by multiple code paths.
606 Return BB is special, because for function splitting it is the only
607 basic block that is duplicated in between header and split part of the
608 function.
610 TODO: We might support multiple return blocks. */
612 static basic_block
613 find_return_bb (void)
615 edge e;
616 basic_block return_bb = EXIT_BLOCK_PTR;
617 gimple_stmt_iterator bsi;
618 bool found_return = false;
619 tree retval = NULL_TREE;
621 if (!single_pred_p (EXIT_BLOCK_PTR))
622 return return_bb;
624 e = single_pred_edge (EXIT_BLOCK_PTR);
625 for (bsi = gsi_last_bb (e->src); !gsi_end_p (bsi); gsi_prev (&bsi))
627 gimple stmt = gsi_stmt (bsi);
628 if (gimple_code (stmt) == GIMPLE_LABEL
629 || is_gimple_debug (stmt)
630 || gimple_clobber_p (stmt))
632 else if (gimple_code (stmt) == GIMPLE_ASSIGN
633 && found_return
634 && gimple_assign_single_p (stmt)
635 && (auto_var_in_fn_p (gimple_assign_rhs1 (stmt),
636 current_function_decl)
637 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
638 && retval == gimple_assign_lhs (stmt))
640 else if (gimple_code (stmt) == GIMPLE_RETURN)
642 found_return = true;
643 retval = gimple_return_retval (stmt);
645 else
646 break;
648 if (gsi_end_p (bsi) && found_return)
649 return_bb = e->src;
651 return return_bb;
654 /* Given return basic block RETURN_BB, see where return value is really
655 stored. */
656 static tree
657 find_retval (basic_block return_bb)
659 gimple_stmt_iterator bsi;
660 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
661 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
662 return gimple_return_retval (gsi_stmt (bsi));
663 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
664 && !gimple_clobber_p (gsi_stmt (bsi)))
665 return gimple_assign_rhs1 (gsi_stmt (bsi));
666 return NULL;
669 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
670 variable, mark it as used in bitmap passed via DATA.
671 Return true when access to T prevents splitting the function. */
673 static bool
674 mark_nonssa_use (gimple stmt ATTRIBUTE_UNUSED, tree t, void *data)
676 t = get_base_address (t);
678 if (!t || is_gimple_reg (t))
679 return false;
681 /* At present we can't pass non-SSA arguments to split function.
682 FIXME: this can be relaxed by passing references to arguments. */
683 if (TREE_CODE (t) == PARM_DECL)
685 if (dump_file && (dump_flags & TDF_DETAILS))
686 fprintf (dump_file,
687 "Cannot split: use of non-ssa function parameter.\n");
688 return true;
691 if ((TREE_CODE (t) == VAR_DECL
692 && auto_var_in_fn_p (t, current_function_decl))
693 || TREE_CODE (t) == RESULT_DECL
694 || TREE_CODE (t) == LABEL_DECL)
695 bitmap_set_bit ((bitmap)data, DECL_UID (t));
697 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
698 to pretend that the value pointed to is actual result decl. */
699 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
700 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
701 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
702 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
703 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
704 return
705 bitmap_bit_p ((bitmap)data,
706 DECL_UID (DECL_RESULT (current_function_decl)));
708 return false;
711 /* Compute local properties of basic block BB we collect when looking for
712 split points. We look for ssa defs and store them in SET_SSA_NAMES,
713 for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic
714 vars stored in NON_SSA_VARS.
716 When BB has edge to RETURN_BB, collect uses in RETURN_BB too.
718 Return false when BB contains something that prevents it from being put into
719 split function. */
721 static bool
722 visit_bb (basic_block bb, basic_block return_bb,
723 bitmap set_ssa_names, bitmap used_ssa_names,
724 bitmap non_ssa_vars)
726 gimple_stmt_iterator bsi;
727 edge e;
728 edge_iterator ei;
729 bool can_split = true;
731 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
733 gimple stmt = gsi_stmt (bsi);
734 tree op;
735 ssa_op_iter iter;
736 tree decl;
738 if (is_gimple_debug (stmt))
739 continue;
741 if (gimple_clobber_p (stmt))
742 continue;
744 /* FIXME: We can split regions containing EH. We can not however
745 split RESX, EH_DISPATCH and EH_POINTER referring to same region
746 into different partitions. This would require tracking of
747 EH regions and checking in consider_split_point if they
748 are not used elsewhere. */
749 if (gimple_code (stmt) == GIMPLE_RESX)
751 if (dump_file && (dump_flags & TDF_DETAILS))
752 fprintf (dump_file, "Cannot split: resx.\n");
753 can_split = false;
755 if (gimple_code (stmt) == GIMPLE_EH_DISPATCH)
757 if (dump_file && (dump_flags & TDF_DETAILS))
758 fprintf (dump_file, "Cannot split: eh dispatch.\n");
759 can_split = false;
762 /* Check builtins that prevent splitting. */
763 if (gimple_code (stmt) == GIMPLE_CALL
764 && (decl = gimple_call_fndecl (stmt)) != NULL_TREE
765 && DECL_BUILT_IN (decl)
766 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
767 switch (DECL_FUNCTION_CODE (decl))
769 /* FIXME: once we will allow passing non-parm values to split part,
770 we need to be sure to handle correct builtin_stack_save and
771 builtin_stack_restore. At the moment we are safe; there is no
772 way to store builtin_stack_save result in non-SSA variable
773 since all calls to those are compiler generated. */
774 case BUILT_IN_APPLY:
775 case BUILT_IN_APPLY_ARGS:
776 case BUILT_IN_VA_START:
777 if (dump_file && (dump_flags & TDF_DETAILS))
778 fprintf (dump_file,
779 "Cannot split: builtin_apply and va_start.\n");
780 can_split = false;
781 break;
782 case BUILT_IN_EH_POINTER:
783 if (dump_file && (dump_flags & TDF_DETAILS))
784 fprintf (dump_file, "Cannot split: builtin_eh_pointer.\n");
785 can_split = false;
786 break;
787 default:
788 break;
791 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
792 bitmap_set_bit (set_ssa_names, SSA_NAME_VERSION (op));
793 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
794 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
795 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
796 mark_nonssa_use,
797 mark_nonssa_use,
798 mark_nonssa_use);
800 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
802 gimple stmt = gsi_stmt (bsi);
803 unsigned int i;
805 if (virtual_operand_p (gimple_phi_result (stmt)))
806 continue;
807 bitmap_set_bit (set_ssa_names,
808 SSA_NAME_VERSION (gimple_phi_result (stmt)));
809 for (i = 0; i < gimple_phi_num_args (stmt); i++)
811 tree op = gimple_phi_arg_def (stmt, i);
812 if (TREE_CODE (op) == SSA_NAME)
813 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
815 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
816 mark_nonssa_use,
817 mark_nonssa_use,
818 mark_nonssa_use);
820 /* Record also uses coming from PHI operand in return BB. */
821 FOR_EACH_EDGE (e, ei, bb->succs)
822 if (e->dest == return_bb)
824 for (bsi = gsi_start_phis (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
826 gimple stmt = gsi_stmt (bsi);
827 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
829 if (virtual_operand_p (gimple_phi_result (stmt)))
830 continue;
831 if (TREE_CODE (op) == SSA_NAME)
832 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
833 else
834 can_split &= !mark_nonssa_use (stmt, op, non_ssa_vars);
837 return can_split;
840 /* Stack entry for recursive DFS walk in find_split_point. */
842 typedef struct
844 /* Basic block we are examining. */
845 basic_block bb;
847 /* SSA names set and used by the BB and all BBs reachable
848 from it via DFS walk. */
849 bitmap set_ssa_names, used_ssa_names;
850 bitmap non_ssa_vars;
852 /* All BBS visited from this BB via DFS walk. */
853 bitmap bbs_visited;
855 /* Last examined edge in DFS walk. Since we walk unoriented graph,
856 the value is up to sum of incoming and outgoing edges of BB. */
857 unsigned int edge_num;
859 /* Stack entry index of earliest BB reachable from current BB
860 or any BB visited later in DFS walk. */
861 int earliest;
863 /* Overall time and size of all BBs reached from this BB in DFS walk. */
864 int overall_time, overall_size;
866 /* When false we can not split on this BB. */
867 bool can_split;
868 } stack_entry;
871 /* Find all articulations and call consider_split on them.
872 OVERALL_TIME and OVERALL_SIZE is time and size of the function.
874 We perform basic algorithm for finding an articulation in a graph
875 created from CFG by considering it to be an unoriented graph.
877 The articulation is discovered via DFS walk. We collect earliest
878 basic block on stack that is reachable via backward edge. Articulation
879 is any basic block such that there is no backward edge bypassing it.
880 To reduce stack usage we maintain heap allocated stack in STACK vector.
881 AUX pointer of BB is set to index it appears in the stack or -1 once
882 it is visited and popped off the stack.
884 The algorithm finds articulation after visiting the whole component
885 reachable by it. This makes it convenient to collect information about
886 the component used by consider_split. */
888 static void
889 find_split_points (int overall_time, int overall_size)
891 stack_entry first;
892 vec<stack_entry> stack = vNULL;
893 basic_block bb;
894 basic_block return_bb = find_return_bb ();
895 struct split_point current;
897 current.header_time = overall_time;
898 current.header_size = overall_size;
899 current.split_time = 0;
900 current.split_size = 0;
901 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
903 first.bb = ENTRY_BLOCK_PTR;
904 first.edge_num = 0;
905 first.overall_time = 0;
906 first.overall_size = 0;
907 first.earliest = INT_MAX;
908 first.set_ssa_names = 0;
909 first.used_ssa_names = 0;
910 first.bbs_visited = 0;
911 stack.safe_push (first);
912 ENTRY_BLOCK_PTR->aux = (void *)(intptr_t)-1;
914 while (!stack.is_empty ())
916 stack_entry *entry = &stack.last ();
918 /* We are walking an acyclic graph, so edge_num counts
919 succ and pred edges together. However when considering
920 articulation, we want to have processed everything reachable
921 from articulation but nothing that reaches into it. */
922 if (entry->edge_num == EDGE_COUNT (entry->bb->succs)
923 && entry->bb != ENTRY_BLOCK_PTR)
925 int pos = stack.length ();
926 entry->can_split &= visit_bb (entry->bb, return_bb,
927 entry->set_ssa_names,
928 entry->used_ssa_names,
929 entry->non_ssa_vars);
930 if (pos <= entry->earliest && !entry->can_split
931 && dump_file && (dump_flags & TDF_DETAILS))
932 fprintf (dump_file,
933 "found articulation at bb %i but can not split\n",
934 entry->bb->index);
935 if (pos <= entry->earliest && entry->can_split)
937 if (dump_file && (dump_flags & TDF_DETAILS))
938 fprintf (dump_file, "found articulation at bb %i\n",
939 entry->bb->index);
940 current.entry_bb = entry->bb;
941 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
942 bitmap_and_compl (current.ssa_names_to_pass,
943 entry->used_ssa_names, entry->set_ssa_names);
944 current.header_time = overall_time - entry->overall_time;
945 current.header_size = overall_size - entry->overall_size;
946 current.split_time = entry->overall_time;
947 current.split_size = entry->overall_size;
948 current.split_bbs = entry->bbs_visited;
949 consider_split (&current, entry->non_ssa_vars, return_bb);
950 BITMAP_FREE (current.ssa_names_to_pass);
953 /* Do actual DFS walk. */
954 if (entry->edge_num
955 < (EDGE_COUNT (entry->bb->succs)
956 + EDGE_COUNT (entry->bb->preds)))
958 edge e;
959 basic_block dest;
960 if (entry->edge_num < EDGE_COUNT (entry->bb->succs))
962 e = EDGE_SUCC (entry->bb, entry->edge_num);
963 dest = e->dest;
965 else
967 e = EDGE_PRED (entry->bb, entry->edge_num
968 - EDGE_COUNT (entry->bb->succs));
969 dest = e->src;
972 entry->edge_num++;
974 /* New BB to visit, push it to the stack. */
975 if (dest != return_bb && dest != EXIT_BLOCK_PTR
976 && !dest->aux)
978 stack_entry new_entry;
980 new_entry.bb = dest;
981 new_entry.edge_num = 0;
982 new_entry.overall_time
983 = bb_info_vec[dest->index].time;
984 new_entry.overall_size
985 = bb_info_vec[dest->index].size;
986 new_entry.earliest = INT_MAX;
987 new_entry.set_ssa_names = BITMAP_ALLOC (NULL);
988 new_entry.used_ssa_names = BITMAP_ALLOC (NULL);
989 new_entry.bbs_visited = BITMAP_ALLOC (NULL);
990 new_entry.non_ssa_vars = BITMAP_ALLOC (NULL);
991 new_entry.can_split = true;
992 bitmap_set_bit (new_entry.bbs_visited, dest->index);
993 stack.safe_push (new_entry);
994 dest->aux = (void *)(intptr_t)stack.length ();
996 /* Back edge found, record the earliest point. */
997 else if ((intptr_t)dest->aux > 0
998 && (intptr_t)dest->aux < entry->earliest)
999 entry->earliest = (intptr_t)dest->aux;
1001 /* We are done with examining the edges. Pop off the value from stack
1002 and merge stuff we accumulate during the walk. */
1003 else if (entry->bb != ENTRY_BLOCK_PTR)
1005 stack_entry *prev = &stack[stack.length () - 2];
1007 entry->bb->aux = (void *)(intptr_t)-1;
1008 prev->can_split &= entry->can_split;
1009 if (prev->set_ssa_names)
1011 bitmap_ior_into (prev->set_ssa_names, entry->set_ssa_names);
1012 bitmap_ior_into (prev->used_ssa_names, entry->used_ssa_names);
1013 bitmap_ior_into (prev->bbs_visited, entry->bbs_visited);
1014 bitmap_ior_into (prev->non_ssa_vars, entry->non_ssa_vars);
1016 if (prev->earliest > entry->earliest)
1017 prev->earliest = entry->earliest;
1018 prev->overall_time += entry->overall_time;
1019 prev->overall_size += entry->overall_size;
1020 BITMAP_FREE (entry->set_ssa_names);
1021 BITMAP_FREE (entry->used_ssa_names);
1022 BITMAP_FREE (entry->bbs_visited);
1023 BITMAP_FREE (entry->non_ssa_vars);
1024 stack.pop ();
1026 else
1027 stack.pop ();
1029 ENTRY_BLOCK_PTR->aux = NULL;
1030 FOR_EACH_BB (bb)
1031 bb->aux = NULL;
1032 stack.release ();
1033 BITMAP_FREE (current.ssa_names_to_pass);
1036 /* Split function at SPLIT_POINT. */
1038 static void
1039 split_function (struct split_point *split_point)
1041 vec<tree> args_to_pass = vNULL;
1042 bitmap args_to_skip;
1043 tree parm;
1044 int num = 0;
1045 struct cgraph_node *node, *cur_node = cgraph_get_node (current_function_decl);
1046 basic_block return_bb = find_return_bb ();
1047 basic_block call_bb;
1048 gimple_stmt_iterator gsi;
1049 gimple call;
1050 edge e;
1051 edge_iterator ei;
1052 tree retval = NULL, real_retval = NULL;
1053 bool split_part_return_p = false;
1054 gimple last_stmt = NULL;
1055 unsigned int i;
1056 tree arg, ddef;
1057 vec<tree, va_gc> **debug_args = NULL;
1059 if (dump_file)
1061 fprintf (dump_file, "\n\nSplitting function at:\n");
1062 dump_split_point (dump_file, split_point);
1065 if (cur_node->local.can_change_signature)
1066 args_to_skip = BITMAP_ALLOC (NULL);
1067 else
1068 args_to_skip = NULL;
1070 /* Collect the parameters of new function and args_to_skip bitmap. */
1071 for (parm = DECL_ARGUMENTS (current_function_decl);
1072 parm; parm = DECL_CHAIN (parm), num++)
1073 if (args_to_skip
1074 && (!is_gimple_reg (parm)
1075 || (ddef = ssa_default_def (cfun, parm)) == NULL_TREE
1076 || !bitmap_bit_p (split_point->ssa_names_to_pass,
1077 SSA_NAME_VERSION (ddef))))
1078 bitmap_set_bit (args_to_skip, num);
1079 else
1081 /* This parm might not have been used up to now, but is going to be
1082 used, hence register it. */
1083 if (is_gimple_reg (parm))
1084 arg = get_or_create_ssa_default_def (cfun, parm);
1085 else
1086 arg = parm;
1088 if (!useless_type_conversion_p (DECL_ARG_TYPE (parm), TREE_TYPE (arg)))
1089 arg = fold_convert (DECL_ARG_TYPE (parm), arg);
1090 args_to_pass.safe_push (arg);
1093 /* See if the split function will return. */
1094 FOR_EACH_EDGE (e, ei, return_bb->preds)
1095 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1096 break;
1097 if (e)
1098 split_part_return_p = true;
1100 /* Add return block to what will become the split function.
1101 We do not return; no return block is needed. */
1102 if (!split_part_return_p)
1104 /* We have no return block, so nothing is needed. */
1105 else if (return_bb == EXIT_BLOCK_PTR)
1107 /* When we do not want to return value, we need to construct
1108 new return block with empty return statement.
1109 FIXME: Once we are able to change return type, we should change function
1110 to return void instead of just outputting function with undefined return
1111 value. For structures this affects quality of codegen. */
1112 else if (!split_point->split_part_set_retval
1113 && find_retval (return_bb))
1115 bool redirected = true;
1116 basic_block new_return_bb = create_basic_block (NULL, 0, return_bb);
1117 gimple_stmt_iterator gsi = gsi_start_bb (new_return_bb);
1118 gsi_insert_after (&gsi, gimple_build_return (NULL), GSI_NEW_STMT);
1119 while (redirected)
1121 redirected = false;
1122 FOR_EACH_EDGE (e, ei, return_bb->preds)
1123 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1125 new_return_bb->count += e->count;
1126 new_return_bb->frequency += EDGE_FREQUENCY (e);
1127 redirect_edge_and_branch (e, new_return_bb);
1128 redirected = true;
1129 break;
1132 e = make_edge (new_return_bb, EXIT_BLOCK_PTR, 0);
1133 e->probability = REG_BR_PROB_BASE;
1134 e->count = new_return_bb->count;
1135 bitmap_set_bit (split_point->split_bbs, new_return_bb->index);
1137 /* When we pass around the value, use existing return block. */
1138 else
1139 bitmap_set_bit (split_point->split_bbs, return_bb->index);
1141 /* If RETURN_BB has virtual operand PHIs, they must be removed and the
1142 virtual operand marked for renaming as we change the CFG in a way that
1143 tree-inline is not able to compensate for.
1145 Note this can happen whether or not we have a return value. If we have
1146 a return value, then RETURN_BB may have PHIs for real operands too. */
1147 if (return_bb != EXIT_BLOCK_PTR)
1149 bool phi_p = false;
1150 for (gsi = gsi_start_phis (return_bb); !gsi_end_p (gsi);)
1152 gimple stmt = gsi_stmt (gsi);
1153 if (!virtual_operand_p (gimple_phi_result (stmt)))
1155 gsi_next (&gsi);
1156 continue;
1158 mark_virtual_phi_result_for_renaming (stmt);
1159 remove_phi_node (&gsi, true);
1160 phi_p = true;
1162 /* In reality we have to rename the reaching definition of the
1163 virtual operand at return_bb as we will eventually release it
1164 when we remove the code region we outlined.
1165 So we have to rename all immediate virtual uses of that region
1166 if we didn't see a PHI definition yet. */
1167 /* ??? In real reality we want to set the reaching vdef of the
1168 entry of the SESE region as the vuse of the call and the reaching
1169 vdef of the exit of the SESE region as the vdef of the call. */
1170 if (!phi_p)
1171 for (gsi = gsi_start_bb (return_bb); !gsi_end_p (gsi); gsi_next (&gsi))
1173 gimple stmt = gsi_stmt (gsi);
1174 if (gimple_vuse (stmt))
1176 gimple_set_vuse (stmt, NULL_TREE);
1177 update_stmt (stmt);
1179 if (gimple_vdef (stmt))
1180 break;
1184 /* Now create the actual clone. */
1185 rebuild_cgraph_edges ();
1186 node = cgraph_function_versioning (cur_node, vNULL,
1187 NULL,
1188 args_to_skip,
1189 !split_part_return_p,
1190 split_point->split_bbs,
1191 split_point->entry_bb, "part");
1192 /* For usual cloning it is enough to clear builtin only when signature
1193 changes. For partial inlining we however can not expect the part
1194 of builtin implementation to have same semantic as the whole. */
1195 if (DECL_BUILT_IN (node->symbol.decl))
1197 DECL_BUILT_IN_CLASS (node->symbol.decl) = NOT_BUILT_IN;
1198 DECL_FUNCTION_CODE (node->symbol.decl) = (enum built_in_function) 0;
1200 cgraph_node_remove_callees (cur_node);
1201 if (!split_part_return_p)
1202 TREE_THIS_VOLATILE (node->symbol.decl) = 1;
1203 if (dump_file)
1204 dump_function_to_file (node->symbol.decl, dump_file, dump_flags);
1206 /* Create the basic block we place call into. It is the entry basic block
1207 split after last label. */
1208 call_bb = split_point->entry_bb;
1209 for (gsi = gsi_start_bb (call_bb); !gsi_end_p (gsi);)
1210 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
1212 last_stmt = gsi_stmt (gsi);
1213 gsi_next (&gsi);
1215 else
1216 break;
1217 e = split_block (split_point->entry_bb, last_stmt);
1218 remove_edge (e);
1220 /* Produce the call statement. */
1221 gsi = gsi_last_bb (call_bb);
1222 FOR_EACH_VEC_ELT (args_to_pass, i, arg)
1223 if (!is_gimple_val (arg))
1225 arg = force_gimple_operand_gsi (&gsi, arg, true, NULL_TREE,
1226 false, GSI_CONTINUE_LINKING);
1227 args_to_pass[i] = arg;
1229 call = gimple_build_call_vec (node->symbol.decl, args_to_pass);
1230 gimple_set_block (call, DECL_INITIAL (current_function_decl));
1231 args_to_pass.release ();
1233 /* For optimized away parameters, add on the caller side
1234 before the call
1235 DEBUG D#X => parm_Y(D)
1236 stmts and associate D#X with parm in decl_debug_args_lookup
1237 vector to say for debug info that if parameter parm had been passed,
1238 it would have value parm_Y(D). */
1239 if (args_to_skip)
1240 for (parm = DECL_ARGUMENTS (current_function_decl), num = 0;
1241 parm; parm = DECL_CHAIN (parm), num++)
1242 if (bitmap_bit_p (args_to_skip, num)
1243 && is_gimple_reg (parm))
1245 tree ddecl;
1246 gimple def_temp;
1248 /* This needs to be done even without MAY_HAVE_DEBUG_STMTS,
1249 otherwise if it didn't exist before, we'd end up with
1250 different SSA_NAME_VERSIONs between -g and -g0. */
1251 arg = get_or_create_ssa_default_def (cfun, parm);
1252 if (!MAY_HAVE_DEBUG_STMTS)
1253 continue;
1255 if (debug_args == NULL)
1256 debug_args = decl_debug_args_insert (node->symbol.decl);
1257 ddecl = make_node (DEBUG_EXPR_DECL);
1258 DECL_ARTIFICIAL (ddecl) = 1;
1259 TREE_TYPE (ddecl) = TREE_TYPE (parm);
1260 DECL_MODE (ddecl) = DECL_MODE (parm);
1261 vec_safe_push (*debug_args, DECL_ORIGIN (parm));
1262 vec_safe_push (*debug_args, ddecl);
1263 def_temp = gimple_build_debug_bind (ddecl, unshare_expr (arg),
1264 call);
1265 gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
1267 /* And on the callee side, add
1268 DEBUG D#Y s=> parm
1269 DEBUG var => D#Y
1270 stmts to the first bb where var is a VAR_DECL created for the
1271 optimized away parameter in DECL_INITIAL block. This hints
1272 in the debug info that var (whole DECL_ORIGIN is the parm PARM_DECL)
1273 is optimized away, but could be looked up at the call site
1274 as value of D#X there. */
1275 if (debug_args != NULL)
1277 unsigned int i;
1278 tree var, vexpr;
1279 gimple_stmt_iterator cgsi;
1280 gimple def_temp;
1282 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1283 var = BLOCK_VARS (DECL_INITIAL (node->symbol.decl));
1284 i = vec_safe_length (*debug_args);
1285 cgsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR));
1288 i -= 2;
1289 while (var != NULL_TREE
1290 && DECL_ABSTRACT_ORIGIN (var) != (**debug_args)[i])
1291 var = TREE_CHAIN (var);
1292 if (var == NULL_TREE)
1293 break;
1294 vexpr = make_node (DEBUG_EXPR_DECL);
1295 parm = (**debug_args)[i];
1296 DECL_ARTIFICIAL (vexpr) = 1;
1297 TREE_TYPE (vexpr) = TREE_TYPE (parm);
1298 DECL_MODE (vexpr) = DECL_MODE (parm);
1299 def_temp = gimple_build_debug_source_bind (vexpr, parm,
1300 NULL);
1301 gsi_insert_before (&cgsi, def_temp, GSI_SAME_STMT);
1302 def_temp = gimple_build_debug_bind (var, vexpr, NULL);
1303 gsi_insert_before (&cgsi, def_temp, GSI_SAME_STMT);
1305 while (i);
1306 pop_cfun ();
1309 /* We avoid address being taken on any variable used by split part,
1310 so return slot optimization is always possible. Moreover this is
1311 required to make DECL_BY_REFERENCE work. */
1312 if (aggregate_value_p (DECL_RESULT (current_function_decl),
1313 TREE_TYPE (current_function_decl)))
1314 gimple_call_set_return_slot_opt (call, true);
1316 /* Update return value. This is bit tricky. When we do not return,
1317 do nothing. When we return we might need to update return_bb
1318 or produce a new return statement. */
1319 if (!split_part_return_p)
1320 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1321 else
1323 e = make_edge (call_bb, return_bb,
1324 return_bb == EXIT_BLOCK_PTR ? 0 : EDGE_FALLTHRU);
1325 e->count = call_bb->count;
1326 e->probability = REG_BR_PROB_BASE;
1328 /* If there is return basic block, see what value we need to store
1329 return value into and put call just before it. */
1330 if (return_bb != EXIT_BLOCK_PTR)
1332 real_retval = retval = find_retval (return_bb);
1334 if (real_retval && split_point->split_part_set_retval)
1336 gimple_stmt_iterator psi;
1338 /* See if we need new SSA_NAME for the result.
1339 When DECL_BY_REFERENCE is true, retval is actually pointer to
1340 return value and it is constant in whole function. */
1341 if (TREE_CODE (retval) == SSA_NAME
1342 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1344 retval = copy_ssa_name (retval, call);
1346 /* See if there is PHI defining return value. */
1347 for (psi = gsi_start_phis (return_bb);
1348 !gsi_end_p (psi); gsi_next (&psi))
1349 if (!virtual_operand_p (gimple_phi_result (gsi_stmt (psi))))
1350 break;
1352 /* When there is PHI, just update its value. */
1353 if (TREE_CODE (retval) == SSA_NAME
1354 && !gsi_end_p (psi))
1355 add_phi_arg (gsi_stmt (psi), retval, e, UNKNOWN_LOCATION);
1356 /* Otherwise update the return BB itself.
1357 find_return_bb allows at most one assignment to return value,
1358 so update first statement. */
1359 else
1361 gimple_stmt_iterator bsi;
1362 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi);
1363 gsi_next (&bsi))
1364 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
1366 gimple_return_set_retval (gsi_stmt (bsi), retval);
1367 break;
1369 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
1370 && !gimple_clobber_p (gsi_stmt (bsi)))
1372 gimple_assign_set_rhs1 (gsi_stmt (bsi), retval);
1373 break;
1375 update_stmt (gsi_stmt (bsi));
1378 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1380 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1381 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1383 else
1385 tree restype;
1386 restype = TREE_TYPE (DECL_RESULT (current_function_decl));
1387 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1388 if (!useless_type_conversion_p (TREE_TYPE (retval), restype))
1390 gimple cpy;
1391 tree tem = create_tmp_reg (restype, NULL);
1392 tem = make_ssa_name (tem, call);
1393 cpy = gimple_build_assign_with_ops (NOP_EXPR, retval,
1394 tem, NULL_TREE);
1395 gsi_insert_after (&gsi, cpy, GSI_NEW_STMT);
1396 retval = tem;
1398 gimple_call_set_lhs (call, retval);
1399 update_stmt (call);
1402 else
1403 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1405 /* We don't use return block (there is either no return in function or
1406 multiple of them). So create new basic block with return statement.
1408 else
1410 gimple ret;
1411 if (split_point->split_part_set_retval
1412 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1414 retval = DECL_RESULT (current_function_decl);
1416 /* We use temporary register to hold value when aggregate_value_p
1417 is false. Similarly for DECL_BY_REFERENCE we must avoid extra
1418 copy. */
1419 if (!aggregate_value_p (retval, TREE_TYPE (current_function_decl))
1420 && !DECL_BY_REFERENCE (retval))
1421 retval = create_tmp_reg (TREE_TYPE (retval), NULL);
1422 if (is_gimple_reg (retval))
1424 /* When returning by reference, there is only one SSA name
1425 assigned to RESULT_DECL (that is pointer to return value).
1426 Look it up or create new one if it is missing. */
1427 if (DECL_BY_REFERENCE (retval))
1428 retval = get_or_create_ssa_default_def (cfun, retval);
1429 /* Otherwise produce new SSA name for return value. */
1430 else
1431 retval = make_ssa_name (retval, call);
1433 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1434 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1435 else
1436 gimple_call_set_lhs (call, retval);
1438 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1439 ret = gimple_build_return (retval);
1440 gsi_insert_after (&gsi, ret, GSI_NEW_STMT);
1443 free_dominance_info (CDI_DOMINATORS);
1444 free_dominance_info (CDI_POST_DOMINATORS);
1445 compute_inline_parameters (node, true);
1448 /* Execute function splitting pass. */
1450 static unsigned int
1451 execute_split_functions (void)
1453 gimple_stmt_iterator bsi;
1454 basic_block bb;
1455 int overall_time = 0, overall_size = 0;
1456 int todo = 0;
1457 struct cgraph_node *node = cgraph_get_node (current_function_decl);
1459 if (flags_from_decl_or_type (current_function_decl)
1460 & (ECF_NORETURN|ECF_MALLOC))
1462 if (dump_file)
1463 fprintf (dump_file, "Not splitting: noreturn/malloc function.\n");
1464 return 0;
1466 if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
1468 if (dump_file)
1469 fprintf (dump_file, "Not splitting: main function.\n");
1470 return 0;
1472 /* This can be relaxed; function might become inlinable after splitting
1473 away the uninlinable part. */
1474 if (inline_edge_summary_vec.exists ()
1475 && !inline_summary (node)->inlinable)
1477 if (dump_file)
1478 fprintf (dump_file, "Not splitting: not inlinable.\n");
1479 return 0;
1481 if (DECL_DISREGARD_INLINE_LIMITS (node->symbol.decl))
1483 if (dump_file)
1484 fprintf (dump_file, "Not splitting: disregarding inline limits.\n");
1485 return 0;
1487 /* This can be relaxed; most of versioning tests actually prevents
1488 a duplication. */
1489 if (!tree_versionable_function_p (current_function_decl))
1491 if (dump_file)
1492 fprintf (dump_file, "Not splitting: not versionable.\n");
1493 return 0;
1495 /* FIXME: we could support this. */
1496 if (DECL_STRUCT_FUNCTION (current_function_decl)->static_chain_decl)
1498 if (dump_file)
1499 fprintf (dump_file, "Not splitting: nested function.\n");
1500 return 0;
1503 /* See if it makes sense to try to split.
1504 It makes sense to split if we inline, that is if we have direct calls to
1505 handle or direct calls are possibly going to appear as result of indirect
1506 inlining or LTO. Also handle -fprofile-generate as LTO to allow non-LTO
1507 training for LTO -fprofile-use build.
1509 Note that we are not completely conservative about disqualifying functions
1510 called once. It is possible that the caller is called more then once and
1511 then inlining would still benefit. */
1512 if ((!node->callers || !node->callers->next_caller)
1513 && !node->symbol.address_taken
1514 && (!flag_lto || !node->symbol.externally_visible))
1516 if (dump_file)
1517 fprintf (dump_file, "Not splitting: not called directly "
1518 "or called once.\n");
1519 return 0;
1522 /* FIXME: We can actually split if splitting reduces call overhead. */
1523 if (!flag_inline_small_functions
1524 && !DECL_DECLARED_INLINE_P (current_function_decl))
1526 if (dump_file)
1527 fprintf (dump_file, "Not splitting: not autoinlining and function"
1528 " is not inline.\n");
1529 return 0;
1532 /* Initialize bitmap to track forbidden calls. */
1533 forbidden_dominators = BITMAP_ALLOC (NULL);
1534 calculate_dominance_info (CDI_DOMINATORS);
1536 /* Compute local info about basic blocks and determine function size/time. */
1537 bb_info_vec.safe_grow_cleared (last_basic_block + 1);
1538 memset (&best_split_point, 0, sizeof (best_split_point));
1539 FOR_EACH_BB (bb)
1541 int time = 0;
1542 int size = 0;
1543 int freq = compute_call_stmt_bb_frequency (current_function_decl, bb);
1545 if (dump_file && (dump_flags & TDF_DETAILS))
1546 fprintf (dump_file, "Basic block %i\n", bb->index);
1548 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1550 int this_time, this_size;
1551 gimple stmt = gsi_stmt (bsi);
1553 this_size = estimate_num_insns (stmt, &eni_size_weights);
1554 this_time = estimate_num_insns (stmt, &eni_time_weights) * freq;
1555 size += this_size;
1556 time += this_time;
1557 check_forbidden_calls (stmt);
1559 if (dump_file && (dump_flags & TDF_DETAILS))
1561 fprintf (dump_file, " freq:%6i size:%3i time:%3i ",
1562 freq, this_size, this_time);
1563 print_gimple_stmt (dump_file, stmt, 0, 0);
1566 overall_time += time;
1567 overall_size += size;
1568 bb_info_vec[bb->index].time = time;
1569 bb_info_vec[bb->index].size = size;
1571 find_split_points (overall_time, overall_size);
1572 if (best_split_point.split_bbs)
1574 split_function (&best_split_point);
1575 BITMAP_FREE (best_split_point.ssa_names_to_pass);
1576 BITMAP_FREE (best_split_point.split_bbs);
1577 todo = TODO_update_ssa | TODO_cleanup_cfg;
1579 BITMAP_FREE (forbidden_dominators);
1580 bb_info_vec.release ();
1581 return todo;
1584 /* Gate function splitting pass. When doing profile feedback, we want
1585 to execute the pass after profiling is read. So disable one in
1586 early optimization. */
1588 static bool
1589 gate_split_functions (void)
1591 return (flag_partial_inlining
1592 && !profile_arc_flag && !flag_branch_probabilities);
1595 struct gimple_opt_pass pass_split_functions =
1598 GIMPLE_PASS,
1599 "fnsplit", /* name */
1600 OPTGROUP_NONE, /* optinfo_flags */
1601 gate_split_functions, /* gate */
1602 execute_split_functions, /* execute */
1603 NULL, /* sub */
1604 NULL, /* next */
1605 0, /* static_pass_number */
1606 TV_IPA_FNSPLIT, /* tv_id */
1607 PROP_cfg, /* properties_required */
1608 0, /* properties_provided */
1609 0, /* properties_destroyed */
1610 0, /* todo_flags_start */
1611 TODO_verify_all /* todo_flags_finish */
1615 /* Gate feedback driven function splitting pass.
1616 We don't need to split when profiling at all, we are producing
1617 lousy code anyway. */
1619 static bool
1620 gate_feedback_split_functions (void)
1622 return (flag_partial_inlining
1623 && flag_branch_probabilities);
1626 /* Execute function splitting pass. */
1628 static unsigned int
1629 execute_feedback_split_functions (void)
1631 unsigned int retval = execute_split_functions ();
1632 if (retval)
1633 retval |= TODO_rebuild_cgraph_edges;
1634 return retval;
1637 struct gimple_opt_pass pass_feedback_split_functions =
1640 GIMPLE_PASS,
1641 "feedback_fnsplit", /* name */
1642 OPTGROUP_NONE, /* optinfo_flags */
1643 gate_feedback_split_functions, /* gate */
1644 execute_feedback_split_functions, /* execute */
1645 NULL, /* sub */
1646 NULL, /* next */
1647 0, /* static_pass_number */
1648 TV_IPA_FNSPLIT, /* tv_id */
1649 PROP_cfg, /* properties_required */
1650 0, /* properties_provided */
1651 0, /* properties_destroyed */
1652 0, /* todo_flags_start */
1653 TODO_verify_all /* todo_flags_finish */