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