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