* config/rl78/rl78-expand.md (umulqihi3): Disable for G10.
[official-gcc.git] / gcc / ipa-split.c
blob2af3a93973b68863ab2d64dd9c3fcef5c40885f3
1 /* Function splitting pass
2 Copyright (C) 2010-2014 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 "basic-block.h"
82 #include "tree-ssa-alias.h"
83 #include "internal-fn.h"
84 #include "gimple-expr.h"
85 #include "is-a.h"
86 #include "gimple.h"
87 #include "stringpool.h"
88 #include "expr.h"
89 #include "calls.h"
90 #include "gimplify.h"
91 #include "gimple-iterator.h"
92 #include "gimplify-me.h"
93 #include "gimple-walk.h"
94 #include "target.h"
95 #include "ipa-prop.h"
96 #include "gimple-ssa.h"
97 #include "tree-cfg.h"
98 #include "tree-phinodes.h"
99 #include "ssa-iterators.h"
100 #include "stringpool.h"
101 #include "tree-ssanames.h"
102 #include "tree-into-ssa.h"
103 #include "tree-dfa.h"
104 #include "tree-pass.h"
105 #include "flags.h"
106 #include "diagnostic.h"
107 #include "tree-dump.h"
108 #include "tree-inline.h"
109 #include "params.h"
110 #include "gimple-pretty-print.h"
111 #include "ipa-inline.h"
112 #include "cfgloop.h"
114 /* Per basic block info. */
116 typedef struct
118 unsigned int size;
119 unsigned int time;
120 } bb_info;
122 static vec<bb_info> bb_info_vec;
124 /* Description of split point. */
126 struct split_point
128 /* Size of the partitions. */
129 unsigned int header_time, header_size, split_time, split_size;
131 /* SSA names that need to be passed into spit function. */
132 bitmap ssa_names_to_pass;
134 /* Basic block where we split (that will become entry point of new function. */
135 basic_block entry_bb;
137 /* Basic blocks we are splitting away. */
138 bitmap split_bbs;
140 /* True when return value is computed on split part and thus it needs
141 to be returned. */
142 bool split_part_set_retval;
145 /* Best split point found. */
147 struct split_point best_split_point;
149 /* Set of basic blocks that are not allowed to dominate a split point. */
151 static bitmap forbidden_dominators;
153 static tree find_retval (basic_block return_bb);
155 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
156 variable, check it if it is present in bitmap passed via DATA. */
158 static bool
159 test_nonssa_use (gimple, tree t, tree, void *data)
161 t = get_base_address (t);
163 if (!t || is_gimple_reg (t))
164 return false;
166 if (TREE_CODE (t) == PARM_DECL
167 || (TREE_CODE (t) == VAR_DECL
168 && auto_var_in_fn_p (t, current_function_decl))
169 || TREE_CODE (t) == RESULT_DECL
170 || TREE_CODE (t) == LABEL_DECL)
171 return bitmap_bit_p ((bitmap)data, DECL_UID (t));
173 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
174 to pretend that the value pointed to is actual result decl. */
175 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
176 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
177 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
178 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
179 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
180 return
181 bitmap_bit_p ((bitmap)data,
182 DECL_UID (DECL_RESULT (current_function_decl)));
184 return false;
187 /* Dump split point CURRENT. */
189 static void
190 dump_split_point (FILE * file, struct split_point *current)
192 fprintf (file,
193 "Split point at BB %i\n"
194 " header time: %i header size: %i\n"
195 " split time: %i split size: %i\n bbs: ",
196 current->entry_bb->index, current->header_time,
197 current->header_size, current->split_time, current->split_size);
198 dump_bitmap (file, current->split_bbs);
199 fprintf (file, " SSA names to pass: ");
200 dump_bitmap (file, current->ssa_names_to_pass);
203 /* Look for all BBs in header that might lead to the split part and verify
204 that they are not defining any non-SSA var used by the split part.
205 Parameters are the same as for consider_split. */
207 static bool
208 verify_non_ssa_vars (struct split_point *current, bitmap non_ssa_vars,
209 basic_block return_bb)
211 bitmap seen = BITMAP_ALLOC (NULL);
212 vec<basic_block> worklist = vNULL;
213 edge e;
214 edge_iterator ei;
215 bool ok = true;
217 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
218 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
219 && !bitmap_bit_p (current->split_bbs, e->src->index))
221 worklist.safe_push (e->src);
222 bitmap_set_bit (seen, e->src->index);
225 while (!worklist.is_empty ())
227 gimple_stmt_iterator bsi;
228 basic_block bb = worklist.pop ();
230 FOR_EACH_EDGE (e, ei, bb->preds)
231 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
232 && bitmap_set_bit (seen, e->src->index))
234 gcc_checking_assert (!bitmap_bit_p (current->split_bbs,
235 e->src->index));
236 worklist.safe_push (e->src);
238 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
240 gimple stmt = gsi_stmt (bsi);
241 if (is_gimple_debug (stmt))
242 continue;
243 if (walk_stmt_load_store_addr_ops
244 (stmt, non_ssa_vars, test_nonssa_use, test_nonssa_use,
245 test_nonssa_use))
247 ok = false;
248 goto done;
250 if (gimple_code (stmt) == GIMPLE_LABEL
251 && test_nonssa_use (stmt, gimple_label_label (stmt),
252 NULL_TREE, non_ssa_vars))
254 ok = false;
255 goto done;
258 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
260 if (walk_stmt_load_store_addr_ops
261 (gsi_stmt (bsi), non_ssa_vars, test_nonssa_use, test_nonssa_use,
262 test_nonssa_use))
264 ok = false;
265 goto done;
268 FOR_EACH_EDGE (e, ei, bb->succs)
270 if (e->dest != return_bb)
271 continue;
272 for (bsi = gsi_start_phis (return_bb); !gsi_end_p (bsi);
273 gsi_next (&bsi))
275 gimple stmt = gsi_stmt (bsi);
276 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
278 if (virtual_operand_p (gimple_phi_result (stmt)))
279 continue;
280 if (TREE_CODE (op) != SSA_NAME
281 && test_nonssa_use (stmt, op, op, non_ssa_vars))
283 ok = false;
284 goto done;
289 done:
290 BITMAP_FREE (seen);
291 worklist.release ();
292 return ok;
295 /* If STMT is a call, check the callee against a list of forbidden
296 predicate functions. If a match is found, look for uses of the
297 call result in condition statements that compare against zero.
298 For each such use, find the block targeted by the condition
299 statement for the nonzero result, and set the bit for this block
300 in the forbidden dominators bitmap. The purpose of this is to avoid
301 selecting a split point where we are likely to lose the chance
302 to optimize away an unused function call. */
304 static void
305 check_forbidden_calls (gimple stmt)
307 imm_use_iterator use_iter;
308 use_operand_p use_p;
309 tree lhs;
311 /* At the moment, __builtin_constant_p is the only forbidden
312 predicate function call (see PR49642). */
313 if (!gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P))
314 return;
316 lhs = gimple_call_lhs (stmt);
318 if (!lhs || TREE_CODE (lhs) != SSA_NAME)
319 return;
321 FOR_EACH_IMM_USE_FAST (use_p, use_iter, lhs)
323 tree op1;
324 basic_block use_bb, forbidden_bb;
325 enum tree_code code;
326 edge true_edge, false_edge;
327 gimple use_stmt = USE_STMT (use_p);
329 if (gimple_code (use_stmt) != GIMPLE_COND)
330 continue;
332 /* Assuming canonical form for GIMPLE_COND here, with constant
333 in second position. */
334 op1 = gimple_cond_rhs (use_stmt);
335 code = gimple_cond_code (use_stmt);
336 use_bb = gimple_bb (use_stmt);
338 extract_true_false_edges_from_block (use_bb, &true_edge, &false_edge);
340 /* We're only interested in comparisons that distinguish
341 unambiguously from zero. */
342 if (!integer_zerop (op1) || code == LE_EXPR || code == GE_EXPR)
343 continue;
345 if (code == EQ_EXPR)
346 forbidden_bb = false_edge->dest;
347 else
348 forbidden_bb = true_edge->dest;
350 bitmap_set_bit (forbidden_dominators, forbidden_bb->index);
354 /* If BB is dominated by any block in the forbidden dominators set,
355 return TRUE; else FALSE. */
357 static bool
358 dominated_by_forbidden (basic_block bb)
360 unsigned dom_bb;
361 bitmap_iterator bi;
363 EXECUTE_IF_SET_IN_BITMAP (forbidden_dominators, 1, dom_bb, bi)
365 if (dominated_by_p (CDI_DOMINATORS, bb,
366 BASIC_BLOCK_FOR_FN (cfun, dom_bb)))
367 return true;
370 return false;
373 /* We found an split_point CURRENT. NON_SSA_VARS is bitmap of all non ssa
374 variables used and RETURN_BB is return basic block.
375 See if we can split function here. */
377 static void
378 consider_split (struct split_point *current, bitmap non_ssa_vars,
379 basic_block return_bb)
381 tree parm;
382 unsigned int num_args = 0;
383 unsigned int call_overhead;
384 edge e;
385 edge_iterator ei;
386 gimple_stmt_iterator bsi;
387 unsigned int i;
388 int incoming_freq = 0;
389 tree retval;
390 bool back_edge = false;
392 if (dump_file && (dump_flags & TDF_DETAILS))
393 dump_split_point (dump_file, current);
395 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
397 if (e->flags & EDGE_DFS_BACK)
398 back_edge = true;
399 if (!bitmap_bit_p (current->split_bbs, e->src->index))
400 incoming_freq += EDGE_FREQUENCY (e);
403 /* Do not split when we would end up calling function anyway. */
404 if (incoming_freq
405 >= (ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency
406 * PARAM_VALUE (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY) / 100))
408 /* When profile is guessed, we can not expect it to give us
409 realistic estimate on likelyness of function taking the
410 complex path. As a special case, when tail of the function is
411 a loop, enable splitting since inlining code skipping the loop
412 is likely noticeable win. */
413 if (back_edge
414 && profile_status_for_fn (cfun) != PROFILE_READ
415 && incoming_freq < ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency)
417 if (dump_file && (dump_flags & TDF_DETAILS))
418 fprintf (dump_file,
419 " Split before loop, accepting despite low frequencies %i %i.\n",
420 incoming_freq,
421 ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency);
423 else
425 if (dump_file && (dump_flags & TDF_DETAILS))
426 fprintf (dump_file,
427 " Refused: incoming frequency is too large.\n");
428 return;
432 if (!current->header_size)
434 if (dump_file && (dump_flags & TDF_DETAILS))
435 fprintf (dump_file, " Refused: header empty\n");
436 return;
439 /* Verify that PHI args on entry are either virtual or all their operands
440 incoming from header are the same. */
441 for (bsi = gsi_start_phis (current->entry_bb); !gsi_end_p (bsi); gsi_next (&bsi))
443 gimple stmt = gsi_stmt (bsi);
444 tree val = NULL;
446 if (virtual_operand_p (gimple_phi_result (stmt)))
447 continue;
448 for (i = 0; i < gimple_phi_num_args (stmt); i++)
450 edge e = gimple_phi_arg_edge (stmt, i);
451 if (!bitmap_bit_p (current->split_bbs, e->src->index))
453 tree edge_val = gimple_phi_arg_def (stmt, i);
454 if (val && edge_val != val)
456 if (dump_file && (dump_flags & TDF_DETAILS))
457 fprintf (dump_file,
458 " Refused: entry BB has PHI with multiple variants\n");
459 return;
461 val = edge_val;
467 /* See what argument we will pass to the split function and compute
468 call overhead. */
469 call_overhead = eni_size_weights.call_cost;
470 for (parm = DECL_ARGUMENTS (current_function_decl); parm;
471 parm = DECL_CHAIN (parm))
473 if (!is_gimple_reg (parm))
475 if (bitmap_bit_p (non_ssa_vars, DECL_UID (parm)))
477 if (dump_file && (dump_flags & TDF_DETAILS))
478 fprintf (dump_file,
479 " Refused: need to pass non-ssa param values\n");
480 return;
483 else
485 tree ddef = ssa_default_def (cfun, parm);
486 if (ddef
487 && bitmap_bit_p (current->ssa_names_to_pass,
488 SSA_NAME_VERSION (ddef)))
490 if (!VOID_TYPE_P (TREE_TYPE (parm)))
491 call_overhead += estimate_move_cost (TREE_TYPE (parm), false);
492 num_args++;
496 if (!VOID_TYPE_P (TREE_TYPE (current_function_decl)))
497 call_overhead += estimate_move_cost (TREE_TYPE (current_function_decl),
498 false);
500 if (current->split_size <= call_overhead)
502 if (dump_file && (dump_flags & TDF_DETAILS))
503 fprintf (dump_file,
504 " Refused: split size is smaller than call overhead\n");
505 return;
507 if (current->header_size + call_overhead
508 >= (unsigned int)(DECL_DECLARED_INLINE_P (current_function_decl)
509 ? MAX_INLINE_INSNS_SINGLE
510 : MAX_INLINE_INSNS_AUTO))
512 if (dump_file && (dump_flags & TDF_DETAILS))
513 fprintf (dump_file,
514 " Refused: header size is too large for inline candidate\n");
515 return;
518 /* FIXME: we currently can pass only SSA function parameters to the split
519 arguments. Once parm_adjustment infrastructure is supported by cloning,
520 we can pass more than that. */
521 if (num_args != bitmap_count_bits (current->ssa_names_to_pass))
524 if (dump_file && (dump_flags & TDF_DETAILS))
525 fprintf (dump_file,
526 " Refused: need to pass non-param values\n");
527 return;
530 /* When there are non-ssa vars used in the split region, see if they
531 are used in the header region. If so, reject the split.
532 FIXME: we can use nested function support to access both. */
533 if (!bitmap_empty_p (non_ssa_vars)
534 && !verify_non_ssa_vars (current, non_ssa_vars, return_bb))
536 if (dump_file && (dump_flags & TDF_DETAILS))
537 fprintf (dump_file,
538 " Refused: split part has non-ssa uses\n");
539 return;
542 /* If the split point is dominated by a forbidden block, reject
543 the split. */
544 if (!bitmap_empty_p (forbidden_dominators)
545 && dominated_by_forbidden (current->entry_bb))
547 if (dump_file && (dump_flags & TDF_DETAILS))
548 fprintf (dump_file,
549 " Refused: split point dominated by forbidden block\n");
550 return;
553 /* See if retval used by return bb is computed by header or split part.
554 When it is computed by split part, we need to produce return statement
555 in the split part and add code to header to pass it around.
557 This is bit tricky to test:
558 1) When there is no return_bb or no return value, we always pass
559 value around.
560 2) Invariants are always computed by caller.
561 3) For SSA we need to look if defining statement is in header or split part
562 4) For non-SSA we need to look where the var is computed. */
563 retval = find_retval (return_bb);
564 if (!retval)
565 current->split_part_set_retval = true;
566 else if (is_gimple_min_invariant (retval))
567 current->split_part_set_retval = false;
568 /* Special case is value returned by reference we record as if it was non-ssa
569 set to result_decl. */
570 else if (TREE_CODE (retval) == SSA_NAME
571 && SSA_NAME_VAR (retval)
572 && TREE_CODE (SSA_NAME_VAR (retval)) == RESULT_DECL
573 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
574 current->split_part_set_retval
575 = bitmap_bit_p (non_ssa_vars, DECL_UID (SSA_NAME_VAR (retval)));
576 else if (TREE_CODE (retval) == SSA_NAME)
577 current->split_part_set_retval
578 = (!SSA_NAME_IS_DEFAULT_DEF (retval)
579 && (bitmap_bit_p (current->split_bbs,
580 gimple_bb (SSA_NAME_DEF_STMT (retval))->index)
581 || gimple_bb (SSA_NAME_DEF_STMT (retval)) == return_bb));
582 else if (TREE_CODE (retval) == PARM_DECL)
583 current->split_part_set_retval = false;
584 else if (TREE_CODE (retval) == VAR_DECL
585 || TREE_CODE (retval) == RESULT_DECL)
586 current->split_part_set_retval
587 = bitmap_bit_p (non_ssa_vars, DECL_UID (retval));
588 else
589 current->split_part_set_retval = true;
591 /* split_function fixes up at most one PHI non-virtual PHI node in return_bb,
592 for the return value. If there are other PHIs, give up. */
593 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
595 gimple_stmt_iterator psi;
597 for (psi = gsi_start_phis (return_bb); !gsi_end_p (psi); gsi_next (&psi))
598 if (!virtual_operand_p (gimple_phi_result (gsi_stmt (psi)))
599 && !(retval
600 && current->split_part_set_retval
601 && TREE_CODE (retval) == SSA_NAME
602 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
603 && SSA_NAME_DEF_STMT (retval) == gsi_stmt (psi)))
605 if (dump_file && (dump_flags & TDF_DETAILS))
606 fprintf (dump_file,
607 " Refused: return bb has extra PHIs\n");
608 return;
612 if (dump_file && (dump_flags & TDF_DETAILS))
613 fprintf (dump_file, " Accepted!\n");
615 /* At the moment chose split point with lowest frequency and that leaves
616 out smallest size of header.
617 In future we might re-consider this heuristics. */
618 if (!best_split_point.split_bbs
619 || best_split_point.entry_bb->frequency > current->entry_bb->frequency
620 || (best_split_point.entry_bb->frequency == current->entry_bb->frequency
621 && best_split_point.split_size < current->split_size))
624 if (dump_file && (dump_flags & TDF_DETAILS))
625 fprintf (dump_file, " New best split point!\n");
626 if (best_split_point.ssa_names_to_pass)
628 BITMAP_FREE (best_split_point.ssa_names_to_pass);
629 BITMAP_FREE (best_split_point.split_bbs);
631 best_split_point = *current;
632 best_split_point.ssa_names_to_pass = BITMAP_ALLOC (NULL);
633 bitmap_copy (best_split_point.ssa_names_to_pass,
634 current->ssa_names_to_pass);
635 best_split_point.split_bbs = BITMAP_ALLOC (NULL);
636 bitmap_copy (best_split_point.split_bbs, current->split_bbs);
640 /* Return basic block containing RETURN statement. We allow basic blocks
641 of the form:
642 <retval> = tmp_var;
643 return <retval>
644 but return_bb can not be more complex than this.
645 If nothing is found, return the exit block.
647 When there are multiple RETURN statement, chose one with return value,
648 since that one is more likely shared by multiple code paths.
650 Return BB is special, because for function splitting it is the only
651 basic block that is duplicated in between header and split part of the
652 function.
654 TODO: We might support multiple return blocks. */
656 static basic_block
657 find_return_bb (void)
659 edge e;
660 basic_block return_bb = EXIT_BLOCK_PTR_FOR_FN (cfun);
661 gimple_stmt_iterator bsi;
662 bool found_return = false;
663 tree retval = NULL_TREE;
665 if (!single_pred_p (EXIT_BLOCK_PTR_FOR_FN (cfun)))
666 return return_bb;
668 e = single_pred_edge (EXIT_BLOCK_PTR_FOR_FN (cfun));
669 for (bsi = gsi_last_bb (e->src); !gsi_end_p (bsi); gsi_prev (&bsi))
671 gimple stmt = gsi_stmt (bsi);
672 if (gimple_code (stmt) == GIMPLE_LABEL
673 || is_gimple_debug (stmt)
674 || gimple_clobber_p (stmt))
676 else if (gimple_code (stmt) == GIMPLE_ASSIGN
677 && found_return
678 && gimple_assign_single_p (stmt)
679 && (auto_var_in_fn_p (gimple_assign_rhs1 (stmt),
680 current_function_decl)
681 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
682 && retval == gimple_assign_lhs (stmt))
684 else if (gimple_code (stmt) == GIMPLE_RETURN)
686 found_return = true;
687 retval = gimple_return_retval (stmt);
689 else
690 break;
692 if (gsi_end_p (bsi) && found_return)
693 return_bb = e->src;
695 return return_bb;
698 /* Given return basic block RETURN_BB, see where return value is really
699 stored. */
700 static tree
701 find_retval (basic_block return_bb)
703 gimple_stmt_iterator bsi;
704 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
705 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
706 return gimple_return_retval (gsi_stmt (bsi));
707 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
708 && !gimple_clobber_p (gsi_stmt (bsi)))
709 return gimple_assign_rhs1 (gsi_stmt (bsi));
710 return NULL;
713 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
714 variable, mark it as used in bitmap passed via DATA.
715 Return true when access to T prevents splitting the function. */
717 static bool
718 mark_nonssa_use (gimple, tree t, tree, void *data)
720 t = get_base_address (t);
722 if (!t || is_gimple_reg (t))
723 return false;
725 /* At present we can't pass non-SSA arguments to split function.
726 FIXME: this can be relaxed by passing references to arguments. */
727 if (TREE_CODE (t) == PARM_DECL)
729 if (dump_file && (dump_flags & TDF_DETAILS))
730 fprintf (dump_file,
731 "Cannot split: use of non-ssa function parameter.\n");
732 return true;
735 if ((TREE_CODE (t) == VAR_DECL
736 && auto_var_in_fn_p (t, current_function_decl))
737 || TREE_CODE (t) == RESULT_DECL
738 || TREE_CODE (t) == LABEL_DECL)
739 bitmap_set_bit ((bitmap)data, DECL_UID (t));
741 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
742 to pretend that the value pointed to is actual result decl. */
743 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
744 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
745 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
746 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
747 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
748 return
749 bitmap_bit_p ((bitmap)data,
750 DECL_UID (DECL_RESULT (current_function_decl)));
752 return false;
755 /* Compute local properties of basic block BB we collect when looking for
756 split points. We look for ssa defs and store them in SET_SSA_NAMES,
757 for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic
758 vars stored in NON_SSA_VARS.
760 When BB has edge to RETURN_BB, collect uses in RETURN_BB too.
762 Return false when BB contains something that prevents it from being put into
763 split function. */
765 static bool
766 visit_bb (basic_block bb, basic_block return_bb,
767 bitmap set_ssa_names, bitmap used_ssa_names,
768 bitmap non_ssa_vars)
770 gimple_stmt_iterator bsi;
771 edge e;
772 edge_iterator ei;
773 bool can_split = true;
775 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
777 gimple stmt = gsi_stmt (bsi);
778 tree op;
779 ssa_op_iter iter;
780 tree decl;
782 if (is_gimple_debug (stmt))
783 continue;
785 if (gimple_clobber_p (stmt))
786 continue;
788 /* FIXME: We can split regions containing EH. We can not however
789 split RESX, EH_DISPATCH and EH_POINTER referring to same region
790 into different partitions. This would require tracking of
791 EH regions and checking in consider_split_point if they
792 are not used elsewhere. */
793 if (gimple_code (stmt) == GIMPLE_RESX)
795 if (dump_file && (dump_flags & TDF_DETAILS))
796 fprintf (dump_file, "Cannot split: resx.\n");
797 can_split = false;
799 if (gimple_code (stmt) == GIMPLE_EH_DISPATCH)
801 if (dump_file && (dump_flags & TDF_DETAILS))
802 fprintf (dump_file, "Cannot split: eh dispatch.\n");
803 can_split = false;
806 /* Check builtins that prevent splitting. */
807 if (gimple_code (stmt) == GIMPLE_CALL
808 && (decl = gimple_call_fndecl (stmt)) != NULL_TREE
809 && DECL_BUILT_IN (decl)
810 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
811 switch (DECL_FUNCTION_CODE (decl))
813 /* FIXME: once we will allow passing non-parm values to split part,
814 we need to be sure to handle correct builtin_stack_save and
815 builtin_stack_restore. At the moment we are safe; there is no
816 way to store builtin_stack_save result in non-SSA variable
817 since all calls to those are compiler generated. */
818 case BUILT_IN_APPLY:
819 case BUILT_IN_APPLY_ARGS:
820 case BUILT_IN_VA_START:
821 if (dump_file && (dump_flags & TDF_DETAILS))
822 fprintf (dump_file,
823 "Cannot split: builtin_apply and va_start.\n");
824 can_split = false;
825 break;
826 case BUILT_IN_EH_POINTER:
827 if (dump_file && (dump_flags & TDF_DETAILS))
828 fprintf (dump_file, "Cannot split: builtin_eh_pointer.\n");
829 can_split = false;
830 break;
831 default:
832 break;
835 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
836 bitmap_set_bit (set_ssa_names, SSA_NAME_VERSION (op));
837 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
838 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
839 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
840 mark_nonssa_use,
841 mark_nonssa_use,
842 mark_nonssa_use);
844 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
846 gimple stmt = gsi_stmt (bsi);
847 unsigned int i;
849 if (virtual_operand_p (gimple_phi_result (stmt)))
850 continue;
851 bitmap_set_bit (set_ssa_names,
852 SSA_NAME_VERSION (gimple_phi_result (stmt)));
853 for (i = 0; i < gimple_phi_num_args (stmt); i++)
855 tree op = gimple_phi_arg_def (stmt, i);
856 if (TREE_CODE (op) == SSA_NAME)
857 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
859 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
860 mark_nonssa_use,
861 mark_nonssa_use,
862 mark_nonssa_use);
864 /* Record also uses coming from PHI operand in return BB. */
865 FOR_EACH_EDGE (e, ei, bb->succs)
866 if (e->dest == return_bb)
868 for (bsi = gsi_start_phis (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
870 gimple stmt = gsi_stmt (bsi);
871 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
873 if (virtual_operand_p (gimple_phi_result (stmt)))
874 continue;
875 if (TREE_CODE (op) == SSA_NAME)
876 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
877 else
878 can_split &= !mark_nonssa_use (stmt, op, op, non_ssa_vars);
881 return can_split;
884 /* Stack entry for recursive DFS walk in find_split_point. */
886 typedef struct
888 /* Basic block we are examining. */
889 basic_block bb;
891 /* SSA names set and used by the BB and all BBs reachable
892 from it via DFS walk. */
893 bitmap set_ssa_names, used_ssa_names;
894 bitmap non_ssa_vars;
896 /* All BBS visited from this BB via DFS walk. */
897 bitmap bbs_visited;
899 /* Last examined edge in DFS walk. Since we walk unoriented graph,
900 the value is up to sum of incoming and outgoing edges of BB. */
901 unsigned int edge_num;
903 /* Stack entry index of earliest BB reachable from current BB
904 or any BB visited later in DFS walk. */
905 int earliest;
907 /* Overall time and size of all BBs reached from this BB in DFS walk. */
908 int overall_time, overall_size;
910 /* When false we can not split on this BB. */
911 bool can_split;
912 } stack_entry;
915 /* Find all articulations and call consider_split on them.
916 OVERALL_TIME and OVERALL_SIZE is time and size of the function.
918 We perform basic algorithm for finding an articulation in a graph
919 created from CFG by considering it to be an unoriented graph.
921 The articulation is discovered via DFS walk. We collect earliest
922 basic block on stack that is reachable via backward edge. Articulation
923 is any basic block such that there is no backward edge bypassing it.
924 To reduce stack usage we maintain heap allocated stack in STACK vector.
925 AUX pointer of BB is set to index it appears in the stack or -1 once
926 it is visited and popped off the stack.
928 The algorithm finds articulation after visiting the whole component
929 reachable by it. This makes it convenient to collect information about
930 the component used by consider_split. */
932 static void
933 find_split_points (int overall_time, int overall_size)
935 stack_entry first;
936 vec<stack_entry> stack = vNULL;
937 basic_block bb;
938 basic_block return_bb = find_return_bb ();
939 struct split_point current;
941 current.header_time = overall_time;
942 current.header_size = overall_size;
943 current.split_time = 0;
944 current.split_size = 0;
945 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
947 first.bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
948 first.edge_num = 0;
949 first.overall_time = 0;
950 first.overall_size = 0;
951 first.earliest = INT_MAX;
952 first.set_ssa_names = 0;
953 first.used_ssa_names = 0;
954 first.non_ssa_vars = 0;
955 first.bbs_visited = 0;
956 first.can_split = false;
957 stack.safe_push (first);
958 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = (void *)(intptr_t)-1;
960 while (!stack.is_empty ())
962 stack_entry *entry = &stack.last ();
964 /* We are walking an acyclic graph, so edge_num counts
965 succ and pred edges together. However when considering
966 articulation, we want to have processed everything reachable
967 from articulation but nothing that reaches into it. */
968 if (entry->edge_num == EDGE_COUNT (entry->bb->succs)
969 && entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
971 int pos = stack.length ();
972 entry->can_split &= visit_bb (entry->bb, return_bb,
973 entry->set_ssa_names,
974 entry->used_ssa_names,
975 entry->non_ssa_vars);
976 if (pos <= entry->earliest && !entry->can_split
977 && dump_file && (dump_flags & TDF_DETAILS))
978 fprintf (dump_file,
979 "found articulation at bb %i but can not split\n",
980 entry->bb->index);
981 if (pos <= entry->earliest && entry->can_split)
983 if (dump_file && (dump_flags & TDF_DETAILS))
984 fprintf (dump_file, "found articulation at bb %i\n",
985 entry->bb->index);
986 current.entry_bb = entry->bb;
987 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
988 bitmap_and_compl (current.ssa_names_to_pass,
989 entry->used_ssa_names, entry->set_ssa_names);
990 current.header_time = overall_time - entry->overall_time;
991 current.header_size = overall_size - entry->overall_size;
992 current.split_time = entry->overall_time;
993 current.split_size = entry->overall_size;
994 current.split_bbs = entry->bbs_visited;
995 consider_split (&current, entry->non_ssa_vars, return_bb);
996 BITMAP_FREE (current.ssa_names_to_pass);
999 /* Do actual DFS walk. */
1000 if (entry->edge_num
1001 < (EDGE_COUNT (entry->bb->succs)
1002 + EDGE_COUNT (entry->bb->preds)))
1004 edge e;
1005 basic_block dest;
1006 if (entry->edge_num < EDGE_COUNT (entry->bb->succs))
1008 e = EDGE_SUCC (entry->bb, entry->edge_num);
1009 dest = e->dest;
1011 else
1013 e = EDGE_PRED (entry->bb, entry->edge_num
1014 - EDGE_COUNT (entry->bb->succs));
1015 dest = e->src;
1018 entry->edge_num++;
1020 /* New BB to visit, push it to the stack. */
1021 if (dest != return_bb && dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
1022 && !dest->aux)
1024 stack_entry new_entry;
1026 new_entry.bb = dest;
1027 new_entry.edge_num = 0;
1028 new_entry.overall_time
1029 = bb_info_vec[dest->index].time;
1030 new_entry.overall_size
1031 = bb_info_vec[dest->index].size;
1032 new_entry.earliest = INT_MAX;
1033 new_entry.set_ssa_names = BITMAP_ALLOC (NULL);
1034 new_entry.used_ssa_names = BITMAP_ALLOC (NULL);
1035 new_entry.bbs_visited = BITMAP_ALLOC (NULL);
1036 new_entry.non_ssa_vars = BITMAP_ALLOC (NULL);
1037 new_entry.can_split = true;
1038 bitmap_set_bit (new_entry.bbs_visited, dest->index);
1039 stack.safe_push (new_entry);
1040 dest->aux = (void *)(intptr_t)stack.length ();
1042 /* Back edge found, record the earliest point. */
1043 else if ((intptr_t)dest->aux > 0
1044 && (intptr_t)dest->aux < entry->earliest)
1045 entry->earliest = (intptr_t)dest->aux;
1047 /* We are done with examining the edges. Pop off the value from stack
1048 and merge stuff we accumulate during the walk. */
1049 else if (entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1051 stack_entry *prev = &stack[stack.length () - 2];
1053 entry->bb->aux = (void *)(intptr_t)-1;
1054 prev->can_split &= entry->can_split;
1055 if (prev->set_ssa_names)
1057 bitmap_ior_into (prev->set_ssa_names, entry->set_ssa_names);
1058 bitmap_ior_into (prev->used_ssa_names, entry->used_ssa_names);
1059 bitmap_ior_into (prev->bbs_visited, entry->bbs_visited);
1060 bitmap_ior_into (prev->non_ssa_vars, entry->non_ssa_vars);
1062 if (prev->earliest > entry->earliest)
1063 prev->earliest = entry->earliest;
1064 prev->overall_time += entry->overall_time;
1065 prev->overall_size += entry->overall_size;
1066 BITMAP_FREE (entry->set_ssa_names);
1067 BITMAP_FREE (entry->used_ssa_names);
1068 BITMAP_FREE (entry->bbs_visited);
1069 BITMAP_FREE (entry->non_ssa_vars);
1070 stack.pop ();
1072 else
1073 stack.pop ();
1075 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = NULL;
1076 FOR_EACH_BB_FN (bb, cfun)
1077 bb->aux = NULL;
1078 stack.release ();
1079 BITMAP_FREE (current.ssa_names_to_pass);
1082 /* Split function at SPLIT_POINT. */
1084 static void
1085 split_function (struct split_point *split_point)
1087 vec<tree> args_to_pass = vNULL;
1088 bitmap args_to_skip;
1089 tree parm;
1090 int num = 0;
1091 cgraph_node *node, *cur_node = cgraph_node::get (current_function_decl);
1092 basic_block return_bb = find_return_bb ();
1093 basic_block call_bb;
1094 gimple_stmt_iterator gsi;
1095 gimple call;
1096 edge e;
1097 edge_iterator ei;
1098 tree retval = NULL, real_retval = NULL;
1099 bool split_part_return_p = false;
1100 gimple last_stmt = NULL;
1101 unsigned int i;
1102 tree arg, ddef;
1103 vec<tree, va_gc> **debug_args = NULL;
1105 if (dump_file)
1107 fprintf (dump_file, "\n\nSplitting function at:\n");
1108 dump_split_point (dump_file, split_point);
1111 if (cur_node->local.can_change_signature)
1112 args_to_skip = BITMAP_ALLOC (NULL);
1113 else
1114 args_to_skip = NULL;
1116 /* Collect the parameters of new function and args_to_skip bitmap. */
1117 for (parm = DECL_ARGUMENTS (current_function_decl);
1118 parm; parm = DECL_CHAIN (parm), num++)
1119 if (args_to_skip
1120 && (!is_gimple_reg (parm)
1121 || (ddef = ssa_default_def (cfun, parm)) == NULL_TREE
1122 || !bitmap_bit_p (split_point->ssa_names_to_pass,
1123 SSA_NAME_VERSION (ddef))))
1124 bitmap_set_bit (args_to_skip, num);
1125 else
1127 /* This parm might not have been used up to now, but is going to be
1128 used, hence register it. */
1129 if (is_gimple_reg (parm))
1130 arg = get_or_create_ssa_default_def (cfun, parm);
1131 else
1132 arg = parm;
1134 if (!useless_type_conversion_p (DECL_ARG_TYPE (parm), TREE_TYPE (arg)))
1135 arg = fold_convert (DECL_ARG_TYPE (parm), arg);
1136 args_to_pass.safe_push (arg);
1139 /* See if the split function will return. */
1140 FOR_EACH_EDGE (e, ei, return_bb->preds)
1141 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1142 break;
1143 if (e)
1144 split_part_return_p = true;
1146 /* Add return block to what will become the split function.
1147 We do not return; no return block is needed. */
1148 if (!split_part_return_p)
1150 /* We have no return block, so nothing is needed. */
1151 else if (return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1153 /* When we do not want to return value, we need to construct
1154 new return block with empty return statement.
1155 FIXME: Once we are able to change return type, we should change function
1156 to return void instead of just outputting function with undefined return
1157 value. For structures this affects quality of codegen. */
1158 else if (!split_point->split_part_set_retval
1159 && find_retval (return_bb))
1161 bool redirected = true;
1162 basic_block new_return_bb = create_basic_block (NULL, 0, return_bb);
1163 gimple_stmt_iterator gsi = gsi_start_bb (new_return_bb);
1164 gsi_insert_after (&gsi, gimple_build_return (NULL), GSI_NEW_STMT);
1165 while (redirected)
1167 redirected = false;
1168 FOR_EACH_EDGE (e, ei, return_bb->preds)
1169 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1171 new_return_bb->count += e->count;
1172 new_return_bb->frequency += EDGE_FREQUENCY (e);
1173 redirect_edge_and_branch (e, new_return_bb);
1174 redirected = true;
1175 break;
1178 e = make_edge (new_return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1179 e->probability = REG_BR_PROB_BASE;
1180 e->count = new_return_bb->count;
1181 add_bb_to_loop (new_return_bb, current_loops->tree_root);
1182 bitmap_set_bit (split_point->split_bbs, new_return_bb->index);
1184 /* When we pass around the value, use existing return block. */
1185 else
1186 bitmap_set_bit (split_point->split_bbs, return_bb->index);
1188 /* If RETURN_BB has virtual operand PHIs, they must be removed and the
1189 virtual operand marked for renaming as we change the CFG in a way that
1190 tree-inline is not able to compensate for.
1192 Note this can happen whether or not we have a return value. If we have
1193 a return value, then RETURN_BB may have PHIs for real operands too. */
1194 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1196 bool phi_p = false;
1197 for (gsi = gsi_start_phis (return_bb); !gsi_end_p (gsi);)
1199 gimple stmt = gsi_stmt (gsi);
1200 if (!virtual_operand_p (gimple_phi_result (stmt)))
1202 gsi_next (&gsi);
1203 continue;
1205 mark_virtual_phi_result_for_renaming (stmt);
1206 remove_phi_node (&gsi, true);
1207 phi_p = true;
1209 /* In reality we have to rename the reaching definition of the
1210 virtual operand at return_bb as we will eventually release it
1211 when we remove the code region we outlined.
1212 So we have to rename all immediate virtual uses of that region
1213 if we didn't see a PHI definition yet. */
1214 /* ??? In real reality we want to set the reaching vdef of the
1215 entry of the SESE region as the vuse of the call and the reaching
1216 vdef of the exit of the SESE region as the vdef of the call. */
1217 if (!phi_p)
1218 for (gsi = gsi_start_bb (return_bb); !gsi_end_p (gsi); gsi_next (&gsi))
1220 gimple stmt = gsi_stmt (gsi);
1221 if (gimple_vuse (stmt))
1223 gimple_set_vuse (stmt, NULL_TREE);
1224 update_stmt (stmt);
1226 if (gimple_vdef (stmt))
1227 break;
1231 /* Now create the actual clone. */
1232 rebuild_cgraph_edges ();
1233 node = cur_node->create_version_clone_with_body
1234 (vNULL, NULL, args_to_skip, !split_part_return_p, split_point->split_bbs,
1235 split_point->entry_bb, "part");
1237 /* Let's take a time profile for splitted function. */
1238 node->tp_first_run = cur_node->tp_first_run + 1;
1240 /* For usual cloning it is enough to clear builtin only when signature
1241 changes. For partial inlining we however can not expect the part
1242 of builtin implementation to have same semantic as the whole. */
1243 if (DECL_BUILT_IN (node->decl))
1245 DECL_BUILT_IN_CLASS (node->decl) = NOT_BUILT_IN;
1246 DECL_FUNCTION_CODE (node->decl) = (enum built_in_function) 0;
1248 /* If the original function is declared inline, there is no point in issuing
1249 a warning for the non-inlinable part. */
1250 DECL_NO_INLINE_WARNING_P (node->decl) = 1;
1251 cur_node->remove_callees ();
1252 cur_node->remove_all_references ();
1253 if (!split_part_return_p)
1254 TREE_THIS_VOLATILE (node->decl) = 1;
1255 if (dump_file)
1256 dump_function_to_file (node->decl, dump_file, dump_flags);
1258 /* Create the basic block we place call into. It is the entry basic block
1259 split after last label. */
1260 call_bb = split_point->entry_bb;
1261 for (gsi = gsi_start_bb (call_bb); !gsi_end_p (gsi);)
1262 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
1264 last_stmt = gsi_stmt (gsi);
1265 gsi_next (&gsi);
1267 else
1268 break;
1269 e = split_block (split_point->entry_bb, last_stmt);
1270 remove_edge (e);
1272 /* Produce the call statement. */
1273 gsi = gsi_last_bb (call_bb);
1274 FOR_EACH_VEC_ELT (args_to_pass, i, arg)
1275 if (!is_gimple_val (arg))
1277 arg = force_gimple_operand_gsi (&gsi, arg, true, NULL_TREE,
1278 false, GSI_CONTINUE_LINKING);
1279 args_to_pass[i] = arg;
1281 call = gimple_build_call_vec (node->decl, args_to_pass);
1282 gimple_set_block (call, DECL_INITIAL (current_function_decl));
1283 args_to_pass.release ();
1285 /* For optimized away parameters, add on the caller side
1286 before the call
1287 DEBUG D#X => parm_Y(D)
1288 stmts and associate D#X with parm in decl_debug_args_lookup
1289 vector to say for debug info that if parameter parm had been passed,
1290 it would have value parm_Y(D). */
1291 if (args_to_skip)
1292 for (parm = DECL_ARGUMENTS (current_function_decl), num = 0;
1293 parm; parm = DECL_CHAIN (parm), num++)
1294 if (bitmap_bit_p (args_to_skip, num)
1295 && is_gimple_reg (parm))
1297 tree ddecl;
1298 gimple def_temp;
1300 /* This needs to be done even without MAY_HAVE_DEBUG_STMTS,
1301 otherwise if it didn't exist before, we'd end up with
1302 different SSA_NAME_VERSIONs between -g and -g0. */
1303 arg = get_or_create_ssa_default_def (cfun, parm);
1304 if (!MAY_HAVE_DEBUG_STMTS)
1305 continue;
1307 if (debug_args == NULL)
1308 debug_args = decl_debug_args_insert (node->decl);
1309 ddecl = make_node (DEBUG_EXPR_DECL);
1310 DECL_ARTIFICIAL (ddecl) = 1;
1311 TREE_TYPE (ddecl) = TREE_TYPE (parm);
1312 DECL_MODE (ddecl) = DECL_MODE (parm);
1313 vec_safe_push (*debug_args, DECL_ORIGIN (parm));
1314 vec_safe_push (*debug_args, ddecl);
1315 def_temp = gimple_build_debug_bind (ddecl, unshare_expr (arg),
1316 call);
1317 gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
1319 /* And on the callee side, add
1320 DEBUG D#Y s=> parm
1321 DEBUG var => D#Y
1322 stmts to the first bb where var is a VAR_DECL created for the
1323 optimized away parameter in DECL_INITIAL block. This hints
1324 in the debug info that var (whole DECL_ORIGIN is the parm PARM_DECL)
1325 is optimized away, but could be looked up at the call site
1326 as value of D#X there. */
1327 if (debug_args != NULL)
1329 unsigned int i;
1330 tree var, vexpr;
1331 gimple_stmt_iterator cgsi;
1332 gimple def_temp;
1334 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1335 var = BLOCK_VARS (DECL_INITIAL (node->decl));
1336 i = vec_safe_length (*debug_args);
1337 cgsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1340 i -= 2;
1341 while (var != NULL_TREE
1342 && DECL_ABSTRACT_ORIGIN (var) != (**debug_args)[i])
1343 var = TREE_CHAIN (var);
1344 if (var == NULL_TREE)
1345 break;
1346 vexpr = make_node (DEBUG_EXPR_DECL);
1347 parm = (**debug_args)[i];
1348 DECL_ARTIFICIAL (vexpr) = 1;
1349 TREE_TYPE (vexpr) = TREE_TYPE (parm);
1350 DECL_MODE (vexpr) = DECL_MODE (parm);
1351 def_temp = gimple_build_debug_source_bind (vexpr, parm,
1352 NULL);
1353 gsi_insert_before (&cgsi, def_temp, GSI_SAME_STMT);
1354 def_temp = gimple_build_debug_bind (var, vexpr, NULL);
1355 gsi_insert_before (&cgsi, def_temp, GSI_SAME_STMT);
1357 while (i);
1358 pop_cfun ();
1361 /* We avoid address being taken on any variable used by split part,
1362 so return slot optimization is always possible. Moreover this is
1363 required to make DECL_BY_REFERENCE work. */
1364 if (aggregate_value_p (DECL_RESULT (current_function_decl),
1365 TREE_TYPE (current_function_decl))
1366 && (!is_gimple_reg_type (TREE_TYPE (DECL_RESULT (current_function_decl)))
1367 || DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))))
1368 gimple_call_set_return_slot_opt (call, true);
1370 /* Update return value. This is bit tricky. When we do not return,
1371 do nothing. When we return we might need to update return_bb
1372 or produce a new return statement. */
1373 if (!split_part_return_p)
1374 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1375 else
1377 e = make_edge (call_bb, return_bb,
1378 return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
1379 ? 0 : EDGE_FALLTHRU);
1380 e->count = call_bb->count;
1381 e->probability = REG_BR_PROB_BASE;
1383 /* If there is return basic block, see what value we need to store
1384 return value into and put call just before it. */
1385 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1387 real_retval = retval = find_retval (return_bb);
1389 if (real_retval && split_point->split_part_set_retval)
1391 gimple_stmt_iterator psi;
1393 /* See if we need new SSA_NAME for the result.
1394 When DECL_BY_REFERENCE is true, retval is actually pointer to
1395 return value and it is constant in whole function. */
1396 if (TREE_CODE (retval) == SSA_NAME
1397 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1399 retval = copy_ssa_name (retval, call);
1401 /* See if there is PHI defining return value. */
1402 for (psi = gsi_start_phis (return_bb);
1403 !gsi_end_p (psi); gsi_next (&psi))
1404 if (!virtual_operand_p (gimple_phi_result (gsi_stmt (psi))))
1405 break;
1407 /* When there is PHI, just update its value. */
1408 if (TREE_CODE (retval) == SSA_NAME
1409 && !gsi_end_p (psi))
1410 add_phi_arg (gsi_stmt (psi), retval, e, UNKNOWN_LOCATION);
1411 /* Otherwise update the return BB itself.
1412 find_return_bb allows at most one assignment to return value,
1413 so update first statement. */
1414 else
1416 gimple_stmt_iterator bsi;
1417 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi);
1418 gsi_next (&bsi))
1419 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
1421 gimple_return_set_retval (gsi_stmt (bsi), retval);
1422 break;
1424 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
1425 && !gimple_clobber_p (gsi_stmt (bsi)))
1427 gimple_assign_set_rhs1 (gsi_stmt (bsi), retval);
1428 break;
1430 update_stmt (gsi_stmt (bsi));
1433 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1435 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1436 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1438 else
1440 tree restype;
1441 restype = TREE_TYPE (DECL_RESULT (current_function_decl));
1442 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1443 if (!useless_type_conversion_p (TREE_TYPE (retval), restype))
1445 gimple cpy;
1446 tree tem = create_tmp_reg (restype, NULL);
1447 tem = make_ssa_name (tem, call);
1448 cpy = gimple_build_assign_with_ops (NOP_EXPR, retval,
1449 tem, NULL_TREE);
1450 gsi_insert_after (&gsi, cpy, GSI_NEW_STMT);
1451 retval = tem;
1453 gimple_call_set_lhs (call, retval);
1454 update_stmt (call);
1457 else
1458 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1460 /* We don't use return block (there is either no return in function or
1461 multiple of them). So create new basic block with return statement.
1463 else
1465 gimple ret;
1466 if (split_point->split_part_set_retval
1467 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1469 retval = DECL_RESULT (current_function_decl);
1471 /* We use temporary register to hold value when aggregate_value_p
1472 is false. Similarly for DECL_BY_REFERENCE we must avoid extra
1473 copy. */
1474 if (!aggregate_value_p (retval, TREE_TYPE (current_function_decl))
1475 && !DECL_BY_REFERENCE (retval))
1476 retval = create_tmp_reg (TREE_TYPE (retval), NULL);
1477 if (is_gimple_reg (retval))
1479 /* When returning by reference, there is only one SSA name
1480 assigned to RESULT_DECL (that is pointer to return value).
1481 Look it up or create new one if it is missing. */
1482 if (DECL_BY_REFERENCE (retval))
1483 retval = get_or_create_ssa_default_def (cfun, retval);
1484 /* Otherwise produce new SSA name for return value. */
1485 else
1486 retval = make_ssa_name (retval, call);
1488 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1489 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1490 else
1491 gimple_call_set_lhs (call, retval);
1493 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1494 ret = gimple_build_return (retval);
1495 gsi_insert_after (&gsi, ret, GSI_NEW_STMT);
1498 free_dominance_info (CDI_DOMINATORS);
1499 free_dominance_info (CDI_POST_DOMINATORS);
1500 compute_inline_parameters (node, true);
1503 /* Execute function splitting pass. */
1505 static unsigned int
1506 execute_split_functions (void)
1508 gimple_stmt_iterator bsi;
1509 basic_block bb;
1510 int overall_time = 0, overall_size = 0;
1511 int todo = 0;
1512 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1514 if (flags_from_decl_or_type (current_function_decl)
1515 & (ECF_NORETURN|ECF_MALLOC))
1517 if (dump_file)
1518 fprintf (dump_file, "Not splitting: noreturn/malloc function.\n");
1519 return 0;
1521 if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
1523 if (dump_file)
1524 fprintf (dump_file, "Not splitting: main function.\n");
1525 return 0;
1527 /* This can be relaxed; function might become inlinable after splitting
1528 away the uninlinable part. */
1529 if (inline_edge_summary_vec.exists ()
1530 && !inline_summary (node)->inlinable)
1532 if (dump_file)
1533 fprintf (dump_file, "Not splitting: not inlinable.\n");
1534 return 0;
1536 if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1538 if (dump_file)
1539 fprintf (dump_file, "Not splitting: disregarding inline limits.\n");
1540 return 0;
1542 /* This can be relaxed; most of versioning tests actually prevents
1543 a duplication. */
1544 if (!tree_versionable_function_p (current_function_decl))
1546 if (dump_file)
1547 fprintf (dump_file, "Not splitting: not versionable.\n");
1548 return 0;
1550 /* FIXME: we could support this. */
1551 if (DECL_STRUCT_FUNCTION (current_function_decl)->static_chain_decl)
1553 if (dump_file)
1554 fprintf (dump_file, "Not splitting: nested function.\n");
1555 return 0;
1558 /* See if it makes sense to try to split.
1559 It makes sense to split if we inline, that is if we have direct calls to
1560 handle or direct calls are possibly going to appear as result of indirect
1561 inlining or LTO. Also handle -fprofile-generate as LTO to allow non-LTO
1562 training for LTO -fprofile-use build.
1564 Note that we are not completely conservative about disqualifying functions
1565 called once. It is possible that the caller is called more then once and
1566 then inlining would still benefit. */
1567 if ((!node->callers
1568 /* Local functions called once will be completely inlined most of time. */
1569 || (!node->callers->next_caller && node->local.local))
1570 && !node->address_taken
1571 && (!flag_lto || !node->externally_visible))
1573 if (dump_file)
1574 fprintf (dump_file, "Not splitting: not called directly "
1575 "or called once.\n");
1576 return 0;
1579 /* FIXME: We can actually split if splitting reduces call overhead. */
1580 if (!flag_inline_small_functions
1581 && !DECL_DECLARED_INLINE_P (current_function_decl))
1583 if (dump_file)
1584 fprintf (dump_file, "Not splitting: not autoinlining and function"
1585 " is not inline.\n");
1586 return 0;
1589 /* We enforce splitting after loop headers when profile info is not
1590 available. */
1591 if (profile_status_for_fn (cfun) != PROFILE_READ)
1592 mark_dfs_back_edges ();
1594 /* Initialize bitmap to track forbidden calls. */
1595 forbidden_dominators = BITMAP_ALLOC (NULL);
1596 calculate_dominance_info (CDI_DOMINATORS);
1598 /* Compute local info about basic blocks and determine function size/time. */
1599 bb_info_vec.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
1600 memset (&best_split_point, 0, sizeof (best_split_point));
1601 FOR_EACH_BB_FN (bb, cfun)
1603 int time = 0;
1604 int size = 0;
1605 int freq = compute_call_stmt_bb_frequency (current_function_decl, bb);
1607 if (dump_file && (dump_flags & TDF_DETAILS))
1608 fprintf (dump_file, "Basic block %i\n", bb->index);
1610 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1612 int this_time, this_size;
1613 gimple stmt = gsi_stmt (bsi);
1615 this_size = estimate_num_insns (stmt, &eni_size_weights);
1616 this_time = estimate_num_insns (stmt, &eni_time_weights) * freq;
1617 size += this_size;
1618 time += this_time;
1619 check_forbidden_calls (stmt);
1621 if (dump_file && (dump_flags & TDF_DETAILS))
1623 fprintf (dump_file, " freq:%6i size:%3i time:%3i ",
1624 freq, this_size, this_time);
1625 print_gimple_stmt (dump_file, stmt, 0, 0);
1628 overall_time += time;
1629 overall_size += size;
1630 bb_info_vec[bb->index].time = time;
1631 bb_info_vec[bb->index].size = size;
1633 find_split_points (overall_time, overall_size);
1634 if (best_split_point.split_bbs)
1636 split_function (&best_split_point);
1637 BITMAP_FREE (best_split_point.ssa_names_to_pass);
1638 BITMAP_FREE (best_split_point.split_bbs);
1639 todo = TODO_update_ssa | TODO_cleanup_cfg;
1641 BITMAP_FREE (forbidden_dominators);
1642 bb_info_vec.release ();
1643 return todo;
1646 namespace {
1648 const pass_data pass_data_split_functions =
1650 GIMPLE_PASS, /* type */
1651 "fnsplit", /* name */
1652 OPTGROUP_NONE, /* optinfo_flags */
1653 TV_IPA_FNSPLIT, /* tv_id */
1654 PROP_cfg, /* properties_required */
1655 0, /* properties_provided */
1656 0, /* properties_destroyed */
1657 0, /* todo_flags_start */
1658 0, /* todo_flags_finish */
1661 class pass_split_functions : public gimple_opt_pass
1663 public:
1664 pass_split_functions (gcc::context *ctxt)
1665 : gimple_opt_pass (pass_data_split_functions, ctxt)
1668 /* opt_pass methods: */
1669 virtual bool gate (function *);
1670 virtual unsigned int execute (function *)
1672 return execute_split_functions ();
1675 }; // class pass_split_functions
1677 bool
1678 pass_split_functions::gate (function *)
1680 /* When doing profile feedback, we want to execute the pass after profiling
1681 is read. So disable one in early optimization. */
1682 return (flag_partial_inlining
1683 && !profile_arc_flag && !flag_branch_probabilities);
1686 } // anon namespace
1688 gimple_opt_pass *
1689 make_pass_split_functions (gcc::context *ctxt)
1691 return new pass_split_functions (ctxt);
1694 /* Execute function splitting pass. */
1696 static unsigned int
1697 execute_feedback_split_functions (void)
1699 unsigned int retval = execute_split_functions ();
1700 if (retval)
1701 retval |= TODO_rebuild_cgraph_edges;
1702 return retval;
1705 namespace {
1707 const pass_data pass_data_feedback_split_functions =
1709 GIMPLE_PASS, /* type */
1710 "feedback_fnsplit", /* name */
1711 OPTGROUP_NONE, /* optinfo_flags */
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 0, /* 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 virtual bool gate (function *);
1729 virtual unsigned int execute (function *)
1731 return execute_feedback_split_functions ();
1734 }; // class pass_feedback_split_functions
1736 bool
1737 pass_feedback_split_functions::gate (function *)
1739 /* We don't need to split when profiling at all, we are producing
1740 lousy code anyway. */
1741 return (flag_partial_inlining
1742 && flag_branch_probabilities);
1745 } // anon namespace
1747 gimple_opt_pass *
1748 make_pass_feedback_split_functions (gcc::context *ctxt)
1750 return new pass_feedback_split_functions (ctxt);