2012-01-19 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ipa-split.c
blob09e04af5b886dd4121e6f2fb57a2164b1c2ace19
1 /* Function splitting pass
2 Copyright (C) 2010, 2011
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 "timevar.h"
89 #include "diagnostic.h"
90 #include "tree-dump.h"
91 #include "tree-inline.h"
92 #include "fibheap.h"
93 #include "params.h"
94 #include "gimple-pretty-print.h"
95 #include "ipa-inline.h"
97 /* Per basic block info. */
99 typedef struct
101 unsigned int size;
102 unsigned int time;
103 } bb_info;
104 DEF_VEC_O(bb_info);
105 DEF_VEC_ALLOC_O(bb_info,heap);
107 static VEC(bb_info, heap) *bb_info_vec;
109 /* Description of split point. */
111 struct split_point
113 /* Size of the partitions. */
114 unsigned int header_time, header_size, split_time, split_size;
116 /* SSA names that need to be passed into spit function. */
117 bitmap ssa_names_to_pass;
119 /* Basic block where we split (that will become entry point of new function. */
120 basic_block entry_bb;
122 /* Basic blocks we are splitting away. */
123 bitmap split_bbs;
125 /* True when return value is computed on split part and thus it needs
126 to be returned. */
127 bool split_part_set_retval;
130 /* Best split point found. */
132 struct split_point best_split_point;
134 /* Set of basic blocks that are not allowed to dominate a split point. */
136 static bitmap forbidden_dominators;
138 static tree find_retval (basic_block return_bb);
140 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
141 variable, check it if it is present in bitmap passed via DATA. */
143 static bool
144 test_nonssa_use (gimple stmt ATTRIBUTE_UNUSED, tree t, void *data)
146 t = get_base_address (t);
148 if (!t || is_gimple_reg (t))
149 return false;
151 if (TREE_CODE (t) == PARM_DECL
152 || (TREE_CODE (t) == VAR_DECL
153 && auto_var_in_fn_p (t, current_function_decl))
154 || TREE_CODE (t) == RESULT_DECL
155 || TREE_CODE (t) == LABEL_DECL)
156 return bitmap_bit_p ((bitmap)data, DECL_UID (t));
158 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
159 to pretend that the value pointed to is actual result decl. */
160 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
161 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
162 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
163 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
164 return
165 bitmap_bit_p ((bitmap)data,
166 DECL_UID (DECL_RESULT (current_function_decl)));
168 return false;
171 /* Dump split point CURRENT. */
173 static void
174 dump_split_point (FILE * file, struct split_point *current)
176 fprintf (file,
177 "Split point at BB %i\n"
178 " header time: %i header size: %i\n"
179 " split time: %i split size: %i\n bbs: ",
180 current->entry_bb->index, current->header_time,
181 current->header_size, current->split_time, current->split_size);
182 dump_bitmap (file, current->split_bbs);
183 fprintf (file, " SSA names to pass: ");
184 dump_bitmap (file, current->ssa_names_to_pass);
187 /* Look for all BBs in header that might lead to the split part and verify
188 that they are not defining any non-SSA var used by the split part.
189 Parameters are the same as for consider_split. */
191 static bool
192 verify_non_ssa_vars (struct split_point *current, bitmap non_ssa_vars,
193 basic_block return_bb)
195 bitmap seen = BITMAP_ALLOC (NULL);
196 VEC (basic_block,heap) *worklist = NULL;
197 edge e;
198 edge_iterator ei;
199 bool ok = true;
201 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
202 if (e->src != ENTRY_BLOCK_PTR
203 && !bitmap_bit_p (current->split_bbs, e->src->index))
205 VEC_safe_push (basic_block, heap, worklist, e->src);
206 bitmap_set_bit (seen, e->src->index);
209 while (!VEC_empty (basic_block, worklist))
211 gimple_stmt_iterator bsi;
212 basic_block bb = VEC_pop (basic_block, worklist);
214 FOR_EACH_EDGE (e, ei, bb->preds)
215 if (e->src != ENTRY_BLOCK_PTR
216 && bitmap_set_bit (seen, e->src->index))
218 gcc_checking_assert (!bitmap_bit_p (current->split_bbs,
219 e->src->index));
220 VEC_safe_push (basic_block, heap, worklist, e->src);
222 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
224 gimple stmt = gsi_stmt (bsi);
225 if (is_gimple_debug (stmt))
226 continue;
227 if (walk_stmt_load_store_addr_ops
228 (stmt, non_ssa_vars, test_nonssa_use, test_nonssa_use,
229 test_nonssa_use))
231 ok = false;
232 goto done;
234 if (gimple_code (stmt) == GIMPLE_LABEL
235 && test_nonssa_use (stmt, gimple_label_label (stmt),
236 non_ssa_vars))
238 ok = false;
239 goto done;
242 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
244 if (walk_stmt_load_store_addr_ops
245 (gsi_stmt (bsi), non_ssa_vars, test_nonssa_use, test_nonssa_use,
246 test_nonssa_use))
248 ok = false;
249 goto done;
252 FOR_EACH_EDGE (e, ei, bb->succs)
254 if (e->dest != return_bb)
255 continue;
256 for (bsi = gsi_start_phis (return_bb); !gsi_end_p (bsi);
257 gsi_next (&bsi))
259 gimple stmt = gsi_stmt (bsi);
260 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
262 if (!is_gimple_reg (gimple_phi_result (stmt)))
263 continue;
264 if (TREE_CODE (op) != SSA_NAME
265 && test_nonssa_use (stmt, op, non_ssa_vars))
267 ok = false;
268 goto done;
273 done:
274 BITMAP_FREE (seen);
275 VEC_free (basic_block, heap, worklist);
276 return ok;
279 /* If STMT is a call, check the callee against a list of forbidden
280 predicate functions. If a match is found, look for uses of the
281 call result in condition statements that compare against zero.
282 For each such use, find the block targeted by the condition
283 statement for the nonzero result, and set the bit for this block
284 in the forbidden dominators bitmap. The purpose of this is to avoid
285 selecting a split point where we are likely to lose the chance
286 to optimize away an unused function call. */
288 static void
289 check_forbidden_calls (gimple stmt)
291 imm_use_iterator use_iter;
292 use_operand_p use_p;
293 tree lhs;
295 /* At the moment, __builtin_constant_p is the only forbidden
296 predicate function call (see PR49642). */
297 if (!gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P))
298 return;
300 lhs = gimple_call_lhs (stmt);
302 if (!lhs || TREE_CODE (lhs) != SSA_NAME)
303 return;
305 FOR_EACH_IMM_USE_FAST (use_p, use_iter, lhs)
307 tree op1;
308 basic_block use_bb, forbidden_bb;
309 enum tree_code code;
310 edge true_edge, false_edge;
311 gimple use_stmt = USE_STMT (use_p);
313 if (gimple_code (use_stmt) != GIMPLE_COND)
314 continue;
316 /* Assuming canonical form for GIMPLE_COND here, with constant
317 in second position. */
318 op1 = gimple_cond_rhs (use_stmt);
319 code = gimple_cond_code (use_stmt);
320 use_bb = gimple_bb (use_stmt);
322 extract_true_false_edges_from_block (use_bb, &true_edge, &false_edge);
324 /* We're only interested in comparisons that distinguish
325 unambiguously from zero. */
326 if (!integer_zerop (op1) || code == LE_EXPR || code == GE_EXPR)
327 continue;
329 if (code == EQ_EXPR)
330 forbidden_bb = false_edge->dest;
331 else
332 forbidden_bb = true_edge->dest;
334 bitmap_set_bit (forbidden_dominators, forbidden_bb->index);
338 /* If BB is dominated by any block in the forbidden dominators set,
339 return TRUE; else FALSE. */
341 static bool
342 dominated_by_forbidden (basic_block bb)
344 unsigned dom_bb;
345 bitmap_iterator bi;
347 EXECUTE_IF_SET_IN_BITMAP (forbidden_dominators, 1, dom_bb, bi)
349 if (dominated_by_p (CDI_DOMINATORS, bb, BASIC_BLOCK (dom_bb)))
350 return true;
353 return false;
356 /* We found an split_point CURRENT. NON_SSA_VARS is bitmap of all non ssa
357 variables used and RETURN_BB is return basic block.
358 See if we can split function here. */
360 static void
361 consider_split (struct split_point *current, bitmap non_ssa_vars,
362 basic_block return_bb)
364 tree parm;
365 unsigned int num_args = 0;
366 unsigned int call_overhead;
367 edge e;
368 edge_iterator ei;
369 gimple_stmt_iterator bsi;
370 unsigned int i;
371 int incoming_freq = 0;
372 tree retval;
374 if (dump_file && (dump_flags & TDF_DETAILS))
375 dump_split_point (dump_file, current);
377 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
378 if (!bitmap_bit_p (current->split_bbs, e->src->index))
379 incoming_freq += EDGE_FREQUENCY (e);
381 /* Do not split when we would end up calling function anyway. */
382 if (incoming_freq
383 >= (ENTRY_BLOCK_PTR->frequency
384 * PARAM_VALUE (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY) / 100))
386 if (dump_file && (dump_flags & TDF_DETAILS))
387 fprintf (dump_file,
388 " Refused: incoming frequency is too large.\n");
389 return;
392 if (!current->header_size)
394 if (dump_file && (dump_flags & TDF_DETAILS))
395 fprintf (dump_file, " Refused: header empty\n");
396 return;
399 /* Verify that PHI args on entry are either virtual or all their operands
400 incoming from header are the same. */
401 for (bsi = gsi_start_phis (current->entry_bb); !gsi_end_p (bsi); gsi_next (&bsi))
403 gimple stmt = gsi_stmt (bsi);
404 tree val = NULL;
406 if (!is_gimple_reg (gimple_phi_result (stmt)))
407 continue;
408 for (i = 0; i < gimple_phi_num_args (stmt); i++)
410 edge e = gimple_phi_arg_edge (stmt, i);
411 if (!bitmap_bit_p (current->split_bbs, e->src->index))
413 tree edge_val = gimple_phi_arg_def (stmt, i);
414 if (val && edge_val != val)
416 if (dump_file && (dump_flags & TDF_DETAILS))
417 fprintf (dump_file,
418 " Refused: entry BB has PHI with multiple variants\n");
419 return;
421 val = edge_val;
427 /* See what argument we will pass to the split function and compute
428 call overhead. */
429 call_overhead = eni_size_weights.call_cost;
430 for (parm = DECL_ARGUMENTS (current_function_decl); parm;
431 parm = DECL_CHAIN (parm))
433 if (!is_gimple_reg (parm))
435 if (bitmap_bit_p (non_ssa_vars, DECL_UID (parm)))
437 if (dump_file && (dump_flags & TDF_DETAILS))
438 fprintf (dump_file,
439 " Refused: need to pass non-ssa param values\n");
440 return;
443 else if (gimple_default_def (cfun, parm)
444 && bitmap_bit_p (current->ssa_names_to_pass,
445 SSA_NAME_VERSION (gimple_default_def
446 (cfun, parm))))
448 if (!VOID_TYPE_P (TREE_TYPE (parm)))
449 call_overhead += estimate_move_cost (TREE_TYPE (parm));
450 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 && TREE_CODE (SSA_NAME_VAR (retval)) == RESULT_DECL
528 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
529 current->split_part_set_retval
530 = bitmap_bit_p (non_ssa_vars, DECL_UID (SSA_NAME_VAR (retval)));
531 else if (TREE_CODE (retval) == SSA_NAME)
532 current->split_part_set_retval
533 = (!SSA_NAME_IS_DEFAULT_DEF (retval)
534 && (bitmap_bit_p (current->split_bbs,
535 gimple_bb (SSA_NAME_DEF_STMT (retval))->index)
536 || gimple_bb (SSA_NAME_DEF_STMT (retval)) == return_bb));
537 else if (TREE_CODE (retval) == PARM_DECL)
538 current->split_part_set_retval = false;
539 else if (TREE_CODE (retval) == VAR_DECL
540 || TREE_CODE (retval) == RESULT_DECL)
541 current->split_part_set_retval
542 = bitmap_bit_p (non_ssa_vars, DECL_UID (retval));
543 else
544 current->split_part_set_retval = true;
546 /* split_function fixes up at most one PHI non-virtual PHI node in return_bb,
547 for the return value. If there are other PHIs, give up. */
548 if (return_bb != EXIT_BLOCK_PTR)
550 gimple_stmt_iterator psi;
552 for (psi = gsi_start_phis (return_bb); !gsi_end_p (psi); gsi_next (&psi))
553 if (is_gimple_reg (gimple_phi_result (gsi_stmt (psi)))
554 && !(retval
555 && current->split_part_set_retval
556 && TREE_CODE (retval) == SSA_NAME
557 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
558 && SSA_NAME_DEF_STMT (retval) == gsi_stmt (psi)))
560 if (dump_file && (dump_flags & TDF_DETAILS))
561 fprintf (dump_file,
562 " Refused: return bb has extra PHIs\n");
563 return;
567 if (dump_file && (dump_flags & TDF_DETAILS))
568 fprintf (dump_file, " Accepted!\n");
570 /* At the moment chose split point with lowest frequency and that leaves
571 out smallest size of header.
572 In future we might re-consider this heuristics. */
573 if (!best_split_point.split_bbs
574 || best_split_point.entry_bb->frequency > current->entry_bb->frequency
575 || (best_split_point.entry_bb->frequency == current->entry_bb->frequency
576 && best_split_point.split_size < current->split_size))
579 if (dump_file && (dump_flags & TDF_DETAILS))
580 fprintf (dump_file, " New best split point!\n");
581 if (best_split_point.ssa_names_to_pass)
583 BITMAP_FREE (best_split_point.ssa_names_to_pass);
584 BITMAP_FREE (best_split_point.split_bbs);
586 best_split_point = *current;
587 best_split_point.ssa_names_to_pass = BITMAP_ALLOC (NULL);
588 bitmap_copy (best_split_point.ssa_names_to_pass,
589 current->ssa_names_to_pass);
590 best_split_point.split_bbs = BITMAP_ALLOC (NULL);
591 bitmap_copy (best_split_point.split_bbs, current->split_bbs);
595 /* Return basic block containing RETURN statement. We allow basic blocks
596 of the form:
597 <retval> = tmp_var;
598 return <retval>
599 but return_bb can not be more complex than this.
600 If nothing is found, return EXIT_BLOCK_PTR.
602 When there are multiple RETURN statement, chose one with return value,
603 since that one is more likely shared by multiple code paths.
605 Return BB is special, because for function splitting it is the only
606 basic block that is duplicated in between header and split part of the
607 function.
609 TODO: We might support multiple return blocks. */
611 static basic_block
612 find_return_bb (void)
614 edge e;
615 basic_block return_bb = EXIT_BLOCK_PTR;
616 gimple_stmt_iterator bsi;
617 bool found_return = false;
618 tree retval = NULL_TREE;
620 if (!single_pred_p (EXIT_BLOCK_PTR))
621 return return_bb;
623 e = single_pred_edge (EXIT_BLOCK_PTR);
624 for (bsi = gsi_last_bb (e->src); !gsi_end_p (bsi); gsi_prev (&bsi))
626 gimple stmt = gsi_stmt (bsi);
627 if (gimple_code (stmt) == GIMPLE_LABEL || is_gimple_debug (stmt))
629 else if (gimple_code (stmt) == GIMPLE_ASSIGN
630 && found_return
631 && gimple_assign_single_p (stmt)
632 && (auto_var_in_fn_p (gimple_assign_rhs1 (stmt),
633 current_function_decl)
634 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
635 && retval == gimple_assign_lhs (stmt))
637 else if (gimple_code (stmt) == GIMPLE_RETURN)
639 found_return = true;
640 retval = gimple_return_retval (stmt);
642 else
643 break;
645 if (gsi_end_p (bsi) && found_return)
646 return_bb = e->src;
648 return return_bb;
651 /* Given return basic block RETURN_BB, see where return value is really
652 stored. */
653 static tree
654 find_retval (basic_block return_bb)
656 gimple_stmt_iterator bsi;
657 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
658 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
659 return gimple_return_retval (gsi_stmt (bsi));
660 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN)
661 return gimple_assign_rhs1 (gsi_stmt (bsi));
662 return NULL;
665 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
666 variable, mark it as used in bitmap passed via DATA.
667 Return true when access to T prevents splitting the function. */
669 static bool
670 mark_nonssa_use (gimple stmt ATTRIBUTE_UNUSED, tree t, void *data)
672 t = get_base_address (t);
674 if (!t || is_gimple_reg (t))
675 return false;
677 /* At present we can't pass non-SSA arguments to split function.
678 FIXME: this can be relaxed by passing references to arguments. */
679 if (TREE_CODE (t) == PARM_DECL)
681 if (dump_file && (dump_flags & TDF_DETAILS))
682 fprintf (dump_file,
683 "Cannot split: use of non-ssa function parameter.\n");
684 return true;
687 if ((TREE_CODE (t) == VAR_DECL
688 && auto_var_in_fn_p (t, current_function_decl))
689 || TREE_CODE (t) == RESULT_DECL
690 || TREE_CODE (t) == LABEL_DECL)
691 bitmap_set_bit ((bitmap)data, DECL_UID (t));
693 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
694 to pretend that the value pointed to is actual result decl. */
695 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
696 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
697 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
698 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
699 return
700 bitmap_bit_p ((bitmap)data,
701 DECL_UID (DECL_RESULT (current_function_decl)));
703 return false;
706 /* Compute local properties of basic block BB we collect when looking for
707 split points. We look for ssa defs and store them in SET_SSA_NAMES,
708 for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic
709 vars stored in NON_SSA_VARS.
711 When BB has edge to RETURN_BB, collect uses in RETURN_BB too.
713 Return false when BB contains something that prevents it from being put into
714 split function. */
716 static bool
717 visit_bb (basic_block bb, basic_block return_bb,
718 bitmap set_ssa_names, bitmap used_ssa_names,
719 bitmap non_ssa_vars)
721 gimple_stmt_iterator bsi;
722 edge e;
723 edge_iterator ei;
724 bool can_split = true;
726 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
728 gimple stmt = gsi_stmt (bsi);
729 tree op;
730 ssa_op_iter iter;
731 tree decl;
733 if (is_gimple_debug (stmt))
734 continue;
736 /* FIXME: We can split regions containing EH. We can not however
737 split RESX, EH_DISPATCH and EH_POINTER referring to same region
738 into different partitions. This would require tracking of
739 EH regions and checking in consider_split_point if they
740 are not used elsewhere. */
741 if (gimple_code (stmt) == GIMPLE_RESX)
743 if (dump_file && (dump_flags & TDF_DETAILS))
744 fprintf (dump_file, "Cannot split: resx.\n");
745 can_split = false;
747 if (gimple_code (stmt) == GIMPLE_EH_DISPATCH)
749 if (dump_file && (dump_flags & TDF_DETAILS))
750 fprintf (dump_file, "Cannot split: eh dispatch.\n");
751 can_split = false;
754 /* Check builtins that prevent splitting. */
755 if (gimple_code (stmt) == GIMPLE_CALL
756 && (decl = gimple_call_fndecl (stmt)) != NULL_TREE
757 && DECL_BUILT_IN (decl)
758 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
759 switch (DECL_FUNCTION_CODE (decl))
761 /* FIXME: once we will allow passing non-parm values to split part,
762 we need to be sure to handle correct builtin_stack_save and
763 builtin_stack_restore. At the moment we are safe; there is no
764 way to store builtin_stack_save result in non-SSA variable
765 since all calls to those are compiler generated. */
766 case BUILT_IN_APPLY:
767 case BUILT_IN_APPLY_ARGS:
768 case BUILT_IN_VA_START:
769 if (dump_file && (dump_flags & TDF_DETAILS))
770 fprintf (dump_file,
771 "Cannot split: builtin_apply and va_start.\n");
772 can_split = false;
773 break;
774 case BUILT_IN_EH_POINTER:
775 if (dump_file && (dump_flags & TDF_DETAILS))
776 fprintf (dump_file, "Cannot split: builtin_eh_pointer.\n");
777 can_split = false;
778 break;
779 default:
780 break;
783 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
784 bitmap_set_bit (set_ssa_names, SSA_NAME_VERSION (op));
785 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
786 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
787 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
788 mark_nonssa_use,
789 mark_nonssa_use,
790 mark_nonssa_use);
792 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
794 gimple stmt = gsi_stmt (bsi);
795 unsigned int i;
797 if (is_gimple_debug (stmt))
798 continue;
799 if (!is_gimple_reg (gimple_phi_result (stmt)))
800 continue;
801 bitmap_set_bit (set_ssa_names,
802 SSA_NAME_VERSION (gimple_phi_result (stmt)));
803 for (i = 0; i < gimple_phi_num_args (stmt); i++)
805 tree op = gimple_phi_arg_def (stmt, i);
806 if (TREE_CODE (op) == SSA_NAME)
807 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
809 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
810 mark_nonssa_use,
811 mark_nonssa_use,
812 mark_nonssa_use);
814 /* Record also uses coming from PHI operand in return BB. */
815 FOR_EACH_EDGE (e, ei, bb->succs)
816 if (e->dest == return_bb)
818 for (bsi = gsi_start_phis (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
820 gimple stmt = gsi_stmt (bsi);
821 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
823 if (is_gimple_debug (stmt))
824 continue;
825 if (!is_gimple_reg (gimple_phi_result (stmt)))
826 continue;
827 if (TREE_CODE (op) == SSA_NAME)
828 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
829 else
830 can_split &= !mark_nonssa_use (stmt, op, non_ssa_vars);
833 return can_split;
836 /* Stack entry for recursive DFS walk in find_split_point. */
838 typedef struct
840 /* Basic block we are examining. */
841 basic_block bb;
843 /* SSA names set and used by the BB and all BBs reachable
844 from it via DFS walk. */
845 bitmap set_ssa_names, used_ssa_names;
846 bitmap non_ssa_vars;
848 /* All BBS visited from this BB via DFS walk. */
849 bitmap bbs_visited;
851 /* Last examined edge in DFS walk. Since we walk unoriented graph,
852 the value is up to sum of incoming and outgoing edges of BB. */
853 unsigned int edge_num;
855 /* Stack entry index of earliest BB reachable from current BB
856 or any BB visited later in DFS walk. */
857 int earliest;
859 /* Overall time and size of all BBs reached from this BB in DFS walk. */
860 int overall_time, overall_size;
862 /* When false we can not split on this BB. */
863 bool can_split;
864 } stack_entry;
865 DEF_VEC_O(stack_entry);
866 DEF_VEC_ALLOC_O(stack_entry,heap);
869 /* Find all articulations and call consider_split on them.
870 OVERALL_TIME and OVERALL_SIZE is time and size of the function.
872 We perform basic algorithm for finding an articulation in a graph
873 created from CFG by considering it to be an unoriented graph.
875 The articulation is discovered via DFS walk. We collect earliest
876 basic block on stack that is reachable via backward edge. Articulation
877 is any basic block such that there is no backward edge bypassing it.
878 To reduce stack usage we maintain heap allocated stack in STACK vector.
879 AUX pointer of BB is set to index it appears in the stack or -1 once
880 it is visited and popped off the stack.
882 The algorithm finds articulation after visiting the whole component
883 reachable by it. This makes it convenient to collect information about
884 the component used by consider_split. */
886 static void
887 find_split_points (int overall_time, int overall_size)
889 stack_entry first;
890 VEC(stack_entry, heap) *stack = NULL;
891 basic_block bb;
892 basic_block return_bb = find_return_bb ();
893 struct split_point current;
895 current.header_time = overall_time;
896 current.header_size = overall_size;
897 current.split_time = 0;
898 current.split_size = 0;
899 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
901 first.bb = ENTRY_BLOCK_PTR;
902 first.edge_num = 0;
903 first.overall_time = 0;
904 first.overall_size = 0;
905 first.earliest = INT_MAX;
906 first.set_ssa_names = 0;
907 first.used_ssa_names = 0;
908 first.bbs_visited = 0;
909 VEC_safe_push (stack_entry, heap, stack, &first);
910 ENTRY_BLOCK_PTR->aux = (void *)(intptr_t)-1;
912 while (!VEC_empty (stack_entry, stack))
914 stack_entry *entry = VEC_last (stack_entry, stack);
916 /* We are walking an acyclic graph, so edge_num counts
917 succ and pred edges together. However when considering
918 articulation, we want to have processed everything reachable
919 from articulation but nothing that reaches into it. */
920 if (entry->edge_num == EDGE_COUNT (entry->bb->succs)
921 && entry->bb != ENTRY_BLOCK_PTR)
923 int pos = VEC_length (stack_entry, stack);
924 entry->can_split &= visit_bb (entry->bb, return_bb,
925 entry->set_ssa_names,
926 entry->used_ssa_names,
927 entry->non_ssa_vars);
928 if (pos <= entry->earliest && !entry->can_split
929 && dump_file && (dump_flags & TDF_DETAILS))
930 fprintf (dump_file,
931 "found articulation at bb %i but can not split\n",
932 entry->bb->index);
933 if (pos <= entry->earliest && entry->can_split)
935 if (dump_file && (dump_flags & TDF_DETAILS))
936 fprintf (dump_file, "found articulation at bb %i\n",
937 entry->bb->index);
938 current.entry_bb = entry->bb;
939 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
940 bitmap_and_compl (current.ssa_names_to_pass,
941 entry->used_ssa_names, entry->set_ssa_names);
942 current.header_time = overall_time - entry->overall_time;
943 current.header_size = overall_size - entry->overall_size;
944 current.split_time = entry->overall_time;
945 current.split_size = entry->overall_size;
946 current.split_bbs = entry->bbs_visited;
947 consider_split (&current, entry->non_ssa_vars, return_bb);
948 BITMAP_FREE (current.ssa_names_to_pass);
951 /* Do actual DFS walk. */
952 if (entry->edge_num
953 < (EDGE_COUNT (entry->bb->succs)
954 + EDGE_COUNT (entry->bb->preds)))
956 edge e;
957 basic_block dest;
958 if (entry->edge_num < EDGE_COUNT (entry->bb->succs))
960 e = EDGE_SUCC (entry->bb, entry->edge_num);
961 dest = e->dest;
963 else
965 e = EDGE_PRED (entry->bb, entry->edge_num
966 - EDGE_COUNT (entry->bb->succs));
967 dest = e->src;
970 entry->edge_num++;
972 /* New BB to visit, push it to the stack. */
973 if (dest != return_bb && dest != EXIT_BLOCK_PTR
974 && !dest->aux)
976 stack_entry new_entry;
978 new_entry.bb = dest;
979 new_entry.edge_num = 0;
980 new_entry.overall_time
981 = VEC_index (bb_info, bb_info_vec, dest->index)->time;
982 new_entry.overall_size
983 = VEC_index (bb_info, bb_info_vec, dest->index)->size;
984 new_entry.earliest = INT_MAX;
985 new_entry.set_ssa_names = BITMAP_ALLOC (NULL);
986 new_entry.used_ssa_names = BITMAP_ALLOC (NULL);
987 new_entry.bbs_visited = BITMAP_ALLOC (NULL);
988 new_entry.non_ssa_vars = BITMAP_ALLOC (NULL);
989 new_entry.can_split = true;
990 bitmap_set_bit (new_entry.bbs_visited, dest->index);
991 VEC_safe_push (stack_entry, heap, stack, &new_entry);
992 dest->aux = (void *)(intptr_t)VEC_length (stack_entry, stack);
994 /* Back edge found, record the earliest point. */
995 else if ((intptr_t)dest->aux > 0
996 && (intptr_t)dest->aux < entry->earliest)
997 entry->earliest = (intptr_t)dest->aux;
999 /* We are done with examining the edges. Pop off the value from stack
1000 and merge stuff we accumulate during the walk. */
1001 else if (entry->bb != ENTRY_BLOCK_PTR)
1003 stack_entry *prev = VEC_index (stack_entry, stack,
1004 VEC_length (stack_entry, stack) - 2);
1006 entry->bb->aux = (void *)(intptr_t)-1;
1007 prev->can_split &= entry->can_split;
1008 if (prev->set_ssa_names)
1010 bitmap_ior_into (prev->set_ssa_names, entry->set_ssa_names);
1011 bitmap_ior_into (prev->used_ssa_names, entry->used_ssa_names);
1012 bitmap_ior_into (prev->bbs_visited, entry->bbs_visited);
1013 bitmap_ior_into (prev->non_ssa_vars, entry->non_ssa_vars);
1015 if (prev->earliest > entry->earliest)
1016 prev->earliest = entry->earliest;
1017 prev->overall_time += entry->overall_time;
1018 prev->overall_size += entry->overall_size;
1019 BITMAP_FREE (entry->set_ssa_names);
1020 BITMAP_FREE (entry->used_ssa_names);
1021 BITMAP_FREE (entry->bbs_visited);
1022 BITMAP_FREE (entry->non_ssa_vars);
1023 VEC_pop (stack_entry, stack);
1025 else
1026 VEC_pop (stack_entry, stack);
1028 ENTRY_BLOCK_PTR->aux = NULL;
1029 FOR_EACH_BB (bb)
1030 bb->aux = NULL;
1031 VEC_free (stack_entry, heap, stack);
1032 BITMAP_FREE (current.ssa_names_to_pass);
1035 /* Split function at SPLIT_POINT. */
1037 static void
1038 split_function (struct split_point *split_point)
1040 VEC (tree, heap) *args_to_pass = NULL;
1041 bitmap args_to_skip;
1042 tree parm;
1043 int num = 0;
1044 struct cgraph_node *node, *cur_node = cgraph_get_node (current_function_decl);
1045 basic_block return_bb = find_return_bb ();
1046 basic_block call_bb;
1047 gimple_stmt_iterator gsi;
1048 gimple call;
1049 edge e;
1050 edge_iterator ei;
1051 tree retval = NULL, real_retval = NULL;
1052 bool split_part_return_p = false;
1053 gimple last_stmt = NULL;
1054 unsigned int i;
1055 tree arg;
1057 if (dump_file)
1059 fprintf (dump_file, "\n\nSplitting function at:\n");
1060 dump_split_point (dump_file, split_point);
1063 if (cur_node->local.can_change_signature)
1064 args_to_skip = BITMAP_ALLOC (NULL);
1065 else
1066 args_to_skip = NULL;
1068 /* Collect the parameters of new function and args_to_skip bitmap. */
1069 for (parm = DECL_ARGUMENTS (current_function_decl);
1070 parm; parm = DECL_CHAIN (parm), num++)
1071 if (args_to_skip
1072 && (!is_gimple_reg (parm)
1073 || !gimple_default_def (cfun, parm)
1074 || !bitmap_bit_p (split_point->ssa_names_to_pass,
1075 SSA_NAME_VERSION (gimple_default_def (cfun,
1076 parm)))))
1077 bitmap_set_bit (args_to_skip, num);
1078 else
1080 /* This parm might not have been used up to now, but is going to be
1081 used, hence register it. */
1082 add_referenced_var (parm);
1083 if (is_gimple_reg (parm))
1085 arg = gimple_default_def (cfun, parm);
1086 if (!arg)
1088 arg = make_ssa_name (parm, gimple_build_nop ());
1089 set_default_def (parm, arg);
1092 else
1093 arg = parm;
1095 if (!useless_type_conversion_p (DECL_ARG_TYPE (parm), TREE_TYPE (arg)))
1096 arg = fold_convert (DECL_ARG_TYPE (parm), arg);
1097 VEC_safe_push (tree, heap, args_to_pass, arg);
1100 /* See if the split function will return. */
1101 FOR_EACH_EDGE (e, ei, return_bb->preds)
1102 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1103 break;
1104 if (e)
1105 split_part_return_p = true;
1107 /* Add return block to what will become the split function.
1108 We do not return; no return block is needed. */
1109 if (!split_part_return_p)
1111 /* We have no return block, so nothing is needed. */
1112 else if (return_bb == EXIT_BLOCK_PTR)
1114 /* When we do not want to return value, we need to construct
1115 new return block with empty return statement.
1116 FIXME: Once we are able to change return type, we should change function
1117 to return void instead of just outputting function with undefined return
1118 value. For structures this affects quality of codegen. */
1119 else if (!split_point->split_part_set_retval
1120 && find_retval (return_bb))
1122 bool redirected = true;
1123 basic_block new_return_bb = create_basic_block (NULL, 0, return_bb);
1124 gimple_stmt_iterator gsi = gsi_start_bb (new_return_bb);
1125 gsi_insert_after (&gsi, gimple_build_return (NULL), GSI_NEW_STMT);
1126 while (redirected)
1128 redirected = false;
1129 FOR_EACH_EDGE (e, ei, return_bb->preds)
1130 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1132 new_return_bb->count += e->count;
1133 new_return_bb->frequency += EDGE_FREQUENCY (e);
1134 redirect_edge_and_branch (e, new_return_bb);
1135 redirected = true;
1136 break;
1139 e = make_edge (new_return_bb, EXIT_BLOCK_PTR, 0);
1140 e->probability = REG_BR_PROB_BASE;
1141 e->count = new_return_bb->count;
1142 bitmap_set_bit (split_point->split_bbs, new_return_bb->index);
1144 /* When we pass around the value, use existing return block. */
1145 else
1146 bitmap_set_bit (split_point->split_bbs, return_bb->index);
1148 /* If RETURN_BB has virtual operand PHIs, they must be removed and the
1149 virtual operand marked for renaming as we change the CFG in a way that
1150 tree-inline is not able to compensate for.
1152 Note this can happen whether or not we have a return value. If we have
1153 a return value, then RETURN_BB may have PHIs for real operands too. */
1154 if (return_bb != EXIT_BLOCK_PTR)
1156 bool phi_p = false;
1157 for (gsi = gsi_start_phis (return_bb); !gsi_end_p (gsi);)
1159 gimple stmt = gsi_stmt (gsi);
1160 if (is_gimple_reg (gimple_phi_result (stmt)))
1162 gsi_next (&gsi);
1163 continue;
1165 mark_virtual_phi_result_for_renaming (stmt);
1166 remove_phi_node (&gsi, true);
1167 phi_p = true;
1169 /* In reality we have to rename the reaching definition of the
1170 virtual operand at return_bb as we will eventually release it
1171 when we remove the code region we outlined.
1172 So we have to rename all immediate virtual uses of that region
1173 if we didn't see a PHI definition yet. */
1174 /* ??? In real reality we want to set the reaching vdef of the
1175 entry of the SESE region as the vuse of the call and the reaching
1176 vdef of the exit of the SESE region as the vdef of the call. */
1177 if (!phi_p)
1178 for (gsi = gsi_start_bb (return_bb); !gsi_end_p (gsi); gsi_next (&gsi))
1180 gimple stmt = gsi_stmt (gsi);
1181 if (gimple_vuse (stmt))
1183 gimple_set_vuse (stmt, NULL_TREE);
1184 update_stmt (stmt);
1186 if (gimple_vdef (stmt))
1187 break;
1191 /* Now create the actual clone. */
1192 rebuild_cgraph_edges ();
1193 node = cgraph_function_versioning (cur_node, NULL, NULL, args_to_skip,
1194 !split_part_return_p,
1195 split_point->split_bbs,
1196 split_point->entry_bb, "part");
1197 /* For usual cloning it is enough to clear builtin only when signature
1198 changes. For partial inlining we however can not expect the part
1199 of builtin implementation to have same semantic as the whole. */
1200 if (DECL_BUILT_IN (node->decl))
1202 DECL_BUILT_IN_CLASS (node->decl) = NOT_BUILT_IN;
1203 DECL_FUNCTION_CODE (node->decl) = (enum built_in_function) 0;
1205 cgraph_node_remove_callees (cur_node);
1206 if (!split_part_return_p)
1207 TREE_THIS_VOLATILE (node->decl) = 1;
1208 if (dump_file)
1209 dump_function_to_file (node->decl, dump_file, dump_flags);
1211 /* Create the basic block we place call into. It is the entry basic block
1212 split after last label. */
1213 call_bb = split_point->entry_bb;
1214 for (gsi = gsi_start_bb (call_bb); !gsi_end_p (gsi);)
1215 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
1217 last_stmt = gsi_stmt (gsi);
1218 gsi_next (&gsi);
1220 else
1221 break;
1222 e = split_block (split_point->entry_bb, last_stmt);
1223 remove_edge (e);
1225 /* Produce the call statement. */
1226 gsi = gsi_last_bb (call_bb);
1227 FOR_EACH_VEC_ELT (tree, args_to_pass, i, arg)
1228 if (!is_gimple_val (arg))
1230 arg = force_gimple_operand_gsi (&gsi, arg, true, NULL_TREE,
1231 false, GSI_CONTINUE_LINKING);
1232 VEC_replace (tree, args_to_pass, i, arg);
1234 call = gimple_build_call_vec (node->decl, args_to_pass);
1235 gimple_set_block (call, DECL_INITIAL (current_function_decl));
1237 /* We avoid address being taken on any variable used by split part,
1238 so return slot optimization is always possible. Moreover this is
1239 required to make DECL_BY_REFERENCE work. */
1240 if (aggregate_value_p (DECL_RESULT (current_function_decl),
1241 TREE_TYPE (current_function_decl)))
1242 gimple_call_set_return_slot_opt (call, true);
1244 /* Update return value. This is bit tricky. When we do not return,
1245 do nothing. When we return we might need to update return_bb
1246 or produce a new return statement. */
1247 if (!split_part_return_p)
1248 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1249 else
1251 e = make_edge (call_bb, return_bb,
1252 return_bb == EXIT_BLOCK_PTR ? 0 : EDGE_FALLTHRU);
1253 e->count = call_bb->count;
1254 e->probability = REG_BR_PROB_BASE;
1256 /* If there is return basic block, see what value we need to store
1257 return value into and put call just before it. */
1258 if (return_bb != EXIT_BLOCK_PTR)
1260 real_retval = retval = find_retval (return_bb);
1262 if (real_retval && split_point->split_part_set_retval)
1264 gimple_stmt_iterator psi;
1266 /* See if we need new SSA_NAME for the result.
1267 When DECL_BY_REFERENCE is true, retval is actually pointer to
1268 return value and it is constant in whole function. */
1269 if (TREE_CODE (retval) == SSA_NAME
1270 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1272 retval = make_ssa_name (SSA_NAME_VAR (retval), call);
1274 /* See if there is PHI defining return value. */
1275 for (psi = gsi_start_phis (return_bb);
1276 !gsi_end_p (psi); gsi_next (&psi))
1277 if (is_gimple_reg (gimple_phi_result (gsi_stmt (psi))))
1278 break;
1280 /* When there is PHI, just update its value. */
1281 if (TREE_CODE (retval) == SSA_NAME
1282 && !gsi_end_p (psi))
1283 add_phi_arg (gsi_stmt (psi), retval, e, UNKNOWN_LOCATION);
1284 /* Otherwise update the return BB itself.
1285 find_return_bb allows at most one assignment to return value,
1286 so update first statement. */
1287 else
1289 gimple_stmt_iterator bsi;
1290 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi);
1291 gsi_next (&bsi))
1292 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
1294 gimple_return_set_retval (gsi_stmt (bsi), retval);
1295 break;
1297 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN)
1299 gimple_assign_set_rhs1 (gsi_stmt (bsi), retval);
1300 break;
1302 update_stmt (gsi_stmt (bsi));
1305 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1307 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1308 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1310 else
1312 tree restype;
1313 restype = TREE_TYPE (DECL_RESULT (current_function_decl));
1314 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1315 if (!useless_type_conversion_p (TREE_TYPE (retval), restype))
1317 gimple cpy;
1318 tree tem = create_tmp_reg (restype, NULL);
1319 tem = make_ssa_name (tem, call);
1320 cpy = gimple_build_assign_with_ops (NOP_EXPR, retval,
1321 tem, NULL_TREE);
1322 gsi_insert_after (&gsi, cpy, GSI_NEW_STMT);
1323 retval = tem;
1325 gimple_call_set_lhs (call, retval);
1326 update_stmt (call);
1329 else
1330 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1332 /* We don't use return block (there is either no return in function or
1333 multiple of them). So create new basic block with return statement.
1335 else
1337 gimple ret;
1338 if (split_point->split_part_set_retval
1339 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1341 retval = DECL_RESULT (current_function_decl);
1343 /* We use temporary register to hold value when aggregate_value_p
1344 is false. Similarly for DECL_BY_REFERENCE we must avoid extra
1345 copy. */
1346 if (!aggregate_value_p (retval, TREE_TYPE (current_function_decl))
1347 && !DECL_BY_REFERENCE (retval))
1348 retval = create_tmp_reg (TREE_TYPE (retval), NULL);
1349 if (is_gimple_reg (retval))
1351 /* When returning by reference, there is only one SSA name
1352 assigned to RESULT_DECL (that is pointer to return value).
1353 Look it up or create new one if it is missing. */
1354 if (DECL_BY_REFERENCE (retval))
1356 tree retval_name;
1357 if ((retval_name = gimple_default_def (cfun, retval))
1358 != NULL)
1359 retval = retval_name;
1360 else
1362 retval_name = make_ssa_name (retval,
1363 gimple_build_nop ());
1364 set_default_def (retval, retval_name);
1365 retval = retval_name;
1368 /* Otherwise produce new SSA name for return value. */
1369 else
1370 retval = make_ssa_name (retval, call);
1372 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1373 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1374 else
1375 gimple_call_set_lhs (call, retval);
1377 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1378 ret = gimple_build_return (retval);
1379 gsi_insert_after (&gsi, ret, GSI_NEW_STMT);
1382 free_dominance_info (CDI_DOMINATORS);
1383 free_dominance_info (CDI_POST_DOMINATORS);
1384 compute_inline_parameters (node, true);
1387 /* Execute function splitting pass. */
1389 static unsigned int
1390 execute_split_functions (void)
1392 gimple_stmt_iterator bsi;
1393 basic_block bb;
1394 int overall_time = 0, overall_size = 0;
1395 int todo = 0;
1396 struct cgraph_node *node = cgraph_get_node (current_function_decl);
1398 if (flags_from_decl_or_type (current_function_decl) & ECF_NORETURN)
1400 if (dump_file)
1401 fprintf (dump_file, "Not splitting: noreturn function.\n");
1402 return 0;
1404 if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
1406 if (dump_file)
1407 fprintf (dump_file, "Not splitting: main function.\n");
1408 return 0;
1410 /* This can be relaxed; function might become inlinable after splitting
1411 away the uninlinable part. */
1412 if (!inline_summary (node)->inlinable)
1414 if (dump_file)
1415 fprintf (dump_file, "Not splitting: not inlinable.\n");
1416 return 0;
1418 if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1420 if (dump_file)
1421 fprintf (dump_file, "Not splitting: disregarding inline limits.\n");
1422 return 0;
1424 /* This can be relaxed; most of versioning tests actually prevents
1425 a duplication. */
1426 if (!tree_versionable_function_p (current_function_decl))
1428 if (dump_file)
1429 fprintf (dump_file, "Not splitting: not versionable.\n");
1430 return 0;
1432 /* FIXME: we could support this. */
1433 if (DECL_STRUCT_FUNCTION (current_function_decl)->static_chain_decl)
1435 if (dump_file)
1436 fprintf (dump_file, "Not splitting: nested function.\n");
1437 return 0;
1440 /* See if it makes sense to try to split.
1441 It makes sense to split if we inline, that is if we have direct calls to
1442 handle or direct calls are possibly going to appear as result of indirect
1443 inlining or LTO. Also handle -fprofile-generate as LTO to allow non-LTO
1444 training for LTO -fprofile-use build.
1446 Note that we are not completely conservative about disqualifying functions
1447 called once. It is possible that the caller is called more then once and
1448 then inlining would still benefit. */
1449 if ((!node->callers || !node->callers->next_caller)
1450 && !node->address_taken
1451 && (!flag_lto || !node->local.externally_visible))
1453 if (dump_file)
1454 fprintf (dump_file, "Not splitting: not called directly "
1455 "or called once.\n");
1456 return 0;
1459 /* FIXME: We can actually split if splitting reduces call overhead. */
1460 if (!flag_inline_small_functions
1461 && !DECL_DECLARED_INLINE_P (current_function_decl))
1463 if (dump_file)
1464 fprintf (dump_file, "Not splitting: not autoinlining and function"
1465 " is not inline.\n");
1466 return 0;
1469 /* Initialize bitmap to track forbidden calls. */
1470 forbidden_dominators = BITMAP_ALLOC (NULL);
1471 calculate_dominance_info (CDI_DOMINATORS);
1473 /* Compute local info about basic blocks and determine function size/time. */
1474 VEC_safe_grow_cleared (bb_info, heap, bb_info_vec, last_basic_block + 1);
1475 memset (&best_split_point, 0, sizeof (best_split_point));
1476 FOR_EACH_BB (bb)
1478 int time = 0;
1479 int size = 0;
1480 int freq = compute_call_stmt_bb_frequency (current_function_decl, bb);
1482 if (dump_file && (dump_flags & TDF_DETAILS))
1483 fprintf (dump_file, "Basic block %i\n", bb->index);
1485 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1487 int this_time, this_size;
1488 gimple stmt = gsi_stmt (bsi);
1490 this_size = estimate_num_insns (stmt, &eni_size_weights);
1491 this_time = estimate_num_insns (stmt, &eni_time_weights) * freq;
1492 size += this_size;
1493 time += this_time;
1494 check_forbidden_calls (stmt);
1496 if (dump_file && (dump_flags & TDF_DETAILS))
1498 fprintf (dump_file, " freq:%6i size:%3i time:%3i ",
1499 freq, this_size, this_time);
1500 print_gimple_stmt (dump_file, stmt, 0, 0);
1503 overall_time += time;
1504 overall_size += size;
1505 VEC_index (bb_info, bb_info_vec, bb->index)->time = time;
1506 VEC_index (bb_info, bb_info_vec, bb->index)->size = size;
1508 find_split_points (overall_time, overall_size);
1509 if (best_split_point.split_bbs)
1511 split_function (&best_split_point);
1512 BITMAP_FREE (best_split_point.ssa_names_to_pass);
1513 BITMAP_FREE (best_split_point.split_bbs);
1514 todo = TODO_update_ssa | TODO_cleanup_cfg;
1516 BITMAP_FREE (forbidden_dominators);
1517 VEC_free (bb_info, heap, bb_info_vec);
1518 bb_info_vec = NULL;
1519 return todo;
1522 /* Gate function splitting pass. When doing profile feedback, we want
1523 to execute the pass after profiling is read. So disable one in
1524 early optimization. */
1526 static bool
1527 gate_split_functions (void)
1529 return (flag_partial_inlining
1530 && !profile_arc_flag && !flag_branch_probabilities);
1533 struct gimple_opt_pass pass_split_functions =
1536 GIMPLE_PASS,
1537 "fnsplit", /* name */
1538 gate_split_functions, /* gate */
1539 execute_split_functions, /* execute */
1540 NULL, /* sub */
1541 NULL, /* next */
1542 0, /* static_pass_number */
1543 TV_IPA_FNSPLIT, /* tv_id */
1544 PROP_cfg, /* properties_required */
1545 0, /* properties_provided */
1546 0, /* properties_destroyed */
1547 0, /* todo_flags_start */
1548 TODO_verify_all /* todo_flags_finish */
1552 /* Gate feedback driven function splitting pass.
1553 We don't need to split when profiling at all, we are producing
1554 lousy code anyway. */
1556 static bool
1557 gate_feedback_split_functions (void)
1559 return (flag_partial_inlining
1560 && flag_branch_probabilities);
1563 /* Execute function splitting pass. */
1565 static unsigned int
1566 execute_feedback_split_functions (void)
1568 unsigned int retval = execute_split_functions ();
1569 if (retval)
1570 retval |= TODO_rebuild_cgraph_edges;
1571 return retval;
1574 struct gimple_opt_pass pass_feedback_split_functions =
1577 GIMPLE_PASS,
1578 "feedback_fnsplit", /* name */
1579 gate_feedback_split_functions, /* gate */
1580 execute_feedback_split_functions, /* execute */
1581 NULL, /* sub */
1582 NULL, /* next */
1583 0, /* static_pass_number */
1584 TV_IPA_FNSPLIT, /* tv_id */
1585 PROP_cfg, /* properties_required */
1586 0, /* properties_provided */
1587 0, /* properties_destroyed */
1588 0, /* todo_flags_start */
1589 TODO_verify_all /* todo_flags_finish */