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