2013-10-25 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ipa-split.c
blob3bec895180c36af89f94f4477d4150318aa4123d
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 "target.h"
82 #include "ipa-prop.h"
83 #include "gimple-ssa.h"
84 #include "tree-cfg.h"
85 #include "tree-phinodes.h"
86 #include "ssa-iterators.h"
87 #include "tree-ssanames.h"
88 #include "tree-into-ssa.h"
89 #include "tree-dfa.h"
90 #include "tree-pass.h"
91 #include "flags.h"
92 #include "diagnostic.h"
93 #include "tree-dump.h"
94 #include "tree-inline.h"
95 #include "params.h"
96 #include "gimple-pretty-print.h"
97 #include "ipa-inline.h"
98 #include "cfgloop.h"
100 /* Per basic block info. */
102 typedef struct
104 unsigned int size;
105 unsigned int time;
106 } bb_info;
108 static vec<bb_info> bb_info_vec;
110 /* Description of split point. */
112 struct split_point
114 /* Size of the partitions. */
115 unsigned int header_time, header_size, split_time, split_size;
117 /* SSA names that need to be passed into spit function. */
118 bitmap ssa_names_to_pass;
120 /* Basic block where we split (that will become entry point of new function. */
121 basic_block entry_bb;
123 /* Basic blocks we are splitting away. */
124 bitmap split_bbs;
126 /* True when return value is computed on split part and thus it needs
127 to be returned. */
128 bool split_part_set_retval;
131 /* Best split point found. */
133 struct split_point best_split_point;
135 /* Set of basic blocks that are not allowed to dominate a split point. */
137 static bitmap forbidden_dominators;
139 static tree find_retval (basic_block return_bb);
141 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
142 variable, check it if it is present in bitmap passed via DATA. */
144 static bool
145 test_nonssa_use (gimple stmt ATTRIBUTE_UNUSED, tree t, void *data)
147 t = get_base_address (t);
149 if (!t || is_gimple_reg (t))
150 return false;
152 if (TREE_CODE (t) == PARM_DECL
153 || (TREE_CODE (t) == VAR_DECL
154 && auto_var_in_fn_p (t, current_function_decl))
155 || TREE_CODE (t) == RESULT_DECL
156 || TREE_CODE (t) == LABEL_DECL)
157 return bitmap_bit_p ((bitmap)data, DECL_UID (t));
159 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
160 to pretend that the value pointed to is actual result decl. */
161 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
162 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
163 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
164 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
165 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
166 return
167 bitmap_bit_p ((bitmap)data,
168 DECL_UID (DECL_RESULT (current_function_decl)));
170 return false;
173 /* Dump split point CURRENT. */
175 static void
176 dump_split_point (FILE * file, struct split_point *current)
178 fprintf (file,
179 "Split point at BB %i\n"
180 " header time: %i header size: %i\n"
181 " split time: %i split size: %i\n bbs: ",
182 current->entry_bb->index, current->header_time,
183 current->header_size, current->split_time, current->split_size);
184 dump_bitmap (file, current->split_bbs);
185 fprintf (file, " SSA names to pass: ");
186 dump_bitmap (file, current->ssa_names_to_pass);
189 /* Look for all BBs in header that might lead to the split part and verify
190 that they are not defining any non-SSA var used by the split part.
191 Parameters are the same as for consider_split. */
193 static bool
194 verify_non_ssa_vars (struct split_point *current, bitmap non_ssa_vars,
195 basic_block return_bb)
197 bitmap seen = BITMAP_ALLOC (NULL);
198 vec<basic_block> worklist = vNULL;
199 edge e;
200 edge_iterator ei;
201 bool ok = true;
203 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
204 if (e->src != ENTRY_BLOCK_PTR
205 && !bitmap_bit_p (current->split_bbs, e->src->index))
207 worklist.safe_push (e->src);
208 bitmap_set_bit (seen, e->src->index);
211 while (!worklist.is_empty ())
213 gimple_stmt_iterator bsi;
214 basic_block bb = worklist.pop ();
216 FOR_EACH_EDGE (e, ei, bb->preds)
217 if (e->src != ENTRY_BLOCK_PTR
218 && bitmap_set_bit (seen, e->src->index))
220 gcc_checking_assert (!bitmap_bit_p (current->split_bbs,
221 e->src->index));
222 worklist.safe_push (e->src);
224 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
226 gimple stmt = gsi_stmt (bsi);
227 if (is_gimple_debug (stmt))
228 continue;
229 if (walk_stmt_load_store_addr_ops
230 (stmt, non_ssa_vars, test_nonssa_use, test_nonssa_use,
231 test_nonssa_use))
233 ok = false;
234 goto done;
236 if (gimple_code (stmt) == GIMPLE_LABEL
237 && test_nonssa_use (stmt, gimple_label_label (stmt),
238 non_ssa_vars))
240 ok = false;
241 goto done;
244 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
246 if (walk_stmt_load_store_addr_ops
247 (gsi_stmt (bsi), non_ssa_vars, test_nonssa_use, test_nonssa_use,
248 test_nonssa_use))
250 ok = false;
251 goto done;
254 FOR_EACH_EDGE (e, ei, bb->succs)
256 if (e->dest != return_bb)
257 continue;
258 for (bsi = gsi_start_phis (return_bb); !gsi_end_p (bsi);
259 gsi_next (&bsi))
261 gimple stmt = gsi_stmt (bsi);
262 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
264 if (virtual_operand_p (gimple_phi_result (stmt)))
265 continue;
266 if (TREE_CODE (op) != SSA_NAME
267 && test_nonssa_use (stmt, op, non_ssa_vars))
269 ok = false;
270 goto done;
275 done:
276 BITMAP_FREE (seen);
277 worklist.release ();
278 return ok;
281 /* If STMT is a call, check the callee against a list of forbidden
282 predicate functions. If a match is found, look for uses of the
283 call result in condition statements that compare against zero.
284 For each such use, find the block targeted by the condition
285 statement for the nonzero result, and set the bit for this block
286 in the forbidden dominators bitmap. The purpose of this is to avoid
287 selecting a split point where we are likely to lose the chance
288 to optimize away an unused function call. */
290 static void
291 check_forbidden_calls (gimple stmt)
293 imm_use_iterator use_iter;
294 use_operand_p use_p;
295 tree lhs;
297 /* At the moment, __builtin_constant_p is the only forbidden
298 predicate function call (see PR49642). */
299 if (!gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P))
300 return;
302 lhs = gimple_call_lhs (stmt);
304 if (!lhs || TREE_CODE (lhs) != SSA_NAME)
305 return;
307 FOR_EACH_IMM_USE_FAST (use_p, use_iter, lhs)
309 tree op1;
310 basic_block use_bb, forbidden_bb;
311 enum tree_code code;
312 edge true_edge, false_edge;
313 gimple use_stmt = USE_STMT (use_p);
315 if (gimple_code (use_stmt) != GIMPLE_COND)
316 continue;
318 /* Assuming canonical form for GIMPLE_COND here, with constant
319 in second position. */
320 op1 = gimple_cond_rhs (use_stmt);
321 code = gimple_cond_code (use_stmt);
322 use_bb = gimple_bb (use_stmt);
324 extract_true_false_edges_from_block (use_bb, &true_edge, &false_edge);
326 /* We're only interested in comparisons that distinguish
327 unambiguously from zero. */
328 if (!integer_zerop (op1) || code == LE_EXPR || code == GE_EXPR)
329 continue;
331 if (code == EQ_EXPR)
332 forbidden_bb = false_edge->dest;
333 else
334 forbidden_bb = true_edge->dest;
336 bitmap_set_bit (forbidden_dominators, forbidden_bb->index);
340 /* If BB is dominated by any block in the forbidden dominators set,
341 return TRUE; else FALSE. */
343 static bool
344 dominated_by_forbidden (basic_block bb)
346 unsigned dom_bb;
347 bitmap_iterator bi;
349 EXECUTE_IF_SET_IN_BITMAP (forbidden_dominators, 1, dom_bb, bi)
351 if (dominated_by_p (CDI_DOMINATORS, bb, BASIC_BLOCK (dom_bb)))
352 return true;
355 return false;
358 /* We found an split_point CURRENT. NON_SSA_VARS is bitmap of all non ssa
359 variables used and RETURN_BB is return basic block.
360 See if we can split function here. */
362 static void
363 consider_split (struct split_point *current, bitmap non_ssa_vars,
364 basic_block return_bb)
366 tree parm;
367 unsigned int num_args = 0;
368 unsigned int call_overhead;
369 edge e;
370 edge_iterator ei;
371 gimple_stmt_iterator bsi;
372 unsigned int i;
373 int incoming_freq = 0;
374 tree retval;
375 bool back_edge = false;
377 if (dump_file && (dump_flags & TDF_DETAILS))
378 dump_split_point (dump_file, current);
380 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
382 if (e->flags & EDGE_DFS_BACK)
383 back_edge = true;
384 if (!bitmap_bit_p (current->split_bbs, e->src->index))
385 incoming_freq += EDGE_FREQUENCY (e);
388 /* Do not split when we would end up calling function anyway. */
389 if (incoming_freq
390 >= (ENTRY_BLOCK_PTR->frequency
391 * PARAM_VALUE (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY) / 100))
393 /* When profile is guessed, we can not expect it to give us
394 realistic estimate on likelyness of function taking the
395 complex path. As a special case, when tail of the function is
396 a loop, enable splitting since inlining code skipping the loop
397 is likely noticeable win. */
398 if (back_edge
399 && profile_status != PROFILE_READ
400 && incoming_freq < ENTRY_BLOCK_PTR->frequency)
402 if (dump_file && (dump_flags & TDF_DETAILS))
403 fprintf (dump_file,
404 " Split before loop, accepting despite low frequencies %i %i.\n",
405 incoming_freq,
406 ENTRY_BLOCK_PTR->frequency);
408 else
410 if (dump_file && (dump_flags & TDF_DETAILS))
411 fprintf (dump_file,
412 " Refused: incoming frequency is too large.\n");
413 return;
417 if (!current->header_size)
419 if (dump_file && (dump_flags & TDF_DETAILS))
420 fprintf (dump_file, " Refused: header empty\n");
421 return;
424 /* Verify that PHI args on entry are either virtual or all their operands
425 incoming from header are the same. */
426 for (bsi = gsi_start_phis (current->entry_bb); !gsi_end_p (bsi); gsi_next (&bsi))
428 gimple stmt = gsi_stmt (bsi);
429 tree val = NULL;
431 if (virtual_operand_p (gimple_phi_result (stmt)))
432 continue;
433 for (i = 0; i < gimple_phi_num_args (stmt); i++)
435 edge e = gimple_phi_arg_edge (stmt, i);
436 if (!bitmap_bit_p (current->split_bbs, e->src->index))
438 tree edge_val = gimple_phi_arg_def (stmt, i);
439 if (val && edge_val != val)
441 if (dump_file && (dump_flags & TDF_DETAILS))
442 fprintf (dump_file,
443 " Refused: entry BB has PHI with multiple variants\n");
444 return;
446 val = edge_val;
452 /* See what argument we will pass to the split function and compute
453 call overhead. */
454 call_overhead = eni_size_weights.call_cost;
455 for (parm = DECL_ARGUMENTS (current_function_decl); parm;
456 parm = DECL_CHAIN (parm))
458 if (!is_gimple_reg (parm))
460 if (bitmap_bit_p (non_ssa_vars, DECL_UID (parm)))
462 if (dump_file && (dump_flags & TDF_DETAILS))
463 fprintf (dump_file,
464 " Refused: need to pass non-ssa param values\n");
465 return;
468 else
470 tree ddef = ssa_default_def (cfun, parm);
471 if (ddef
472 && bitmap_bit_p (current->ssa_names_to_pass,
473 SSA_NAME_VERSION (ddef)))
475 if (!VOID_TYPE_P (TREE_TYPE (parm)))
476 call_overhead += estimate_move_cost (TREE_TYPE (parm));
477 num_args++;
481 if (!VOID_TYPE_P (TREE_TYPE (current_function_decl)))
482 call_overhead += estimate_move_cost (TREE_TYPE (current_function_decl));
484 if (current->split_size <= call_overhead)
486 if (dump_file && (dump_flags & TDF_DETAILS))
487 fprintf (dump_file,
488 " Refused: split size is smaller than call overhead\n");
489 return;
491 if (current->header_size + call_overhead
492 >= (unsigned int)(DECL_DECLARED_INLINE_P (current_function_decl)
493 ? MAX_INLINE_INSNS_SINGLE
494 : MAX_INLINE_INSNS_AUTO))
496 if (dump_file && (dump_flags & TDF_DETAILS))
497 fprintf (dump_file,
498 " Refused: header size is too large for inline candidate\n");
499 return;
502 /* FIXME: we currently can pass only SSA function parameters to the split
503 arguments. Once parm_adjustment infrastructure is supported by cloning,
504 we can pass more than that. */
505 if (num_args != bitmap_count_bits (current->ssa_names_to_pass))
508 if (dump_file && (dump_flags & TDF_DETAILS))
509 fprintf (dump_file,
510 " Refused: need to pass non-param values\n");
511 return;
514 /* When there are non-ssa vars used in the split region, see if they
515 are used in the header region. If so, reject the split.
516 FIXME: we can use nested function support to access both. */
517 if (!bitmap_empty_p (non_ssa_vars)
518 && !verify_non_ssa_vars (current, non_ssa_vars, return_bb))
520 if (dump_file && (dump_flags & TDF_DETAILS))
521 fprintf (dump_file,
522 " Refused: split part has non-ssa uses\n");
523 return;
526 /* If the split point is dominated by a forbidden block, reject
527 the split. */
528 if (!bitmap_empty_p (forbidden_dominators)
529 && dominated_by_forbidden (current->entry_bb))
531 if (dump_file && (dump_flags & TDF_DETAILS))
532 fprintf (dump_file,
533 " Refused: split point dominated by forbidden block\n");
534 return;
537 /* See if retval used by return bb is computed by header or split part.
538 When it is computed by split part, we need to produce return statement
539 in the split part and add code to header to pass it around.
541 This is bit tricky to test:
542 1) When there is no return_bb or no return value, we always pass
543 value around.
544 2) Invariants are always computed by caller.
545 3) For SSA we need to look if defining statement is in header or split part
546 4) For non-SSA we need to look where the var is computed. */
547 retval = find_retval (return_bb);
548 if (!retval)
549 current->split_part_set_retval = true;
550 else if (is_gimple_min_invariant (retval))
551 current->split_part_set_retval = false;
552 /* Special case is value returned by reference we record as if it was non-ssa
553 set to result_decl. */
554 else if (TREE_CODE (retval) == SSA_NAME
555 && SSA_NAME_VAR (retval)
556 && TREE_CODE (SSA_NAME_VAR (retval)) == RESULT_DECL
557 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
558 current->split_part_set_retval
559 = bitmap_bit_p (non_ssa_vars, DECL_UID (SSA_NAME_VAR (retval)));
560 else if (TREE_CODE (retval) == SSA_NAME)
561 current->split_part_set_retval
562 = (!SSA_NAME_IS_DEFAULT_DEF (retval)
563 && (bitmap_bit_p (current->split_bbs,
564 gimple_bb (SSA_NAME_DEF_STMT (retval))->index)
565 || gimple_bb (SSA_NAME_DEF_STMT (retval)) == return_bb));
566 else if (TREE_CODE (retval) == PARM_DECL)
567 current->split_part_set_retval = false;
568 else if (TREE_CODE (retval) == VAR_DECL
569 || TREE_CODE (retval) == RESULT_DECL)
570 current->split_part_set_retval
571 = bitmap_bit_p (non_ssa_vars, DECL_UID (retval));
572 else
573 current->split_part_set_retval = true;
575 /* split_function fixes up at most one PHI non-virtual PHI node in return_bb,
576 for the return value. If there are other PHIs, give up. */
577 if (return_bb != EXIT_BLOCK_PTR)
579 gimple_stmt_iterator psi;
581 for (psi = gsi_start_phis (return_bb); !gsi_end_p (psi); gsi_next (&psi))
582 if (!virtual_operand_p (gimple_phi_result (gsi_stmt (psi)))
583 && !(retval
584 && current->split_part_set_retval
585 && TREE_CODE (retval) == SSA_NAME
586 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
587 && SSA_NAME_DEF_STMT (retval) == gsi_stmt (psi)))
589 if (dump_file && (dump_flags & TDF_DETAILS))
590 fprintf (dump_file,
591 " Refused: return bb has extra PHIs\n");
592 return;
596 if (dump_file && (dump_flags & TDF_DETAILS))
597 fprintf (dump_file, " Accepted!\n");
599 /* At the moment chose split point with lowest frequency and that leaves
600 out smallest size of header.
601 In future we might re-consider this heuristics. */
602 if (!best_split_point.split_bbs
603 || best_split_point.entry_bb->frequency > current->entry_bb->frequency
604 || (best_split_point.entry_bb->frequency == current->entry_bb->frequency
605 && best_split_point.split_size < current->split_size))
608 if (dump_file && (dump_flags & TDF_DETAILS))
609 fprintf (dump_file, " New best split point!\n");
610 if (best_split_point.ssa_names_to_pass)
612 BITMAP_FREE (best_split_point.ssa_names_to_pass);
613 BITMAP_FREE (best_split_point.split_bbs);
615 best_split_point = *current;
616 best_split_point.ssa_names_to_pass = BITMAP_ALLOC (NULL);
617 bitmap_copy (best_split_point.ssa_names_to_pass,
618 current->ssa_names_to_pass);
619 best_split_point.split_bbs = BITMAP_ALLOC (NULL);
620 bitmap_copy (best_split_point.split_bbs, current->split_bbs);
624 /* Return basic block containing RETURN statement. We allow basic blocks
625 of the form:
626 <retval> = tmp_var;
627 return <retval>
628 but return_bb can not be more complex than this.
629 If nothing is found, return EXIT_BLOCK_PTR.
631 When there are multiple RETURN statement, chose one with return value,
632 since that one is more likely shared by multiple code paths.
634 Return BB is special, because for function splitting it is the only
635 basic block that is duplicated in between header and split part of the
636 function.
638 TODO: We might support multiple return blocks. */
640 static basic_block
641 find_return_bb (void)
643 edge e;
644 basic_block return_bb = EXIT_BLOCK_PTR;
645 gimple_stmt_iterator bsi;
646 bool found_return = false;
647 tree retval = NULL_TREE;
649 if (!single_pred_p (EXIT_BLOCK_PTR))
650 return return_bb;
652 e = single_pred_edge (EXIT_BLOCK_PTR);
653 for (bsi = gsi_last_bb (e->src); !gsi_end_p (bsi); gsi_prev (&bsi))
655 gimple stmt = gsi_stmt (bsi);
656 if (gimple_code (stmt) == GIMPLE_LABEL
657 || is_gimple_debug (stmt)
658 || gimple_clobber_p (stmt))
660 else if (gimple_code (stmt) == GIMPLE_ASSIGN
661 && found_return
662 && gimple_assign_single_p (stmt)
663 && (auto_var_in_fn_p (gimple_assign_rhs1 (stmt),
664 current_function_decl)
665 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
666 && retval == gimple_assign_lhs (stmt))
668 else if (gimple_code (stmt) == GIMPLE_RETURN)
670 found_return = true;
671 retval = gimple_return_retval (stmt);
673 else
674 break;
676 if (gsi_end_p (bsi) && found_return)
677 return_bb = e->src;
679 return return_bb;
682 /* Given return basic block RETURN_BB, see where return value is really
683 stored. */
684 static tree
685 find_retval (basic_block return_bb)
687 gimple_stmt_iterator bsi;
688 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
689 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
690 return gimple_return_retval (gsi_stmt (bsi));
691 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
692 && !gimple_clobber_p (gsi_stmt (bsi)))
693 return gimple_assign_rhs1 (gsi_stmt (bsi));
694 return NULL;
697 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
698 variable, mark it as used in bitmap passed via DATA.
699 Return true when access to T prevents splitting the function. */
701 static bool
702 mark_nonssa_use (gimple stmt ATTRIBUTE_UNUSED, tree t, void *data)
704 t = get_base_address (t);
706 if (!t || is_gimple_reg (t))
707 return false;
709 /* At present we can't pass non-SSA arguments to split function.
710 FIXME: this can be relaxed by passing references to arguments. */
711 if (TREE_CODE (t) == PARM_DECL)
713 if (dump_file && (dump_flags & TDF_DETAILS))
714 fprintf (dump_file,
715 "Cannot split: use of non-ssa function parameter.\n");
716 return true;
719 if ((TREE_CODE (t) == VAR_DECL
720 && auto_var_in_fn_p (t, current_function_decl))
721 || TREE_CODE (t) == RESULT_DECL
722 || TREE_CODE (t) == LABEL_DECL)
723 bitmap_set_bit ((bitmap)data, DECL_UID (t));
725 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
726 to pretend that the value pointed to is actual result decl. */
727 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
728 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
729 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
730 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
731 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
732 return
733 bitmap_bit_p ((bitmap)data,
734 DECL_UID (DECL_RESULT (current_function_decl)));
736 return false;
739 /* Compute local properties of basic block BB we collect when looking for
740 split points. We look for ssa defs and store them in SET_SSA_NAMES,
741 for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic
742 vars stored in NON_SSA_VARS.
744 When BB has edge to RETURN_BB, collect uses in RETURN_BB too.
746 Return false when BB contains something that prevents it from being put into
747 split function. */
749 static bool
750 visit_bb (basic_block bb, basic_block return_bb,
751 bitmap set_ssa_names, bitmap used_ssa_names,
752 bitmap non_ssa_vars)
754 gimple_stmt_iterator bsi;
755 edge e;
756 edge_iterator ei;
757 bool can_split = true;
759 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
761 gimple stmt = gsi_stmt (bsi);
762 tree op;
763 ssa_op_iter iter;
764 tree decl;
766 if (is_gimple_debug (stmt))
767 continue;
769 if (gimple_clobber_p (stmt))
770 continue;
772 /* FIXME: We can split regions containing EH. We can not however
773 split RESX, EH_DISPATCH and EH_POINTER referring to same region
774 into different partitions. This would require tracking of
775 EH regions and checking in consider_split_point if they
776 are not used elsewhere. */
777 if (gimple_code (stmt) == GIMPLE_RESX)
779 if (dump_file && (dump_flags & TDF_DETAILS))
780 fprintf (dump_file, "Cannot split: resx.\n");
781 can_split = false;
783 if (gimple_code (stmt) == GIMPLE_EH_DISPATCH)
785 if (dump_file && (dump_flags & TDF_DETAILS))
786 fprintf (dump_file, "Cannot split: eh dispatch.\n");
787 can_split = false;
790 /* Check builtins that prevent splitting. */
791 if (gimple_code (stmt) == GIMPLE_CALL
792 && (decl = gimple_call_fndecl (stmt)) != NULL_TREE
793 && DECL_BUILT_IN (decl)
794 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
795 switch (DECL_FUNCTION_CODE (decl))
797 /* FIXME: once we will allow passing non-parm values to split part,
798 we need to be sure to handle correct builtin_stack_save and
799 builtin_stack_restore. At the moment we are safe; there is no
800 way to store builtin_stack_save result in non-SSA variable
801 since all calls to those are compiler generated. */
802 case BUILT_IN_APPLY:
803 case BUILT_IN_APPLY_ARGS:
804 case BUILT_IN_VA_START:
805 if (dump_file && (dump_flags & TDF_DETAILS))
806 fprintf (dump_file,
807 "Cannot split: builtin_apply and va_start.\n");
808 can_split = false;
809 break;
810 case BUILT_IN_EH_POINTER:
811 if (dump_file && (dump_flags & TDF_DETAILS))
812 fprintf (dump_file, "Cannot split: builtin_eh_pointer.\n");
813 can_split = false;
814 break;
815 default:
816 break;
819 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
820 bitmap_set_bit (set_ssa_names, SSA_NAME_VERSION (op));
821 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
822 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
823 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
824 mark_nonssa_use,
825 mark_nonssa_use,
826 mark_nonssa_use);
828 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
830 gimple stmt = gsi_stmt (bsi);
831 unsigned int i;
833 if (virtual_operand_p (gimple_phi_result (stmt)))
834 continue;
835 bitmap_set_bit (set_ssa_names,
836 SSA_NAME_VERSION (gimple_phi_result (stmt)));
837 for (i = 0; i < gimple_phi_num_args (stmt); i++)
839 tree op = gimple_phi_arg_def (stmt, i);
840 if (TREE_CODE (op) == SSA_NAME)
841 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
843 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
844 mark_nonssa_use,
845 mark_nonssa_use,
846 mark_nonssa_use);
848 /* Record also uses coming from PHI operand in return BB. */
849 FOR_EACH_EDGE (e, ei, bb->succs)
850 if (e->dest == return_bb)
852 for (bsi = gsi_start_phis (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
854 gimple stmt = gsi_stmt (bsi);
855 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
857 if (virtual_operand_p (gimple_phi_result (stmt)))
858 continue;
859 if (TREE_CODE (op) == SSA_NAME)
860 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
861 else
862 can_split &= !mark_nonssa_use (stmt, op, non_ssa_vars);
865 return can_split;
868 /* Stack entry for recursive DFS walk in find_split_point. */
870 typedef struct
872 /* Basic block we are examining. */
873 basic_block bb;
875 /* SSA names set and used by the BB and all BBs reachable
876 from it via DFS walk. */
877 bitmap set_ssa_names, used_ssa_names;
878 bitmap non_ssa_vars;
880 /* All BBS visited from this BB via DFS walk. */
881 bitmap bbs_visited;
883 /* Last examined edge in DFS walk. Since we walk unoriented graph,
884 the value is up to sum of incoming and outgoing edges of BB. */
885 unsigned int edge_num;
887 /* Stack entry index of earliest BB reachable from current BB
888 or any BB visited later in DFS walk. */
889 int earliest;
891 /* Overall time and size of all BBs reached from this BB in DFS walk. */
892 int overall_time, overall_size;
894 /* When false we can not split on this BB. */
895 bool can_split;
896 } stack_entry;
899 /* Find all articulations and call consider_split on them.
900 OVERALL_TIME and OVERALL_SIZE is time and size of the function.
902 We perform basic algorithm for finding an articulation in a graph
903 created from CFG by considering it to be an unoriented graph.
905 The articulation is discovered via DFS walk. We collect earliest
906 basic block on stack that is reachable via backward edge. Articulation
907 is any basic block such that there is no backward edge bypassing it.
908 To reduce stack usage we maintain heap allocated stack in STACK vector.
909 AUX pointer of BB is set to index it appears in the stack or -1 once
910 it is visited and popped off the stack.
912 The algorithm finds articulation after visiting the whole component
913 reachable by it. This makes it convenient to collect information about
914 the component used by consider_split. */
916 static void
917 find_split_points (int overall_time, int overall_size)
919 stack_entry first;
920 vec<stack_entry> stack = vNULL;
921 basic_block bb;
922 basic_block return_bb = find_return_bb ();
923 struct split_point current;
925 current.header_time = overall_time;
926 current.header_size = overall_size;
927 current.split_time = 0;
928 current.split_size = 0;
929 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
931 first.bb = ENTRY_BLOCK_PTR;
932 first.edge_num = 0;
933 first.overall_time = 0;
934 first.overall_size = 0;
935 first.earliest = INT_MAX;
936 first.set_ssa_names = 0;
937 first.used_ssa_names = 0;
938 first.bbs_visited = 0;
939 stack.safe_push (first);
940 ENTRY_BLOCK_PTR->aux = (void *)(intptr_t)-1;
942 while (!stack.is_empty ())
944 stack_entry *entry = &stack.last ();
946 /* We are walking an acyclic graph, so edge_num counts
947 succ and pred edges together. However when considering
948 articulation, we want to have processed everything reachable
949 from articulation but nothing that reaches into it. */
950 if (entry->edge_num == EDGE_COUNT (entry->bb->succs)
951 && entry->bb != ENTRY_BLOCK_PTR)
953 int pos = stack.length ();
954 entry->can_split &= visit_bb (entry->bb, return_bb,
955 entry->set_ssa_names,
956 entry->used_ssa_names,
957 entry->non_ssa_vars);
958 if (pos <= entry->earliest && !entry->can_split
959 && dump_file && (dump_flags & TDF_DETAILS))
960 fprintf (dump_file,
961 "found articulation at bb %i but can not split\n",
962 entry->bb->index);
963 if (pos <= entry->earliest && entry->can_split)
965 if (dump_file && (dump_flags & TDF_DETAILS))
966 fprintf (dump_file, "found articulation at bb %i\n",
967 entry->bb->index);
968 current.entry_bb = entry->bb;
969 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
970 bitmap_and_compl (current.ssa_names_to_pass,
971 entry->used_ssa_names, entry->set_ssa_names);
972 current.header_time = overall_time - entry->overall_time;
973 current.header_size = overall_size - entry->overall_size;
974 current.split_time = entry->overall_time;
975 current.split_size = entry->overall_size;
976 current.split_bbs = entry->bbs_visited;
977 consider_split (&current, entry->non_ssa_vars, return_bb);
978 BITMAP_FREE (current.ssa_names_to_pass);
981 /* Do actual DFS walk. */
982 if (entry->edge_num
983 < (EDGE_COUNT (entry->bb->succs)
984 + EDGE_COUNT (entry->bb->preds)))
986 edge e;
987 basic_block dest;
988 if (entry->edge_num < EDGE_COUNT (entry->bb->succs))
990 e = EDGE_SUCC (entry->bb, entry->edge_num);
991 dest = e->dest;
993 else
995 e = EDGE_PRED (entry->bb, entry->edge_num
996 - EDGE_COUNT (entry->bb->succs));
997 dest = e->src;
1000 entry->edge_num++;
1002 /* New BB to visit, push it to the stack. */
1003 if (dest != return_bb && dest != EXIT_BLOCK_PTR
1004 && !dest->aux)
1006 stack_entry new_entry;
1008 new_entry.bb = dest;
1009 new_entry.edge_num = 0;
1010 new_entry.overall_time
1011 = bb_info_vec[dest->index].time;
1012 new_entry.overall_size
1013 = bb_info_vec[dest->index].size;
1014 new_entry.earliest = INT_MAX;
1015 new_entry.set_ssa_names = BITMAP_ALLOC (NULL);
1016 new_entry.used_ssa_names = BITMAP_ALLOC (NULL);
1017 new_entry.bbs_visited = BITMAP_ALLOC (NULL);
1018 new_entry.non_ssa_vars = BITMAP_ALLOC (NULL);
1019 new_entry.can_split = true;
1020 bitmap_set_bit (new_entry.bbs_visited, dest->index);
1021 stack.safe_push (new_entry);
1022 dest->aux = (void *)(intptr_t)stack.length ();
1024 /* Back edge found, record the earliest point. */
1025 else if ((intptr_t)dest->aux > 0
1026 && (intptr_t)dest->aux < entry->earliest)
1027 entry->earliest = (intptr_t)dest->aux;
1029 /* We are done with examining the edges. Pop off the value from stack
1030 and merge stuff we accumulate during the walk. */
1031 else if (entry->bb != ENTRY_BLOCK_PTR)
1033 stack_entry *prev = &stack[stack.length () - 2];
1035 entry->bb->aux = (void *)(intptr_t)-1;
1036 prev->can_split &= entry->can_split;
1037 if (prev->set_ssa_names)
1039 bitmap_ior_into (prev->set_ssa_names, entry->set_ssa_names);
1040 bitmap_ior_into (prev->used_ssa_names, entry->used_ssa_names);
1041 bitmap_ior_into (prev->bbs_visited, entry->bbs_visited);
1042 bitmap_ior_into (prev->non_ssa_vars, entry->non_ssa_vars);
1044 if (prev->earliest > entry->earliest)
1045 prev->earliest = entry->earliest;
1046 prev->overall_time += entry->overall_time;
1047 prev->overall_size += entry->overall_size;
1048 BITMAP_FREE (entry->set_ssa_names);
1049 BITMAP_FREE (entry->used_ssa_names);
1050 BITMAP_FREE (entry->bbs_visited);
1051 BITMAP_FREE (entry->non_ssa_vars);
1052 stack.pop ();
1054 else
1055 stack.pop ();
1057 ENTRY_BLOCK_PTR->aux = NULL;
1058 FOR_EACH_BB (bb)
1059 bb->aux = NULL;
1060 stack.release ();
1061 BITMAP_FREE (current.ssa_names_to_pass);
1064 /* Split function at SPLIT_POINT. */
1066 static void
1067 split_function (struct split_point *split_point)
1069 vec<tree> args_to_pass = vNULL;
1070 bitmap args_to_skip;
1071 tree parm;
1072 int num = 0;
1073 struct cgraph_node *node, *cur_node = cgraph_get_node (current_function_decl);
1074 basic_block return_bb = find_return_bb ();
1075 basic_block call_bb;
1076 gimple_stmt_iterator gsi;
1077 gimple call;
1078 edge e;
1079 edge_iterator ei;
1080 tree retval = NULL, real_retval = NULL;
1081 bool split_part_return_p = false;
1082 gimple last_stmt = NULL;
1083 unsigned int i;
1084 tree arg, ddef;
1085 vec<tree, va_gc> **debug_args = NULL;
1087 if (dump_file)
1089 fprintf (dump_file, "\n\nSplitting function at:\n");
1090 dump_split_point (dump_file, split_point);
1093 if (cur_node->local.can_change_signature)
1094 args_to_skip = BITMAP_ALLOC (NULL);
1095 else
1096 args_to_skip = NULL;
1098 /* Collect the parameters of new function and args_to_skip bitmap. */
1099 for (parm = DECL_ARGUMENTS (current_function_decl);
1100 parm; parm = DECL_CHAIN (parm), num++)
1101 if (args_to_skip
1102 && (!is_gimple_reg (parm)
1103 || (ddef = ssa_default_def (cfun, parm)) == NULL_TREE
1104 || !bitmap_bit_p (split_point->ssa_names_to_pass,
1105 SSA_NAME_VERSION (ddef))))
1106 bitmap_set_bit (args_to_skip, num);
1107 else
1109 /* This parm might not have been used up to now, but is going to be
1110 used, hence register it. */
1111 if (is_gimple_reg (parm))
1112 arg = get_or_create_ssa_default_def (cfun, parm);
1113 else
1114 arg = parm;
1116 if (!useless_type_conversion_p (DECL_ARG_TYPE (parm), TREE_TYPE (arg)))
1117 arg = fold_convert (DECL_ARG_TYPE (parm), arg);
1118 args_to_pass.safe_push (arg);
1121 /* See if the split function will return. */
1122 FOR_EACH_EDGE (e, ei, return_bb->preds)
1123 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1124 break;
1125 if (e)
1126 split_part_return_p = true;
1128 /* Add return block to what will become the split function.
1129 We do not return; no return block is needed. */
1130 if (!split_part_return_p)
1132 /* We have no return block, so nothing is needed. */
1133 else if (return_bb == EXIT_BLOCK_PTR)
1135 /* When we do not want to return value, we need to construct
1136 new return block with empty return statement.
1137 FIXME: Once we are able to change return type, we should change function
1138 to return void instead of just outputting function with undefined return
1139 value. For structures this affects quality of codegen. */
1140 else if (!split_point->split_part_set_retval
1141 && find_retval (return_bb))
1143 bool redirected = true;
1144 basic_block new_return_bb = create_basic_block (NULL, 0, return_bb);
1145 gimple_stmt_iterator gsi = gsi_start_bb (new_return_bb);
1146 gsi_insert_after (&gsi, gimple_build_return (NULL), GSI_NEW_STMT);
1147 while (redirected)
1149 redirected = false;
1150 FOR_EACH_EDGE (e, ei, return_bb->preds)
1151 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1153 new_return_bb->count += e->count;
1154 new_return_bb->frequency += EDGE_FREQUENCY (e);
1155 redirect_edge_and_branch (e, new_return_bb);
1156 redirected = true;
1157 break;
1160 e = make_edge (new_return_bb, EXIT_BLOCK_PTR, 0);
1161 e->probability = REG_BR_PROB_BASE;
1162 e->count = new_return_bb->count;
1163 if (current_loops)
1164 add_bb_to_loop (new_return_bb, current_loops->tree_root);
1165 bitmap_set_bit (split_point->split_bbs, new_return_bb->index);
1167 /* When we pass around the value, use existing return block. */
1168 else
1169 bitmap_set_bit (split_point->split_bbs, return_bb->index);
1171 /* If RETURN_BB has virtual operand PHIs, they must be removed and the
1172 virtual operand marked for renaming as we change the CFG in a way that
1173 tree-inline is not able to compensate for.
1175 Note this can happen whether or not we have a return value. If we have
1176 a return value, then RETURN_BB may have PHIs for real operands too. */
1177 if (return_bb != EXIT_BLOCK_PTR)
1179 bool phi_p = false;
1180 for (gsi = gsi_start_phis (return_bb); !gsi_end_p (gsi);)
1182 gimple stmt = gsi_stmt (gsi);
1183 if (!virtual_operand_p (gimple_phi_result (stmt)))
1185 gsi_next (&gsi);
1186 continue;
1188 mark_virtual_phi_result_for_renaming (stmt);
1189 remove_phi_node (&gsi, true);
1190 phi_p = true;
1192 /* In reality we have to rename the reaching definition of the
1193 virtual operand at return_bb as we will eventually release it
1194 when we remove the code region we outlined.
1195 So we have to rename all immediate virtual uses of that region
1196 if we didn't see a PHI definition yet. */
1197 /* ??? In real reality we want to set the reaching vdef of the
1198 entry of the SESE region as the vuse of the call and the reaching
1199 vdef of the exit of the SESE region as the vdef of the call. */
1200 if (!phi_p)
1201 for (gsi = gsi_start_bb (return_bb); !gsi_end_p (gsi); gsi_next (&gsi))
1203 gimple stmt = gsi_stmt (gsi);
1204 if (gimple_vuse (stmt))
1206 gimple_set_vuse (stmt, NULL_TREE);
1207 update_stmt (stmt);
1209 if (gimple_vdef (stmt))
1210 break;
1214 /* Now create the actual clone. */
1215 rebuild_cgraph_edges ();
1216 node = cgraph_function_versioning (cur_node, vNULL,
1217 NULL,
1218 args_to_skip,
1219 !split_part_return_p,
1220 split_point->split_bbs,
1221 split_point->entry_bb, "part");
1222 /* For usual cloning it is enough to clear builtin only when signature
1223 changes. For partial inlining we however can not expect the part
1224 of builtin implementation to have same semantic as the whole. */
1225 if (DECL_BUILT_IN (node->symbol.decl))
1227 DECL_BUILT_IN_CLASS (node->symbol.decl) = NOT_BUILT_IN;
1228 DECL_FUNCTION_CODE (node->symbol.decl) = (enum built_in_function) 0;
1230 /* If the original function is declared inline, there is no point in issuing
1231 a warning for the non-inlinable part. */
1232 DECL_NO_INLINE_WARNING_P (node->symbol.decl) = 1;
1233 cgraph_node_remove_callees (cur_node);
1234 ipa_remove_all_references (&cur_node->symbol.ref_list);
1235 if (!split_part_return_p)
1236 TREE_THIS_VOLATILE (node->symbol.decl) = 1;
1237 if (dump_file)
1238 dump_function_to_file (node->symbol.decl, dump_file, dump_flags);
1240 /* Create the basic block we place call into. It is the entry basic block
1241 split after last label. */
1242 call_bb = split_point->entry_bb;
1243 for (gsi = gsi_start_bb (call_bb); !gsi_end_p (gsi);)
1244 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
1246 last_stmt = gsi_stmt (gsi);
1247 gsi_next (&gsi);
1249 else
1250 break;
1251 e = split_block (split_point->entry_bb, last_stmt);
1252 remove_edge (e);
1254 /* Produce the call statement. */
1255 gsi = gsi_last_bb (call_bb);
1256 FOR_EACH_VEC_ELT (args_to_pass, i, arg)
1257 if (!is_gimple_val (arg))
1259 arg = force_gimple_operand_gsi (&gsi, arg, true, NULL_TREE,
1260 false, GSI_CONTINUE_LINKING);
1261 args_to_pass[i] = arg;
1263 call = gimple_build_call_vec (node->symbol.decl, args_to_pass);
1264 gimple_set_block (call, DECL_INITIAL (current_function_decl));
1265 args_to_pass.release ();
1267 /* For optimized away parameters, add on the caller side
1268 before the call
1269 DEBUG D#X => parm_Y(D)
1270 stmts and associate D#X with parm in decl_debug_args_lookup
1271 vector to say for debug info that if parameter parm had been passed,
1272 it would have value parm_Y(D). */
1273 if (args_to_skip)
1274 for (parm = DECL_ARGUMENTS (current_function_decl), num = 0;
1275 parm; parm = DECL_CHAIN (parm), num++)
1276 if (bitmap_bit_p (args_to_skip, num)
1277 && is_gimple_reg (parm))
1279 tree ddecl;
1280 gimple def_temp;
1282 /* This needs to be done even without MAY_HAVE_DEBUG_STMTS,
1283 otherwise if it didn't exist before, we'd end up with
1284 different SSA_NAME_VERSIONs between -g and -g0. */
1285 arg = get_or_create_ssa_default_def (cfun, parm);
1286 if (!MAY_HAVE_DEBUG_STMTS)
1287 continue;
1289 if (debug_args == NULL)
1290 debug_args = decl_debug_args_insert (node->symbol.decl);
1291 ddecl = make_node (DEBUG_EXPR_DECL);
1292 DECL_ARTIFICIAL (ddecl) = 1;
1293 TREE_TYPE (ddecl) = TREE_TYPE (parm);
1294 DECL_MODE (ddecl) = DECL_MODE (parm);
1295 vec_safe_push (*debug_args, DECL_ORIGIN (parm));
1296 vec_safe_push (*debug_args, ddecl);
1297 def_temp = gimple_build_debug_bind (ddecl, unshare_expr (arg),
1298 call);
1299 gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
1301 /* And on the callee side, add
1302 DEBUG D#Y s=> parm
1303 DEBUG var => D#Y
1304 stmts to the first bb where var is a VAR_DECL created for the
1305 optimized away parameter in DECL_INITIAL block. This hints
1306 in the debug info that var (whole DECL_ORIGIN is the parm PARM_DECL)
1307 is optimized away, but could be looked up at the call site
1308 as value of D#X there. */
1309 if (debug_args != NULL)
1311 unsigned int i;
1312 tree var, vexpr;
1313 gimple_stmt_iterator cgsi;
1314 gimple def_temp;
1316 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1317 var = BLOCK_VARS (DECL_INITIAL (node->symbol.decl));
1318 i = vec_safe_length (*debug_args);
1319 cgsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR));
1322 i -= 2;
1323 while (var != NULL_TREE
1324 && DECL_ABSTRACT_ORIGIN (var) != (**debug_args)[i])
1325 var = TREE_CHAIN (var);
1326 if (var == NULL_TREE)
1327 break;
1328 vexpr = make_node (DEBUG_EXPR_DECL);
1329 parm = (**debug_args)[i];
1330 DECL_ARTIFICIAL (vexpr) = 1;
1331 TREE_TYPE (vexpr) = TREE_TYPE (parm);
1332 DECL_MODE (vexpr) = DECL_MODE (parm);
1333 def_temp = gimple_build_debug_source_bind (vexpr, parm,
1334 NULL);
1335 gsi_insert_before (&cgsi, def_temp, GSI_SAME_STMT);
1336 def_temp = gimple_build_debug_bind (var, vexpr, NULL);
1337 gsi_insert_before (&cgsi, def_temp, GSI_SAME_STMT);
1339 while (i);
1340 pop_cfun ();
1343 /* We avoid address being taken on any variable used by split part,
1344 so return slot optimization is always possible. Moreover this is
1345 required to make DECL_BY_REFERENCE work. */
1346 if (aggregate_value_p (DECL_RESULT (current_function_decl),
1347 TREE_TYPE (current_function_decl))
1348 && (!is_gimple_reg_type (TREE_TYPE (DECL_RESULT (current_function_decl)))
1349 || DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))))
1350 gimple_call_set_return_slot_opt (call, true);
1352 /* Update return value. This is bit tricky. When we do not return,
1353 do nothing. When we return we might need to update return_bb
1354 or produce a new return statement. */
1355 if (!split_part_return_p)
1356 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1357 else
1359 e = make_edge (call_bb, return_bb,
1360 return_bb == EXIT_BLOCK_PTR ? 0 : EDGE_FALLTHRU);
1361 e->count = call_bb->count;
1362 e->probability = REG_BR_PROB_BASE;
1364 /* If there is return basic block, see what value we need to store
1365 return value into and put call just before it. */
1366 if (return_bb != EXIT_BLOCK_PTR)
1368 real_retval = retval = find_retval (return_bb);
1370 if (real_retval && split_point->split_part_set_retval)
1372 gimple_stmt_iterator psi;
1374 /* See if we need new SSA_NAME for the result.
1375 When DECL_BY_REFERENCE is true, retval is actually pointer to
1376 return value and it is constant in whole function. */
1377 if (TREE_CODE (retval) == SSA_NAME
1378 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1380 retval = copy_ssa_name (retval, call);
1382 /* See if there is PHI defining return value. */
1383 for (psi = gsi_start_phis (return_bb);
1384 !gsi_end_p (psi); gsi_next (&psi))
1385 if (!virtual_operand_p (gimple_phi_result (gsi_stmt (psi))))
1386 break;
1388 /* When there is PHI, just update its value. */
1389 if (TREE_CODE (retval) == SSA_NAME
1390 && !gsi_end_p (psi))
1391 add_phi_arg (gsi_stmt (psi), retval, e, UNKNOWN_LOCATION);
1392 /* Otherwise update the return BB itself.
1393 find_return_bb allows at most one assignment to return value,
1394 so update first statement. */
1395 else
1397 gimple_stmt_iterator bsi;
1398 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi);
1399 gsi_next (&bsi))
1400 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
1402 gimple_return_set_retval (gsi_stmt (bsi), retval);
1403 break;
1405 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
1406 && !gimple_clobber_p (gsi_stmt (bsi)))
1408 gimple_assign_set_rhs1 (gsi_stmt (bsi), retval);
1409 break;
1411 update_stmt (gsi_stmt (bsi));
1414 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1416 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1417 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1419 else
1421 tree restype;
1422 restype = TREE_TYPE (DECL_RESULT (current_function_decl));
1423 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1424 if (!useless_type_conversion_p (TREE_TYPE (retval), restype))
1426 gimple cpy;
1427 tree tem = create_tmp_reg (restype, NULL);
1428 tem = make_ssa_name (tem, call);
1429 cpy = gimple_build_assign_with_ops (NOP_EXPR, retval,
1430 tem, NULL_TREE);
1431 gsi_insert_after (&gsi, cpy, GSI_NEW_STMT);
1432 retval = tem;
1434 gimple_call_set_lhs (call, retval);
1435 update_stmt (call);
1438 else
1439 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1441 /* We don't use return block (there is either no return in function or
1442 multiple of them). So create new basic block with return statement.
1444 else
1446 gimple ret;
1447 if (split_point->split_part_set_retval
1448 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1450 retval = DECL_RESULT (current_function_decl);
1452 /* We use temporary register to hold value when aggregate_value_p
1453 is false. Similarly for DECL_BY_REFERENCE we must avoid extra
1454 copy. */
1455 if (!aggregate_value_p (retval, TREE_TYPE (current_function_decl))
1456 && !DECL_BY_REFERENCE (retval))
1457 retval = create_tmp_reg (TREE_TYPE (retval), NULL);
1458 if (is_gimple_reg (retval))
1460 /* When returning by reference, there is only one SSA name
1461 assigned to RESULT_DECL (that is pointer to return value).
1462 Look it up or create new one if it is missing. */
1463 if (DECL_BY_REFERENCE (retval))
1464 retval = get_or_create_ssa_default_def (cfun, retval);
1465 /* Otherwise produce new SSA name for return value. */
1466 else
1467 retval = make_ssa_name (retval, call);
1469 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1470 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1471 else
1472 gimple_call_set_lhs (call, retval);
1474 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1475 ret = gimple_build_return (retval);
1476 gsi_insert_after (&gsi, ret, GSI_NEW_STMT);
1479 free_dominance_info (CDI_DOMINATORS);
1480 free_dominance_info (CDI_POST_DOMINATORS);
1481 compute_inline_parameters (node, true);
1484 /* Execute function splitting pass. */
1486 static unsigned int
1487 execute_split_functions (void)
1489 gimple_stmt_iterator bsi;
1490 basic_block bb;
1491 int overall_time = 0, overall_size = 0;
1492 int todo = 0;
1493 struct cgraph_node *node = cgraph_get_node (current_function_decl);
1495 if (flags_from_decl_or_type (current_function_decl)
1496 & (ECF_NORETURN|ECF_MALLOC))
1498 if (dump_file)
1499 fprintf (dump_file, "Not splitting: noreturn/malloc function.\n");
1500 return 0;
1502 if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
1504 if (dump_file)
1505 fprintf (dump_file, "Not splitting: main function.\n");
1506 return 0;
1508 /* This can be relaxed; function might become inlinable after splitting
1509 away the uninlinable part. */
1510 if (inline_edge_summary_vec.exists ()
1511 && !inline_summary (node)->inlinable)
1513 if (dump_file)
1514 fprintf (dump_file, "Not splitting: not inlinable.\n");
1515 return 0;
1517 if (DECL_DISREGARD_INLINE_LIMITS (node->symbol.decl))
1519 if (dump_file)
1520 fprintf (dump_file, "Not splitting: disregarding inline limits.\n");
1521 return 0;
1523 /* This can be relaxed; most of versioning tests actually prevents
1524 a duplication. */
1525 if (!tree_versionable_function_p (current_function_decl))
1527 if (dump_file)
1528 fprintf (dump_file, "Not splitting: not versionable.\n");
1529 return 0;
1531 /* FIXME: we could support this. */
1532 if (DECL_STRUCT_FUNCTION (current_function_decl)->static_chain_decl)
1534 if (dump_file)
1535 fprintf (dump_file, "Not splitting: nested function.\n");
1536 return 0;
1539 /* See if it makes sense to try to split.
1540 It makes sense to split if we inline, that is if we have direct calls to
1541 handle or direct calls are possibly going to appear as result of indirect
1542 inlining or LTO. Also handle -fprofile-generate as LTO to allow non-LTO
1543 training for LTO -fprofile-use build.
1545 Note that we are not completely conservative about disqualifying functions
1546 called once. It is possible that the caller is called more then once and
1547 then inlining would still benefit. */
1548 if ((!node->callers
1549 /* Local functions called once will be completely inlined most of time. */
1550 || (!node->callers->next_caller && node->local.local))
1551 && !node->symbol.address_taken
1552 && (!flag_lto || !node->symbol.externally_visible))
1554 if (dump_file)
1555 fprintf (dump_file, "Not splitting: not called directly "
1556 "or called once.\n");
1557 return 0;
1560 /* FIXME: We can actually split if splitting reduces call overhead. */
1561 if (!flag_inline_small_functions
1562 && !DECL_DECLARED_INLINE_P (current_function_decl))
1564 if (dump_file)
1565 fprintf (dump_file, "Not splitting: not autoinlining and function"
1566 " is not inline.\n");
1567 return 0;
1570 /* We enforce splitting after loop headers when profile info is not
1571 available. */
1572 if (profile_status != PROFILE_READ)
1573 mark_dfs_back_edges ();
1575 /* Initialize bitmap to track forbidden calls. */
1576 forbidden_dominators = BITMAP_ALLOC (NULL);
1577 calculate_dominance_info (CDI_DOMINATORS);
1579 /* Compute local info about basic blocks and determine function size/time. */
1580 bb_info_vec.safe_grow_cleared (last_basic_block + 1);
1581 memset (&best_split_point, 0, sizeof (best_split_point));
1582 FOR_EACH_BB (bb)
1584 int time = 0;
1585 int size = 0;
1586 int freq = compute_call_stmt_bb_frequency (current_function_decl, bb);
1588 if (dump_file && (dump_flags & TDF_DETAILS))
1589 fprintf (dump_file, "Basic block %i\n", bb->index);
1591 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1593 int this_time, this_size;
1594 gimple stmt = gsi_stmt (bsi);
1596 this_size = estimate_num_insns (stmt, &eni_size_weights);
1597 this_time = estimate_num_insns (stmt, &eni_time_weights) * freq;
1598 size += this_size;
1599 time += this_time;
1600 check_forbidden_calls (stmt);
1602 if (dump_file && (dump_flags & TDF_DETAILS))
1604 fprintf (dump_file, " freq:%6i size:%3i time:%3i ",
1605 freq, this_size, this_time);
1606 print_gimple_stmt (dump_file, stmt, 0, 0);
1609 overall_time += time;
1610 overall_size += size;
1611 bb_info_vec[bb->index].time = time;
1612 bb_info_vec[bb->index].size = size;
1614 find_split_points (overall_time, overall_size);
1615 if (best_split_point.split_bbs)
1617 split_function (&best_split_point);
1618 BITMAP_FREE (best_split_point.ssa_names_to_pass);
1619 BITMAP_FREE (best_split_point.split_bbs);
1620 todo = TODO_update_ssa | TODO_cleanup_cfg;
1622 BITMAP_FREE (forbidden_dominators);
1623 bb_info_vec.release ();
1624 return todo;
1627 /* Gate function splitting pass. When doing profile feedback, we want
1628 to execute the pass after profiling is read. So disable one in
1629 early optimization. */
1631 static bool
1632 gate_split_functions (void)
1634 return (flag_partial_inlining
1635 && !profile_arc_flag && !flag_branch_probabilities);
1638 namespace {
1640 const pass_data pass_data_split_functions =
1642 GIMPLE_PASS, /* type */
1643 "fnsplit", /* name */
1644 OPTGROUP_NONE, /* optinfo_flags */
1645 true, /* has_gate */
1646 true, /* has_execute */
1647 TV_IPA_FNSPLIT, /* tv_id */
1648 PROP_cfg, /* properties_required */
1649 0, /* properties_provided */
1650 0, /* properties_destroyed */
1651 0, /* todo_flags_start */
1652 TODO_verify_all, /* todo_flags_finish */
1655 class pass_split_functions : public gimple_opt_pass
1657 public:
1658 pass_split_functions (gcc::context *ctxt)
1659 : gimple_opt_pass (pass_data_split_functions, ctxt)
1662 /* opt_pass methods: */
1663 bool gate () { return gate_split_functions (); }
1664 unsigned int execute () { return execute_split_functions (); }
1666 }; // class pass_split_functions
1668 } // anon namespace
1670 gimple_opt_pass *
1671 make_pass_split_functions (gcc::context *ctxt)
1673 return new pass_split_functions (ctxt);
1676 /* Gate feedback driven function splitting pass.
1677 We don't need to split when profiling at all, we are producing
1678 lousy code anyway. */
1680 static bool
1681 gate_feedback_split_functions (void)
1683 return (flag_partial_inlining
1684 && flag_branch_probabilities);
1687 /* Execute function splitting pass. */
1689 static unsigned int
1690 execute_feedback_split_functions (void)
1692 unsigned int retval = execute_split_functions ();
1693 if (retval)
1694 retval |= TODO_rebuild_cgraph_edges;
1695 return retval;
1698 namespace {
1700 const pass_data pass_data_feedback_split_functions =
1702 GIMPLE_PASS, /* type */
1703 "feedback_fnsplit", /* name */
1704 OPTGROUP_NONE, /* optinfo_flags */
1705 true, /* has_gate */
1706 true, /* has_execute */
1707 TV_IPA_FNSPLIT, /* tv_id */
1708 PROP_cfg, /* properties_required */
1709 0, /* properties_provided */
1710 0, /* properties_destroyed */
1711 0, /* todo_flags_start */
1712 TODO_verify_all, /* todo_flags_finish */
1715 class pass_feedback_split_functions : public gimple_opt_pass
1717 public:
1718 pass_feedback_split_functions (gcc::context *ctxt)
1719 : gimple_opt_pass (pass_data_feedback_split_functions, ctxt)
1722 /* opt_pass methods: */
1723 bool gate () { return gate_feedback_split_functions (); }
1724 unsigned int execute () { return execute_feedback_split_functions (); }
1726 }; // class pass_feedback_split_functions
1728 } // anon namespace
1730 gimple_opt_pass *
1731 make_pass_feedback_split_functions (gcc::context *ctxt)
1733 return new pass_feedback_split_functions (ctxt);