]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ipa-split.c
New symbol_summary class introduced.
[thirdparty/gcc.git] / gcc / ipa-split.c
CommitLineData
3e485f62 1/* Function splitting pass
23a5b65a 2 Copyright (C) 2010-2014 Free Software Foundation, Inc.
3e485f62
JH
3 Contributed by Jan Hubicka <jh@suse.cz>
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21/* The purpose of this pass is to split function bodies to improve
22 inlining. I.e. for function of the form:
23
24 func (...)
25 {
26 if (cheap_test)
27 something_small
28 else
29 something_big
30 }
31
32 Produce:
33
34 func.part (...)
35 {
36 something_big
37 }
38
39 func (...)
40 {
41 if (cheap_test)
42 something_small
43 else
44 func.part (...);
45 }
46
47 When func becomes inlinable and when cheap_test is often true, inlining func,
ed7656f6 48 but not fund.part leads to performance improvement similar as inlining
3e485f62
JH
49 original func while the code size growth is smaller.
50
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.
58
59 The decisions what functions to split are in execute_split_functions
60 and consider_split.
61
62 There are several possible future improvements for this pass including:
63
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. */
76
77#include "config.h"
78#include "system.h"
79#include "coretypes.h"
80#include "tree.h"
60393bbc
AM
81#include "predict.h"
82#include "vec.h"
83#include "hashtab.h"
84#include "hash-set.h"
85#include "machmode.h"
86#include "tm.h"
87#include "hard-reg-set.h"
88#include "input.h"
89#include "function.h"
90#include "dominance.h"
91#include "cfg.h"
92#include "cfganal.h"
2fb9a547
AM
93#include "basic-block.h"
94#include "tree-ssa-alias.h"
95#include "internal-fn.h"
96#include "gimple-expr.h"
97#include "is-a.h"
18f429e2 98#include "gimple.h"
d8a2d370
DN
99#include "stringpool.h"
100#include "expr.h"
101#include "calls.h"
45b0be94 102#include "gimplify.h"
5be5c238 103#include "gimple-iterator.h"
18f429e2 104#include "gimplify-me.h"
5be5c238 105#include "gimple-walk.h"
3e485f62 106#include "target.h"
c582198b
AM
107#include "hash-map.h"
108#include "plugin-api.h"
109#include "ipa-ref.h"
110#include "cgraph.h"
111#include "alloc-pool.h"
3e485f62 112#include "ipa-prop.h"
442b4905
AM
113#include "gimple-ssa.h"
114#include "tree-cfg.h"
115#include "tree-phinodes.h"
116#include "ssa-iterators.h"
d8a2d370 117#include "stringpool.h"
442b4905
AM
118#include "tree-ssanames.h"
119#include "tree-into-ssa.h"
120#include "tree-dfa.h"
3e485f62
JH
121#include "tree-pass.h"
122#include "flags.h"
3e485f62
JH
123#include "diagnostic.h"
124#include "tree-dump.h"
125#include "tree-inline.h"
3e485f62
JH
126#include "params.h"
127#include "gimple-pretty-print.h"
e7f23018 128#include "ipa-inline.h"
a9e0d843 129#include "cfgloop.h"
d5e254e1 130#include "tree-chkp.h"
3e485f62
JH
131
132/* Per basic block info. */
133
134typedef struct
135{
136 unsigned int size;
137 unsigned int time;
11478306 138} split_bb_info;
3e485f62 139
11478306 140static vec<split_bb_info> bb_info_vec;
3e485f62
JH
141
142/* Description of split point. */
143
144struct split_point
145{
146 /* Size of the partitions. */
147 unsigned int header_time, header_size, split_time, split_size;
148
ed7656f6 149 /* SSA names that need to be passed into spit function. */
3e485f62
JH
150 bitmap ssa_names_to_pass;
151
152 /* Basic block where we split (that will become entry point of new function. */
153 basic_block entry_bb;
154
155 /* Basic blocks we are splitting away. */
156 bitmap split_bbs;
241a2b9e
JH
157
158 /* True when return value is computed on split part and thus it needs
159 to be returned. */
160 bool split_part_set_retval;
3e485f62
JH
161};
162
163/* Best split point found. */
164
165struct split_point best_split_point;
166
b2e25729
BS
167/* Set of basic blocks that are not allowed to dominate a split point. */
168
169static bitmap forbidden_dominators;
170
241a2b9e 171static tree find_retval (basic_block return_bb);
d5e254e1 172static tree find_retbnd (basic_block return_bb);
241a2b9e 173
1802378d 174/* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
3e485f62
JH
175 variable, check it if it is present in bitmap passed via DATA. */
176
177static bool
9f1363cd 178test_nonssa_use (gimple, tree t, tree, void *data)
3e485f62
JH
179{
180 t = get_base_address (t);
181
1802378d
EB
182 if (!t || is_gimple_reg (t))
183 return false;
184
185 if (TREE_CODE (t) == PARM_DECL
186 || (TREE_CODE (t) == VAR_DECL
3e485f62 187 && auto_var_in_fn_p (t, current_function_decl))
1802378d 188 || TREE_CODE (t) == RESULT_DECL
412c4af7
JH
189 /* Normal labels are part of CFG and will be handled gratefuly.
190 Forced labels however can be used directly by statements and
191 need to stay in one partition along with their uses. */
192 || (TREE_CODE (t) == LABEL_DECL
193 && FORCED_LABEL (t)))
3e485f62 194 return bitmap_bit_p ((bitmap)data, DECL_UID (t));
241a2b9e 195
1802378d
EB
196 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
197 to pretend that the value pointed to is actual result decl. */
198 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
241a2b9e 199 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
70b5e7dc 200 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
241a2b9e
JH
201 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
202 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1802378d
EB
203 return
204 bitmap_bit_p ((bitmap)data,
205 DECL_UID (DECL_RESULT (current_function_decl)));
206
3e485f62
JH
207 return false;
208}
209
210/* Dump split point CURRENT. */
211
212static void
213dump_split_point (FILE * file, struct split_point *current)
214{
215 fprintf (file,
cfef45c8
RG
216 "Split point at BB %i\n"
217 " header time: %i header size: %i\n"
218 " split time: %i split size: %i\n bbs: ",
3e485f62
JH
219 current->entry_bb->index, current->header_time,
220 current->header_size, current->split_time, current->split_size);
221 dump_bitmap (file, current->split_bbs);
222 fprintf (file, " SSA names to pass: ");
223 dump_bitmap (file, current->ssa_names_to_pass);
224}
225
1802378d
EB
226/* Look for all BBs in header that might lead to the split part and verify
227 that they are not defining any non-SSA var used by the split part.
2094f1fc
JH
228 Parameters are the same as for consider_split. */
229
230static bool
231verify_non_ssa_vars (struct split_point *current, bitmap non_ssa_vars,
232 basic_block return_bb)
233{
234 bitmap seen = BITMAP_ALLOC (NULL);
6e1aa848 235 vec<basic_block> worklist = vNULL;
2094f1fc
JH
236 edge e;
237 edge_iterator ei;
238 bool ok = true;
412c4af7 239 basic_block bb;
1802378d 240
2094f1fc 241 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
fefa31b5 242 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
2094f1fc
JH
243 && !bitmap_bit_p (current->split_bbs, e->src->index))
244 {
9771b263 245 worklist.safe_push (e->src);
2094f1fc
JH
246 bitmap_set_bit (seen, e->src->index);
247 }
1802378d 248
9771b263 249 while (!worklist.is_empty ())
2094f1fc 250 {
412c4af7 251 bb = worklist.pop ();
2094f1fc 252 FOR_EACH_EDGE (e, ei, bb->preds)
fefa31b5 253 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
fcaa4ca4 254 && bitmap_set_bit (seen, e->src->index))
2094f1fc
JH
255 {
256 gcc_checking_assert (!bitmap_bit_p (current->split_bbs,
257 e->src->index));
9771b263 258 worklist.safe_push (e->src);
2094f1fc 259 }
538dd0b7
DM
260 for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
261 gsi_next (&bsi))
2094f1fc 262 {
1802378d
EB
263 gimple stmt = gsi_stmt (bsi);
264 if (is_gimple_debug (stmt))
2094f1fc
JH
265 continue;
266 if (walk_stmt_load_store_addr_ops
1802378d
EB
267 (stmt, non_ssa_vars, test_nonssa_use, test_nonssa_use,
268 test_nonssa_use))
2094f1fc
JH
269 {
270 ok = false;
271 goto done;
272 }
538dd0b7
DM
273 if (glabel *label_stmt = dyn_cast <glabel *> (stmt))
274 if (test_nonssa_use (stmt, gimple_label_label (label_stmt),
275 NULL_TREE, non_ssa_vars))
276 {
277 ok = false;
278 goto done;
279 }
2094f1fc 280 }
538dd0b7
DM
281 for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
282 gsi_next (&bsi))
2094f1fc
JH
283 {
284 if (walk_stmt_load_store_addr_ops
1802378d
EB
285 (gsi_stmt (bsi), non_ssa_vars, test_nonssa_use, test_nonssa_use,
286 test_nonssa_use))
2094f1fc
JH
287 {
288 ok = false;
289 goto done;
290 }
291 }
292 FOR_EACH_EDGE (e, ei, bb->succs)
293 {
294 if (e->dest != return_bb)
295 continue;
538dd0b7
DM
296 for (gphi_iterator bsi = gsi_start_phis (return_bb);
297 !gsi_end_p (bsi);
2094f1fc
JH
298 gsi_next (&bsi))
299 {
538dd0b7 300 gphi *stmt = bsi.phi ();
2094f1fc
JH
301 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
302
ea057359 303 if (virtual_operand_p (gimple_phi_result (stmt)))
2094f1fc
JH
304 continue;
305 if (TREE_CODE (op) != SSA_NAME
9f1363cd 306 && test_nonssa_use (stmt, op, op, non_ssa_vars))
2094f1fc
JH
307 {
308 ok = false;
309 goto done;
310 }
311 }
312 }
313 }
412c4af7
JH
314
315 /* Verify that the rest of function does not define any label
316 used by the split part. */
317 FOR_EACH_BB_FN (bb, cfun)
318 if (!bitmap_bit_p (current->split_bbs, bb->index)
319 && !bitmap_bit_p (seen, bb->index))
320 {
321 gimple_stmt_iterator bsi;
322 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
538dd0b7 323 if (glabel *label_stmt = dyn_cast <glabel *> (gsi_stmt (bsi)))
412c4af7 324 {
538dd0b7
DM
325 if (test_nonssa_use (label_stmt,
326 gimple_label_label (label_stmt),
327 NULL_TREE, non_ssa_vars))
328 {
329 ok = false;
330 goto done;
331 }
412c4af7 332 }
538dd0b7 333 else
412c4af7
JH
334 break;
335 }
336
2094f1fc
JH
337done:
338 BITMAP_FREE (seen);
9771b263 339 worklist.release ();
2094f1fc
JH
340 return ok;
341}
342
b2e25729
BS
343/* If STMT is a call, check the callee against a list of forbidden
344 predicate functions. If a match is found, look for uses of the
345 call result in condition statements that compare against zero.
346 For each such use, find the block targeted by the condition
347 statement for the nonzero result, and set the bit for this block
348 in the forbidden dominators bitmap. The purpose of this is to avoid
349 selecting a split point where we are likely to lose the chance
350 to optimize away an unused function call. */
351
352static void
353check_forbidden_calls (gimple stmt)
354{
355 imm_use_iterator use_iter;
356 use_operand_p use_p;
357 tree lhs;
358
359 /* At the moment, __builtin_constant_p is the only forbidden
360 predicate function call (see PR49642). */
361 if (!gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P))
362 return;
363
364 lhs = gimple_call_lhs (stmt);
365
366 if (!lhs || TREE_CODE (lhs) != SSA_NAME)
367 return;
368
369 FOR_EACH_IMM_USE_FAST (use_p, use_iter, lhs)
370 {
371 tree op1;
372 basic_block use_bb, forbidden_bb;
373 enum tree_code code;
374 edge true_edge, false_edge;
538dd0b7 375 gcond *use_stmt;
b2e25729 376
538dd0b7
DM
377 use_stmt = dyn_cast <gcond *> (USE_STMT (use_p));
378 if (!use_stmt)
b2e25729
BS
379 continue;
380
381 /* Assuming canonical form for GIMPLE_COND here, with constant
382 in second position. */
383 op1 = gimple_cond_rhs (use_stmt);
384 code = gimple_cond_code (use_stmt);
385 use_bb = gimple_bb (use_stmt);
386
387 extract_true_false_edges_from_block (use_bb, &true_edge, &false_edge);
388
389 /* We're only interested in comparisons that distinguish
390 unambiguously from zero. */
391 if (!integer_zerop (op1) || code == LE_EXPR || code == GE_EXPR)
392 continue;
393
394 if (code == EQ_EXPR)
395 forbidden_bb = false_edge->dest;
396 else
397 forbidden_bb = true_edge->dest;
398
399 bitmap_set_bit (forbidden_dominators, forbidden_bb->index);
400 }
401}
402
403/* If BB is dominated by any block in the forbidden dominators set,
404 return TRUE; else FALSE. */
405
406static bool
407dominated_by_forbidden (basic_block bb)
408{
409 unsigned dom_bb;
410 bitmap_iterator bi;
411
412 EXECUTE_IF_SET_IN_BITMAP (forbidden_dominators, 1, dom_bb, bi)
413 {
06e28de2
DM
414 if (dominated_by_p (CDI_DOMINATORS, bb,
415 BASIC_BLOCK_FOR_FN (cfun, dom_bb)))
b2e25729
BS
416 return true;
417 }
418
419 return false;
420}
421
d5e254e1
IE
422/* For give split point CURRENT and return block RETURN_BB return 1
423 if ssa name VAL is set by split part and 0 otherwise. */
424static bool
425split_part_set_ssa_name_p (tree val, struct split_point *current,
426 basic_block return_bb)
427{
428 if (TREE_CODE (val) != SSA_NAME)
429 return false;
430
431 return (!SSA_NAME_IS_DEFAULT_DEF (val)
432 && (bitmap_bit_p (current->split_bbs,
433 gimple_bb (SSA_NAME_DEF_STMT (val))->index)
434 || gimple_bb (SSA_NAME_DEF_STMT (val)) == return_bb));
435}
436
3e485f62
JH
437/* We found an split_point CURRENT. NON_SSA_VARS is bitmap of all non ssa
438 variables used and RETURN_BB is return basic block.
439 See if we can split function here. */
440
441static void
442consider_split (struct split_point *current, bitmap non_ssa_vars,
443 basic_block return_bb)
444{
445 tree parm;
446 unsigned int num_args = 0;
447 unsigned int call_overhead;
448 edge e;
449 edge_iterator ei;
538dd0b7 450 gphi_iterator bsi;
8b3057b3 451 unsigned int i;
ed7656f6 452 int incoming_freq = 0;
241a2b9e 453 tree retval;
d5e254e1 454 tree retbnd;
e70670cf 455 bool back_edge = false;
8b3057b3 456
3e485f62
JH
457 if (dump_file && (dump_flags & TDF_DETAILS))
458 dump_split_point (dump_file, current);
459
8b3057b3 460 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
e70670cf
JH
461 {
462 if (e->flags & EDGE_DFS_BACK)
463 back_edge = true;
464 if (!bitmap_bit_p (current->split_bbs, e->src->index))
465 incoming_freq += EDGE_FREQUENCY (e);
466 }
8b3057b3 467
3e485f62 468 /* Do not split when we would end up calling function anyway. */
ed7656f6 469 if (incoming_freq
fefa31b5 470 >= (ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency
3e485f62
JH
471 * PARAM_VALUE (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY) / 100))
472 {
e70670cf
JH
473 /* When profile is guessed, we can not expect it to give us
474 realistic estimate on likelyness of function taking the
475 complex path. As a special case, when tail of the function is
476 a loop, enable splitting since inlining code skipping the loop
477 is likely noticeable win. */
478 if (back_edge
0a6a6ac9 479 && profile_status_for_fn (cfun) != PROFILE_READ
fefa31b5 480 && incoming_freq < ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency)
e70670cf
JH
481 {
482 if (dump_file && (dump_flags & TDF_DETAILS))
483 fprintf (dump_file,
484 " Split before loop, accepting despite low frequencies %i %i.\n",
485 incoming_freq,
fefa31b5 486 ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency);
e70670cf
JH
487 }
488 else
489 {
490 if (dump_file && (dump_flags & TDF_DETAILS))
491 fprintf (dump_file,
492 " Refused: incoming frequency is too large.\n");
493 return;
494 }
3e485f62
JH
495 }
496
497 if (!current->header_size)
498 {
499 if (dump_file && (dump_flags & TDF_DETAILS))
500 fprintf (dump_file, " Refused: header empty\n");
3e485f62
JH
501 return;
502 }
503
ed7656f6
JJ
504 /* Verify that PHI args on entry are either virtual or all their operands
505 incoming from header are the same. */
8b3057b3 506 for (bsi = gsi_start_phis (current->entry_bb); !gsi_end_p (bsi); gsi_next (&bsi))
3e485f62 507 {
538dd0b7 508 gphi *stmt = bsi.phi ();
8b3057b3
JH
509 tree val = NULL;
510
ea057359 511 if (virtual_operand_p (gimple_phi_result (stmt)))
8b3057b3
JH
512 continue;
513 for (i = 0; i < gimple_phi_num_args (stmt); i++)
514 {
515 edge e = gimple_phi_arg_edge (stmt, i);
516 if (!bitmap_bit_p (current->split_bbs, e->src->index))
517 {
518 tree edge_val = gimple_phi_arg_def (stmt, i);
519 if (val && edge_val != val)
520 {
521 if (dump_file && (dump_flags & TDF_DETAILS))
522 fprintf (dump_file,
523 " Refused: entry BB has PHI with multiple variants\n");
524 return;
525 }
526 val = edge_val;
527 }
528 }
3e485f62
JH
529 }
530
531
532 /* See what argument we will pass to the split function and compute
533 call overhead. */
534 call_overhead = eni_size_weights.call_cost;
535 for (parm = DECL_ARGUMENTS (current_function_decl); parm;
910ad8de 536 parm = DECL_CHAIN (parm))
3e485f62
JH
537 {
538 if (!is_gimple_reg (parm))
539 {
540 if (bitmap_bit_p (non_ssa_vars, DECL_UID (parm)))
541 {
542 if (dump_file && (dump_flags & TDF_DETAILS))
543 fprintf (dump_file,
544 " Refused: need to pass non-ssa param values\n");
545 return;
546 }
547 }
32244553 548 else
3e485f62 549 {
32244553
RG
550 tree ddef = ssa_default_def (cfun, parm);
551 if (ddef
552 && bitmap_bit_p (current->ssa_names_to_pass,
553 SSA_NAME_VERSION (ddef)))
554 {
555 if (!VOID_TYPE_P (TREE_TYPE (parm)))
b4c9af96 556 call_overhead += estimate_move_cost (TREE_TYPE (parm), false);
32244553
RG
557 num_args++;
558 }
3e485f62
JH
559 }
560 }
561 if (!VOID_TYPE_P (TREE_TYPE (current_function_decl)))
b4c9af96
RB
562 call_overhead += estimate_move_cost (TREE_TYPE (current_function_decl),
563 false);
3e485f62
JH
564
565 if (current->split_size <= call_overhead)
566 {
567 if (dump_file && (dump_flags & TDF_DETAILS))
568 fprintf (dump_file,
569 " Refused: split size is smaller than call overhead\n");
570 return;
571 }
572 if (current->header_size + call_overhead
573 >= (unsigned int)(DECL_DECLARED_INLINE_P (current_function_decl)
574 ? MAX_INLINE_INSNS_SINGLE
575 : MAX_INLINE_INSNS_AUTO))
576 {
577 if (dump_file && (dump_flags & TDF_DETAILS))
578 fprintf (dump_file,
579 " Refused: header size is too large for inline candidate\n");
580 return;
581 }
582
583 /* FIXME: we currently can pass only SSA function parameters to the split
d402c33d 584 arguments. Once parm_adjustment infrastructure is supported by cloning,
3e485f62
JH
585 we can pass more than that. */
586 if (num_args != bitmap_count_bits (current->ssa_names_to_pass))
587 {
8b3057b3 588
3e485f62
JH
589 if (dump_file && (dump_flags & TDF_DETAILS))
590 fprintf (dump_file,
591 " Refused: need to pass non-param values\n");
592 return;
593 }
594
595 /* When there are non-ssa vars used in the split region, see if they
596 are used in the header region. If so, reject the split.
597 FIXME: we can use nested function support to access both. */
2094f1fc
JH
598 if (!bitmap_empty_p (non_ssa_vars)
599 && !verify_non_ssa_vars (current, non_ssa_vars, return_bb))
3e485f62 600 {
2094f1fc
JH
601 if (dump_file && (dump_flags & TDF_DETAILS))
602 fprintf (dump_file,
603 " Refused: split part has non-ssa uses\n");
3e485f62
JH
604 return;
605 }
b2e25729
BS
606
607 /* If the split point is dominated by a forbidden block, reject
608 the split. */
609 if (!bitmap_empty_p (forbidden_dominators)
610 && dominated_by_forbidden (current->entry_bb))
611 {
612 if (dump_file && (dump_flags & TDF_DETAILS))
613 fprintf (dump_file,
614 " Refused: split point dominated by forbidden block\n");
615 return;
616 }
617
241a2b9e
JH
618 /* See if retval used by return bb is computed by header or split part.
619 When it is computed by split part, we need to produce return statement
620 in the split part and add code to header to pass it around.
621
622 This is bit tricky to test:
623 1) When there is no return_bb or no return value, we always pass
624 value around.
625 2) Invariants are always computed by caller.
626 3) For SSA we need to look if defining statement is in header or split part
627 4) For non-SSA we need to look where the var is computed. */
628 retval = find_retval (return_bb);
629 if (!retval)
630 current->split_part_set_retval = true;
631 else if (is_gimple_min_invariant (retval))
632 current->split_part_set_retval = false;
633 /* Special case is value returned by reference we record as if it was non-ssa
634 set to result_decl. */
635 else if (TREE_CODE (retval) == SSA_NAME
70b5e7dc 636 && SSA_NAME_VAR (retval)
241a2b9e
JH
637 && TREE_CODE (SSA_NAME_VAR (retval)) == RESULT_DECL
638 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
639 current->split_part_set_retval
640 = bitmap_bit_p (non_ssa_vars, DECL_UID (SSA_NAME_VAR (retval)));
641 else if (TREE_CODE (retval) == SSA_NAME)
642 current->split_part_set_retval
d5e254e1 643 = split_part_set_ssa_name_p (retval, current, return_bb);
241a2b9e
JH
644 else if (TREE_CODE (retval) == PARM_DECL)
645 current->split_part_set_retval = false;
646 else if (TREE_CODE (retval) == VAR_DECL
647 || TREE_CODE (retval) == RESULT_DECL)
648 current->split_part_set_retval
649 = bitmap_bit_p (non_ssa_vars, DECL_UID (retval));
650 else
651 current->split_part_set_retval = true;
652
d5e254e1
IE
653 /* See if retbnd used by return bb is computed by header or split part. */
654 retbnd = find_retbnd (return_bb);
655 if (retbnd)
656 {
657 bool split_part_set_retbnd
658 = split_part_set_ssa_name_p (retbnd, current, return_bb);
659
660 /* If we have both return value and bounds then keep their definitions
661 in a single function. We use SSA names to link returned bounds and
662 value and therefore do not handle cases when result is passed by
663 reference (which should not be our case anyway since bounds are
664 returned for pointers only). */
665 if ((DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
666 && current->split_part_set_retval)
667 || split_part_set_retbnd != current->split_part_set_retval)
668 {
669 if (dump_file && (dump_flags & TDF_DETAILS))
670 fprintf (dump_file,
671 " Refused: split point splits return value and bounds\n");
672 return;
673 }
674 }
675
28fc44f3
JJ
676 /* split_function fixes up at most one PHI non-virtual PHI node in return_bb,
677 for the return value. If there are other PHIs, give up. */
fefa31b5 678 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
28fc44f3 679 {
538dd0b7 680 gphi_iterator psi;
28fc44f3
JJ
681
682 for (psi = gsi_start_phis (return_bb); !gsi_end_p (psi); gsi_next (&psi))
538dd0b7 683 if (!virtual_operand_p (gimple_phi_result (psi.phi ()))
28fc44f3
JJ
684 && !(retval
685 && current->split_part_set_retval
686 && TREE_CODE (retval) == SSA_NAME
687 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
538dd0b7 688 && SSA_NAME_DEF_STMT (retval) == psi.phi ()))
28fc44f3
JJ
689 {
690 if (dump_file && (dump_flags & TDF_DETAILS))
691 fprintf (dump_file,
692 " Refused: return bb has extra PHIs\n");
693 return;
694 }
695 }
696
697 if (dump_file && (dump_flags & TDF_DETAILS))
698 fprintf (dump_file, " Accepted!\n");
699
3e485f62
JH
700 /* At the moment chose split point with lowest frequency and that leaves
701 out smallest size of header.
702 In future we might re-consider this heuristics. */
703 if (!best_split_point.split_bbs
704 || best_split_point.entry_bb->frequency > current->entry_bb->frequency
705 || (best_split_point.entry_bb->frequency == current->entry_bb->frequency
706 && best_split_point.split_size < current->split_size))
707
708 {
709 if (dump_file && (dump_flags & TDF_DETAILS))
710 fprintf (dump_file, " New best split point!\n");
711 if (best_split_point.ssa_names_to_pass)
712 {
713 BITMAP_FREE (best_split_point.ssa_names_to_pass);
714 BITMAP_FREE (best_split_point.split_bbs);
715 }
716 best_split_point = *current;
717 best_split_point.ssa_names_to_pass = BITMAP_ALLOC (NULL);
718 bitmap_copy (best_split_point.ssa_names_to_pass,
719 current->ssa_names_to_pass);
720 best_split_point.split_bbs = BITMAP_ALLOC (NULL);
721 bitmap_copy (best_split_point.split_bbs, current->split_bbs);
722 }
723}
724
2094f1fc
JH
725/* Return basic block containing RETURN statement. We allow basic blocks
726 of the form:
727 <retval> = tmp_var;
728 return <retval>
729 but return_bb can not be more complex than this.
6626665f 730 If nothing is found, return the exit block.
2094f1fc 731
3e485f62
JH
732 When there are multiple RETURN statement, chose one with return value,
733 since that one is more likely shared by multiple code paths.
2094f1fc
JH
734
735 Return BB is special, because for function splitting it is the only
736 basic block that is duplicated in between header and split part of the
737 function.
738
3e485f62
JH
739 TODO: We might support multiple return blocks. */
740
741static basic_block
742find_return_bb (void)
743{
744 edge e;
fefa31b5 745 basic_block return_bb = EXIT_BLOCK_PTR_FOR_FN (cfun);
68457901
JJ
746 gimple_stmt_iterator bsi;
747 bool found_return = false;
748 tree retval = NULL_TREE;
3e485f62 749
fefa31b5 750 if (!single_pred_p (EXIT_BLOCK_PTR_FOR_FN (cfun)))
68457901
JJ
751 return return_bb;
752
fefa31b5 753 e = single_pred_edge (EXIT_BLOCK_PTR_FOR_FN (cfun));
68457901
JJ
754 for (bsi = gsi_last_bb (e->src); !gsi_end_p (bsi); gsi_prev (&bsi))
755 {
756 gimple stmt = gsi_stmt (bsi);
a348dc7f
JJ
757 if (gimple_code (stmt) == GIMPLE_LABEL
758 || is_gimple_debug (stmt)
759 || gimple_clobber_p (stmt))
68457901
JJ
760 ;
761 else if (gimple_code (stmt) == GIMPLE_ASSIGN
762 && found_return
763 && gimple_assign_single_p (stmt)
764 && (auto_var_in_fn_p (gimple_assign_rhs1 (stmt),
765 current_function_decl)
766 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
767 && retval == gimple_assign_lhs (stmt))
768 ;
538dd0b7 769 else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
68457901
JJ
770 {
771 found_return = true;
538dd0b7 772 retval = gimple_return_retval (return_stmt);
68457901
JJ
773 }
774 else
775 break;
776 }
777 if (gsi_end_p (bsi) && found_return)
778 return_bb = e->src;
3e485f62 779
3e485f62
JH
780 return return_bb;
781}
782
ed7656f6 783/* Given return basic block RETURN_BB, see where return value is really
2094f1fc
JH
784 stored. */
785static tree
786find_retval (basic_block return_bb)
787{
788 gimple_stmt_iterator bsi;
789 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
538dd0b7
DM
790 if (greturn *return_stmt = dyn_cast <greturn *> (gsi_stmt (bsi)))
791 return gimple_return_retval (return_stmt);
a348dc7f
JJ
792 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
793 && !gimple_clobber_p (gsi_stmt (bsi)))
2094f1fc
JH
794 return gimple_assign_rhs1 (gsi_stmt (bsi));
795 return NULL;
796}
797
d5e254e1
IE
798/* Given return basic block RETURN_BB, see where return bounds are really
799 stored. */
800static tree
801find_retbnd (basic_block return_bb)
802{
803 gimple_stmt_iterator bsi;
804 for (bsi = gsi_last_bb (return_bb); !gsi_end_p (bsi); gsi_prev (&bsi))
805 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
806 return gimple_return_retbnd (gsi_stmt (bsi));
807 return NULL;
808}
809
1802378d
EB
810/* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
811 variable, mark it as used in bitmap passed via DATA.
3e485f62
JH
812 Return true when access to T prevents splitting the function. */
813
814static bool
9f1363cd 815mark_nonssa_use (gimple, tree t, tree, void *data)
3e485f62
JH
816{
817 t = get_base_address (t);
818
819 if (!t || is_gimple_reg (t))
820 return false;
821
822 /* At present we can't pass non-SSA arguments to split function.
823 FIXME: this can be relaxed by passing references to arguments. */
824 if (TREE_CODE (t) == PARM_DECL)
825 {
826 if (dump_file && (dump_flags & TDF_DETAILS))
1802378d
EB
827 fprintf (dump_file,
828 "Cannot split: use of non-ssa function parameter.\n");
3e485f62
JH
829 return true;
830 }
831
1802378d
EB
832 if ((TREE_CODE (t) == VAR_DECL
833 && auto_var_in_fn_p (t, current_function_decl))
834 || TREE_CODE (t) == RESULT_DECL
412c4af7
JH
835 || (TREE_CODE (t) == LABEL_DECL
836 && FORCED_LABEL (t)))
3e485f62 837 bitmap_set_bit ((bitmap)data, DECL_UID (t));
241a2b9e 838
1802378d
EB
839 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
840 to pretend that the value pointed to is actual result decl. */
841 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
241a2b9e 842 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
70b5e7dc 843 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
241a2b9e
JH
844 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
845 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1802378d
EB
846 return
847 bitmap_bit_p ((bitmap)data,
848 DECL_UID (DECL_RESULT (current_function_decl)));
849
3e485f62
JH
850 return false;
851}
852
853/* Compute local properties of basic block BB we collect when looking for
854 split points. We look for ssa defs and store them in SET_SSA_NAMES,
855 for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic
856 vars stored in NON_SSA_VARS.
857
858 When BB has edge to RETURN_BB, collect uses in RETURN_BB too.
859
860 Return false when BB contains something that prevents it from being put into
861 split function. */
862
863static bool
864visit_bb (basic_block bb, basic_block return_bb,
865 bitmap set_ssa_names, bitmap used_ssa_names,
866 bitmap non_ssa_vars)
867{
3e485f62
JH
868 edge e;
869 edge_iterator ei;
870 bool can_split = true;
871
538dd0b7
DM
872 for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
873 gsi_next (&bsi))
3e485f62
JH
874 {
875 gimple stmt = gsi_stmt (bsi);
876 tree op;
877 ssa_op_iter iter;
878 tree decl;
879
880 if (is_gimple_debug (stmt))
881 continue;
882
a348dc7f
JJ
883 if (gimple_clobber_p (stmt))
884 continue;
885
3e485f62
JH
886 /* FIXME: We can split regions containing EH. We can not however
887 split RESX, EH_DISPATCH and EH_POINTER referring to same region
888 into different partitions. This would require tracking of
889 EH regions and checking in consider_split_point if they
890 are not used elsewhere. */
1da7d8c0 891 if (gimple_code (stmt) == GIMPLE_RESX)
3e485f62
JH
892 {
893 if (dump_file && (dump_flags & TDF_DETAILS))
1da7d8c0 894 fprintf (dump_file, "Cannot split: resx.\n");
3e485f62
JH
895 can_split = false;
896 }
897 if (gimple_code (stmt) == GIMPLE_EH_DISPATCH)
898 {
899 if (dump_file && (dump_flags & TDF_DETAILS))
1802378d 900 fprintf (dump_file, "Cannot split: eh dispatch.\n");
3e485f62
JH
901 can_split = false;
902 }
903
904 /* Check builtins that prevent splitting. */
905 if (gimple_code (stmt) == GIMPLE_CALL
906 && (decl = gimple_call_fndecl (stmt)) != NULL_TREE
907 && DECL_BUILT_IN (decl)
908 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
909 switch (DECL_FUNCTION_CODE (decl))
910 {
911 /* FIXME: once we will allow passing non-parm values to split part,
912 we need to be sure to handle correct builtin_stack_save and
913 builtin_stack_restore. At the moment we are safe; there is no
914 way to store builtin_stack_save result in non-SSA variable
915 since all calls to those are compiler generated. */
916 case BUILT_IN_APPLY:
61e03ffc 917 case BUILT_IN_APPLY_ARGS:
3e485f62
JH
918 case BUILT_IN_VA_START:
919 if (dump_file && (dump_flags & TDF_DETAILS))
1802378d
EB
920 fprintf (dump_file,
921 "Cannot split: builtin_apply and va_start.\n");
3e485f62
JH
922 can_split = false;
923 break;
924 case BUILT_IN_EH_POINTER:
925 if (dump_file && (dump_flags & TDF_DETAILS))
1802378d 926 fprintf (dump_file, "Cannot split: builtin_eh_pointer.\n");
3e485f62
JH
927 can_split = false;
928 break;
929 default:
930 break;
931 }
932
933 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
934 bitmap_set_bit (set_ssa_names, SSA_NAME_VERSION (op));
935 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
936 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
937 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
938 mark_nonssa_use,
939 mark_nonssa_use,
940 mark_nonssa_use);
941 }
538dd0b7
DM
942 for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
943 gsi_next (&bsi))
3e485f62 944 {
538dd0b7 945 gphi *stmt = bsi.phi ();
8b3057b3 946 unsigned int i;
3e485f62 947
ea057359 948 if (virtual_operand_p (gimple_phi_result (stmt)))
3e485f62 949 continue;
8b3057b3
JH
950 bitmap_set_bit (set_ssa_names,
951 SSA_NAME_VERSION (gimple_phi_result (stmt)));
952 for (i = 0; i < gimple_phi_num_args (stmt); i++)
953 {
954 tree op = gimple_phi_arg_def (stmt, i);
955 if (TREE_CODE (op) == SSA_NAME)
956 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
957 }
3e485f62
JH
958 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
959 mark_nonssa_use,
960 mark_nonssa_use,
961 mark_nonssa_use);
962 }
ed7656f6 963 /* Record also uses coming from PHI operand in return BB. */
3e485f62
JH
964 FOR_EACH_EDGE (e, ei, bb->succs)
965 if (e->dest == return_bb)
966 {
538dd0b7
DM
967 for (gphi_iterator bsi = gsi_start_phis (return_bb);
968 !gsi_end_p (bsi);
969 gsi_next (&bsi))
3e485f62 970 {
538dd0b7 971 gphi *stmt = bsi.phi ();
3e485f62
JH
972 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
973
ea057359 974 if (virtual_operand_p (gimple_phi_result (stmt)))
3e485f62 975 continue;
3e485f62
JH
976 if (TREE_CODE (op) == SSA_NAME)
977 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
978 else
9f1363cd 979 can_split &= !mark_nonssa_use (stmt, op, op, non_ssa_vars);
3e485f62 980 }
3e485f62
JH
981 }
982 return can_split;
983}
984
985/* Stack entry for recursive DFS walk in find_split_point. */
986
987typedef struct
988{
989 /* Basic block we are examining. */
990 basic_block bb;
991
992 /* SSA names set and used by the BB and all BBs reachable
993 from it via DFS walk. */
994 bitmap set_ssa_names, used_ssa_names;
995 bitmap non_ssa_vars;
996
997 /* All BBS visited from this BB via DFS walk. */
998 bitmap bbs_visited;
999
1000 /* Last examined edge in DFS walk. Since we walk unoriented graph,
ed7656f6 1001 the value is up to sum of incoming and outgoing edges of BB. */
3e485f62
JH
1002 unsigned int edge_num;
1003
1004 /* Stack entry index of earliest BB reachable from current BB
ed7656f6 1005 or any BB visited later in DFS walk. */
3e485f62
JH
1006 int earliest;
1007
1008 /* Overall time and size of all BBs reached from this BB in DFS walk. */
1009 int overall_time, overall_size;
1010
1011 /* When false we can not split on this BB. */
1012 bool can_split;
1013} stack_entry;
3e485f62
JH
1014
1015
1016/* Find all articulations and call consider_split on them.
1017 OVERALL_TIME and OVERALL_SIZE is time and size of the function.
1018
1019 We perform basic algorithm for finding an articulation in a graph
1020 created from CFG by considering it to be an unoriented graph.
1021
1022 The articulation is discovered via DFS walk. We collect earliest
1023 basic block on stack that is reachable via backward edge. Articulation
1024 is any basic block such that there is no backward edge bypassing it.
1025 To reduce stack usage we maintain heap allocated stack in STACK vector.
1026 AUX pointer of BB is set to index it appears in the stack or -1 once
1027 it is visited and popped off the stack.
1028
1029 The algorithm finds articulation after visiting the whole component
1030 reachable by it. This makes it convenient to collect information about
1031 the component used by consider_split. */
1032
1033static void
1034find_split_points (int overall_time, int overall_size)
1035{
1036 stack_entry first;
6e1aa848 1037 vec<stack_entry> stack = vNULL;
3e485f62
JH
1038 basic_block bb;
1039 basic_block return_bb = find_return_bb ();
1040 struct split_point current;
1041
1042 current.header_time = overall_time;
1043 current.header_size = overall_size;
1044 current.split_time = 0;
1045 current.split_size = 0;
1046 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
1047
fefa31b5 1048 first.bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
3e485f62
JH
1049 first.edge_num = 0;
1050 first.overall_time = 0;
1051 first.overall_size = 0;
1052 first.earliest = INT_MAX;
1053 first.set_ssa_names = 0;
1054 first.used_ssa_names = 0;
e15eb172 1055 first.non_ssa_vars = 0;
3e485f62 1056 first.bbs_visited = 0;
e15eb172 1057 first.can_split = false;
9771b263 1058 stack.safe_push (first);
fefa31b5 1059 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = (void *)(intptr_t)-1;
3e485f62 1060
9771b263 1061 while (!stack.is_empty ())
3e485f62 1062 {
9771b263 1063 stack_entry *entry = &stack.last ();
3e485f62
JH
1064
1065 /* We are walking an acyclic graph, so edge_num counts
1066 succ and pred edges together. However when considering
1067 articulation, we want to have processed everything reachable
1068 from articulation but nothing that reaches into it. */
1069 if (entry->edge_num == EDGE_COUNT (entry->bb->succs)
fefa31b5 1070 && entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
3e485f62 1071 {
9771b263 1072 int pos = stack.length ();
3e485f62
JH
1073 entry->can_split &= visit_bb (entry->bb, return_bb,
1074 entry->set_ssa_names,
1075 entry->used_ssa_names,
1076 entry->non_ssa_vars);
1077 if (pos <= entry->earliest && !entry->can_split
1078 && dump_file && (dump_flags & TDF_DETAILS))
1079 fprintf (dump_file,
1080 "found articulation at bb %i but can not split\n",
1081 entry->bb->index);
1082 if (pos <= entry->earliest && entry->can_split)
1083 {
1084 if (dump_file && (dump_flags & TDF_DETAILS))
1085 fprintf (dump_file, "found articulation at bb %i\n",
1086 entry->bb->index);
1087 current.entry_bb = entry->bb;
1088 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
1089 bitmap_and_compl (current.ssa_names_to_pass,
1090 entry->used_ssa_names, entry->set_ssa_names);
1091 current.header_time = overall_time - entry->overall_time;
1092 current.header_size = overall_size - entry->overall_size;
1093 current.split_time = entry->overall_time;
1094 current.split_size = entry->overall_size;
1095 current.split_bbs = entry->bbs_visited;
1096 consider_split (&current, entry->non_ssa_vars, return_bb);
1097 BITMAP_FREE (current.ssa_names_to_pass);
1098 }
1099 }
1100 /* Do actual DFS walk. */
1101 if (entry->edge_num
1102 < (EDGE_COUNT (entry->bb->succs)
1103 + EDGE_COUNT (entry->bb->preds)))
1104 {
1105 edge e;
1106 basic_block dest;
1107 if (entry->edge_num < EDGE_COUNT (entry->bb->succs))
1108 {
1109 e = EDGE_SUCC (entry->bb, entry->edge_num);
1110 dest = e->dest;
1111 }
1112 else
1113 {
1114 e = EDGE_PRED (entry->bb, entry->edge_num
1115 - EDGE_COUNT (entry->bb->succs));
1116 dest = e->src;
1117 }
1118
1119 entry->edge_num++;
1120
1121 /* New BB to visit, push it to the stack. */
fefa31b5 1122 if (dest != return_bb && dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
3e485f62
JH
1123 && !dest->aux)
1124 {
1125 stack_entry new_entry;
1126
1127 new_entry.bb = dest;
1128 new_entry.edge_num = 0;
1129 new_entry.overall_time
9771b263 1130 = bb_info_vec[dest->index].time;
3e485f62 1131 new_entry.overall_size
9771b263 1132 = bb_info_vec[dest->index].size;
3e485f62
JH
1133 new_entry.earliest = INT_MAX;
1134 new_entry.set_ssa_names = BITMAP_ALLOC (NULL);
1135 new_entry.used_ssa_names = BITMAP_ALLOC (NULL);
1136 new_entry.bbs_visited = BITMAP_ALLOC (NULL);
1137 new_entry.non_ssa_vars = BITMAP_ALLOC (NULL);
1138 new_entry.can_split = true;
1139 bitmap_set_bit (new_entry.bbs_visited, dest->index);
9771b263
DN
1140 stack.safe_push (new_entry);
1141 dest->aux = (void *)(intptr_t)stack.length ();
3e485f62
JH
1142 }
1143 /* Back edge found, record the earliest point. */
1144 else if ((intptr_t)dest->aux > 0
1145 && (intptr_t)dest->aux < entry->earliest)
1146 entry->earliest = (intptr_t)dest->aux;
1147 }
ed7656f6
JJ
1148 /* We are done with examining the edges. Pop off the value from stack
1149 and merge stuff we accumulate during the walk. */
fefa31b5 1150 else if (entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
3e485f62 1151 {
9771b263 1152 stack_entry *prev = &stack[stack.length () - 2];
3e485f62
JH
1153
1154 entry->bb->aux = (void *)(intptr_t)-1;
1155 prev->can_split &= entry->can_split;
1156 if (prev->set_ssa_names)
1157 {
1158 bitmap_ior_into (prev->set_ssa_names, entry->set_ssa_names);
1159 bitmap_ior_into (prev->used_ssa_names, entry->used_ssa_names);
1160 bitmap_ior_into (prev->bbs_visited, entry->bbs_visited);
1161 bitmap_ior_into (prev->non_ssa_vars, entry->non_ssa_vars);
1162 }
1163 if (prev->earliest > entry->earliest)
1164 prev->earliest = entry->earliest;
1165 prev->overall_time += entry->overall_time;
1166 prev->overall_size += entry->overall_size;
1167 BITMAP_FREE (entry->set_ssa_names);
1168 BITMAP_FREE (entry->used_ssa_names);
1169 BITMAP_FREE (entry->bbs_visited);
1170 BITMAP_FREE (entry->non_ssa_vars);
9771b263 1171 stack.pop ();
3e485f62
JH
1172 }
1173 else
9771b263 1174 stack.pop ();
3e485f62 1175 }
fefa31b5 1176 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = NULL;
11cd3bed 1177 FOR_EACH_BB_FN (bb, cfun)
3e485f62 1178 bb->aux = NULL;
9771b263 1179 stack.release ();
3e485f62
JH
1180 BITMAP_FREE (current.ssa_names_to_pass);
1181}
1182
d5e254e1
IE
1183/* Build and insert initialization of returned bounds RETBND
1184 for returned value RETVAL. Statements are inserted after
1185 a statement pointed by GSI and GSI is modified to point to
1186 the last inserted statement. */
1187
1188static void
1189insert_bndret_call_after (tree retbnd, tree retval, gimple_stmt_iterator *gsi)
1190{
1191 tree fndecl = targetm.builtin_chkp_function (BUILT_IN_CHKP_BNDRET);
1192 gimple bndret = gimple_build_call (fndecl, 1, retval);
1193 gimple_call_set_lhs (bndret, retbnd);
1194 gsi_insert_after (gsi, bndret, GSI_CONTINUE_LINKING);
1195}
3e485f62
JH
1196/* Split function at SPLIT_POINT. */
1197
1198static void
1199split_function (struct split_point *split_point)
1200{
6e1aa848 1201 vec<tree> args_to_pass = vNULL;
201176d3 1202 bitmap args_to_skip;
3e485f62
JH
1203 tree parm;
1204 int num = 0;
d52f5295 1205 cgraph_node *node, *cur_node = cgraph_node::get (current_function_decl);
3e485f62
JH
1206 basic_block return_bb = find_return_bb ();
1207 basic_block call_bb;
538dd0b7 1208 gcall *call;
3e485f62
JH
1209 edge e;
1210 edge_iterator ei;
d5e254e1 1211 tree retval = NULL, real_retval = NULL, retbnd = NULL;
3e485f62 1212 bool split_part_return_p = false;
d5e254e1 1213 bool with_bounds = chkp_function_instrumented_p (current_function_decl);
3e485f62 1214 gimple last_stmt = NULL;
371556ee 1215 unsigned int i;
32244553 1216 tree arg, ddef;
9771b263 1217 vec<tree, va_gc> **debug_args = NULL;
3e485f62
JH
1218
1219 if (dump_file)
1220 {
1221 fprintf (dump_file, "\n\nSplitting function at:\n");
1222 dump_split_point (dump_file, split_point);
1223 }
1224
201176d3
MJ
1225 if (cur_node->local.can_change_signature)
1226 args_to_skip = BITMAP_ALLOC (NULL);
1227 else
1228 args_to_skip = NULL;
1229
3e485f62
JH
1230 /* Collect the parameters of new function and args_to_skip bitmap. */
1231 for (parm = DECL_ARGUMENTS (current_function_decl);
910ad8de 1232 parm; parm = DECL_CHAIN (parm), num++)
201176d3
MJ
1233 if (args_to_skip
1234 && (!is_gimple_reg (parm)
32244553 1235 || (ddef = ssa_default_def (cfun, parm)) == NULL_TREE
201176d3 1236 || !bitmap_bit_p (split_point->ssa_names_to_pass,
32244553 1237 SSA_NAME_VERSION (ddef))))
3e485f62
JH
1238 bitmap_set_bit (args_to_skip, num);
1239 else
371556ee 1240 {
86814190
MJ
1241 /* This parm might not have been used up to now, but is going to be
1242 used, hence register it. */
86814190 1243 if (is_gimple_reg (parm))
32244553 1244 arg = get_or_create_ssa_default_def (cfun, parm);
86814190
MJ
1245 else
1246 arg = parm;
201176d3 1247
2b3c0885
RG
1248 if (!useless_type_conversion_p (DECL_ARG_TYPE (parm), TREE_TYPE (arg)))
1249 arg = fold_convert (DECL_ARG_TYPE (parm), arg);
9771b263 1250 args_to_pass.safe_push (arg);
371556ee 1251 }
3e485f62
JH
1252
1253 /* See if the split function will return. */
1254 FOR_EACH_EDGE (e, ei, return_bb->preds)
1255 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1256 break;
1257 if (e)
1258 split_part_return_p = true;
1259
241a2b9e
JH
1260 /* Add return block to what will become the split function.
1261 We do not return; no return block is needed. */
1262 if (!split_part_return_p)
1263 ;
1264 /* We have no return block, so nothing is needed. */
fefa31b5 1265 else if (return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
241a2b9e
JH
1266 ;
1267 /* When we do not want to return value, we need to construct
1268 new return block with empty return statement.
1269 FIXME: Once we are able to change return type, we should change function
1270 to return void instead of just outputting function with undefined return
1271 value. For structures this affects quality of codegen. */
1272 else if (!split_point->split_part_set_retval
1273 && find_retval (return_bb))
1274 {
1275 bool redirected = true;
1276 basic_block new_return_bb = create_basic_block (NULL, 0, return_bb);
1277 gimple_stmt_iterator gsi = gsi_start_bb (new_return_bb);
1278 gsi_insert_after (&gsi, gimple_build_return (NULL), GSI_NEW_STMT);
1279 while (redirected)
1280 {
1281 redirected = false;
1282 FOR_EACH_EDGE (e, ei, return_bb->preds)
1283 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1284 {
1285 new_return_bb->count += e->count;
1286 new_return_bb->frequency += EDGE_FREQUENCY (e);
1287 redirect_edge_and_branch (e, new_return_bb);
1288 redirected = true;
1289 break;
1290 }
1291 }
fefa31b5 1292 e = make_edge (new_return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
241a2b9e
JH
1293 e->probability = REG_BR_PROB_BASE;
1294 e->count = new_return_bb->count;
726338f4 1295 add_bb_to_loop (new_return_bb, current_loops->tree_root);
241a2b9e 1296 bitmap_set_bit (split_point->split_bbs, new_return_bb->index);
2e5e346d
JL
1297 }
1298 /* When we pass around the value, use existing return block. */
1299 else
1300 bitmap_set_bit (split_point->split_bbs, return_bb->index);
1301
1302 /* If RETURN_BB has virtual operand PHIs, they must be removed and the
1303 virtual operand marked for renaming as we change the CFG in a way that
cfef45c8 1304 tree-inline is not able to compensate for.
2e5e346d
JL
1305
1306 Note this can happen whether or not we have a return value. If we have
1307 a return value, then RETURN_BB may have PHIs for real operands too. */
fefa31b5 1308 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
2e5e346d 1309 {
cfef45c8 1310 bool phi_p = false;
538dd0b7
DM
1311 for (gphi_iterator gsi = gsi_start_phis (return_bb);
1312 !gsi_end_p (gsi);)
241a2b9e 1313 {
538dd0b7 1314 gphi *stmt = gsi.phi ();
ea057359 1315 if (!virtual_operand_p (gimple_phi_result (stmt)))
2e5e346d
JL
1316 {
1317 gsi_next (&gsi);
1318 continue;
1319 }
6b8c9df8
RG
1320 mark_virtual_phi_result_for_renaming (stmt);
1321 remove_phi_node (&gsi, true);
cfef45c8 1322 phi_p = true;
241a2b9e 1323 }
cfef45c8
RG
1324 /* In reality we have to rename the reaching definition of the
1325 virtual operand at return_bb as we will eventually release it
1326 when we remove the code region we outlined.
1327 So we have to rename all immediate virtual uses of that region
1328 if we didn't see a PHI definition yet. */
1329 /* ??? In real reality we want to set the reaching vdef of the
1330 entry of the SESE region as the vuse of the call and the reaching
1331 vdef of the exit of the SESE region as the vdef of the call. */
1332 if (!phi_p)
538dd0b7
DM
1333 for (gimple_stmt_iterator gsi = gsi_start_bb (return_bb);
1334 !gsi_end_p (gsi);
1335 gsi_next (&gsi))
cfef45c8
RG
1336 {
1337 gimple stmt = gsi_stmt (gsi);
1338 if (gimple_vuse (stmt))
1339 {
1340 gimple_set_vuse (stmt, NULL_TREE);
1341 update_stmt (stmt);
1342 }
1343 if (gimple_vdef (stmt))
1344 break;
1345 }
241a2b9e 1346 }
3e485f62
JH
1347
1348 /* Now create the actual clone. */
3dafb85c 1349 cgraph_edge::rebuild_edges ();
d52f5295
ML
1350 node = cur_node->create_version_clone_with_body
1351 (vNULL, NULL, args_to_skip, !split_part_return_p, split_point->split_bbs,
1352 split_point->entry_bb, "part");
9cec31f4
ML
1353
1354 /* Let's take a time profile for splitted function. */
1355 node->tp_first_run = cur_node->tp_first_run + 1;
1356
d402c33d
JH
1357 /* For usual cloning it is enough to clear builtin only when signature
1358 changes. For partial inlining we however can not expect the part
1359 of builtin implementation to have same semantic as the whole. */
67348ccc 1360 if (DECL_BUILT_IN (node->decl))
d402c33d 1361 {
67348ccc
DM
1362 DECL_BUILT_IN_CLASS (node->decl) = NOT_BUILT_IN;
1363 DECL_FUNCTION_CODE (node->decl) = (enum built_in_function) 0;
d402c33d 1364 }
d5e254e1
IE
1365
1366 /* If the original function is instrumented then it's
1367 part is also instrumented. */
1368 if (with_bounds)
1369 chkp_function_mark_instrumented (node->decl);
1370
9a6af450
EB
1371 /* If the original function is declared inline, there is no point in issuing
1372 a warning for the non-inlinable part. */
67348ccc 1373 DECL_NO_INLINE_WARNING_P (node->decl) = 1;
d52f5295 1374 cur_node->remove_callees ();
d122681a 1375 cur_node->remove_all_references ();
3e485f62 1376 if (!split_part_return_p)
67348ccc 1377 TREE_THIS_VOLATILE (node->decl) = 1;
3e485f62 1378 if (dump_file)
67348ccc 1379 dump_function_to_file (node->decl, dump_file, dump_flags);
3e485f62
JH
1380
1381 /* Create the basic block we place call into. It is the entry basic block
1382 split after last label. */
1383 call_bb = split_point->entry_bb;
538dd0b7 1384 for (gimple_stmt_iterator gsi = gsi_start_bb (call_bb); !gsi_end_p (gsi);)
3e485f62
JH
1385 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
1386 {
1387 last_stmt = gsi_stmt (gsi);
1388 gsi_next (&gsi);
1389 }
1390 else
1391 break;
1392 e = split_block (split_point->entry_bb, last_stmt);
1393 remove_edge (e);
1394
1395 /* Produce the call statement. */
538dd0b7 1396 gimple_stmt_iterator gsi = gsi_last_bb (call_bb);
9771b263 1397 FOR_EACH_VEC_ELT (args_to_pass, i, arg)
2b3c0885
RG
1398 if (!is_gimple_val (arg))
1399 {
1400 arg = force_gimple_operand_gsi (&gsi, arg, true, NULL_TREE,
f6e52e91 1401 false, GSI_CONTINUE_LINKING);
9771b263 1402 args_to_pass[i] = arg;
2b3c0885 1403 }
67348ccc 1404 call = gimple_build_call_vec (node->decl, args_to_pass);
d5e254e1 1405 gimple_call_set_with_bounds (call, with_bounds);
3e485f62 1406 gimple_set_block (call, DECL_INITIAL (current_function_decl));
9771b263 1407 args_to_pass.release ();
3e485f62 1408
878eef4a
JJ
1409 /* For optimized away parameters, add on the caller side
1410 before the call
1411 DEBUG D#X => parm_Y(D)
1412 stmts and associate D#X with parm in decl_debug_args_lookup
1413 vector to say for debug info that if parameter parm had been passed,
1414 it would have value parm_Y(D). */
1415 if (args_to_skip)
1416 for (parm = DECL_ARGUMENTS (current_function_decl), num = 0;
1417 parm; parm = DECL_CHAIN (parm), num++)
1418 if (bitmap_bit_p (args_to_skip, num)
1419 && is_gimple_reg (parm))
1420 {
1421 tree ddecl;
1422 gimple def_temp;
1423
1424 /* This needs to be done even without MAY_HAVE_DEBUG_STMTS,
1425 otherwise if it didn't exist before, we'd end up with
1426 different SSA_NAME_VERSIONs between -g and -g0. */
1427 arg = get_or_create_ssa_default_def (cfun, parm);
1428 if (!MAY_HAVE_DEBUG_STMTS)
1429 continue;
1430
1431 if (debug_args == NULL)
67348ccc 1432 debug_args = decl_debug_args_insert (node->decl);
878eef4a
JJ
1433 ddecl = make_node (DEBUG_EXPR_DECL);
1434 DECL_ARTIFICIAL (ddecl) = 1;
1435 TREE_TYPE (ddecl) = TREE_TYPE (parm);
1436 DECL_MODE (ddecl) = DECL_MODE (parm);
9771b263
DN
1437 vec_safe_push (*debug_args, DECL_ORIGIN (parm));
1438 vec_safe_push (*debug_args, ddecl);
878eef4a
JJ
1439 def_temp = gimple_build_debug_bind (ddecl, unshare_expr (arg),
1440 call);
1441 gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
1442 }
1443 /* And on the callee side, add
1444 DEBUG D#Y s=> parm
1445 DEBUG var => D#Y
1446 stmts to the first bb where var is a VAR_DECL created for the
1447 optimized away parameter in DECL_INITIAL block. This hints
1448 in the debug info that var (whole DECL_ORIGIN is the parm PARM_DECL)
1449 is optimized away, but could be looked up at the call site
1450 as value of D#X there. */
1451 if (debug_args != NULL)
1452 {
1453 unsigned int i;
1454 tree var, vexpr;
1455 gimple_stmt_iterator cgsi;
1456 gimple def_temp;
1457
67348ccc
DM
1458 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1459 var = BLOCK_VARS (DECL_INITIAL (node->decl));
9771b263 1460 i = vec_safe_length (*debug_args);
fefa31b5 1461 cgsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
878eef4a
JJ
1462 do
1463 {
1464 i -= 2;
1465 while (var != NULL_TREE
9771b263 1466 && DECL_ABSTRACT_ORIGIN (var) != (**debug_args)[i])
878eef4a
JJ
1467 var = TREE_CHAIN (var);
1468 if (var == NULL_TREE)
1469 break;
1470 vexpr = make_node (DEBUG_EXPR_DECL);
9771b263 1471 parm = (**debug_args)[i];
878eef4a
JJ
1472 DECL_ARTIFICIAL (vexpr) = 1;
1473 TREE_TYPE (vexpr) = TREE_TYPE (parm);
1474 DECL_MODE (vexpr) = DECL_MODE (parm);
1475 def_temp = gimple_build_debug_source_bind (vexpr, parm,
1476 NULL);
1477 gsi_insert_before (&cgsi, def_temp, GSI_SAME_STMT);
1478 def_temp = gimple_build_debug_bind (var, vexpr, NULL);
1479 gsi_insert_before (&cgsi, def_temp, GSI_SAME_STMT);
1480 }
1481 while (i);
1482 pop_cfun ();
1483 }
1484
556e9ba0
JH
1485 /* We avoid address being taken on any variable used by split part,
1486 so return slot optimization is always possible. Moreover this is
1487 required to make DECL_BY_REFERENCE work. */
1488 if (aggregate_value_p (DECL_RESULT (current_function_decl),
22110e6c
EB
1489 TREE_TYPE (current_function_decl))
1490 && (!is_gimple_reg_type (TREE_TYPE (DECL_RESULT (current_function_decl)))
1491 || DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))))
556e9ba0
JH
1492 gimple_call_set_return_slot_opt (call, true);
1493
3e485f62
JH
1494 /* Update return value. This is bit tricky. When we do not return,
1495 do nothing. When we return we might need to update return_bb
1496 or produce a new return statement. */
1497 if (!split_part_return_p)
1498 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1499 else
1500 {
1501 e = make_edge (call_bb, return_bb,
fefa31b5
DM
1502 return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
1503 ? 0 : EDGE_FALLTHRU);
3e485f62
JH
1504 e->count = call_bb->count;
1505 e->probability = REG_BR_PROB_BASE;
6938f93f
JH
1506
1507 /* If there is return basic block, see what value we need to store
1508 return value into and put call just before it. */
fefa31b5 1509 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3e485f62 1510 {
2094f1fc 1511 real_retval = retval = find_retval (return_bb);
d5e254e1 1512 retbnd = find_retbnd (return_bb);
6938f93f 1513
241a2b9e 1514 if (real_retval && split_point->split_part_set_retval)
3e485f62 1515 {
538dd0b7 1516 gphi_iterator psi;
3e485f62 1517
6938f93f
JH
1518 /* See if we need new SSA_NAME for the result.
1519 When DECL_BY_REFERENCE is true, retval is actually pointer to
1520 return value and it is constant in whole function. */
1521 if (TREE_CODE (retval) == SSA_NAME
1522 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
3e485f62 1523 {
070ecdfd 1524 retval = copy_ssa_name (retval, call);
6938f93f
JH
1525
1526 /* See if there is PHI defining return value. */
1527 for (psi = gsi_start_phis (return_bb);
1528 !gsi_end_p (psi); gsi_next (&psi))
538dd0b7 1529 if (!virtual_operand_p (gimple_phi_result (psi.phi ())))
6938f93f
JH
1530 break;
1531
1532 /* When there is PHI, just update its value. */
3e485f62
JH
1533 if (TREE_CODE (retval) == SSA_NAME
1534 && !gsi_end_p (psi))
538dd0b7 1535 add_phi_arg (psi.phi (), retval, e, UNKNOWN_LOCATION);
6938f93f
JH
1536 /* Otherwise update the return BB itself.
1537 find_return_bb allows at most one assignment to return value,
1538 so update first statement. */
1539 else
3e485f62 1540 {
2094f1fc
JH
1541 gimple_stmt_iterator bsi;
1542 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi);
1543 gsi_next (&bsi))
538dd0b7
DM
1544 if (greturn *return_stmt
1545 = dyn_cast <greturn *> (gsi_stmt (bsi)))
2094f1fc 1546 {
538dd0b7 1547 gimple_return_set_retval (return_stmt, retval);
2094f1fc
JH
1548 break;
1549 }
2e216592
JJ
1550 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
1551 && !gimple_clobber_p (gsi_stmt (bsi)))
2094f1fc
JH
1552 {
1553 gimple_assign_set_rhs1 (gsi_stmt (bsi), retval);
1554 break;
1555 }
1556 update_stmt (gsi_stmt (bsi));
3e485f62 1557 }
d5e254e1
IE
1558
1559 /* Replace retbnd with new one. */
1560 if (retbnd)
1561 {
1562 gimple_stmt_iterator bsi;
1563 for (bsi = gsi_last_bb (return_bb); !gsi_end_p (bsi);
1564 gsi_prev (&bsi))
1565 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
1566 {
1567 retbnd = copy_ssa_name (retbnd, call);
1568 gimple_return_set_retbnd (gsi_stmt (bsi), retbnd);
1569 update_stmt (gsi_stmt (bsi));
1570 break;
1571 }
1572 }
3e485f62 1573 }
556e9ba0 1574 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
42b05b6e
RG
1575 {
1576 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1577 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1578 }
556e9ba0 1579 else
42b05b6e
RG
1580 {
1581 tree restype;
1582 restype = TREE_TYPE (DECL_RESULT (current_function_decl));
1583 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1584 if (!useless_type_conversion_p (TREE_TYPE (retval), restype))
1585 {
1586 gimple cpy;
b731b390 1587 tree tem = create_tmp_reg (restype);
42b05b6e 1588 tem = make_ssa_name (tem, call);
0d0e4a03 1589 cpy = gimple_build_assign (retval, NOP_EXPR, tem);
42b05b6e
RG
1590 gsi_insert_after (&gsi, cpy, GSI_NEW_STMT);
1591 retval = tem;
1592 }
d5e254e1
IE
1593 /* Build bndret call to obtain returned bounds. */
1594 if (retbnd)
1595 insert_bndret_call_after (retbnd, retval, &gsi);
42b05b6e
RG
1596 gimple_call_set_lhs (call, retval);
1597 update_stmt (call);
1598 }
3e485f62 1599 }
42b05b6e
RG
1600 else
1601 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
3e485f62 1602 }
6938f93f
JH
1603 /* We don't use return block (there is either no return in function or
1604 multiple of them). So create new basic block with return statement.
1605 */
3e485f62
JH
1606 else
1607 {
538dd0b7 1608 greturn *ret;
241a2b9e
JH
1609 if (split_point->split_part_set_retval
1610 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
3e485f62 1611 {
4021f4a1 1612 retval = DECL_RESULT (current_function_decl);
8a9c1ae6 1613
d5e254e1
IE
1614 if (chkp_function_instrumented_p (current_function_decl)
1615 && BOUNDED_P (retval))
b731b390 1616 retbnd = create_tmp_reg (pointer_bounds_type_node);
d5e254e1 1617
8a9c1ae6
JH
1618 /* We use temporary register to hold value when aggregate_value_p
1619 is false. Similarly for DECL_BY_REFERENCE we must avoid extra
1620 copy. */
1621 if (!aggregate_value_p (retval, TREE_TYPE (current_function_decl))
1622 && !DECL_BY_REFERENCE (retval))
b731b390 1623 retval = create_tmp_reg (TREE_TYPE (retval));
3e485f62 1624 if (is_gimple_reg (retval))
6938f93f
JH
1625 {
1626 /* When returning by reference, there is only one SSA name
1627 assigned to RESULT_DECL (that is pointer to return value).
1628 Look it up or create new one if it is missing. */
1629 if (DECL_BY_REFERENCE (retval))
32244553 1630 retval = get_or_create_ssa_default_def (cfun, retval);
6938f93f
JH
1631 /* Otherwise produce new SSA name for return value. */
1632 else
1633 retval = make_ssa_name (retval, call);
1634 }
556e9ba0
JH
1635 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1636 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1637 else
1638 gimple_call_set_lhs (call, retval);
3e485f62
JH
1639 }
1640 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
d5e254e1
IE
1641 /* Build bndret call to obtain returned bounds. */
1642 if (retbnd)
1643 insert_bndret_call_after (retbnd, retval, &gsi);
3e485f62
JH
1644 ret = gimple_build_return (retval);
1645 gsi_insert_after (&gsi, ret, GSI_NEW_STMT);
1646 }
1647 }
1648 free_dominance_info (CDI_DOMINATORS);
1649 free_dominance_info (CDI_POST_DOMINATORS);
632b4f8e 1650 compute_inline_parameters (node, true);
3e485f62
JH
1651}
1652
1653/* Execute function splitting pass. */
1654
1655static unsigned int
1656execute_split_functions (void)
1657{
1658 gimple_stmt_iterator bsi;
1659 basic_block bb;
1660 int overall_time = 0, overall_size = 0;
1661 int todo = 0;
d52f5295 1662 struct cgraph_node *node = cgraph_node::get (current_function_decl);
3e485f62 1663
b2d2adc6
RG
1664 if (flags_from_decl_or_type (current_function_decl)
1665 & (ECF_NORETURN|ECF_MALLOC))
3e485f62
JH
1666 {
1667 if (dump_file)
b2d2adc6 1668 fprintf (dump_file, "Not splitting: noreturn/malloc function.\n");
3e485f62
JH
1669 return 0;
1670 }
1671 if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
1672 {
1673 if (dump_file)
1674 fprintf (dump_file, "Not splitting: main function.\n");
1675 return 0;
1676 }
1677 /* This can be relaxed; function might become inlinable after splitting
1678 away the uninlinable part. */
9771b263
DN
1679 if (inline_edge_summary_vec.exists ()
1680 && !inline_summary (node)->inlinable)
3e485f62
JH
1681 {
1682 if (dump_file)
1683 fprintf (dump_file, "Not splitting: not inlinable.\n");
1684 return 0;
1685 }
67348ccc 1686 if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
3e485f62
JH
1687 {
1688 if (dump_file)
ed7656f6 1689 fprintf (dump_file, "Not splitting: disregarding inline limits.\n");
3e485f62
JH
1690 return 0;
1691 }
1692 /* This can be relaxed; most of versioning tests actually prevents
1693 a duplication. */
1694 if (!tree_versionable_function_p (current_function_decl))
1695 {
1696 if (dump_file)
1697 fprintf (dump_file, "Not splitting: not versionable.\n");
1698 return 0;
1699 }
1700 /* FIXME: we could support this. */
1701 if (DECL_STRUCT_FUNCTION (current_function_decl)->static_chain_decl)
1702 {
1703 if (dump_file)
1704 fprintf (dump_file, "Not splitting: nested function.\n");
1705 return 0;
1706 }
3e485f62
JH
1707
1708 /* See if it makes sense to try to split.
1709 It makes sense to split if we inline, that is if we have direct calls to
1710 handle or direct calls are possibly going to appear as result of indirect
cf9712cc
JH
1711 inlining or LTO. Also handle -fprofile-generate as LTO to allow non-LTO
1712 training for LTO -fprofile-use build.
1713
3e485f62
JH
1714 Note that we are not completely conservative about disqualifying functions
1715 called once. It is possible that the caller is called more then once and
1716 then inlining would still benefit. */
c91061e6
JH
1717 if ((!node->callers
1718 /* Local functions called once will be completely inlined most of time. */
1719 || (!node->callers->next_caller && node->local.local))
67348ccc
DM
1720 && !node->address_taken
1721 && (!flag_lto || !node->externally_visible))
3e485f62
JH
1722 {
1723 if (dump_file)
1724 fprintf (dump_file, "Not splitting: not called directly "
1725 "or called once.\n");
1726 return 0;
1727 }
1728
1729 /* FIXME: We can actually split if splitting reduces call overhead. */
1730 if (!flag_inline_small_functions
1731 && !DECL_DECLARED_INLINE_P (current_function_decl))
1732 {
1733 if (dump_file)
1734 fprintf (dump_file, "Not splitting: not autoinlining and function"
1735 " is not inline.\n");
1736 return 0;
1737 }
1738
e70670cf
JH
1739 /* We enforce splitting after loop headers when profile info is not
1740 available. */
0a6a6ac9 1741 if (profile_status_for_fn (cfun) != PROFILE_READ)
e70670cf
JH
1742 mark_dfs_back_edges ();
1743
b2e25729
BS
1744 /* Initialize bitmap to track forbidden calls. */
1745 forbidden_dominators = BITMAP_ALLOC (NULL);
1746 calculate_dominance_info (CDI_DOMINATORS);
1747
3e485f62 1748 /* Compute local info about basic blocks and determine function size/time. */
8b1c6fd7 1749 bb_info_vec.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
3e485f62 1750 memset (&best_split_point, 0, sizeof (best_split_point));
11cd3bed 1751 FOR_EACH_BB_FN (bb, cfun)
3e485f62
JH
1752 {
1753 int time = 0;
1754 int size = 0;
1755 int freq = compute_call_stmt_bb_frequency (current_function_decl, bb);
1756
1757 if (dump_file && (dump_flags & TDF_DETAILS))
1758 fprintf (dump_file, "Basic block %i\n", bb->index);
1759
1760 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1761 {
1762 int this_time, this_size;
1763 gimple stmt = gsi_stmt (bsi);
1764
1765 this_size = estimate_num_insns (stmt, &eni_size_weights);
1766 this_time = estimate_num_insns (stmt, &eni_time_weights) * freq;
1767 size += this_size;
1768 time += this_time;
b2e25729 1769 check_forbidden_calls (stmt);
3e485f62
JH
1770
1771 if (dump_file && (dump_flags & TDF_DETAILS))
1772 {
1773 fprintf (dump_file, " freq:%6i size:%3i time:%3i ",
1774 freq, this_size, this_time);
1775 print_gimple_stmt (dump_file, stmt, 0, 0);
1776 }
1777 }
1778 overall_time += time;
1779 overall_size += size;
9771b263
DN
1780 bb_info_vec[bb->index].time = time;
1781 bb_info_vec[bb->index].size = size;
3e485f62
JH
1782 }
1783 find_split_points (overall_time, overall_size);
1784 if (best_split_point.split_bbs)
1785 {
1786 split_function (&best_split_point);
1787 BITMAP_FREE (best_split_point.ssa_names_to_pass);
1788 BITMAP_FREE (best_split_point.split_bbs);
1789 todo = TODO_update_ssa | TODO_cleanup_cfg;
1790 }
b2e25729 1791 BITMAP_FREE (forbidden_dominators);
9771b263 1792 bb_info_vec.release ();
3e485f62
JH
1793 return todo;
1794}
1795
27a4cd48
DM
1796namespace {
1797
1798const pass_data pass_data_split_functions =
3e485f62 1799{
27a4cd48
DM
1800 GIMPLE_PASS, /* type */
1801 "fnsplit", /* name */
1802 OPTGROUP_NONE, /* optinfo_flags */
27a4cd48
DM
1803 TV_IPA_FNSPLIT, /* tv_id */
1804 PROP_cfg, /* properties_required */
1805 0, /* properties_provided */
1806 0, /* properties_destroyed */
1807 0, /* todo_flags_start */
3bea341f 1808 0, /* todo_flags_finish */
3e485f62 1809};
cf9712cc 1810
27a4cd48
DM
1811class pass_split_functions : public gimple_opt_pass
1812{
1813public:
c3284718
RS
1814 pass_split_functions (gcc::context *ctxt)
1815 : gimple_opt_pass (pass_data_split_functions, ctxt)
27a4cd48
DM
1816 {}
1817
1818 /* opt_pass methods: */
1a3d085c 1819 virtual bool gate (function *);
be55bfe6
TS
1820 virtual unsigned int execute (function *)
1821 {
1822 return execute_split_functions ();
1823 }
27a4cd48
DM
1824
1825}; // class pass_split_functions
1826
1a3d085c
TS
1827bool
1828pass_split_functions::gate (function *)
1829{
1830 /* When doing profile feedback, we want to execute the pass after profiling
1831 is read. So disable one in early optimization. */
1832 return (flag_partial_inlining
1833 && !profile_arc_flag && !flag_branch_probabilities);
1834}
1835
27a4cd48
DM
1836} // anon namespace
1837
1838gimple_opt_pass *
1839make_pass_split_functions (gcc::context *ctxt)
1840{
1841 return new pass_split_functions (ctxt);
1842}
1843
cf9712cc
JH
1844/* Execute function splitting pass. */
1845
1846static unsigned int
1847execute_feedback_split_functions (void)
1848{
1849 unsigned int retval = execute_split_functions ();
1850 if (retval)
1851 retval |= TODO_rebuild_cgraph_edges;
1852 return retval;
1853}
1854
27a4cd48
DM
1855namespace {
1856
1857const pass_data pass_data_feedback_split_functions =
cf9712cc 1858{
27a4cd48
DM
1859 GIMPLE_PASS, /* type */
1860 "feedback_fnsplit", /* name */
1861 OPTGROUP_NONE, /* optinfo_flags */
27a4cd48
DM
1862 TV_IPA_FNSPLIT, /* tv_id */
1863 PROP_cfg, /* properties_required */
1864 0, /* properties_provided */
1865 0, /* properties_destroyed */
1866 0, /* todo_flags_start */
3bea341f 1867 0, /* todo_flags_finish */
cf9712cc 1868};
27a4cd48
DM
1869
1870class pass_feedback_split_functions : public gimple_opt_pass
1871{
1872public:
c3284718
RS
1873 pass_feedback_split_functions (gcc::context *ctxt)
1874 : gimple_opt_pass (pass_data_feedback_split_functions, ctxt)
27a4cd48
DM
1875 {}
1876
1877 /* opt_pass methods: */
1a3d085c 1878 virtual bool gate (function *);
be55bfe6
TS
1879 virtual unsigned int execute (function *)
1880 {
1881 return execute_feedback_split_functions ();
1882 }
27a4cd48
DM
1883
1884}; // class pass_feedback_split_functions
1885
1a3d085c
TS
1886bool
1887pass_feedback_split_functions::gate (function *)
1888{
1889 /* We don't need to split when profiling at all, we are producing
1890 lousy code anyway. */
1891 return (flag_partial_inlining
1892 && flag_branch_probabilities);
1893}
1894
27a4cd48
DM
1895} // anon namespace
1896
1897gimple_opt_pass *
1898make_pass_feedback_split_functions (gcc::context *ctxt)
1899{
1900 return new pass_feedback_split_functions (ctxt);
1901}