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