1 /* Function splitting pass
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka <jh@suse.cz>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* The purpose of this pass is to split function bodies to improve
23 inlining. I.e. for function of the form:
48 When func becomes inlinable and when cheap_test is often true, inlining func,
49 but not fund.part leads to performance imrovement similar as inlining
50 original func while the code size growth is smaller.
52 The pass is organized in three stages:
53 1) Collect local info about basic block into BB_INFO structure and
54 compute function body estimated size and time.
55 2) Via DFS walk find all possible basic blocks where we can split
57 3) If split point is found, split at the specified BB by creating a clone
58 and updating function to call it.
60 The decisions what functions to split are in execute_split_functions
63 There are several possible future improvements for this pass including:
65 1) Splitting to break up large functions
66 2) Splitting to reduce stack frame usage
67 3) Allow split part of function to use values computed in the header part.
68 The values needs to be passed to split function, perhaps via same
69 interface as for nested functions or as argument.
70 4) Support for simple rematerialization. I.e. when split part use
71 value computed in header from function parameter in very cheap way, we
72 can just recompute it.
73 5) Support splitting of nested functions.
74 6) Support non-SSA arguments.
75 7) There is nothing preventing us from producing multiple parts of single function
76 when needed or splitting also the parts. */
80 #include "coretypes.h"
85 #include "tree-flow.h"
86 #include "tree-pass.h"
89 #include "diagnostic.h"
90 #include "tree-dump.h"
91 #include "tree-inline.h"
94 #include "gimple-pretty-print.h"
96 /* Per basic block info. */
104 DEF_VEC_ALLOC_O(bb_info,heap);
106 static VEC(bb_info, heap) *bb_info_vec;
108 /* Description of split point. */
112 /* Size of the partitions. */
113 unsigned int header_time, header_size, split_time, split_size;
115 /* SSA names that need to be passed into spit funciton. */
116 bitmap ssa_names_to_pass;
118 /* Basic block where we split (that will become entry point of new function. */
119 basic_block entry_bb;
121 /* Basic blocks we are splitting away. */
125 /* Best split point found. */
127 struct split_point best_split_point;
129 /* Callback for walk_stmt_load_store_addr_ops. If T is non-ssa automatic
130 variable, check it if it is present in bitmap passed via DATA. */
133 test_nonssa_use (gimple stmt ATTRIBUTE_UNUSED, tree t,
134 void *data ATTRIBUTE_UNUSED)
136 t = get_base_address (t);
138 if (t && !is_gimple_reg (t)
139 && ((TREE_CODE (t) == VAR_DECL
140 && auto_var_in_fn_p (t, current_function_decl))
141 || (TREE_CODE (t) == PARM_DECL)))
142 return bitmap_bit_p ((bitmap)data, DECL_UID (t));
146 /* Dump split point CURRENT. */
149 dump_split_point (FILE * file, struct split_point *current)
152 "Split point at BB %i header time:%i header size: %i"
153 " split time: %i split size: %i\n bbs: ",
154 current->entry_bb->index, current->header_time,
155 current->header_size, current->split_time, current->split_size);
156 dump_bitmap (file, current->split_bbs);
157 fprintf (file, " SSA names to pass: ");
158 dump_bitmap (file, current->ssa_names_to_pass);
161 /* We found an split_point CURRENT. NON_SSA_VARS is bitmap of all non ssa
162 variables used and RETURN_BB is return basic block.
163 See if we can split function here. */
166 consider_split (struct split_point *current, bitmap non_ssa_vars,
167 basic_block return_bb)
170 unsigned int num_args = 0;
171 unsigned int call_overhead;
174 gimple_stmt_iterator bsi;
176 int incomming_freq = 0;
178 if (dump_file && (dump_flags & TDF_DETAILS))
179 dump_split_point (dump_file, current);
181 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
182 if (!bitmap_bit_p (current->split_bbs, e->src->index))
183 incomming_freq += EDGE_FREQUENCY (e);
185 /* Do not split when we would end up calling function anyway. */
187 >= (ENTRY_BLOCK_PTR->frequency
188 * PARAM_VALUE (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY) / 100))
190 if (dump_file && (dump_flags & TDF_DETAILS))
192 " Refused: incomming frequency is too large.\n");
196 if (!current->header_size)
198 if (dump_file && (dump_flags & TDF_DETAILS))
199 fprintf (dump_file, " Refused: header empty\n");
204 /* Verify that PHI args on entry are either virutal or all their operands
205 incomming from header are the same. */
206 for (bsi = gsi_start_phis (current->entry_bb); !gsi_end_p (bsi); gsi_next (&bsi))
208 gimple stmt = gsi_stmt (bsi);
211 if (!is_gimple_reg (gimple_phi_result (stmt)))
213 for (i = 0; i < gimple_phi_num_args (stmt); i++)
215 edge e = gimple_phi_arg_edge (stmt, i);
216 if (!bitmap_bit_p (current->split_bbs, e->src->index))
218 tree edge_val = gimple_phi_arg_def (stmt, i);
219 if (val && edge_val != val)
221 if (dump_file && (dump_flags & TDF_DETAILS))
223 " Refused: entry BB has PHI with multiple variants\n");
232 /* See what argument we will pass to the split function and compute
234 call_overhead = eni_size_weights.call_cost;
235 for (parm = DECL_ARGUMENTS (current_function_decl); parm;
236 parm = TREE_CHAIN (parm))
238 if (!is_gimple_reg (parm))
240 if (bitmap_bit_p (non_ssa_vars, DECL_UID (parm)))
242 if (dump_file && (dump_flags & TDF_DETAILS))
244 " Refused: need to pass non-ssa param values\n");
248 else if (gimple_default_def (cfun, parm)
249 && bitmap_bit_p (current->ssa_names_to_pass,
250 SSA_NAME_VERSION (gimple_default_def
253 if (!VOID_TYPE_P (TREE_TYPE (parm)))
254 call_overhead += estimate_move_cost (TREE_TYPE (parm));
258 if (!VOID_TYPE_P (TREE_TYPE (current_function_decl)))
259 call_overhead += estimate_move_cost (TREE_TYPE (current_function_decl));
261 if (current->split_size <= call_overhead)
263 if (dump_file && (dump_flags & TDF_DETAILS))
265 " Refused: split size is smaller than call overhead\n");
268 if (current->header_size + call_overhead
269 >= (unsigned int)(DECL_DECLARED_INLINE_P (current_function_decl)
270 ? MAX_INLINE_INSNS_SINGLE
271 : MAX_INLINE_INSNS_AUTO))
273 if (dump_file && (dump_flags & TDF_DETAILS))
275 " Refused: header size is too large for inline candidate\n");
279 /* FIXME: we currently can pass only SSA function parameters to the split
280 arguments. Once parm_adjustment infrastructure is supported by clonning,
281 we can pass more than that. */
282 if (num_args != bitmap_count_bits (current->ssa_names_to_pass))
285 if (dump_file && (dump_flags & TDF_DETAILS))
287 " Refused: need to pass non-param values\n");
291 /* When there are non-ssa vars used in the split region, see if they
292 are used in the header region. If so, reject the split.
293 FIXME: we can use nested function support to access both. */
294 if (!bitmap_empty_p (non_ssa_vars))
299 gimple_stmt_iterator bsi;
300 if (!bitmap_bit_p (current->split_bbs, bb->index))
302 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
304 if (is_gimple_debug (gsi_stmt (bsi)))
306 if (walk_stmt_load_store_addr_ops
307 (gsi_stmt (bsi), non_ssa_vars, test_nonssa_use,
308 test_nonssa_use, test_nonssa_use))
310 if (dump_file && (dump_flags & TDF_DETAILS))
312 " Refused: split part has non-ssa uses\n");
316 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
318 if (walk_stmt_load_store_addr_ops
319 (gsi_stmt (bsi), non_ssa_vars, test_nonssa_use,
320 test_nonssa_use, test_nonssa_use))
322 if (dump_file && (dump_flags & TDF_DETAILS))
324 " Refused: split part has non-ssa uses\n");
328 FOR_EACH_EDGE (e, ei, bb->succs)
330 if (e->dest != return_bb)
332 for (bsi = gsi_start_phis (return_bb); !gsi_end_p (bsi);
335 gimple stmt = gsi_stmt (bsi);
336 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
338 if (!is_gimple_reg (gimple_phi_result (stmt)))
340 if (TREE_CODE (op) != SSA_NAME
341 && test_nonssa_use (stmt, op, non_ssa_vars))
343 if (dump_file && (dump_flags & TDF_DETAILS))
345 " Refused: split part has non-ssa uses\n");
353 if (dump_file && (dump_flags & TDF_DETAILS))
354 fprintf (dump_file, " Accepted!\n");
356 /* At the moment chose split point with lowest frequency and that leaves
357 out smallest size of header.
358 In future we might re-consider this heuristics. */
359 if (!best_split_point.split_bbs
360 || best_split_point.entry_bb->frequency > current->entry_bb->frequency
361 || (best_split_point.entry_bb->frequency == current->entry_bb->frequency
362 && best_split_point.split_size < current->split_size))
365 if (dump_file && (dump_flags & TDF_DETAILS))
366 fprintf (dump_file, " New best split point!\n");
367 if (best_split_point.ssa_names_to_pass)
369 BITMAP_FREE (best_split_point.ssa_names_to_pass);
370 BITMAP_FREE (best_split_point.split_bbs);
372 best_split_point = *current;
373 best_split_point.ssa_names_to_pass = BITMAP_ALLOC (NULL);
374 bitmap_copy (best_split_point.ssa_names_to_pass,
375 current->ssa_names_to_pass);
376 best_split_point.split_bbs = BITMAP_ALLOC (NULL);
377 bitmap_copy (best_split_point.split_bbs, current->split_bbs);
381 /* Return basic block containing RETURN statement, or EXIT_BLOCK_PTR if none
383 When there are multiple RETURN statement, chose one with return value,
384 since that one is more likely shared by multiple code paths.
385 TODO: We might support multiple return blocks. */
388 find_return_bb (void)
392 basic_block return_bb = EXIT_BLOCK_PTR;
394 if (EDGE_COUNT (EXIT_BLOCK_PTR->preds) == 1)
395 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
397 gimple_stmt_iterator bsi;
398 bool found_return = false;
399 tree retval = NULL_TREE;
401 for (bsi = gsi_start_bb (e->src); !gsi_end_p (bsi); gsi_next (&bsi))
402 if (gimple_code (gsi_stmt (bsi)) != GIMPLE_RETURN
403 && gimple_code (gsi_stmt (bsi)) != GIMPLE_LABEL
404 && !is_gimple_debug (gsi_stmt (bsi)))
406 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
409 retval = gimple_return_retval (gsi_stmt (bsi));
411 if (gsi_end_p (bsi) && found_return)
422 /* Callback for walk_stmt_load_store_addr_ops. If T is non-ssa automatic
423 variable, mark it as used in bitmap passed via DATA.
424 Return true when access to T prevents splitting the function. */
427 mark_nonssa_use (gimple stmt ATTRIBUTE_UNUSED, tree t,
428 void *data ATTRIBUTE_UNUSED)
430 t = get_base_address (t);
432 if (!t || is_gimple_reg (t))
435 /* At present we can't pass non-SSA arguments to split function.
436 FIXME: this can be relaxed by passing references to arguments. */
437 if (TREE_CODE (t) == PARM_DECL)
439 if (dump_file && (dump_flags & TDF_DETAILS))
440 fprintf (dump_file, "Can not split use of non-ssa function parameter.\n");
444 if (TREE_CODE (t) == VAR_DECL && auto_var_in_fn_p (t, current_function_decl))
445 bitmap_set_bit ((bitmap)data, DECL_UID (t));
449 /* Compute local properties of basic block BB we collect when looking for
450 split points. We look for ssa defs and store them in SET_SSA_NAMES,
451 for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic
452 vars stored in NON_SSA_VARS.
454 When BB has edge to RETURN_BB, collect uses in RETURN_BB too.
456 Return false when BB contains something that prevents it from being put into
460 visit_bb (basic_block bb, basic_block return_bb,
461 bitmap set_ssa_names, bitmap used_ssa_names,
464 gimple_stmt_iterator bsi;
467 bool can_split = true;
469 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
471 gimple stmt = gsi_stmt (bsi);
476 if (is_gimple_debug (stmt))
479 /* FIXME: We can split regions containing EH. We can not however
480 split RESX, EH_DISPATCH and EH_POINTER referring to same region
481 into different partitions. This would require tracking of
482 EH regions and checking in consider_split_point if they
483 are not used elsewhere. */
484 if (gimple_code (stmt) == GIMPLE_RESX
485 && stmt_can_throw_external (stmt))
487 if (dump_file && (dump_flags & TDF_DETAILS))
488 fprintf (dump_file, "Can not split external resx.\n");
491 if (gimple_code (stmt) == GIMPLE_EH_DISPATCH)
493 if (dump_file && (dump_flags & TDF_DETAILS))
494 fprintf (dump_file, "Can not split eh dispatch.\n");
498 /* Check builtins that prevent splitting. */
499 if (gimple_code (stmt) == GIMPLE_CALL
500 && (decl = gimple_call_fndecl (stmt)) != NULL_TREE
501 && DECL_BUILT_IN (decl)
502 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
503 switch (DECL_FUNCTION_CODE (decl))
505 /* FIXME: once we will allow passing non-parm values to split part,
506 we need to be sure to handle correct builtin_stack_save and
507 builtin_stack_restore. At the moment we are safe; there is no
508 way to store builtin_stack_save result in non-SSA variable
509 since all calls to those are compiler generated. */
511 case BUILT_IN_VA_START:
512 if (dump_file && (dump_flags & TDF_DETAILS))
513 fprintf (dump_file, "Can not split builtin_apply and va_start.\n");
516 case BUILT_IN_EH_POINTER:
517 if (dump_file && (dump_flags & TDF_DETAILS))
518 fprintf (dump_file, "Can not split builtin_eh_pointer.\n");
525 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
526 bitmap_set_bit (set_ssa_names, SSA_NAME_VERSION (op));
527 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
528 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
529 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
534 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
536 gimple stmt = gsi_stmt (bsi);
539 if (is_gimple_debug (stmt))
541 if (!is_gimple_reg (gimple_phi_result (stmt)))
543 bitmap_set_bit (set_ssa_names,
544 SSA_NAME_VERSION (gimple_phi_result (stmt)));
545 for (i = 0; i < gimple_phi_num_args (stmt); i++)
547 tree op = gimple_phi_arg_def (stmt, i);
548 if (TREE_CODE (op) == SSA_NAME)
549 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
551 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
556 /* Record also uses comming from PHI operand in return BB. */
557 FOR_EACH_EDGE (e, ei, bb->succs)
558 if (e->dest == return_bb)
560 bool found_phi = false;
561 for (bsi = gsi_start_phis (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
563 gimple stmt = gsi_stmt (bsi);
564 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
566 if (is_gimple_debug (stmt))
568 if (!is_gimple_reg (gimple_phi_result (stmt)))
571 if (TREE_CODE (op) == SSA_NAME)
572 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
574 can_split &= !mark_nonssa_use (stmt, op, non_ssa_vars);
576 if (!gsi_end_p (gsi_last_bb (return_bb)))
579 gimple stmt = gsi_stmt (gsi_last_bb (return_bb));
582 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
583 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
584 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
593 /* Stack entry for recursive DFS walk in find_split_point. */
597 /* Basic block we are examining. */
600 /* SSA names set and used by the BB and all BBs reachable
601 from it via DFS walk. */
602 bitmap set_ssa_names, used_ssa_names;
605 /* All BBS visited from this BB via DFS walk. */
608 /* Last examined edge in DFS walk. Since we walk unoriented graph,
609 the value is up to sum of incomming and outgoing edges of BB. */
610 unsigned int edge_num;
612 /* Stack entry index of earliest BB reachable from current BB
613 or any BB visited later in DFS valk. */
616 /* Overall time and size of all BBs reached from this BB in DFS walk. */
617 int overall_time, overall_size;
619 /* When false we can not split on this BB. */
622 DEF_VEC_O(stack_entry);
623 DEF_VEC_ALLOC_O(stack_entry,heap);
626 /* Find all articulations and call consider_split on them.
627 OVERALL_TIME and OVERALL_SIZE is time and size of the function.
629 We perform basic algorithm for finding an articulation in a graph
630 created from CFG by considering it to be an unoriented graph.
632 The articulation is discovered via DFS walk. We collect earliest
633 basic block on stack that is reachable via backward edge. Articulation
634 is any basic block such that there is no backward edge bypassing it.
635 To reduce stack usage we maintain heap allocated stack in STACK vector.
636 AUX pointer of BB is set to index it appears in the stack or -1 once
637 it is visited and popped off the stack.
639 The algorithm finds articulation after visiting the whole component
640 reachable by it. This makes it convenient to collect information about
641 the component used by consider_split. */
644 find_split_points (int overall_time, int overall_size)
647 VEC(stack_entry, heap) *stack = NULL;
649 basic_block return_bb = find_return_bb ();
650 struct split_point current;
652 current.header_time = overall_time;
653 current.header_size = overall_size;
654 current.split_time = 0;
655 current.split_size = 0;
656 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
658 first.bb = ENTRY_BLOCK_PTR;
660 first.overall_time = 0;
661 first.overall_size = 0;
662 first.earliest = INT_MAX;
663 first.set_ssa_names = 0;
664 first.used_ssa_names = 0;
665 first.bbs_visited = 0;
666 VEC_safe_push (stack_entry, heap, stack, &first);
667 ENTRY_BLOCK_PTR->aux = (void *)(intptr_t)-1;
669 while (!VEC_empty (stack_entry, stack))
671 stack_entry *entry = VEC_last (stack_entry, stack);
673 /* We are walking an acyclic graph, so edge_num counts
674 succ and pred edges together. However when considering
675 articulation, we want to have processed everything reachable
676 from articulation but nothing that reaches into it. */
677 if (entry->edge_num == EDGE_COUNT (entry->bb->succs)
678 && entry->bb != ENTRY_BLOCK_PTR)
680 int pos = VEC_length (stack_entry, stack);
681 entry->can_split &= visit_bb (entry->bb, return_bb,
682 entry->set_ssa_names,
683 entry->used_ssa_names,
684 entry->non_ssa_vars);
685 if (pos <= entry->earliest && !entry->can_split
686 && dump_file && (dump_flags & TDF_DETAILS))
688 "found articulation at bb %i but can not split\n",
690 if (pos <= entry->earliest && entry->can_split)
692 if (dump_file && (dump_flags & TDF_DETAILS))
693 fprintf (dump_file, "found articulation at bb %i\n",
695 current.entry_bb = entry->bb;
696 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
697 bitmap_and_compl (current.ssa_names_to_pass,
698 entry->used_ssa_names, entry->set_ssa_names);
699 current.header_time = overall_time - entry->overall_time;
700 current.header_size = overall_size - entry->overall_size;
701 current.split_time = entry->overall_time;
702 current.split_size = entry->overall_size;
703 current.split_bbs = entry->bbs_visited;
704 consider_split (¤t, entry->non_ssa_vars, return_bb);
705 BITMAP_FREE (current.ssa_names_to_pass);
708 /* Do actual DFS walk. */
710 < (EDGE_COUNT (entry->bb->succs)
711 + EDGE_COUNT (entry->bb->preds)))
715 if (entry->edge_num < EDGE_COUNT (entry->bb->succs))
717 e = EDGE_SUCC (entry->bb, entry->edge_num);
722 e = EDGE_PRED (entry->bb, entry->edge_num
723 - EDGE_COUNT (entry->bb->succs));
729 /* New BB to visit, push it to the stack. */
730 if (dest != return_bb && dest != EXIT_BLOCK_PTR
733 stack_entry new_entry;
736 new_entry.edge_num = 0;
737 new_entry.overall_time
738 = VEC_index (bb_info, bb_info_vec, dest->index)->time;
739 new_entry.overall_size
740 = VEC_index (bb_info, bb_info_vec, dest->index)->size;
741 new_entry.earliest = INT_MAX;
742 new_entry.set_ssa_names = BITMAP_ALLOC (NULL);
743 new_entry.used_ssa_names = BITMAP_ALLOC (NULL);
744 new_entry.bbs_visited = BITMAP_ALLOC (NULL);
745 new_entry.non_ssa_vars = BITMAP_ALLOC (NULL);
746 new_entry.can_split = true;
747 bitmap_set_bit (new_entry.bbs_visited, dest->index);
748 VEC_safe_push (stack_entry, heap, stack, &new_entry);
749 dest->aux = (void *)(intptr_t)VEC_length (stack_entry, stack);
751 /* Back edge found, record the earliest point. */
752 else if ((intptr_t)dest->aux > 0
753 && (intptr_t)dest->aux < entry->earliest)
754 entry->earliest = (intptr_t)dest->aux;
756 /* We are done with examing the edges. pop off the value from stack and
757 merge stuff we cummulate during the walk. */
758 else if (entry->bb != ENTRY_BLOCK_PTR)
760 stack_entry *prev = VEC_index (stack_entry, stack,
761 VEC_length (stack_entry, stack) - 2);
763 entry->bb->aux = (void *)(intptr_t)-1;
764 prev->can_split &= entry->can_split;
765 if (prev->set_ssa_names)
767 bitmap_ior_into (prev->set_ssa_names, entry->set_ssa_names);
768 bitmap_ior_into (prev->used_ssa_names, entry->used_ssa_names);
769 bitmap_ior_into (prev->bbs_visited, entry->bbs_visited);
770 bitmap_ior_into (prev->non_ssa_vars, entry->non_ssa_vars);
772 if (prev->earliest > entry->earliest)
773 prev->earliest = entry->earliest;
774 prev->overall_time += entry->overall_time;
775 prev->overall_size += entry->overall_size;
776 BITMAP_FREE (entry->set_ssa_names);
777 BITMAP_FREE (entry->used_ssa_names);
778 BITMAP_FREE (entry->bbs_visited);
779 BITMAP_FREE (entry->non_ssa_vars);
780 VEC_pop (stack_entry, stack);
783 VEC_pop (stack_entry, stack);
785 ENTRY_BLOCK_PTR->aux = NULL;
788 BITMAP_FREE (current.ssa_names_to_pass);
791 /* Split function at SPLIT_POINT. */
794 split_function (struct split_point *split_point)
796 VEC (tree, heap) *args_to_pass = NULL;
797 bitmap args_to_skip = BITMAP_ALLOC (NULL);
800 struct cgraph_node *node;
801 basic_block return_bb = find_return_bb ();
803 gimple_stmt_iterator gsi;
807 tree retval = NULL, real_retval = NULL;
808 bool split_part_return_p = false;
809 gimple last_stmt = NULL;
813 fprintf (dump_file, "\n\nSplitting function at:\n");
814 dump_split_point (dump_file, split_point);
817 /* Collect the parameters of new function and args_to_skip bitmap. */
818 for (parm = DECL_ARGUMENTS (current_function_decl);
819 parm; parm = TREE_CHAIN (parm), num++)
820 if (!is_gimple_reg (parm)
821 || !gimple_default_def (cfun, parm)
822 || !bitmap_bit_p (split_point->ssa_names_to_pass,
823 SSA_NAME_VERSION (gimple_default_def (cfun, parm))))
824 bitmap_set_bit (args_to_skip, num);
826 VEC_safe_push (tree, heap, args_to_pass, gimple_default_def (cfun, parm));
828 /* See if the split function will return. */
829 FOR_EACH_EDGE (e, ei, return_bb->preds)
830 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
833 split_part_return_p = true;
835 /* If we return, we will need the return block. */
836 if (return_bb != EXIT_BLOCK_PTR && split_part_return_p)
837 bitmap_set_bit (split_point->split_bbs, return_bb->index);
839 /* Now create the actual clone. */
840 rebuild_cgraph_edges ();
841 node = cgraph_function_versioning (cgraph_node (current_function_decl),
844 split_point->split_bbs,
845 split_point->entry_bb, "_part");
846 cgraph_node_remove_callees (cgraph_node (current_function_decl));
847 if (!split_part_return_p)
848 TREE_THIS_VOLATILE (node->decl) = 1;
850 dump_function_to_file (node->decl, dump_file, dump_flags);
852 /* Create the basic block we place call into. It is the entry basic block
853 split after last label. */
854 call_bb = split_point->entry_bb;
855 for (gsi = gsi_start_bb (call_bb); !gsi_end_p (gsi);)
856 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
858 last_stmt = gsi_stmt (gsi);
863 e = split_block (split_point->entry_bb, last_stmt);
866 /* Produce the call statement. */
867 gsi = gsi_last_bb (call_bb);
868 call = gimple_build_call_vec (node->decl, args_to_pass);
869 gimple_set_block (call, DECL_INITIAL (current_function_decl));
871 /* Update return value. This is bit tricky. When we do not return,
872 do nothing. When we return we might need to update return_bb
873 or produce a new return statement. */
874 if (!split_part_return_p)
875 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
878 e = make_edge (call_bb, return_bb,
879 return_bb == EXIT_BLOCK_PTR ? 0 : EDGE_FALLTHRU);
880 e->count = call_bb->count;
881 e->probability = REG_BR_PROB_BASE;
882 if (return_bb != EXIT_BLOCK_PTR)
884 gimple return_stmt = gsi_stmt (gsi_last_bb (return_bb));
885 gcc_assert (gimple_code (return_stmt) == GIMPLE_RETURN);
887 if ((real_retval = retval = gimple_return_retval (return_stmt))
888 && !is_gimple_min_invariant (retval)
889 && (TREE_CODE (retval) != SSA_NAME
890 || !SSA_NAME_IS_DEFAULT_DEF (retval)))
892 gimple_stmt_iterator psi;
894 /* See if there is PHI definind return value. */
895 for (psi = gsi_start_phis (return_bb);
896 !gsi_end_p (psi); gsi_next (&psi))
897 if (is_gimple_reg (gimple_phi_result (gsi_stmt (psi))))
900 /* When we have PHI, update PHI. When there is no PHI,
901 update the return statement itself. */
902 if (TREE_CODE (retval) == SSA_NAME)
904 retval = make_ssa_name (SSA_NAME_VAR (retval), call);
905 if (TREE_CODE (retval) == SSA_NAME
907 add_phi_arg (gsi_stmt (psi), retval, e, UNKNOWN_LOCATION);
908 else if (TREE_CODE (retval) == SSA_NAME)
910 gimple_return_set_retval (return_stmt, retval);
911 update_stmt (return_stmt);
914 gimple_call_set_lhs (call, retval);
916 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
921 if (!VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
924 = create_tmp_var (TREE_TYPE (TREE_TYPE (current_function_decl)),
926 if (is_gimple_reg (retval))
927 retval = make_ssa_name (retval, call);
928 gimple_call_set_lhs (call, retval);
930 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
931 ret = gimple_build_return (retval);
932 gsi_insert_after (&gsi, ret, GSI_NEW_STMT);
935 free_dominance_info (CDI_DOMINATORS);
936 free_dominance_info (CDI_POST_DOMINATORS);
937 compute_inline_parameters (node);
940 /* Execute function splitting pass. */
943 execute_split_functions (void)
945 gimple_stmt_iterator bsi;
947 int overall_time = 0, overall_size = 0;
949 struct cgraph_node *node = cgraph_node (current_function_decl);
951 if (flags_from_decl_or_type (current_function_decl) & ECF_NORETURN)
954 fprintf (dump_file, "Not splitting: noreturn function.\n");
957 if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
960 fprintf (dump_file, "Not splitting: main function.\n");
963 /* This can be relaxed; function might become inlinable after splitting
964 away the uninlinable part. */
965 if (!node->local.inlinable)
968 fprintf (dump_file, "Not splitting: not inlinable.\n");
971 if (node->local.disregard_inline_limits)
974 fprintf (dump_file, "Not splitting: disregading inline limits.\n");
977 /* This can be relaxed; most of versioning tests actually prevents
979 if (!tree_versionable_function_p (current_function_decl))
982 fprintf (dump_file, "Not splitting: not versionable.\n");
985 /* FIXME: we could support this. */
986 if (DECL_STRUCT_FUNCTION (current_function_decl)->static_chain_decl)
989 fprintf (dump_file, "Not splitting: nested function.\n");
992 /* FIXME: Should be easy to support. */
993 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
996 fprintf (dump_file, "Not splitting: returns value by reference.\n");
1000 /* See if it makes sense to try to split.
1001 It makes sense to split if we inline, that is if we have direct calls to
1002 handle or direct calls are possibly going to appear as result of indirect
1004 Note that we are not completely conservative about disqualifying functions
1005 called once. It is possible that the caller is called more then once and
1006 then inlining would still benefit. */
1007 if ((!node->callers || !node->callers->next_caller)
1008 && !node->address_taken
1009 && ((!flag_lto && !flag_whopr) || !node->local.externally_visible))
1012 fprintf (dump_file, "Not splitting: not called directly "
1013 "or called once.\n");
1017 /* FIXME: We can actually split if splitting reduces call overhead. */
1018 if (!flag_inline_small_functions
1019 && !DECL_DECLARED_INLINE_P (current_function_decl))
1022 fprintf (dump_file, "Not splitting: not autoinlining and function"
1023 " is not inline.\n");
1027 /* Compute local info about basic blocks and determine function size/time. */
1028 VEC_safe_grow_cleared (bb_info, heap, bb_info_vec, last_basic_block + 1);
1029 memset (&best_split_point, 0, sizeof (best_split_point));
1034 int freq = compute_call_stmt_bb_frequency (current_function_decl, bb);
1036 if (dump_file && (dump_flags & TDF_DETAILS))
1037 fprintf (dump_file, "Basic block %i\n", bb->index);
1039 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1041 int this_time, this_size;
1042 gimple stmt = gsi_stmt (bsi);
1044 this_size = estimate_num_insns (stmt, &eni_size_weights);
1045 this_time = estimate_num_insns (stmt, &eni_time_weights) * freq;
1049 if (dump_file && (dump_flags & TDF_DETAILS))
1051 fprintf (dump_file, " freq:%6i size:%3i time:%3i ",
1052 freq, this_size, this_time);
1053 print_gimple_stmt (dump_file, stmt, 0, 0);
1056 overall_time += time;
1057 overall_size += size;
1058 VEC_index (bb_info, bb_info_vec, bb->index)->time = time;
1059 VEC_index (bb_info, bb_info_vec, bb->index)->size = size;
1061 find_split_points (overall_time, overall_size);
1062 if (best_split_point.split_bbs)
1064 split_function (&best_split_point);
1065 BITMAP_FREE (best_split_point.ssa_names_to_pass);
1066 BITMAP_FREE (best_split_point.split_bbs);
1067 todo = TODO_update_ssa | TODO_cleanup_cfg;
1069 VEC_free (bb_info, heap, bb_info_vec);
1075 gate_split_functions (void)
1077 return flag_partial_inlining;
1080 struct gimple_opt_pass pass_split_functions =
1084 "fnsplit", /* name */
1085 gate_split_functions, /* gate */
1086 execute_split_functions, /* execute */
1089 0, /* static_pass_number */
1090 TV_IPA_FNSPLIT, /* tv_id */
1091 PROP_cfg, /* properties_required */
1092 0, /* properties_provided */
1093 0, /* properties_destroyed */
1094 0, /* todo_flags_start */
1095 TODO_dump_func /* todo_flags_finish */