Concretize gimple_call_set_fntype
[official-gcc.git] / gcc / ipa-split.c
blob08dbf7d3f91c82c6cacafc13beed94dcdc1c59fe
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 } split_bb_info;
122 static vec<split_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 /* Normal labels are part of CFG and will be handled gratefuly.
171 Forced labels however can be used directly by statements and
172 need to stay in one partition along with their uses. */
173 || (TREE_CODE (t) == LABEL_DECL
174 && FORCED_LABEL (t)))
175 return bitmap_bit_p ((bitmap)data, DECL_UID (t));
177 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
178 to pretend that the value pointed to is actual result decl. */
179 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
180 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
181 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
182 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
183 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
184 return
185 bitmap_bit_p ((bitmap)data,
186 DECL_UID (DECL_RESULT (current_function_decl)));
188 return false;
191 /* Dump split point CURRENT. */
193 static void
194 dump_split_point (FILE * file, struct split_point *current)
196 fprintf (file,
197 "Split point at BB %i\n"
198 " header time: %i header size: %i\n"
199 " split time: %i split size: %i\n bbs: ",
200 current->entry_bb->index, current->header_time,
201 current->header_size, current->split_time, current->split_size);
202 dump_bitmap (file, current->split_bbs);
203 fprintf (file, " SSA names to pass: ");
204 dump_bitmap (file, current->ssa_names_to_pass);
207 /* Look for all BBs in header that might lead to the split part and verify
208 that they are not defining any non-SSA var used by the split part.
209 Parameters are the same as for consider_split. */
211 static bool
212 verify_non_ssa_vars (struct split_point *current, bitmap non_ssa_vars,
213 basic_block return_bb)
215 bitmap seen = BITMAP_ALLOC (NULL);
216 vec<basic_block> worklist = vNULL;
217 edge e;
218 edge_iterator ei;
219 bool ok = true;
220 basic_block bb;
222 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
223 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
224 && !bitmap_bit_p (current->split_bbs, e->src->index))
226 worklist.safe_push (e->src);
227 bitmap_set_bit (seen, e->src->index);
230 while (!worklist.is_empty ())
232 gimple_stmt_iterator bsi;
234 bb = worklist.pop ();
235 FOR_EACH_EDGE (e, ei, bb->preds)
236 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
237 && bitmap_set_bit (seen, e->src->index))
239 gcc_checking_assert (!bitmap_bit_p (current->split_bbs,
240 e->src->index));
241 worklist.safe_push (e->src);
243 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
245 gimple stmt = gsi_stmt (bsi);
246 if (is_gimple_debug (stmt))
247 continue;
248 if (walk_stmt_load_store_addr_ops
249 (stmt, non_ssa_vars, test_nonssa_use, test_nonssa_use,
250 test_nonssa_use))
252 ok = false;
253 goto done;
255 if (gimple_label label_stmt = dyn_cast <gimple_label> (stmt))
256 if (test_nonssa_use (stmt, gimple_label_label (label_stmt),
257 NULL_TREE, non_ssa_vars))
259 ok = false;
260 goto done;
263 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
265 if (walk_stmt_load_store_addr_ops
266 (gsi_stmt (bsi), non_ssa_vars, test_nonssa_use, test_nonssa_use,
267 test_nonssa_use))
269 ok = false;
270 goto done;
273 FOR_EACH_EDGE (e, ei, bb->succs)
275 if (e->dest != return_bb)
276 continue;
277 for (bsi = gsi_start_phis (return_bb); !gsi_end_p (bsi);
278 gsi_next (&bsi))
280 gimple stmt = gsi_stmt (bsi);
281 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
283 if (virtual_operand_p (gimple_phi_result (stmt)))
284 continue;
285 if (TREE_CODE (op) != SSA_NAME
286 && test_nonssa_use (stmt, op, op, non_ssa_vars))
288 ok = false;
289 goto done;
295 /* Verify that the rest of function does not define any label
296 used by the split part. */
297 FOR_EACH_BB_FN (bb, cfun)
298 if (!bitmap_bit_p (current->split_bbs, bb->index)
299 && !bitmap_bit_p (seen, bb->index))
301 gimple_stmt_iterator bsi;
302 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
303 if (gimple_label label_stmt =
304 dyn_cast <gimple_label> (gsi_stmt (bsi)))
306 if (test_nonssa_use (label_stmt,
307 gimple_label_label (label_stmt),
308 NULL_TREE, non_ssa_vars))
310 ok = false;
311 goto done;
314 else
315 break;
318 done:
319 BITMAP_FREE (seen);
320 worklist.release ();
321 return ok;
324 /* If STMT is a call, check the callee against a list of forbidden
325 predicate functions. If a match is found, look for uses of the
326 call result in condition statements that compare against zero.
327 For each such use, find the block targeted by the condition
328 statement for the nonzero result, and set the bit for this block
329 in the forbidden dominators bitmap. The purpose of this is to avoid
330 selecting a split point where we are likely to lose the chance
331 to optimize away an unused function call. */
333 static void
334 check_forbidden_calls (gimple stmt)
336 imm_use_iterator use_iter;
337 use_operand_p use_p;
338 tree lhs;
340 /* At the moment, __builtin_constant_p is the only forbidden
341 predicate function call (see PR49642). */
342 if (!gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P))
343 return;
345 lhs = gimple_call_lhs (stmt);
347 if (!lhs || TREE_CODE (lhs) != SSA_NAME)
348 return;
350 FOR_EACH_IMM_USE_FAST (use_p, use_iter, lhs)
352 tree op1;
353 basic_block use_bb, forbidden_bb;
354 enum tree_code code;
355 edge true_edge, false_edge;
356 gimple_cond use_stmt;
358 use_stmt = dyn_cast <gimple_cond> (USE_STMT (use_p));
359 if (!use_stmt)
360 continue;
362 /* Assuming canonical form for GIMPLE_COND here, with constant
363 in second position. */
364 op1 = gimple_cond_rhs (use_stmt);
365 code = gimple_cond_code (use_stmt);
366 use_bb = gimple_bb (use_stmt);
368 extract_true_false_edges_from_block (use_bb, &true_edge, &false_edge);
370 /* We're only interested in comparisons that distinguish
371 unambiguously from zero. */
372 if (!integer_zerop (op1) || code == LE_EXPR || code == GE_EXPR)
373 continue;
375 if (code == EQ_EXPR)
376 forbidden_bb = false_edge->dest;
377 else
378 forbidden_bb = true_edge->dest;
380 bitmap_set_bit (forbidden_dominators, forbidden_bb->index);
384 /* If BB is dominated by any block in the forbidden dominators set,
385 return TRUE; else FALSE. */
387 static bool
388 dominated_by_forbidden (basic_block bb)
390 unsigned dom_bb;
391 bitmap_iterator bi;
393 EXECUTE_IF_SET_IN_BITMAP (forbidden_dominators, 1, dom_bb, bi)
395 if (dominated_by_p (CDI_DOMINATORS, bb,
396 BASIC_BLOCK_FOR_FN (cfun, dom_bb)))
397 return true;
400 return false;
403 /* We found an split_point CURRENT. NON_SSA_VARS is bitmap of all non ssa
404 variables used and RETURN_BB is return basic block.
405 See if we can split function here. */
407 static void
408 consider_split (struct split_point *current, bitmap non_ssa_vars,
409 basic_block return_bb)
411 tree parm;
412 unsigned int num_args = 0;
413 unsigned int call_overhead;
414 edge e;
415 edge_iterator ei;
416 gimple_phi_iterator bsi;
417 unsigned int i;
418 int incoming_freq = 0;
419 tree retval;
420 bool back_edge = false;
422 if (dump_file && (dump_flags & TDF_DETAILS))
423 dump_split_point (dump_file, current);
425 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
427 if (e->flags & EDGE_DFS_BACK)
428 back_edge = true;
429 if (!bitmap_bit_p (current->split_bbs, e->src->index))
430 incoming_freq += EDGE_FREQUENCY (e);
433 /* Do not split when we would end up calling function anyway. */
434 if (incoming_freq
435 >= (ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency
436 * PARAM_VALUE (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY) / 100))
438 /* When profile is guessed, we can not expect it to give us
439 realistic estimate on likelyness of function taking the
440 complex path. As a special case, when tail of the function is
441 a loop, enable splitting since inlining code skipping the loop
442 is likely noticeable win. */
443 if (back_edge
444 && profile_status_for_fn (cfun) != PROFILE_READ
445 && incoming_freq < ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency)
447 if (dump_file && (dump_flags & TDF_DETAILS))
448 fprintf (dump_file,
449 " Split before loop, accepting despite low frequencies %i %i.\n",
450 incoming_freq,
451 ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency);
453 else
455 if (dump_file && (dump_flags & TDF_DETAILS))
456 fprintf (dump_file,
457 " Refused: incoming frequency is too large.\n");
458 return;
462 if (!current->header_size)
464 if (dump_file && (dump_flags & TDF_DETAILS))
465 fprintf (dump_file, " Refused: header empty\n");
466 return;
469 /* Verify that PHI args on entry are either virtual or all their operands
470 incoming from header are the same. */
471 for (bsi = gsi_start_phis (current->entry_bb); !gsi_end_p (bsi); gsi_next (&bsi))
473 gimple_phi stmt = bsi.phi ();
474 tree val = NULL;
476 if (virtual_operand_p (gimple_phi_result (stmt)))
477 continue;
478 for (i = 0; i < gimple_phi_num_args (stmt); i++)
480 edge e = gimple_phi_arg_edge (stmt, i);
481 if (!bitmap_bit_p (current->split_bbs, e->src->index))
483 tree edge_val = gimple_phi_arg_def (stmt, i);
484 if (val && edge_val != val)
486 if (dump_file && (dump_flags & TDF_DETAILS))
487 fprintf (dump_file,
488 " Refused: entry BB has PHI with multiple variants\n");
489 return;
491 val = edge_val;
497 /* See what argument we will pass to the split function and compute
498 call overhead. */
499 call_overhead = eni_size_weights.call_cost;
500 for (parm = DECL_ARGUMENTS (current_function_decl); parm;
501 parm = DECL_CHAIN (parm))
503 if (!is_gimple_reg (parm))
505 if (bitmap_bit_p (non_ssa_vars, DECL_UID (parm)))
507 if (dump_file && (dump_flags & TDF_DETAILS))
508 fprintf (dump_file,
509 " Refused: need to pass non-ssa param values\n");
510 return;
513 else
515 tree ddef = ssa_default_def (cfun, parm);
516 if (ddef
517 && bitmap_bit_p (current->ssa_names_to_pass,
518 SSA_NAME_VERSION (ddef)))
520 if (!VOID_TYPE_P (TREE_TYPE (parm)))
521 call_overhead += estimate_move_cost (TREE_TYPE (parm), false);
522 num_args++;
526 if (!VOID_TYPE_P (TREE_TYPE (current_function_decl)))
527 call_overhead += estimate_move_cost (TREE_TYPE (current_function_decl),
528 false);
530 if (current->split_size <= call_overhead)
532 if (dump_file && (dump_flags & TDF_DETAILS))
533 fprintf (dump_file,
534 " Refused: split size is smaller than call overhead\n");
535 return;
537 if (current->header_size + call_overhead
538 >= (unsigned int)(DECL_DECLARED_INLINE_P (current_function_decl)
539 ? MAX_INLINE_INSNS_SINGLE
540 : MAX_INLINE_INSNS_AUTO))
542 if (dump_file && (dump_flags & TDF_DETAILS))
543 fprintf (dump_file,
544 " Refused: header size is too large for inline candidate\n");
545 return;
548 /* FIXME: we currently can pass only SSA function parameters to the split
549 arguments. Once parm_adjustment infrastructure is supported by cloning,
550 we can pass more than that. */
551 if (num_args != bitmap_count_bits (current->ssa_names_to_pass))
554 if (dump_file && (dump_flags & TDF_DETAILS))
555 fprintf (dump_file,
556 " Refused: need to pass non-param values\n");
557 return;
560 /* When there are non-ssa vars used in the split region, see if they
561 are used in the header region. If so, reject the split.
562 FIXME: we can use nested function support to access both. */
563 if (!bitmap_empty_p (non_ssa_vars)
564 && !verify_non_ssa_vars (current, non_ssa_vars, return_bb))
566 if (dump_file && (dump_flags & TDF_DETAILS))
567 fprintf (dump_file,
568 " Refused: split part has non-ssa uses\n");
569 return;
572 /* If the split point is dominated by a forbidden block, reject
573 the split. */
574 if (!bitmap_empty_p (forbidden_dominators)
575 && dominated_by_forbidden (current->entry_bb))
577 if (dump_file && (dump_flags & TDF_DETAILS))
578 fprintf (dump_file,
579 " Refused: split point dominated by forbidden block\n");
580 return;
583 /* See if retval used by return bb is computed by header or split part.
584 When it is computed by split part, we need to produce return statement
585 in the split part and add code to header to pass it around.
587 This is bit tricky to test:
588 1) When there is no return_bb or no return value, we always pass
589 value around.
590 2) Invariants are always computed by caller.
591 3) For SSA we need to look if defining statement is in header or split part
592 4) For non-SSA we need to look where the var is computed. */
593 retval = find_retval (return_bb);
594 if (!retval)
595 current->split_part_set_retval = true;
596 else if (is_gimple_min_invariant (retval))
597 current->split_part_set_retval = false;
598 /* Special case is value returned by reference we record as if it was non-ssa
599 set to result_decl. */
600 else if (TREE_CODE (retval) == SSA_NAME
601 && SSA_NAME_VAR (retval)
602 && TREE_CODE (SSA_NAME_VAR (retval)) == RESULT_DECL
603 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
604 current->split_part_set_retval
605 = bitmap_bit_p (non_ssa_vars, DECL_UID (SSA_NAME_VAR (retval)));
606 else if (TREE_CODE (retval) == SSA_NAME)
607 current->split_part_set_retval
608 = (!SSA_NAME_IS_DEFAULT_DEF (retval)
609 && (bitmap_bit_p (current->split_bbs,
610 gimple_bb (SSA_NAME_DEF_STMT (retval))->index)
611 || gimple_bb (SSA_NAME_DEF_STMT (retval)) == return_bb));
612 else if (TREE_CODE (retval) == PARM_DECL)
613 current->split_part_set_retval = false;
614 else if (TREE_CODE (retval) == VAR_DECL
615 || TREE_CODE (retval) == RESULT_DECL)
616 current->split_part_set_retval
617 = bitmap_bit_p (non_ssa_vars, DECL_UID (retval));
618 else
619 current->split_part_set_retval = true;
621 /* split_function fixes up at most one PHI non-virtual PHI node in return_bb,
622 for the return value. If there are other PHIs, give up. */
623 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
625 gimple_stmt_iterator psi;
627 for (psi = gsi_start_phis (return_bb); !gsi_end_p (psi); gsi_next (&psi))
628 if (!virtual_operand_p (gimple_phi_result (gsi_stmt (psi)))
629 && !(retval
630 && current->split_part_set_retval
631 && TREE_CODE (retval) == SSA_NAME
632 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
633 && SSA_NAME_DEF_STMT (retval) == gsi_stmt (psi)))
635 if (dump_file && (dump_flags & TDF_DETAILS))
636 fprintf (dump_file,
637 " Refused: return bb has extra PHIs\n");
638 return;
642 if (dump_file && (dump_flags & TDF_DETAILS))
643 fprintf (dump_file, " Accepted!\n");
645 /* At the moment chose split point with lowest frequency and that leaves
646 out smallest size of header.
647 In future we might re-consider this heuristics. */
648 if (!best_split_point.split_bbs
649 || best_split_point.entry_bb->frequency > current->entry_bb->frequency
650 || (best_split_point.entry_bb->frequency == current->entry_bb->frequency
651 && best_split_point.split_size < current->split_size))
654 if (dump_file && (dump_flags & TDF_DETAILS))
655 fprintf (dump_file, " New best split point!\n");
656 if (best_split_point.ssa_names_to_pass)
658 BITMAP_FREE (best_split_point.ssa_names_to_pass);
659 BITMAP_FREE (best_split_point.split_bbs);
661 best_split_point = *current;
662 best_split_point.ssa_names_to_pass = BITMAP_ALLOC (NULL);
663 bitmap_copy (best_split_point.ssa_names_to_pass,
664 current->ssa_names_to_pass);
665 best_split_point.split_bbs = BITMAP_ALLOC (NULL);
666 bitmap_copy (best_split_point.split_bbs, current->split_bbs);
670 /* Return basic block containing RETURN statement. We allow basic blocks
671 of the form:
672 <retval> = tmp_var;
673 return <retval>
674 but return_bb can not be more complex than this.
675 If nothing is found, return the exit block.
677 When there are multiple RETURN statement, chose one with return value,
678 since that one is more likely shared by multiple code paths.
680 Return BB is special, because for function splitting it is the only
681 basic block that is duplicated in between header and split part of the
682 function.
684 TODO: We might support multiple return blocks. */
686 static basic_block
687 find_return_bb (void)
689 edge e;
690 basic_block return_bb = EXIT_BLOCK_PTR_FOR_FN (cfun);
691 gimple_stmt_iterator bsi;
692 bool found_return = false;
693 tree retval = NULL_TREE;
695 if (!single_pred_p (EXIT_BLOCK_PTR_FOR_FN (cfun)))
696 return return_bb;
698 e = single_pred_edge (EXIT_BLOCK_PTR_FOR_FN (cfun));
699 for (bsi = gsi_last_bb (e->src); !gsi_end_p (bsi); gsi_prev (&bsi))
701 gimple stmt = gsi_stmt (bsi);
702 if (gimple_code (stmt) == GIMPLE_LABEL
703 || is_gimple_debug (stmt)
704 || gimple_clobber_p (stmt))
706 else if (gimple_code (stmt) == GIMPLE_ASSIGN
707 && found_return
708 && gimple_assign_single_p (stmt)
709 && (auto_var_in_fn_p (gimple_assign_rhs1 (stmt),
710 current_function_decl)
711 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
712 && retval == gimple_assign_lhs (stmt))
714 else if (gimple_return return_stmt = dyn_cast <gimple_return> (stmt))
716 found_return = true;
717 retval = gimple_return_retval (return_stmt);
719 else
720 break;
722 if (gsi_end_p (bsi) && found_return)
723 return_bb = e->src;
725 return return_bb;
728 /* Given return basic block RETURN_BB, see where return value is really
729 stored. */
730 static tree
731 find_retval (basic_block return_bb)
733 gimple_stmt_iterator bsi;
734 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
735 if (gimple_return return_stmt = dyn_cast <gimple_return> (gsi_stmt (bsi)))
736 return gimple_return_retval (return_stmt);
737 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
738 && !gimple_clobber_p (gsi_stmt (bsi)))
739 return gimple_assign_rhs1 (gsi_stmt (bsi));
740 return NULL;
743 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
744 variable, mark it as used in bitmap passed via DATA.
745 Return true when access to T prevents splitting the function. */
747 static bool
748 mark_nonssa_use (gimple, tree t, tree, void *data)
750 t = get_base_address (t);
752 if (!t || is_gimple_reg (t))
753 return false;
755 /* At present we can't pass non-SSA arguments to split function.
756 FIXME: this can be relaxed by passing references to arguments. */
757 if (TREE_CODE (t) == PARM_DECL)
759 if (dump_file && (dump_flags & TDF_DETAILS))
760 fprintf (dump_file,
761 "Cannot split: use of non-ssa function parameter.\n");
762 return true;
765 if ((TREE_CODE (t) == VAR_DECL
766 && auto_var_in_fn_p (t, current_function_decl))
767 || TREE_CODE (t) == RESULT_DECL
768 || (TREE_CODE (t) == LABEL_DECL
769 && FORCED_LABEL (t)))
770 bitmap_set_bit ((bitmap)data, DECL_UID (t));
772 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
773 to pretend that the value pointed to is actual result decl. */
774 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
775 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
776 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
777 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
778 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
779 return
780 bitmap_bit_p ((bitmap)data,
781 DECL_UID (DECL_RESULT (current_function_decl)));
783 return false;
786 /* Compute local properties of basic block BB we collect when looking for
787 split points. We look for ssa defs and store them in SET_SSA_NAMES,
788 for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic
789 vars stored in NON_SSA_VARS.
791 When BB has edge to RETURN_BB, collect uses in RETURN_BB too.
793 Return false when BB contains something that prevents it from being put into
794 split function. */
796 static bool
797 visit_bb (basic_block bb, basic_block return_bb,
798 bitmap set_ssa_names, bitmap used_ssa_names,
799 bitmap non_ssa_vars)
801 gimple_stmt_iterator bsi;
802 edge e;
803 edge_iterator ei;
804 bool can_split = true;
806 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
808 gimple stmt = gsi_stmt (bsi);
809 tree op;
810 ssa_op_iter iter;
811 tree decl;
813 if (is_gimple_debug (stmt))
814 continue;
816 if (gimple_clobber_p (stmt))
817 continue;
819 /* FIXME: We can split regions containing EH. We can not however
820 split RESX, EH_DISPATCH and EH_POINTER referring to same region
821 into different partitions. This would require tracking of
822 EH regions and checking in consider_split_point if they
823 are not used elsewhere. */
824 if (gimple_code (stmt) == GIMPLE_RESX)
826 if (dump_file && (dump_flags & TDF_DETAILS))
827 fprintf (dump_file, "Cannot split: resx.\n");
828 can_split = false;
830 if (gimple_code (stmt) == GIMPLE_EH_DISPATCH)
832 if (dump_file && (dump_flags & TDF_DETAILS))
833 fprintf (dump_file, "Cannot split: eh dispatch.\n");
834 can_split = false;
837 /* Check builtins that prevent splitting. */
838 if (gimple_code (stmt) == GIMPLE_CALL
839 && (decl = gimple_call_fndecl (stmt)) != NULL_TREE
840 && DECL_BUILT_IN (decl)
841 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
842 switch (DECL_FUNCTION_CODE (decl))
844 /* FIXME: once we will allow passing non-parm values to split part,
845 we need to be sure to handle correct builtin_stack_save and
846 builtin_stack_restore. At the moment we are safe; there is no
847 way to store builtin_stack_save result in non-SSA variable
848 since all calls to those are compiler generated. */
849 case BUILT_IN_APPLY:
850 case BUILT_IN_APPLY_ARGS:
851 case BUILT_IN_VA_START:
852 if (dump_file && (dump_flags & TDF_DETAILS))
853 fprintf (dump_file,
854 "Cannot split: builtin_apply and va_start.\n");
855 can_split = false;
856 break;
857 case BUILT_IN_EH_POINTER:
858 if (dump_file && (dump_flags & TDF_DETAILS))
859 fprintf (dump_file, "Cannot split: builtin_eh_pointer.\n");
860 can_split = false;
861 break;
862 default:
863 break;
866 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
867 bitmap_set_bit (set_ssa_names, SSA_NAME_VERSION (op));
868 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
869 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
870 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
871 mark_nonssa_use,
872 mark_nonssa_use,
873 mark_nonssa_use);
875 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
877 gimple stmt = gsi_stmt (bsi);
878 unsigned int i;
880 if (virtual_operand_p (gimple_phi_result (stmt)))
881 continue;
882 bitmap_set_bit (set_ssa_names,
883 SSA_NAME_VERSION (gimple_phi_result (stmt)));
884 for (i = 0; i < gimple_phi_num_args (stmt); i++)
886 tree op = gimple_phi_arg_def (stmt, i);
887 if (TREE_CODE (op) == SSA_NAME)
888 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
890 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
891 mark_nonssa_use,
892 mark_nonssa_use,
893 mark_nonssa_use);
895 /* Record also uses coming from PHI operand in return BB. */
896 FOR_EACH_EDGE (e, ei, bb->succs)
897 if (e->dest == return_bb)
899 for (bsi = gsi_start_phis (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
901 gimple stmt = gsi_stmt (bsi);
902 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
904 if (virtual_operand_p (gimple_phi_result (stmt)))
905 continue;
906 if (TREE_CODE (op) == SSA_NAME)
907 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
908 else
909 can_split &= !mark_nonssa_use (stmt, op, op, non_ssa_vars);
912 return can_split;
915 /* Stack entry for recursive DFS walk in find_split_point. */
917 typedef struct
919 /* Basic block we are examining. */
920 basic_block bb;
922 /* SSA names set and used by the BB and all BBs reachable
923 from it via DFS walk. */
924 bitmap set_ssa_names, used_ssa_names;
925 bitmap non_ssa_vars;
927 /* All BBS visited from this BB via DFS walk. */
928 bitmap bbs_visited;
930 /* Last examined edge in DFS walk. Since we walk unoriented graph,
931 the value is up to sum of incoming and outgoing edges of BB. */
932 unsigned int edge_num;
934 /* Stack entry index of earliest BB reachable from current BB
935 or any BB visited later in DFS walk. */
936 int earliest;
938 /* Overall time and size of all BBs reached from this BB in DFS walk. */
939 int overall_time, overall_size;
941 /* When false we can not split on this BB. */
942 bool can_split;
943 } stack_entry;
946 /* Find all articulations and call consider_split on them.
947 OVERALL_TIME and OVERALL_SIZE is time and size of the function.
949 We perform basic algorithm for finding an articulation in a graph
950 created from CFG by considering it to be an unoriented graph.
952 The articulation is discovered via DFS walk. We collect earliest
953 basic block on stack that is reachable via backward edge. Articulation
954 is any basic block such that there is no backward edge bypassing it.
955 To reduce stack usage we maintain heap allocated stack in STACK vector.
956 AUX pointer of BB is set to index it appears in the stack or -1 once
957 it is visited and popped off the stack.
959 The algorithm finds articulation after visiting the whole component
960 reachable by it. This makes it convenient to collect information about
961 the component used by consider_split. */
963 static void
964 find_split_points (int overall_time, int overall_size)
966 stack_entry first;
967 vec<stack_entry> stack = vNULL;
968 basic_block bb;
969 basic_block return_bb = find_return_bb ();
970 struct split_point current;
972 current.header_time = overall_time;
973 current.header_size = overall_size;
974 current.split_time = 0;
975 current.split_size = 0;
976 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
978 first.bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
979 first.edge_num = 0;
980 first.overall_time = 0;
981 first.overall_size = 0;
982 first.earliest = INT_MAX;
983 first.set_ssa_names = 0;
984 first.used_ssa_names = 0;
985 first.non_ssa_vars = 0;
986 first.bbs_visited = 0;
987 first.can_split = false;
988 stack.safe_push (first);
989 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = (void *)(intptr_t)-1;
991 while (!stack.is_empty ())
993 stack_entry *entry = &stack.last ();
995 /* We are walking an acyclic graph, so edge_num counts
996 succ and pred edges together. However when considering
997 articulation, we want to have processed everything reachable
998 from articulation but nothing that reaches into it. */
999 if (entry->edge_num == EDGE_COUNT (entry->bb->succs)
1000 && entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1002 int pos = stack.length ();
1003 entry->can_split &= visit_bb (entry->bb, return_bb,
1004 entry->set_ssa_names,
1005 entry->used_ssa_names,
1006 entry->non_ssa_vars);
1007 if (pos <= entry->earliest && !entry->can_split
1008 && dump_file && (dump_flags & TDF_DETAILS))
1009 fprintf (dump_file,
1010 "found articulation at bb %i but can not split\n",
1011 entry->bb->index);
1012 if (pos <= entry->earliest && entry->can_split)
1014 if (dump_file && (dump_flags & TDF_DETAILS))
1015 fprintf (dump_file, "found articulation at bb %i\n",
1016 entry->bb->index);
1017 current.entry_bb = entry->bb;
1018 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
1019 bitmap_and_compl (current.ssa_names_to_pass,
1020 entry->used_ssa_names, entry->set_ssa_names);
1021 current.header_time = overall_time - entry->overall_time;
1022 current.header_size = overall_size - entry->overall_size;
1023 current.split_time = entry->overall_time;
1024 current.split_size = entry->overall_size;
1025 current.split_bbs = entry->bbs_visited;
1026 consider_split (&current, entry->non_ssa_vars, return_bb);
1027 BITMAP_FREE (current.ssa_names_to_pass);
1030 /* Do actual DFS walk. */
1031 if (entry->edge_num
1032 < (EDGE_COUNT (entry->bb->succs)
1033 + EDGE_COUNT (entry->bb->preds)))
1035 edge e;
1036 basic_block dest;
1037 if (entry->edge_num < EDGE_COUNT (entry->bb->succs))
1039 e = EDGE_SUCC (entry->bb, entry->edge_num);
1040 dest = e->dest;
1042 else
1044 e = EDGE_PRED (entry->bb, entry->edge_num
1045 - EDGE_COUNT (entry->bb->succs));
1046 dest = e->src;
1049 entry->edge_num++;
1051 /* New BB to visit, push it to the stack. */
1052 if (dest != return_bb && dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
1053 && !dest->aux)
1055 stack_entry new_entry;
1057 new_entry.bb = dest;
1058 new_entry.edge_num = 0;
1059 new_entry.overall_time
1060 = bb_info_vec[dest->index].time;
1061 new_entry.overall_size
1062 = bb_info_vec[dest->index].size;
1063 new_entry.earliest = INT_MAX;
1064 new_entry.set_ssa_names = BITMAP_ALLOC (NULL);
1065 new_entry.used_ssa_names = BITMAP_ALLOC (NULL);
1066 new_entry.bbs_visited = BITMAP_ALLOC (NULL);
1067 new_entry.non_ssa_vars = BITMAP_ALLOC (NULL);
1068 new_entry.can_split = true;
1069 bitmap_set_bit (new_entry.bbs_visited, dest->index);
1070 stack.safe_push (new_entry);
1071 dest->aux = (void *)(intptr_t)stack.length ();
1073 /* Back edge found, record the earliest point. */
1074 else if ((intptr_t)dest->aux > 0
1075 && (intptr_t)dest->aux < entry->earliest)
1076 entry->earliest = (intptr_t)dest->aux;
1078 /* We are done with examining the edges. Pop off the value from stack
1079 and merge stuff we accumulate during the walk. */
1080 else if (entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1082 stack_entry *prev = &stack[stack.length () - 2];
1084 entry->bb->aux = (void *)(intptr_t)-1;
1085 prev->can_split &= entry->can_split;
1086 if (prev->set_ssa_names)
1088 bitmap_ior_into (prev->set_ssa_names, entry->set_ssa_names);
1089 bitmap_ior_into (prev->used_ssa_names, entry->used_ssa_names);
1090 bitmap_ior_into (prev->bbs_visited, entry->bbs_visited);
1091 bitmap_ior_into (prev->non_ssa_vars, entry->non_ssa_vars);
1093 if (prev->earliest > entry->earliest)
1094 prev->earliest = entry->earliest;
1095 prev->overall_time += entry->overall_time;
1096 prev->overall_size += entry->overall_size;
1097 BITMAP_FREE (entry->set_ssa_names);
1098 BITMAP_FREE (entry->used_ssa_names);
1099 BITMAP_FREE (entry->bbs_visited);
1100 BITMAP_FREE (entry->non_ssa_vars);
1101 stack.pop ();
1103 else
1104 stack.pop ();
1106 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = NULL;
1107 FOR_EACH_BB_FN (bb, cfun)
1108 bb->aux = NULL;
1109 stack.release ();
1110 BITMAP_FREE (current.ssa_names_to_pass);
1113 /* Split function at SPLIT_POINT. */
1115 static void
1116 split_function (struct split_point *split_point)
1118 vec<tree> args_to_pass = vNULL;
1119 bitmap args_to_skip;
1120 tree parm;
1121 int num = 0;
1122 cgraph_node *node, *cur_node = cgraph_node::get (current_function_decl);
1123 basic_block return_bb = find_return_bb ();
1124 basic_block call_bb;
1125 gimple_stmt_iterator gsi;
1126 gimple_call call;
1127 edge e;
1128 edge_iterator ei;
1129 tree retval = NULL, real_retval = NULL;
1130 bool split_part_return_p = false;
1131 gimple last_stmt = NULL;
1132 unsigned int i;
1133 tree arg, ddef;
1134 vec<tree, va_gc> **debug_args = NULL;
1136 if (dump_file)
1138 fprintf (dump_file, "\n\nSplitting function at:\n");
1139 dump_split_point (dump_file, split_point);
1142 if (cur_node->local.can_change_signature)
1143 args_to_skip = BITMAP_ALLOC (NULL);
1144 else
1145 args_to_skip = NULL;
1147 /* Collect the parameters of new function and args_to_skip bitmap. */
1148 for (parm = DECL_ARGUMENTS (current_function_decl);
1149 parm; parm = DECL_CHAIN (parm), num++)
1150 if (args_to_skip
1151 && (!is_gimple_reg (parm)
1152 || (ddef = ssa_default_def (cfun, parm)) == NULL_TREE
1153 || !bitmap_bit_p (split_point->ssa_names_to_pass,
1154 SSA_NAME_VERSION (ddef))))
1155 bitmap_set_bit (args_to_skip, num);
1156 else
1158 /* This parm might not have been used up to now, but is going to be
1159 used, hence register it. */
1160 if (is_gimple_reg (parm))
1161 arg = get_or_create_ssa_default_def (cfun, parm);
1162 else
1163 arg = parm;
1165 if (!useless_type_conversion_p (DECL_ARG_TYPE (parm), TREE_TYPE (arg)))
1166 arg = fold_convert (DECL_ARG_TYPE (parm), arg);
1167 args_to_pass.safe_push (arg);
1170 /* See if the split function will return. */
1171 FOR_EACH_EDGE (e, ei, return_bb->preds)
1172 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1173 break;
1174 if (e)
1175 split_part_return_p = true;
1177 /* Add return block to what will become the split function.
1178 We do not return; no return block is needed. */
1179 if (!split_part_return_p)
1181 /* We have no return block, so nothing is needed. */
1182 else if (return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1184 /* When we do not want to return value, we need to construct
1185 new return block with empty return statement.
1186 FIXME: Once we are able to change return type, we should change function
1187 to return void instead of just outputting function with undefined return
1188 value. For structures this affects quality of codegen. */
1189 else if (!split_point->split_part_set_retval
1190 && find_retval (return_bb))
1192 bool redirected = true;
1193 basic_block new_return_bb = create_basic_block (NULL, 0, return_bb);
1194 gimple_stmt_iterator gsi = gsi_start_bb (new_return_bb);
1195 gsi_insert_after (&gsi, gimple_build_return (NULL), GSI_NEW_STMT);
1196 while (redirected)
1198 redirected = false;
1199 FOR_EACH_EDGE (e, ei, return_bb->preds)
1200 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1202 new_return_bb->count += e->count;
1203 new_return_bb->frequency += EDGE_FREQUENCY (e);
1204 redirect_edge_and_branch (e, new_return_bb);
1205 redirected = true;
1206 break;
1209 e = make_edge (new_return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1210 e->probability = REG_BR_PROB_BASE;
1211 e->count = new_return_bb->count;
1212 add_bb_to_loop (new_return_bb, current_loops->tree_root);
1213 bitmap_set_bit (split_point->split_bbs, new_return_bb->index);
1215 /* When we pass around the value, use existing return block. */
1216 else
1217 bitmap_set_bit (split_point->split_bbs, return_bb->index);
1219 /* If RETURN_BB has virtual operand PHIs, they must be removed and the
1220 virtual operand marked for renaming as we change the CFG in a way that
1221 tree-inline is not able to compensate for.
1223 Note this can happen whether or not we have a return value. If we have
1224 a return value, then RETURN_BB may have PHIs for real operands too. */
1225 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1227 bool phi_p = false;
1228 for (gsi = gsi_start_phis (return_bb); !gsi_end_p (gsi);)
1230 gimple stmt = gsi_stmt (gsi);
1231 if (!virtual_operand_p (gimple_phi_result (stmt)))
1233 gsi_next (&gsi);
1234 continue;
1236 mark_virtual_phi_result_for_renaming (stmt);
1237 remove_phi_node (&gsi, true);
1238 phi_p = true;
1240 /* In reality we have to rename the reaching definition of the
1241 virtual operand at return_bb as we will eventually release it
1242 when we remove the code region we outlined.
1243 So we have to rename all immediate virtual uses of that region
1244 if we didn't see a PHI definition yet. */
1245 /* ??? In real reality we want to set the reaching vdef of the
1246 entry of the SESE region as the vuse of the call and the reaching
1247 vdef of the exit of the SESE region as the vdef of the call. */
1248 if (!phi_p)
1249 for (gsi = gsi_start_bb (return_bb); !gsi_end_p (gsi); gsi_next (&gsi))
1251 gimple stmt = gsi_stmt (gsi);
1252 if (gimple_vuse (stmt))
1254 gimple_set_vuse (stmt, NULL_TREE);
1255 update_stmt (stmt);
1257 if (gimple_vdef (stmt))
1258 break;
1262 /* Now create the actual clone. */
1263 cgraph_edge::rebuild_edges ();
1264 node = cur_node->create_version_clone_with_body
1265 (vNULL, NULL, args_to_skip, !split_part_return_p, split_point->split_bbs,
1266 split_point->entry_bb, "part");
1268 /* Let's take a time profile for splitted function. */
1269 node->tp_first_run = cur_node->tp_first_run + 1;
1271 /* For usual cloning it is enough to clear builtin only when signature
1272 changes. For partial inlining we however can not expect the part
1273 of builtin implementation to have same semantic as the whole. */
1274 if (DECL_BUILT_IN (node->decl))
1276 DECL_BUILT_IN_CLASS (node->decl) = NOT_BUILT_IN;
1277 DECL_FUNCTION_CODE (node->decl) = (enum built_in_function) 0;
1279 /* If the original function is declared inline, there is no point in issuing
1280 a warning for the non-inlinable part. */
1281 DECL_NO_INLINE_WARNING_P (node->decl) = 1;
1282 cur_node->remove_callees ();
1283 cur_node->remove_all_references ();
1284 if (!split_part_return_p)
1285 TREE_THIS_VOLATILE (node->decl) = 1;
1286 if (dump_file)
1287 dump_function_to_file (node->decl, dump_file, dump_flags);
1289 /* Create the basic block we place call into. It is the entry basic block
1290 split after last label. */
1291 call_bb = split_point->entry_bb;
1292 for (gsi = gsi_start_bb (call_bb); !gsi_end_p (gsi);)
1293 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
1295 last_stmt = gsi_stmt (gsi);
1296 gsi_next (&gsi);
1298 else
1299 break;
1300 e = split_block (split_point->entry_bb, last_stmt);
1301 remove_edge (e);
1303 /* Produce the call statement. */
1304 gsi = gsi_last_bb (call_bb);
1305 FOR_EACH_VEC_ELT (args_to_pass, i, arg)
1306 if (!is_gimple_val (arg))
1308 arg = force_gimple_operand_gsi (&gsi, arg, true, NULL_TREE,
1309 false, GSI_CONTINUE_LINKING);
1310 args_to_pass[i] = arg;
1312 call = gimple_build_call_vec (node->decl, args_to_pass);
1313 gimple_set_block (call, DECL_INITIAL (current_function_decl));
1314 args_to_pass.release ();
1316 /* For optimized away parameters, add on the caller side
1317 before the call
1318 DEBUG D#X => parm_Y(D)
1319 stmts and associate D#X with parm in decl_debug_args_lookup
1320 vector to say for debug info that if parameter parm had been passed,
1321 it would have value parm_Y(D). */
1322 if (args_to_skip)
1323 for (parm = DECL_ARGUMENTS (current_function_decl), num = 0;
1324 parm; parm = DECL_CHAIN (parm), num++)
1325 if (bitmap_bit_p (args_to_skip, num)
1326 && is_gimple_reg (parm))
1328 tree ddecl;
1329 gimple def_temp;
1331 /* This needs to be done even without MAY_HAVE_DEBUG_STMTS,
1332 otherwise if it didn't exist before, we'd end up with
1333 different SSA_NAME_VERSIONs between -g and -g0. */
1334 arg = get_or_create_ssa_default_def (cfun, parm);
1335 if (!MAY_HAVE_DEBUG_STMTS)
1336 continue;
1338 if (debug_args == NULL)
1339 debug_args = decl_debug_args_insert (node->decl);
1340 ddecl = make_node (DEBUG_EXPR_DECL);
1341 DECL_ARTIFICIAL (ddecl) = 1;
1342 TREE_TYPE (ddecl) = TREE_TYPE (parm);
1343 DECL_MODE (ddecl) = DECL_MODE (parm);
1344 vec_safe_push (*debug_args, DECL_ORIGIN (parm));
1345 vec_safe_push (*debug_args, ddecl);
1346 def_temp = gimple_build_debug_bind (ddecl, unshare_expr (arg),
1347 call);
1348 gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
1350 /* And on the callee side, add
1351 DEBUG D#Y s=> parm
1352 DEBUG var => D#Y
1353 stmts to the first bb where var is a VAR_DECL created for the
1354 optimized away parameter in DECL_INITIAL block. This hints
1355 in the debug info that var (whole DECL_ORIGIN is the parm PARM_DECL)
1356 is optimized away, but could be looked up at the call site
1357 as value of D#X there. */
1358 if (debug_args != NULL)
1360 unsigned int i;
1361 tree var, vexpr;
1362 gimple_stmt_iterator cgsi;
1363 gimple def_temp;
1365 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1366 var = BLOCK_VARS (DECL_INITIAL (node->decl));
1367 i = vec_safe_length (*debug_args);
1368 cgsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1371 i -= 2;
1372 while (var != NULL_TREE
1373 && DECL_ABSTRACT_ORIGIN (var) != (**debug_args)[i])
1374 var = TREE_CHAIN (var);
1375 if (var == NULL_TREE)
1376 break;
1377 vexpr = make_node (DEBUG_EXPR_DECL);
1378 parm = (**debug_args)[i];
1379 DECL_ARTIFICIAL (vexpr) = 1;
1380 TREE_TYPE (vexpr) = TREE_TYPE (parm);
1381 DECL_MODE (vexpr) = DECL_MODE (parm);
1382 def_temp = gimple_build_debug_source_bind (vexpr, parm,
1383 NULL);
1384 gsi_insert_before (&cgsi, def_temp, GSI_SAME_STMT);
1385 def_temp = gimple_build_debug_bind (var, vexpr, NULL);
1386 gsi_insert_before (&cgsi, def_temp, GSI_SAME_STMT);
1388 while (i);
1389 pop_cfun ();
1392 /* We avoid address being taken on any variable used by split part,
1393 so return slot optimization is always possible. Moreover this is
1394 required to make DECL_BY_REFERENCE work. */
1395 if (aggregate_value_p (DECL_RESULT (current_function_decl),
1396 TREE_TYPE (current_function_decl))
1397 && (!is_gimple_reg_type (TREE_TYPE (DECL_RESULT (current_function_decl)))
1398 || DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))))
1399 gimple_call_set_return_slot_opt (call, true);
1401 /* Update return value. This is bit tricky. When we do not return,
1402 do nothing. When we return we might need to update return_bb
1403 or produce a new return statement. */
1404 if (!split_part_return_p)
1405 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1406 else
1408 e = make_edge (call_bb, return_bb,
1409 return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
1410 ? 0 : EDGE_FALLTHRU);
1411 e->count = call_bb->count;
1412 e->probability = REG_BR_PROB_BASE;
1414 /* If there is return basic block, see what value we need to store
1415 return value into and put call just before it. */
1416 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1418 real_retval = retval = find_retval (return_bb);
1420 if (real_retval && split_point->split_part_set_retval)
1422 gimple_phi_iterator psi;
1424 /* See if we need new SSA_NAME for the result.
1425 When DECL_BY_REFERENCE is true, retval is actually pointer to
1426 return value and it is constant in whole function. */
1427 if (TREE_CODE (retval) == SSA_NAME
1428 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1430 retval = copy_ssa_name (retval, call);
1432 /* See if there is PHI defining return value. */
1433 for (psi = gsi_start_phis (return_bb);
1434 !gsi_end_p (psi); gsi_next (&psi))
1435 if (!virtual_operand_p (gimple_phi_result (psi.phi ())))
1436 break;
1438 /* When there is PHI, just update its value. */
1439 if (TREE_CODE (retval) == SSA_NAME
1440 && !gsi_end_p (psi))
1441 add_phi_arg (psi.phi (), retval, e, UNKNOWN_LOCATION);
1442 /* Otherwise update the return BB itself.
1443 find_return_bb allows at most one assignment to return value,
1444 so update first statement. */
1445 else
1447 gimple_stmt_iterator bsi;
1448 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi);
1449 gsi_next (&bsi))
1450 if (gimple_return return_stmt =
1451 dyn_cast <gimple_return> (gsi_stmt (bsi)))
1453 gimple_return_set_retval (return_stmt, retval);
1454 break;
1456 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
1457 && !gimple_clobber_p (gsi_stmt (bsi)))
1459 gimple_assign_set_rhs1 (gsi_stmt (bsi), retval);
1460 break;
1462 update_stmt (gsi_stmt (bsi));
1465 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1467 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1468 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1470 else
1472 tree restype;
1473 restype = TREE_TYPE (DECL_RESULT (current_function_decl));
1474 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1475 if (!useless_type_conversion_p (TREE_TYPE (retval), restype))
1477 gimple cpy;
1478 tree tem = create_tmp_reg (restype, NULL);
1479 tem = make_ssa_name (tem, call);
1480 cpy = gimple_build_assign_with_ops (NOP_EXPR, retval,
1481 tem, NULL_TREE);
1482 gsi_insert_after (&gsi, cpy, GSI_NEW_STMT);
1483 retval = tem;
1485 gimple_call_set_lhs (call, retval);
1486 update_stmt (call);
1489 else
1490 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1492 /* We don't use return block (there is either no return in function or
1493 multiple of them). So create new basic block with return statement.
1495 else
1497 gimple_return ret;
1498 if (split_point->split_part_set_retval
1499 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1501 retval = DECL_RESULT (current_function_decl);
1503 /* We use temporary register to hold value when aggregate_value_p
1504 is false. Similarly for DECL_BY_REFERENCE we must avoid extra
1505 copy. */
1506 if (!aggregate_value_p (retval, TREE_TYPE (current_function_decl))
1507 && !DECL_BY_REFERENCE (retval))
1508 retval = create_tmp_reg (TREE_TYPE (retval), NULL);
1509 if (is_gimple_reg (retval))
1511 /* When returning by reference, there is only one SSA name
1512 assigned to RESULT_DECL (that is pointer to return value).
1513 Look it up or create new one if it is missing. */
1514 if (DECL_BY_REFERENCE (retval))
1515 retval = get_or_create_ssa_default_def (cfun, retval);
1516 /* Otherwise produce new SSA name for return value. */
1517 else
1518 retval = make_ssa_name (retval, call);
1520 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1521 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1522 else
1523 gimple_call_set_lhs (call, retval);
1525 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1526 ret = gimple_build_return (retval);
1527 gsi_insert_after (&gsi, ret, GSI_NEW_STMT);
1530 free_dominance_info (CDI_DOMINATORS);
1531 free_dominance_info (CDI_POST_DOMINATORS);
1532 compute_inline_parameters (node, true);
1535 /* Execute function splitting pass. */
1537 static unsigned int
1538 execute_split_functions (void)
1540 gimple_stmt_iterator bsi;
1541 basic_block bb;
1542 int overall_time = 0, overall_size = 0;
1543 int todo = 0;
1544 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1546 if (flags_from_decl_or_type (current_function_decl)
1547 & (ECF_NORETURN|ECF_MALLOC))
1549 if (dump_file)
1550 fprintf (dump_file, "Not splitting: noreturn/malloc function.\n");
1551 return 0;
1553 if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
1555 if (dump_file)
1556 fprintf (dump_file, "Not splitting: main function.\n");
1557 return 0;
1559 /* This can be relaxed; function might become inlinable after splitting
1560 away the uninlinable part. */
1561 if (inline_edge_summary_vec.exists ()
1562 && !inline_summary (node)->inlinable)
1564 if (dump_file)
1565 fprintf (dump_file, "Not splitting: not inlinable.\n");
1566 return 0;
1568 if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1570 if (dump_file)
1571 fprintf (dump_file, "Not splitting: disregarding inline limits.\n");
1572 return 0;
1574 /* This can be relaxed; most of versioning tests actually prevents
1575 a duplication. */
1576 if (!tree_versionable_function_p (current_function_decl))
1578 if (dump_file)
1579 fprintf (dump_file, "Not splitting: not versionable.\n");
1580 return 0;
1582 /* FIXME: we could support this. */
1583 if (DECL_STRUCT_FUNCTION (current_function_decl)->static_chain_decl)
1585 if (dump_file)
1586 fprintf (dump_file, "Not splitting: nested function.\n");
1587 return 0;
1590 /* See if it makes sense to try to split.
1591 It makes sense to split if we inline, that is if we have direct calls to
1592 handle or direct calls are possibly going to appear as result of indirect
1593 inlining or LTO. Also handle -fprofile-generate as LTO to allow non-LTO
1594 training for LTO -fprofile-use build.
1596 Note that we are not completely conservative about disqualifying functions
1597 called once. It is possible that the caller is called more then once and
1598 then inlining would still benefit. */
1599 if ((!node->callers
1600 /* Local functions called once will be completely inlined most of time. */
1601 || (!node->callers->next_caller && node->local.local))
1602 && !node->address_taken
1603 && (!flag_lto || !node->externally_visible))
1605 if (dump_file)
1606 fprintf (dump_file, "Not splitting: not called directly "
1607 "or called once.\n");
1608 return 0;
1611 /* FIXME: We can actually split if splitting reduces call overhead. */
1612 if (!flag_inline_small_functions
1613 && !DECL_DECLARED_INLINE_P (current_function_decl))
1615 if (dump_file)
1616 fprintf (dump_file, "Not splitting: not autoinlining and function"
1617 " is not inline.\n");
1618 return 0;
1621 /* We enforce splitting after loop headers when profile info is not
1622 available. */
1623 if (profile_status_for_fn (cfun) != PROFILE_READ)
1624 mark_dfs_back_edges ();
1626 /* Initialize bitmap to track forbidden calls. */
1627 forbidden_dominators = BITMAP_ALLOC (NULL);
1628 calculate_dominance_info (CDI_DOMINATORS);
1630 /* Compute local info about basic blocks and determine function size/time. */
1631 bb_info_vec.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
1632 memset (&best_split_point, 0, sizeof (best_split_point));
1633 FOR_EACH_BB_FN (bb, cfun)
1635 int time = 0;
1636 int size = 0;
1637 int freq = compute_call_stmt_bb_frequency (current_function_decl, bb);
1639 if (dump_file && (dump_flags & TDF_DETAILS))
1640 fprintf (dump_file, "Basic block %i\n", bb->index);
1642 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1644 int this_time, this_size;
1645 gimple stmt = gsi_stmt (bsi);
1647 this_size = estimate_num_insns (stmt, &eni_size_weights);
1648 this_time = estimate_num_insns (stmt, &eni_time_weights) * freq;
1649 size += this_size;
1650 time += this_time;
1651 check_forbidden_calls (stmt);
1653 if (dump_file && (dump_flags & TDF_DETAILS))
1655 fprintf (dump_file, " freq:%6i size:%3i time:%3i ",
1656 freq, this_size, this_time);
1657 print_gimple_stmt (dump_file, stmt, 0, 0);
1660 overall_time += time;
1661 overall_size += size;
1662 bb_info_vec[bb->index].time = time;
1663 bb_info_vec[bb->index].size = size;
1665 find_split_points (overall_time, overall_size);
1666 if (best_split_point.split_bbs)
1668 split_function (&best_split_point);
1669 BITMAP_FREE (best_split_point.ssa_names_to_pass);
1670 BITMAP_FREE (best_split_point.split_bbs);
1671 todo = TODO_update_ssa | TODO_cleanup_cfg;
1673 BITMAP_FREE (forbidden_dominators);
1674 bb_info_vec.release ();
1675 return todo;
1678 namespace {
1680 const pass_data pass_data_split_functions =
1682 GIMPLE_PASS, /* type */
1683 "fnsplit", /* name */
1684 OPTGROUP_NONE, /* optinfo_flags */
1685 TV_IPA_FNSPLIT, /* tv_id */
1686 PROP_cfg, /* properties_required */
1687 0, /* properties_provided */
1688 0, /* properties_destroyed */
1689 0, /* todo_flags_start */
1690 0, /* todo_flags_finish */
1693 class pass_split_functions : public gimple_opt_pass
1695 public:
1696 pass_split_functions (gcc::context *ctxt)
1697 : gimple_opt_pass (pass_data_split_functions, ctxt)
1700 /* opt_pass methods: */
1701 virtual bool gate (function *);
1702 virtual unsigned int execute (function *)
1704 return execute_split_functions ();
1707 }; // class pass_split_functions
1709 bool
1710 pass_split_functions::gate (function *)
1712 /* When doing profile feedback, we want to execute the pass after profiling
1713 is read. So disable one in early optimization. */
1714 return (flag_partial_inlining
1715 && !profile_arc_flag && !flag_branch_probabilities);
1718 } // anon namespace
1720 gimple_opt_pass *
1721 make_pass_split_functions (gcc::context *ctxt)
1723 return new pass_split_functions (ctxt);
1726 /* Execute function splitting pass. */
1728 static unsigned int
1729 execute_feedback_split_functions (void)
1731 unsigned int retval = execute_split_functions ();
1732 if (retval)
1733 retval |= TODO_rebuild_cgraph_edges;
1734 return retval;
1737 namespace {
1739 const pass_data pass_data_feedback_split_functions =
1741 GIMPLE_PASS, /* type */
1742 "feedback_fnsplit", /* name */
1743 OPTGROUP_NONE, /* optinfo_flags */
1744 TV_IPA_FNSPLIT, /* tv_id */
1745 PROP_cfg, /* properties_required */
1746 0, /* properties_provided */
1747 0, /* properties_destroyed */
1748 0, /* todo_flags_start */
1749 0, /* todo_flags_finish */
1752 class pass_feedback_split_functions : public gimple_opt_pass
1754 public:
1755 pass_feedback_split_functions (gcc::context *ctxt)
1756 : gimple_opt_pass (pass_data_feedback_split_functions, ctxt)
1759 /* opt_pass methods: */
1760 virtual bool gate (function *);
1761 virtual unsigned int execute (function *)
1763 return execute_feedback_split_functions ();
1766 }; // class pass_feedback_split_functions
1768 bool
1769 pass_feedback_split_functions::gate (function *)
1771 /* We don't need to split when profiling at all, we are producing
1772 lousy code anyway. */
1773 return (flag_partial_inlining
1774 && flag_branch_probabilities);
1777 } // anon namespace
1779 gimple_opt_pass *
1780 make_pass_feedback_split_functions (gcc::context *ctxt)
1782 return new pass_feedback_split_functions (ctxt);