Fix bootstrap failure.
[official-gcc.git] / gcc / ipa-split.c
blobac68c87f2823adf963b22db4a6afb69c84fd39ae
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 "predict.h"
82 #include "vec.h"
83 #include "hashtab.h"
84 #include "hash-set.h"
85 #include "machmode.h"
86 #include "tm.h"
87 #include "hard-reg-set.h"
88 #include "input.h"
89 #include "function.h"
90 #include "dominance.h"
91 #include "cfg.h"
92 #include "cfganal.h"
93 #include "basic-block.h"
94 #include "tree-ssa-alias.h"
95 #include "internal-fn.h"
96 #include "gimple-expr.h"
97 #include "is-a.h"
98 #include "gimple.h"
99 #include "stringpool.h"
100 #include "expr.h"
101 #include "calls.h"
102 #include "gimplify.h"
103 #include "gimple-iterator.h"
104 #include "gimplify-me.h"
105 #include "gimple-walk.h"
106 #include "target.h"
107 #include "hash-map.h"
108 #include "plugin-api.h"
109 #include "ipa-ref.h"
110 #include "cgraph.h"
111 #include "alloc-pool.h"
112 #include "ipa-prop.h"
113 #include "gimple-ssa.h"
114 #include "tree-cfg.h"
115 #include "tree-phinodes.h"
116 #include "ssa-iterators.h"
117 #include "stringpool.h"
118 #include "tree-ssanames.h"
119 #include "tree-into-ssa.h"
120 #include "tree-dfa.h"
121 #include "tree-pass.h"
122 #include "flags.h"
123 #include "diagnostic.h"
124 #include "tree-dump.h"
125 #include "tree-inline.h"
126 #include "params.h"
127 #include "gimple-pretty-print.h"
128 #include "ipa-inline.h"
129 #include "cfgloop.h"
131 /* Per basic block info. */
133 typedef struct
135 unsigned int size;
136 unsigned int time;
137 } split_bb_info;
139 static vec<split_bb_info> bb_info_vec;
141 /* Description of split point. */
143 struct split_point
145 /* Size of the partitions. */
146 unsigned int header_time, header_size, split_time, split_size;
148 /* SSA names that need to be passed into spit function. */
149 bitmap ssa_names_to_pass;
151 /* Basic block where we split (that will become entry point of new function. */
152 basic_block entry_bb;
154 /* Basic blocks we are splitting away. */
155 bitmap split_bbs;
157 /* True when return value is computed on split part and thus it needs
158 to be returned. */
159 bool split_part_set_retval;
162 /* Best split point found. */
164 struct split_point best_split_point;
166 /* Set of basic blocks that are not allowed to dominate a split point. */
168 static bitmap forbidden_dominators;
170 static tree find_retval (basic_block return_bb);
172 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
173 variable, check it if it is present in bitmap passed via DATA. */
175 static bool
176 test_nonssa_use (gimple, tree t, tree, void *data)
178 t = get_base_address (t);
180 if (!t || is_gimple_reg (t))
181 return false;
183 if (TREE_CODE (t) == PARM_DECL
184 || (TREE_CODE (t) == VAR_DECL
185 && auto_var_in_fn_p (t, current_function_decl))
186 || TREE_CODE (t) == RESULT_DECL
187 /* Normal labels are part of CFG and will be handled gratefuly.
188 Forced labels however can be used directly by statements and
189 need to stay in one partition along with their uses. */
190 || (TREE_CODE (t) == LABEL_DECL
191 && FORCED_LABEL (t)))
192 return bitmap_bit_p ((bitmap)data, DECL_UID (t));
194 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
195 to pretend that the value pointed to is actual result decl. */
196 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
197 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
198 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
199 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
200 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
201 return
202 bitmap_bit_p ((bitmap)data,
203 DECL_UID (DECL_RESULT (current_function_decl)));
205 return false;
208 /* Dump split point CURRENT. */
210 static void
211 dump_split_point (FILE * file, struct split_point *current)
213 fprintf (file,
214 "Split point at BB %i\n"
215 " header time: %i header size: %i\n"
216 " split time: %i split size: %i\n bbs: ",
217 current->entry_bb->index, current->header_time,
218 current->header_size, current->split_time, current->split_size);
219 dump_bitmap (file, current->split_bbs);
220 fprintf (file, " SSA names to pass: ");
221 dump_bitmap (file, current->ssa_names_to_pass);
224 /* Look for all BBs in header that might lead to the split part and verify
225 that they are not defining any non-SSA var used by the split part.
226 Parameters are the same as for consider_split. */
228 static bool
229 verify_non_ssa_vars (struct split_point *current, bitmap non_ssa_vars,
230 basic_block return_bb)
232 bitmap seen = BITMAP_ALLOC (NULL);
233 vec<basic_block> worklist = vNULL;
234 edge e;
235 edge_iterator ei;
236 bool ok = true;
237 basic_block bb;
239 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
240 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
241 && !bitmap_bit_p (current->split_bbs, e->src->index))
243 worklist.safe_push (e->src);
244 bitmap_set_bit (seen, e->src->index);
247 while (!worklist.is_empty ())
249 gimple_stmt_iterator bsi;
251 bb = worklist.pop ();
252 FOR_EACH_EDGE (e, ei, bb->preds)
253 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
254 && bitmap_set_bit (seen, e->src->index))
256 gcc_checking_assert (!bitmap_bit_p (current->split_bbs,
257 e->src->index));
258 worklist.safe_push (e->src);
260 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
262 gimple stmt = gsi_stmt (bsi);
263 if (is_gimple_debug (stmt))
264 continue;
265 if (walk_stmt_load_store_addr_ops
266 (stmt, non_ssa_vars, test_nonssa_use, test_nonssa_use,
267 test_nonssa_use))
269 ok = false;
270 goto done;
272 if (gimple_code (stmt) == GIMPLE_LABEL
273 && test_nonssa_use (stmt, gimple_label_label (stmt),
274 NULL_TREE, non_ssa_vars))
276 ok = false;
277 goto done;
280 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
282 if (walk_stmt_load_store_addr_ops
283 (gsi_stmt (bsi), non_ssa_vars, test_nonssa_use, test_nonssa_use,
284 test_nonssa_use))
286 ok = false;
287 goto done;
290 FOR_EACH_EDGE (e, ei, bb->succs)
292 if (e->dest != return_bb)
293 continue;
294 for (bsi = gsi_start_phis (return_bb); !gsi_end_p (bsi);
295 gsi_next (&bsi))
297 gimple stmt = gsi_stmt (bsi);
298 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
300 if (virtual_operand_p (gimple_phi_result (stmt)))
301 continue;
302 if (TREE_CODE (op) != SSA_NAME
303 && test_nonssa_use (stmt, op, op, non_ssa_vars))
305 ok = false;
306 goto done;
312 /* Verify that the rest of function does not define any label
313 used by the split part. */
314 FOR_EACH_BB_FN (bb, cfun)
315 if (!bitmap_bit_p (current->split_bbs, bb->index)
316 && !bitmap_bit_p (seen, bb->index))
318 gimple_stmt_iterator bsi;
319 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
320 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_LABEL
321 && test_nonssa_use (gsi_stmt (bsi),
322 gimple_label_label (gsi_stmt (bsi)),
323 NULL_TREE, non_ssa_vars))
325 ok = false;
326 goto done;
328 else if (gimple_code (gsi_stmt (bsi)) != GIMPLE_LABEL)
329 break;
332 done:
333 BITMAP_FREE (seen);
334 worklist.release ();
335 return ok;
338 /* If STMT is a call, check the callee against a list of forbidden
339 predicate functions. If a match is found, look for uses of the
340 call result in condition statements that compare against zero.
341 For each such use, find the block targeted by the condition
342 statement for the nonzero result, and set the bit for this block
343 in the forbidden dominators bitmap. The purpose of this is to avoid
344 selecting a split point where we are likely to lose the chance
345 to optimize away an unused function call. */
347 static void
348 check_forbidden_calls (gimple stmt)
350 imm_use_iterator use_iter;
351 use_operand_p use_p;
352 tree lhs;
354 /* At the moment, __builtin_constant_p is the only forbidden
355 predicate function call (see PR49642). */
356 if (!gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P))
357 return;
359 lhs = gimple_call_lhs (stmt);
361 if (!lhs || TREE_CODE (lhs) != SSA_NAME)
362 return;
364 FOR_EACH_IMM_USE_FAST (use_p, use_iter, lhs)
366 tree op1;
367 basic_block use_bb, forbidden_bb;
368 enum tree_code code;
369 edge true_edge, false_edge;
370 gimple use_stmt = USE_STMT (use_p);
372 if (gimple_code (use_stmt) != GIMPLE_COND)
373 continue;
375 /* Assuming canonical form for GIMPLE_COND here, with constant
376 in second position. */
377 op1 = gimple_cond_rhs (use_stmt);
378 code = gimple_cond_code (use_stmt);
379 use_bb = gimple_bb (use_stmt);
381 extract_true_false_edges_from_block (use_bb, &true_edge, &false_edge);
383 /* We're only interested in comparisons that distinguish
384 unambiguously from zero. */
385 if (!integer_zerop (op1) || code == LE_EXPR || code == GE_EXPR)
386 continue;
388 if (code == EQ_EXPR)
389 forbidden_bb = false_edge->dest;
390 else
391 forbidden_bb = true_edge->dest;
393 bitmap_set_bit (forbidden_dominators, forbidden_bb->index);
397 /* If BB is dominated by any block in the forbidden dominators set,
398 return TRUE; else FALSE. */
400 static bool
401 dominated_by_forbidden (basic_block bb)
403 unsigned dom_bb;
404 bitmap_iterator bi;
406 EXECUTE_IF_SET_IN_BITMAP (forbidden_dominators, 1, dom_bb, bi)
408 if (dominated_by_p (CDI_DOMINATORS, bb,
409 BASIC_BLOCK_FOR_FN (cfun, dom_bb)))
410 return true;
413 return false;
416 /* We found an split_point CURRENT. NON_SSA_VARS is bitmap of all non ssa
417 variables used and RETURN_BB is return basic block.
418 See if we can split function here. */
420 static void
421 consider_split (struct split_point *current, bitmap non_ssa_vars,
422 basic_block return_bb)
424 tree parm;
425 unsigned int num_args = 0;
426 unsigned int call_overhead;
427 edge e;
428 edge_iterator ei;
429 gimple_stmt_iterator bsi;
430 unsigned int i;
431 int incoming_freq = 0;
432 tree retval;
433 bool back_edge = false;
435 if (dump_file && (dump_flags & TDF_DETAILS))
436 dump_split_point (dump_file, current);
438 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
440 if (e->flags & EDGE_DFS_BACK)
441 back_edge = true;
442 if (!bitmap_bit_p (current->split_bbs, e->src->index))
443 incoming_freq += EDGE_FREQUENCY (e);
446 /* Do not split when we would end up calling function anyway. */
447 if (incoming_freq
448 >= (ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency
449 * PARAM_VALUE (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY) / 100))
451 /* When profile is guessed, we can not expect it to give us
452 realistic estimate on likelyness of function taking the
453 complex path. As a special case, when tail of the function is
454 a loop, enable splitting since inlining code skipping the loop
455 is likely noticeable win. */
456 if (back_edge
457 && profile_status_for_fn (cfun) != PROFILE_READ
458 && incoming_freq < ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency)
460 if (dump_file && (dump_flags & TDF_DETAILS))
461 fprintf (dump_file,
462 " Split before loop, accepting despite low frequencies %i %i.\n",
463 incoming_freq,
464 ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency);
466 else
468 if (dump_file && (dump_flags & TDF_DETAILS))
469 fprintf (dump_file,
470 " Refused: incoming frequency is too large.\n");
471 return;
475 if (!current->header_size)
477 if (dump_file && (dump_flags & TDF_DETAILS))
478 fprintf (dump_file, " Refused: header empty\n");
479 return;
482 /* Verify that PHI args on entry are either virtual or all their operands
483 incoming from header are the same. */
484 for (bsi = gsi_start_phis (current->entry_bb); !gsi_end_p (bsi); gsi_next (&bsi))
486 gimple stmt = gsi_stmt (bsi);
487 tree val = NULL;
489 if (virtual_operand_p (gimple_phi_result (stmt)))
490 continue;
491 for (i = 0; i < gimple_phi_num_args (stmt); i++)
493 edge e = gimple_phi_arg_edge (stmt, i);
494 if (!bitmap_bit_p (current->split_bbs, e->src->index))
496 tree edge_val = gimple_phi_arg_def (stmt, i);
497 if (val && edge_val != val)
499 if (dump_file && (dump_flags & TDF_DETAILS))
500 fprintf (dump_file,
501 " Refused: entry BB has PHI with multiple variants\n");
502 return;
504 val = edge_val;
510 /* See what argument we will pass to the split function and compute
511 call overhead. */
512 call_overhead = eni_size_weights.call_cost;
513 for (parm = DECL_ARGUMENTS (current_function_decl); parm;
514 parm = DECL_CHAIN (parm))
516 if (!is_gimple_reg (parm))
518 if (bitmap_bit_p (non_ssa_vars, DECL_UID (parm)))
520 if (dump_file && (dump_flags & TDF_DETAILS))
521 fprintf (dump_file,
522 " Refused: need to pass non-ssa param values\n");
523 return;
526 else
528 tree ddef = ssa_default_def (cfun, parm);
529 if (ddef
530 && bitmap_bit_p (current->ssa_names_to_pass,
531 SSA_NAME_VERSION (ddef)))
533 if (!VOID_TYPE_P (TREE_TYPE (parm)))
534 call_overhead += estimate_move_cost (TREE_TYPE (parm), false);
535 num_args++;
539 if (!VOID_TYPE_P (TREE_TYPE (current_function_decl)))
540 call_overhead += estimate_move_cost (TREE_TYPE (current_function_decl),
541 false);
543 if (current->split_size <= call_overhead)
545 if (dump_file && (dump_flags & TDF_DETAILS))
546 fprintf (dump_file,
547 " Refused: split size is smaller than call overhead\n");
548 return;
550 if (current->header_size + call_overhead
551 >= (unsigned int)(DECL_DECLARED_INLINE_P (current_function_decl)
552 ? MAX_INLINE_INSNS_SINGLE
553 : MAX_INLINE_INSNS_AUTO))
555 if (dump_file && (dump_flags & TDF_DETAILS))
556 fprintf (dump_file,
557 " Refused: header size is too large for inline candidate\n");
558 return;
561 /* FIXME: we currently can pass only SSA function parameters to the split
562 arguments. Once parm_adjustment infrastructure is supported by cloning,
563 we can pass more than that. */
564 if (num_args != bitmap_count_bits (current->ssa_names_to_pass))
567 if (dump_file && (dump_flags & TDF_DETAILS))
568 fprintf (dump_file,
569 " Refused: need to pass non-param values\n");
570 return;
573 /* When there are non-ssa vars used in the split region, see if they
574 are used in the header region. If so, reject the split.
575 FIXME: we can use nested function support to access both. */
576 if (!bitmap_empty_p (non_ssa_vars)
577 && !verify_non_ssa_vars (current, non_ssa_vars, return_bb))
579 if (dump_file && (dump_flags & TDF_DETAILS))
580 fprintf (dump_file,
581 " Refused: split part has non-ssa uses\n");
582 return;
585 /* If the split point is dominated by a forbidden block, reject
586 the split. */
587 if (!bitmap_empty_p (forbidden_dominators)
588 && dominated_by_forbidden (current->entry_bb))
590 if (dump_file && (dump_flags & TDF_DETAILS))
591 fprintf (dump_file,
592 " Refused: split point dominated by forbidden block\n");
593 return;
596 /* See if retval used by return bb is computed by header or split part.
597 When it is computed by split part, we need to produce return statement
598 in the split part and add code to header to pass it around.
600 This is bit tricky to test:
601 1) When there is no return_bb or no return value, we always pass
602 value around.
603 2) Invariants are always computed by caller.
604 3) For SSA we need to look if defining statement is in header or split part
605 4) For non-SSA we need to look where the var is computed. */
606 retval = find_retval (return_bb);
607 if (!retval)
608 current->split_part_set_retval = true;
609 else if (is_gimple_min_invariant (retval))
610 current->split_part_set_retval = false;
611 /* Special case is value returned by reference we record as if it was non-ssa
612 set to result_decl. */
613 else if (TREE_CODE (retval) == SSA_NAME
614 && SSA_NAME_VAR (retval)
615 && TREE_CODE (SSA_NAME_VAR (retval)) == RESULT_DECL
616 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
617 current->split_part_set_retval
618 = bitmap_bit_p (non_ssa_vars, DECL_UID (SSA_NAME_VAR (retval)));
619 else if (TREE_CODE (retval) == SSA_NAME)
620 current->split_part_set_retval
621 = (!SSA_NAME_IS_DEFAULT_DEF (retval)
622 && (bitmap_bit_p (current->split_bbs,
623 gimple_bb (SSA_NAME_DEF_STMT (retval))->index)
624 || gimple_bb (SSA_NAME_DEF_STMT (retval)) == return_bb));
625 else if (TREE_CODE (retval) == PARM_DECL)
626 current->split_part_set_retval = false;
627 else if (TREE_CODE (retval) == VAR_DECL
628 || TREE_CODE (retval) == RESULT_DECL)
629 current->split_part_set_retval
630 = bitmap_bit_p (non_ssa_vars, DECL_UID (retval));
631 else
632 current->split_part_set_retval = true;
634 /* split_function fixes up at most one PHI non-virtual PHI node in return_bb,
635 for the return value. If there are other PHIs, give up. */
636 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
638 gimple_stmt_iterator psi;
640 for (psi = gsi_start_phis (return_bb); !gsi_end_p (psi); gsi_next (&psi))
641 if (!virtual_operand_p (gimple_phi_result (gsi_stmt (psi)))
642 && !(retval
643 && current->split_part_set_retval
644 && TREE_CODE (retval) == SSA_NAME
645 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
646 && SSA_NAME_DEF_STMT (retval) == gsi_stmt (psi)))
648 if (dump_file && (dump_flags & TDF_DETAILS))
649 fprintf (dump_file,
650 " Refused: return bb has extra PHIs\n");
651 return;
655 if (dump_file && (dump_flags & TDF_DETAILS))
656 fprintf (dump_file, " Accepted!\n");
658 /* At the moment chose split point with lowest frequency and that leaves
659 out smallest size of header.
660 In future we might re-consider this heuristics. */
661 if (!best_split_point.split_bbs
662 || best_split_point.entry_bb->frequency > current->entry_bb->frequency
663 || (best_split_point.entry_bb->frequency == current->entry_bb->frequency
664 && best_split_point.split_size < current->split_size))
667 if (dump_file && (dump_flags & TDF_DETAILS))
668 fprintf (dump_file, " New best split point!\n");
669 if (best_split_point.ssa_names_to_pass)
671 BITMAP_FREE (best_split_point.ssa_names_to_pass);
672 BITMAP_FREE (best_split_point.split_bbs);
674 best_split_point = *current;
675 best_split_point.ssa_names_to_pass = BITMAP_ALLOC (NULL);
676 bitmap_copy (best_split_point.ssa_names_to_pass,
677 current->ssa_names_to_pass);
678 best_split_point.split_bbs = BITMAP_ALLOC (NULL);
679 bitmap_copy (best_split_point.split_bbs, current->split_bbs);
683 /* Return basic block containing RETURN statement. We allow basic blocks
684 of the form:
685 <retval> = tmp_var;
686 return <retval>
687 but return_bb can not be more complex than this.
688 If nothing is found, return the exit block.
690 When there are multiple RETURN statement, chose one with return value,
691 since that one is more likely shared by multiple code paths.
693 Return BB is special, because for function splitting it is the only
694 basic block that is duplicated in between header and split part of the
695 function.
697 TODO: We might support multiple return blocks. */
699 static basic_block
700 find_return_bb (void)
702 edge e;
703 basic_block return_bb = EXIT_BLOCK_PTR_FOR_FN (cfun);
704 gimple_stmt_iterator bsi;
705 bool found_return = false;
706 tree retval = NULL_TREE;
708 if (!single_pred_p (EXIT_BLOCK_PTR_FOR_FN (cfun)))
709 return return_bb;
711 e = single_pred_edge (EXIT_BLOCK_PTR_FOR_FN (cfun));
712 for (bsi = gsi_last_bb (e->src); !gsi_end_p (bsi); gsi_prev (&bsi))
714 gimple stmt = gsi_stmt (bsi);
715 if (gimple_code (stmt) == GIMPLE_LABEL
716 || is_gimple_debug (stmt)
717 || gimple_clobber_p (stmt))
719 else if (gimple_code (stmt) == GIMPLE_ASSIGN
720 && found_return
721 && gimple_assign_single_p (stmt)
722 && (auto_var_in_fn_p (gimple_assign_rhs1 (stmt),
723 current_function_decl)
724 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
725 && retval == gimple_assign_lhs (stmt))
727 else if (gimple_code (stmt) == GIMPLE_RETURN)
729 found_return = true;
730 retval = gimple_return_retval (stmt);
732 else
733 break;
735 if (gsi_end_p (bsi) && found_return)
736 return_bb = e->src;
738 return return_bb;
741 /* Given return basic block RETURN_BB, see where return value is really
742 stored. */
743 static tree
744 find_retval (basic_block return_bb)
746 gimple_stmt_iterator bsi;
747 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
748 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
749 return gimple_return_retval (gsi_stmt (bsi));
750 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
751 && !gimple_clobber_p (gsi_stmt (bsi)))
752 return gimple_assign_rhs1 (gsi_stmt (bsi));
753 return NULL;
756 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
757 variable, mark it as used in bitmap passed via DATA.
758 Return true when access to T prevents splitting the function. */
760 static bool
761 mark_nonssa_use (gimple, tree t, tree, void *data)
763 t = get_base_address (t);
765 if (!t || is_gimple_reg (t))
766 return false;
768 /* At present we can't pass non-SSA arguments to split function.
769 FIXME: this can be relaxed by passing references to arguments. */
770 if (TREE_CODE (t) == PARM_DECL)
772 if (dump_file && (dump_flags & TDF_DETAILS))
773 fprintf (dump_file,
774 "Cannot split: use of non-ssa function parameter.\n");
775 return true;
778 if ((TREE_CODE (t) == VAR_DECL
779 && auto_var_in_fn_p (t, current_function_decl))
780 || TREE_CODE (t) == RESULT_DECL
781 || (TREE_CODE (t) == LABEL_DECL
782 && FORCED_LABEL (t)))
783 bitmap_set_bit ((bitmap)data, DECL_UID (t));
785 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
786 to pretend that the value pointed to is actual result decl. */
787 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
788 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
789 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
790 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
791 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
792 return
793 bitmap_bit_p ((bitmap)data,
794 DECL_UID (DECL_RESULT (current_function_decl)));
796 return false;
799 /* Compute local properties of basic block BB we collect when looking for
800 split points. We look for ssa defs and store them in SET_SSA_NAMES,
801 for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic
802 vars stored in NON_SSA_VARS.
804 When BB has edge to RETURN_BB, collect uses in RETURN_BB too.
806 Return false when BB contains something that prevents it from being put into
807 split function. */
809 static bool
810 visit_bb (basic_block bb, basic_block return_bb,
811 bitmap set_ssa_names, bitmap used_ssa_names,
812 bitmap non_ssa_vars)
814 gimple_stmt_iterator bsi;
815 edge e;
816 edge_iterator ei;
817 bool can_split = true;
819 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
821 gimple stmt = gsi_stmt (bsi);
822 tree op;
823 ssa_op_iter iter;
824 tree decl;
826 if (is_gimple_debug (stmt))
827 continue;
829 if (gimple_clobber_p (stmt))
830 continue;
832 /* FIXME: We can split regions containing EH. We can not however
833 split RESX, EH_DISPATCH and EH_POINTER referring to same region
834 into different partitions. This would require tracking of
835 EH regions and checking in consider_split_point if they
836 are not used elsewhere. */
837 if (gimple_code (stmt) == GIMPLE_RESX)
839 if (dump_file && (dump_flags & TDF_DETAILS))
840 fprintf (dump_file, "Cannot split: resx.\n");
841 can_split = false;
843 if (gimple_code (stmt) == GIMPLE_EH_DISPATCH)
845 if (dump_file && (dump_flags & TDF_DETAILS))
846 fprintf (dump_file, "Cannot split: eh dispatch.\n");
847 can_split = false;
850 /* Check builtins that prevent splitting. */
851 if (gimple_code (stmt) == GIMPLE_CALL
852 && (decl = gimple_call_fndecl (stmt)) != NULL_TREE
853 && DECL_BUILT_IN (decl)
854 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
855 switch (DECL_FUNCTION_CODE (decl))
857 /* FIXME: once we will allow passing non-parm values to split part,
858 we need to be sure to handle correct builtin_stack_save and
859 builtin_stack_restore. At the moment we are safe; there is no
860 way to store builtin_stack_save result in non-SSA variable
861 since all calls to those are compiler generated. */
862 case BUILT_IN_APPLY:
863 case BUILT_IN_APPLY_ARGS:
864 case BUILT_IN_VA_START:
865 if (dump_file && (dump_flags & TDF_DETAILS))
866 fprintf (dump_file,
867 "Cannot split: builtin_apply and va_start.\n");
868 can_split = false;
869 break;
870 case BUILT_IN_EH_POINTER:
871 if (dump_file && (dump_flags & TDF_DETAILS))
872 fprintf (dump_file, "Cannot split: builtin_eh_pointer.\n");
873 can_split = false;
874 break;
875 default:
876 break;
879 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
880 bitmap_set_bit (set_ssa_names, SSA_NAME_VERSION (op));
881 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
882 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
883 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
884 mark_nonssa_use,
885 mark_nonssa_use,
886 mark_nonssa_use);
888 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
890 gimple stmt = gsi_stmt (bsi);
891 unsigned int i;
893 if (virtual_operand_p (gimple_phi_result (stmt)))
894 continue;
895 bitmap_set_bit (set_ssa_names,
896 SSA_NAME_VERSION (gimple_phi_result (stmt)));
897 for (i = 0; i < gimple_phi_num_args (stmt); i++)
899 tree op = gimple_phi_arg_def (stmt, i);
900 if (TREE_CODE (op) == SSA_NAME)
901 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
903 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
904 mark_nonssa_use,
905 mark_nonssa_use,
906 mark_nonssa_use);
908 /* Record also uses coming from PHI operand in return BB. */
909 FOR_EACH_EDGE (e, ei, bb->succs)
910 if (e->dest == return_bb)
912 for (bsi = gsi_start_phis (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
914 gimple stmt = gsi_stmt (bsi);
915 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
917 if (virtual_operand_p (gimple_phi_result (stmt)))
918 continue;
919 if (TREE_CODE (op) == SSA_NAME)
920 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
921 else
922 can_split &= !mark_nonssa_use (stmt, op, op, non_ssa_vars);
925 return can_split;
928 /* Stack entry for recursive DFS walk in find_split_point. */
930 typedef struct
932 /* Basic block we are examining. */
933 basic_block bb;
935 /* SSA names set and used by the BB and all BBs reachable
936 from it via DFS walk. */
937 bitmap set_ssa_names, used_ssa_names;
938 bitmap non_ssa_vars;
940 /* All BBS visited from this BB via DFS walk. */
941 bitmap bbs_visited;
943 /* Last examined edge in DFS walk. Since we walk unoriented graph,
944 the value is up to sum of incoming and outgoing edges of BB. */
945 unsigned int edge_num;
947 /* Stack entry index of earliest BB reachable from current BB
948 or any BB visited later in DFS walk. */
949 int earliest;
951 /* Overall time and size of all BBs reached from this BB in DFS walk. */
952 int overall_time, overall_size;
954 /* When false we can not split on this BB. */
955 bool can_split;
956 } stack_entry;
959 /* Find all articulations and call consider_split on them.
960 OVERALL_TIME and OVERALL_SIZE is time and size of the function.
962 We perform basic algorithm for finding an articulation in a graph
963 created from CFG by considering it to be an unoriented graph.
965 The articulation is discovered via DFS walk. We collect earliest
966 basic block on stack that is reachable via backward edge. Articulation
967 is any basic block such that there is no backward edge bypassing it.
968 To reduce stack usage we maintain heap allocated stack in STACK vector.
969 AUX pointer of BB is set to index it appears in the stack or -1 once
970 it is visited and popped off the stack.
972 The algorithm finds articulation after visiting the whole component
973 reachable by it. This makes it convenient to collect information about
974 the component used by consider_split. */
976 static void
977 find_split_points (int overall_time, int overall_size)
979 stack_entry first;
980 vec<stack_entry> stack = vNULL;
981 basic_block bb;
982 basic_block return_bb = find_return_bb ();
983 struct split_point current;
985 current.header_time = overall_time;
986 current.header_size = overall_size;
987 current.split_time = 0;
988 current.split_size = 0;
989 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
991 first.bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
992 first.edge_num = 0;
993 first.overall_time = 0;
994 first.overall_size = 0;
995 first.earliest = INT_MAX;
996 first.set_ssa_names = 0;
997 first.used_ssa_names = 0;
998 first.non_ssa_vars = 0;
999 first.bbs_visited = 0;
1000 first.can_split = false;
1001 stack.safe_push (first);
1002 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = (void *)(intptr_t)-1;
1004 while (!stack.is_empty ())
1006 stack_entry *entry = &stack.last ();
1008 /* We are walking an acyclic graph, so edge_num counts
1009 succ and pred edges together. However when considering
1010 articulation, we want to have processed everything reachable
1011 from articulation but nothing that reaches into it. */
1012 if (entry->edge_num == EDGE_COUNT (entry->bb->succs)
1013 && entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1015 int pos = stack.length ();
1016 entry->can_split &= visit_bb (entry->bb, return_bb,
1017 entry->set_ssa_names,
1018 entry->used_ssa_names,
1019 entry->non_ssa_vars);
1020 if (pos <= entry->earliest && !entry->can_split
1021 && dump_file && (dump_flags & TDF_DETAILS))
1022 fprintf (dump_file,
1023 "found articulation at bb %i but can not split\n",
1024 entry->bb->index);
1025 if (pos <= entry->earliest && entry->can_split)
1027 if (dump_file && (dump_flags & TDF_DETAILS))
1028 fprintf (dump_file, "found articulation at bb %i\n",
1029 entry->bb->index);
1030 current.entry_bb = entry->bb;
1031 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
1032 bitmap_and_compl (current.ssa_names_to_pass,
1033 entry->used_ssa_names, entry->set_ssa_names);
1034 current.header_time = overall_time - entry->overall_time;
1035 current.header_size = overall_size - entry->overall_size;
1036 current.split_time = entry->overall_time;
1037 current.split_size = entry->overall_size;
1038 current.split_bbs = entry->bbs_visited;
1039 consider_split (&current, entry->non_ssa_vars, return_bb);
1040 BITMAP_FREE (current.ssa_names_to_pass);
1043 /* Do actual DFS walk. */
1044 if (entry->edge_num
1045 < (EDGE_COUNT (entry->bb->succs)
1046 + EDGE_COUNT (entry->bb->preds)))
1048 edge e;
1049 basic_block dest;
1050 if (entry->edge_num < EDGE_COUNT (entry->bb->succs))
1052 e = EDGE_SUCC (entry->bb, entry->edge_num);
1053 dest = e->dest;
1055 else
1057 e = EDGE_PRED (entry->bb, entry->edge_num
1058 - EDGE_COUNT (entry->bb->succs));
1059 dest = e->src;
1062 entry->edge_num++;
1064 /* New BB to visit, push it to the stack. */
1065 if (dest != return_bb && dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
1066 && !dest->aux)
1068 stack_entry new_entry;
1070 new_entry.bb = dest;
1071 new_entry.edge_num = 0;
1072 new_entry.overall_time
1073 = bb_info_vec[dest->index].time;
1074 new_entry.overall_size
1075 = bb_info_vec[dest->index].size;
1076 new_entry.earliest = INT_MAX;
1077 new_entry.set_ssa_names = BITMAP_ALLOC (NULL);
1078 new_entry.used_ssa_names = BITMAP_ALLOC (NULL);
1079 new_entry.bbs_visited = BITMAP_ALLOC (NULL);
1080 new_entry.non_ssa_vars = BITMAP_ALLOC (NULL);
1081 new_entry.can_split = true;
1082 bitmap_set_bit (new_entry.bbs_visited, dest->index);
1083 stack.safe_push (new_entry);
1084 dest->aux = (void *)(intptr_t)stack.length ();
1086 /* Back edge found, record the earliest point. */
1087 else if ((intptr_t)dest->aux > 0
1088 && (intptr_t)dest->aux < entry->earliest)
1089 entry->earliest = (intptr_t)dest->aux;
1091 /* We are done with examining the edges. Pop off the value from stack
1092 and merge stuff we accumulate during the walk. */
1093 else if (entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1095 stack_entry *prev = &stack[stack.length () - 2];
1097 entry->bb->aux = (void *)(intptr_t)-1;
1098 prev->can_split &= entry->can_split;
1099 if (prev->set_ssa_names)
1101 bitmap_ior_into (prev->set_ssa_names, entry->set_ssa_names);
1102 bitmap_ior_into (prev->used_ssa_names, entry->used_ssa_names);
1103 bitmap_ior_into (prev->bbs_visited, entry->bbs_visited);
1104 bitmap_ior_into (prev->non_ssa_vars, entry->non_ssa_vars);
1106 if (prev->earliest > entry->earliest)
1107 prev->earliest = entry->earliest;
1108 prev->overall_time += entry->overall_time;
1109 prev->overall_size += entry->overall_size;
1110 BITMAP_FREE (entry->set_ssa_names);
1111 BITMAP_FREE (entry->used_ssa_names);
1112 BITMAP_FREE (entry->bbs_visited);
1113 BITMAP_FREE (entry->non_ssa_vars);
1114 stack.pop ();
1116 else
1117 stack.pop ();
1119 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = NULL;
1120 FOR_EACH_BB_FN (bb, cfun)
1121 bb->aux = NULL;
1122 stack.release ();
1123 BITMAP_FREE (current.ssa_names_to_pass);
1126 /* Split function at SPLIT_POINT. */
1128 static void
1129 split_function (struct split_point *split_point)
1131 vec<tree> args_to_pass = vNULL;
1132 bitmap args_to_skip;
1133 tree parm;
1134 int num = 0;
1135 cgraph_node *node, *cur_node = cgraph_node::get (current_function_decl);
1136 basic_block return_bb = find_return_bb ();
1137 basic_block call_bb;
1138 gimple_stmt_iterator gsi;
1139 gimple call;
1140 edge e;
1141 edge_iterator ei;
1142 tree retval = NULL, real_retval = NULL;
1143 bool split_part_return_p = false;
1144 gimple last_stmt = NULL;
1145 unsigned int i;
1146 tree arg, ddef;
1147 vec<tree, va_gc> **debug_args = NULL;
1149 if (dump_file)
1151 fprintf (dump_file, "\n\nSplitting function at:\n");
1152 dump_split_point (dump_file, split_point);
1155 if (cur_node->local.can_change_signature)
1156 args_to_skip = BITMAP_ALLOC (NULL);
1157 else
1158 args_to_skip = NULL;
1160 /* Collect the parameters of new function and args_to_skip bitmap. */
1161 for (parm = DECL_ARGUMENTS (current_function_decl);
1162 parm; parm = DECL_CHAIN (parm), num++)
1163 if (args_to_skip
1164 && (!is_gimple_reg (parm)
1165 || (ddef = ssa_default_def (cfun, parm)) == NULL_TREE
1166 || !bitmap_bit_p (split_point->ssa_names_to_pass,
1167 SSA_NAME_VERSION (ddef))))
1168 bitmap_set_bit (args_to_skip, num);
1169 else
1171 /* This parm might not have been used up to now, but is going to be
1172 used, hence register it. */
1173 if (is_gimple_reg (parm))
1174 arg = get_or_create_ssa_default_def (cfun, parm);
1175 else
1176 arg = parm;
1178 if (!useless_type_conversion_p (DECL_ARG_TYPE (parm), TREE_TYPE (arg)))
1179 arg = fold_convert (DECL_ARG_TYPE (parm), arg);
1180 args_to_pass.safe_push (arg);
1183 /* See if the split function will return. */
1184 FOR_EACH_EDGE (e, ei, return_bb->preds)
1185 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1186 break;
1187 if (e)
1188 split_part_return_p = true;
1190 /* Add return block to what will become the split function.
1191 We do not return; no return block is needed. */
1192 if (!split_part_return_p)
1194 /* We have no return block, so nothing is needed. */
1195 else if (return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1197 /* When we do not want to return value, we need to construct
1198 new return block with empty return statement.
1199 FIXME: Once we are able to change return type, we should change function
1200 to return void instead of just outputting function with undefined return
1201 value. For structures this affects quality of codegen. */
1202 else if (!split_point->split_part_set_retval
1203 && find_retval (return_bb))
1205 bool redirected = true;
1206 basic_block new_return_bb = create_basic_block (NULL, 0, return_bb);
1207 gimple_stmt_iterator gsi = gsi_start_bb (new_return_bb);
1208 gsi_insert_after (&gsi, gimple_build_return (NULL), GSI_NEW_STMT);
1209 while (redirected)
1211 redirected = false;
1212 FOR_EACH_EDGE (e, ei, return_bb->preds)
1213 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1215 new_return_bb->count += e->count;
1216 new_return_bb->frequency += EDGE_FREQUENCY (e);
1217 redirect_edge_and_branch (e, new_return_bb);
1218 redirected = true;
1219 break;
1222 e = make_edge (new_return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1223 e->probability = REG_BR_PROB_BASE;
1224 e->count = new_return_bb->count;
1225 add_bb_to_loop (new_return_bb, current_loops->tree_root);
1226 bitmap_set_bit (split_point->split_bbs, new_return_bb->index);
1228 /* When we pass around the value, use existing return block. */
1229 else
1230 bitmap_set_bit (split_point->split_bbs, return_bb->index);
1232 /* If RETURN_BB has virtual operand PHIs, they must be removed and the
1233 virtual operand marked for renaming as we change the CFG in a way that
1234 tree-inline is not able to compensate for.
1236 Note this can happen whether or not we have a return value. If we have
1237 a return value, then RETURN_BB may have PHIs for real operands too. */
1238 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1240 bool phi_p = false;
1241 for (gsi = gsi_start_phis (return_bb); !gsi_end_p (gsi);)
1243 gimple stmt = gsi_stmt (gsi);
1244 if (!virtual_operand_p (gimple_phi_result (stmt)))
1246 gsi_next (&gsi);
1247 continue;
1249 mark_virtual_phi_result_for_renaming (stmt);
1250 remove_phi_node (&gsi, true);
1251 phi_p = true;
1253 /* In reality we have to rename the reaching definition of the
1254 virtual operand at return_bb as we will eventually release it
1255 when we remove the code region we outlined.
1256 So we have to rename all immediate virtual uses of that region
1257 if we didn't see a PHI definition yet. */
1258 /* ??? In real reality we want to set the reaching vdef of the
1259 entry of the SESE region as the vuse of the call and the reaching
1260 vdef of the exit of the SESE region as the vdef of the call. */
1261 if (!phi_p)
1262 for (gsi = gsi_start_bb (return_bb); !gsi_end_p (gsi); gsi_next (&gsi))
1264 gimple stmt = gsi_stmt (gsi);
1265 if (gimple_vuse (stmt))
1267 gimple_set_vuse (stmt, NULL_TREE);
1268 update_stmt (stmt);
1270 if (gimple_vdef (stmt))
1271 break;
1275 /* Now create the actual clone. */
1276 cgraph_edge::rebuild_edges ();
1277 node = cur_node->create_version_clone_with_body
1278 (vNULL, NULL, args_to_skip, !split_part_return_p, split_point->split_bbs,
1279 split_point->entry_bb, "part");
1281 /* Let's take a time profile for splitted function. */
1282 node->tp_first_run = cur_node->tp_first_run + 1;
1284 /* For usual cloning it is enough to clear builtin only when signature
1285 changes. For partial inlining we however can not expect the part
1286 of builtin implementation to have same semantic as the whole. */
1287 if (DECL_BUILT_IN (node->decl))
1289 DECL_BUILT_IN_CLASS (node->decl) = NOT_BUILT_IN;
1290 DECL_FUNCTION_CODE (node->decl) = (enum built_in_function) 0;
1292 /* If the original function is declared inline, there is no point in issuing
1293 a warning for the non-inlinable part. */
1294 DECL_NO_INLINE_WARNING_P (node->decl) = 1;
1295 cur_node->remove_callees ();
1296 cur_node->remove_all_references ();
1297 if (!split_part_return_p)
1298 TREE_THIS_VOLATILE (node->decl) = 1;
1299 if (dump_file)
1300 dump_function_to_file (node->decl, dump_file, dump_flags);
1302 /* Create the basic block we place call into. It is the entry basic block
1303 split after last label. */
1304 call_bb = split_point->entry_bb;
1305 for (gsi = gsi_start_bb (call_bb); !gsi_end_p (gsi);)
1306 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
1308 last_stmt = gsi_stmt (gsi);
1309 gsi_next (&gsi);
1311 else
1312 break;
1313 e = split_block (split_point->entry_bb, last_stmt);
1314 remove_edge (e);
1316 /* Produce the call statement. */
1317 gsi = gsi_last_bb (call_bb);
1318 FOR_EACH_VEC_ELT (args_to_pass, i, arg)
1319 if (!is_gimple_val (arg))
1321 arg = force_gimple_operand_gsi (&gsi, arg, true, NULL_TREE,
1322 false, GSI_CONTINUE_LINKING);
1323 args_to_pass[i] = arg;
1325 call = gimple_build_call_vec (node->decl, args_to_pass);
1326 gimple_set_block (call, DECL_INITIAL (current_function_decl));
1327 args_to_pass.release ();
1329 /* For optimized away parameters, add on the caller side
1330 before the call
1331 DEBUG D#X => parm_Y(D)
1332 stmts and associate D#X with parm in decl_debug_args_lookup
1333 vector to say for debug info that if parameter parm had been passed,
1334 it would have value parm_Y(D). */
1335 if (args_to_skip)
1336 for (parm = DECL_ARGUMENTS (current_function_decl), num = 0;
1337 parm; parm = DECL_CHAIN (parm), num++)
1338 if (bitmap_bit_p (args_to_skip, num)
1339 && is_gimple_reg (parm))
1341 tree ddecl;
1342 gimple def_temp;
1344 /* This needs to be done even without MAY_HAVE_DEBUG_STMTS,
1345 otherwise if it didn't exist before, we'd end up with
1346 different SSA_NAME_VERSIONs between -g and -g0. */
1347 arg = get_or_create_ssa_default_def (cfun, parm);
1348 if (!MAY_HAVE_DEBUG_STMTS)
1349 continue;
1351 if (debug_args == NULL)
1352 debug_args = decl_debug_args_insert (node->decl);
1353 ddecl = make_node (DEBUG_EXPR_DECL);
1354 DECL_ARTIFICIAL (ddecl) = 1;
1355 TREE_TYPE (ddecl) = TREE_TYPE (parm);
1356 DECL_MODE (ddecl) = DECL_MODE (parm);
1357 vec_safe_push (*debug_args, DECL_ORIGIN (parm));
1358 vec_safe_push (*debug_args, ddecl);
1359 def_temp = gimple_build_debug_bind (ddecl, unshare_expr (arg),
1360 call);
1361 gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
1363 /* And on the callee side, add
1364 DEBUG D#Y s=> parm
1365 DEBUG var => D#Y
1366 stmts to the first bb where var is a VAR_DECL created for the
1367 optimized away parameter in DECL_INITIAL block. This hints
1368 in the debug info that var (whole DECL_ORIGIN is the parm PARM_DECL)
1369 is optimized away, but could be looked up at the call site
1370 as value of D#X there. */
1371 if (debug_args != NULL)
1373 unsigned int i;
1374 tree var, vexpr;
1375 gimple_stmt_iterator cgsi;
1376 gimple def_temp;
1378 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1379 var = BLOCK_VARS (DECL_INITIAL (node->decl));
1380 i = vec_safe_length (*debug_args);
1381 cgsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1384 i -= 2;
1385 while (var != NULL_TREE
1386 && DECL_ABSTRACT_ORIGIN (var) != (**debug_args)[i])
1387 var = TREE_CHAIN (var);
1388 if (var == NULL_TREE)
1389 break;
1390 vexpr = make_node (DEBUG_EXPR_DECL);
1391 parm = (**debug_args)[i];
1392 DECL_ARTIFICIAL (vexpr) = 1;
1393 TREE_TYPE (vexpr) = TREE_TYPE (parm);
1394 DECL_MODE (vexpr) = DECL_MODE (parm);
1395 def_temp = gimple_build_debug_source_bind (vexpr, parm,
1396 NULL);
1397 gsi_insert_before (&cgsi, def_temp, GSI_SAME_STMT);
1398 def_temp = gimple_build_debug_bind (var, vexpr, NULL);
1399 gsi_insert_before (&cgsi, def_temp, GSI_SAME_STMT);
1401 while (i);
1402 pop_cfun ();
1405 /* We avoid address being taken on any variable used by split part,
1406 so return slot optimization is always possible. Moreover this is
1407 required to make DECL_BY_REFERENCE work. */
1408 if (aggregate_value_p (DECL_RESULT (current_function_decl),
1409 TREE_TYPE (current_function_decl))
1410 && (!is_gimple_reg_type (TREE_TYPE (DECL_RESULT (current_function_decl)))
1411 || DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))))
1412 gimple_call_set_return_slot_opt (call, true);
1414 /* Update return value. This is bit tricky. When we do not return,
1415 do nothing. When we return we might need to update return_bb
1416 or produce a new return statement. */
1417 if (!split_part_return_p)
1418 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1419 else
1421 e = make_edge (call_bb, return_bb,
1422 return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
1423 ? 0 : EDGE_FALLTHRU);
1424 e->count = call_bb->count;
1425 e->probability = REG_BR_PROB_BASE;
1427 /* If there is return basic block, see what value we need to store
1428 return value into and put call just before it. */
1429 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1431 real_retval = retval = find_retval (return_bb);
1433 if (real_retval && split_point->split_part_set_retval)
1435 gimple_stmt_iterator psi;
1437 /* See if we need new SSA_NAME for the result.
1438 When DECL_BY_REFERENCE is true, retval is actually pointer to
1439 return value and it is constant in whole function. */
1440 if (TREE_CODE (retval) == SSA_NAME
1441 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1443 retval = copy_ssa_name (retval, call);
1445 /* See if there is PHI defining return value. */
1446 for (psi = gsi_start_phis (return_bb);
1447 !gsi_end_p (psi); gsi_next (&psi))
1448 if (!virtual_operand_p (gimple_phi_result (gsi_stmt (psi))))
1449 break;
1451 /* When there is PHI, just update its value. */
1452 if (TREE_CODE (retval) == SSA_NAME
1453 && !gsi_end_p (psi))
1454 add_phi_arg (gsi_stmt (psi), retval, e, UNKNOWN_LOCATION);
1455 /* Otherwise update the return BB itself.
1456 find_return_bb allows at most one assignment to return value,
1457 so update first statement. */
1458 else
1460 gimple_stmt_iterator bsi;
1461 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi);
1462 gsi_next (&bsi))
1463 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
1465 gimple_return_set_retval (gsi_stmt (bsi), retval);
1466 break;
1468 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
1469 && !gimple_clobber_p (gsi_stmt (bsi)))
1471 gimple_assign_set_rhs1 (gsi_stmt (bsi), retval);
1472 break;
1474 update_stmt (gsi_stmt (bsi));
1477 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1479 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1480 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1482 else
1484 tree restype;
1485 restype = TREE_TYPE (DECL_RESULT (current_function_decl));
1486 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1487 if (!useless_type_conversion_p (TREE_TYPE (retval), restype))
1489 gimple cpy;
1490 tree tem = create_tmp_reg (restype, NULL);
1491 tem = make_ssa_name (tem, call);
1492 cpy = gimple_build_assign_with_ops (NOP_EXPR, retval,
1493 tem, NULL_TREE);
1494 gsi_insert_after (&gsi, cpy, GSI_NEW_STMT);
1495 retval = tem;
1497 gimple_call_set_lhs (call, retval);
1498 update_stmt (call);
1501 else
1502 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1504 /* We don't use return block (there is either no return in function or
1505 multiple of them). So create new basic block with return statement.
1507 else
1509 gimple ret;
1510 if (split_point->split_part_set_retval
1511 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1513 retval = DECL_RESULT (current_function_decl);
1515 /* We use temporary register to hold value when aggregate_value_p
1516 is false. Similarly for DECL_BY_REFERENCE we must avoid extra
1517 copy. */
1518 if (!aggregate_value_p (retval, TREE_TYPE (current_function_decl))
1519 && !DECL_BY_REFERENCE (retval))
1520 retval = create_tmp_reg (TREE_TYPE (retval), NULL);
1521 if (is_gimple_reg (retval))
1523 /* When returning by reference, there is only one SSA name
1524 assigned to RESULT_DECL (that is pointer to return value).
1525 Look it up or create new one if it is missing. */
1526 if (DECL_BY_REFERENCE (retval))
1527 retval = get_or_create_ssa_default_def (cfun, retval);
1528 /* Otherwise produce new SSA name for return value. */
1529 else
1530 retval = make_ssa_name (retval, call);
1532 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1533 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1534 else
1535 gimple_call_set_lhs (call, retval);
1537 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1538 ret = gimple_build_return (retval);
1539 gsi_insert_after (&gsi, ret, GSI_NEW_STMT);
1542 free_dominance_info (CDI_DOMINATORS);
1543 free_dominance_info (CDI_POST_DOMINATORS);
1544 compute_inline_parameters (node, true);
1547 /* Execute function splitting pass. */
1549 static unsigned int
1550 execute_split_functions (void)
1552 gimple_stmt_iterator bsi;
1553 basic_block bb;
1554 int overall_time = 0, overall_size = 0;
1555 int todo = 0;
1556 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1558 if (flags_from_decl_or_type (current_function_decl)
1559 & (ECF_NORETURN|ECF_MALLOC))
1561 if (dump_file)
1562 fprintf (dump_file, "Not splitting: noreturn/malloc function.\n");
1563 return 0;
1565 if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
1567 if (dump_file)
1568 fprintf (dump_file, "Not splitting: main function.\n");
1569 return 0;
1571 /* This can be relaxed; function might become inlinable after splitting
1572 away the uninlinable part. */
1573 if (inline_edge_summary_vec.exists ()
1574 && !inline_summary (node)->inlinable)
1576 if (dump_file)
1577 fprintf (dump_file, "Not splitting: not inlinable.\n");
1578 return 0;
1580 if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1582 if (dump_file)
1583 fprintf (dump_file, "Not splitting: disregarding inline limits.\n");
1584 return 0;
1586 /* This can be relaxed; most of versioning tests actually prevents
1587 a duplication. */
1588 if (!tree_versionable_function_p (current_function_decl))
1590 if (dump_file)
1591 fprintf (dump_file, "Not splitting: not versionable.\n");
1592 return 0;
1594 /* FIXME: we could support this. */
1595 if (DECL_STRUCT_FUNCTION (current_function_decl)->static_chain_decl)
1597 if (dump_file)
1598 fprintf (dump_file, "Not splitting: nested function.\n");
1599 return 0;
1602 /* See if it makes sense to try to split.
1603 It makes sense to split if we inline, that is if we have direct calls to
1604 handle or direct calls are possibly going to appear as result of indirect
1605 inlining or LTO. Also handle -fprofile-generate as LTO to allow non-LTO
1606 training for LTO -fprofile-use build.
1608 Note that we are not completely conservative about disqualifying functions
1609 called once. It is possible that the caller is called more then once and
1610 then inlining would still benefit. */
1611 if ((!node->callers
1612 /* Local functions called once will be completely inlined most of time. */
1613 || (!node->callers->next_caller && node->local.local))
1614 && !node->address_taken
1615 && (!flag_lto || !node->externally_visible))
1617 if (dump_file)
1618 fprintf (dump_file, "Not splitting: not called directly "
1619 "or called once.\n");
1620 return 0;
1623 /* FIXME: We can actually split if splitting reduces call overhead. */
1624 if (!flag_inline_small_functions
1625 && !DECL_DECLARED_INLINE_P (current_function_decl))
1627 if (dump_file)
1628 fprintf (dump_file, "Not splitting: not autoinlining and function"
1629 " is not inline.\n");
1630 return 0;
1633 /* We enforce splitting after loop headers when profile info is not
1634 available. */
1635 if (profile_status_for_fn (cfun) != PROFILE_READ)
1636 mark_dfs_back_edges ();
1638 /* Initialize bitmap to track forbidden calls. */
1639 forbidden_dominators = BITMAP_ALLOC (NULL);
1640 calculate_dominance_info (CDI_DOMINATORS);
1642 /* Compute local info about basic blocks and determine function size/time. */
1643 bb_info_vec.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
1644 memset (&best_split_point, 0, sizeof (best_split_point));
1645 FOR_EACH_BB_FN (bb, cfun)
1647 int time = 0;
1648 int size = 0;
1649 int freq = compute_call_stmt_bb_frequency (current_function_decl, bb);
1651 if (dump_file && (dump_flags & TDF_DETAILS))
1652 fprintf (dump_file, "Basic block %i\n", bb->index);
1654 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1656 int this_time, this_size;
1657 gimple stmt = gsi_stmt (bsi);
1659 this_size = estimate_num_insns (stmt, &eni_size_weights);
1660 this_time = estimate_num_insns (stmt, &eni_time_weights) * freq;
1661 size += this_size;
1662 time += this_time;
1663 check_forbidden_calls (stmt);
1665 if (dump_file && (dump_flags & TDF_DETAILS))
1667 fprintf (dump_file, " freq:%6i size:%3i time:%3i ",
1668 freq, this_size, this_time);
1669 print_gimple_stmt (dump_file, stmt, 0, 0);
1672 overall_time += time;
1673 overall_size += size;
1674 bb_info_vec[bb->index].time = time;
1675 bb_info_vec[bb->index].size = size;
1677 find_split_points (overall_time, overall_size);
1678 if (best_split_point.split_bbs)
1680 split_function (&best_split_point);
1681 BITMAP_FREE (best_split_point.ssa_names_to_pass);
1682 BITMAP_FREE (best_split_point.split_bbs);
1683 todo = TODO_update_ssa | TODO_cleanup_cfg;
1685 BITMAP_FREE (forbidden_dominators);
1686 bb_info_vec.release ();
1687 return todo;
1690 namespace {
1692 const pass_data pass_data_split_functions =
1694 GIMPLE_PASS, /* type */
1695 "fnsplit", /* name */
1696 OPTGROUP_NONE, /* optinfo_flags */
1697 TV_IPA_FNSPLIT, /* tv_id */
1698 PROP_cfg, /* properties_required */
1699 0, /* properties_provided */
1700 0, /* properties_destroyed */
1701 0, /* todo_flags_start */
1702 0, /* todo_flags_finish */
1705 class pass_split_functions : public gimple_opt_pass
1707 public:
1708 pass_split_functions (gcc::context *ctxt)
1709 : gimple_opt_pass (pass_data_split_functions, ctxt)
1712 /* opt_pass methods: */
1713 virtual bool gate (function *);
1714 virtual unsigned int execute (function *)
1716 return execute_split_functions ();
1719 }; // class pass_split_functions
1721 bool
1722 pass_split_functions::gate (function *)
1724 /* When doing profile feedback, we want to execute the pass after profiling
1725 is read. So disable one in early optimization. */
1726 return (flag_partial_inlining
1727 && !profile_arc_flag && !flag_branch_probabilities);
1730 } // anon namespace
1732 gimple_opt_pass *
1733 make_pass_split_functions (gcc::context *ctxt)
1735 return new pass_split_functions (ctxt);
1738 /* Execute function splitting pass. */
1740 static unsigned int
1741 execute_feedback_split_functions (void)
1743 unsigned int retval = execute_split_functions ();
1744 if (retval)
1745 retval |= TODO_rebuild_cgraph_edges;
1746 return retval;
1749 namespace {
1751 const pass_data pass_data_feedback_split_functions =
1753 GIMPLE_PASS, /* type */
1754 "feedback_fnsplit", /* name */
1755 OPTGROUP_NONE, /* optinfo_flags */
1756 TV_IPA_FNSPLIT, /* tv_id */
1757 PROP_cfg, /* properties_required */
1758 0, /* properties_provided */
1759 0, /* properties_destroyed */
1760 0, /* todo_flags_start */
1761 0, /* todo_flags_finish */
1764 class pass_feedback_split_functions : public gimple_opt_pass
1766 public:
1767 pass_feedback_split_functions (gcc::context *ctxt)
1768 : gimple_opt_pass (pass_data_feedback_split_functions, ctxt)
1771 /* opt_pass methods: */
1772 virtual bool gate (function *);
1773 virtual unsigned int execute (function *)
1775 return execute_feedback_split_functions ();
1778 }; // class pass_feedback_split_functions
1780 bool
1781 pass_feedback_split_functions::gate (function *)
1783 /* We don't need to split when profiling at all, we are producing
1784 lousy code anyway. */
1785 return (flag_partial_inlining
1786 && flag_branch_probabilities);
1789 } // anon namespace
1791 gimple_opt_pass *
1792 make_pass_feedback_split_functions (gcc::context *ctxt)
1794 return new pass_feedback_split_functions (ctxt);