gcc/c-family/
[official-gcc.git] / gcc / ipa-split.c
blobe55b3f59dbcc4d0463b45c6164027a9ee005448b
1 /* Function splitting pass
2 Copyright (C) 2010-2013 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka <jh@suse.cz>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* The purpose of this pass is to split function bodies to improve
22 inlining. I.e. for function of the form:
24 func (...)
26 if (cheap_test)
27 something_small
28 else
29 something_big
32 Produce:
34 func.part (...)
36 something_big
39 func (...)
41 if (cheap_test)
42 something_small
43 else
44 func.part (...);
47 When func becomes inlinable and when cheap_test is often true, inlining func,
48 but not fund.part leads to performance improvement similar as inlining
49 original func while the code size growth is smaller.
51 The pass is organized in three stages:
52 1) Collect local info about basic block into BB_INFO structure and
53 compute function body estimated size and time.
54 2) Via DFS walk find all possible basic blocks where we can split
55 and chose best one.
56 3) If split point is found, split at the specified BB by creating a clone
57 and updating function to call it.
59 The decisions what functions to split are in execute_split_functions
60 and consider_split.
62 There are several possible future improvements for this pass including:
64 1) Splitting to break up large functions
65 2) Splitting to reduce stack frame usage
66 3) Allow split part of function to use values computed in the header part.
67 The values needs to be passed to split function, perhaps via same
68 interface as for nested functions or as argument.
69 4) Support for simple rematerialization. I.e. when split part use
70 value computed in header from function parameter in very cheap way, we
71 can just recompute it.
72 5) Support splitting of nested functions.
73 6) Support non-SSA arguments.
74 7) There is nothing preventing us from producing multiple parts of single function
75 when needed or splitting also the parts. */
77 #include "config.h"
78 #include "system.h"
79 #include "coretypes.h"
80 #include "tree.h"
81 #include "gimple.h"
82 #include "gimplify.h"
83 #include "gimple-iterator.h"
84 #include "gimplify-me.h"
85 #include "gimple-walk.h"
86 #include "target.h"
87 #include "ipa-prop.h"
88 #include "gimple-ssa.h"
89 #include "tree-cfg.h"
90 #include "tree-phinodes.h"
91 #include "ssa-iterators.h"
92 #include "tree-ssanames.h"
93 #include "tree-into-ssa.h"
94 #include "tree-dfa.h"
95 #include "tree-pass.h"
96 #include "flags.h"
97 #include "diagnostic.h"
98 #include "tree-dump.h"
99 #include "tree-inline.h"
100 #include "params.h"
101 #include "gimple-pretty-print.h"
102 #include "ipa-inline.h"
103 #include "cfgloop.h"
105 /* Per basic block info. */
107 typedef struct
109 unsigned int size;
110 unsigned int time;
111 } bb_info;
113 static vec<bb_info> bb_info_vec;
115 /* Description of split point. */
117 struct split_point
119 /* Size of the partitions. */
120 unsigned int header_time, header_size, split_time, split_size;
122 /* SSA names that need to be passed into spit function. */
123 bitmap ssa_names_to_pass;
125 /* Basic block where we split (that will become entry point of new function. */
126 basic_block entry_bb;
128 /* Basic blocks we are splitting away. */
129 bitmap split_bbs;
131 /* True when return value is computed on split part and thus it needs
132 to be returned. */
133 bool split_part_set_retval;
136 /* Best split point found. */
138 struct split_point best_split_point;
140 /* Set of basic blocks that are not allowed to dominate a split point. */
142 static bitmap forbidden_dominators;
144 static tree find_retval (basic_block return_bb);
146 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
147 variable, check it if it is present in bitmap passed via DATA. */
149 static bool
150 test_nonssa_use (gimple stmt ATTRIBUTE_UNUSED, tree t, void *data)
152 t = get_base_address (t);
154 if (!t || is_gimple_reg (t))
155 return false;
157 if (TREE_CODE (t) == PARM_DECL
158 || (TREE_CODE (t) == VAR_DECL
159 && auto_var_in_fn_p (t, current_function_decl))
160 || TREE_CODE (t) == RESULT_DECL
161 || TREE_CODE (t) == LABEL_DECL)
162 return bitmap_bit_p ((bitmap)data, DECL_UID (t));
164 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
165 to pretend that the value pointed to is actual result decl. */
166 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
167 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
168 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
169 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
170 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
171 return
172 bitmap_bit_p ((bitmap)data,
173 DECL_UID (DECL_RESULT (current_function_decl)));
175 return false;
178 /* Dump split point CURRENT. */
180 static void
181 dump_split_point (FILE * file, struct split_point *current)
183 fprintf (file,
184 "Split point at BB %i\n"
185 " header time: %i header size: %i\n"
186 " split time: %i split size: %i\n bbs: ",
187 current->entry_bb->index, current->header_time,
188 current->header_size, current->split_time, current->split_size);
189 dump_bitmap (file, current->split_bbs);
190 fprintf (file, " SSA names to pass: ");
191 dump_bitmap (file, current->ssa_names_to_pass);
194 /* Look for all BBs in header that might lead to the split part and verify
195 that they are not defining any non-SSA var used by the split part.
196 Parameters are the same as for consider_split. */
198 static bool
199 verify_non_ssa_vars (struct split_point *current, bitmap non_ssa_vars,
200 basic_block return_bb)
202 bitmap seen = BITMAP_ALLOC (NULL);
203 vec<basic_block> worklist = vNULL;
204 edge e;
205 edge_iterator ei;
206 bool ok = true;
208 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
209 if (e->src != ENTRY_BLOCK_PTR
210 && !bitmap_bit_p (current->split_bbs, e->src->index))
212 worklist.safe_push (e->src);
213 bitmap_set_bit (seen, e->src->index);
216 while (!worklist.is_empty ())
218 gimple_stmt_iterator bsi;
219 basic_block bb = worklist.pop ();
221 FOR_EACH_EDGE (e, ei, bb->preds)
222 if (e->src != ENTRY_BLOCK_PTR
223 && bitmap_set_bit (seen, e->src->index))
225 gcc_checking_assert (!bitmap_bit_p (current->split_bbs,
226 e->src->index));
227 worklist.safe_push (e->src);
229 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
231 gimple stmt = gsi_stmt (bsi);
232 if (is_gimple_debug (stmt))
233 continue;
234 if (walk_stmt_load_store_addr_ops
235 (stmt, non_ssa_vars, test_nonssa_use, test_nonssa_use,
236 test_nonssa_use))
238 ok = false;
239 goto done;
241 if (gimple_code (stmt) == GIMPLE_LABEL
242 && test_nonssa_use (stmt, gimple_label_label (stmt),
243 non_ssa_vars))
245 ok = false;
246 goto done;
249 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
251 if (walk_stmt_load_store_addr_ops
252 (gsi_stmt (bsi), non_ssa_vars, test_nonssa_use, test_nonssa_use,
253 test_nonssa_use))
255 ok = false;
256 goto done;
259 FOR_EACH_EDGE (e, ei, bb->succs)
261 if (e->dest != return_bb)
262 continue;
263 for (bsi = gsi_start_phis (return_bb); !gsi_end_p (bsi);
264 gsi_next (&bsi))
266 gimple stmt = gsi_stmt (bsi);
267 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
269 if (virtual_operand_p (gimple_phi_result (stmt)))
270 continue;
271 if (TREE_CODE (op) != SSA_NAME
272 && test_nonssa_use (stmt, op, non_ssa_vars))
274 ok = false;
275 goto done;
280 done:
281 BITMAP_FREE (seen);
282 worklist.release ();
283 return ok;
286 /* If STMT is a call, check the callee against a list of forbidden
287 predicate functions. If a match is found, look for uses of the
288 call result in condition statements that compare against zero.
289 For each such use, find the block targeted by the condition
290 statement for the nonzero result, and set the bit for this block
291 in the forbidden dominators bitmap. The purpose of this is to avoid
292 selecting a split point where we are likely to lose the chance
293 to optimize away an unused function call. */
295 static void
296 check_forbidden_calls (gimple stmt)
298 imm_use_iterator use_iter;
299 use_operand_p use_p;
300 tree lhs;
302 /* At the moment, __builtin_constant_p is the only forbidden
303 predicate function call (see PR49642). */
304 if (!gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P))
305 return;
307 lhs = gimple_call_lhs (stmt);
309 if (!lhs || TREE_CODE (lhs) != SSA_NAME)
310 return;
312 FOR_EACH_IMM_USE_FAST (use_p, use_iter, lhs)
314 tree op1;
315 basic_block use_bb, forbidden_bb;
316 enum tree_code code;
317 edge true_edge, false_edge;
318 gimple use_stmt = USE_STMT (use_p);
320 if (gimple_code (use_stmt) != GIMPLE_COND)
321 continue;
323 /* Assuming canonical form for GIMPLE_COND here, with constant
324 in second position. */
325 op1 = gimple_cond_rhs (use_stmt);
326 code = gimple_cond_code (use_stmt);
327 use_bb = gimple_bb (use_stmt);
329 extract_true_false_edges_from_block (use_bb, &true_edge, &false_edge);
331 /* We're only interested in comparisons that distinguish
332 unambiguously from zero. */
333 if (!integer_zerop (op1) || code == LE_EXPR || code == GE_EXPR)
334 continue;
336 if (code == EQ_EXPR)
337 forbidden_bb = false_edge->dest;
338 else
339 forbidden_bb = true_edge->dest;
341 bitmap_set_bit (forbidden_dominators, forbidden_bb->index);
345 /* If BB is dominated by any block in the forbidden dominators set,
346 return TRUE; else FALSE. */
348 static bool
349 dominated_by_forbidden (basic_block bb)
351 unsigned dom_bb;
352 bitmap_iterator bi;
354 EXECUTE_IF_SET_IN_BITMAP (forbidden_dominators, 1, dom_bb, bi)
356 if (dominated_by_p (CDI_DOMINATORS, bb, BASIC_BLOCK (dom_bb)))
357 return true;
360 return false;
363 /* We found an split_point CURRENT. NON_SSA_VARS is bitmap of all non ssa
364 variables used and RETURN_BB is return basic block.
365 See if we can split function here. */
367 static void
368 consider_split (struct split_point *current, bitmap non_ssa_vars,
369 basic_block return_bb)
371 tree parm;
372 unsigned int num_args = 0;
373 unsigned int call_overhead;
374 edge e;
375 edge_iterator ei;
376 gimple_stmt_iterator bsi;
377 unsigned int i;
378 int incoming_freq = 0;
379 tree retval;
380 bool back_edge = false;
382 if (dump_file && (dump_flags & TDF_DETAILS))
383 dump_split_point (dump_file, current);
385 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
387 if (e->flags & EDGE_DFS_BACK)
388 back_edge = true;
389 if (!bitmap_bit_p (current->split_bbs, e->src->index))
390 incoming_freq += EDGE_FREQUENCY (e);
393 /* Do not split when we would end up calling function anyway. */
394 if (incoming_freq
395 >= (ENTRY_BLOCK_PTR->frequency
396 * PARAM_VALUE (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY) / 100))
398 /* When profile is guessed, we can not expect it to give us
399 realistic estimate on likelyness of function taking the
400 complex path. As a special case, when tail of the function is
401 a loop, enable splitting since inlining code skipping the loop
402 is likely noticeable win. */
403 if (back_edge
404 && profile_status != PROFILE_READ
405 && incoming_freq < ENTRY_BLOCK_PTR->frequency)
407 if (dump_file && (dump_flags & TDF_DETAILS))
408 fprintf (dump_file,
409 " Split before loop, accepting despite low frequencies %i %i.\n",
410 incoming_freq,
411 ENTRY_BLOCK_PTR->frequency);
413 else
415 if (dump_file && (dump_flags & TDF_DETAILS))
416 fprintf (dump_file,
417 " Refused: incoming frequency is too large.\n");
418 return;
422 if (!current->header_size)
424 if (dump_file && (dump_flags & TDF_DETAILS))
425 fprintf (dump_file, " Refused: header empty\n");
426 return;
429 /* Verify that PHI args on entry are either virtual or all their operands
430 incoming from header are the same. */
431 for (bsi = gsi_start_phis (current->entry_bb); !gsi_end_p (bsi); gsi_next (&bsi))
433 gimple stmt = gsi_stmt (bsi);
434 tree val = NULL;
436 if (virtual_operand_p (gimple_phi_result (stmt)))
437 continue;
438 for (i = 0; i < gimple_phi_num_args (stmt); i++)
440 edge e = gimple_phi_arg_edge (stmt, i);
441 if (!bitmap_bit_p (current->split_bbs, e->src->index))
443 tree edge_val = gimple_phi_arg_def (stmt, i);
444 if (val && edge_val != val)
446 if (dump_file && (dump_flags & TDF_DETAILS))
447 fprintf (dump_file,
448 " Refused: entry BB has PHI with multiple variants\n");
449 return;
451 val = edge_val;
457 /* See what argument we will pass to the split function and compute
458 call overhead. */
459 call_overhead = eni_size_weights.call_cost;
460 for (parm = DECL_ARGUMENTS (current_function_decl); parm;
461 parm = DECL_CHAIN (parm))
463 if (!is_gimple_reg (parm))
465 if (bitmap_bit_p (non_ssa_vars, DECL_UID (parm)))
467 if (dump_file && (dump_flags & TDF_DETAILS))
468 fprintf (dump_file,
469 " Refused: need to pass non-ssa param values\n");
470 return;
473 else
475 tree ddef = ssa_default_def (cfun, parm);
476 if (ddef
477 && bitmap_bit_p (current->ssa_names_to_pass,
478 SSA_NAME_VERSION (ddef)))
480 if (!VOID_TYPE_P (TREE_TYPE (parm)))
481 call_overhead += estimate_move_cost (TREE_TYPE (parm));
482 num_args++;
486 if (!VOID_TYPE_P (TREE_TYPE (current_function_decl)))
487 call_overhead += estimate_move_cost (TREE_TYPE (current_function_decl));
489 if (current->split_size <= call_overhead)
491 if (dump_file && (dump_flags & TDF_DETAILS))
492 fprintf (dump_file,
493 " Refused: split size is smaller than call overhead\n");
494 return;
496 if (current->header_size + call_overhead
497 >= (unsigned int)(DECL_DECLARED_INLINE_P (current_function_decl)
498 ? MAX_INLINE_INSNS_SINGLE
499 : MAX_INLINE_INSNS_AUTO))
501 if (dump_file && (dump_flags & TDF_DETAILS))
502 fprintf (dump_file,
503 " Refused: header size is too large for inline candidate\n");
504 return;
507 /* FIXME: we currently can pass only SSA function parameters to the split
508 arguments. Once parm_adjustment infrastructure is supported by cloning,
509 we can pass more than that. */
510 if (num_args != bitmap_count_bits (current->ssa_names_to_pass))
513 if (dump_file && (dump_flags & TDF_DETAILS))
514 fprintf (dump_file,
515 " Refused: need to pass non-param values\n");
516 return;
519 /* When there are non-ssa vars used in the split region, see if they
520 are used in the header region. If so, reject the split.
521 FIXME: we can use nested function support to access both. */
522 if (!bitmap_empty_p (non_ssa_vars)
523 && !verify_non_ssa_vars (current, non_ssa_vars, return_bb))
525 if (dump_file && (dump_flags & TDF_DETAILS))
526 fprintf (dump_file,
527 " Refused: split part has non-ssa uses\n");
528 return;
531 /* If the split point is dominated by a forbidden block, reject
532 the split. */
533 if (!bitmap_empty_p (forbidden_dominators)
534 && dominated_by_forbidden (current->entry_bb))
536 if (dump_file && (dump_flags & TDF_DETAILS))
537 fprintf (dump_file,
538 " Refused: split point dominated by forbidden block\n");
539 return;
542 /* See if retval used by return bb is computed by header or split part.
543 When it is computed by split part, we need to produce return statement
544 in the split part and add code to header to pass it around.
546 This is bit tricky to test:
547 1) When there is no return_bb or no return value, we always pass
548 value around.
549 2) Invariants are always computed by caller.
550 3) For SSA we need to look if defining statement is in header or split part
551 4) For non-SSA we need to look where the var is computed. */
552 retval = find_retval (return_bb);
553 if (!retval)
554 current->split_part_set_retval = true;
555 else if (is_gimple_min_invariant (retval))
556 current->split_part_set_retval = false;
557 /* Special case is value returned by reference we record as if it was non-ssa
558 set to result_decl. */
559 else if (TREE_CODE (retval) == SSA_NAME
560 && SSA_NAME_VAR (retval)
561 && TREE_CODE (SSA_NAME_VAR (retval)) == RESULT_DECL
562 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
563 current->split_part_set_retval
564 = bitmap_bit_p (non_ssa_vars, DECL_UID (SSA_NAME_VAR (retval)));
565 else if (TREE_CODE (retval) == SSA_NAME)
566 current->split_part_set_retval
567 = (!SSA_NAME_IS_DEFAULT_DEF (retval)
568 && (bitmap_bit_p (current->split_bbs,
569 gimple_bb (SSA_NAME_DEF_STMT (retval))->index)
570 || gimple_bb (SSA_NAME_DEF_STMT (retval)) == return_bb));
571 else if (TREE_CODE (retval) == PARM_DECL)
572 current->split_part_set_retval = false;
573 else if (TREE_CODE (retval) == VAR_DECL
574 || TREE_CODE (retval) == RESULT_DECL)
575 current->split_part_set_retval
576 = bitmap_bit_p (non_ssa_vars, DECL_UID (retval));
577 else
578 current->split_part_set_retval = true;
580 /* split_function fixes up at most one PHI non-virtual PHI node in return_bb,
581 for the return value. If there are other PHIs, give up. */
582 if (return_bb != EXIT_BLOCK_PTR)
584 gimple_stmt_iterator psi;
586 for (psi = gsi_start_phis (return_bb); !gsi_end_p (psi); gsi_next (&psi))
587 if (!virtual_operand_p (gimple_phi_result (gsi_stmt (psi)))
588 && !(retval
589 && current->split_part_set_retval
590 && TREE_CODE (retval) == SSA_NAME
591 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
592 && SSA_NAME_DEF_STMT (retval) == gsi_stmt (psi)))
594 if (dump_file && (dump_flags & TDF_DETAILS))
595 fprintf (dump_file,
596 " Refused: return bb has extra PHIs\n");
597 return;
601 if (dump_file && (dump_flags & TDF_DETAILS))
602 fprintf (dump_file, " Accepted!\n");
604 /* At the moment chose split point with lowest frequency and that leaves
605 out smallest size of header.
606 In future we might re-consider this heuristics. */
607 if (!best_split_point.split_bbs
608 || best_split_point.entry_bb->frequency > current->entry_bb->frequency
609 || (best_split_point.entry_bb->frequency == current->entry_bb->frequency
610 && best_split_point.split_size < current->split_size))
613 if (dump_file && (dump_flags & TDF_DETAILS))
614 fprintf (dump_file, " New best split point!\n");
615 if (best_split_point.ssa_names_to_pass)
617 BITMAP_FREE (best_split_point.ssa_names_to_pass);
618 BITMAP_FREE (best_split_point.split_bbs);
620 best_split_point = *current;
621 best_split_point.ssa_names_to_pass = BITMAP_ALLOC (NULL);
622 bitmap_copy (best_split_point.ssa_names_to_pass,
623 current->ssa_names_to_pass);
624 best_split_point.split_bbs = BITMAP_ALLOC (NULL);
625 bitmap_copy (best_split_point.split_bbs, current->split_bbs);
629 /* Return basic block containing RETURN statement. We allow basic blocks
630 of the form:
631 <retval> = tmp_var;
632 return <retval>
633 but return_bb can not be more complex than this.
634 If nothing is found, return EXIT_BLOCK_PTR.
636 When there are multiple RETURN statement, chose one with return value,
637 since that one is more likely shared by multiple code paths.
639 Return BB is special, because for function splitting it is the only
640 basic block that is duplicated in between header and split part of the
641 function.
643 TODO: We might support multiple return blocks. */
645 static basic_block
646 find_return_bb (void)
648 edge e;
649 basic_block return_bb = EXIT_BLOCK_PTR;
650 gimple_stmt_iterator bsi;
651 bool found_return = false;
652 tree retval = NULL_TREE;
654 if (!single_pred_p (EXIT_BLOCK_PTR))
655 return return_bb;
657 e = single_pred_edge (EXIT_BLOCK_PTR);
658 for (bsi = gsi_last_bb (e->src); !gsi_end_p (bsi); gsi_prev (&bsi))
660 gimple stmt = gsi_stmt (bsi);
661 if (gimple_code (stmt) == GIMPLE_LABEL
662 || is_gimple_debug (stmt)
663 || gimple_clobber_p (stmt))
665 else if (gimple_code (stmt) == GIMPLE_ASSIGN
666 && found_return
667 && gimple_assign_single_p (stmt)
668 && (auto_var_in_fn_p (gimple_assign_rhs1 (stmt),
669 current_function_decl)
670 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
671 && retval == gimple_assign_lhs (stmt))
673 else if (gimple_code (stmt) == GIMPLE_RETURN)
675 found_return = true;
676 retval = gimple_return_retval (stmt);
678 else
679 break;
681 if (gsi_end_p (bsi) && found_return)
682 return_bb = e->src;
684 return return_bb;
687 /* Given return basic block RETURN_BB, see where return value is really
688 stored. */
689 static tree
690 find_retval (basic_block return_bb)
692 gimple_stmt_iterator bsi;
693 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
694 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
695 return gimple_return_retval (gsi_stmt (bsi));
696 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
697 && !gimple_clobber_p (gsi_stmt (bsi)))
698 return gimple_assign_rhs1 (gsi_stmt (bsi));
699 return NULL;
702 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
703 variable, mark it as used in bitmap passed via DATA.
704 Return true when access to T prevents splitting the function. */
706 static bool
707 mark_nonssa_use (gimple stmt ATTRIBUTE_UNUSED, tree t, void *data)
709 t = get_base_address (t);
711 if (!t || is_gimple_reg (t))
712 return false;
714 /* At present we can't pass non-SSA arguments to split function.
715 FIXME: this can be relaxed by passing references to arguments. */
716 if (TREE_CODE (t) == PARM_DECL)
718 if (dump_file && (dump_flags & TDF_DETAILS))
719 fprintf (dump_file,
720 "Cannot split: use of non-ssa function parameter.\n");
721 return true;
724 if ((TREE_CODE (t) == VAR_DECL
725 && auto_var_in_fn_p (t, current_function_decl))
726 || TREE_CODE (t) == RESULT_DECL
727 || TREE_CODE (t) == LABEL_DECL)
728 bitmap_set_bit ((bitmap)data, DECL_UID (t));
730 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
731 to pretend that the value pointed to is actual result decl. */
732 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
733 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
734 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
735 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
736 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
737 return
738 bitmap_bit_p ((bitmap)data,
739 DECL_UID (DECL_RESULT (current_function_decl)));
741 return false;
744 /* Compute local properties of basic block BB we collect when looking for
745 split points. We look for ssa defs and store them in SET_SSA_NAMES,
746 for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic
747 vars stored in NON_SSA_VARS.
749 When BB has edge to RETURN_BB, collect uses in RETURN_BB too.
751 Return false when BB contains something that prevents it from being put into
752 split function. */
754 static bool
755 visit_bb (basic_block bb, basic_block return_bb,
756 bitmap set_ssa_names, bitmap used_ssa_names,
757 bitmap non_ssa_vars)
759 gimple_stmt_iterator bsi;
760 edge e;
761 edge_iterator ei;
762 bool can_split = true;
764 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
766 gimple stmt = gsi_stmt (bsi);
767 tree op;
768 ssa_op_iter iter;
769 tree decl;
771 if (is_gimple_debug (stmt))
772 continue;
774 if (gimple_clobber_p (stmt))
775 continue;
777 /* FIXME: We can split regions containing EH. We can not however
778 split RESX, EH_DISPATCH and EH_POINTER referring to same region
779 into different partitions. This would require tracking of
780 EH regions and checking in consider_split_point if they
781 are not used elsewhere. */
782 if (gimple_code (stmt) == GIMPLE_RESX)
784 if (dump_file && (dump_flags & TDF_DETAILS))
785 fprintf (dump_file, "Cannot split: resx.\n");
786 can_split = false;
788 if (gimple_code (stmt) == GIMPLE_EH_DISPATCH)
790 if (dump_file && (dump_flags & TDF_DETAILS))
791 fprintf (dump_file, "Cannot split: eh dispatch.\n");
792 can_split = false;
795 /* Check builtins that prevent splitting. */
796 if (gimple_code (stmt) == GIMPLE_CALL
797 && (decl = gimple_call_fndecl (stmt)) != NULL_TREE
798 && DECL_BUILT_IN (decl)
799 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
800 switch (DECL_FUNCTION_CODE (decl))
802 /* FIXME: once we will allow passing non-parm values to split part,
803 we need to be sure to handle correct builtin_stack_save and
804 builtin_stack_restore. At the moment we are safe; there is no
805 way to store builtin_stack_save result in non-SSA variable
806 since all calls to those are compiler generated. */
807 case BUILT_IN_APPLY:
808 case BUILT_IN_APPLY_ARGS:
809 case BUILT_IN_VA_START:
810 if (dump_file && (dump_flags & TDF_DETAILS))
811 fprintf (dump_file,
812 "Cannot split: builtin_apply and va_start.\n");
813 can_split = false;
814 break;
815 case BUILT_IN_EH_POINTER:
816 if (dump_file && (dump_flags & TDF_DETAILS))
817 fprintf (dump_file, "Cannot split: builtin_eh_pointer.\n");
818 can_split = false;
819 break;
820 default:
821 break;
824 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
825 bitmap_set_bit (set_ssa_names, SSA_NAME_VERSION (op));
826 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
827 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
828 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
829 mark_nonssa_use,
830 mark_nonssa_use,
831 mark_nonssa_use);
833 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
835 gimple stmt = gsi_stmt (bsi);
836 unsigned int i;
838 if (virtual_operand_p (gimple_phi_result (stmt)))
839 continue;
840 bitmap_set_bit (set_ssa_names,
841 SSA_NAME_VERSION (gimple_phi_result (stmt)));
842 for (i = 0; i < gimple_phi_num_args (stmt); i++)
844 tree op = gimple_phi_arg_def (stmt, i);
845 if (TREE_CODE (op) == SSA_NAME)
846 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
848 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
849 mark_nonssa_use,
850 mark_nonssa_use,
851 mark_nonssa_use);
853 /* Record also uses coming from PHI operand in return BB. */
854 FOR_EACH_EDGE (e, ei, bb->succs)
855 if (e->dest == return_bb)
857 for (bsi = gsi_start_phis (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
859 gimple stmt = gsi_stmt (bsi);
860 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
862 if (virtual_operand_p (gimple_phi_result (stmt)))
863 continue;
864 if (TREE_CODE (op) == SSA_NAME)
865 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
866 else
867 can_split &= !mark_nonssa_use (stmt, op, non_ssa_vars);
870 return can_split;
873 /* Stack entry for recursive DFS walk in find_split_point. */
875 typedef struct
877 /* Basic block we are examining. */
878 basic_block bb;
880 /* SSA names set and used by the BB and all BBs reachable
881 from it via DFS walk. */
882 bitmap set_ssa_names, used_ssa_names;
883 bitmap non_ssa_vars;
885 /* All BBS visited from this BB via DFS walk. */
886 bitmap bbs_visited;
888 /* Last examined edge in DFS walk. Since we walk unoriented graph,
889 the value is up to sum of incoming and outgoing edges of BB. */
890 unsigned int edge_num;
892 /* Stack entry index of earliest BB reachable from current BB
893 or any BB visited later in DFS walk. */
894 int earliest;
896 /* Overall time and size of all BBs reached from this BB in DFS walk. */
897 int overall_time, overall_size;
899 /* When false we can not split on this BB. */
900 bool can_split;
901 } stack_entry;
904 /* Find all articulations and call consider_split on them.
905 OVERALL_TIME and OVERALL_SIZE is time and size of the function.
907 We perform basic algorithm for finding an articulation in a graph
908 created from CFG by considering it to be an unoriented graph.
910 The articulation is discovered via DFS walk. We collect earliest
911 basic block on stack that is reachable via backward edge. Articulation
912 is any basic block such that there is no backward edge bypassing it.
913 To reduce stack usage we maintain heap allocated stack in STACK vector.
914 AUX pointer of BB is set to index it appears in the stack or -1 once
915 it is visited and popped off the stack.
917 The algorithm finds articulation after visiting the whole component
918 reachable by it. This makes it convenient to collect information about
919 the component used by consider_split. */
921 static void
922 find_split_points (int overall_time, int overall_size)
924 stack_entry first;
925 vec<stack_entry> stack = vNULL;
926 basic_block bb;
927 basic_block return_bb = find_return_bb ();
928 struct split_point current;
930 current.header_time = overall_time;
931 current.header_size = overall_size;
932 current.split_time = 0;
933 current.split_size = 0;
934 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
936 first.bb = ENTRY_BLOCK_PTR;
937 first.edge_num = 0;
938 first.overall_time = 0;
939 first.overall_size = 0;
940 first.earliest = INT_MAX;
941 first.set_ssa_names = 0;
942 first.used_ssa_names = 0;
943 first.bbs_visited = 0;
944 stack.safe_push (first);
945 ENTRY_BLOCK_PTR->aux = (void *)(intptr_t)-1;
947 while (!stack.is_empty ())
949 stack_entry *entry = &stack.last ();
951 /* We are walking an acyclic graph, so edge_num counts
952 succ and pred edges together. However when considering
953 articulation, we want to have processed everything reachable
954 from articulation but nothing that reaches into it. */
955 if (entry->edge_num == EDGE_COUNT (entry->bb->succs)
956 && entry->bb != ENTRY_BLOCK_PTR)
958 int pos = stack.length ();
959 entry->can_split &= visit_bb (entry->bb, return_bb,
960 entry->set_ssa_names,
961 entry->used_ssa_names,
962 entry->non_ssa_vars);
963 if (pos <= entry->earliest && !entry->can_split
964 && dump_file && (dump_flags & TDF_DETAILS))
965 fprintf (dump_file,
966 "found articulation at bb %i but can not split\n",
967 entry->bb->index);
968 if (pos <= entry->earliest && entry->can_split)
970 if (dump_file && (dump_flags & TDF_DETAILS))
971 fprintf (dump_file, "found articulation at bb %i\n",
972 entry->bb->index);
973 current.entry_bb = entry->bb;
974 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
975 bitmap_and_compl (current.ssa_names_to_pass,
976 entry->used_ssa_names, entry->set_ssa_names);
977 current.header_time = overall_time - entry->overall_time;
978 current.header_size = overall_size - entry->overall_size;
979 current.split_time = entry->overall_time;
980 current.split_size = entry->overall_size;
981 current.split_bbs = entry->bbs_visited;
982 consider_split (&current, entry->non_ssa_vars, return_bb);
983 BITMAP_FREE (current.ssa_names_to_pass);
986 /* Do actual DFS walk. */
987 if (entry->edge_num
988 < (EDGE_COUNT (entry->bb->succs)
989 + EDGE_COUNT (entry->bb->preds)))
991 edge e;
992 basic_block dest;
993 if (entry->edge_num < EDGE_COUNT (entry->bb->succs))
995 e = EDGE_SUCC (entry->bb, entry->edge_num);
996 dest = e->dest;
998 else
1000 e = EDGE_PRED (entry->bb, entry->edge_num
1001 - EDGE_COUNT (entry->bb->succs));
1002 dest = e->src;
1005 entry->edge_num++;
1007 /* New BB to visit, push it to the stack. */
1008 if (dest != return_bb && dest != EXIT_BLOCK_PTR
1009 && !dest->aux)
1011 stack_entry new_entry;
1013 new_entry.bb = dest;
1014 new_entry.edge_num = 0;
1015 new_entry.overall_time
1016 = bb_info_vec[dest->index].time;
1017 new_entry.overall_size
1018 = bb_info_vec[dest->index].size;
1019 new_entry.earliest = INT_MAX;
1020 new_entry.set_ssa_names = BITMAP_ALLOC (NULL);
1021 new_entry.used_ssa_names = BITMAP_ALLOC (NULL);
1022 new_entry.bbs_visited = BITMAP_ALLOC (NULL);
1023 new_entry.non_ssa_vars = BITMAP_ALLOC (NULL);
1024 new_entry.can_split = true;
1025 bitmap_set_bit (new_entry.bbs_visited, dest->index);
1026 stack.safe_push (new_entry);
1027 dest->aux = (void *)(intptr_t)stack.length ();
1029 /* Back edge found, record the earliest point. */
1030 else if ((intptr_t)dest->aux > 0
1031 && (intptr_t)dest->aux < entry->earliest)
1032 entry->earliest = (intptr_t)dest->aux;
1034 /* We are done with examining the edges. Pop off the value from stack
1035 and merge stuff we accumulate during the walk. */
1036 else if (entry->bb != ENTRY_BLOCK_PTR)
1038 stack_entry *prev = &stack[stack.length () - 2];
1040 entry->bb->aux = (void *)(intptr_t)-1;
1041 prev->can_split &= entry->can_split;
1042 if (prev->set_ssa_names)
1044 bitmap_ior_into (prev->set_ssa_names, entry->set_ssa_names);
1045 bitmap_ior_into (prev->used_ssa_names, entry->used_ssa_names);
1046 bitmap_ior_into (prev->bbs_visited, entry->bbs_visited);
1047 bitmap_ior_into (prev->non_ssa_vars, entry->non_ssa_vars);
1049 if (prev->earliest > entry->earliest)
1050 prev->earliest = entry->earliest;
1051 prev->overall_time += entry->overall_time;
1052 prev->overall_size += entry->overall_size;
1053 BITMAP_FREE (entry->set_ssa_names);
1054 BITMAP_FREE (entry->used_ssa_names);
1055 BITMAP_FREE (entry->bbs_visited);
1056 BITMAP_FREE (entry->non_ssa_vars);
1057 stack.pop ();
1059 else
1060 stack.pop ();
1062 ENTRY_BLOCK_PTR->aux = NULL;
1063 FOR_EACH_BB (bb)
1064 bb->aux = NULL;
1065 stack.release ();
1066 BITMAP_FREE (current.ssa_names_to_pass);
1069 /* Split function at SPLIT_POINT. */
1071 static void
1072 split_function (struct split_point *split_point)
1074 vec<tree> args_to_pass = vNULL;
1075 bitmap args_to_skip;
1076 tree parm;
1077 int num = 0;
1078 struct cgraph_node *node, *cur_node = cgraph_get_node (current_function_decl);
1079 basic_block return_bb = find_return_bb ();
1080 basic_block call_bb;
1081 gimple_stmt_iterator gsi;
1082 gimple call;
1083 edge e;
1084 edge_iterator ei;
1085 tree retval = NULL, real_retval = NULL;
1086 bool split_part_return_p = false;
1087 gimple last_stmt = NULL;
1088 unsigned int i;
1089 tree arg, ddef;
1090 vec<tree, va_gc> **debug_args = NULL;
1092 if (dump_file)
1094 fprintf (dump_file, "\n\nSplitting function at:\n");
1095 dump_split_point (dump_file, split_point);
1098 if (cur_node->local.can_change_signature)
1099 args_to_skip = BITMAP_ALLOC (NULL);
1100 else
1101 args_to_skip = NULL;
1103 /* Collect the parameters of new function and args_to_skip bitmap. */
1104 for (parm = DECL_ARGUMENTS (current_function_decl);
1105 parm; parm = DECL_CHAIN (parm), num++)
1106 if (args_to_skip
1107 && (!is_gimple_reg (parm)
1108 || (ddef = ssa_default_def (cfun, parm)) == NULL_TREE
1109 || !bitmap_bit_p (split_point->ssa_names_to_pass,
1110 SSA_NAME_VERSION (ddef))))
1111 bitmap_set_bit (args_to_skip, num);
1112 else
1114 /* This parm might not have been used up to now, but is going to be
1115 used, hence register it. */
1116 if (is_gimple_reg (parm))
1117 arg = get_or_create_ssa_default_def (cfun, parm);
1118 else
1119 arg = parm;
1121 if (!useless_type_conversion_p (DECL_ARG_TYPE (parm), TREE_TYPE (arg)))
1122 arg = fold_convert (DECL_ARG_TYPE (parm), arg);
1123 args_to_pass.safe_push (arg);
1126 /* See if the split function will return. */
1127 FOR_EACH_EDGE (e, ei, return_bb->preds)
1128 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1129 break;
1130 if (e)
1131 split_part_return_p = true;
1133 /* Add return block to what will become the split function.
1134 We do not return; no return block is needed. */
1135 if (!split_part_return_p)
1137 /* We have no return block, so nothing is needed. */
1138 else if (return_bb == EXIT_BLOCK_PTR)
1140 /* When we do not want to return value, we need to construct
1141 new return block with empty return statement.
1142 FIXME: Once we are able to change return type, we should change function
1143 to return void instead of just outputting function with undefined return
1144 value. For structures this affects quality of codegen. */
1145 else if (!split_point->split_part_set_retval
1146 && find_retval (return_bb))
1148 bool redirected = true;
1149 basic_block new_return_bb = create_basic_block (NULL, 0, return_bb);
1150 gimple_stmt_iterator gsi = gsi_start_bb (new_return_bb);
1151 gsi_insert_after (&gsi, gimple_build_return (NULL), GSI_NEW_STMT);
1152 while (redirected)
1154 redirected = false;
1155 FOR_EACH_EDGE (e, ei, return_bb->preds)
1156 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1158 new_return_bb->count += e->count;
1159 new_return_bb->frequency += EDGE_FREQUENCY (e);
1160 redirect_edge_and_branch (e, new_return_bb);
1161 redirected = true;
1162 break;
1165 e = make_edge (new_return_bb, EXIT_BLOCK_PTR, 0);
1166 e->probability = REG_BR_PROB_BASE;
1167 e->count = new_return_bb->count;
1168 if (current_loops)
1169 add_bb_to_loop (new_return_bb, current_loops->tree_root);
1170 bitmap_set_bit (split_point->split_bbs, new_return_bb->index);
1172 /* When we pass around the value, use existing return block. */
1173 else
1174 bitmap_set_bit (split_point->split_bbs, return_bb->index);
1176 /* If RETURN_BB has virtual operand PHIs, they must be removed and the
1177 virtual operand marked for renaming as we change the CFG in a way that
1178 tree-inline is not able to compensate for.
1180 Note this can happen whether or not we have a return value. If we have
1181 a return value, then RETURN_BB may have PHIs for real operands too. */
1182 if (return_bb != EXIT_BLOCK_PTR)
1184 bool phi_p = false;
1185 for (gsi = gsi_start_phis (return_bb); !gsi_end_p (gsi);)
1187 gimple stmt = gsi_stmt (gsi);
1188 if (!virtual_operand_p (gimple_phi_result (stmt)))
1190 gsi_next (&gsi);
1191 continue;
1193 mark_virtual_phi_result_for_renaming (stmt);
1194 remove_phi_node (&gsi, true);
1195 phi_p = true;
1197 /* In reality we have to rename the reaching definition of the
1198 virtual operand at return_bb as we will eventually release it
1199 when we remove the code region we outlined.
1200 So we have to rename all immediate virtual uses of that region
1201 if we didn't see a PHI definition yet. */
1202 /* ??? In real reality we want to set the reaching vdef of the
1203 entry of the SESE region as the vuse of the call and the reaching
1204 vdef of the exit of the SESE region as the vdef of the call. */
1205 if (!phi_p)
1206 for (gsi = gsi_start_bb (return_bb); !gsi_end_p (gsi); gsi_next (&gsi))
1208 gimple stmt = gsi_stmt (gsi);
1209 if (gimple_vuse (stmt))
1211 gimple_set_vuse (stmt, NULL_TREE);
1212 update_stmt (stmt);
1214 if (gimple_vdef (stmt))
1215 break;
1219 /* Now create the actual clone. */
1220 rebuild_cgraph_edges ();
1221 node = cgraph_function_versioning (cur_node, vNULL,
1222 NULL,
1223 args_to_skip,
1224 !split_part_return_p,
1225 split_point->split_bbs,
1226 split_point->entry_bb, "part");
1227 /* For usual cloning it is enough to clear builtin only when signature
1228 changes. For partial inlining we however can not expect the part
1229 of builtin implementation to have same semantic as the whole. */
1230 if (DECL_BUILT_IN (node->decl))
1232 DECL_BUILT_IN_CLASS (node->decl) = NOT_BUILT_IN;
1233 DECL_FUNCTION_CODE (node->decl) = (enum built_in_function) 0;
1235 /* If the original function is declared inline, there is no point in issuing
1236 a warning for the non-inlinable part. */
1237 DECL_NO_INLINE_WARNING_P (node->decl) = 1;
1238 cgraph_node_remove_callees (cur_node);
1239 ipa_remove_all_references (&cur_node->ref_list);
1240 if (!split_part_return_p)
1241 TREE_THIS_VOLATILE (node->decl) = 1;
1242 if (dump_file)
1243 dump_function_to_file (node->decl, dump_file, dump_flags);
1245 /* Create the basic block we place call into. It is the entry basic block
1246 split after last label. */
1247 call_bb = split_point->entry_bb;
1248 for (gsi = gsi_start_bb (call_bb); !gsi_end_p (gsi);)
1249 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
1251 last_stmt = gsi_stmt (gsi);
1252 gsi_next (&gsi);
1254 else
1255 break;
1256 e = split_block (split_point->entry_bb, last_stmt);
1257 remove_edge (e);
1259 /* Produce the call statement. */
1260 gsi = gsi_last_bb (call_bb);
1261 FOR_EACH_VEC_ELT (args_to_pass, i, arg)
1262 if (!is_gimple_val (arg))
1264 arg = force_gimple_operand_gsi (&gsi, arg, true, NULL_TREE,
1265 false, GSI_CONTINUE_LINKING);
1266 args_to_pass[i] = arg;
1268 call = gimple_build_call_vec (node->decl, args_to_pass);
1269 gimple_set_block (call, DECL_INITIAL (current_function_decl));
1270 args_to_pass.release ();
1272 /* For optimized away parameters, add on the caller side
1273 before the call
1274 DEBUG D#X => parm_Y(D)
1275 stmts and associate D#X with parm in decl_debug_args_lookup
1276 vector to say for debug info that if parameter parm had been passed,
1277 it would have value parm_Y(D). */
1278 if (args_to_skip)
1279 for (parm = DECL_ARGUMENTS (current_function_decl), num = 0;
1280 parm; parm = DECL_CHAIN (parm), num++)
1281 if (bitmap_bit_p (args_to_skip, num)
1282 && is_gimple_reg (parm))
1284 tree ddecl;
1285 gimple def_temp;
1287 /* This needs to be done even without MAY_HAVE_DEBUG_STMTS,
1288 otherwise if it didn't exist before, we'd end up with
1289 different SSA_NAME_VERSIONs between -g and -g0. */
1290 arg = get_or_create_ssa_default_def (cfun, parm);
1291 if (!MAY_HAVE_DEBUG_STMTS)
1292 continue;
1294 if (debug_args == NULL)
1295 debug_args = decl_debug_args_insert (node->decl);
1296 ddecl = make_node (DEBUG_EXPR_DECL);
1297 DECL_ARTIFICIAL (ddecl) = 1;
1298 TREE_TYPE (ddecl) = TREE_TYPE (parm);
1299 DECL_MODE (ddecl) = DECL_MODE (parm);
1300 vec_safe_push (*debug_args, DECL_ORIGIN (parm));
1301 vec_safe_push (*debug_args, ddecl);
1302 def_temp = gimple_build_debug_bind (ddecl, unshare_expr (arg),
1303 call);
1304 gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
1306 /* And on the callee side, add
1307 DEBUG D#Y s=> parm
1308 DEBUG var => D#Y
1309 stmts to the first bb where var is a VAR_DECL created for the
1310 optimized away parameter in DECL_INITIAL block. This hints
1311 in the debug info that var (whole DECL_ORIGIN is the parm PARM_DECL)
1312 is optimized away, but could be looked up at the call site
1313 as value of D#X there. */
1314 if (debug_args != NULL)
1316 unsigned int i;
1317 tree var, vexpr;
1318 gimple_stmt_iterator cgsi;
1319 gimple def_temp;
1321 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1322 var = BLOCK_VARS (DECL_INITIAL (node->decl));
1323 i = vec_safe_length (*debug_args);
1324 cgsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR));
1327 i -= 2;
1328 while (var != NULL_TREE
1329 && DECL_ABSTRACT_ORIGIN (var) != (**debug_args)[i])
1330 var = TREE_CHAIN (var);
1331 if (var == NULL_TREE)
1332 break;
1333 vexpr = make_node (DEBUG_EXPR_DECL);
1334 parm = (**debug_args)[i];
1335 DECL_ARTIFICIAL (vexpr) = 1;
1336 TREE_TYPE (vexpr) = TREE_TYPE (parm);
1337 DECL_MODE (vexpr) = DECL_MODE (parm);
1338 def_temp = gimple_build_debug_source_bind (vexpr, parm,
1339 NULL);
1340 gsi_insert_before (&cgsi, def_temp, GSI_SAME_STMT);
1341 def_temp = gimple_build_debug_bind (var, vexpr, NULL);
1342 gsi_insert_before (&cgsi, def_temp, GSI_SAME_STMT);
1344 while (i);
1345 pop_cfun ();
1348 /* We avoid address being taken on any variable used by split part,
1349 so return slot optimization is always possible. Moreover this is
1350 required to make DECL_BY_REFERENCE work. */
1351 if (aggregate_value_p (DECL_RESULT (current_function_decl),
1352 TREE_TYPE (current_function_decl))
1353 && (!is_gimple_reg_type (TREE_TYPE (DECL_RESULT (current_function_decl)))
1354 || DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))))
1355 gimple_call_set_return_slot_opt (call, true);
1357 /* Update return value. This is bit tricky. When we do not return,
1358 do nothing. When we return we might need to update return_bb
1359 or produce a new return statement. */
1360 if (!split_part_return_p)
1361 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1362 else
1364 e = make_edge (call_bb, return_bb,
1365 return_bb == EXIT_BLOCK_PTR ? 0 : EDGE_FALLTHRU);
1366 e->count = call_bb->count;
1367 e->probability = REG_BR_PROB_BASE;
1369 /* If there is return basic block, see what value we need to store
1370 return value into and put call just before it. */
1371 if (return_bb != EXIT_BLOCK_PTR)
1373 real_retval = retval = find_retval (return_bb);
1375 if (real_retval && split_point->split_part_set_retval)
1377 gimple_stmt_iterator psi;
1379 /* See if we need new SSA_NAME for the result.
1380 When DECL_BY_REFERENCE is true, retval is actually pointer to
1381 return value and it is constant in whole function. */
1382 if (TREE_CODE (retval) == SSA_NAME
1383 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1385 retval = copy_ssa_name (retval, call);
1387 /* See if there is PHI defining return value. */
1388 for (psi = gsi_start_phis (return_bb);
1389 !gsi_end_p (psi); gsi_next (&psi))
1390 if (!virtual_operand_p (gimple_phi_result (gsi_stmt (psi))))
1391 break;
1393 /* When there is PHI, just update its value. */
1394 if (TREE_CODE (retval) == SSA_NAME
1395 && !gsi_end_p (psi))
1396 add_phi_arg (gsi_stmt (psi), retval, e, UNKNOWN_LOCATION);
1397 /* Otherwise update the return BB itself.
1398 find_return_bb allows at most one assignment to return value,
1399 so update first statement. */
1400 else
1402 gimple_stmt_iterator bsi;
1403 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi);
1404 gsi_next (&bsi))
1405 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
1407 gimple_return_set_retval (gsi_stmt (bsi), retval);
1408 break;
1410 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
1411 && !gimple_clobber_p (gsi_stmt (bsi)))
1413 gimple_assign_set_rhs1 (gsi_stmt (bsi), retval);
1414 break;
1416 update_stmt (gsi_stmt (bsi));
1419 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1421 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1422 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1424 else
1426 tree restype;
1427 restype = TREE_TYPE (DECL_RESULT (current_function_decl));
1428 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1429 if (!useless_type_conversion_p (TREE_TYPE (retval), restype))
1431 gimple cpy;
1432 tree tem = create_tmp_reg (restype, NULL);
1433 tem = make_ssa_name (tem, call);
1434 cpy = gimple_build_assign_with_ops (NOP_EXPR, retval,
1435 tem, NULL_TREE);
1436 gsi_insert_after (&gsi, cpy, GSI_NEW_STMT);
1437 retval = tem;
1439 gimple_call_set_lhs (call, retval);
1440 update_stmt (call);
1443 else
1444 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1446 /* We don't use return block (there is either no return in function or
1447 multiple of them). So create new basic block with return statement.
1449 else
1451 gimple ret;
1452 if (split_point->split_part_set_retval
1453 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1455 retval = DECL_RESULT (current_function_decl);
1457 /* We use temporary register to hold value when aggregate_value_p
1458 is false. Similarly for DECL_BY_REFERENCE we must avoid extra
1459 copy. */
1460 if (!aggregate_value_p (retval, TREE_TYPE (current_function_decl))
1461 && !DECL_BY_REFERENCE (retval))
1462 retval = create_tmp_reg (TREE_TYPE (retval), NULL);
1463 if (is_gimple_reg (retval))
1465 /* When returning by reference, there is only one SSA name
1466 assigned to RESULT_DECL (that is pointer to return value).
1467 Look it up or create new one if it is missing. */
1468 if (DECL_BY_REFERENCE (retval))
1469 retval = get_or_create_ssa_default_def (cfun, retval);
1470 /* Otherwise produce new SSA name for return value. */
1471 else
1472 retval = make_ssa_name (retval, call);
1474 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1475 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1476 else
1477 gimple_call_set_lhs (call, retval);
1479 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1480 ret = gimple_build_return (retval);
1481 gsi_insert_after (&gsi, ret, GSI_NEW_STMT);
1484 free_dominance_info (CDI_DOMINATORS);
1485 free_dominance_info (CDI_POST_DOMINATORS);
1486 compute_inline_parameters (node, true);
1489 /* Execute function splitting pass. */
1491 static unsigned int
1492 execute_split_functions (void)
1494 gimple_stmt_iterator bsi;
1495 basic_block bb;
1496 int overall_time = 0, overall_size = 0;
1497 int todo = 0;
1498 struct cgraph_node *node = cgraph_get_node (current_function_decl);
1500 if (flags_from_decl_or_type (current_function_decl)
1501 & (ECF_NORETURN|ECF_MALLOC))
1503 if (dump_file)
1504 fprintf (dump_file, "Not splitting: noreturn/malloc function.\n");
1505 return 0;
1507 if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
1509 if (dump_file)
1510 fprintf (dump_file, "Not splitting: main function.\n");
1511 return 0;
1513 /* This can be relaxed; function might become inlinable after splitting
1514 away the uninlinable part. */
1515 if (inline_edge_summary_vec.exists ()
1516 && !inline_summary (node)->inlinable)
1518 if (dump_file)
1519 fprintf (dump_file, "Not splitting: not inlinable.\n");
1520 return 0;
1522 if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1524 if (dump_file)
1525 fprintf (dump_file, "Not splitting: disregarding inline limits.\n");
1526 return 0;
1528 /* This can be relaxed; most of versioning tests actually prevents
1529 a duplication. */
1530 if (!tree_versionable_function_p (current_function_decl))
1532 if (dump_file)
1533 fprintf (dump_file, "Not splitting: not versionable.\n");
1534 return 0;
1536 /* FIXME: we could support this. */
1537 if (DECL_STRUCT_FUNCTION (current_function_decl)->static_chain_decl)
1539 if (dump_file)
1540 fprintf (dump_file, "Not splitting: nested function.\n");
1541 return 0;
1544 /* See if it makes sense to try to split.
1545 It makes sense to split if we inline, that is if we have direct calls to
1546 handle or direct calls are possibly going to appear as result of indirect
1547 inlining or LTO. Also handle -fprofile-generate as LTO to allow non-LTO
1548 training for LTO -fprofile-use build.
1550 Note that we are not completely conservative about disqualifying functions
1551 called once. It is possible that the caller is called more then once and
1552 then inlining would still benefit. */
1553 if ((!node->callers
1554 /* Local functions called once will be completely inlined most of time. */
1555 || (!node->callers->next_caller && node->local.local))
1556 && !node->address_taken
1557 && (!flag_lto || !node->externally_visible))
1559 if (dump_file)
1560 fprintf (dump_file, "Not splitting: not called directly "
1561 "or called once.\n");
1562 return 0;
1565 /* FIXME: We can actually split if splitting reduces call overhead. */
1566 if (!flag_inline_small_functions
1567 && !DECL_DECLARED_INLINE_P (current_function_decl))
1569 if (dump_file)
1570 fprintf (dump_file, "Not splitting: not autoinlining and function"
1571 " is not inline.\n");
1572 return 0;
1575 /* We enforce splitting after loop headers when profile info is not
1576 available. */
1577 if (profile_status != PROFILE_READ)
1578 mark_dfs_back_edges ();
1580 /* Initialize bitmap to track forbidden calls. */
1581 forbidden_dominators = BITMAP_ALLOC (NULL);
1582 calculate_dominance_info (CDI_DOMINATORS);
1584 /* Compute local info about basic blocks and determine function size/time. */
1585 bb_info_vec.safe_grow_cleared (last_basic_block + 1);
1586 memset (&best_split_point, 0, sizeof (best_split_point));
1587 FOR_EACH_BB (bb)
1589 int time = 0;
1590 int size = 0;
1591 int freq = compute_call_stmt_bb_frequency (current_function_decl, bb);
1593 if (dump_file && (dump_flags & TDF_DETAILS))
1594 fprintf (dump_file, "Basic block %i\n", bb->index);
1596 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1598 int this_time, this_size;
1599 gimple stmt = gsi_stmt (bsi);
1601 this_size = estimate_num_insns (stmt, &eni_size_weights);
1602 this_time = estimate_num_insns (stmt, &eni_time_weights) * freq;
1603 size += this_size;
1604 time += this_time;
1605 check_forbidden_calls (stmt);
1607 if (dump_file && (dump_flags & TDF_DETAILS))
1609 fprintf (dump_file, " freq:%6i size:%3i time:%3i ",
1610 freq, this_size, this_time);
1611 print_gimple_stmt (dump_file, stmt, 0, 0);
1614 overall_time += time;
1615 overall_size += size;
1616 bb_info_vec[bb->index].time = time;
1617 bb_info_vec[bb->index].size = size;
1619 find_split_points (overall_time, overall_size);
1620 if (best_split_point.split_bbs)
1622 split_function (&best_split_point);
1623 BITMAP_FREE (best_split_point.ssa_names_to_pass);
1624 BITMAP_FREE (best_split_point.split_bbs);
1625 todo = TODO_update_ssa | TODO_cleanup_cfg;
1627 BITMAP_FREE (forbidden_dominators);
1628 bb_info_vec.release ();
1629 return todo;
1632 /* Gate function splitting pass. When doing profile feedback, we want
1633 to execute the pass after profiling is read. So disable one in
1634 early optimization. */
1636 static bool
1637 gate_split_functions (void)
1639 return (flag_partial_inlining
1640 && !profile_arc_flag && !flag_branch_probabilities);
1643 namespace {
1645 const pass_data pass_data_split_functions =
1647 GIMPLE_PASS, /* type */
1648 "fnsplit", /* name */
1649 OPTGROUP_NONE, /* optinfo_flags */
1650 true, /* has_gate */
1651 true, /* has_execute */
1652 TV_IPA_FNSPLIT, /* tv_id */
1653 PROP_cfg, /* properties_required */
1654 0, /* properties_provided */
1655 0, /* properties_destroyed */
1656 0, /* todo_flags_start */
1657 TODO_verify_all, /* todo_flags_finish */
1660 class pass_split_functions : public gimple_opt_pass
1662 public:
1663 pass_split_functions (gcc::context *ctxt)
1664 : gimple_opt_pass (pass_data_split_functions, ctxt)
1667 /* opt_pass methods: */
1668 bool gate () { return gate_split_functions (); }
1669 unsigned int execute () { return execute_split_functions (); }
1671 }; // class pass_split_functions
1673 } // anon namespace
1675 gimple_opt_pass *
1676 make_pass_split_functions (gcc::context *ctxt)
1678 return new pass_split_functions (ctxt);
1681 /* Gate feedback driven function splitting pass.
1682 We don't need to split when profiling at all, we are producing
1683 lousy code anyway. */
1685 static bool
1686 gate_feedback_split_functions (void)
1688 return (flag_partial_inlining
1689 && flag_branch_probabilities);
1692 /* Execute function splitting pass. */
1694 static unsigned int
1695 execute_feedback_split_functions (void)
1697 unsigned int retval = execute_split_functions ();
1698 if (retval)
1699 retval |= TODO_rebuild_cgraph_edges;
1700 return retval;
1703 namespace {
1705 const pass_data pass_data_feedback_split_functions =
1707 GIMPLE_PASS, /* type */
1708 "feedback_fnsplit", /* name */
1709 OPTGROUP_NONE, /* optinfo_flags */
1710 true, /* has_gate */
1711 true, /* has_execute */
1712 TV_IPA_FNSPLIT, /* tv_id */
1713 PROP_cfg, /* properties_required */
1714 0, /* properties_provided */
1715 0, /* properties_destroyed */
1716 0, /* todo_flags_start */
1717 TODO_verify_all, /* todo_flags_finish */
1720 class pass_feedback_split_functions : public gimple_opt_pass
1722 public:
1723 pass_feedback_split_functions (gcc::context *ctxt)
1724 : gimple_opt_pass (pass_data_feedback_split_functions, ctxt)
1727 /* opt_pass methods: */
1728 bool gate () { return gate_feedback_split_functions (); }
1729 unsigned int execute () { return execute_feedback_split_functions (); }
1731 }; // class pass_feedback_split_functions
1733 } // anon namespace
1735 gimple_opt_pass *
1736 make_pass_feedback_split_functions (gcc::context *ctxt)
1738 return new pass_feedback_split_functions (ctxt);