1 /* Function splitting pass
2 Copyright (C) 2010-2016 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka <jh@suse.cz>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along 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,
48 but not fund.part leads to performance improvement similar as inlining
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"
80 #include "backend.h"
81 #include "rtl.h"
82 #include "tree.h"
83 #include "gimple.h"
84 #include "cfghooks.h"
85 #include "alloc-pool.h"
86 #include "tree-pass.h"
87 #include "ssa.h"
88 #include "cgraph.h"
89 #include "diagnostic.h"
90 #include "fold-const.h"
91 #include "cfganal.h"
92 #include "calls.h"
93 #include "gimplify.h"
94 #include "gimple-iterator.h"
95 #include "gimplify-me.h"
96 #include "gimple-walk.h"
97 #include "symbol-summary.h"
98 #include "ipa-prop.h"
99 #include "tree-cfg.h"
100 #include "tree-into-ssa.h"
101 #include "tree-dfa.h"
102 #include "tree-inline.h"
103 #include "params.h"
104 #include "gimple-pretty-print.h"
105 #include "ipa-inline.h"
106 #include "cfgloop.h"
107 #include "tree-chkp.h"
108
109 /* Per basic block info. */
110
111 struct split_bb_info
112 {
113 unsigned int size;
114 unsigned int time;
115 };
116
117 static vec<split_bb_info> bb_info_vec;
118
119 /* Description of split point. */
120
121 struct split_point
122 {
123 /* Size of the partitions. */
124 unsigned int header_time, header_size, split_time, split_size;
125
126 /* SSA names that need to be passed into spit function. */
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;
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;
138 };
139
140 /* Best split point found. */
141
142 struct split_point best_split_point;
143
144 /* Set of basic blocks that are not allowed to dominate a split point. */
145
146 static bitmap forbidden_dominators;
147
148 static tree find_retval (basic_block return_bb);
149 static tree find_retbnd (basic_block return_bb);
150
151 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
152 variable, check it if it is present in bitmap passed via DATA. */
153
154 static bool
test_nonssa_use(gimple *,tree t,tree,void * data)155 test_nonssa_use (gimple *, tree t, tree, void *data)
156 {
157 t = get_base_address (t);
158
159 if (!t || is_gimple_reg (t))
160 return false;
161
162 if (TREE_CODE (t) == PARM_DECL
163 || (TREE_CODE (t) == VAR_DECL
164 && auto_var_in_fn_p (t, current_function_decl))
165 || TREE_CODE (t) == RESULT_DECL
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)))
171 return bitmap_bit_p ((bitmap)data, DECL_UID (t));
172
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))
176 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
177 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
178 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
179 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
180 return
181 bitmap_bit_p ((bitmap)data,
182 DECL_UID (DECL_RESULT (current_function_decl)));
183
184 return false;
185 }
186
187 /* Dump split point CURRENT. */
188
189 static void
dump_split_point(FILE * file,struct split_point * current)190 dump_split_point (FILE * file, struct split_point *current)
191 {
192 fprintf (file,
193 "Split point at BB %i\n"
194 " header time: %i header size: %i\n"
195 " split time: %i split size: %i\n bbs: ",
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
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.
205 Parameters are the same as for consider_split. */
206
207 static bool
verify_non_ssa_vars(struct split_point * current,bitmap non_ssa_vars,basic_block return_bb)208 verify_non_ssa_vars (struct split_point *current, bitmap non_ssa_vars,
209 basic_block return_bb)
210 {
211 bitmap seen = BITMAP_ALLOC (NULL);
212 vec<basic_block> worklist = vNULL;
213 edge e;
214 edge_iterator ei;
215 bool ok = true;
216 basic_block bb;
217
218 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
219 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
220 && !bitmap_bit_p (current->split_bbs, e->src->index))
221 {
222 worklist.safe_push (e->src);
223 bitmap_set_bit (seen, e->src->index);
224 }
225
226 while (!worklist.is_empty ())
227 {
228 bb = worklist.pop ();
229 FOR_EACH_EDGE (e, ei, bb->preds)
230 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
231 && bitmap_set_bit (seen, e->src->index))
232 {
233 gcc_checking_assert (!bitmap_bit_p (current->split_bbs,
234 e->src->index));
235 worklist.safe_push (e->src);
236 }
237 for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
238 gsi_next (&bsi))
239 {
240 gimple *stmt = gsi_stmt (bsi);
241 if (is_gimple_debug (stmt))
242 continue;
243 if (walk_stmt_load_store_addr_ops
244 (stmt, non_ssa_vars, test_nonssa_use, test_nonssa_use,
245 test_nonssa_use))
246 {
247 ok = false;
248 goto done;
249 }
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 }
257 }
258 for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
259 gsi_next (&bsi))
260 {
261 if (walk_stmt_load_store_addr_ops
262 (gsi_stmt (bsi), non_ssa_vars, test_nonssa_use, test_nonssa_use,
263 test_nonssa_use))
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;
273 for (gphi_iterator bsi = gsi_start_phis (return_bb);
274 !gsi_end_p (bsi);
275 gsi_next (&bsi))
276 {
277 gphi *stmt = bsi.phi ();
278 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
279
280 if (virtual_operand_p (gimple_phi_result (stmt)))
281 continue;
282 if (TREE_CODE (op) != SSA_NAME
283 && test_nonssa_use (stmt, op, op, non_ssa_vars))
284 {
285 ok = false;
286 goto done;
287 }
288 }
289 }
290 }
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))
300 if (glabel *label_stmt = dyn_cast <glabel *> (gsi_stmt (bsi)))
301 {
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 }
309 }
310 else
311 break;
312 }
313
314 done:
315 BITMAP_FREE (seen);
316 worklist.release ();
317 return ok;
318 }
319
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
329 static void
check_forbidden_calls(gimple * stmt)330 check_forbidden_calls (gimple *stmt)
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;
352 gcond *use_stmt;
353
354 use_stmt = dyn_cast <gcond *> (USE_STMT (use_p));
355 if (!use_stmt)
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
383 static bool
dominated_by_forbidden(basic_block bb)384 dominated_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 {
391 if (dominated_by_p (CDI_DOMINATORS, bb,
392 BASIC_BLOCK_FOR_FN (cfun, dom_bb)))
393 return true;
394 }
395
396 return false;
397 }
398
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. */
401 static bool
split_part_set_ssa_name_p(tree val,struct split_point * current,basic_block return_bb)402 split_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
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
418 static void
consider_split(struct split_point * current,bitmap non_ssa_vars,basic_block return_bb)419 consider_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;
427 gphi_iterator bsi;
428 unsigned int i;
429 int incoming_freq = 0;
430 tree retval;
431 tree retbnd;
432 bool back_edge = false;
433
434 if (dump_file && (dump_flags & TDF_DETAILS))
435 dump_split_point (dump_file, current);
436
437 FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
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 }
444
445 /* Do not split when we would end up calling function anyway. */
446 if (incoming_freq
447 >= (ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency
448 * PARAM_VALUE (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY) / 100))
449 {
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
456 && profile_status_for_fn (cfun) != PROFILE_READ
457 && incoming_freq < ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency)
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,
463 ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency);
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 }
472 }
473
474 if (!current->header_size)
475 {
476 if (dump_file && (dump_flags & TDF_DETAILS))
477 fprintf (dump_file, " Refused: header empty\n");
478 return;
479 }
480
481 /* Verify that PHI args on entry are either virtual or all their operands
482 incoming from header are the same. */
483 for (bsi = gsi_start_phis (current->entry_bb); !gsi_end_p (bsi); gsi_next (&bsi))
484 {
485 gphi *stmt = bsi.phi ();
486 tree val = NULL;
487
488 if (virtual_operand_p (gimple_phi_result (stmt)))
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 }
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;
513 parm = DECL_CHAIN (parm))
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 }
525 else
526 {
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)))
533 call_overhead += estimate_move_cost (TREE_TYPE (parm), false);
534 num_args++;
535 }
536 }
537 }
538 if (!VOID_TYPE_P (TREE_TYPE (current_function_decl)))
539 call_overhead += estimate_move_cost (TREE_TYPE (current_function_decl),
540 false);
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
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
585 /* FIXME: we currently can pass only SSA function parameters to the split
586 arguments. Once parm_adjustment infrastructure is supported by cloning,
587 we can pass more than that. */
588 if (num_args != bitmap_count_bits (current->ssa_names_to_pass))
589 {
590
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. */
600 if (!bitmap_empty_p (non_ssa_vars)
601 && !verify_non_ssa_vars (current, non_ssa_vars, return_bb))
602 {
603 if (dump_file && (dump_flags & TDF_DETAILS))
604 fprintf (dump_file,
605 " Refused: split part has non-ssa uses\n");
606 return;
607 }
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
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 {
633 /* If there is a return_bb with no return value in function returning
634 value by reference, also make the split part return void, otherwise
635 we expansion would try to create a non-POD temporary, which is
636 invalid. */
637 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
638 && DECL_RESULT (current_function_decl)
639 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
640 current->split_part_set_retval = false;
641 else
642 current->split_part_set_retval = true;
643 }
644 else if (is_gimple_min_invariant (retval))
645 current->split_part_set_retval = false;
646 /* Special case is value returned by reference we record as if it was non-ssa
647 set to result_decl. */
648 else if (TREE_CODE (retval) == SSA_NAME
649 && SSA_NAME_VAR (retval)
650 && TREE_CODE (SSA_NAME_VAR (retval)) == RESULT_DECL
651 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
652 current->split_part_set_retval
653 = bitmap_bit_p (non_ssa_vars, DECL_UID (SSA_NAME_VAR (retval)));
654 else if (TREE_CODE (retval) == SSA_NAME)
655 current->split_part_set_retval
656 = split_part_set_ssa_name_p (retval, current, return_bb);
657 else if (TREE_CODE (retval) == PARM_DECL)
658 current->split_part_set_retval = false;
659 else if (TREE_CODE (retval) == VAR_DECL
660 || TREE_CODE (retval) == RESULT_DECL)
661 current->split_part_set_retval
662 = bitmap_bit_p (non_ssa_vars, DECL_UID (retval));
663 else
664 current->split_part_set_retval = true;
665
666 /* See if retbnd used by return bb is computed by header or split part. */
667 retbnd = find_retbnd (return_bb);
668 if (retbnd)
669 {
670 bool split_part_set_retbnd
671 = split_part_set_ssa_name_p (retbnd, current, return_bb);
672
673 /* If we have both return value and bounds then keep their definitions
674 in a single function. We use SSA names to link returned bounds and
675 value and therefore do not handle cases when result is passed by
676 reference (which should not be our case anyway since bounds are
677 returned for pointers only). */
678 if ((DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
679 && current->split_part_set_retval)
680 || split_part_set_retbnd != current->split_part_set_retval)
681 {
682 if (dump_file && (dump_flags & TDF_DETAILS))
683 fprintf (dump_file,
684 " Refused: split point splits return value and bounds\n");
685 return;
686 }
687 }
688
689 /* split_function fixes up at most one PHI non-virtual PHI node in return_bb,
690 for the return value. If there are other PHIs, give up. */
691 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
692 {
693 gphi_iterator psi;
694
695 for (psi = gsi_start_phis (return_bb); !gsi_end_p (psi); gsi_next (&psi))
696 if (!virtual_operand_p (gimple_phi_result (psi.phi ()))
697 && !(retval
698 && current->split_part_set_retval
699 && TREE_CODE (retval) == SSA_NAME
700 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
701 && SSA_NAME_DEF_STMT (retval) == psi.phi ()))
702 {
703 if (dump_file && (dump_flags & TDF_DETAILS))
704 fprintf (dump_file,
705 " Refused: return bb has extra PHIs\n");
706 return;
707 }
708 }
709
710 if (dump_file && (dump_flags & TDF_DETAILS))
711 fprintf (dump_file, " Accepted!\n");
712
713 /* At the moment chose split point with lowest frequency and that leaves
714 out smallest size of header.
715 In future we might re-consider this heuristics. */
716 if (!best_split_point.split_bbs
717 || best_split_point.entry_bb->frequency > current->entry_bb->frequency
718 || (best_split_point.entry_bb->frequency == current->entry_bb->frequency
719 && best_split_point.split_size < current->split_size))
720
721 {
722 if (dump_file && (dump_flags & TDF_DETAILS))
723 fprintf (dump_file, " New best split point!\n");
724 if (best_split_point.ssa_names_to_pass)
725 {
726 BITMAP_FREE (best_split_point.ssa_names_to_pass);
727 BITMAP_FREE (best_split_point.split_bbs);
728 }
729 best_split_point = *current;
730 best_split_point.ssa_names_to_pass = BITMAP_ALLOC (NULL);
731 bitmap_copy (best_split_point.ssa_names_to_pass,
732 current->ssa_names_to_pass);
733 best_split_point.split_bbs = BITMAP_ALLOC (NULL);
734 bitmap_copy (best_split_point.split_bbs, current->split_bbs);
735 }
736 }
737
738 /* Return basic block containing RETURN statement. We allow basic blocks
739 of the form:
740 <retval> = tmp_var;
741 return <retval>
742 but return_bb can not be more complex than this (except for
743 -fsanitize=thread we allow TSAN_FUNC_EXIT () internal call in there).
744 If nothing is found, return the exit block.
745
746 When there are multiple RETURN statement, chose one with return value,
747 since that one is more likely shared by multiple code paths.
748
749 Return BB is special, because for function splitting it is the only
750 basic block that is duplicated in between header and split part of the
751 function.
752
753 TODO: We might support multiple return blocks. */
754
755 static basic_block
find_return_bb(void)756 find_return_bb (void)
757 {
758 edge e;
759 basic_block return_bb = EXIT_BLOCK_PTR_FOR_FN (cfun);
760 gimple_stmt_iterator bsi;
761 bool found_return = false;
762 tree retval = NULL_TREE;
763
764 if (!single_pred_p (EXIT_BLOCK_PTR_FOR_FN (cfun)))
765 return return_bb;
766
767 e = single_pred_edge (EXIT_BLOCK_PTR_FOR_FN (cfun));
768 for (bsi = gsi_last_bb (e->src); !gsi_end_p (bsi); gsi_prev (&bsi))
769 {
770 gimple *stmt = gsi_stmt (bsi);
771 if (gimple_code (stmt) == GIMPLE_LABEL
772 || is_gimple_debug (stmt)
773 || gimple_clobber_p (stmt))
774 ;
775 else if (gimple_code (stmt) == GIMPLE_ASSIGN
776 && found_return
777 && gimple_assign_single_p (stmt)
778 && (auto_var_in_fn_p (gimple_assign_rhs1 (stmt),
779 current_function_decl)
780 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
781 && retval == gimple_assign_lhs (stmt))
782 ;
783 else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
784 {
785 found_return = true;
786 retval = gimple_return_retval (return_stmt);
787 }
788 /* For -fsanitize=thread, allow also TSAN_FUNC_EXIT () in the return
789 bb. */
790 else if ((flag_sanitize & SANITIZE_THREAD)
791 && is_gimple_call (stmt)
792 && gimple_call_internal_p (stmt)
793 && gimple_call_internal_fn (stmt) == IFN_TSAN_FUNC_EXIT)
794 ;
795 else
796 break;
797 }
798 if (gsi_end_p (bsi) && found_return)
799 return_bb = e->src;
800
801 return return_bb;
802 }
803
804 /* Given return basic block RETURN_BB, see where return value is really
805 stored. */
806 static tree
find_retval(basic_block return_bb)807 find_retval (basic_block return_bb)
808 {
809 gimple_stmt_iterator bsi;
810 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
811 if (greturn *return_stmt = dyn_cast <greturn *> (gsi_stmt (bsi)))
812 return gimple_return_retval (return_stmt);
813 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
814 && !gimple_clobber_p (gsi_stmt (bsi)))
815 return gimple_assign_rhs1 (gsi_stmt (bsi));
816 return NULL;
817 }
818
819 /* Given return basic block RETURN_BB, see where return bounds are really
820 stored. */
821 static tree
find_retbnd(basic_block return_bb)822 find_retbnd (basic_block return_bb)
823 {
824 gimple_stmt_iterator bsi;
825 for (bsi = gsi_last_bb (return_bb); !gsi_end_p (bsi); gsi_prev (&bsi))
826 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
827 return gimple_return_retbnd (gsi_stmt (bsi));
828 return NULL;
829 }
830
831 /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
832 variable, mark it as used in bitmap passed via DATA.
833 Return true when access to T prevents splitting the function. */
834
835 static bool
mark_nonssa_use(gimple *,tree t,tree,void * data)836 mark_nonssa_use (gimple *, tree t, tree, void *data)
837 {
838 t = get_base_address (t);
839
840 if (!t || is_gimple_reg (t))
841 return false;
842
843 /* At present we can't pass non-SSA arguments to split function.
844 FIXME: this can be relaxed by passing references to arguments. */
845 if (TREE_CODE (t) == PARM_DECL)
846 {
847 if (dump_file && (dump_flags & TDF_DETAILS))
848 fprintf (dump_file,
849 "Cannot split: use of non-ssa function parameter.\n");
850 return true;
851 }
852
853 if ((TREE_CODE (t) == VAR_DECL
854 && auto_var_in_fn_p (t, current_function_decl))
855 || TREE_CODE (t) == RESULT_DECL
856 || (TREE_CODE (t) == LABEL_DECL
857 && FORCED_LABEL (t)))
858 bitmap_set_bit ((bitmap)data, DECL_UID (t));
859
860 /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
861 to pretend that the value pointed to is actual result decl. */
862 if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
863 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
864 && SSA_NAME_VAR (TREE_OPERAND (t, 0))
865 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
866 && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
867 return
868 bitmap_bit_p ((bitmap)data,
869 DECL_UID (DECL_RESULT (current_function_decl)));
870
871 return false;
872 }
873
874 /* Compute local properties of basic block BB we collect when looking for
875 split points. We look for ssa defs and store them in SET_SSA_NAMES,
876 for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic
877 vars stored in NON_SSA_VARS.
878
879 When BB has edge to RETURN_BB, collect uses in RETURN_BB too.
880
881 Return false when BB contains something that prevents it from being put into
882 split function. */
883
884 static bool
visit_bb(basic_block bb,basic_block return_bb,bitmap set_ssa_names,bitmap used_ssa_names,bitmap non_ssa_vars)885 visit_bb (basic_block bb, basic_block return_bb,
886 bitmap set_ssa_names, bitmap used_ssa_names,
887 bitmap non_ssa_vars)
888 {
889 edge e;
890 edge_iterator ei;
891 bool can_split = true;
892
893 for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
894 gsi_next (&bsi))
895 {
896 gimple *stmt = gsi_stmt (bsi);
897 tree op;
898 ssa_op_iter iter;
899 tree decl;
900
901 if (is_gimple_debug (stmt))
902 continue;
903
904 if (gimple_clobber_p (stmt))
905 continue;
906
907 /* FIXME: We can split regions containing EH. We can not however
908 split RESX, EH_DISPATCH and EH_POINTER referring to same region
909 into different partitions. This would require tracking of
910 EH regions and checking in consider_split_point if they
911 are not used elsewhere. */
912 if (gimple_code (stmt) == GIMPLE_RESX)
913 {
914 if (dump_file && (dump_flags & TDF_DETAILS))
915 fprintf (dump_file, "Cannot split: resx.\n");
916 can_split = false;
917 }
918 if (gimple_code (stmt) == GIMPLE_EH_DISPATCH)
919 {
920 if (dump_file && (dump_flags & TDF_DETAILS))
921 fprintf (dump_file, "Cannot split: eh dispatch.\n");
922 can_split = false;
923 }
924
925 /* Check builtins that prevent splitting. */
926 if (gimple_code (stmt) == GIMPLE_CALL
927 && (decl = gimple_call_fndecl (stmt)) != NULL_TREE
928 && DECL_BUILT_IN (decl)
929 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
930 switch (DECL_FUNCTION_CODE (decl))
931 {
932 /* FIXME: once we will allow passing non-parm values to split part,
933 we need to be sure to handle correct builtin_stack_save and
934 builtin_stack_restore. At the moment we are safe; there is no
935 way to store builtin_stack_save result in non-SSA variable
936 since all calls to those are compiler generated. */
937 case BUILT_IN_APPLY:
938 case BUILT_IN_APPLY_ARGS:
939 case BUILT_IN_VA_START:
940 if (dump_file && (dump_flags & TDF_DETAILS))
941 fprintf (dump_file,
942 "Cannot split: builtin_apply and va_start.\n");
943 can_split = false;
944 break;
945 case BUILT_IN_EH_POINTER:
946 if (dump_file && (dump_flags & TDF_DETAILS))
947 fprintf (dump_file, "Cannot split: builtin_eh_pointer.\n");
948 can_split = false;
949 break;
950 default:
951 break;
952 }
953
954 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
955 bitmap_set_bit (set_ssa_names, SSA_NAME_VERSION (op));
956 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
957 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
958 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
959 mark_nonssa_use,
960 mark_nonssa_use,
961 mark_nonssa_use);
962 }
963 for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
964 gsi_next (&bsi))
965 {
966 gphi *stmt = bsi.phi ();
967 unsigned int i;
968
969 if (virtual_operand_p (gimple_phi_result (stmt)))
970 continue;
971 bitmap_set_bit (set_ssa_names,
972 SSA_NAME_VERSION (gimple_phi_result (stmt)));
973 for (i = 0; i < gimple_phi_num_args (stmt); i++)
974 {
975 tree op = gimple_phi_arg_def (stmt, i);
976 if (TREE_CODE (op) == SSA_NAME)
977 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
978 }
979 can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
980 mark_nonssa_use,
981 mark_nonssa_use,
982 mark_nonssa_use);
983 }
984 /* Record also uses coming from PHI operand in return BB. */
985 FOR_EACH_EDGE (e, ei, bb->succs)
986 if (e->dest == return_bb)
987 {
988 for (gphi_iterator bsi = gsi_start_phis (return_bb);
989 !gsi_end_p (bsi);
990 gsi_next (&bsi))
991 {
992 gphi *stmt = bsi.phi ();
993 tree op = gimple_phi_arg_def (stmt, e->dest_idx);
994
995 if (virtual_operand_p (gimple_phi_result (stmt)))
996 continue;
997 if (TREE_CODE (op) == SSA_NAME)
998 bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
999 else
1000 can_split &= !mark_nonssa_use (stmt, op, op, non_ssa_vars);
1001 }
1002 }
1003 return can_split;
1004 }
1005
1006 /* Stack entry for recursive DFS walk in find_split_point. */
1007
1008 struct stack_entry
1009 {
1010 /* Basic block we are examining. */
1011 basic_block bb;
1012
1013 /* SSA names set and used by the BB and all BBs reachable
1014 from it via DFS walk. */
1015 bitmap set_ssa_names, used_ssa_names;
1016 bitmap non_ssa_vars;
1017
1018 /* All BBS visited from this BB via DFS walk. */
1019 bitmap bbs_visited;
1020
1021 /* Last examined edge in DFS walk. Since we walk unoriented graph,
1022 the value is up to sum of incoming and outgoing edges of BB. */
1023 unsigned int edge_num;
1024
1025 /* Stack entry index of earliest BB reachable from current BB
1026 or any BB visited later in DFS walk. */
1027 int earliest;
1028
1029 /* Overall time and size of all BBs reached from this BB in DFS walk. */
1030 int overall_time, overall_size;
1031
1032 /* When false we can not split on this BB. */
1033 bool can_split;
1034 };
1035
1036
1037 /* Find all articulations and call consider_split on them.
1038 OVERALL_TIME and OVERALL_SIZE is time and size of the function.
1039
1040 We perform basic algorithm for finding an articulation in a graph
1041 created from CFG by considering it to be an unoriented graph.
1042
1043 The articulation is discovered via DFS walk. We collect earliest
1044 basic block on stack that is reachable via backward edge. Articulation
1045 is any basic block such that there is no backward edge bypassing it.
1046 To reduce stack usage we maintain heap allocated stack in STACK vector.
1047 AUX pointer of BB is set to index it appears in the stack or -1 once
1048 it is visited and popped off the stack.
1049
1050 The algorithm finds articulation after visiting the whole component
1051 reachable by it. This makes it convenient to collect information about
1052 the component used by consider_split. */
1053
1054 static void
find_split_points(basic_block return_bb,int overall_time,int overall_size)1055 find_split_points (basic_block return_bb, int overall_time, int overall_size)
1056 {
1057 stack_entry first;
1058 vec<stack_entry> stack = vNULL;
1059 basic_block bb;
1060 struct split_point current;
1061
1062 current.header_time = overall_time;
1063 current.header_size = overall_size;
1064 current.split_time = 0;
1065 current.split_size = 0;
1066 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
1067
1068 first.bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1069 first.edge_num = 0;
1070 first.overall_time = 0;
1071 first.overall_size = 0;
1072 first.earliest = INT_MAX;
1073 first.set_ssa_names = 0;
1074 first.used_ssa_names = 0;
1075 first.non_ssa_vars = 0;
1076 first.bbs_visited = 0;
1077 first.can_split = false;
1078 stack.safe_push (first);
1079 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = (void *)(intptr_t)-1;
1080
1081 while (!stack.is_empty ())
1082 {
1083 stack_entry *entry = &stack.last ();
1084
1085 /* We are walking an acyclic graph, so edge_num counts
1086 succ and pred edges together. However when considering
1087 articulation, we want to have processed everything reachable
1088 from articulation but nothing that reaches into it. */
1089 if (entry->edge_num == EDGE_COUNT (entry->bb->succs)
1090 && entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1091 {
1092 int pos = stack.length ();
1093 entry->can_split &= visit_bb (entry->bb, return_bb,
1094 entry->set_ssa_names,
1095 entry->used_ssa_names,
1096 entry->non_ssa_vars);
1097 if (pos <= entry->earliest && !entry->can_split
1098 && dump_file && (dump_flags & TDF_DETAILS))
1099 fprintf (dump_file,
1100 "found articulation at bb %i but can not split\n",
1101 entry->bb->index);
1102 if (pos <= entry->earliest && entry->can_split)
1103 {
1104 if (dump_file && (dump_flags & TDF_DETAILS))
1105 fprintf (dump_file, "found articulation at bb %i\n",
1106 entry->bb->index);
1107 current.entry_bb = entry->bb;
1108 current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
1109 bitmap_and_compl (current.ssa_names_to_pass,
1110 entry->used_ssa_names, entry->set_ssa_names);
1111 current.header_time = overall_time - entry->overall_time;
1112 current.header_size = overall_size - entry->overall_size;
1113 current.split_time = entry->overall_time;
1114 current.split_size = entry->overall_size;
1115 current.split_bbs = entry->bbs_visited;
1116 consider_split (¤t, entry->non_ssa_vars, return_bb);
1117 BITMAP_FREE (current.ssa_names_to_pass);
1118 }
1119 }
1120 /* Do actual DFS walk. */
1121 if (entry->edge_num
1122 < (EDGE_COUNT (entry->bb->succs)
1123 + EDGE_COUNT (entry->bb->preds)))
1124 {
1125 edge e;
1126 basic_block dest;
1127 if (entry->edge_num < EDGE_COUNT (entry->bb->succs))
1128 {
1129 e = EDGE_SUCC (entry->bb, entry->edge_num);
1130 dest = e->dest;
1131 }
1132 else
1133 {
1134 e = EDGE_PRED (entry->bb, entry->edge_num
1135 - EDGE_COUNT (entry->bb->succs));
1136 dest = e->src;
1137 }
1138
1139 entry->edge_num++;
1140
1141 /* New BB to visit, push it to the stack. */
1142 if (dest != return_bb && dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
1143 && !dest->aux)
1144 {
1145 stack_entry new_entry;
1146
1147 new_entry.bb = dest;
1148 new_entry.edge_num = 0;
1149 new_entry.overall_time
1150 = bb_info_vec[dest->index].time;
1151 new_entry.overall_size
1152 = bb_info_vec[dest->index].size;
1153 new_entry.earliest = INT_MAX;
1154 new_entry.set_ssa_names = BITMAP_ALLOC (NULL);
1155 new_entry.used_ssa_names = BITMAP_ALLOC (NULL);
1156 new_entry.bbs_visited = BITMAP_ALLOC (NULL);
1157 new_entry.non_ssa_vars = BITMAP_ALLOC (NULL);
1158 new_entry.can_split = true;
1159 bitmap_set_bit (new_entry.bbs_visited, dest->index);
1160 stack.safe_push (new_entry);
1161 dest->aux = (void *)(intptr_t)stack.length ();
1162 }
1163 /* Back edge found, record the earliest point. */
1164 else if ((intptr_t)dest->aux > 0
1165 && (intptr_t)dest->aux < entry->earliest)
1166 entry->earliest = (intptr_t)dest->aux;
1167 }
1168 /* We are done with examining the edges. Pop off the value from stack
1169 and merge stuff we accumulate during the walk. */
1170 else if (entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1171 {
1172 stack_entry *prev = &stack[stack.length () - 2];
1173
1174 entry->bb->aux = (void *)(intptr_t)-1;
1175 prev->can_split &= entry->can_split;
1176 if (prev->set_ssa_names)
1177 {
1178 bitmap_ior_into (prev->set_ssa_names, entry->set_ssa_names);
1179 bitmap_ior_into (prev->used_ssa_names, entry->used_ssa_names);
1180 bitmap_ior_into (prev->bbs_visited, entry->bbs_visited);
1181 bitmap_ior_into (prev->non_ssa_vars, entry->non_ssa_vars);
1182 }
1183 if (prev->earliest > entry->earliest)
1184 prev->earliest = entry->earliest;
1185 prev->overall_time += entry->overall_time;
1186 prev->overall_size += entry->overall_size;
1187 BITMAP_FREE (entry->set_ssa_names);
1188 BITMAP_FREE (entry->used_ssa_names);
1189 BITMAP_FREE (entry->bbs_visited);
1190 BITMAP_FREE (entry->non_ssa_vars);
1191 stack.pop ();
1192 }
1193 else
1194 stack.pop ();
1195 }
1196 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = NULL;
1197 FOR_EACH_BB_FN (bb, cfun)
1198 bb->aux = NULL;
1199 stack.release ();
1200 BITMAP_FREE (current.ssa_names_to_pass);
1201 }
1202
1203 /* Split function at SPLIT_POINT. */
1204
1205 static void
split_function(basic_block return_bb,struct split_point * split_point,bool add_tsan_func_exit)1206 split_function (basic_block return_bb, struct split_point *split_point,
1207 bool add_tsan_func_exit)
1208 {
1209 vec<tree> args_to_pass = vNULL;
1210 bitmap args_to_skip;
1211 tree parm;
1212 int num = 0;
1213 cgraph_node *node, *cur_node = cgraph_node::get (current_function_decl);
1214 basic_block call_bb;
1215 gcall *call, *tsan_func_exit_call = NULL;
1216 edge e;
1217 edge_iterator ei;
1218 tree retval = NULL, real_retval = NULL, retbnd = NULL;
1219 bool with_bounds = chkp_function_instrumented_p (current_function_decl);
1220 gimple *last_stmt = NULL;
1221 unsigned int i;
1222 tree arg, ddef;
1223
1224 if (dump_file)
1225 {
1226 fprintf (dump_file, "\n\nSplitting function at:\n");
1227 dump_split_point (dump_file, split_point);
1228 }
1229
1230 if (cur_node->local.can_change_signature)
1231 args_to_skip = BITMAP_ALLOC (NULL);
1232 else
1233 args_to_skip = NULL;
1234
1235 /* Collect the parameters of new function and args_to_skip bitmap. */
1236 for (parm = DECL_ARGUMENTS (current_function_decl);
1237 parm; parm = DECL_CHAIN (parm), num++)
1238 if (args_to_skip
1239 && (!is_gimple_reg (parm)
1240 || (ddef = ssa_default_def (cfun, parm)) == NULL_TREE
1241 || !bitmap_bit_p (split_point->ssa_names_to_pass,
1242 SSA_NAME_VERSION (ddef))))
1243 bitmap_set_bit (args_to_skip, num);
1244 else
1245 {
1246 /* This parm might not have been used up to now, but is going to be
1247 used, hence register it. */
1248 if (is_gimple_reg (parm))
1249 arg = get_or_create_ssa_default_def (cfun, parm);
1250 else
1251 arg = parm;
1252
1253 if (!useless_type_conversion_p (DECL_ARG_TYPE (parm), TREE_TYPE (arg)))
1254 arg = fold_convert (DECL_ARG_TYPE (parm), arg);
1255 args_to_pass.safe_push (arg);
1256 }
1257
1258 /* See if the split function will return. */
1259 bool split_part_return_p = false;
1260 FOR_EACH_EDGE (e, ei, return_bb->preds)
1261 {
1262 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1263 split_part_return_p = true;
1264 }
1265
1266 /* Add return block to what will become the split function.
1267 We do not return; no return block is needed. */
1268 if (!split_part_return_p)
1269 ;
1270 /* We have no return block, so nothing is needed. */
1271 else if (return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1272 ;
1273 /* When we do not want to return value, we need to construct
1274 new return block with empty return statement.
1275 FIXME: Once we are able to change return type, we should change function
1276 to return void instead of just outputting function with undefined return
1277 value. For structures this affects quality of codegen. */
1278 else if ((retval = find_retval (return_bb))
1279 && !split_point->split_part_set_retval)
1280 {
1281 bool redirected = true;
1282 basic_block new_return_bb = create_basic_block (NULL, 0, return_bb);
1283 gimple_stmt_iterator gsi = gsi_start_bb (new_return_bb);
1284 gsi_insert_after (&gsi, gimple_build_return (NULL), GSI_NEW_STMT);
1285 while (redirected)
1286 {
1287 redirected = false;
1288 FOR_EACH_EDGE (e, ei, return_bb->preds)
1289 if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1290 {
1291 new_return_bb->count += e->count;
1292 new_return_bb->frequency += EDGE_FREQUENCY (e);
1293 redirect_edge_and_branch (e, new_return_bb);
1294 redirected = true;
1295 break;
1296 }
1297 }
1298 e = make_edge (new_return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1299 e->probability = REG_BR_PROB_BASE;
1300 e->count = new_return_bb->count;
1301 add_bb_to_loop (new_return_bb, current_loops->tree_root);
1302 bitmap_set_bit (split_point->split_bbs, new_return_bb->index);
1303 retbnd = find_retbnd (return_bb);
1304 }
1305 /* When we pass around the value, use existing return block. */
1306 else
1307 {
1308 bitmap_set_bit (split_point->split_bbs, return_bb->index);
1309 retbnd = find_retbnd (return_bb);
1310 }
1311
1312 /* If RETURN_BB has virtual operand PHIs, they must be removed and the
1313 virtual operand marked for renaming as we change the CFG in a way that
1314 tree-inline is not able to compensate for.
1315
1316 Note this can happen whether or not we have a return value. If we have
1317 a return value, then RETURN_BB may have PHIs for real operands too. */
1318 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1319 {
1320 bool phi_p = false;
1321 for (gphi_iterator gsi = gsi_start_phis (return_bb);
1322 !gsi_end_p (gsi);)
1323 {
1324 gphi *stmt = gsi.phi ();
1325 if (!virtual_operand_p (gimple_phi_result (stmt)))
1326 {
1327 gsi_next (&gsi);
1328 continue;
1329 }
1330 mark_virtual_phi_result_for_renaming (stmt);
1331 remove_phi_node (&gsi, true);
1332 phi_p = true;
1333 }
1334 /* In reality we have to rename the reaching definition of the
1335 virtual operand at return_bb as we will eventually release it
1336 when we remove the code region we outlined.
1337 So we have to rename all immediate virtual uses of that region
1338 if we didn't see a PHI definition yet. */
1339 /* ??? In real reality we want to set the reaching vdef of the
1340 entry of the SESE region as the vuse of the call and the reaching
1341 vdef of the exit of the SESE region as the vdef of the call. */
1342 if (!phi_p)
1343 for (gimple_stmt_iterator gsi = gsi_start_bb (return_bb);
1344 !gsi_end_p (gsi);
1345 gsi_next (&gsi))
1346 {
1347 gimple *stmt = gsi_stmt (gsi);
1348 if (gimple_vuse (stmt))
1349 {
1350 gimple_set_vuse (stmt, NULL_TREE);
1351 update_stmt (stmt);
1352 }
1353 if (gimple_vdef (stmt))
1354 break;
1355 }
1356 }
1357
1358 /* Now create the actual clone. */
1359 cgraph_edge::rebuild_edges ();
1360 node = cur_node->create_version_clone_with_body
1361 (vNULL, NULL, args_to_skip,
1362 !split_part_return_p || !split_point->split_part_set_retval,
1363 split_point->split_bbs, split_point->entry_bb, "part");
1364
1365 node->split_part = true;
1366
1367 if (cur_node->same_comdat_group)
1368 {
1369 /* TODO: call is versionable if we make sure that all
1370 callers are inside of a comdat group. */
1371 cur_node->calls_comdat_local = 1;
1372 node->add_to_same_comdat_group (cur_node);
1373 }
1374
1375
1376 /* Let's take a time profile for splitted function. */
1377 node->tp_first_run = cur_node->tp_first_run + 1;
1378
1379 /* For usual cloning it is enough to clear builtin only when signature
1380 changes. For partial inlining we however can not expect the part
1381 of builtin implementation to have same semantic as the whole. */
1382 if (DECL_BUILT_IN (node->decl))
1383 {
1384 DECL_BUILT_IN_CLASS (node->decl) = NOT_BUILT_IN;
1385 DECL_FUNCTION_CODE (node->decl) = (enum built_in_function) 0;
1386 }
1387
1388 /* If return_bb contains any clobbers that refer to SSA_NAMEs
1389 set in the split part, remove them. Also reset debug stmts that
1390 refer to SSA_NAMEs set in the split part. */
1391 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1392 {
1393 gimple_stmt_iterator gsi = gsi_start_bb (return_bb);
1394 while (!gsi_end_p (gsi))
1395 {
1396 tree op;
1397 ssa_op_iter iter;
1398 gimple *stmt = gsi_stmt (gsi);
1399 bool remove = false;
1400 if (gimple_clobber_p (stmt) || is_gimple_debug (stmt))
1401 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
1402 {
1403 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (op));
1404 if (op != retval
1405 && bb
1406 && bb != return_bb
1407 && bitmap_bit_p (split_point->split_bbs, bb->index))
1408 {
1409 if (is_gimple_debug (stmt))
1410 {
1411 gimple_debug_bind_reset_value (stmt);
1412 update_stmt (stmt);
1413 }
1414 else
1415 remove = true;
1416 break;
1417 }
1418 }
1419 if (remove)
1420 gsi_remove (&gsi, true);
1421 else
1422 gsi_next (&gsi);
1423 }
1424 }
1425
1426 /* If the original function is instrumented then it's
1427 part is also instrumented. */
1428 if (with_bounds)
1429 chkp_function_mark_instrumented (node->decl);
1430
1431 /* If the original function is declared inline, there is no point in issuing
1432 a warning for the non-inlinable part. */
1433 DECL_NO_INLINE_WARNING_P (node->decl) = 1;
1434 cur_node->remove_callees ();
1435 cur_node->remove_all_references ();
1436 if (!split_part_return_p)
1437 TREE_THIS_VOLATILE (node->decl) = 1;
1438 if (dump_file)
1439 dump_function_to_file (node->decl, dump_file, dump_flags);
1440
1441 /* Create the basic block we place call into. It is the entry basic block
1442 split after last label. */
1443 call_bb = split_point->entry_bb;
1444 for (gimple_stmt_iterator gsi = gsi_start_bb (call_bb); !gsi_end_p (gsi);)
1445 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
1446 {
1447 last_stmt = gsi_stmt (gsi);
1448 gsi_next (&gsi);
1449 }
1450 else
1451 break;
1452 e = split_block (split_point->entry_bb, last_stmt);
1453 remove_edge (e);
1454
1455 /* Produce the call statement. */
1456 gimple_stmt_iterator gsi = gsi_last_bb (call_bb);
1457 FOR_EACH_VEC_ELT (args_to_pass, i, arg)
1458 if (!is_gimple_val (arg))
1459 {
1460 arg = force_gimple_operand_gsi (&gsi, arg, true, NULL_TREE,
1461 false, GSI_CONTINUE_LINKING);
1462 args_to_pass[i] = arg;
1463 }
1464 call = gimple_build_call_vec (node->decl, args_to_pass);
1465 gimple_call_set_with_bounds (call, with_bounds);
1466 gimple_set_block (call, DECL_INITIAL (current_function_decl));
1467 args_to_pass.release ();
1468
1469 /* For optimized away parameters, add on the caller side
1470 before the call
1471 DEBUG D#X => parm_Y(D)
1472 stmts and associate D#X with parm in decl_debug_args_lookup
1473 vector to say for debug info that if parameter parm had been passed,
1474 it would have value parm_Y(D). */
1475 if (args_to_skip)
1476 {
1477 vec<tree, va_gc> **debug_args = NULL;
1478 unsigned i = 0, len = 0;
1479 if (MAY_HAVE_DEBUG_STMTS)
1480 {
1481 debug_args = decl_debug_args_lookup (node->decl);
1482 if (debug_args)
1483 len = vec_safe_length (*debug_args);
1484 }
1485 for (parm = DECL_ARGUMENTS (current_function_decl), num = 0;
1486 parm; parm = DECL_CHAIN (parm), num++)
1487 if (bitmap_bit_p (args_to_skip, num) && is_gimple_reg (parm))
1488 {
1489 tree ddecl;
1490 gimple *def_temp;
1491
1492 /* This needs to be done even without MAY_HAVE_DEBUG_STMTS,
1493 otherwise if it didn't exist before, we'd end up with
1494 different SSA_NAME_VERSIONs between -g and -g0. */
1495 arg = get_or_create_ssa_default_def (cfun, parm);
1496 if (!MAY_HAVE_DEBUG_STMTS || debug_args == NULL)
1497 continue;
1498
1499 while (i < len && (**debug_args)[i] != DECL_ORIGIN (parm))
1500 i += 2;
1501 if (i >= len)
1502 continue;
1503 ddecl = (**debug_args)[i + 1];
1504 def_temp
1505 = gimple_build_debug_bind (ddecl, unshare_expr (arg), call);
1506 gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
1507 }
1508 }
1509
1510 /* We avoid address being taken on any variable used by split part,
1511 so return slot optimization is always possible. Moreover this is
1512 required to make DECL_BY_REFERENCE work. */
1513 if (aggregate_value_p (DECL_RESULT (current_function_decl),
1514 TREE_TYPE (current_function_decl))
1515 && (!is_gimple_reg_type (TREE_TYPE (DECL_RESULT (current_function_decl)))
1516 || DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))))
1517 gimple_call_set_return_slot_opt (call, true);
1518
1519 if (add_tsan_func_exit)
1520 tsan_func_exit_call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
1521
1522 /* Update return value. This is bit tricky. When we do not return,
1523 do nothing. When we return we might need to update return_bb
1524 or produce a new return statement. */
1525 if (!split_part_return_p)
1526 {
1527 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1528 if (tsan_func_exit_call)
1529 gsi_insert_after (&gsi, tsan_func_exit_call, GSI_NEW_STMT);
1530 }
1531 else
1532 {
1533 e = make_edge (call_bb, return_bb,
1534 return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
1535 ? 0 : EDGE_FALLTHRU);
1536 e->count = call_bb->count;
1537 e->probability = REG_BR_PROB_BASE;
1538
1539 /* If there is return basic block, see what value we need to store
1540 return value into and put call just before it. */
1541 if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1542 {
1543 real_retval = retval;
1544 if (real_retval && split_point->split_part_set_retval)
1545 {
1546 gphi_iterator psi;
1547
1548 /* See if we need new SSA_NAME for the result.
1549 When DECL_BY_REFERENCE is true, retval is actually pointer to
1550 return value and it is constant in whole function. */
1551 if (TREE_CODE (retval) == SSA_NAME
1552 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1553 {
1554 retval = copy_ssa_name (retval, call);
1555
1556 /* See if there is PHI defining return value. */
1557 for (psi = gsi_start_phis (return_bb);
1558 !gsi_end_p (psi); gsi_next (&psi))
1559 if (!virtual_operand_p (gimple_phi_result (psi.phi ())))
1560 break;
1561
1562 /* When there is PHI, just update its value. */
1563 if (TREE_CODE (retval) == SSA_NAME
1564 && !gsi_end_p (psi))
1565 add_phi_arg (psi.phi (), retval, e, UNKNOWN_LOCATION);
1566 /* Otherwise update the return BB itself.
1567 find_return_bb allows at most one assignment to return value,
1568 so update first statement. */
1569 else
1570 {
1571 gimple_stmt_iterator bsi;
1572 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi);
1573 gsi_next (&bsi))
1574 if (greturn *return_stmt
1575 = dyn_cast <greturn *> (gsi_stmt (bsi)))
1576 {
1577 gimple_return_set_retval (return_stmt, retval);
1578 break;
1579 }
1580 else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
1581 && !gimple_clobber_p (gsi_stmt (bsi)))
1582 {
1583 gimple_assign_set_rhs1 (gsi_stmt (bsi), retval);
1584 break;
1585 }
1586 update_stmt (gsi_stmt (bsi));
1587 /* Also adjust clobbers and debug stmts in return_bb. */
1588 for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi);
1589 gsi_next (&bsi))
1590 {
1591 gimple *stmt = gsi_stmt (bsi);
1592 if (gimple_clobber_p (stmt)
1593 || is_gimple_debug (stmt))
1594 {
1595 ssa_op_iter iter;
1596 use_operand_p use_p;
1597 bool update = false;
1598 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter,
1599 SSA_OP_USE)
1600 if (USE_FROM_PTR (use_p) == real_retval)
1601 {
1602 SET_USE (use_p, retval);
1603 update = true;
1604 }
1605 if (update)
1606 update_stmt (stmt);
1607 }
1608 }
1609 }
1610
1611 /* Replace retbnd with new one. */
1612 if (retbnd)
1613 {
1614 gimple_stmt_iterator bsi;
1615 for (bsi = gsi_last_bb (return_bb); !gsi_end_p (bsi);
1616 gsi_prev (&bsi))
1617 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
1618 {
1619 retbnd = copy_ssa_name (retbnd, call);
1620 gimple_return_set_retbnd (gsi_stmt (bsi), retbnd);
1621 update_stmt (gsi_stmt (bsi));
1622 break;
1623 }
1624 }
1625 }
1626 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1627 {
1628 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1629 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1630 }
1631 else
1632 {
1633 tree restype;
1634 restype = TREE_TYPE (DECL_RESULT (current_function_decl));
1635 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1636 if (!useless_type_conversion_p (TREE_TYPE (retval), restype))
1637 {
1638 gimple *cpy;
1639 tree tem = create_tmp_reg (restype);
1640 tem = make_ssa_name (tem, call);
1641 cpy = gimple_build_assign (retval, NOP_EXPR, tem);
1642 gsi_insert_after (&gsi, cpy, GSI_NEW_STMT);
1643 retval = tem;
1644 }
1645 /* Build bndret call to obtain returned bounds. */
1646 if (retbnd)
1647 chkp_insert_retbnd_call (retbnd, retval, &gsi);
1648 gimple_call_set_lhs (call, retval);
1649 update_stmt (call);
1650 }
1651 }
1652 else
1653 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1654 if (tsan_func_exit_call)
1655 gsi_insert_after (&gsi, tsan_func_exit_call, GSI_NEW_STMT);
1656 }
1657 /* We don't use return block (there is either no return in function or
1658 multiple of them). So create new basic block with return statement.
1659 */
1660 else
1661 {
1662 greturn *ret;
1663 if (split_point->split_part_set_retval
1664 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1665 {
1666 retval = DECL_RESULT (current_function_decl);
1667
1668 if (chkp_function_instrumented_p (current_function_decl)
1669 && BOUNDED_P (retval))
1670 retbnd = create_tmp_reg (pointer_bounds_type_node);
1671
1672 /* We use temporary register to hold value when aggregate_value_p
1673 is false. Similarly for DECL_BY_REFERENCE we must avoid extra
1674 copy. */
1675 if (!aggregate_value_p (retval, TREE_TYPE (current_function_decl))
1676 && !DECL_BY_REFERENCE (retval))
1677 retval = create_tmp_reg (TREE_TYPE (retval));
1678 if (is_gimple_reg (retval))
1679 {
1680 /* When returning by reference, there is only one SSA name
1681 assigned to RESULT_DECL (that is pointer to return value).
1682 Look it up or create new one if it is missing. */
1683 if (DECL_BY_REFERENCE (retval))
1684 retval = get_or_create_ssa_default_def (cfun, retval);
1685 /* Otherwise produce new SSA name for return value. */
1686 else
1687 retval = make_ssa_name (retval, call);
1688 }
1689 if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1690 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1691 else
1692 gimple_call_set_lhs (call, retval);
1693 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1694 }
1695 else
1696 {
1697 gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1698 if (retval
1699 && is_gimple_reg_type (TREE_TYPE (retval))
1700 && !is_gimple_val (retval))
1701 {
1702 gassign *g
1703 = gimple_build_assign (make_ssa_name (TREE_TYPE (retval)),
1704 retval);
1705 retval = gimple_assign_lhs (g);
1706 gsi_insert_after (&gsi, g, GSI_NEW_STMT);
1707 }
1708 }
1709 /* Build bndret call to obtain returned bounds. */
1710 if (retbnd)
1711 chkp_insert_retbnd_call (retbnd, retval, &gsi);
1712 if (tsan_func_exit_call)
1713 gsi_insert_after (&gsi, tsan_func_exit_call, GSI_NEW_STMT);
1714 ret = gimple_build_return (retval);
1715 gsi_insert_after (&gsi, ret, GSI_NEW_STMT);
1716 }
1717 }
1718 free_dominance_info (CDI_DOMINATORS);
1719 free_dominance_info (CDI_POST_DOMINATORS);
1720 compute_inline_parameters (node, true);
1721 }
1722
1723 /* Execute function splitting pass. */
1724
1725 static unsigned int
execute_split_functions(void)1726 execute_split_functions (void)
1727 {
1728 gimple_stmt_iterator bsi;
1729 basic_block bb;
1730 int overall_time = 0, overall_size = 0;
1731 int todo = 0;
1732 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1733
1734 if (flags_from_decl_or_type (current_function_decl)
1735 & (ECF_NORETURN|ECF_MALLOC))
1736 {
1737 if (dump_file)
1738 fprintf (dump_file, "Not splitting: noreturn/malloc function.\n");
1739 return 0;
1740 }
1741 if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
1742 {
1743 if (dump_file)
1744 fprintf (dump_file, "Not splitting: main function.\n");
1745 return 0;
1746 }
1747 /* This can be relaxed; function might become inlinable after splitting
1748 away the uninlinable part. */
1749 if (inline_edge_summary_vec.exists ()
1750 && !inline_summaries->get (node)->inlinable)
1751 {
1752 if (dump_file)
1753 fprintf (dump_file, "Not splitting: not inlinable.\n");
1754 return 0;
1755 }
1756 if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1757 {
1758 if (dump_file)
1759 fprintf (dump_file, "Not splitting: disregarding inline limits.\n");
1760 return 0;
1761 }
1762 /* This can be relaxed; most of versioning tests actually prevents
1763 a duplication. */
1764 if (!tree_versionable_function_p (current_function_decl))
1765 {
1766 if (dump_file)
1767 fprintf (dump_file, "Not splitting: not versionable.\n");
1768 return 0;
1769 }
1770 /* FIXME: we could support this. */
1771 if (DECL_STRUCT_FUNCTION (current_function_decl)->static_chain_decl)
1772 {
1773 if (dump_file)
1774 fprintf (dump_file, "Not splitting: nested function.\n");
1775 return 0;
1776 }
1777
1778 /* See if it makes sense to try to split.
1779 It makes sense to split if we inline, that is if we have direct calls to
1780 handle or direct calls are possibly going to appear as result of indirect
1781 inlining or LTO. Also handle -fprofile-generate as LTO to allow non-LTO
1782 training for LTO -fprofile-use build.
1783
1784 Note that we are not completely conservative about disqualifying functions
1785 called once. It is possible that the caller is called more then once and
1786 then inlining would still benefit. */
1787 if ((!node->callers
1788 /* Local functions called once will be completely inlined most of time. */
1789 || (!node->callers->next_caller && node->local.local))
1790 && !node->address_taken
1791 && !node->has_aliases_p ()
1792 && (!flag_lto || !node->externally_visible))
1793 {
1794 if (dump_file)
1795 fprintf (dump_file, "Not splitting: not called directly "
1796 "or called once.\n");
1797 return 0;
1798 }
1799
1800 /* FIXME: We can actually split if splitting reduces call overhead. */
1801 if (!flag_inline_small_functions
1802 && !DECL_DECLARED_INLINE_P (current_function_decl))
1803 {
1804 if (dump_file)
1805 fprintf (dump_file, "Not splitting: not autoinlining and function"
1806 " is not inline.\n");
1807 return 0;
1808 }
1809
1810 /* We enforce splitting after loop headers when profile info is not
1811 available. */
1812 if (profile_status_for_fn (cfun) != PROFILE_READ)
1813 mark_dfs_back_edges ();
1814
1815 /* Initialize bitmap to track forbidden calls. */
1816 forbidden_dominators = BITMAP_ALLOC (NULL);
1817 calculate_dominance_info (CDI_DOMINATORS);
1818
1819 /* Compute local info about basic blocks and determine function size/time. */
1820 bb_info_vec.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
1821 memset (&best_split_point, 0, sizeof (best_split_point));
1822 basic_block return_bb = find_return_bb ();
1823 int tsan_exit_found = -1;
1824 FOR_EACH_BB_FN (bb, cfun)
1825 {
1826 int time = 0;
1827 int size = 0;
1828 int freq = compute_call_stmt_bb_frequency (current_function_decl, bb);
1829
1830 if (dump_file && (dump_flags & TDF_DETAILS))
1831 fprintf (dump_file, "Basic block %i\n", bb->index);
1832
1833 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1834 {
1835 int this_time, this_size;
1836 gimple *stmt = gsi_stmt (bsi);
1837
1838 this_size = estimate_num_insns (stmt, &eni_size_weights);
1839 this_time = estimate_num_insns (stmt, &eni_time_weights) * freq;
1840 size += this_size;
1841 time += this_time;
1842 check_forbidden_calls (stmt);
1843
1844 if (dump_file && (dump_flags & TDF_DETAILS))
1845 {
1846 fprintf (dump_file, " freq:%6i size:%3i time:%3i ",
1847 freq, this_size, this_time);
1848 print_gimple_stmt (dump_file, stmt, 0, 0);
1849 }
1850
1851 if ((flag_sanitize & SANITIZE_THREAD)
1852 && is_gimple_call (stmt)
1853 && gimple_call_internal_p (stmt)
1854 && gimple_call_internal_fn (stmt) == IFN_TSAN_FUNC_EXIT)
1855 {
1856 /* We handle TSAN_FUNC_EXIT for splitting either in the
1857 return_bb, or in its immediate predecessors. */
1858 if ((bb != return_bb && !find_edge (bb, return_bb))
1859 || (tsan_exit_found != -1
1860 && tsan_exit_found != (bb != return_bb)))
1861 {
1862 if (dump_file)
1863 fprintf (dump_file, "Not splitting: TSAN_FUNC_EXIT"
1864 " in unexpected basic block.\n");
1865 BITMAP_FREE (forbidden_dominators);
1866 bb_info_vec.release ();
1867 return 0;
1868 }
1869 tsan_exit_found = bb != return_bb;
1870 }
1871 }
1872 overall_time += time;
1873 overall_size += size;
1874 bb_info_vec[bb->index].time = time;
1875 bb_info_vec[bb->index].size = size;
1876 }
1877 find_split_points (return_bb, overall_time, overall_size);
1878 if (best_split_point.split_bbs)
1879 {
1880 split_function (return_bb, &best_split_point, tsan_exit_found == 1);
1881 BITMAP_FREE (best_split_point.ssa_names_to_pass);
1882 BITMAP_FREE (best_split_point.split_bbs);
1883 todo = TODO_update_ssa | TODO_cleanup_cfg;
1884 }
1885 BITMAP_FREE (forbidden_dominators);
1886 bb_info_vec.release ();
1887 return todo;
1888 }
1889
1890 namespace {
1891
1892 const pass_data pass_data_split_functions =
1893 {
1894 GIMPLE_PASS, /* type */
1895 "fnsplit", /* name */
1896 OPTGROUP_NONE, /* optinfo_flags */
1897 TV_IPA_FNSPLIT, /* tv_id */
1898 PROP_cfg, /* properties_required */
1899 0, /* properties_provided */
1900 0, /* properties_destroyed */
1901 0, /* todo_flags_start */
1902 0, /* todo_flags_finish */
1903 };
1904
1905 class pass_split_functions : public gimple_opt_pass
1906 {
1907 public:
pass_split_functions(gcc::context * ctxt)1908 pass_split_functions (gcc::context *ctxt)
1909 : gimple_opt_pass (pass_data_split_functions, ctxt)
1910 {}
1911
1912 /* opt_pass methods: */
1913 virtual bool gate (function *);
execute(function *)1914 virtual unsigned int execute (function *)
1915 {
1916 return execute_split_functions ();
1917 }
1918
1919 }; // class pass_split_functions
1920
1921 bool
gate(function *)1922 pass_split_functions::gate (function *)
1923 {
1924 /* When doing profile feedback, we want to execute the pass after profiling
1925 is read. So disable one in early optimization. */
1926 return (flag_partial_inlining
1927 && !profile_arc_flag && !flag_branch_probabilities);
1928 }
1929
1930 } // anon namespace
1931
1932 gimple_opt_pass *
make_pass_split_functions(gcc::context * ctxt)1933 make_pass_split_functions (gcc::context *ctxt)
1934 {
1935 return new pass_split_functions (ctxt);
1936 }
1937
1938 /* Execute function splitting pass. */
1939
1940 static unsigned int
execute_feedback_split_functions(void)1941 execute_feedback_split_functions (void)
1942 {
1943 unsigned int retval = execute_split_functions ();
1944 if (retval)
1945 retval |= TODO_rebuild_cgraph_edges;
1946 return retval;
1947 }
1948
1949 namespace {
1950
1951 const pass_data pass_data_feedback_split_functions =
1952 {
1953 GIMPLE_PASS, /* type */
1954 "feedback_fnsplit", /* name */
1955 OPTGROUP_NONE, /* optinfo_flags */
1956 TV_IPA_FNSPLIT, /* tv_id */
1957 PROP_cfg, /* properties_required */
1958 0, /* properties_provided */
1959 0, /* properties_destroyed */
1960 0, /* todo_flags_start */
1961 0, /* todo_flags_finish */
1962 };
1963
1964 class pass_feedback_split_functions : public gimple_opt_pass
1965 {
1966 public:
pass_feedback_split_functions(gcc::context * ctxt)1967 pass_feedback_split_functions (gcc::context *ctxt)
1968 : gimple_opt_pass (pass_data_feedback_split_functions, ctxt)
1969 {}
1970
1971 /* opt_pass methods: */
1972 virtual bool gate (function *);
execute(function *)1973 virtual unsigned int execute (function *)
1974 {
1975 return execute_feedback_split_functions ();
1976 }
1977
1978 }; // class pass_feedback_split_functions
1979
1980 bool
gate(function *)1981 pass_feedback_split_functions::gate (function *)
1982 {
1983 /* We don't need to split when profiling at all, we are producing
1984 lousy code anyway. */
1985 return (flag_partial_inlining
1986 && flag_branch_probabilities);
1987 }
1988
1989 } // anon namespace
1990
1991 gimple_opt_pass *
make_pass_feedback_split_functions(gcc::context * ctxt)1992 make_pass_feedback_split_functions (gcc::context *ctxt)
1993 {
1994 return new pass_feedback_split_functions (ctxt);
1995 }
1996