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