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