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