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