* pt.c (lookup_template_class_1): Splice out abi_tag attribute if
[official-gcc.git] / gcc / ipa-split.c
blobfde436538f53897dd6555d54999a8269ad8f244c
1 /* Function splitting pass
2 Copyright (C) 2010-2014 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka <jh@suse.cz>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* The purpose of this pass is to split function bodies to improve
22 inlining. I.e. for function of the form:
24 func (...)
26 if (cheap_test)
27 something_small
28 else
29 something_big
32 Produce:
34 func.part (...)
36 something_big
39 func (...)
41 if (cheap_test)
42 something_small
43 else
44 func.part (...);
47 When func becomes inlinable and when cheap_test is often true, inlining func,
48 but not fund.part leads to performance improvement similar as inlining
49 original func while the code size growth is smaller.
51 The pass is organized in three stages:
52 1) Collect local info about basic block into BB_INFO structure and
53 compute function body estimated size and time.
54 2) Via DFS walk find all possible basic blocks where we can split
55 and chose best one.
56 3) If split point is found, split at the specified BB by creating a clone
57 and updating function to call it.
59 The decisions what functions to split are in execute_split_functions
60 and consider_split.
62 There are several possible future improvements for this pass including:
64 1) Splitting to break up large functions
65 2) Splitting to reduce stack frame usage
66 3) Allow split part of function to use values computed in the header part.
67 The values needs to be passed to split function, perhaps via same
68 interface as for nested functions or as argument.
69 4) Support for simple rematerialization. I.e. when split part use
70 value computed in header from function parameter in very cheap way, we
71 can just recompute it.
72 5) Support splitting of nested functions.
73 6) Support non-SSA arguments.
74 7) There is nothing preventing us from producing multiple parts of single function
75 when needed or splitting also the parts. */
77 #include "config.h"
78 #include "system.h"
79 #include "coretypes.h"
80 #include "tree.h"
81 #include "basic-block.h"
82 #include "tree-ssa-alias.h"
83 #include "internal-fn.h"
84 #include "gimple-expr.h"
85 #include "is-a.h"
86 #include "gimple.h"
87 #include "stringpool.h"
88 #include "expr.h"
89 #include "calls.h"
90 #include "gimplify.h"
91 #include "gimple-iterator.h"
92 #include "gimplify-me.h"
93 #include "gimple-walk.h"
94 #include "target.h"
95 #include "ipa-prop.h"
96 #include "gimple-ssa.h"
97 #include "tree-cfg.h"
98 #include "tree-phinodes.h"
99 #include "ssa-iterators.h"
100 #include "stringpool.h"
101 #include "tree-ssanames.h"
102 #include "tree-into-ssa.h"
103 #include "tree-dfa.h"
104 #include "tree-pass.h"
105 #include "flags.h"
106 #include "diagnostic.h"
107 #include "tree-dump.h"
108 #include "tree-inline.h"
109 #include "params.h"
110 #include "gimple-pretty-print.h"
111 #include "ipa-inline.h"
112 #include "cfgloop.h"
114 /* Per basic block info. */
116 typedef struct
118 unsigned int size;
119 unsigned int time;
120 } bb_info;
122 static vec<bb_info> bb_info_vec;
124 /* Description of split point. */
126 struct split_point
128 /* Size of the partitions. */
129 unsigned int header_time, header_size, split_time, split_size;
131 /* SSA names that need to be passed into spit function. */
132 bitmap ssa_names_to_pass;
134 /* Basic block where we split (that will become entry point of new function. */
135 basic_block entry_bb;
137 /* Basic blocks we are splitting away. */
138 bitmap split_bbs;
140 /* True when return value is computed on split part and thus it needs
141 to be returned. */
142 bool split_part_set_retval;
145 /* Best split point found. */
147 struct split_point best_split_point;
149 /* Set of basic blocks that are not allowed to dominate a split point. */
151 static bitmap forbidden_dominators;
153 static tree find_retval (basic_block return_bb);
155 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
156 variable, check it if it is present in bitmap passed via DATA. */
158 static bool
159 test_nonssa_use (gimple, tree t, tree, void *data)
161 t = get_base_address (t);
163 if (!t || is_gimple_reg (t))
164 return false;
166 if (TREE_CODE (t) == PARM_DECL
167 || (TREE_CODE (t) == VAR_DECL
168 && auto_var_in_fn_p (t, current_function_decl))
169 || TREE_CODE (t) == RESULT_DECL
170 /* Normal labels are part of CFG and will be handled gratefuly.
171 Forced labels however can be used directly by statements and
172 need to stay in one partition along with their uses. */
173 || (TREE_CODE (t) == LABEL_DECL
174 && FORCED_LABEL (t)))
175 return bitmap_bit_p ((bitmap)data, DECL_UID (t));
177 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
178 to pretend that the value pointed to is actual result decl. */
179 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
180 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
181 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
182 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
183 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
184 return
185 bitmap_bit_p ((bitmap)data,
186 DECL_UID (DECL_RESULT (current_function_decl)));
188 return false;
191 /* Dump split point CURRENT. */
193 static void
194 dump_split_point (FILE * file, struct split_point *current)
196 fprintf (file,
197 "Split point at BB %i\n"
198 " header time: %i header size: %i\n"
199 " split time: %i split size: %i\n bbs: ",
200 current->entry_bb->index, current->header_time,
201 current->header_size, current->split_time, current->split_size);
202 dump_bitmap (file, current->split_bbs);
203 fprintf (file, " SSA names to pass: ");
204 dump_bitmap (file, current->ssa_names_to_pass);
207 /* Look for all BBs in header that might lead to the split part and verify
208 that they are not defining any non-SSA var used by the split part.
209 Parameters are the same as for consider_split. */
211 static bool
212 verify_non_ssa_vars (struct split_point *current, bitmap non_ssa_vars,
213 basic_block return_bb)
215 bitmap seen = BITMAP_ALLOC (NULL);
216 vec<basic_block> worklist = vNULL;
217 edge e;
218 edge_iterator ei;
219 bool ok = true;
220 basic_block bb;
222 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
223 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
224 && !bitmap_bit_p (current->split_bbs, e->src->index))
226 worklist.safe_push (e->src);
227 bitmap_set_bit (seen, e->src->index);
230 while (!worklist.is_empty ())
232 gimple_stmt_iterator bsi;
234 bb = worklist.pop ();
235 FOR_EACH_EDGE (e, ei, bb->preds)
236 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
237 && bitmap_set_bit (seen, e->src->index))
239 gcc_checking_assert (!bitmap_bit_p (current->split_bbs,
240 e->src->index));
241 worklist.safe_push (e->src);
243 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
245 gimple stmt = gsi_stmt (bsi);
246 if (is_gimple_debug (stmt))
247 continue;
248 if (walk_stmt_load_store_addr_ops
249 (stmt, non_ssa_vars, test_nonssa_use, test_nonssa_use,
250 test_nonssa_use))
252 ok = false;
253 goto done;
255 if (gimple_code (stmt) == GIMPLE_LABEL
256 && test_nonssa_use (stmt, gimple_label_label (stmt),
257 NULL_TREE, non_ssa_vars))
259 ok = false;
260 goto done;
263 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
265 if (walk_stmt_load_store_addr_ops
266 (gsi_stmt (bsi), non_ssa_vars, test_nonssa_use, test_nonssa_use,
267 test_nonssa_use))
269 ok = false;
270 goto done;
273 FOR_EACH_EDGE (e, ei, bb->succs)
275 if (e->dest != return_bb)
276 continue;
277 for (bsi = gsi_start_phis (return_bb); !gsi_end_p (bsi);
278 gsi_next (&bsi))
280 gimple stmt = gsi_stmt (bsi);
281 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
283 if (virtual_operand_p (gimple_phi_result (stmt)))
284 continue;
285 if (TREE_CODE (op) != SSA_NAME
286 && test_nonssa_use (stmt, op, op, non_ssa_vars))
288 ok = false;
289 goto done;
295 /* Verify that the rest of function does not define any label
296 used by the split part. */
297 FOR_EACH_BB_FN (bb, cfun)
298 if (!bitmap_bit_p (current->split_bbs, bb->index)
299 && !bitmap_bit_p (seen, bb->index))
301 gimple_stmt_iterator bsi;
302 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
303 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_LABEL
304 && test_nonssa_use (gsi_stmt (bsi),
305 gimple_label_label (gsi_stmt (bsi)),
306 NULL_TREE, non_ssa_vars))
308 ok = false;
309 goto done;
311 else if (gimple_code (gsi_stmt (bsi)) != GIMPLE_LABEL)
312 break;
315 done:
316 BITMAP_FREE (seen);
317 worklist.release ();
318 return ok;
321 /* If STMT is a call, check the callee against a list of forbidden
322 predicate functions. If a match is found, look for uses of the
323 call result in condition statements that compare against zero.
324 For each such use, find the block targeted by the condition
325 statement for the nonzero result, and set the bit for this block
326 in the forbidden dominators bitmap. The purpose of this is to avoid
327 selecting a split point where we are likely to lose the chance
328 to optimize away an unused function call. */
330 static void
331 check_forbidden_calls (gimple stmt)
333 imm_use_iterator use_iter;
334 use_operand_p use_p;
335 tree lhs;
337 /* At the moment, __builtin_constant_p is the only forbidden
338 predicate function call (see PR49642). */
339 if (!gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P))
340 return;
342 lhs = gimple_call_lhs (stmt);
344 if (!lhs || TREE_CODE (lhs) != SSA_NAME)
345 return;
347 FOR_EACH_IMM_USE_FAST (use_p, use_iter, lhs)
349 tree op1;
350 basic_block use_bb, forbidden_bb;
351 enum tree_code code;
352 edge true_edge, false_edge;
353 gimple use_stmt = USE_STMT (use_p);
355 if (gimple_code (use_stmt) != GIMPLE_COND)
356 continue;
358 /* Assuming canonical form for GIMPLE_COND here, with constant
359 in second position. */
360 op1 = gimple_cond_rhs (use_stmt);
361 code = gimple_cond_code (use_stmt);
362 use_bb = gimple_bb (use_stmt);
364 extract_true_false_edges_from_block (use_bb, &true_edge, &false_edge);
366 /* We're only interested in comparisons that distinguish
367 unambiguously from zero. */
368 if (!integer_zerop (op1) || code == LE_EXPR || code == GE_EXPR)
369 continue;
371 if (code == EQ_EXPR)
372 forbidden_bb = false_edge->dest;
373 else
374 forbidden_bb = true_edge->dest;
376 bitmap_set_bit (forbidden_dominators, forbidden_bb->index);
380 /* If BB is dominated by any block in the forbidden dominators set,
381 return TRUE; else FALSE. */
383 static bool
384 dominated_by_forbidden (basic_block bb)
386 unsigned dom_bb;
387 bitmap_iterator bi;
389 EXECUTE_IF_SET_IN_BITMAP (forbidden_dominators, 1, dom_bb, bi)
391 if (dominated_by_p (CDI_DOMINATORS, bb,
392 BASIC_BLOCK_FOR_FN (cfun, dom_bb)))
393 return true;
396 return false;
399 /* We found an split_point CURRENT. NON_SSA_VARS is bitmap of all non ssa
400 variables used and RETURN_BB is return basic block.
401 See if we can split function here. */
403 static void
404 consider_split (struct split_point *current, bitmap non_ssa_vars,
405 basic_block return_bb)
407 tree parm;
408 unsigned int num_args = 0;
409 unsigned int call_overhead;
410 edge e;
411 edge_iterator ei;
412 gimple_stmt_iterator bsi;
413 unsigned int i;
414 int incoming_freq = 0;
415 tree retval;
416 bool back_edge = false;
418 if (dump_file && (dump_flags & TDF_DETAILS))
419 dump_split_point (dump_file, current);
421 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
423 if (e->flags & EDGE_DFS_BACK)
424 back_edge = true;
425 if (!bitmap_bit_p (current->split_bbs, e->src->index))
426 incoming_freq += EDGE_FREQUENCY (e);
429 /* Do not split when we would end up calling function anyway. */
430 if (incoming_freq
431 >= (ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency
432 * PARAM_VALUE (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY) / 100))
434 /* When profile is guessed, we can not expect it to give us
435 realistic estimate on likelyness of function taking the
436 complex path. As a special case, when tail of the function is
437 a loop, enable splitting since inlining code skipping the loop
438 is likely noticeable win. */
439 if (back_edge
440 && profile_status_for_fn (cfun) != PROFILE_READ
441 && incoming_freq < ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency)
443 if (dump_file && (dump_flags & TDF_DETAILS))
444 fprintf (dump_file,
445 " Split before loop, accepting despite low frequencies %i %i.\n",
446 incoming_freq,
447 ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency);
449 else
451 if (dump_file && (dump_flags & TDF_DETAILS))
452 fprintf (dump_file,
453 " Refused: incoming frequency is too large.\n");
454 return;
458 if (!current->header_size)
460 if (dump_file && (dump_flags & TDF_DETAILS))
461 fprintf (dump_file, " Refused: header empty\n");
462 return;
465 /* Verify that PHI args on entry are either virtual or all their operands
466 incoming from header are the same. */
467 for (bsi = gsi_start_phis (current->entry_bb); !gsi_end_p (bsi); gsi_next (&bsi))
469 gimple stmt = gsi_stmt (bsi);
470 tree val = NULL;
472 if (virtual_operand_p (gimple_phi_result (stmt)))
473 continue;
474 for (i = 0; i < gimple_phi_num_args (stmt); i++)
476 edge e = gimple_phi_arg_edge (stmt, i);
477 if (!bitmap_bit_p (current->split_bbs, e->src->index))
479 tree edge_val = gimple_phi_arg_def (stmt, i);
480 if (val && edge_val != val)
482 if (dump_file && (dump_flags & TDF_DETAILS))
483 fprintf (dump_file,
484 " Refused: entry BB has PHI with multiple variants\n");
485 return;
487 val = edge_val;
493 /* See what argument we will pass to the split function and compute
494 call overhead. */
495 call_overhead = eni_size_weights.call_cost;
496 for (parm = DECL_ARGUMENTS (current_function_decl); parm;
497 parm = DECL_CHAIN (parm))
499 if (!is_gimple_reg (parm))
501 if (bitmap_bit_p (non_ssa_vars, DECL_UID (parm)))
503 if (dump_file && (dump_flags & TDF_DETAILS))
504 fprintf (dump_file,
505 " Refused: need to pass non-ssa param values\n");
506 return;
509 else
511 tree ddef = ssa_default_def (cfun, parm);
512 if (ddef
513 && bitmap_bit_p (current->ssa_names_to_pass,
514 SSA_NAME_VERSION (ddef)))
516 if (!VOID_TYPE_P (TREE_TYPE (parm)))
517 call_overhead += estimate_move_cost (TREE_TYPE (parm), false);
518 num_args++;
522 if (!VOID_TYPE_P (TREE_TYPE (current_function_decl)))
523 call_overhead += estimate_move_cost (TREE_TYPE (current_function_decl),
524 false);
526 if (current->split_size <= call_overhead)
528 if (dump_file && (dump_flags & TDF_DETAILS))
529 fprintf (dump_file,
530 " Refused: split size is smaller than call overhead\n");
531 return;
533 if (current->header_size + call_overhead
534 >= (unsigned int)(DECL_DECLARED_INLINE_P (current_function_decl)
535 ? MAX_INLINE_INSNS_SINGLE
536 : MAX_INLINE_INSNS_AUTO))
538 if (dump_file && (dump_flags & TDF_DETAILS))
539 fprintf (dump_file,
540 " Refused: header size is too large for inline candidate\n");
541 return;
544 /* FIXME: we currently can pass only SSA function parameters to the split
545 arguments. Once parm_adjustment infrastructure is supported by cloning,
546 we can pass more than that. */
547 if (num_args != bitmap_count_bits (current->ssa_names_to_pass))
550 if (dump_file && (dump_flags & TDF_DETAILS))
551 fprintf (dump_file,
552 " Refused: need to pass non-param values\n");
553 return;
556 /* When there are non-ssa vars used in the split region, see if they
557 are used in the header region. If so, reject the split.
558 FIXME: we can use nested function support to access both. */
559 if (!bitmap_empty_p (non_ssa_vars)
560 && !verify_non_ssa_vars (current, non_ssa_vars, return_bb))
562 if (dump_file && (dump_flags & TDF_DETAILS))
563 fprintf (dump_file,
564 " Refused: split part has non-ssa uses\n");
565 return;
568 /* If the split point is dominated by a forbidden block, reject
569 the split. */
570 if (!bitmap_empty_p (forbidden_dominators)
571 && dominated_by_forbidden (current->entry_bb))
573 if (dump_file && (dump_flags & TDF_DETAILS))
574 fprintf (dump_file,
575 " Refused: split point dominated by forbidden block\n");
576 return;
579 /* See if retval used by return bb is computed by header or split part.
580 When it is computed by split part, we need to produce return statement
581 in the split part and add code to header to pass it around.
583 This is bit tricky to test:
584 1) When there is no return_bb or no return value, we always pass
585 value around.
586 2) Invariants are always computed by caller.
587 3) For SSA we need to look if defining statement is in header or split part
588 4) For non-SSA we need to look where the var is computed. */
589 retval = find_retval (return_bb);
590 if (!retval)
591 current->split_part_set_retval = true;
592 else if (is_gimple_min_invariant (retval))
593 current->split_part_set_retval = false;
594 /* Special case is value returned by reference we record as if it was non-ssa
595 set to result_decl. */
596 else if (TREE_CODE (retval) == SSA_NAME
597 && SSA_NAME_VAR (retval)
598 && TREE_CODE (SSA_NAME_VAR (retval)) == RESULT_DECL
599 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
600 current->split_part_set_retval
601 = bitmap_bit_p (non_ssa_vars, DECL_UID (SSA_NAME_VAR (retval)));
602 else if (TREE_CODE (retval) == SSA_NAME)
603 current->split_part_set_retval
604 = (!SSA_NAME_IS_DEFAULT_DEF (retval)
605 && (bitmap_bit_p (current->split_bbs,
606 gimple_bb (SSA_NAME_DEF_STMT (retval))->index)
607 || gimple_bb (SSA_NAME_DEF_STMT (retval)) == return_bb));
608 else if (TREE_CODE (retval) == PARM_DECL)
609 current->split_part_set_retval = false;
610 else if (TREE_CODE (retval) == VAR_DECL
611 || TREE_CODE (retval) == RESULT_DECL)
612 current->split_part_set_retval
613 = bitmap_bit_p (non_ssa_vars, DECL_UID (retval));
614 else
615 current->split_part_set_retval = true;
617 /* split_function fixes up at most one PHI non-virtual PHI node in return_bb,
618 for the return value. If there are other PHIs, give up. */
619 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
621 gimple_stmt_iterator psi;
623 for (psi = gsi_start_phis (return_bb); !gsi_end_p (psi); gsi_next (&psi))
624 if (!virtual_operand_p (gimple_phi_result (gsi_stmt (psi)))
625 && !(retval
626 && current->split_part_set_retval
627 && TREE_CODE (retval) == SSA_NAME
628 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
629 && SSA_NAME_DEF_STMT (retval) == gsi_stmt (psi)))
631 if (dump_file && (dump_flags & TDF_DETAILS))
632 fprintf (dump_file,
633 " Refused: return bb has extra PHIs\n");
634 return;
638 if (dump_file && (dump_flags & TDF_DETAILS))
639 fprintf (dump_file, " Accepted!\n");
641 /* At the moment chose split point with lowest frequency and that leaves
642 out smallest size of header.
643 In future we might re-consider this heuristics. */
644 if (!best_split_point.split_bbs
645 || best_split_point.entry_bb->frequency > current->entry_bb->frequency
646 || (best_split_point.entry_bb->frequency == current->entry_bb->frequency
647 && best_split_point.split_size < current->split_size))
650 if (dump_file && (dump_flags & TDF_DETAILS))
651 fprintf (dump_file, " New best split point!\n");
652 if (best_split_point.ssa_names_to_pass)
654 BITMAP_FREE (best_split_point.ssa_names_to_pass);
655 BITMAP_FREE (best_split_point.split_bbs);
657 best_split_point = *current;
658 best_split_point.ssa_names_to_pass = BITMAP_ALLOC (NULL);
659 bitmap_copy (best_split_point.ssa_names_to_pass,
660 current->ssa_names_to_pass);
661 best_split_point.split_bbs = BITMAP_ALLOC (NULL);
662 bitmap_copy (best_split_point.split_bbs, current->split_bbs);
666 /* Return basic block containing RETURN statement. We allow basic blocks
667 of the form:
668 <retval> = tmp_var;
669 return <retval>
670 but return_bb can not be more complex than this.
671 If nothing is found, return the exit block.
673 When there are multiple RETURN statement, chose one with return value,
674 since that one is more likely shared by multiple code paths.
676 Return BB is special, because for function splitting it is the only
677 basic block that is duplicated in between header and split part of the
678 function.
680 TODO: We might support multiple return blocks. */
682 static basic_block
683 find_return_bb (void)
685 edge e;
686 basic_block return_bb = EXIT_BLOCK_PTR_FOR_FN (cfun);
687 gimple_stmt_iterator bsi;
688 bool found_return = false;
689 tree retval = NULL_TREE;
691 if (!single_pred_p (EXIT_BLOCK_PTR_FOR_FN (cfun)))
692 return return_bb;
694 e = single_pred_edge (EXIT_BLOCK_PTR_FOR_FN (cfun));
695 for (bsi = gsi_last_bb (e->src); !gsi_end_p (bsi); gsi_prev (&bsi))
697 gimple stmt = gsi_stmt (bsi);
698 if (gimple_code (stmt) == GIMPLE_LABEL
699 || is_gimple_debug (stmt)
700 || gimple_clobber_p (stmt))
702 else if (gimple_code (stmt) == GIMPLE_ASSIGN
703 && found_return
704 && gimple_assign_single_p (stmt)
705 && (auto_var_in_fn_p (gimple_assign_rhs1 (stmt),
706 current_function_decl)
707 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
708 && retval == gimple_assign_lhs (stmt))
710 else if (gimple_code (stmt) == GIMPLE_RETURN)
712 found_return = true;
713 retval = gimple_return_retval (stmt);
715 else
716 break;
718 if (gsi_end_p (bsi) && found_return)
719 return_bb = e->src;
721 return return_bb;
724 /* Given return basic block RETURN_BB, see where return value is really
725 stored. */
726 static tree
727 find_retval (basic_block return_bb)
729 gimple_stmt_iterator bsi;
730 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
731 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
732 return gimple_return_retval (gsi_stmt (bsi));
733 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
734 && !gimple_clobber_p (gsi_stmt (bsi)))
735 return gimple_assign_rhs1 (gsi_stmt (bsi));
736 return NULL;
739 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
740 variable, mark it as used in bitmap passed via DATA.
741 Return true when access to T prevents splitting the function. */
743 static bool
744 mark_nonssa_use (gimple, tree t, tree, void *data)
746 t = get_base_address (t);
748 if (!t || is_gimple_reg (t))
749 return false;
751 /* At present we can't pass non-SSA arguments to split function.
752 FIXME: this can be relaxed by passing references to arguments. */
753 if (TREE_CODE (t) == PARM_DECL)
755 if (dump_file && (dump_flags & TDF_DETAILS))
756 fprintf (dump_file,
757 "Cannot split: use of non-ssa function parameter.\n");
758 return true;
761 if ((TREE_CODE (t) == VAR_DECL
762 && auto_var_in_fn_p (t, current_function_decl))
763 || TREE_CODE (t) == RESULT_DECL
764 || (TREE_CODE (t) == LABEL_DECL
765 && FORCED_LABEL (t)))
766 bitmap_set_bit ((bitmap)data, DECL_UID (t));
768 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
769 to pretend that the value pointed to is actual result decl. */
770 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
771 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
772 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
773 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
774 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
775 return
776 bitmap_bit_p ((bitmap)data,
777 DECL_UID (DECL_RESULT (current_function_decl)));
779 return false;
782 /* Compute local properties of basic block BB we collect when looking for
783 split points. We look for ssa defs and store them in SET_SSA_NAMES,
784 for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic
785 vars stored in NON_SSA_VARS.
787 When BB has edge to RETURN_BB, collect uses in RETURN_BB too.
789 Return false when BB contains something that prevents it from being put into
790 split function. */
792 static bool
793 visit_bb (basic_block bb, basic_block return_bb,
794 bitmap set_ssa_names, bitmap used_ssa_names,
795 bitmap non_ssa_vars)
797 gimple_stmt_iterator bsi;
798 edge e;
799 edge_iterator ei;
800 bool can_split = true;
802 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
804 gimple stmt = gsi_stmt (bsi);
805 tree op;
806 ssa_op_iter iter;
807 tree decl;
809 if (is_gimple_debug (stmt))
810 continue;
812 if (gimple_clobber_p (stmt))
813 continue;
815 /* FIXME: We can split regions containing EH. We can not however
816 split RESX, EH_DISPATCH and EH_POINTER referring to same region
817 into different partitions. This would require tracking of
818 EH regions and checking in consider_split_point if they
819 are not used elsewhere. */
820 if (gimple_code (stmt) == GIMPLE_RESX)
822 if (dump_file && (dump_flags & TDF_DETAILS))
823 fprintf (dump_file, "Cannot split: resx.\n");
824 can_split = false;
826 if (gimple_code (stmt) == GIMPLE_EH_DISPATCH)
828 if (dump_file && (dump_flags & TDF_DETAILS))
829 fprintf (dump_file, "Cannot split: eh dispatch.\n");
830 can_split = false;
833 /* Check builtins that prevent splitting. */
834 if (gimple_code (stmt) == GIMPLE_CALL
835 && (decl = gimple_call_fndecl (stmt)) != NULL_TREE
836 && DECL_BUILT_IN (decl)
837 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
838 switch (DECL_FUNCTION_CODE (decl))
840 /* FIXME: once we will allow passing non-parm values to split part,
841 we need to be sure to handle correct builtin_stack_save and
842 builtin_stack_restore. At the moment we are safe; there is no
843 way to store builtin_stack_save result in non-SSA variable
844 since all calls to those are compiler generated. */
845 case BUILT_IN_APPLY:
846 case BUILT_IN_APPLY_ARGS:
847 case BUILT_IN_VA_START:
848 if (dump_file && (dump_flags & TDF_DETAILS))
849 fprintf (dump_file,
850 "Cannot split: builtin_apply and va_start.\n");
851 can_split = false;
852 break;
853 case BUILT_IN_EH_POINTER:
854 if (dump_file && (dump_flags & TDF_DETAILS))
855 fprintf (dump_file, "Cannot split: builtin_eh_pointer.\n");
856 can_split = false;
857 break;
858 default:
859 break;
862 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
863 bitmap_set_bit (set_ssa_names, SSA_NAME_VERSION (op));
864 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
865 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
866 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
867 mark_nonssa_use,
868 mark_nonssa_use,
869 mark_nonssa_use);
871 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
873 gimple stmt = gsi_stmt (bsi);
874 unsigned int i;
876 if (virtual_operand_p (gimple_phi_result (stmt)))
877 continue;
878 bitmap_set_bit (set_ssa_names,
879 SSA_NAME_VERSION (gimple_phi_result (stmt)));
880 for (i = 0; i < gimple_phi_num_args (stmt); i++)
882 tree op = gimple_phi_arg_def (stmt, i);
883 if (TREE_CODE (op) == SSA_NAME)
884 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
886 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
887 mark_nonssa_use,
888 mark_nonssa_use,
889 mark_nonssa_use);
891 /* Record also uses coming from PHI operand in return BB. */
892 FOR_EACH_EDGE (e, ei, bb->succs)
893 if (e->dest == return_bb)
895 for (bsi = gsi_start_phis (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
897 gimple stmt = gsi_stmt (bsi);
898 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
900 if (virtual_operand_p (gimple_phi_result (stmt)))
901 continue;
902 if (TREE_CODE (op) == SSA_NAME)
903 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
904 else
905 can_split &= !mark_nonssa_use (stmt, op, op, non_ssa_vars);
908 return can_split;
911 /* Stack entry for recursive DFS walk in find_split_point. */
913 typedef struct
915 /* Basic block we are examining. */
916 basic_block bb;
918 /* SSA names set and used by the BB and all BBs reachable
919 from it via DFS walk. */
920 bitmap set_ssa_names, used_ssa_names;
921 bitmap non_ssa_vars;
923 /* All BBS visited from this BB via DFS walk. */
924 bitmap bbs_visited;
926 /* Last examined edge in DFS walk. Since we walk unoriented graph,
927 the value is up to sum of incoming and outgoing edges of BB. */
928 unsigned int edge_num;
930 /* Stack entry index of earliest BB reachable from current BB
931 or any BB visited later in DFS walk. */
932 int earliest;
934 /* Overall time and size of all BBs reached from this BB in DFS walk. */
935 int overall_time, overall_size;
937 /* When false we can not split on this BB. */
938 bool can_split;
939 } stack_entry;
942 /* Find all articulations and call consider_split on them.
943 OVERALL_TIME and OVERALL_SIZE is time and size of the function.
945 We perform basic algorithm for finding an articulation in a graph
946 created from CFG by considering it to be an unoriented graph.
948 The articulation is discovered via DFS walk. We collect earliest
949 basic block on stack that is reachable via backward edge. Articulation
950 is any basic block such that there is no backward edge bypassing it.
951 To reduce stack usage we maintain heap allocated stack in STACK vector.
952 AUX pointer of BB is set to index it appears in the stack or -1 once
953 it is visited and popped off the stack.
955 The algorithm finds articulation after visiting the whole component
956 reachable by it. This makes it convenient to collect information about
957 the component used by consider_split. */
959 static void
960 find_split_points (int overall_time, int overall_size)
962 stack_entry first;
963 vec<stack_entry> stack = vNULL;
964 basic_block bb;
965 basic_block return_bb = find_return_bb ();
966 struct split_point current;
968 current.header_time = overall_time;
969 current.header_size = overall_size;
970 current.split_time = 0;
971 current.split_size = 0;
972 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
974 first.bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
975 first.edge_num = 0;
976 first.overall_time = 0;
977 first.overall_size = 0;
978 first.earliest = INT_MAX;
979 first.set_ssa_names = 0;
980 first.used_ssa_names = 0;
981 first.non_ssa_vars = 0;
982 first.bbs_visited = 0;
983 first.can_split = false;
984 stack.safe_push (first);
985 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = (void *)(intptr_t)-1;
987 while (!stack.is_empty ())
989 stack_entry *entry = &stack.last ();
991 /* We are walking an acyclic graph, so edge_num counts
992 succ and pred edges together. However when considering
993 articulation, we want to have processed everything reachable
994 from articulation but nothing that reaches into it. */
995 if (entry->edge_num == EDGE_COUNT (entry->bb->succs)
996 && entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
998 int pos = stack.length ();
999 entry->can_split &= visit_bb (entry->bb, return_bb,
1000 entry->set_ssa_names,
1001 entry->used_ssa_names,
1002 entry->non_ssa_vars);
1003 if (pos <= entry->earliest && !entry->can_split
1004 && dump_file && (dump_flags & TDF_DETAILS))
1005 fprintf (dump_file,
1006 "found articulation at bb %i but can not split\n",
1007 entry->bb->index);
1008 if (pos <= entry->earliest && entry->can_split)
1010 if (dump_file && (dump_flags & TDF_DETAILS))
1011 fprintf (dump_file, "found articulation at bb %i\n",
1012 entry->bb->index);
1013 current.entry_bb = entry->bb;
1014 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
1015 bitmap_and_compl (current.ssa_names_to_pass,
1016 entry->used_ssa_names, entry->set_ssa_names);
1017 current.header_time = overall_time - entry->overall_time;
1018 current.header_size = overall_size - entry->overall_size;
1019 current.split_time = entry->overall_time;
1020 current.split_size = entry->overall_size;
1021 current.split_bbs = entry->bbs_visited;
1022 consider_split (&current, entry->non_ssa_vars, return_bb);
1023 BITMAP_FREE (current.ssa_names_to_pass);
1026 /* Do actual DFS walk. */
1027 if (entry->edge_num
1028 < (EDGE_COUNT (entry->bb->succs)
1029 + EDGE_COUNT (entry->bb->preds)))
1031 edge e;
1032 basic_block dest;
1033 if (entry->edge_num < EDGE_COUNT (entry->bb->succs))
1035 e = EDGE_SUCC (entry->bb, entry->edge_num);
1036 dest = e->dest;
1038 else
1040 e = EDGE_PRED (entry->bb, entry->edge_num
1041 - EDGE_COUNT (entry->bb->succs));
1042 dest = e->src;
1045 entry->edge_num++;
1047 /* New BB to visit, push it to the stack. */
1048 if (dest != return_bb && dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
1049 && !dest->aux)
1051 stack_entry new_entry;
1053 new_entry.bb = dest;
1054 new_entry.edge_num = 0;
1055 new_entry.overall_time
1056 = bb_info_vec[dest->index].time;
1057 new_entry.overall_size
1058 = bb_info_vec[dest->index].size;
1059 new_entry.earliest = INT_MAX;
1060 new_entry.set_ssa_names = BITMAP_ALLOC (NULL);
1061 new_entry.used_ssa_names = BITMAP_ALLOC (NULL);
1062 new_entry.bbs_visited = BITMAP_ALLOC (NULL);
1063 new_entry.non_ssa_vars = BITMAP_ALLOC (NULL);
1064 new_entry.can_split = true;
1065 bitmap_set_bit (new_entry.bbs_visited, dest->index);
1066 stack.safe_push (new_entry);
1067 dest->aux = (void *)(intptr_t)stack.length ();
1069 /* Back edge found, record the earliest point. */
1070 else if ((intptr_t)dest->aux > 0
1071 && (intptr_t)dest->aux < entry->earliest)
1072 entry->earliest = (intptr_t)dest->aux;
1074 /* We are done with examining the edges. Pop off the value from stack
1075 and merge stuff we accumulate during the walk. */
1076 else if (entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1078 stack_entry *prev = &stack[stack.length () - 2];
1080 entry->bb->aux = (void *)(intptr_t)-1;
1081 prev->can_split &= entry->can_split;
1082 if (prev->set_ssa_names)
1084 bitmap_ior_into (prev->set_ssa_names, entry->set_ssa_names);
1085 bitmap_ior_into (prev->used_ssa_names, entry->used_ssa_names);
1086 bitmap_ior_into (prev->bbs_visited, entry->bbs_visited);
1087 bitmap_ior_into (prev->non_ssa_vars, entry->non_ssa_vars);
1089 if (prev->earliest > entry->earliest)
1090 prev->earliest = entry->earliest;
1091 prev->overall_time += entry->overall_time;
1092 prev->overall_size += entry->overall_size;
1093 BITMAP_FREE (entry->set_ssa_names);
1094 BITMAP_FREE (entry->used_ssa_names);
1095 BITMAP_FREE (entry->bbs_visited);
1096 BITMAP_FREE (entry->non_ssa_vars);
1097 stack.pop ();
1099 else
1100 stack.pop ();
1102 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = NULL;
1103 FOR_EACH_BB_FN (bb, cfun)
1104 bb->aux = NULL;
1105 stack.release ();
1106 BITMAP_FREE (current.ssa_names_to_pass);
1109 /* Split function at SPLIT_POINT. */
1111 static void
1112 split_function (struct split_point *split_point)
1114 vec<tree> args_to_pass = vNULL;
1115 bitmap args_to_skip;
1116 tree parm;
1117 int num = 0;
1118 cgraph_node *node, *cur_node = cgraph_node::get (current_function_decl);
1119 basic_block return_bb = find_return_bb ();
1120 basic_block call_bb;
1121 gimple_stmt_iterator gsi;
1122 gimple call;
1123 edge e;
1124 edge_iterator ei;
1125 tree retval = NULL, real_retval = NULL;
1126 bool split_part_return_p = false;
1127 gimple last_stmt = NULL;
1128 unsigned int i;
1129 tree arg, ddef;
1130 vec<tree, va_gc> **debug_args = NULL;
1132 if (dump_file)
1134 fprintf (dump_file, "\n\nSplitting function at:\n");
1135 dump_split_point (dump_file, split_point);
1138 if (cur_node->local.can_change_signature)
1139 args_to_skip = BITMAP_ALLOC (NULL);
1140 else
1141 args_to_skip = NULL;
1143 /* Collect the parameters of new function and args_to_skip bitmap. */
1144 for (parm = DECL_ARGUMENTS (current_function_decl);
1145 parm; parm = DECL_CHAIN (parm), num++)
1146 if (args_to_skip
1147 && (!is_gimple_reg (parm)
1148 || (ddef = ssa_default_def (cfun, parm)) == NULL_TREE
1149 || !bitmap_bit_p (split_point->ssa_names_to_pass,
1150 SSA_NAME_VERSION (ddef))))
1151 bitmap_set_bit (args_to_skip, num);
1152 else
1154 /* This parm might not have been used up to now, but is going to be
1155 used, hence register it. */
1156 if (is_gimple_reg (parm))
1157 arg = get_or_create_ssa_default_def (cfun, parm);
1158 else
1159 arg = parm;
1161 if (!useless_type_conversion_p (DECL_ARG_TYPE (parm), TREE_TYPE (arg)))
1162 arg = fold_convert (DECL_ARG_TYPE (parm), arg);
1163 args_to_pass.safe_push (arg);
1166 /* See if the split function will return. */
1167 FOR_EACH_EDGE (e, ei, return_bb->preds)
1168 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1169 break;
1170 if (e)
1171 split_part_return_p = true;
1173 /* Add return block to what will become the split function.
1174 We do not return; no return block is needed. */
1175 if (!split_part_return_p)
1177 /* We have no return block, so nothing is needed. */
1178 else if (return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1180 /* When we do not want to return value, we need to construct
1181 new return block with empty return statement.
1182 FIXME: Once we are able to change return type, we should change function
1183 to return void instead of just outputting function with undefined return
1184 value. For structures this affects quality of codegen. */
1185 else if (!split_point->split_part_set_retval
1186 && find_retval (return_bb))
1188 bool redirected = true;
1189 basic_block new_return_bb = create_basic_block (NULL, 0, return_bb);
1190 gimple_stmt_iterator gsi = gsi_start_bb (new_return_bb);
1191 gsi_insert_after (&gsi, gimple_build_return (NULL), GSI_NEW_STMT);
1192 while (redirected)
1194 redirected = false;
1195 FOR_EACH_EDGE (e, ei, return_bb->preds)
1196 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1198 new_return_bb->count += e->count;
1199 new_return_bb->frequency += EDGE_FREQUENCY (e);
1200 redirect_edge_and_branch (e, new_return_bb);
1201 redirected = true;
1202 break;
1205 e = make_edge (new_return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1206 e->probability = REG_BR_PROB_BASE;
1207 e->count = new_return_bb->count;
1208 add_bb_to_loop (new_return_bb, current_loops->tree_root);
1209 bitmap_set_bit (split_point->split_bbs, new_return_bb->index);
1211 /* When we pass around the value, use existing return block. */
1212 else
1213 bitmap_set_bit (split_point->split_bbs, return_bb->index);
1215 /* If RETURN_BB has virtual operand PHIs, they must be removed and the
1216 virtual operand marked for renaming as we change the CFG in a way that
1217 tree-inline is not able to compensate for.
1219 Note this can happen whether or not we have a return value. If we have
1220 a return value, then RETURN_BB may have PHIs for real operands too. */
1221 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1223 bool phi_p = false;
1224 for (gsi = gsi_start_phis (return_bb); !gsi_end_p (gsi);)
1226 gimple stmt = gsi_stmt (gsi);
1227 if (!virtual_operand_p (gimple_phi_result (stmt)))
1229 gsi_next (&gsi);
1230 continue;
1232 mark_virtual_phi_result_for_renaming (stmt);
1233 remove_phi_node (&gsi, true);
1234 phi_p = true;
1236 /* In reality we have to rename the reaching definition of the
1237 virtual operand at return_bb as we will eventually release it
1238 when we remove the code region we outlined.
1239 So we have to rename all immediate virtual uses of that region
1240 if we didn't see a PHI definition yet. */
1241 /* ??? In real reality we want to set the reaching vdef of the
1242 entry of the SESE region as the vuse of the call and the reaching
1243 vdef of the exit of the SESE region as the vdef of the call. */
1244 if (!phi_p)
1245 for (gsi = gsi_start_bb (return_bb); !gsi_end_p (gsi); gsi_next (&gsi))
1247 gimple stmt = gsi_stmt (gsi);
1248 if (gimple_vuse (stmt))
1250 gimple_set_vuse (stmt, NULL_TREE);
1251 update_stmt (stmt);
1253 if (gimple_vdef (stmt))
1254 break;
1258 /* Now create the actual clone. */
1259 cgraph_edge::rebuild_edges ();
1260 node = cur_node->create_version_clone_with_body
1261 (vNULL, NULL, args_to_skip, !split_part_return_p, split_point->split_bbs,
1262 split_point->entry_bb, "part");
1264 /* Let's take a time profile for splitted function. */
1265 node->tp_first_run = cur_node->tp_first_run + 1;
1267 /* For usual cloning it is enough to clear builtin only when signature
1268 changes. For partial inlining we however can not expect the part
1269 of builtin implementation to have same semantic as the whole. */
1270 if (DECL_BUILT_IN (node->decl))
1272 DECL_BUILT_IN_CLASS (node->decl) = NOT_BUILT_IN;
1273 DECL_FUNCTION_CODE (node->decl) = (enum built_in_function) 0;
1275 /* If the original function is declared inline, there is no point in issuing
1276 a warning for the non-inlinable part. */
1277 DECL_NO_INLINE_WARNING_P (node->decl) = 1;
1278 cur_node->remove_callees ();
1279 cur_node->remove_all_references ();
1280 if (!split_part_return_p)
1281 TREE_THIS_VOLATILE (node->decl) = 1;
1282 if (dump_file)
1283 dump_function_to_file (node->decl, dump_file, dump_flags);
1285 /* Create the basic block we place call into. It is the entry basic block
1286 split after last label. */
1287 call_bb = split_point->entry_bb;
1288 for (gsi = gsi_start_bb (call_bb); !gsi_end_p (gsi);)
1289 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
1291 last_stmt = gsi_stmt (gsi);
1292 gsi_next (&gsi);
1294 else
1295 break;
1296 e = split_block (split_point->entry_bb, last_stmt);
1297 remove_edge (e);
1299 /* Produce the call statement. */
1300 gsi = gsi_last_bb (call_bb);
1301 FOR_EACH_VEC_ELT (args_to_pass, i, arg)
1302 if (!is_gimple_val (arg))
1304 arg = force_gimple_operand_gsi (&gsi, arg, true, NULL_TREE,
1305 false, GSI_CONTINUE_LINKING);
1306 args_to_pass[i] = arg;
1308 call = gimple_build_call_vec (node->decl, args_to_pass);
1309 gimple_set_block (call, DECL_INITIAL (current_function_decl));
1310 args_to_pass.release ();
1312 /* For optimized away parameters, add on the caller side
1313 before the call
1314 DEBUG D#X => parm_Y(D)
1315 stmts and associate D#X with parm in decl_debug_args_lookup
1316 vector to say for debug info that if parameter parm had been passed,
1317 it would have value parm_Y(D). */
1318 if (args_to_skip)
1319 for (parm = DECL_ARGUMENTS (current_function_decl), num = 0;
1320 parm; parm = DECL_CHAIN (parm), num++)
1321 if (bitmap_bit_p (args_to_skip, num)
1322 && is_gimple_reg (parm))
1324 tree ddecl;
1325 gimple def_temp;
1327 /* This needs to be done even without MAY_HAVE_DEBUG_STMTS,
1328 otherwise if it didn't exist before, we'd end up with
1329 different SSA_NAME_VERSIONs between -g and -g0. */
1330 arg = get_or_create_ssa_default_def (cfun, parm);
1331 if (!MAY_HAVE_DEBUG_STMTS)
1332 continue;
1334 if (debug_args == NULL)
1335 debug_args = decl_debug_args_insert (node->decl);
1336 ddecl = make_node (DEBUG_EXPR_DECL);
1337 DECL_ARTIFICIAL (ddecl) = 1;
1338 TREE_TYPE (ddecl) = TREE_TYPE (parm);
1339 DECL_MODE (ddecl) = DECL_MODE (parm);
1340 vec_safe_push (*debug_args, DECL_ORIGIN (parm));
1341 vec_safe_push (*debug_args, ddecl);
1342 def_temp = gimple_build_debug_bind (ddecl, unshare_expr (arg),
1343 call);
1344 gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
1346 /* And on the callee side, add
1347 DEBUG D#Y s=> parm
1348 DEBUG var => D#Y
1349 stmts to the first bb where var is a VAR_DECL created for the
1350 optimized away parameter in DECL_INITIAL block. This hints
1351 in the debug info that var (whole DECL_ORIGIN is the parm PARM_DECL)
1352 is optimized away, but could be looked up at the call site
1353 as value of D#X there. */
1354 if (debug_args != NULL)
1356 unsigned int i;
1357 tree var, vexpr;
1358 gimple_stmt_iterator cgsi;
1359 gimple def_temp;
1361 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1362 var = BLOCK_VARS (DECL_INITIAL (node->decl));
1363 i = vec_safe_length (*debug_args);
1364 cgsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1367 i -= 2;
1368 while (var != NULL_TREE
1369 && DECL_ABSTRACT_ORIGIN (var) != (**debug_args)[i])
1370 var = TREE_CHAIN (var);
1371 if (var == NULL_TREE)
1372 break;
1373 vexpr = make_node (DEBUG_EXPR_DECL);
1374 parm = (**debug_args)[i];
1375 DECL_ARTIFICIAL (vexpr) = 1;
1376 TREE_TYPE (vexpr) = TREE_TYPE (parm);
1377 DECL_MODE (vexpr) = DECL_MODE (parm);
1378 def_temp = gimple_build_debug_source_bind (vexpr, parm,
1379 NULL);
1380 gsi_insert_before (&cgsi, def_temp, GSI_SAME_STMT);
1381 def_temp = gimple_build_debug_bind (var, vexpr, NULL);
1382 gsi_insert_before (&cgsi, def_temp, GSI_SAME_STMT);
1384 while (i);
1385 pop_cfun ();
1388 /* We avoid address being taken on any variable used by split part,
1389 so return slot optimization is always possible. Moreover this is
1390 required to make DECL_BY_REFERENCE work. */
1391 if (aggregate_value_p (DECL_RESULT (current_function_decl),
1392 TREE_TYPE (current_function_decl))
1393 && (!is_gimple_reg_type (TREE_TYPE (DECL_RESULT (current_function_decl)))
1394 || DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))))
1395 gimple_call_set_return_slot_opt (call, true);
1397 /* Update return value. This is bit tricky. When we do not return,
1398 do nothing. When we return we might need to update return_bb
1399 or produce a new return statement. */
1400 if (!split_part_return_p)
1401 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1402 else
1404 e = make_edge (call_bb, return_bb,
1405 return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
1406 ? 0 : EDGE_FALLTHRU);
1407 e->count = call_bb->count;
1408 e->probability = REG_BR_PROB_BASE;
1410 /* If there is return basic block, see what value we need to store
1411 return value into and put call just before it. */
1412 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1414 real_retval = retval = find_retval (return_bb);
1416 if (real_retval && split_point->split_part_set_retval)
1418 gimple_stmt_iterator psi;
1420 /* See if we need new SSA_NAME for the result.
1421 When DECL_BY_REFERENCE is true, retval is actually pointer to
1422 return value and it is constant in whole function. */
1423 if (TREE_CODE (retval) == SSA_NAME
1424 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1426 retval = copy_ssa_name (retval, call);
1428 /* See if there is PHI defining return value. */
1429 for (psi = gsi_start_phis (return_bb);
1430 !gsi_end_p (psi); gsi_next (&psi))
1431 if (!virtual_operand_p (gimple_phi_result (gsi_stmt (psi))))
1432 break;
1434 /* When there is PHI, just update its value. */
1435 if (TREE_CODE (retval) == SSA_NAME
1436 && !gsi_end_p (psi))
1437 add_phi_arg (gsi_stmt (psi), retval, e, UNKNOWN_LOCATION);
1438 /* Otherwise update the return BB itself.
1439 find_return_bb allows at most one assignment to return value,
1440 so update first statement. */
1441 else
1443 gimple_stmt_iterator bsi;
1444 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi);
1445 gsi_next (&bsi))
1446 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
1448 gimple_return_set_retval (gsi_stmt (bsi), retval);
1449 break;
1451 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
1452 && !gimple_clobber_p (gsi_stmt (bsi)))
1454 gimple_assign_set_rhs1 (gsi_stmt (bsi), retval);
1455 break;
1457 update_stmt (gsi_stmt (bsi));
1460 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1462 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1463 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1465 else
1467 tree restype;
1468 restype = TREE_TYPE (DECL_RESULT (current_function_decl));
1469 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1470 if (!useless_type_conversion_p (TREE_TYPE (retval), restype))
1472 gimple cpy;
1473 tree tem = create_tmp_reg (restype, NULL);
1474 tem = make_ssa_name (tem, call);
1475 cpy = gimple_build_assign_with_ops (NOP_EXPR, retval,
1476 tem, NULL_TREE);
1477 gsi_insert_after (&gsi, cpy, GSI_NEW_STMT);
1478 retval = tem;
1480 gimple_call_set_lhs (call, retval);
1481 update_stmt (call);
1484 else
1485 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1487 /* We don't use return block (there is either no return in function or
1488 multiple of them). So create new basic block with return statement.
1490 else
1492 gimple ret;
1493 if (split_point->split_part_set_retval
1494 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1496 retval = DECL_RESULT (current_function_decl);
1498 /* We use temporary register to hold value when aggregate_value_p
1499 is false. Similarly for DECL_BY_REFERENCE we must avoid extra
1500 copy. */
1501 if (!aggregate_value_p (retval, TREE_TYPE (current_function_decl))
1502 && !DECL_BY_REFERENCE (retval))
1503 retval = create_tmp_reg (TREE_TYPE (retval), NULL);
1504 if (is_gimple_reg (retval))
1506 /* When returning by reference, there is only one SSA name
1507 assigned to RESULT_DECL (that is pointer to return value).
1508 Look it up or create new one if it is missing. */
1509 if (DECL_BY_REFERENCE (retval))
1510 retval = get_or_create_ssa_default_def (cfun, retval);
1511 /* Otherwise produce new SSA name for return value. */
1512 else
1513 retval = make_ssa_name (retval, call);
1515 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1516 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1517 else
1518 gimple_call_set_lhs (call, retval);
1520 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1521 ret = gimple_build_return (retval);
1522 gsi_insert_after (&gsi, ret, GSI_NEW_STMT);
1525 free_dominance_info (CDI_DOMINATORS);
1526 free_dominance_info (CDI_POST_DOMINATORS);
1527 compute_inline_parameters (node, true);
1530 /* Execute function splitting pass. */
1532 static unsigned int
1533 execute_split_functions (void)
1535 gimple_stmt_iterator bsi;
1536 basic_block bb;
1537 int overall_time = 0, overall_size = 0;
1538 int todo = 0;
1539 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1541 if (flags_from_decl_or_type (current_function_decl)
1542 & (ECF_NORETURN|ECF_MALLOC))
1544 if (dump_file)
1545 fprintf (dump_file, "Not splitting: noreturn/malloc function.\n");
1546 return 0;
1548 if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
1550 if (dump_file)
1551 fprintf (dump_file, "Not splitting: main function.\n");
1552 return 0;
1554 /* This can be relaxed; function might become inlinable after splitting
1555 away the uninlinable part. */
1556 if (inline_edge_summary_vec.exists ()
1557 && !inline_summary (node)->inlinable)
1559 if (dump_file)
1560 fprintf (dump_file, "Not splitting: not inlinable.\n");
1561 return 0;
1563 if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1565 if (dump_file)
1566 fprintf (dump_file, "Not splitting: disregarding inline limits.\n");
1567 return 0;
1569 /* This can be relaxed; most of versioning tests actually prevents
1570 a duplication. */
1571 if (!tree_versionable_function_p (current_function_decl))
1573 if (dump_file)
1574 fprintf (dump_file, "Not splitting: not versionable.\n");
1575 return 0;
1577 /* FIXME: we could support this. */
1578 if (DECL_STRUCT_FUNCTION (current_function_decl)->static_chain_decl)
1580 if (dump_file)
1581 fprintf (dump_file, "Not splitting: nested function.\n");
1582 return 0;
1585 /* See if it makes sense to try to split.
1586 It makes sense to split if we inline, that is if we have direct calls to
1587 handle or direct calls are possibly going to appear as result of indirect
1588 inlining or LTO. Also handle -fprofile-generate as LTO to allow non-LTO
1589 training for LTO -fprofile-use build.
1591 Note that we are not completely conservative about disqualifying functions
1592 called once. It is possible that the caller is called more then once and
1593 then inlining would still benefit. */
1594 if ((!node->callers
1595 /* Local functions called once will be completely inlined most of time. */
1596 || (!node->callers->next_caller && node->local.local))
1597 && !node->address_taken
1598 && (!flag_lto || !node->externally_visible))
1600 if (dump_file)
1601 fprintf (dump_file, "Not splitting: not called directly "
1602 "or called once.\n");
1603 return 0;
1606 /* FIXME: We can actually split if splitting reduces call overhead. */
1607 if (!flag_inline_small_functions
1608 && !DECL_DECLARED_INLINE_P (current_function_decl))
1610 if (dump_file)
1611 fprintf (dump_file, "Not splitting: not autoinlining and function"
1612 " is not inline.\n");
1613 return 0;
1616 /* We enforce splitting after loop headers when profile info is not
1617 available. */
1618 if (profile_status_for_fn (cfun) != PROFILE_READ)
1619 mark_dfs_back_edges ();
1621 /* Initialize bitmap to track forbidden calls. */
1622 forbidden_dominators = BITMAP_ALLOC (NULL);
1623 calculate_dominance_info (CDI_DOMINATORS);
1625 /* Compute local info about basic blocks and determine function size/time. */
1626 bb_info_vec.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
1627 memset (&best_split_point, 0, sizeof (best_split_point));
1628 FOR_EACH_BB_FN (bb, cfun)
1630 int time = 0;
1631 int size = 0;
1632 int freq = compute_call_stmt_bb_frequency (current_function_decl, bb);
1634 if (dump_file && (dump_flags & TDF_DETAILS))
1635 fprintf (dump_file, "Basic block %i\n", bb->index);
1637 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1639 int this_time, this_size;
1640 gimple stmt = gsi_stmt (bsi);
1642 this_size = estimate_num_insns (stmt, &eni_size_weights);
1643 this_time = estimate_num_insns (stmt, &eni_time_weights) * freq;
1644 size += this_size;
1645 time += this_time;
1646 check_forbidden_calls (stmt);
1648 if (dump_file && (dump_flags & TDF_DETAILS))
1650 fprintf (dump_file, " freq:%6i size:%3i time:%3i ",
1651 freq, this_size, this_time);
1652 print_gimple_stmt (dump_file, stmt, 0, 0);
1655 overall_time += time;
1656 overall_size += size;
1657 bb_info_vec[bb->index].time = time;
1658 bb_info_vec[bb->index].size = size;
1660 find_split_points (overall_time, overall_size);
1661 if (best_split_point.split_bbs)
1663 split_function (&best_split_point);
1664 BITMAP_FREE (best_split_point.ssa_names_to_pass);
1665 BITMAP_FREE (best_split_point.split_bbs);
1666 todo = TODO_update_ssa | TODO_cleanup_cfg;
1668 BITMAP_FREE (forbidden_dominators);
1669 bb_info_vec.release ();
1670 return todo;
1673 namespace {
1675 const pass_data pass_data_split_functions =
1677 GIMPLE_PASS, /* type */
1678 "fnsplit", /* name */
1679 OPTGROUP_NONE, /* optinfo_flags */
1680 TV_IPA_FNSPLIT, /* tv_id */
1681 PROP_cfg, /* properties_required */
1682 0, /* properties_provided */
1683 0, /* properties_destroyed */
1684 0, /* todo_flags_start */
1685 0, /* todo_flags_finish */
1688 class pass_split_functions : public gimple_opt_pass
1690 public:
1691 pass_split_functions (gcc::context *ctxt)
1692 : gimple_opt_pass (pass_data_split_functions, ctxt)
1695 /* opt_pass methods: */
1696 virtual bool gate (function *);
1697 virtual unsigned int execute (function *)
1699 return execute_split_functions ();
1702 }; // class pass_split_functions
1704 bool
1705 pass_split_functions::gate (function *)
1707 /* When doing profile feedback, we want to execute the pass after profiling
1708 is read. So disable one in early optimization. */
1709 return (flag_partial_inlining
1710 && !profile_arc_flag && !flag_branch_probabilities);
1713 } // anon namespace
1715 gimple_opt_pass *
1716 make_pass_split_functions (gcc::context *ctxt)
1718 return new pass_split_functions (ctxt);
1721 /* Execute function splitting pass. */
1723 static unsigned int
1724 execute_feedback_split_functions (void)
1726 unsigned int retval = execute_split_functions ();
1727 if (retval)
1728 retval |= TODO_rebuild_cgraph_edges;
1729 return retval;
1732 namespace {
1734 const pass_data pass_data_feedback_split_functions =
1736 GIMPLE_PASS, /* type */
1737 "feedback_fnsplit", /* name */
1738 OPTGROUP_NONE, /* optinfo_flags */
1739 TV_IPA_FNSPLIT, /* tv_id */
1740 PROP_cfg, /* properties_required */
1741 0, /* properties_provided */
1742 0, /* properties_destroyed */
1743 0, /* todo_flags_start */
1744 0, /* todo_flags_finish */
1747 class pass_feedback_split_functions : public gimple_opt_pass
1749 public:
1750 pass_feedback_split_functions (gcc::context *ctxt)
1751 : gimple_opt_pass (pass_data_feedback_split_functions, ctxt)
1754 /* opt_pass methods: */
1755 virtual bool gate (function *);
1756 virtual unsigned int execute (function *)
1758 return execute_feedback_split_functions ();
1761 }; // class pass_feedback_split_functions
1763 bool
1764 pass_feedback_split_functions::gate (function *)
1766 /* We don't need to split when profiling at all, we are producing
1767 lousy code anyway. */
1768 return (flag_partial_inlining
1769 && flag_branch_probabilities);
1772 } // anon namespace
1774 gimple_opt_pass *
1775 make_pass_feedback_split_functions (gcc::context *ctxt)
1777 return new pass_feedback_split_functions (ctxt);