Preparatory work before subclass renaming
[official-gcc.git] / gcc / ipa-split.c
blob56bd9ca671ac8c369fafaf70e4dc2812353a7581
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 bb = worklist.pop ();
233 FOR_EACH_EDGE (e, ei, bb->preds)
234 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
235 && bitmap_set_bit (seen, e->src->index))
237 gcc_checking_assert (!bitmap_bit_p (current->split_bbs,
238 e->src->index));
239 worklist.safe_push (e->src);
241 for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
242 gsi_next (&bsi))
244 gimple stmt = gsi_stmt (bsi);
245 if (is_gimple_debug (stmt))
246 continue;
247 if (walk_stmt_load_store_addr_ops
248 (stmt, non_ssa_vars, test_nonssa_use, test_nonssa_use,
249 test_nonssa_use))
251 ok = false;
252 goto done;
254 if (gimple_label label_stmt = dyn_cast <gimple_label> (stmt))
255 if (test_nonssa_use (stmt, gimple_label_label (label_stmt),
256 NULL_TREE, non_ssa_vars))
258 ok = false;
259 goto done;
262 for (gimple_phi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
263 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 (gimple_phi_iterator bsi = gsi_start_phis (return_bb);
278 !gsi_end_p (bsi);
279 gsi_next (&bsi))
281 gimple_phi stmt = bsi.phi ();
282 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
284 if (virtual_operand_p (gimple_phi_result (stmt)))
285 continue;
286 if (TREE_CODE (op) != SSA_NAME
287 && test_nonssa_use (stmt, op, op, non_ssa_vars))
289 ok = false;
290 goto done;
296 /* Verify that the rest of function does not define any label
297 used by the split part. */
298 FOR_EACH_BB_FN (bb, cfun)
299 if (!bitmap_bit_p (current->split_bbs, bb->index)
300 && !bitmap_bit_p (seen, bb->index))
302 gimple_stmt_iterator bsi;
303 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
304 if (gimple_label label_stmt =
305 dyn_cast <gimple_label> (gsi_stmt (bsi)))
307 if (test_nonssa_use (label_stmt,
308 gimple_label_label (label_stmt),
309 NULL_TREE, non_ssa_vars))
311 ok = false;
312 goto done;
315 else
316 break;
319 done:
320 BITMAP_FREE (seen);
321 worklist.release ();
322 return ok;
325 /* If STMT is a call, check the callee against a list of forbidden
326 predicate functions. If a match is found, look for uses of the
327 call result in condition statements that compare against zero.
328 For each such use, find the block targeted by the condition
329 statement for the nonzero result, and set the bit for this block
330 in the forbidden dominators bitmap. The purpose of this is to avoid
331 selecting a split point where we are likely to lose the chance
332 to optimize away an unused function call. */
334 static void
335 check_forbidden_calls (gimple stmt)
337 imm_use_iterator use_iter;
338 use_operand_p use_p;
339 tree lhs;
341 /* At the moment, __builtin_constant_p is the only forbidden
342 predicate function call (see PR49642). */
343 if (!gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P))
344 return;
346 lhs = gimple_call_lhs (stmt);
348 if (!lhs || TREE_CODE (lhs) != SSA_NAME)
349 return;
351 FOR_EACH_IMM_USE_FAST (use_p, use_iter, lhs)
353 tree op1;
354 basic_block use_bb, forbidden_bb;
355 enum tree_code code;
356 edge true_edge, false_edge;
357 gimple_cond use_stmt;
359 use_stmt = dyn_cast <gimple_cond> (USE_STMT (use_p));
360 if (!use_stmt)
361 continue;
363 /* Assuming canonical form for GIMPLE_COND here, with constant
364 in second position. */
365 op1 = gimple_cond_rhs (use_stmt);
366 code = gimple_cond_code (use_stmt);
367 use_bb = gimple_bb (use_stmt);
369 extract_true_false_edges_from_block (use_bb, &true_edge, &false_edge);
371 /* We're only interested in comparisons that distinguish
372 unambiguously from zero. */
373 if (!integer_zerop (op1) || code == LE_EXPR || code == GE_EXPR)
374 continue;
376 if (code == EQ_EXPR)
377 forbidden_bb = false_edge->dest;
378 else
379 forbidden_bb = true_edge->dest;
381 bitmap_set_bit (forbidden_dominators, forbidden_bb->index);
385 /* If BB is dominated by any block in the forbidden dominators set,
386 return TRUE; else FALSE. */
388 static bool
389 dominated_by_forbidden (basic_block bb)
391 unsigned dom_bb;
392 bitmap_iterator bi;
394 EXECUTE_IF_SET_IN_BITMAP (forbidden_dominators, 1, dom_bb, bi)
396 if (dominated_by_p (CDI_DOMINATORS, bb,
397 BASIC_BLOCK_FOR_FN (cfun, dom_bb)))
398 return true;
401 return false;
404 /* We found an split_point CURRENT. NON_SSA_VARS is bitmap of all non ssa
405 variables used and RETURN_BB is return basic block.
406 See if we can split function here. */
408 static void
409 consider_split (struct split_point *current, bitmap non_ssa_vars,
410 basic_block return_bb)
412 tree parm;
413 unsigned int num_args = 0;
414 unsigned int call_overhead;
415 edge e;
416 edge_iterator ei;
417 gimple_phi_iterator bsi;
418 unsigned int i;
419 int incoming_freq = 0;
420 tree retval;
421 bool back_edge = false;
423 if (dump_file && (dump_flags & TDF_DETAILS))
424 dump_split_point (dump_file, current);
426 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
428 if (e->flags & EDGE_DFS_BACK)
429 back_edge = true;
430 if (!bitmap_bit_p (current->split_bbs, e->src->index))
431 incoming_freq += EDGE_FREQUENCY (e);
434 /* Do not split when we would end up calling function anyway. */
435 if (incoming_freq
436 >= (ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency
437 * PARAM_VALUE (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY) / 100))
439 /* When profile is guessed, we can not expect it to give us
440 realistic estimate on likelyness of function taking the
441 complex path. As a special case, when tail of the function is
442 a loop, enable splitting since inlining code skipping the loop
443 is likely noticeable win. */
444 if (back_edge
445 && profile_status_for_fn (cfun) != PROFILE_READ
446 && incoming_freq < ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency)
448 if (dump_file && (dump_flags & TDF_DETAILS))
449 fprintf (dump_file,
450 " Split before loop, accepting despite low frequencies %i %i.\n",
451 incoming_freq,
452 ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency);
454 else
456 if (dump_file && (dump_flags & TDF_DETAILS))
457 fprintf (dump_file,
458 " Refused: incoming frequency is too large.\n");
459 return;
463 if (!current->header_size)
465 if (dump_file && (dump_flags & TDF_DETAILS))
466 fprintf (dump_file, " Refused: header empty\n");
467 return;
470 /* Verify that PHI args on entry are either virtual or all their operands
471 incoming from header are the same. */
472 for (bsi = gsi_start_phis (current->entry_bb); !gsi_end_p (bsi); gsi_next (&bsi))
474 gimple_phi stmt = bsi.phi ();
475 tree val = NULL;
477 if (virtual_operand_p (gimple_phi_result (stmt)))
478 continue;
479 for (i = 0; i < gimple_phi_num_args (stmt); i++)
481 edge e = gimple_phi_arg_edge (stmt, i);
482 if (!bitmap_bit_p (current->split_bbs, e->src->index))
484 tree edge_val = gimple_phi_arg_def (stmt, i);
485 if (val && edge_val != val)
487 if (dump_file && (dump_flags & TDF_DETAILS))
488 fprintf (dump_file,
489 " Refused: entry BB has PHI with multiple variants\n");
490 return;
492 val = edge_val;
498 /* See what argument we will pass to the split function and compute
499 call overhead. */
500 call_overhead = eni_size_weights.call_cost;
501 for (parm = DECL_ARGUMENTS (current_function_decl); parm;
502 parm = DECL_CHAIN (parm))
504 if (!is_gimple_reg (parm))
506 if (bitmap_bit_p (non_ssa_vars, DECL_UID (parm)))
508 if (dump_file && (dump_flags & TDF_DETAILS))
509 fprintf (dump_file,
510 " Refused: need to pass non-ssa param values\n");
511 return;
514 else
516 tree ddef = ssa_default_def (cfun, parm);
517 if (ddef
518 && bitmap_bit_p (current->ssa_names_to_pass,
519 SSA_NAME_VERSION (ddef)))
521 if (!VOID_TYPE_P (TREE_TYPE (parm)))
522 call_overhead += estimate_move_cost (TREE_TYPE (parm), false);
523 num_args++;
527 if (!VOID_TYPE_P (TREE_TYPE (current_function_decl)))
528 call_overhead += estimate_move_cost (TREE_TYPE (current_function_decl),
529 false);
531 if (current->split_size <= call_overhead)
533 if (dump_file && (dump_flags & TDF_DETAILS))
534 fprintf (dump_file,
535 " Refused: split size is smaller than call overhead\n");
536 return;
538 if (current->header_size + call_overhead
539 >= (unsigned int)(DECL_DECLARED_INLINE_P (current_function_decl)
540 ? MAX_INLINE_INSNS_SINGLE
541 : MAX_INLINE_INSNS_AUTO))
543 if (dump_file && (dump_flags & TDF_DETAILS))
544 fprintf (dump_file,
545 " Refused: header size is too large for inline candidate\n");
546 return;
549 /* FIXME: we currently can pass only SSA function parameters to the split
550 arguments. Once parm_adjustment infrastructure is supported by cloning,
551 we can pass more than that. */
552 if (num_args != bitmap_count_bits (current->ssa_names_to_pass))
555 if (dump_file && (dump_flags & TDF_DETAILS))
556 fprintf (dump_file,
557 " Refused: need to pass non-param values\n");
558 return;
561 /* When there are non-ssa vars used in the split region, see if they
562 are used in the header region. If so, reject the split.
563 FIXME: we can use nested function support to access both. */
564 if (!bitmap_empty_p (non_ssa_vars)
565 && !verify_non_ssa_vars (current, non_ssa_vars, return_bb))
567 if (dump_file && (dump_flags & TDF_DETAILS))
568 fprintf (dump_file,
569 " Refused: split part has non-ssa uses\n");
570 return;
573 /* If the split point is dominated by a forbidden block, reject
574 the split. */
575 if (!bitmap_empty_p (forbidden_dominators)
576 && dominated_by_forbidden (current->entry_bb))
578 if (dump_file && (dump_flags & TDF_DETAILS))
579 fprintf (dump_file,
580 " Refused: split point dominated by forbidden block\n");
581 return;
584 /* See if retval used by return bb is computed by header or split part.
585 When it is computed by split part, we need to produce return statement
586 in the split part and add code to header to pass it around.
588 This is bit tricky to test:
589 1) When there is no return_bb or no return value, we always pass
590 value around.
591 2) Invariants are always computed by caller.
592 3) For SSA we need to look if defining statement is in header or split part
593 4) For non-SSA we need to look where the var is computed. */
594 retval = find_retval (return_bb);
595 if (!retval)
596 current->split_part_set_retval = true;
597 else if (is_gimple_min_invariant (retval))
598 current->split_part_set_retval = false;
599 /* Special case is value returned by reference we record as if it was non-ssa
600 set to result_decl. */
601 else if (TREE_CODE (retval) == SSA_NAME
602 && SSA_NAME_VAR (retval)
603 && TREE_CODE (SSA_NAME_VAR (retval)) == RESULT_DECL
604 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
605 current->split_part_set_retval
606 = bitmap_bit_p (non_ssa_vars, DECL_UID (SSA_NAME_VAR (retval)));
607 else if (TREE_CODE (retval) == SSA_NAME)
608 current->split_part_set_retval
609 = (!SSA_NAME_IS_DEFAULT_DEF (retval)
610 && (bitmap_bit_p (current->split_bbs,
611 gimple_bb (SSA_NAME_DEF_STMT (retval))->index)
612 || gimple_bb (SSA_NAME_DEF_STMT (retval)) == return_bb));
613 else if (TREE_CODE (retval) == PARM_DECL)
614 current->split_part_set_retval = false;
615 else if (TREE_CODE (retval) == VAR_DECL
616 || TREE_CODE (retval) == RESULT_DECL)
617 current->split_part_set_retval
618 = bitmap_bit_p (non_ssa_vars, DECL_UID (retval));
619 else
620 current->split_part_set_retval = true;
622 /* split_function fixes up at most one PHI non-virtual PHI node in return_bb,
623 for the return value. If there are other PHIs, give up. */
624 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
626 gimple_phi_iterator psi;
628 for (psi = gsi_start_phis (return_bb); !gsi_end_p (psi); gsi_next (&psi))
629 if (!virtual_operand_p (gimple_phi_result (psi.phi ()))
630 && !(retval
631 && current->split_part_set_retval
632 && TREE_CODE (retval) == SSA_NAME
633 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
634 && SSA_NAME_DEF_STMT (retval) == psi.phi ()))
636 if (dump_file && (dump_flags & TDF_DETAILS))
637 fprintf (dump_file,
638 " Refused: return bb has extra PHIs\n");
639 return;
643 if (dump_file && (dump_flags & TDF_DETAILS))
644 fprintf (dump_file, " Accepted!\n");
646 /* At the moment chose split point with lowest frequency and that leaves
647 out smallest size of header.
648 In future we might re-consider this heuristics. */
649 if (!best_split_point.split_bbs
650 || best_split_point.entry_bb->frequency > current->entry_bb->frequency
651 || (best_split_point.entry_bb->frequency == current->entry_bb->frequency
652 && best_split_point.split_size < current->split_size))
655 if (dump_file && (dump_flags & TDF_DETAILS))
656 fprintf (dump_file, " New best split point!\n");
657 if (best_split_point.ssa_names_to_pass)
659 BITMAP_FREE (best_split_point.ssa_names_to_pass);
660 BITMAP_FREE (best_split_point.split_bbs);
662 best_split_point = *current;
663 best_split_point.ssa_names_to_pass = BITMAP_ALLOC (NULL);
664 bitmap_copy (best_split_point.ssa_names_to_pass,
665 current->ssa_names_to_pass);
666 best_split_point.split_bbs = BITMAP_ALLOC (NULL);
667 bitmap_copy (best_split_point.split_bbs, current->split_bbs);
671 /* Return basic block containing RETURN statement. We allow basic blocks
672 of the form:
673 <retval> = tmp_var;
674 return <retval>
675 but return_bb can not be more complex than this.
676 If nothing is found, return the exit block.
678 When there are multiple RETURN statement, chose one with return value,
679 since that one is more likely shared by multiple code paths.
681 Return BB is special, because for function splitting it is the only
682 basic block that is duplicated in between header and split part of the
683 function.
685 TODO: We might support multiple return blocks. */
687 static basic_block
688 find_return_bb (void)
690 edge e;
691 basic_block return_bb = EXIT_BLOCK_PTR_FOR_FN (cfun);
692 gimple_stmt_iterator bsi;
693 bool found_return = false;
694 tree retval = NULL_TREE;
696 if (!single_pred_p (EXIT_BLOCK_PTR_FOR_FN (cfun)))
697 return return_bb;
699 e = single_pred_edge (EXIT_BLOCK_PTR_FOR_FN (cfun));
700 for (bsi = gsi_last_bb (e->src); !gsi_end_p (bsi); gsi_prev (&bsi))
702 gimple stmt = gsi_stmt (bsi);
703 if (gimple_code (stmt) == GIMPLE_LABEL
704 || is_gimple_debug (stmt)
705 || gimple_clobber_p (stmt))
707 else if (gimple_code (stmt) == GIMPLE_ASSIGN
708 && found_return
709 && gimple_assign_single_p (stmt)
710 && (auto_var_in_fn_p (gimple_assign_rhs1 (stmt),
711 current_function_decl)
712 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
713 && retval == gimple_assign_lhs (stmt))
715 else if (gimple_return return_stmt = dyn_cast <gimple_return> (stmt))
717 found_return = true;
718 retval = gimple_return_retval (return_stmt);
720 else
721 break;
723 if (gsi_end_p (bsi) && found_return)
724 return_bb = e->src;
726 return return_bb;
729 /* Given return basic block RETURN_BB, see where return value is really
730 stored. */
731 static tree
732 find_retval (basic_block return_bb)
734 gimple_stmt_iterator bsi;
735 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
736 if (gimple_return return_stmt = dyn_cast <gimple_return> (gsi_stmt (bsi)))
737 return gimple_return_retval (return_stmt);
738 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
739 && !gimple_clobber_p (gsi_stmt (bsi)))
740 return gimple_assign_rhs1 (gsi_stmt (bsi));
741 return NULL;
744 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
745 variable, mark it as used in bitmap passed via DATA.
746 Return true when access to T prevents splitting the function. */
748 static bool
749 mark_nonssa_use (gimple, tree t, tree, void *data)
751 t = get_base_address (t);
753 if (!t || is_gimple_reg (t))
754 return false;
756 /* At present we can't pass non-SSA arguments to split function.
757 FIXME: this can be relaxed by passing references to arguments. */
758 if (TREE_CODE (t) == PARM_DECL)
760 if (dump_file && (dump_flags & TDF_DETAILS))
761 fprintf (dump_file,
762 "Cannot split: use of non-ssa function parameter.\n");
763 return true;
766 if ((TREE_CODE (t) == VAR_DECL
767 && auto_var_in_fn_p (t, current_function_decl))
768 || TREE_CODE (t) == RESULT_DECL
769 || (TREE_CODE (t) == LABEL_DECL
770 && FORCED_LABEL (t)))
771 bitmap_set_bit ((bitmap)data, DECL_UID (t));
773 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
774 to pretend that the value pointed to is actual result decl. */
775 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
776 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
777 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
778 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
779 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
780 return
781 bitmap_bit_p ((bitmap)data,
782 DECL_UID (DECL_RESULT (current_function_decl)));
784 return false;
787 /* Compute local properties of basic block BB we collect when looking for
788 split points. We look for ssa defs and store them in SET_SSA_NAMES,
789 for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic
790 vars stored in NON_SSA_VARS.
792 When BB has edge to RETURN_BB, collect uses in RETURN_BB too.
794 Return false when BB contains something that prevents it from being put into
795 split function. */
797 static bool
798 visit_bb (basic_block bb, basic_block return_bb,
799 bitmap set_ssa_names, bitmap used_ssa_names,
800 bitmap non_ssa_vars)
802 edge e;
803 edge_iterator ei;
804 bool can_split = true;
806 for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
807 gsi_next (&bsi))
809 gimple stmt = gsi_stmt (bsi);
810 tree op;
811 ssa_op_iter iter;
812 tree decl;
814 if (is_gimple_debug (stmt))
815 continue;
817 if (gimple_clobber_p (stmt))
818 continue;
820 /* FIXME: We can split regions containing EH. We can not however
821 split RESX, EH_DISPATCH and EH_POINTER referring to same region
822 into different partitions. This would require tracking of
823 EH regions and checking in consider_split_point if they
824 are not used elsewhere. */
825 if (gimple_code (stmt) == GIMPLE_RESX)
827 if (dump_file && (dump_flags & TDF_DETAILS))
828 fprintf (dump_file, "Cannot split: resx.\n");
829 can_split = false;
831 if (gimple_code (stmt) == GIMPLE_EH_DISPATCH)
833 if (dump_file && (dump_flags & TDF_DETAILS))
834 fprintf (dump_file, "Cannot split: eh dispatch.\n");
835 can_split = false;
838 /* Check builtins that prevent splitting. */
839 if (gimple_code (stmt) == GIMPLE_CALL
840 && (decl = gimple_call_fndecl (stmt)) != NULL_TREE
841 && DECL_BUILT_IN (decl)
842 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
843 switch (DECL_FUNCTION_CODE (decl))
845 /* FIXME: once we will allow passing non-parm values to split part,
846 we need to be sure to handle correct builtin_stack_save and
847 builtin_stack_restore. At the moment we are safe; there is no
848 way to store builtin_stack_save result in non-SSA variable
849 since all calls to those are compiler generated. */
850 case BUILT_IN_APPLY:
851 case BUILT_IN_APPLY_ARGS:
852 case BUILT_IN_VA_START:
853 if (dump_file && (dump_flags & TDF_DETAILS))
854 fprintf (dump_file,
855 "Cannot split: builtin_apply and va_start.\n");
856 can_split = false;
857 break;
858 case BUILT_IN_EH_POINTER:
859 if (dump_file && (dump_flags & TDF_DETAILS))
860 fprintf (dump_file, "Cannot split: builtin_eh_pointer.\n");
861 can_split = false;
862 break;
863 default:
864 break;
867 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
868 bitmap_set_bit (set_ssa_names, SSA_NAME_VERSION (op));
869 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
870 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
871 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
872 mark_nonssa_use,
873 mark_nonssa_use,
874 mark_nonssa_use);
876 for (gimple_phi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
877 gsi_next (&bsi))
879 gimple_phi stmt = bsi.phi ();
880 unsigned int i;
882 if (virtual_operand_p (gimple_phi_result (stmt)))
883 continue;
884 bitmap_set_bit (set_ssa_names,
885 SSA_NAME_VERSION (gimple_phi_result (stmt)));
886 for (i = 0; i < gimple_phi_num_args (stmt); i++)
888 tree op = gimple_phi_arg_def (stmt, i);
889 if (TREE_CODE (op) == SSA_NAME)
890 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
892 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
893 mark_nonssa_use,
894 mark_nonssa_use,
895 mark_nonssa_use);
897 /* Record also uses coming from PHI operand in return BB. */
898 FOR_EACH_EDGE (e, ei, bb->succs)
899 if (e->dest == return_bb)
901 for (gimple_phi_iterator bsi = gsi_start_phis (return_bb);
902 !gsi_end_p (bsi);
903 gsi_next (&bsi))
905 gimple_phi stmt = bsi.phi ();
906 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
908 if (virtual_operand_p (gimple_phi_result (stmt)))
909 continue;
910 if (TREE_CODE (op) == SSA_NAME)
911 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
912 else
913 can_split &= !mark_nonssa_use (stmt, op, op, non_ssa_vars);
916 return can_split;
919 /* Stack entry for recursive DFS walk in find_split_point. */
921 typedef struct
923 /* Basic block we are examining. */
924 basic_block bb;
926 /* SSA names set and used by the BB and all BBs reachable
927 from it via DFS walk. */
928 bitmap set_ssa_names, used_ssa_names;
929 bitmap non_ssa_vars;
931 /* All BBS visited from this BB via DFS walk. */
932 bitmap bbs_visited;
934 /* Last examined edge in DFS walk. Since we walk unoriented graph,
935 the value is up to sum of incoming and outgoing edges of BB. */
936 unsigned int edge_num;
938 /* Stack entry index of earliest BB reachable from current BB
939 or any BB visited later in DFS walk. */
940 int earliest;
942 /* Overall time and size of all BBs reached from this BB in DFS walk. */
943 int overall_time, overall_size;
945 /* When false we can not split on this BB. */
946 bool can_split;
947 } stack_entry;
950 /* Find all articulations and call consider_split on them.
951 OVERALL_TIME and OVERALL_SIZE is time and size of the function.
953 We perform basic algorithm for finding an articulation in a graph
954 created from CFG by considering it to be an unoriented graph.
956 The articulation is discovered via DFS walk. We collect earliest
957 basic block on stack that is reachable via backward edge. Articulation
958 is any basic block such that there is no backward edge bypassing it.
959 To reduce stack usage we maintain heap allocated stack in STACK vector.
960 AUX pointer of BB is set to index it appears in the stack or -1 once
961 it is visited and popped off the stack.
963 The algorithm finds articulation after visiting the whole component
964 reachable by it. This makes it convenient to collect information about
965 the component used by consider_split. */
967 static void
968 find_split_points (int overall_time, int overall_size)
970 stack_entry first;
971 vec<stack_entry> stack = vNULL;
972 basic_block bb;
973 basic_block return_bb = find_return_bb ();
974 struct split_point current;
976 current.header_time = overall_time;
977 current.header_size = overall_size;
978 current.split_time = 0;
979 current.split_size = 0;
980 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
982 first.bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
983 first.edge_num = 0;
984 first.overall_time = 0;
985 first.overall_size = 0;
986 first.earliest = INT_MAX;
987 first.set_ssa_names = 0;
988 first.used_ssa_names = 0;
989 first.non_ssa_vars = 0;
990 first.bbs_visited = 0;
991 first.can_split = false;
992 stack.safe_push (first);
993 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = (void *)(intptr_t)-1;
995 while (!stack.is_empty ())
997 stack_entry *entry = &stack.last ();
999 /* We are walking an acyclic graph, so edge_num counts
1000 succ and pred edges together. However when considering
1001 articulation, we want to have processed everything reachable
1002 from articulation but nothing that reaches into it. */
1003 if (entry->edge_num == EDGE_COUNT (entry->bb->succs)
1004 && entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1006 int pos = stack.length ();
1007 entry->can_split &= visit_bb (entry->bb, return_bb,
1008 entry->set_ssa_names,
1009 entry->used_ssa_names,
1010 entry->non_ssa_vars);
1011 if (pos <= entry->earliest && !entry->can_split
1012 && dump_file && (dump_flags & TDF_DETAILS))
1013 fprintf (dump_file,
1014 "found articulation at bb %i but can not split\n",
1015 entry->bb->index);
1016 if (pos <= entry->earliest && entry->can_split)
1018 if (dump_file && (dump_flags & TDF_DETAILS))
1019 fprintf (dump_file, "found articulation at bb %i\n",
1020 entry->bb->index);
1021 current.entry_bb = entry->bb;
1022 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
1023 bitmap_and_compl (current.ssa_names_to_pass,
1024 entry->used_ssa_names, entry->set_ssa_names);
1025 current.header_time = overall_time - entry->overall_time;
1026 current.header_size = overall_size - entry->overall_size;
1027 current.split_time = entry->overall_time;
1028 current.split_size = entry->overall_size;
1029 current.split_bbs = entry->bbs_visited;
1030 consider_split (&current, entry->non_ssa_vars, return_bb);
1031 BITMAP_FREE (current.ssa_names_to_pass);
1034 /* Do actual DFS walk. */
1035 if (entry->edge_num
1036 < (EDGE_COUNT (entry->bb->succs)
1037 + EDGE_COUNT (entry->bb->preds)))
1039 edge e;
1040 basic_block dest;
1041 if (entry->edge_num < EDGE_COUNT (entry->bb->succs))
1043 e = EDGE_SUCC (entry->bb, entry->edge_num);
1044 dest = e->dest;
1046 else
1048 e = EDGE_PRED (entry->bb, entry->edge_num
1049 - EDGE_COUNT (entry->bb->succs));
1050 dest = e->src;
1053 entry->edge_num++;
1055 /* New BB to visit, push it to the stack. */
1056 if (dest != return_bb && dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
1057 && !dest->aux)
1059 stack_entry new_entry;
1061 new_entry.bb = dest;
1062 new_entry.edge_num = 0;
1063 new_entry.overall_time
1064 = bb_info_vec[dest->index].time;
1065 new_entry.overall_size
1066 = bb_info_vec[dest->index].size;
1067 new_entry.earliest = INT_MAX;
1068 new_entry.set_ssa_names = BITMAP_ALLOC (NULL);
1069 new_entry.used_ssa_names = BITMAP_ALLOC (NULL);
1070 new_entry.bbs_visited = BITMAP_ALLOC (NULL);
1071 new_entry.non_ssa_vars = BITMAP_ALLOC (NULL);
1072 new_entry.can_split = true;
1073 bitmap_set_bit (new_entry.bbs_visited, dest->index);
1074 stack.safe_push (new_entry);
1075 dest->aux = (void *)(intptr_t)stack.length ();
1077 /* Back edge found, record the earliest point. */
1078 else if ((intptr_t)dest->aux > 0
1079 && (intptr_t)dest->aux < entry->earliest)
1080 entry->earliest = (intptr_t)dest->aux;
1082 /* We are done with examining the edges. Pop off the value from stack
1083 and merge stuff we accumulate during the walk. */
1084 else if (entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1086 stack_entry *prev = &stack[stack.length () - 2];
1088 entry->bb->aux = (void *)(intptr_t)-1;
1089 prev->can_split &= entry->can_split;
1090 if (prev->set_ssa_names)
1092 bitmap_ior_into (prev->set_ssa_names, entry->set_ssa_names);
1093 bitmap_ior_into (prev->used_ssa_names, entry->used_ssa_names);
1094 bitmap_ior_into (prev->bbs_visited, entry->bbs_visited);
1095 bitmap_ior_into (prev->non_ssa_vars, entry->non_ssa_vars);
1097 if (prev->earliest > entry->earliest)
1098 prev->earliest = entry->earliest;
1099 prev->overall_time += entry->overall_time;
1100 prev->overall_size += entry->overall_size;
1101 BITMAP_FREE (entry->set_ssa_names);
1102 BITMAP_FREE (entry->used_ssa_names);
1103 BITMAP_FREE (entry->bbs_visited);
1104 BITMAP_FREE (entry->non_ssa_vars);
1105 stack.pop ();
1107 else
1108 stack.pop ();
1110 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = NULL;
1111 FOR_EACH_BB_FN (bb, cfun)
1112 bb->aux = NULL;
1113 stack.release ();
1114 BITMAP_FREE (current.ssa_names_to_pass);
1117 /* Split function at SPLIT_POINT. */
1119 static void
1120 split_function (struct split_point *split_point)
1122 vec<tree> args_to_pass = vNULL;
1123 bitmap args_to_skip;
1124 tree parm;
1125 int num = 0;
1126 cgraph_node *node, *cur_node = cgraph_node::get (current_function_decl);
1127 basic_block return_bb = find_return_bb ();
1128 basic_block call_bb;
1129 gimple_call call;
1130 edge e;
1131 edge_iterator ei;
1132 tree retval = NULL, real_retval = NULL;
1133 bool split_part_return_p = false;
1134 gimple last_stmt = NULL;
1135 unsigned int i;
1136 tree arg, ddef;
1137 vec<tree, va_gc> **debug_args = NULL;
1139 if (dump_file)
1141 fprintf (dump_file, "\n\nSplitting function at:\n");
1142 dump_split_point (dump_file, split_point);
1145 if (cur_node->local.can_change_signature)
1146 args_to_skip = BITMAP_ALLOC (NULL);
1147 else
1148 args_to_skip = NULL;
1150 /* Collect the parameters of new function and args_to_skip bitmap. */
1151 for (parm = DECL_ARGUMENTS (current_function_decl);
1152 parm; parm = DECL_CHAIN (parm), num++)
1153 if (args_to_skip
1154 && (!is_gimple_reg (parm)
1155 || (ddef = ssa_default_def (cfun, parm)) == NULL_TREE
1156 || !bitmap_bit_p (split_point->ssa_names_to_pass,
1157 SSA_NAME_VERSION (ddef))))
1158 bitmap_set_bit (args_to_skip, num);
1159 else
1161 /* This parm might not have been used up to now, but is going to be
1162 used, hence register it. */
1163 if (is_gimple_reg (parm))
1164 arg = get_or_create_ssa_default_def (cfun, parm);
1165 else
1166 arg = parm;
1168 if (!useless_type_conversion_p (DECL_ARG_TYPE (parm), TREE_TYPE (arg)))
1169 arg = fold_convert (DECL_ARG_TYPE (parm), arg);
1170 args_to_pass.safe_push (arg);
1173 /* See if the split function will return. */
1174 FOR_EACH_EDGE (e, ei, return_bb->preds)
1175 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1176 break;
1177 if (e)
1178 split_part_return_p = true;
1180 /* Add return block to what will become the split function.
1181 We do not return; no return block is needed. */
1182 if (!split_part_return_p)
1184 /* We have no return block, so nothing is needed. */
1185 else if (return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1187 /* When we do not want to return value, we need to construct
1188 new return block with empty return statement.
1189 FIXME: Once we are able to change return type, we should change function
1190 to return void instead of just outputting function with undefined return
1191 value. For structures this affects quality of codegen. */
1192 else if (!split_point->split_part_set_retval
1193 && find_retval (return_bb))
1195 bool redirected = true;
1196 basic_block new_return_bb = create_basic_block (NULL, 0, return_bb);
1197 gimple_stmt_iterator gsi = gsi_start_bb (new_return_bb);
1198 gsi_insert_after (&gsi, gimple_build_return (NULL), GSI_NEW_STMT);
1199 while (redirected)
1201 redirected = false;
1202 FOR_EACH_EDGE (e, ei, return_bb->preds)
1203 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1205 new_return_bb->count += e->count;
1206 new_return_bb->frequency += EDGE_FREQUENCY (e);
1207 redirect_edge_and_branch (e, new_return_bb);
1208 redirected = true;
1209 break;
1212 e = make_edge (new_return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1213 e->probability = REG_BR_PROB_BASE;
1214 e->count = new_return_bb->count;
1215 add_bb_to_loop (new_return_bb, current_loops->tree_root);
1216 bitmap_set_bit (split_point->split_bbs, new_return_bb->index);
1218 /* When we pass around the value, use existing return block. */
1219 else
1220 bitmap_set_bit (split_point->split_bbs, return_bb->index);
1222 /* If RETURN_BB has virtual operand PHIs, they must be removed and the
1223 virtual operand marked for renaming as we change the CFG in a way that
1224 tree-inline is not able to compensate for.
1226 Note this can happen whether or not we have a return value. If we have
1227 a return value, then RETURN_BB may have PHIs for real operands too. */
1228 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1230 bool phi_p = false;
1231 for (gimple_phi_iterator gsi = gsi_start_phis (return_bb);
1232 !gsi_end_p (gsi);)
1234 gimple_phi stmt = gsi.phi ();
1235 if (!virtual_operand_p (gimple_phi_result (stmt)))
1237 gsi_next (&gsi);
1238 continue;
1240 mark_virtual_phi_result_for_renaming (stmt);
1241 remove_phi_node (&gsi, true);
1242 phi_p = true;
1244 /* In reality we have to rename the reaching definition of the
1245 virtual operand at return_bb as we will eventually release it
1246 when we remove the code region we outlined.
1247 So we have to rename all immediate virtual uses of that region
1248 if we didn't see a PHI definition yet. */
1249 /* ??? In real reality we want to set the reaching vdef of the
1250 entry of the SESE region as the vuse of the call and the reaching
1251 vdef of the exit of the SESE region as the vdef of the call. */
1252 if (!phi_p)
1253 for (gimple_stmt_iterator gsi = gsi_start_bb (return_bb);
1254 !gsi_end_p (gsi);
1255 gsi_next (&gsi))
1257 gimple stmt = gsi_stmt (gsi);
1258 if (gimple_vuse (stmt))
1260 gimple_set_vuse (stmt, NULL_TREE);
1261 update_stmt (stmt);
1263 if (gimple_vdef (stmt))
1264 break;
1268 /* Now create the actual clone. */
1269 cgraph_edge::rebuild_edges ();
1270 node = cur_node->create_version_clone_with_body
1271 (vNULL, NULL, args_to_skip, !split_part_return_p, split_point->split_bbs,
1272 split_point->entry_bb, "part");
1274 /* Let's take a time profile for splitted function. */
1275 node->tp_first_run = cur_node->tp_first_run + 1;
1277 /* For usual cloning it is enough to clear builtin only when signature
1278 changes. For partial inlining we however can not expect the part
1279 of builtin implementation to have same semantic as the whole. */
1280 if (DECL_BUILT_IN (node->decl))
1282 DECL_BUILT_IN_CLASS (node->decl) = NOT_BUILT_IN;
1283 DECL_FUNCTION_CODE (node->decl) = (enum built_in_function) 0;
1285 /* If the original function is declared inline, there is no point in issuing
1286 a warning for the non-inlinable part. */
1287 DECL_NO_INLINE_WARNING_P (node->decl) = 1;
1288 cur_node->remove_callees ();
1289 cur_node->remove_all_references ();
1290 if (!split_part_return_p)
1291 TREE_THIS_VOLATILE (node->decl) = 1;
1292 if (dump_file)
1293 dump_function_to_file (node->decl, dump_file, dump_flags);
1295 /* Create the basic block we place call into. It is the entry basic block
1296 split after last label. */
1297 call_bb = split_point->entry_bb;
1298 for (gimple_stmt_iterator gsi = gsi_start_bb (call_bb); !gsi_end_p (gsi);)
1299 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
1301 last_stmt = gsi_stmt (gsi);
1302 gsi_next (&gsi);
1304 else
1305 break;
1306 e = split_block (split_point->entry_bb, last_stmt);
1307 remove_edge (e);
1309 /* Produce the call statement. */
1310 gimple_stmt_iterator gsi = gsi_last_bb (call_bb);
1311 FOR_EACH_VEC_ELT (args_to_pass, i, arg)
1312 if (!is_gimple_val (arg))
1314 arg = force_gimple_operand_gsi (&gsi, arg, true, NULL_TREE,
1315 false, GSI_CONTINUE_LINKING);
1316 args_to_pass[i] = arg;
1318 call = gimple_build_call_vec (node->decl, args_to_pass);
1319 gimple_set_block (call, DECL_INITIAL (current_function_decl));
1320 args_to_pass.release ();
1322 /* For optimized away parameters, add on the caller side
1323 before the call
1324 DEBUG D#X => parm_Y(D)
1325 stmts and associate D#X with parm in decl_debug_args_lookup
1326 vector to say for debug info that if parameter parm had been passed,
1327 it would have value parm_Y(D). */
1328 if (args_to_skip)
1329 for (parm = DECL_ARGUMENTS (current_function_decl), num = 0;
1330 parm; parm = DECL_CHAIN (parm), num++)
1331 if (bitmap_bit_p (args_to_skip, num)
1332 && is_gimple_reg (parm))
1334 tree ddecl;
1335 gimple def_temp;
1337 /* This needs to be done even without MAY_HAVE_DEBUG_STMTS,
1338 otherwise if it didn't exist before, we'd end up with
1339 different SSA_NAME_VERSIONs between -g and -g0. */
1340 arg = get_or_create_ssa_default_def (cfun, parm);
1341 if (!MAY_HAVE_DEBUG_STMTS)
1342 continue;
1344 if (debug_args == NULL)
1345 debug_args = decl_debug_args_insert (node->decl);
1346 ddecl = make_node (DEBUG_EXPR_DECL);
1347 DECL_ARTIFICIAL (ddecl) = 1;
1348 TREE_TYPE (ddecl) = TREE_TYPE (parm);
1349 DECL_MODE (ddecl) = DECL_MODE (parm);
1350 vec_safe_push (*debug_args, DECL_ORIGIN (parm));
1351 vec_safe_push (*debug_args, ddecl);
1352 def_temp = gimple_build_debug_bind (ddecl, unshare_expr (arg),
1353 call);
1354 gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
1356 /* And on the callee side, add
1357 DEBUG D#Y s=> parm
1358 DEBUG var => D#Y
1359 stmts to the first bb where var is a VAR_DECL created for the
1360 optimized away parameter in DECL_INITIAL block. This hints
1361 in the debug info that var (whole DECL_ORIGIN is the parm PARM_DECL)
1362 is optimized away, but could be looked up at the call site
1363 as value of D#X there. */
1364 if (debug_args != NULL)
1366 unsigned int i;
1367 tree var, vexpr;
1368 gimple_stmt_iterator cgsi;
1369 gimple def_temp;
1371 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1372 var = BLOCK_VARS (DECL_INITIAL (node->decl));
1373 i = vec_safe_length (*debug_args);
1374 cgsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1377 i -= 2;
1378 while (var != NULL_TREE
1379 && DECL_ABSTRACT_ORIGIN (var) != (**debug_args)[i])
1380 var = TREE_CHAIN (var);
1381 if (var == NULL_TREE)
1382 break;
1383 vexpr = make_node (DEBUG_EXPR_DECL);
1384 parm = (**debug_args)[i];
1385 DECL_ARTIFICIAL (vexpr) = 1;
1386 TREE_TYPE (vexpr) = TREE_TYPE (parm);
1387 DECL_MODE (vexpr) = DECL_MODE (parm);
1388 def_temp = gimple_build_debug_source_bind (vexpr, parm,
1389 NULL);
1390 gsi_insert_before (&cgsi, def_temp, GSI_SAME_STMT);
1391 def_temp = gimple_build_debug_bind (var, vexpr, NULL);
1392 gsi_insert_before (&cgsi, def_temp, GSI_SAME_STMT);
1394 while (i);
1395 pop_cfun ();
1398 /* We avoid address being taken on any variable used by split part,
1399 so return slot optimization is always possible. Moreover this is
1400 required to make DECL_BY_REFERENCE work. */
1401 if (aggregate_value_p (DECL_RESULT (current_function_decl),
1402 TREE_TYPE (current_function_decl))
1403 && (!is_gimple_reg_type (TREE_TYPE (DECL_RESULT (current_function_decl)))
1404 || DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))))
1405 gimple_call_set_return_slot_opt (call, true);
1407 /* Update return value. This is bit tricky. When we do not return,
1408 do nothing. When we return we might need to update return_bb
1409 or produce a new return statement. */
1410 if (!split_part_return_p)
1411 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1412 else
1414 e = make_edge (call_bb, return_bb,
1415 return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
1416 ? 0 : EDGE_FALLTHRU);
1417 e->count = call_bb->count;
1418 e->probability = REG_BR_PROB_BASE;
1420 /* If there is return basic block, see what value we need to store
1421 return value into and put call just before it. */
1422 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1424 real_retval = retval = find_retval (return_bb);
1426 if (real_retval && split_point->split_part_set_retval)
1428 gimple_phi_iterator psi;
1430 /* See if we need new SSA_NAME for the result.
1431 When DECL_BY_REFERENCE is true, retval is actually pointer to
1432 return value and it is constant in whole function. */
1433 if (TREE_CODE (retval) == SSA_NAME
1434 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1436 retval = copy_ssa_name (retval, call);
1438 /* See if there is PHI defining return value. */
1439 for (psi = gsi_start_phis (return_bb);
1440 !gsi_end_p (psi); gsi_next (&psi))
1441 if (!virtual_operand_p (gimple_phi_result (psi.phi ())))
1442 break;
1444 /* When there is PHI, just update its value. */
1445 if (TREE_CODE (retval) == SSA_NAME
1446 && !gsi_end_p (psi))
1447 add_phi_arg (psi.phi (), retval, e, UNKNOWN_LOCATION);
1448 /* Otherwise update the return BB itself.
1449 find_return_bb allows at most one assignment to return value,
1450 so update first statement. */
1451 else
1453 gimple_stmt_iterator bsi;
1454 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi);
1455 gsi_next (&bsi))
1456 if (gimple_return return_stmt =
1457 dyn_cast <gimple_return> (gsi_stmt (bsi)))
1459 gimple_return_set_retval (return_stmt, retval);
1460 break;
1462 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
1463 && !gimple_clobber_p (gsi_stmt (bsi)))
1465 gimple_assign_set_rhs1 (gsi_stmt (bsi), retval);
1466 break;
1468 update_stmt (gsi_stmt (bsi));
1471 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1473 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1474 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1476 else
1478 tree restype;
1479 restype = TREE_TYPE (DECL_RESULT (current_function_decl));
1480 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1481 if (!useless_type_conversion_p (TREE_TYPE (retval), restype))
1483 gimple cpy;
1484 tree tem = create_tmp_reg (restype, NULL);
1485 tem = make_ssa_name (tem, call);
1486 cpy = gimple_build_assign_with_ops (NOP_EXPR, retval,
1487 tem, NULL_TREE);
1488 gsi_insert_after (&gsi, cpy, GSI_NEW_STMT);
1489 retval = tem;
1491 gimple_call_set_lhs (call, retval);
1492 update_stmt (call);
1495 else
1496 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1498 /* We don't use return block (there is either no return in function or
1499 multiple of them). So create new basic block with return statement.
1501 else
1503 gimple_return ret;
1504 if (split_point->split_part_set_retval
1505 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1507 retval = DECL_RESULT (current_function_decl);
1509 /* We use temporary register to hold value when aggregate_value_p
1510 is false. Similarly for DECL_BY_REFERENCE we must avoid extra
1511 copy. */
1512 if (!aggregate_value_p (retval, TREE_TYPE (current_function_decl))
1513 && !DECL_BY_REFERENCE (retval))
1514 retval = create_tmp_reg (TREE_TYPE (retval), NULL);
1515 if (is_gimple_reg (retval))
1517 /* When returning by reference, there is only one SSA name
1518 assigned to RESULT_DECL (that is pointer to return value).
1519 Look it up or create new one if it is missing. */
1520 if (DECL_BY_REFERENCE (retval))
1521 retval = get_or_create_ssa_default_def (cfun, retval);
1522 /* Otherwise produce new SSA name for return value. */
1523 else
1524 retval = make_ssa_name (retval, call);
1526 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1527 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1528 else
1529 gimple_call_set_lhs (call, retval);
1531 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1532 ret = gimple_build_return (retval);
1533 gsi_insert_after (&gsi, ret, GSI_NEW_STMT);
1536 free_dominance_info (CDI_DOMINATORS);
1537 free_dominance_info (CDI_POST_DOMINATORS);
1538 compute_inline_parameters (node, true);
1541 /* Execute function splitting pass. */
1543 static unsigned int
1544 execute_split_functions (void)
1546 gimple_stmt_iterator bsi;
1547 basic_block bb;
1548 int overall_time = 0, overall_size = 0;
1549 int todo = 0;
1550 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1552 if (flags_from_decl_or_type (current_function_decl)
1553 & (ECF_NORETURN|ECF_MALLOC))
1555 if (dump_file)
1556 fprintf (dump_file, "Not splitting: noreturn/malloc function.\n");
1557 return 0;
1559 if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
1561 if (dump_file)
1562 fprintf (dump_file, "Not splitting: main function.\n");
1563 return 0;
1565 /* This can be relaxed; function might become inlinable after splitting
1566 away the uninlinable part. */
1567 if (inline_edge_summary_vec.exists ()
1568 && !inline_summary (node)->inlinable)
1570 if (dump_file)
1571 fprintf (dump_file, "Not splitting: not inlinable.\n");
1572 return 0;
1574 if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1576 if (dump_file)
1577 fprintf (dump_file, "Not splitting: disregarding inline limits.\n");
1578 return 0;
1580 /* This can be relaxed; most of versioning tests actually prevents
1581 a duplication. */
1582 if (!tree_versionable_function_p (current_function_decl))
1584 if (dump_file)
1585 fprintf (dump_file, "Not splitting: not versionable.\n");
1586 return 0;
1588 /* FIXME: we could support this. */
1589 if (DECL_STRUCT_FUNCTION (current_function_decl)->static_chain_decl)
1591 if (dump_file)
1592 fprintf (dump_file, "Not splitting: nested function.\n");
1593 return 0;
1596 /* See if it makes sense to try to split.
1597 It makes sense to split if we inline, that is if we have direct calls to
1598 handle or direct calls are possibly going to appear as result of indirect
1599 inlining or LTO. Also handle -fprofile-generate as LTO to allow non-LTO
1600 training for LTO -fprofile-use build.
1602 Note that we are not completely conservative about disqualifying functions
1603 called once. It is possible that the caller is called more then once and
1604 then inlining would still benefit. */
1605 if ((!node->callers
1606 /* Local functions called once will be completely inlined most of time. */
1607 || (!node->callers->next_caller && node->local.local))
1608 && !node->address_taken
1609 && (!flag_lto || !node->externally_visible))
1611 if (dump_file)
1612 fprintf (dump_file, "Not splitting: not called directly "
1613 "or called once.\n");
1614 return 0;
1617 /* FIXME: We can actually split if splitting reduces call overhead. */
1618 if (!flag_inline_small_functions
1619 && !DECL_DECLARED_INLINE_P (current_function_decl))
1621 if (dump_file)
1622 fprintf (dump_file, "Not splitting: not autoinlining and function"
1623 " is not inline.\n");
1624 return 0;
1627 /* We enforce splitting after loop headers when profile info is not
1628 available. */
1629 if (profile_status_for_fn (cfun) != PROFILE_READ)
1630 mark_dfs_back_edges ();
1632 /* Initialize bitmap to track forbidden calls. */
1633 forbidden_dominators = BITMAP_ALLOC (NULL);
1634 calculate_dominance_info (CDI_DOMINATORS);
1636 /* Compute local info about basic blocks and determine function size/time. */
1637 bb_info_vec.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
1638 memset (&best_split_point, 0, sizeof (best_split_point));
1639 FOR_EACH_BB_FN (bb, cfun)
1641 int time = 0;
1642 int size = 0;
1643 int freq = compute_call_stmt_bb_frequency (current_function_decl, bb);
1645 if (dump_file && (dump_flags & TDF_DETAILS))
1646 fprintf (dump_file, "Basic block %i\n", bb->index);
1648 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1650 int this_time, this_size;
1651 gimple stmt = gsi_stmt (bsi);
1653 this_size = estimate_num_insns (stmt, &eni_size_weights);
1654 this_time = estimate_num_insns (stmt, &eni_time_weights) * freq;
1655 size += this_size;
1656 time += this_time;
1657 check_forbidden_calls (stmt);
1659 if (dump_file && (dump_flags & TDF_DETAILS))
1661 fprintf (dump_file, " freq:%6i size:%3i time:%3i ",
1662 freq, this_size, this_time);
1663 print_gimple_stmt (dump_file, stmt, 0, 0);
1666 overall_time += time;
1667 overall_size += size;
1668 bb_info_vec[bb->index].time = time;
1669 bb_info_vec[bb->index].size = size;
1671 find_split_points (overall_time, overall_size);
1672 if (best_split_point.split_bbs)
1674 split_function (&best_split_point);
1675 BITMAP_FREE (best_split_point.ssa_names_to_pass);
1676 BITMAP_FREE (best_split_point.split_bbs);
1677 todo = TODO_update_ssa | TODO_cleanup_cfg;
1679 BITMAP_FREE (forbidden_dominators);
1680 bb_info_vec.release ();
1681 return todo;
1684 namespace {
1686 const pass_data pass_data_split_functions =
1688 GIMPLE_PASS, /* type */
1689 "fnsplit", /* name */
1690 OPTGROUP_NONE, /* optinfo_flags */
1691 TV_IPA_FNSPLIT, /* tv_id */
1692 PROP_cfg, /* properties_required */
1693 0, /* properties_provided */
1694 0, /* properties_destroyed */
1695 0, /* todo_flags_start */
1696 0, /* todo_flags_finish */
1699 class pass_split_functions : public gimple_opt_pass
1701 public:
1702 pass_split_functions (gcc::context *ctxt)
1703 : gimple_opt_pass (pass_data_split_functions, ctxt)
1706 /* opt_pass methods: */
1707 virtual bool gate (function *);
1708 virtual unsigned int execute (function *)
1710 return execute_split_functions ();
1713 }; // class pass_split_functions
1715 bool
1716 pass_split_functions::gate (function *)
1718 /* When doing profile feedback, we want to execute the pass after profiling
1719 is read. So disable one in early optimization. */
1720 return (flag_partial_inlining
1721 && !profile_arc_flag && !flag_branch_probabilities);
1724 } // anon namespace
1726 gimple_opt_pass *
1727 make_pass_split_functions (gcc::context *ctxt)
1729 return new pass_split_functions (ctxt);
1732 /* Execute function splitting pass. */
1734 static unsigned int
1735 execute_feedback_split_functions (void)
1737 unsigned int retval = execute_split_functions ();
1738 if (retval)
1739 retval |= TODO_rebuild_cgraph_edges;
1740 return retval;
1743 namespace {
1745 const pass_data pass_data_feedback_split_functions =
1747 GIMPLE_PASS, /* type */
1748 "feedback_fnsplit", /* name */
1749 OPTGROUP_NONE, /* optinfo_flags */
1750 TV_IPA_FNSPLIT, /* tv_id */
1751 PROP_cfg, /* properties_required */
1752 0, /* properties_provided */
1753 0, /* properties_destroyed */
1754 0, /* todo_flags_start */
1755 0, /* todo_flags_finish */
1758 class pass_feedback_split_functions : public gimple_opt_pass
1760 public:
1761 pass_feedback_split_functions (gcc::context *ctxt)
1762 : gimple_opt_pass (pass_data_feedback_split_functions, ctxt)
1765 /* opt_pass methods: */
1766 virtual bool gate (function *);
1767 virtual unsigned int execute (function *)
1769 return execute_feedback_split_functions ();
1772 }; // class pass_feedback_split_functions
1774 bool
1775 pass_feedback_split_functions::gate (function *)
1777 /* We don't need to split when profiling at all, we are producing
1778 lousy code anyway. */
1779 return (flag_partial_inlining
1780 && flag_branch_probabilities);
1783 } // anon namespace
1785 gimple_opt_pass *
1786 make_pass_feedback_split_functions (gcc::context *ctxt)
1788 return new pass_feedback_split_functions (ctxt);