]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ipa-split.c
* decl.c (decls_match): Assert that the arguments are decls.
[thirdparty/gcc.git] / gcc / ipa-split.c
CommitLineData
3e485f62 1/* Function splitting pass
371556ee 2 Copyright (C) 2010, 2011
3e485f62
JH
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka <jh@suse.cz>
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 3, or (at your option) any later
11version.
12
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
21
22/* The purpose of this pass is to split function bodies to improve
23 inlining. I.e. for function of the form:
24
25 func (...)
26 {
27 if (cheap_test)
28 something_small
29 else
30 something_big
31 }
32
33 Produce:
34
35 func.part (...)
36 {
37 something_big
38 }
39
40 func (...)
41 {
42 if (cheap_test)
43 something_small
44 else
45 func.part (...);
46 }
47
48 When func becomes inlinable and when cheap_test is often true, inlining func,
ed7656f6 49 but not fund.part leads to performance improvement similar as inlining
3e485f62
JH
50 original func while the code size growth is smaller.
51
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
56 and chose best one.
57 3) If split point is found, split at the specified BB by creating a clone
58 and updating function to call it.
59
60 The decisions what functions to split are in execute_split_functions
61 and consider_split.
62
63 There are several possible future improvements for this pass including:
64
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. */
77
78#include "config.h"
79#include "system.h"
80#include "coretypes.h"
81#include "tree.h"
82#include "target.h"
83#include "cgraph.h"
84#include "ipa-prop.h"
85#include "tree-flow.h"
86#include "tree-pass.h"
87#include "flags.h"
88#include "timevar.h"
89#include "diagnostic.h"
90#include "tree-dump.h"
91#include "tree-inline.h"
92#include "fibheap.h"
93#include "params.h"
94#include "gimple-pretty-print.h"
e7f23018 95#include "ipa-inline.h"
3e485f62
JH
96
97/* Per basic block info. */
98
99typedef struct
100{
101 unsigned int size;
102 unsigned int time;
103} bb_info;
104DEF_VEC_O(bb_info);
105DEF_VEC_ALLOC_O(bb_info,heap);
106
107static VEC(bb_info, heap) *bb_info_vec;
108
109/* Description of split point. */
110
111struct split_point
112{
113 /* Size of the partitions. */
114 unsigned int header_time, header_size, split_time, split_size;
115
ed7656f6 116 /* SSA names that need to be passed into spit function. */
3e485f62
JH
117 bitmap ssa_names_to_pass;
118
119 /* Basic block where we split (that will become entry point of new function. */
120 basic_block entry_bb;
121
122 /* Basic blocks we are splitting away. */
123 bitmap split_bbs;
241a2b9e
JH
124
125 /* True when return value is computed on split part and thus it needs
126 to be returned. */
127 bool split_part_set_retval;
3e485f62
JH
128};
129
130/* Best split point found. */
131
132struct split_point best_split_point;
133
241a2b9e
JH
134static tree find_retval (basic_block return_bb);
135
1802378d 136/* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
3e485f62
JH
137 variable, check it if it is present in bitmap passed via DATA. */
138
139static bool
1802378d 140test_nonssa_use (gimple stmt ATTRIBUTE_UNUSED, tree t, void *data)
3e485f62
JH
141{
142 t = get_base_address (t);
143
1802378d
EB
144 if (!t || is_gimple_reg (t))
145 return false;
146
147 if (TREE_CODE (t) == PARM_DECL
148 || (TREE_CODE (t) == VAR_DECL
3e485f62 149 && auto_var_in_fn_p (t, current_function_decl))
1802378d
EB
150 || TREE_CODE (t) == RESULT_DECL
151 || TREE_CODE (t) == LABEL_DECL)
3e485f62 152 return bitmap_bit_p ((bitmap)data, DECL_UID (t));
241a2b9e 153
1802378d
EB
154 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
155 to pretend that the value pointed to is actual result decl. */
156 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
241a2b9e
JH
157 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
158 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
159 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1802378d
EB
160 return
161 bitmap_bit_p ((bitmap)data,
162 DECL_UID (DECL_RESULT (current_function_decl)));
163
3e485f62
JH
164 return false;
165}
166
167/* Dump split point CURRENT. */
168
169static void
170dump_split_point (FILE * file, struct split_point *current)
171{
172 fprintf (file,
cfef45c8
RG
173 "Split point at BB %i\n"
174 " header time: %i header size: %i\n"
175 " split time: %i split size: %i\n bbs: ",
3e485f62
JH
176 current->entry_bb->index, current->header_time,
177 current->header_size, current->split_time, current->split_size);
178 dump_bitmap (file, current->split_bbs);
179 fprintf (file, " SSA names to pass: ");
180 dump_bitmap (file, current->ssa_names_to_pass);
181}
182
1802378d
EB
183/* Look for all BBs in header that might lead to the split part and verify
184 that they are not defining any non-SSA var used by the split part.
2094f1fc
JH
185 Parameters are the same as for consider_split. */
186
187static bool
188verify_non_ssa_vars (struct split_point *current, bitmap non_ssa_vars,
189 basic_block return_bb)
190{
191 bitmap seen = BITMAP_ALLOC (NULL);
192 VEC (basic_block,heap) *worklist = NULL;
193 edge e;
194 edge_iterator ei;
195 bool ok = true;
1802378d 196
2094f1fc
JH
197 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
198 if (e->src != ENTRY_BLOCK_PTR
199 && !bitmap_bit_p (current->split_bbs, e->src->index))
200 {
201 VEC_safe_push (basic_block, heap, worklist, e->src);
202 bitmap_set_bit (seen, e->src->index);
203 }
1802378d 204
2094f1fc
JH
205 while (!VEC_empty (basic_block, worklist))
206 {
207 gimple_stmt_iterator bsi;
208 basic_block bb = VEC_pop (basic_block, worklist);
209
210 FOR_EACH_EDGE (e, ei, bb->preds)
211 if (e->src != ENTRY_BLOCK_PTR
fcaa4ca4 212 && bitmap_set_bit (seen, e->src->index))
2094f1fc
JH
213 {
214 gcc_checking_assert (!bitmap_bit_p (current->split_bbs,
215 e->src->index));
216 VEC_safe_push (basic_block, heap, worklist, e->src);
2094f1fc
JH
217 }
218 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
219 {
1802378d
EB
220 gimple stmt = gsi_stmt (bsi);
221 if (is_gimple_debug (stmt))
2094f1fc
JH
222 continue;
223 if (walk_stmt_load_store_addr_ops
1802378d
EB
224 (stmt, non_ssa_vars, test_nonssa_use, test_nonssa_use,
225 test_nonssa_use))
2094f1fc
JH
226 {
227 ok = false;
228 goto done;
229 }
1802378d
EB
230 if (gimple_code (stmt) == GIMPLE_LABEL
231 && test_nonssa_use (stmt, gimple_label_label (stmt),
232 non_ssa_vars))
233 {
234 ok = false;
235 goto done;
236 }
2094f1fc
JH
237 }
238 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
239 {
240 if (walk_stmt_load_store_addr_ops
1802378d
EB
241 (gsi_stmt (bsi), non_ssa_vars, test_nonssa_use, test_nonssa_use,
242 test_nonssa_use))
2094f1fc
JH
243 {
244 ok = false;
245 goto done;
246 }
247 }
248 FOR_EACH_EDGE (e, ei, bb->succs)
249 {
250 if (e->dest != return_bb)
251 continue;
252 for (bsi = gsi_start_phis (return_bb); !gsi_end_p (bsi);
253 gsi_next (&bsi))
254 {
255 gimple stmt = gsi_stmt (bsi);
256 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
257
258 if (!is_gimple_reg (gimple_phi_result (stmt)))
259 continue;
260 if (TREE_CODE (op) != SSA_NAME
261 && test_nonssa_use (stmt, op, non_ssa_vars))
262 {
263 ok = false;
264 goto done;
265 }
266 }
267 }
268 }
269done:
270 BITMAP_FREE (seen);
271 VEC_free (basic_block, heap, worklist);
272 return ok;
273}
274
3e485f62
JH
275/* We found an split_point CURRENT. NON_SSA_VARS is bitmap of all non ssa
276 variables used and RETURN_BB is return basic block.
277 See if we can split function here. */
278
279static void
280consider_split (struct split_point *current, bitmap non_ssa_vars,
281 basic_block return_bb)
282{
283 tree parm;
284 unsigned int num_args = 0;
285 unsigned int call_overhead;
286 edge e;
287 edge_iterator ei;
8b3057b3
JH
288 gimple_stmt_iterator bsi;
289 unsigned int i;
ed7656f6 290 int incoming_freq = 0;
241a2b9e 291 tree retval;
8b3057b3 292
3e485f62
JH
293 if (dump_file && (dump_flags & TDF_DETAILS))
294 dump_split_point (dump_file, current);
295
8b3057b3
JH
296 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
297 if (!bitmap_bit_p (current->split_bbs, e->src->index))
ed7656f6 298 incoming_freq += EDGE_FREQUENCY (e);
8b3057b3 299
3e485f62 300 /* Do not split when we would end up calling function anyway. */
ed7656f6 301 if (incoming_freq
3e485f62
JH
302 >= (ENTRY_BLOCK_PTR->frequency
303 * PARAM_VALUE (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY) / 100))
304 {
305 if (dump_file && (dump_flags & TDF_DETAILS))
306 fprintf (dump_file,
ed7656f6 307 " Refused: incoming frequency is too large.\n");
3e485f62
JH
308 return;
309 }
310
311 if (!current->header_size)
312 {
313 if (dump_file && (dump_flags & TDF_DETAILS))
314 fprintf (dump_file, " Refused: header empty\n");
3e485f62
JH
315 return;
316 }
317
ed7656f6
JJ
318 /* Verify that PHI args on entry are either virtual or all their operands
319 incoming from header are the same. */
8b3057b3 320 for (bsi = gsi_start_phis (current->entry_bb); !gsi_end_p (bsi); gsi_next (&bsi))
3e485f62 321 {
8b3057b3
JH
322 gimple stmt = gsi_stmt (bsi);
323 tree val = NULL;
324
325 if (!is_gimple_reg (gimple_phi_result (stmt)))
326 continue;
327 for (i = 0; i < gimple_phi_num_args (stmt); i++)
328 {
329 edge e = gimple_phi_arg_edge (stmt, i);
330 if (!bitmap_bit_p (current->split_bbs, e->src->index))
331 {
332 tree edge_val = gimple_phi_arg_def (stmt, i);
333 if (val && edge_val != val)
334 {
335 if (dump_file && (dump_flags & TDF_DETAILS))
336 fprintf (dump_file,
337 " Refused: entry BB has PHI with multiple variants\n");
338 return;
339 }
340 val = edge_val;
341 }
342 }
3e485f62
JH
343 }
344
345
346 /* See what argument we will pass to the split function and compute
347 call overhead. */
348 call_overhead = eni_size_weights.call_cost;
349 for (parm = DECL_ARGUMENTS (current_function_decl); parm;
910ad8de 350 parm = DECL_CHAIN (parm))
3e485f62
JH
351 {
352 if (!is_gimple_reg (parm))
353 {
354 if (bitmap_bit_p (non_ssa_vars, DECL_UID (parm)))
355 {
356 if (dump_file && (dump_flags & TDF_DETAILS))
357 fprintf (dump_file,
358 " Refused: need to pass non-ssa param values\n");
359 return;
360 }
361 }
362 else if (gimple_default_def (cfun, parm)
363 && bitmap_bit_p (current->ssa_names_to_pass,
364 SSA_NAME_VERSION (gimple_default_def
365 (cfun, parm))))
366 {
367 if (!VOID_TYPE_P (TREE_TYPE (parm)))
368 call_overhead += estimate_move_cost (TREE_TYPE (parm));
369 num_args++;
370 }
371 }
372 if (!VOID_TYPE_P (TREE_TYPE (current_function_decl)))
373 call_overhead += estimate_move_cost (TREE_TYPE (current_function_decl));
374
375 if (current->split_size <= call_overhead)
376 {
377 if (dump_file && (dump_flags & TDF_DETAILS))
378 fprintf (dump_file,
379 " Refused: split size is smaller than call overhead\n");
380 return;
381 }
382 if (current->header_size + call_overhead
383 >= (unsigned int)(DECL_DECLARED_INLINE_P (current_function_decl)
384 ? MAX_INLINE_INSNS_SINGLE
385 : MAX_INLINE_INSNS_AUTO))
386 {
387 if (dump_file && (dump_flags & TDF_DETAILS))
388 fprintf (dump_file,
389 " Refused: header size is too large for inline candidate\n");
390 return;
391 }
392
393 /* FIXME: we currently can pass only SSA function parameters to the split
d402c33d 394 arguments. Once parm_adjustment infrastructure is supported by cloning,
3e485f62
JH
395 we can pass more than that. */
396 if (num_args != bitmap_count_bits (current->ssa_names_to_pass))
397 {
8b3057b3 398
3e485f62
JH
399 if (dump_file && (dump_flags & TDF_DETAILS))
400 fprintf (dump_file,
401 " Refused: need to pass non-param values\n");
402 return;
403 }
404
405 /* When there are non-ssa vars used in the split region, see if they
406 are used in the header region. If so, reject the split.
407 FIXME: we can use nested function support to access both. */
2094f1fc
JH
408 if (!bitmap_empty_p (non_ssa_vars)
409 && !verify_non_ssa_vars (current, non_ssa_vars, return_bb))
3e485f62 410 {
2094f1fc
JH
411 if (dump_file && (dump_flags & TDF_DETAILS))
412 fprintf (dump_file,
413 " Refused: split part has non-ssa uses\n");
3e485f62
JH
414 return;
415 }
241a2b9e
JH
416 /* See if retval used by return bb is computed by header or split part.
417 When it is computed by split part, we need to produce return statement
418 in the split part and add code to header to pass it around.
419
420 This is bit tricky to test:
421 1) When there is no return_bb or no return value, we always pass
422 value around.
423 2) Invariants are always computed by caller.
424 3) For SSA we need to look if defining statement is in header or split part
425 4) For non-SSA we need to look where the var is computed. */
426 retval = find_retval (return_bb);
427 if (!retval)
428 current->split_part_set_retval = true;
429 else if (is_gimple_min_invariant (retval))
430 current->split_part_set_retval = false;
431 /* Special case is value returned by reference we record as if it was non-ssa
432 set to result_decl. */
433 else if (TREE_CODE (retval) == SSA_NAME
434 && TREE_CODE (SSA_NAME_VAR (retval)) == RESULT_DECL
435 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
436 current->split_part_set_retval
437 = bitmap_bit_p (non_ssa_vars, DECL_UID (SSA_NAME_VAR (retval)));
438 else if (TREE_CODE (retval) == SSA_NAME)
439 current->split_part_set_retval
440 = (!SSA_NAME_IS_DEFAULT_DEF (retval)
441 && (bitmap_bit_p (current->split_bbs,
442 gimple_bb (SSA_NAME_DEF_STMT (retval))->index)
443 || gimple_bb (SSA_NAME_DEF_STMT (retval)) == return_bb));
444 else if (TREE_CODE (retval) == PARM_DECL)
445 current->split_part_set_retval = false;
446 else if (TREE_CODE (retval) == VAR_DECL
447 || TREE_CODE (retval) == RESULT_DECL)
448 current->split_part_set_retval
449 = bitmap_bit_p (non_ssa_vars, DECL_UID (retval));
450 else
451 current->split_part_set_retval = true;
452
28fc44f3
JJ
453 /* split_function fixes up at most one PHI non-virtual PHI node in return_bb,
454 for the return value. If there are other PHIs, give up. */
455 if (return_bb != EXIT_BLOCK_PTR)
456 {
457 gimple_stmt_iterator psi;
458
459 for (psi = gsi_start_phis (return_bb); !gsi_end_p (psi); gsi_next (&psi))
460 if (is_gimple_reg (gimple_phi_result (gsi_stmt (psi)))
461 && !(retval
462 && current->split_part_set_retval
463 && TREE_CODE (retval) == SSA_NAME
464 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
465 && SSA_NAME_DEF_STMT (retval) == gsi_stmt (psi)))
466 {
467 if (dump_file && (dump_flags & TDF_DETAILS))
468 fprintf (dump_file,
469 " Refused: return bb has extra PHIs\n");
470 return;
471 }
472 }
473
474 if (dump_file && (dump_flags & TDF_DETAILS))
475 fprintf (dump_file, " Accepted!\n");
476
3e485f62
JH
477 /* At the moment chose split point with lowest frequency and that leaves
478 out smallest size of header.
479 In future we might re-consider this heuristics. */
480 if (!best_split_point.split_bbs
481 || best_split_point.entry_bb->frequency > current->entry_bb->frequency
482 || (best_split_point.entry_bb->frequency == current->entry_bb->frequency
483 && best_split_point.split_size < current->split_size))
484
485 {
486 if (dump_file && (dump_flags & TDF_DETAILS))
487 fprintf (dump_file, " New best split point!\n");
488 if (best_split_point.ssa_names_to_pass)
489 {
490 BITMAP_FREE (best_split_point.ssa_names_to_pass);
491 BITMAP_FREE (best_split_point.split_bbs);
492 }
493 best_split_point = *current;
494 best_split_point.ssa_names_to_pass = BITMAP_ALLOC (NULL);
495 bitmap_copy (best_split_point.ssa_names_to_pass,
496 current->ssa_names_to_pass);
497 best_split_point.split_bbs = BITMAP_ALLOC (NULL);
498 bitmap_copy (best_split_point.split_bbs, current->split_bbs);
499 }
500}
501
2094f1fc
JH
502/* Return basic block containing RETURN statement. We allow basic blocks
503 of the form:
504 <retval> = tmp_var;
505 return <retval>
506 but return_bb can not be more complex than this.
507 If nothing is found, return EXIT_BLOCK_PTR.
508
3e485f62
JH
509 When there are multiple RETURN statement, chose one with return value,
510 since that one is more likely shared by multiple code paths.
2094f1fc
JH
511
512 Return BB is special, because for function splitting it is the only
513 basic block that is duplicated in between header and split part of the
514 function.
515
3e485f62
JH
516 TODO: We might support multiple return blocks. */
517
518static basic_block
519find_return_bb (void)
520{
521 edge e;
3e485f62 522 basic_block return_bb = EXIT_BLOCK_PTR;
68457901
JJ
523 gimple_stmt_iterator bsi;
524 bool found_return = false;
525 tree retval = NULL_TREE;
3e485f62 526
68457901
JJ
527 if (!single_pred_p (EXIT_BLOCK_PTR))
528 return return_bb;
529
530 e = single_pred_edge (EXIT_BLOCK_PTR);
531 for (bsi = gsi_last_bb (e->src); !gsi_end_p (bsi); gsi_prev (&bsi))
532 {
533 gimple stmt = gsi_stmt (bsi);
534 if (gimple_code (stmt) == GIMPLE_LABEL || is_gimple_debug (stmt))
535 ;
536 else if (gimple_code (stmt) == GIMPLE_ASSIGN
537 && found_return
538 && gimple_assign_single_p (stmt)
539 && (auto_var_in_fn_p (gimple_assign_rhs1 (stmt),
540 current_function_decl)
541 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
542 && retval == gimple_assign_lhs (stmt))
543 ;
544 else if (gimple_code (stmt) == GIMPLE_RETURN)
545 {
546 found_return = true;
547 retval = gimple_return_retval (stmt);
548 }
549 else
550 break;
551 }
552 if (gsi_end_p (bsi) && found_return)
553 return_bb = e->src;
3e485f62 554
3e485f62
JH
555 return return_bb;
556}
557
ed7656f6 558/* Given return basic block RETURN_BB, see where return value is really
2094f1fc
JH
559 stored. */
560static tree
561find_retval (basic_block return_bb)
562{
563 gimple_stmt_iterator bsi;
564 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
565 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
566 return gimple_return_retval (gsi_stmt (bsi));
567 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN)
568 return gimple_assign_rhs1 (gsi_stmt (bsi));
569 return NULL;
570}
571
1802378d
EB
572/* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
573 variable, mark it as used in bitmap passed via DATA.
3e485f62
JH
574 Return true when access to T prevents splitting the function. */
575
576static bool
1802378d 577mark_nonssa_use (gimple stmt ATTRIBUTE_UNUSED, tree t, void *data)
3e485f62
JH
578{
579 t = get_base_address (t);
580
581 if (!t || is_gimple_reg (t))
582 return false;
583
584 /* At present we can't pass non-SSA arguments to split function.
585 FIXME: this can be relaxed by passing references to arguments. */
586 if (TREE_CODE (t) == PARM_DECL)
587 {
588 if (dump_file && (dump_flags & TDF_DETAILS))
1802378d
EB
589 fprintf (dump_file,
590 "Cannot split: use of non-ssa function parameter.\n");
3e485f62
JH
591 return true;
592 }
593
1802378d
EB
594 if ((TREE_CODE (t) == VAR_DECL
595 && auto_var_in_fn_p (t, current_function_decl))
596 || TREE_CODE (t) == RESULT_DECL
597 || TREE_CODE (t) == LABEL_DECL)
3e485f62 598 bitmap_set_bit ((bitmap)data, DECL_UID (t));
241a2b9e 599
1802378d
EB
600 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
601 to pretend that the value pointed to is actual result decl. */
602 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
241a2b9e
JH
603 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
604 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
605 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1802378d
EB
606 return
607 bitmap_bit_p ((bitmap)data,
608 DECL_UID (DECL_RESULT (current_function_decl)));
609
3e485f62
JH
610 return false;
611}
612
613/* Compute local properties of basic block BB we collect when looking for
614 split points. We look for ssa defs and store them in SET_SSA_NAMES,
615 for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic
616 vars stored in NON_SSA_VARS.
617
618 When BB has edge to RETURN_BB, collect uses in RETURN_BB too.
619
620 Return false when BB contains something that prevents it from being put into
621 split function. */
622
623static bool
624visit_bb (basic_block bb, basic_block return_bb,
625 bitmap set_ssa_names, bitmap used_ssa_names,
626 bitmap non_ssa_vars)
627{
628 gimple_stmt_iterator bsi;
629 edge e;
630 edge_iterator ei;
631 bool can_split = true;
632
633 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
634 {
635 gimple stmt = gsi_stmt (bsi);
636 tree op;
637 ssa_op_iter iter;
638 tree decl;
639
640 if (is_gimple_debug (stmt))
641 continue;
642
643 /* FIXME: We can split regions containing EH. We can not however
644 split RESX, EH_DISPATCH and EH_POINTER referring to same region
645 into different partitions. This would require tracking of
646 EH regions and checking in consider_split_point if they
647 are not used elsewhere. */
1da7d8c0 648 if (gimple_code (stmt) == GIMPLE_RESX)
3e485f62
JH
649 {
650 if (dump_file && (dump_flags & TDF_DETAILS))
1da7d8c0 651 fprintf (dump_file, "Cannot split: resx.\n");
3e485f62
JH
652 can_split = false;
653 }
654 if (gimple_code (stmt) == GIMPLE_EH_DISPATCH)
655 {
656 if (dump_file && (dump_flags & TDF_DETAILS))
1802378d 657 fprintf (dump_file, "Cannot split: eh dispatch.\n");
3e485f62
JH
658 can_split = false;
659 }
660
661 /* Check builtins that prevent splitting. */
662 if (gimple_code (stmt) == GIMPLE_CALL
663 && (decl = gimple_call_fndecl (stmt)) != NULL_TREE
664 && DECL_BUILT_IN (decl)
665 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
666 switch (DECL_FUNCTION_CODE (decl))
667 {
668 /* FIXME: once we will allow passing non-parm values to split part,
669 we need to be sure to handle correct builtin_stack_save and
670 builtin_stack_restore. At the moment we are safe; there is no
671 way to store builtin_stack_save result in non-SSA variable
672 since all calls to those are compiler generated. */
673 case BUILT_IN_APPLY:
61e03ffc 674 case BUILT_IN_APPLY_ARGS:
3e485f62
JH
675 case BUILT_IN_VA_START:
676 if (dump_file && (dump_flags & TDF_DETAILS))
1802378d
EB
677 fprintf (dump_file,
678 "Cannot split: builtin_apply and va_start.\n");
3e485f62
JH
679 can_split = false;
680 break;
681 case BUILT_IN_EH_POINTER:
682 if (dump_file && (dump_flags & TDF_DETAILS))
1802378d 683 fprintf (dump_file, "Cannot split: builtin_eh_pointer.\n");
3e485f62
JH
684 can_split = false;
685 break;
686 default:
687 break;
688 }
689
690 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
691 bitmap_set_bit (set_ssa_names, SSA_NAME_VERSION (op));
692 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
693 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
694 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
695 mark_nonssa_use,
696 mark_nonssa_use,
697 mark_nonssa_use);
698 }
699 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
700 {
701 gimple stmt = gsi_stmt (bsi);
8b3057b3 702 unsigned int i;
3e485f62
JH
703
704 if (is_gimple_debug (stmt))
705 continue;
706 if (!is_gimple_reg (gimple_phi_result (stmt)))
707 continue;
8b3057b3
JH
708 bitmap_set_bit (set_ssa_names,
709 SSA_NAME_VERSION (gimple_phi_result (stmt)));
710 for (i = 0; i < gimple_phi_num_args (stmt); i++)
711 {
712 tree op = gimple_phi_arg_def (stmt, i);
713 if (TREE_CODE (op) == SSA_NAME)
714 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
715 }
3e485f62
JH
716 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
717 mark_nonssa_use,
718 mark_nonssa_use,
719 mark_nonssa_use);
720 }
ed7656f6 721 /* Record also uses coming from PHI operand in return BB. */
3e485f62
JH
722 FOR_EACH_EDGE (e, ei, bb->succs)
723 if (e->dest == return_bb)
724 {
3e485f62
JH
725 for (bsi = gsi_start_phis (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
726 {
727 gimple stmt = gsi_stmt (bsi);
728 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
729
730 if (is_gimple_debug (stmt))
731 continue;
732 if (!is_gimple_reg (gimple_phi_result (stmt)))
733 continue;
3e485f62
JH
734 if (TREE_CODE (op) == SSA_NAME)
735 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
736 else
737 can_split &= !mark_nonssa_use (stmt, op, non_ssa_vars);
738 }
3e485f62
JH
739 }
740 return can_split;
741}
742
743/* Stack entry for recursive DFS walk in find_split_point. */
744
745typedef struct
746{
747 /* Basic block we are examining. */
748 basic_block bb;
749
750 /* SSA names set and used by the BB and all BBs reachable
751 from it via DFS walk. */
752 bitmap set_ssa_names, used_ssa_names;
753 bitmap non_ssa_vars;
754
755 /* All BBS visited from this BB via DFS walk. */
756 bitmap bbs_visited;
757
758 /* Last examined edge in DFS walk. Since we walk unoriented graph,
ed7656f6 759 the value is up to sum of incoming and outgoing edges of BB. */
3e485f62
JH
760 unsigned int edge_num;
761
762 /* Stack entry index of earliest BB reachable from current BB
ed7656f6 763 or any BB visited later in DFS walk. */
3e485f62
JH
764 int earliest;
765
766 /* Overall time and size of all BBs reached from this BB in DFS walk. */
767 int overall_time, overall_size;
768
769 /* When false we can not split on this BB. */
770 bool can_split;
771} stack_entry;
772DEF_VEC_O(stack_entry);
773DEF_VEC_ALLOC_O(stack_entry,heap);
774
775
776/* Find all articulations and call consider_split on them.
777 OVERALL_TIME and OVERALL_SIZE is time and size of the function.
778
779 We perform basic algorithm for finding an articulation in a graph
780 created from CFG by considering it to be an unoriented graph.
781
782 The articulation is discovered via DFS walk. We collect earliest
783 basic block on stack that is reachable via backward edge. Articulation
784 is any basic block such that there is no backward edge bypassing it.
785 To reduce stack usage we maintain heap allocated stack in STACK vector.
786 AUX pointer of BB is set to index it appears in the stack or -1 once
787 it is visited and popped off the stack.
788
789 The algorithm finds articulation after visiting the whole component
790 reachable by it. This makes it convenient to collect information about
791 the component used by consider_split. */
792
793static void
794find_split_points (int overall_time, int overall_size)
795{
796 stack_entry first;
797 VEC(stack_entry, heap) *stack = NULL;
798 basic_block bb;
799 basic_block return_bb = find_return_bb ();
800 struct split_point current;
801
802 current.header_time = overall_time;
803 current.header_size = overall_size;
804 current.split_time = 0;
805 current.split_size = 0;
806 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
807
808 first.bb = ENTRY_BLOCK_PTR;
809 first.edge_num = 0;
810 first.overall_time = 0;
811 first.overall_size = 0;
812 first.earliest = INT_MAX;
813 first.set_ssa_names = 0;
814 first.used_ssa_names = 0;
815 first.bbs_visited = 0;
816 VEC_safe_push (stack_entry, heap, stack, &first);
817 ENTRY_BLOCK_PTR->aux = (void *)(intptr_t)-1;
818
819 while (!VEC_empty (stack_entry, stack))
820 {
821 stack_entry *entry = VEC_last (stack_entry, stack);
822
823 /* We are walking an acyclic graph, so edge_num counts
824 succ and pred edges together. However when considering
825 articulation, we want to have processed everything reachable
826 from articulation but nothing that reaches into it. */
827 if (entry->edge_num == EDGE_COUNT (entry->bb->succs)
828 && entry->bb != ENTRY_BLOCK_PTR)
829 {
830 int pos = VEC_length (stack_entry, stack);
831 entry->can_split &= visit_bb (entry->bb, return_bb,
832 entry->set_ssa_names,
833 entry->used_ssa_names,
834 entry->non_ssa_vars);
835 if (pos <= entry->earliest && !entry->can_split
836 && dump_file && (dump_flags & TDF_DETAILS))
837 fprintf (dump_file,
838 "found articulation at bb %i but can not split\n",
839 entry->bb->index);
840 if (pos <= entry->earliest && entry->can_split)
841 {
842 if (dump_file && (dump_flags & TDF_DETAILS))
843 fprintf (dump_file, "found articulation at bb %i\n",
844 entry->bb->index);
845 current.entry_bb = entry->bb;
846 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
847 bitmap_and_compl (current.ssa_names_to_pass,
848 entry->used_ssa_names, entry->set_ssa_names);
849 current.header_time = overall_time - entry->overall_time;
850 current.header_size = overall_size - entry->overall_size;
851 current.split_time = entry->overall_time;
852 current.split_size = entry->overall_size;
853 current.split_bbs = entry->bbs_visited;
854 consider_split (&current, entry->non_ssa_vars, return_bb);
855 BITMAP_FREE (current.ssa_names_to_pass);
856 }
857 }
858 /* Do actual DFS walk. */
859 if (entry->edge_num
860 < (EDGE_COUNT (entry->bb->succs)
861 + EDGE_COUNT (entry->bb->preds)))
862 {
863 edge e;
864 basic_block dest;
865 if (entry->edge_num < EDGE_COUNT (entry->bb->succs))
866 {
867 e = EDGE_SUCC (entry->bb, entry->edge_num);
868 dest = e->dest;
869 }
870 else
871 {
872 e = EDGE_PRED (entry->bb, entry->edge_num
873 - EDGE_COUNT (entry->bb->succs));
874 dest = e->src;
875 }
876
877 entry->edge_num++;
878
879 /* New BB to visit, push it to the stack. */
880 if (dest != return_bb && dest != EXIT_BLOCK_PTR
881 && !dest->aux)
882 {
883 stack_entry new_entry;
884
885 new_entry.bb = dest;
886 new_entry.edge_num = 0;
887 new_entry.overall_time
888 = VEC_index (bb_info, bb_info_vec, dest->index)->time;
889 new_entry.overall_size
890 = VEC_index (bb_info, bb_info_vec, dest->index)->size;
891 new_entry.earliest = INT_MAX;
892 new_entry.set_ssa_names = BITMAP_ALLOC (NULL);
893 new_entry.used_ssa_names = BITMAP_ALLOC (NULL);
894 new_entry.bbs_visited = BITMAP_ALLOC (NULL);
895 new_entry.non_ssa_vars = BITMAP_ALLOC (NULL);
896 new_entry.can_split = true;
897 bitmap_set_bit (new_entry.bbs_visited, dest->index);
898 VEC_safe_push (stack_entry, heap, stack, &new_entry);
899 dest->aux = (void *)(intptr_t)VEC_length (stack_entry, stack);
900 }
901 /* Back edge found, record the earliest point. */
902 else if ((intptr_t)dest->aux > 0
903 && (intptr_t)dest->aux < entry->earliest)
904 entry->earliest = (intptr_t)dest->aux;
905 }
ed7656f6
JJ
906 /* We are done with examining the edges. Pop off the value from stack
907 and merge stuff we accumulate during the walk. */
3e485f62
JH
908 else if (entry->bb != ENTRY_BLOCK_PTR)
909 {
910 stack_entry *prev = VEC_index (stack_entry, stack,
911 VEC_length (stack_entry, stack) - 2);
912
913 entry->bb->aux = (void *)(intptr_t)-1;
914 prev->can_split &= entry->can_split;
915 if (prev->set_ssa_names)
916 {
917 bitmap_ior_into (prev->set_ssa_names, entry->set_ssa_names);
918 bitmap_ior_into (prev->used_ssa_names, entry->used_ssa_names);
919 bitmap_ior_into (prev->bbs_visited, entry->bbs_visited);
920 bitmap_ior_into (prev->non_ssa_vars, entry->non_ssa_vars);
921 }
922 if (prev->earliest > entry->earliest)
923 prev->earliest = entry->earliest;
924 prev->overall_time += entry->overall_time;
925 prev->overall_size += entry->overall_size;
926 BITMAP_FREE (entry->set_ssa_names);
927 BITMAP_FREE (entry->used_ssa_names);
928 BITMAP_FREE (entry->bbs_visited);
929 BITMAP_FREE (entry->non_ssa_vars);
930 VEC_pop (stack_entry, stack);
931 }
932 else
933 VEC_pop (stack_entry, stack);
934 }
935 ENTRY_BLOCK_PTR->aux = NULL;
936 FOR_EACH_BB (bb)
937 bb->aux = NULL;
ff61e417 938 VEC_free (stack_entry, heap, stack);
3e485f62
JH
939 BITMAP_FREE (current.ssa_names_to_pass);
940}
941
942/* Split function at SPLIT_POINT. */
943
944static void
945split_function (struct split_point *split_point)
946{
947 VEC (tree, heap) *args_to_pass = NULL;
201176d3 948 bitmap args_to_skip;
3e485f62
JH
949 tree parm;
950 int num = 0;
201176d3 951 struct cgraph_node *node, *cur_node = cgraph_get_node (current_function_decl);
3e485f62
JH
952 basic_block return_bb = find_return_bb ();
953 basic_block call_bb;
954 gimple_stmt_iterator gsi;
955 gimple call;
956 edge e;
957 edge_iterator ei;
958 tree retval = NULL, real_retval = NULL;
959 bool split_part_return_p = false;
960 gimple last_stmt = NULL;
371556ee
JJ
961 unsigned int i;
962 tree arg;
3e485f62
JH
963
964 if (dump_file)
965 {
966 fprintf (dump_file, "\n\nSplitting function at:\n");
967 dump_split_point (dump_file, split_point);
968 }
969
201176d3
MJ
970 if (cur_node->local.can_change_signature)
971 args_to_skip = BITMAP_ALLOC (NULL);
972 else
973 args_to_skip = NULL;
974
3e485f62
JH
975 /* Collect the parameters of new function and args_to_skip bitmap. */
976 for (parm = DECL_ARGUMENTS (current_function_decl);
910ad8de 977 parm; parm = DECL_CHAIN (parm), num++)
201176d3
MJ
978 if (args_to_skip
979 && (!is_gimple_reg (parm)
980 || !gimple_default_def (cfun, parm)
981 || !bitmap_bit_p (split_point->ssa_names_to_pass,
982 SSA_NAME_VERSION (gimple_default_def (cfun,
983 parm)))))
3e485f62
JH
984 bitmap_set_bit (args_to_skip, num);
985 else
371556ee 986 {
86814190
MJ
987 /* This parm might not have been used up to now, but is going to be
988 used, hence register it. */
989 add_referenced_var (parm);
990 if (is_gimple_reg (parm))
201176d3 991 {
86814190
MJ
992 arg = gimple_default_def (cfun, parm);
993 if (!arg)
994 {
995 arg = make_ssa_name (parm, gimple_build_nop ());
996 set_default_def (parm, arg);
997 }
201176d3 998 }
86814190
MJ
999 else
1000 arg = parm;
201176d3 1001
2b3c0885
RG
1002 if (!useless_type_conversion_p (DECL_ARG_TYPE (parm), TREE_TYPE (arg)))
1003 arg = fold_convert (DECL_ARG_TYPE (parm), arg);
371556ee
JJ
1004 VEC_safe_push (tree, heap, args_to_pass, arg);
1005 }
3e485f62
JH
1006
1007 /* See if the split function will return. */
1008 FOR_EACH_EDGE (e, ei, return_bb->preds)
1009 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1010 break;
1011 if (e)
1012 split_part_return_p = true;
1013
241a2b9e
JH
1014 /* Add return block to what will become the split function.
1015 We do not return; no return block is needed. */
1016 if (!split_part_return_p)
1017 ;
1018 /* We have no return block, so nothing is needed. */
1019 else if (return_bb == EXIT_BLOCK_PTR)
1020 ;
1021 /* When we do not want to return value, we need to construct
1022 new return block with empty return statement.
1023 FIXME: Once we are able to change return type, we should change function
1024 to return void instead of just outputting function with undefined return
1025 value. For structures this affects quality of codegen. */
1026 else if (!split_point->split_part_set_retval
1027 && find_retval (return_bb))
1028 {
1029 bool redirected = true;
1030 basic_block new_return_bb = create_basic_block (NULL, 0, return_bb);
1031 gimple_stmt_iterator gsi = gsi_start_bb (new_return_bb);
1032 gsi_insert_after (&gsi, gimple_build_return (NULL), GSI_NEW_STMT);
1033 while (redirected)
1034 {
1035 redirected = false;
1036 FOR_EACH_EDGE (e, ei, return_bb->preds)
1037 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1038 {
1039 new_return_bb->count += e->count;
1040 new_return_bb->frequency += EDGE_FREQUENCY (e);
1041 redirect_edge_and_branch (e, new_return_bb);
1042 redirected = true;
1043 break;
1044 }
1045 }
1046 e = make_edge (new_return_bb, EXIT_BLOCK_PTR, 0);
1047 e->probability = REG_BR_PROB_BASE;
1048 e->count = new_return_bb->count;
1049 bitmap_set_bit (split_point->split_bbs, new_return_bb->index);
2e5e346d
JL
1050 }
1051 /* When we pass around the value, use existing return block. */
1052 else
1053 bitmap_set_bit (split_point->split_bbs, return_bb->index);
1054
1055 /* If RETURN_BB has virtual operand PHIs, they must be removed and the
1056 virtual operand marked for renaming as we change the CFG in a way that
cfef45c8 1057 tree-inline is not able to compensate for.
2e5e346d
JL
1058
1059 Note this can happen whether or not we have a return value. If we have
1060 a return value, then RETURN_BB may have PHIs for real operands too. */
1061 if (return_bb != EXIT_BLOCK_PTR)
1062 {
cfef45c8 1063 bool phi_p = false;
241a2b9e
JH
1064 for (gsi = gsi_start_phis (return_bb); !gsi_end_p (gsi);)
1065 {
1066 gimple stmt = gsi_stmt (gsi);
2e5e346d
JL
1067 if (is_gimple_reg (gimple_phi_result (stmt)))
1068 {
1069 gsi_next (&gsi);
1070 continue;
1071 }
6b8c9df8
RG
1072 mark_virtual_phi_result_for_renaming (stmt);
1073 remove_phi_node (&gsi, true);
cfef45c8 1074 phi_p = true;
241a2b9e 1075 }
cfef45c8
RG
1076 /* In reality we have to rename the reaching definition of the
1077 virtual operand at return_bb as we will eventually release it
1078 when we remove the code region we outlined.
1079 So we have to rename all immediate virtual uses of that region
1080 if we didn't see a PHI definition yet. */
1081 /* ??? In real reality we want to set the reaching vdef of the
1082 entry of the SESE region as the vuse of the call and the reaching
1083 vdef of the exit of the SESE region as the vdef of the call. */
1084 if (!phi_p)
1085 for (gsi = gsi_start_bb (return_bb); !gsi_end_p (gsi); gsi_next (&gsi))
1086 {
1087 gimple stmt = gsi_stmt (gsi);
1088 if (gimple_vuse (stmt))
1089 {
1090 gimple_set_vuse (stmt, NULL_TREE);
1091 update_stmt (stmt);
1092 }
1093 if (gimple_vdef (stmt))
1094 break;
1095 }
241a2b9e 1096 }
3e485f62
JH
1097
1098 /* Now create the actual clone. */
1099 rebuild_cgraph_edges ();
201176d3 1100 node = cgraph_function_versioning (cur_node, NULL, NULL, args_to_skip,
3e485f62 1101 split_point->split_bbs,
2094f1fc 1102 split_point->entry_bb, "part");
d402c33d
JH
1103 /* For usual cloning it is enough to clear builtin only when signature
1104 changes. For partial inlining we however can not expect the part
1105 of builtin implementation to have same semantic as the whole. */
1106 if (DECL_BUILT_IN (node->decl))
1107 {
1108 DECL_BUILT_IN_CLASS (node->decl) = NOT_BUILT_IN;
1109 DECL_FUNCTION_CODE (node->decl) = (enum built_in_function) 0;
1110 }
201176d3 1111 cgraph_node_remove_callees (cur_node);
3e485f62
JH
1112 if (!split_part_return_p)
1113 TREE_THIS_VOLATILE (node->decl) = 1;
1114 if (dump_file)
1115 dump_function_to_file (node->decl, dump_file, dump_flags);
1116
1117 /* Create the basic block we place call into. It is the entry basic block
1118 split after last label. */
1119 call_bb = split_point->entry_bb;
1120 for (gsi = gsi_start_bb (call_bb); !gsi_end_p (gsi);)
1121 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
1122 {
1123 last_stmt = gsi_stmt (gsi);
1124 gsi_next (&gsi);
1125 }
1126 else
1127 break;
1128 e = split_block (split_point->entry_bb, last_stmt);
1129 remove_edge (e);
1130
1131 /* Produce the call statement. */
1132 gsi = gsi_last_bb (call_bb);
2b3c0885
RG
1133 FOR_EACH_VEC_ELT (tree, args_to_pass, i, arg)
1134 if (!is_gimple_val (arg))
1135 {
1136 arg = force_gimple_operand_gsi (&gsi, arg, true, NULL_TREE,
f6e52e91 1137 false, GSI_CONTINUE_LINKING);
2b3c0885
RG
1138 VEC_replace (tree, args_to_pass, i, arg);
1139 }
3e485f62
JH
1140 call = gimple_build_call_vec (node->decl, args_to_pass);
1141 gimple_set_block (call, DECL_INITIAL (current_function_decl));
1142
556e9ba0
JH
1143 /* We avoid address being taken on any variable used by split part,
1144 so return slot optimization is always possible. Moreover this is
1145 required to make DECL_BY_REFERENCE work. */
1146 if (aggregate_value_p (DECL_RESULT (current_function_decl),
1147 TREE_TYPE (current_function_decl)))
1148 gimple_call_set_return_slot_opt (call, true);
1149
3e485f62
JH
1150 /* Update return value. This is bit tricky. When we do not return,
1151 do nothing. When we return we might need to update return_bb
1152 or produce a new return statement. */
1153 if (!split_part_return_p)
1154 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1155 else
1156 {
1157 e = make_edge (call_bb, return_bb,
1158 return_bb == EXIT_BLOCK_PTR ? 0 : EDGE_FALLTHRU);
1159 e->count = call_bb->count;
1160 e->probability = REG_BR_PROB_BASE;
6938f93f
JH
1161
1162 /* If there is return basic block, see what value we need to store
1163 return value into and put call just before it. */
3e485f62
JH
1164 if (return_bb != EXIT_BLOCK_PTR)
1165 {
2094f1fc 1166 real_retval = retval = find_retval (return_bb);
6938f93f 1167
241a2b9e 1168 if (real_retval && split_point->split_part_set_retval)
3e485f62
JH
1169 {
1170 gimple_stmt_iterator psi;
1171
6938f93f
JH
1172 /* See if we need new SSA_NAME for the result.
1173 When DECL_BY_REFERENCE is true, retval is actually pointer to
1174 return value and it is constant in whole function. */
1175 if (TREE_CODE (retval) == SSA_NAME
1176 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
3e485f62
JH
1177 {
1178 retval = make_ssa_name (SSA_NAME_VAR (retval), call);
6938f93f
JH
1179
1180 /* See if there is PHI defining return value. */
1181 for (psi = gsi_start_phis (return_bb);
1182 !gsi_end_p (psi); gsi_next (&psi))
1183 if (is_gimple_reg (gimple_phi_result (gsi_stmt (psi))))
1184 break;
1185
1186 /* When there is PHI, just update its value. */
3e485f62
JH
1187 if (TREE_CODE (retval) == SSA_NAME
1188 && !gsi_end_p (psi))
1189 add_phi_arg (gsi_stmt (psi), retval, e, UNKNOWN_LOCATION);
6938f93f
JH
1190 /* Otherwise update the return BB itself.
1191 find_return_bb allows at most one assignment to return value,
1192 so update first statement. */
1193 else
3e485f62 1194 {
2094f1fc
JH
1195 gimple_stmt_iterator bsi;
1196 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi);
1197 gsi_next (&bsi))
1198 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
1199 {
1200 gimple_return_set_retval (gsi_stmt (bsi), retval);
1201 break;
1202 }
1203 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN)
1204 {
1205 gimple_assign_set_rhs1 (gsi_stmt (bsi), retval);
1206 break;
1207 }
1208 update_stmt (gsi_stmt (bsi));
3e485f62
JH
1209 }
1210 }
556e9ba0 1211 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
42b05b6e
RG
1212 {
1213 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1214 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1215 }
556e9ba0 1216 else
42b05b6e
RG
1217 {
1218 tree restype;
1219 restype = TREE_TYPE (DECL_RESULT (current_function_decl));
1220 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1221 if (!useless_type_conversion_p (TREE_TYPE (retval), restype))
1222 {
1223 gimple cpy;
1224 tree tem = create_tmp_reg (restype, NULL);
1225 tem = make_ssa_name (tem, call);
1226 cpy = gimple_build_assign_with_ops (NOP_EXPR, retval,
1227 tem, NULL_TREE);
1228 gsi_insert_after (&gsi, cpy, GSI_NEW_STMT);
1229 retval = tem;
1230 }
1231 gimple_call_set_lhs (call, retval);
1232 update_stmt (call);
1233 }
3e485f62 1234 }
42b05b6e
RG
1235 else
1236 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
3e485f62 1237 }
6938f93f
JH
1238 /* We don't use return block (there is either no return in function or
1239 multiple of them). So create new basic block with return statement.
1240 */
3e485f62
JH
1241 else
1242 {
1243 gimple ret;
241a2b9e
JH
1244 if (split_point->split_part_set_retval
1245 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
3e485f62 1246 {
4021f4a1 1247 retval = DECL_RESULT (current_function_decl);
8a9c1ae6
JH
1248
1249 /* We use temporary register to hold value when aggregate_value_p
1250 is false. Similarly for DECL_BY_REFERENCE we must avoid extra
1251 copy. */
1252 if (!aggregate_value_p (retval, TREE_TYPE (current_function_decl))
1253 && !DECL_BY_REFERENCE (retval))
1254 retval = create_tmp_reg (TREE_TYPE (retval), NULL);
3e485f62 1255 if (is_gimple_reg (retval))
6938f93f
JH
1256 {
1257 /* When returning by reference, there is only one SSA name
1258 assigned to RESULT_DECL (that is pointer to return value).
1259 Look it up or create new one if it is missing. */
1260 if (DECL_BY_REFERENCE (retval))
1261 {
1262 tree retval_name;
1263 if ((retval_name = gimple_default_def (cfun, retval))
1264 != NULL)
1265 retval = retval_name;
1266 else
1267 {
1268 retval_name = make_ssa_name (retval,
1269 gimple_build_nop ());
1270 set_default_def (retval, retval_name);
1271 retval = retval_name;
1272 }
1273 }
1274 /* Otherwise produce new SSA name for return value. */
1275 else
1276 retval = make_ssa_name (retval, call);
1277 }
556e9ba0
JH
1278 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1279 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1280 else
1281 gimple_call_set_lhs (call, retval);
3e485f62
JH
1282 }
1283 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1284 ret = gimple_build_return (retval);
1285 gsi_insert_after (&gsi, ret, GSI_NEW_STMT);
1286 }
1287 }
1288 free_dominance_info (CDI_DOMINATORS);
1289 free_dominance_info (CDI_POST_DOMINATORS);
632b4f8e 1290 compute_inline_parameters (node, true);
3e485f62
JH
1291}
1292
1293/* Execute function splitting pass. */
1294
1295static unsigned int
1296execute_split_functions (void)
1297{
1298 gimple_stmt_iterator bsi;
1299 basic_block bb;
1300 int overall_time = 0, overall_size = 0;
1301 int todo = 0;
581985d7 1302 struct cgraph_node *node = cgraph_get_node (current_function_decl);
3e485f62
JH
1303
1304 if (flags_from_decl_or_type (current_function_decl) & ECF_NORETURN)
1305 {
1306 if (dump_file)
1307 fprintf (dump_file, "Not splitting: noreturn function.\n");
1308 return 0;
1309 }
1310 if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
1311 {
1312 if (dump_file)
1313 fprintf (dump_file, "Not splitting: main function.\n");
1314 return 0;
1315 }
1316 /* This can be relaxed; function might become inlinable after splitting
1317 away the uninlinable part. */
e7f23018 1318 if (!inline_summary (node)->inlinable)
3e485f62
JH
1319 {
1320 if (dump_file)
1321 fprintf (dump_file, "Not splitting: not inlinable.\n");
1322 return 0;
1323 }
e7f23018 1324 if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
3e485f62
JH
1325 {
1326 if (dump_file)
ed7656f6 1327 fprintf (dump_file, "Not splitting: disregarding inline limits.\n");
3e485f62
JH
1328 return 0;
1329 }
1330 /* This can be relaxed; most of versioning tests actually prevents
1331 a duplication. */
1332 if (!tree_versionable_function_p (current_function_decl))
1333 {
1334 if (dump_file)
1335 fprintf (dump_file, "Not splitting: not versionable.\n");
1336 return 0;
1337 }
1338 /* FIXME: we could support this. */
1339 if (DECL_STRUCT_FUNCTION (current_function_decl)->static_chain_decl)
1340 {
1341 if (dump_file)
1342 fprintf (dump_file, "Not splitting: nested function.\n");
1343 return 0;
1344 }
3e485f62
JH
1345
1346 /* See if it makes sense to try to split.
1347 It makes sense to split if we inline, that is if we have direct calls to
1348 handle or direct calls are possibly going to appear as result of indirect
cf9712cc
JH
1349 inlining or LTO. Also handle -fprofile-generate as LTO to allow non-LTO
1350 training for LTO -fprofile-use build.
1351
3e485f62
JH
1352 Note that we are not completely conservative about disqualifying functions
1353 called once. It is possible that the caller is called more then once and
1354 then inlining would still benefit. */
1355 if ((!node->callers || !node->callers->next_caller)
1356 && !node->address_taken
014d92e1 1357 && (!flag_lto || !node->local.externally_visible))
3e485f62
JH
1358 {
1359 if (dump_file)
1360 fprintf (dump_file, "Not splitting: not called directly "
1361 "or called once.\n");
1362 return 0;
1363 }
1364
1365 /* FIXME: We can actually split if splitting reduces call overhead. */
1366 if (!flag_inline_small_functions
1367 && !DECL_DECLARED_INLINE_P (current_function_decl))
1368 {
1369 if (dump_file)
1370 fprintf (dump_file, "Not splitting: not autoinlining and function"
1371 " is not inline.\n");
1372 return 0;
1373 }
1374
1375 /* Compute local info about basic blocks and determine function size/time. */
1376 VEC_safe_grow_cleared (bb_info, heap, bb_info_vec, last_basic_block + 1);
1377 memset (&best_split_point, 0, sizeof (best_split_point));
1378 FOR_EACH_BB (bb)
1379 {
1380 int time = 0;
1381 int size = 0;
1382 int freq = compute_call_stmt_bb_frequency (current_function_decl, bb);
1383
1384 if (dump_file && (dump_flags & TDF_DETAILS))
1385 fprintf (dump_file, "Basic block %i\n", bb->index);
1386
1387 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1388 {
1389 int this_time, this_size;
1390 gimple stmt = gsi_stmt (bsi);
1391
1392 this_size = estimate_num_insns (stmt, &eni_size_weights);
1393 this_time = estimate_num_insns (stmt, &eni_time_weights) * freq;
1394 size += this_size;
1395 time += this_time;
1396
1397 if (dump_file && (dump_flags & TDF_DETAILS))
1398 {
1399 fprintf (dump_file, " freq:%6i size:%3i time:%3i ",
1400 freq, this_size, this_time);
1401 print_gimple_stmt (dump_file, stmt, 0, 0);
1402 }
1403 }
1404 overall_time += time;
1405 overall_size += size;
1406 VEC_index (bb_info, bb_info_vec, bb->index)->time = time;
1407 VEC_index (bb_info, bb_info_vec, bb->index)->size = size;
1408 }
1409 find_split_points (overall_time, overall_size);
1410 if (best_split_point.split_bbs)
1411 {
1412 split_function (&best_split_point);
1413 BITMAP_FREE (best_split_point.ssa_names_to_pass);
1414 BITMAP_FREE (best_split_point.split_bbs);
1415 todo = TODO_update_ssa | TODO_cleanup_cfg;
1416 }
1417 VEC_free (bb_info, heap, bb_info_vec);
1418 bb_info_vec = NULL;
1419 return todo;
1420}
1421
cf9712cc
JH
1422/* Gate function splitting pass. When doing profile feedback, we want
1423 to execute the pass after profiling is read. So disable one in
1424 early optimization. */
1425
3e485f62
JH
1426static bool
1427gate_split_functions (void)
1428{
cf9712cc
JH
1429 return (flag_partial_inlining
1430 && !profile_arc_flag && !flag_branch_probabilities);
3e485f62
JH
1431}
1432
1433struct gimple_opt_pass pass_split_functions =
1434{
1435 {
1436 GIMPLE_PASS,
1437 "fnsplit", /* name */
1438 gate_split_functions, /* gate */
1439 execute_split_functions, /* execute */
1440 NULL, /* sub */
1441 NULL, /* next */
1442 0, /* static_pass_number */
1443 TV_IPA_FNSPLIT, /* tv_id */
1444 PROP_cfg, /* properties_required */
1445 0, /* properties_provided */
1446 0, /* properties_destroyed */
1447 0, /* todo_flags_start */
40b6510f 1448 TODO_verify_all /* todo_flags_finish */
3e485f62
JH
1449 }
1450};
cf9712cc
JH
1451
1452/* Gate feedback driven function splitting pass.
1453 We don't need to split when profiling at all, we are producing
1454 lousy code anyway. */
1455
1456static bool
1457gate_feedback_split_functions (void)
1458{
1459 return (flag_partial_inlining
1460 && flag_branch_probabilities);
1461}
1462
1463/* Execute function splitting pass. */
1464
1465static unsigned int
1466execute_feedback_split_functions (void)
1467{
1468 unsigned int retval = execute_split_functions ();
1469 if (retval)
1470 retval |= TODO_rebuild_cgraph_edges;
1471 return retval;
1472}
1473
1474struct gimple_opt_pass pass_feedback_split_functions =
1475{
1476 {
1477 GIMPLE_PASS,
1478 "feedback_fnsplit", /* name */
1479 gate_feedback_split_functions, /* gate */
1480 execute_feedback_split_functions, /* execute */
1481 NULL, /* sub */
1482 NULL, /* next */
1483 0, /* static_pass_number */
1484 TV_IPA_FNSPLIT, /* tv_id */
1485 PROP_cfg, /* properties_required */
1486 0, /* properties_provided */
1487 0, /* properties_destroyed */
1488 0, /* todo_flags_start */
40b6510f 1489 TODO_verify_all /* todo_flags_finish */
cf9712cc
JH
1490 }
1491};