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