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