Line data Source code
1 : /* Function splitting pass
2 : Copyright (C) 2010-2026 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 "sreal.h"
99 : #include "ipa-cp.h"
100 : #include "ipa-prop.h"
101 : #include "tree-cfg.h"
102 : #include "tree-into-ssa.h"
103 : #include "tree-dfa.h"
104 : #include "tree-inline.h"
105 : #include "gimple-pretty-print.h"
106 : #include "ipa-fnsummary.h"
107 : #include "cfgloop.h"
108 : #include "attribs.h"
109 : #include "ipa-strub.h"
110 :
111 : /* Per basic block info. */
112 :
113 8809541 : class split_bb_info
114 : {
115 : public:
116 : unsigned int size;
117 : sreal time;
118 : };
119 :
120 : static vec<split_bb_info> bb_info_vec;
121 :
122 : /* Description of split point. */
123 :
124 1312827 : class split_point
125 : {
126 : public:
127 : /* Size of the partitions. */
128 : sreal header_time, split_time;
129 : unsigned int header_size, split_size;
130 :
131 : /* SSA names that need to be passed into spit function. */
132 : bitmap ssa_names_to_pass;
133 :
134 : /* Basic block where we split (that will become entry point of new function. */
135 : basic_block entry_bb;
136 :
137 : /* Count for entering the split part.
138 : This is not count of the entry_bb because it may be in loop. */
139 : profile_count count;
140 :
141 : /* Basic blocks we are splitting away. */
142 : bitmap split_bbs;
143 :
144 : /* True when return value is computed on split part and thus it needs
145 : to be returned. */
146 : bool split_part_set_retval;
147 : };
148 :
149 : /* Best split point found. */
150 :
151 : class split_point best_split_point;
152 :
153 : /* Set of basic blocks that are not allowed to dominate a split point. */
154 :
155 : static bitmap forbidden_dominators;
156 :
157 : static tree find_retval (basic_block return_bb);
158 :
159 : /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
160 : variable, check it if it is present in bitmap passed via DATA. */
161 :
162 : static bool
163 28565 : test_nonssa_use (gimple *, tree t, tree, void *data)
164 : {
165 28565 : t = get_base_address (t);
166 :
167 28565 : if (!t || is_gimple_reg (t))
168 : return false;
169 :
170 28565 : if (TREE_CODE (t) == PARM_DECL
171 28484 : || (VAR_P (t)
172 5746 : && auto_var_in_fn_p (t, current_function_decl))
173 23578 : || TREE_CODE (t) == RESULT_DECL
174 : /* Normal labels are part of CFG and will be handled gratefully.
175 : Forced labels however can be used directly by statements and
176 : need to stay in one partition along with their uses. */
177 51577 : || (TREE_CODE (t) == LABEL_DECL
178 5707 : && FORCED_LABEL (t)))
179 5556 : return bitmap_bit_p ((bitmap)data, DECL_UID (t));
180 :
181 : /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
182 : to pretend that the value pointed to is actual result decl. */
183 8059 : if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
184 14950 : && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
185 14946 : && SSA_NAME_VAR (TREE_OPERAND (t, 0))
186 11747 : && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
187 23009 : && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
188 0 : return
189 0 : bitmap_bit_p ((bitmap)data,
190 0 : DECL_UID (DECL_RESULT (current_function_decl)));
191 :
192 : return false;
193 : }
194 :
195 : /* Dump split point CURRENT. */
196 :
197 : static void
198 13 : dump_split_point (FILE * file, class split_point *current)
199 : {
200 13 : fprintf (file,
201 : "Split point at BB %i\n"
202 : " header time: %f header size: %i\n"
203 : " split time: %f split size: %i\n bbs: ",
204 13 : current->entry_bb->index, current->header_time.to_double (),
205 : current->header_size, current->split_time.to_double (),
206 : current->split_size);
207 13 : dump_bitmap (file, current->split_bbs);
208 13 : fprintf (file, " SSA names to pass: ");
209 13 : dump_bitmap (file, current->ssa_names_to_pass);
210 13 : }
211 :
212 : /* Look for all BBs in header that might lead to the split part and verify
213 : that they are not defining any non-SSA var used by the split part.
214 : Parameters are the same as for consider_split. */
215 :
216 : static bool
217 12424 : verify_non_ssa_vars (class split_point *current, bitmap non_ssa_vars,
218 : basic_block return_bb)
219 : {
220 12424 : bitmap seen = BITMAP_ALLOC (NULL);
221 12424 : vec<basic_block> worklist = vNULL;
222 12424 : edge e;
223 12424 : edge_iterator ei;
224 12424 : bool ok = true;
225 12424 : basic_block bb;
226 :
227 26469 : FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
228 14045 : if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
229 14045 : && !bitmap_bit_p (current->split_bbs, e->src->index))
230 : {
231 13521 : worklist.safe_push (e->src);
232 13521 : bitmap_set_bit (seen, e->src->index);
233 : }
234 :
235 29504 : while (!worklist.is_empty ())
236 : {
237 19947 : bb = worklist.pop ();
238 41758 : FOR_EACH_EDGE (e, ei, bb->preds)
239 21811 : if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
240 21811 : && bitmap_set_bit (seen, e->src->index))
241 : {
242 8542 : gcc_checking_assert (!bitmap_bit_p (current->split_bbs,
243 : e->src->index));
244 8542 : worklist.safe_push (e->src);
245 : }
246 372360 : for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
247 332466 : gsi_next (&bsi))
248 : {
249 335333 : gimple *stmt = gsi_stmt (bsi);
250 335333 : if (is_gimple_debug (stmt))
251 287584 : continue;
252 47749 : if (walk_stmt_load_store_addr_ops
253 47749 : (stmt, non_ssa_vars, test_nonssa_use, test_nonssa_use,
254 : test_nonssa_use))
255 : {
256 2867 : ok = false;
257 2867 : goto done;
258 : }
259 332552 : if (glabel *label_stmt = dyn_cast <glabel *> (stmt))
260 86 : if (test_nonssa_use (stmt, gimple_label_label (label_stmt),
261 : NULL_TREE, non_ssa_vars))
262 : {
263 0 : ok = false;
264 0 : goto done;
265 : }
266 : }
267 18095 : for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
268 1015 : gsi_next (&bsi))
269 : {
270 1015 : if (walk_stmt_load_store_addr_ops
271 1015 : (gsi_stmt (bsi), non_ssa_vars, test_nonssa_use, test_nonssa_use,
272 : test_nonssa_use))
273 : {
274 0 : ok = false;
275 0 : goto done;
276 : }
277 : }
278 52377 : FOR_EACH_EDGE (e, ei, bb->succs)
279 : {
280 35297 : if (e->dest != return_bb)
281 32773 : continue;
282 2524 : for (gphi_iterator bsi = gsi_start_phis (return_bb);
283 5713 : !gsi_end_p (bsi);
284 3189 : gsi_next (&bsi))
285 : {
286 3189 : gphi *stmt = bsi.phi ();
287 3189 : tree op = gimple_phi_arg_def (stmt, e->dest_idx);
288 :
289 6378 : if (virtual_operand_p (gimple_phi_result (stmt)))
290 2404 : continue;
291 785 : if (TREE_CODE (op) != SSA_NAME
292 785 : && test_nonssa_use (stmt, op, op, non_ssa_vars))
293 : {
294 0 : ok = false;
295 0 : goto done;
296 : }
297 : }
298 : }
299 : }
300 :
301 : /* Verify that the rest of function does not define any label
302 : used by the split part. */
303 103628 : FOR_EACH_BB_FN (bb, cfun)
304 94074 : if (!bitmap_bit_p (current->split_bbs, bb->index)
305 94074 : && !bitmap_bit_p (seen, bb->index))
306 : {
307 32907 : gimple_stmt_iterator bsi;
308 71432 : for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
309 99692 : if (glabel *label_stmt = dyn_cast <glabel *> (gsi_stmt (bsi)))
310 : {
311 5621 : if (test_nonssa_use (label_stmt,
312 : gimple_label_label (label_stmt),
313 : NULL_TREE, non_ssa_vars))
314 : {
315 3 : ok = false;
316 3 : goto done;
317 : }
318 : }
319 : else
320 : break;
321 : }
322 :
323 9554 : done:
324 12424 : BITMAP_FREE (seen);
325 12424 : worklist.release ();
326 12424 : return ok;
327 : }
328 :
329 : /* If STMT is a call, check the callee against a list of forbidden
330 : predicate functions. If a match is found, look for uses of the
331 : call result in condition statements that compare against zero.
332 : For each such use, find the block targeted by the condition
333 : statement for the nonzero result, and set the bit for this block
334 : in the forbidden dominators bitmap. The purpose of this is to avoid
335 : selecting a split point where we are likely to lose the chance
336 : to optimize away an unused function call. */
337 :
338 : static void
339 42234670 : check_forbidden_calls (gimple *stmt)
340 : {
341 42234670 : imm_use_iterator use_iter;
342 42234670 : use_operand_p use_p;
343 42234670 : tree lhs;
344 :
345 : /* At the moment, __builtin_constant_p is the only forbidden
346 : predicate function call (see PR49642). */
347 42234670 : if (!gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P))
348 42216442 : return;
349 :
350 18228 : lhs = gimple_call_lhs (stmt);
351 :
352 18228 : if (!lhs || TREE_CODE (lhs) != SSA_NAME)
353 : return;
354 :
355 39336 : FOR_EACH_IMM_USE_FAST (use_p, use_iter, lhs)
356 : {
357 21108 : tree op1;
358 21108 : basic_block use_bb, forbidden_bb;
359 21108 : enum tree_code code;
360 21108 : edge true_edge, false_edge;
361 21108 : gcond *use_stmt;
362 :
363 21108 : use_stmt = dyn_cast <gcond *> (USE_STMT (use_p));
364 21108 : if (!use_stmt)
365 29 : continue;
366 :
367 : /* Assuming canonical form for GIMPLE_COND here, with constant
368 : in second position. */
369 21079 : op1 = gimple_cond_rhs (use_stmt);
370 21079 : code = gimple_cond_code (use_stmt);
371 21079 : use_bb = gimple_bb (use_stmt);
372 :
373 21079 : extract_true_false_edges_from_block (use_bb, &true_edge, &false_edge);
374 :
375 : /* We're only interested in comparisons that distinguish
376 : unambiguously from zero. */
377 21079 : if (!integer_zerop (op1) || code == LE_EXPR || code == GE_EXPR)
378 0 : continue;
379 :
380 21079 : if (code == EQ_EXPR)
381 2959 : forbidden_bb = false_edge->dest;
382 : else
383 18120 : forbidden_bb = true_edge->dest;
384 :
385 21079 : bitmap_set_bit (forbidden_dominators, forbidden_bb->index);
386 18228 : }
387 : }
388 :
389 : /* If BB is dominated by any block in the forbidden dominators set,
390 : return TRUE; else FALSE. */
391 :
392 : static bool
393 1436 : dominated_by_forbidden (basic_block bb)
394 : {
395 1436 : unsigned dom_bb;
396 1436 : bitmap_iterator bi;
397 :
398 4312 : EXECUTE_IF_SET_IN_BITMAP (forbidden_dominators, 1, dom_bb, bi)
399 : {
400 2894 : if (dominated_by_p (CDI_DOMINATORS, bb,
401 2894 : BASIC_BLOCK_FOR_FN (cfun, dom_bb)))
402 : return true;
403 : }
404 :
405 : return false;
406 : }
407 :
408 : /* For give split point CURRENT and return block RETURN_BB return 1
409 : if ssa name VAL is set by split part and 0 otherwise. */
410 : static bool
411 19618 : split_part_set_ssa_name_p (tree val, class split_point *current,
412 : basic_block return_bb)
413 : {
414 19618 : if (TREE_CODE (val) != SSA_NAME)
415 : return false;
416 :
417 19618 : return (!SSA_NAME_IS_DEFAULT_DEF (val)
418 19618 : && (bitmap_bit_p (current->split_bbs,
419 16129 : gimple_bb (SSA_NAME_DEF_STMT (val))->index)
420 16125 : || gimple_bb (SSA_NAME_DEF_STMT (val)) == return_bb));
421 : }
422 :
423 : /* We found an split_point CURRENT. NON_SSA_VARS is bitmap of all non ssa
424 : variables used and RETURN_BB is return basic block.
425 : See if we can split function here. */
426 :
427 : static void
428 2646724 : consider_split (class split_point *current, bitmap non_ssa_vars,
429 : basic_block return_bb)
430 : {
431 2646724 : tree parm;
432 2646724 : unsigned int num_args = 0;
433 2646724 : unsigned int call_overhead;
434 2646724 : edge e;
435 2646724 : edge_iterator ei;
436 2646724 : gphi_iterator bsi;
437 2646724 : unsigned int i;
438 2646724 : tree retval;
439 2646724 : bool back_edge = false;
440 :
441 2646724 : if (dump_file && (dump_flags & TDF_DETAILS))
442 6 : dump_split_point (dump_file, current);
443 :
444 2646724 : current->count = profile_count::zero ();
445 5765057 : FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
446 : {
447 3118333 : if (e->flags & EDGE_DFS_BACK)
448 150327 : back_edge = true;
449 3118333 : if (!bitmap_bit_p (current->split_bbs, e->src->index))
450 2968005 : current->count += e->count ();
451 : }
452 :
453 : /* Do not split when we would end up calling function anyway.
454 : Compares are three state, use !(...<...) to also give up when outcome
455 : is unknown. */
456 5293448 : if (!(current->count
457 2646724 : < (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.apply_scale
458 5293448 : (param_partial_inlining_entry_probability, 100))))
459 : {
460 : /* When profile is guessed, we cannot expect it to give us
461 : realistic estimate on likeliness of function taking the
462 : complex path. As a special case, when tail of the function is
463 : a loop, enable splitting since inlining code skipping the loop
464 : is likely noticeable win. */
465 1509989 : if (back_edge
466 89941 : && profile_status_for_fn (cfun) != PROFILE_READ
467 1599890 : && current->count
468 89901 : < ENTRY_BLOCK_PTR_FOR_FN (cfun)->count)
469 : {
470 15248 : if (dump_file && (dump_flags & TDF_DETAILS))
471 : {
472 0 : fprintf (dump_file,
473 : " Split before loop, accepting despite low counts");
474 0 : current->count.dump (dump_file);
475 0 : fprintf (dump_file, " ");
476 0 : ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.dump (dump_file);
477 : }
478 : }
479 : else
480 : {
481 1494741 : if (dump_file && (dump_flags & TDF_DETAILS))
482 3 : fprintf (dump_file,
483 : " Refused: incoming frequency is too large.\n");
484 2577923 : return;
485 : }
486 : }
487 :
488 1151983 : if (!current->header_size)
489 : {
490 0 : if (dump_file && (dump_flags & TDF_DETAILS))
491 0 : fprintf (dump_file, " Refused: header empty\n");
492 : return;
493 : }
494 :
495 : /* Verify that PHI args on entry are either virtual or all their operands
496 : incoming from header are the same. */
497 1330089 : for (bsi = gsi_start_phis (current->entry_bb); !gsi_end_p (bsi); gsi_next (&bsi))
498 : {
499 191850 : gphi *stmt = bsi.phi ();
500 191850 : tree val = NULL;
501 :
502 383700 : if (virtual_operand_p (gimple_phi_result (stmt)))
503 48793 : continue;
504 415492 : for (i = 0; i < gimple_phi_num_args (stmt); i++)
505 : {
506 286179 : edge e = gimple_phi_arg_edge (stmt, i);
507 286179 : if (!bitmap_bit_p (current->split_bbs, e->src->index))
508 : {
509 157020 : tree edge_val = gimple_phi_arg_def (stmt, i);
510 157020 : if (val && edge_val != val)
511 : {
512 13744 : if (dump_file && (dump_flags & TDF_DETAILS))
513 0 : fprintf (dump_file,
514 : " Refused: entry BB has PHI with multiple variants\n");
515 : return;
516 : }
517 : val = edge_val;
518 : }
519 : }
520 : }
521 :
522 :
523 : /* See what argument we will pass to the split function and compute
524 : call overhead. */
525 1138239 : call_overhead = eni_size_weights.call_cost;
526 3357403 : for (parm = DECL_ARGUMENTS (current_function_decl); parm;
527 2219164 : parm = DECL_CHAIN (parm))
528 : {
529 2219164 : if (!is_gimple_reg (parm))
530 : {
531 175449 : if (bitmap_bit_p (non_ssa_vars, DECL_UID (parm)))
532 : {
533 0 : if (dump_file && (dump_flags & TDF_DETAILS))
534 0 : fprintf (dump_file,
535 : " Refused: need to pass non-ssa param values\n");
536 : return;
537 : }
538 : }
539 : else
540 : {
541 2043715 : tree ddef = ssa_default_def (cfun, parm);
542 2043715 : if (ddef
543 4030967 : && bitmap_bit_p (current->ssa_names_to_pass,
544 1987252 : SSA_NAME_VERSION (ddef)))
545 : {
546 657129 : if (!VOID_TYPE_P (TREE_TYPE (parm)))
547 657129 : call_overhead += estimate_move_cost (TREE_TYPE (parm), false);
548 657129 : num_args++;
549 : }
550 : }
551 : }
552 1138239 : if (!VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
553 739519 : call_overhead += estimate_move_cost (TREE_TYPE (TREE_TYPE
554 : (current_function_decl)),
555 : false);
556 :
557 1138239 : if (current->split_size <= call_overhead)
558 : {
559 548660 : if (dump_file && (dump_flags & TDF_DETAILS))
560 0 : fprintf (dump_file,
561 : " Refused: split size is smaller than call overhead\n");
562 : return;
563 : }
564 : /* FIXME: The logic here is not very precise, because inliner does use
565 : inline predicates to reduce function body size. We add 10 to anticipate
566 : that. Next stage1 we should try to be more meaningful here. */
567 589579 : if (current->header_size + call_overhead
568 589579 : >= (unsigned int)(DECL_DECLARED_INLINE_P (current_function_decl)
569 151955 : ? param_max_inline_insns_single
570 589579 : : param_max_inline_insns_auto) + 10)
571 : {
572 416308 : if (dump_file && (dump_flags & TDF_DETAILS))
573 0 : fprintf (dump_file,
574 : " Refused: header size is too large for inline candidate\n");
575 : return;
576 : }
577 :
578 : /* Splitting functions brings the target out of comdat group; this will
579 : lead to code duplication if the function is reused by other unit.
580 : Limit this duplication. This is consistent with limit in tree-sra.cc
581 : FIXME: with LTO we ought to be able to do better! */
582 301637 : if (DECL_ONE_ONLY (current_function_decl)
583 301630 : && current->split_size >= (unsigned int) param_max_inline_insns_auto + 10)
584 : {
585 12184 : if (dump_file && (dump_flags & TDF_DETAILS))
586 0 : fprintf (dump_file,
587 : " Refused: function is COMDAT and tail is too large\n");
588 : return;
589 : }
590 : /* For comdat functions also reject very small tails; those will likely get
591 : inlined back and we do not want to risk the duplication overhead.
592 : FIXME: with LTO we ought to be able to do better! */
593 277269 : if (DECL_ONE_ONLY (current_function_decl)
594 161087 : && current->split_size
595 116175 : <= (unsigned int) param_early_inlining_insns / 2)
596 : {
597 29125 : if (dump_file && (dump_flags & TDF_DETAILS))
598 0 : fprintf (dump_file,
599 : " Refused: function is COMDAT and tail is too small\n");
600 : return;
601 : }
602 :
603 : /* FIXME: we currently can pass only SSA function parameters to the split
604 : arguments. Once parm_adjustment infrastructure is supported by cloning,
605 : we can pass more than that. */
606 131962 : if (num_args != bitmap_count_bits (current->ssa_names_to_pass))
607 : {
608 :
609 60273 : if (dump_file && (dump_flags & TDF_DETAILS))
610 0 : fprintf (dump_file,
611 : " Refused: need to pass non-param values\n");
612 : return;
613 : }
614 :
615 : /* When there are non-ssa vars used in the split region, see if they
616 : are used in the header region. If so, reject the split.
617 : FIXME: we can use nested function support to access both. */
618 71689 : if (!bitmap_empty_p (non_ssa_vars)
619 71689 : && !verify_non_ssa_vars (current, non_ssa_vars, return_bb))
620 : {
621 2870 : if (dump_file && (dump_flags & TDF_DETAILS))
622 0 : fprintf (dump_file,
623 : " Refused: split part has non-ssa uses\n");
624 : return;
625 : }
626 :
627 : /* If the split point is dominated by a forbidden block, reject
628 : the split. */
629 68819 : if (!bitmap_empty_p (forbidden_dominators)
630 68819 : && dominated_by_forbidden (current->entry_bb))
631 : {
632 18 : if (dump_file && (dump_flags & TDF_DETAILS))
633 0 : fprintf (dump_file,
634 : " Refused: split point dominated by forbidden block\n");
635 : return;
636 : }
637 :
638 : /* See if retval used by return bb is computed by header or split part.
639 : When it is computed by split part, we need to produce return statement
640 : in the split part and add code to header to pass it around.
641 :
642 : This is bit tricky to test:
643 : 1) When there is no return_bb or no return value, we always pass
644 : value around.
645 : 2) Invariants are always computed by caller.
646 : 3) For SSA we need to look if defining statement is in header or split part
647 : 4) For non-SSA we need to look where the var is computed. */
648 68801 : retval = find_retval (return_bb);
649 68801 : if (!retval)
650 : {
651 : /* If there is a return_bb with no return value in function returning
652 : value by reference, also make the split part return void, otherwise
653 : we expansion would try to create a non-POD temporary, which is
654 : invalid. */
655 43424 : if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
656 33119 : && DECL_RESULT (current_function_decl)
657 76543 : && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
658 0 : current->split_part_set_retval = false;
659 : else
660 43424 : current->split_part_set_retval = true;
661 : }
662 25377 : else if (is_gimple_min_invariant (retval))
663 2281 : current->split_part_set_retval = false;
664 : /* Special case is value returned by reference we record as if it was non-ssa
665 : set to result_decl. */
666 23096 : else if (TREE_CODE (retval) == SSA_NAME
667 19625 : && SSA_NAME_VAR (retval)
668 5565 : && TREE_CODE (SSA_NAME_VAR (retval)) == RESULT_DECL
669 23103 : && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
670 7 : current->split_part_set_retval
671 7 : = bitmap_bit_p (non_ssa_vars, DECL_UID (SSA_NAME_VAR (retval)));
672 23089 : else if (TREE_CODE (retval) == SSA_NAME)
673 19618 : current->split_part_set_retval
674 19618 : = split_part_set_ssa_name_p (retval, current, return_bb);
675 3471 : else if (TREE_CODE (retval) == PARM_DECL)
676 0 : current->split_part_set_retval = false;
677 3471 : else if (VAR_P (retval)
678 505 : || TREE_CODE (retval) == RESULT_DECL)
679 3471 : current->split_part_set_retval
680 3471 : = bitmap_bit_p (non_ssa_vars, DECL_UID (retval));
681 : else
682 0 : current->split_part_set_retval = true;
683 :
684 : /* split_function fixes up at most one PHI non-virtual PHI node in return_bb,
685 : for the return value. If there are other PHIs, give up. */
686 68801 : if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
687 : {
688 58496 : gphi_iterator psi;
689 :
690 119518 : for (psi = gsi_start_phis (return_bb); !gsi_end_p (psi); gsi_next (&psi))
691 61022 : if (!virtual_operand_p (gimple_phi_result (psi.phi ()))
692 61022 : && !(retval
693 14082 : && current->split_part_set_retval
694 14082 : && TREE_CODE (retval) == SSA_NAME
695 14082 : && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
696 14082 : && SSA_NAME_DEF_STMT (retval) == psi.phi ()))
697 : {
698 0 : if (dump_file && (dump_flags & TDF_DETAILS))
699 0 : fprintf (dump_file,
700 : " Refused: return bb has extra PHIs\n");
701 0 : return;
702 : }
703 : }
704 :
705 68801 : if (dump_file && (dump_flags & TDF_DETAILS))
706 3 : fprintf (dump_file, " Accepted!\n");
707 :
708 : /* At the moment chose split point with lowest count and that leaves
709 : out smallest size of header.
710 : In future we might re-consider this heuristics. */
711 68801 : if (!best_split_point.split_bbs
712 16490 : || best_split_point.count
713 16490 : > current->count
714 142572 : || (best_split_point.count == current->count
715 4970 : && best_split_point.split_size < current->split_size))
716 :
717 : {
718 56012 : if (dump_file && (dump_flags & TDF_DETAILS))
719 2 : fprintf (dump_file, " New best split point!\n");
720 56012 : if (best_split_point.ssa_names_to_pass)
721 : {
722 3701 : BITMAP_FREE (best_split_point.ssa_names_to_pass);
723 3701 : BITMAP_FREE (best_split_point.split_bbs);
724 : }
725 56012 : best_split_point = *current;
726 56012 : best_split_point.ssa_names_to_pass = BITMAP_ALLOC (NULL);
727 56012 : bitmap_copy (best_split_point.ssa_names_to_pass,
728 56012 : current->ssa_names_to_pass);
729 56012 : best_split_point.split_bbs = BITMAP_ALLOC (NULL);
730 56012 : bitmap_copy (best_split_point.split_bbs, current->split_bbs);
731 : }
732 : }
733 :
734 : /* Return basic block containing RETURN statement. We allow basic blocks
735 : of the form:
736 : <retval> = tmp_var;
737 : return <retval>
738 : but return_bb cannot be more complex than this (except for
739 : -fsanitize=thread we allow TSAN_FUNC_EXIT () internal call in there).
740 : If nothing is found, return the exit block.
741 :
742 : When there are multiple RETURN statement, chose one with return value,
743 : since that one is more likely shared by multiple code paths.
744 :
745 : Return BB is special, because for function splitting it is the only
746 : basic block that is duplicated in between header and split part of the
747 : function.
748 :
749 : TODO: We might support multiple return blocks. */
750 :
751 : static basic_block
752 1312843 : find_return_bb (void)
753 : {
754 1312843 : edge e;
755 1312843 : basic_block return_bb = EXIT_BLOCK_PTR_FOR_FN (cfun);
756 1312843 : gimple_stmt_iterator bsi;
757 1312843 : bool found_return = false;
758 1312843 : tree retval = NULL_TREE;
759 :
760 1312843 : if (!single_pred_p (EXIT_BLOCK_PTR_FOR_FN (cfun)))
761 : return return_bb;
762 :
763 1312621 : e = single_pred_edge (EXIT_BLOCK_PTR_FOR_FN (cfun));
764 6787936 : for (bsi = gsi_last_bb (e->src); !gsi_end_p (bsi); gsi_prev (&bsi))
765 : {
766 4657789 : gimple *stmt = gsi_stmt (bsi);
767 4657789 : gassign *assign;
768 4657789 : if (gimple_code (stmt) == GIMPLE_LABEL
769 4646768 : || is_gimple_debug (stmt)
770 7172570 : || gimple_clobber_p (stmt))
771 : ;
772 2228312 : else if ((assign = dyn_cast<gassign *> (stmt))
773 634094 : && found_return
774 634094 : && gimple_assign_single_p (assign)
775 533117 : && (auto_var_in_fn_p (gimple_assign_rhs1 (assign),
776 : current_function_decl)
777 514471 : || is_gimple_min_invariant (gimple_assign_rhs1 (assign)))
778 111738 : && retval == gimple_assign_lhs (assign))
779 : ;
780 2216607 : else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
781 : {
782 1312621 : found_return = true;
783 1312621 : retval = gimple_return_retval (return_stmt);
784 : }
785 : /* For -fsanitize=thread, allow also TSAN_FUNC_EXIT () in the return
786 : bb. */
787 903986 : else if ((flag_sanitize & SANITIZE_THREAD)
788 903986 : && gimple_call_internal_p (stmt, IFN_TSAN_FUNC_EXIT))
789 : ;
790 : else
791 : break;
792 : }
793 1312621 : if (gsi_end_p (bsi) && found_return)
794 408763 : return_bb = e->src;
795 :
796 : return return_bb;
797 : }
798 :
799 : /* Given return basic block RETURN_BB, see where return value is really
800 : stored. */
801 : static tree
802 106565 : find_retval (basic_block return_bb)
803 : {
804 106565 : gimple_stmt_iterator bsi;
805 106565 : gassign *assign;
806 307276 : for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
807 190406 : if (greturn *return_stmt = dyn_cast <greturn *> (gsi_stmt (bsi)))
808 96192 : return gimple_return_retval (return_stmt);
809 99436 : else if ((assign = dyn_cast<gassign *> (gsi_stmt (bsi)))
810 5290 : && !gimple_clobber_p (assign))
811 68 : return gimple_assign_rhs1 (assign);
812 : return NULL;
813 : }
814 :
815 : /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic
816 : variable, mark it as used in bitmap passed via DATA.
817 : Return true when access to T prevents splitting the function. */
818 :
819 : static bool
820 8218547 : mark_nonssa_use (gimple *, tree t, tree, void *data)
821 : {
822 8218547 : t = get_base_address (t);
823 :
824 8218547 : if (!t || is_gimple_reg (t))
825 : return false;
826 :
827 : /* At present we can't pass non-SSA arguments to split function.
828 : FIXME: this can be relaxed by passing references to arguments. */
829 8218547 : if (TREE_CODE (t) == PARM_DECL)
830 : {
831 252856 : if (dump_file && (dump_flags & TDF_DETAILS))
832 0 : fprintf (dump_file,
833 : "Cannot split: use of non-ssa function parameter.\n");
834 : return true;
835 : }
836 :
837 3407536 : if ((VAR_P (t) && auto_var_in_fn_p (t, current_function_decl))
838 5305678 : || TREE_CODE (t) == RESULT_DECL
839 13220584 : || (TREE_CODE (t) == LABEL_DECL && FORCED_LABEL (t)))
840 2710834 : bitmap_set_bit ((bitmap)data, DECL_UID (t));
841 :
842 : /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want
843 : to pretend that the value pointed to is actual result decl. */
844 4436439 : if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
845 3529252 : && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
846 3528671 : && SSA_NAME_VAR (TREE_OPERAND (t, 0))
847 2831674 : && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
848 8037976 : && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
849 72285 : return
850 72285 : bitmap_bit_p ((bitmap)data,
851 72285 : DECL_UID (DECL_RESULT (current_function_decl)));
852 :
853 : return false;
854 : }
855 :
856 : /* Compute local properties of basic block BB we collect when looking for
857 : split points. We look for ssa defs and store them in SET_SSA_NAMES,
858 : for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic
859 : vars stored in NON_SSA_VARS.
860 :
861 : When BB has edge to RETURN_BB, collect uses in RETURN_BB too.
862 :
863 : Return false when BB contains something that prevents it from being put into
864 : split function. */
865 :
866 : static bool
867 4462181 : visit_bb (basic_block bb, basic_block return_bb,
868 : bitmap set_ssa_names, bitmap used_ssa_names,
869 : bitmap non_ssa_vars)
870 : {
871 4462181 : edge e;
872 4462181 : edge_iterator ei;
873 4462181 : bool can_split = true;
874 :
875 49840237 : for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
876 40915875 : gsi_next (&bsi))
877 : {
878 40915875 : gimple *stmt = gsi_stmt (bsi);
879 40915875 : tree op;
880 40915875 : ssa_op_iter iter;
881 :
882 40915875 : if (is_gimple_debug (stmt))
883 27296622 : continue;
884 :
885 14518875 : if (gimple_clobber_p (stmt))
886 899622 : continue;
887 :
888 : /* FIXME: We can split regions containing EH. We cannot however
889 : split RESX, EH_DISPATCH and EH_POINTER referring to same region
890 : into different partitions. This would require tracking of
891 : EH regions and checking in consider_split_point if they
892 : are not used elsewhere. */
893 13619253 : if (gimple_code (stmt) == GIMPLE_RESX)
894 : {
895 114158 : if (dump_file && (dump_flags & TDF_DETAILS))
896 0 : fprintf (dump_file, "Cannot split: resx.\n");
897 : can_split = false;
898 : }
899 13619253 : if (gimple_code (stmt) == GIMPLE_EH_DISPATCH)
900 : {
901 8950 : if (dump_file && (dump_flags & TDF_DETAILS))
902 0 : fprintf (dump_file, "Cannot split: eh dispatch.\n");
903 : can_split = false;
904 : }
905 :
906 : /* Check calls that would prevent splitting. */
907 13619253 : if (gimple_code (stmt) == GIMPLE_CALL)
908 : {
909 2446518 : if (tree decl = gimple_call_fndecl (stmt))
910 : {
911 : /* Check builtins that would prevent splitting. */
912 2357580 : if (fndecl_built_in_p (decl, BUILT_IN_NORMAL))
913 707358 : switch (DECL_FUNCTION_CODE (decl))
914 : {
915 : /* FIXME: once we will allow passing non-parm values to
916 : split part, we need to be sure to handle correct
917 : builtin_stack_save and builtin_stack_restore. At the
918 : moment we are safe; there is no way to store
919 : builtin_stack_save result in non-SSA variable since all
920 : calls to those are compiler generated. */
921 10 : case BUILT_IN_APPLY:
922 10 : case BUILT_IN_APPLY_ARGS:
923 10 : case BUILT_IN_VA_START:
924 10 : if (dump_file && (dump_flags & TDF_DETAILS))
925 0 : fprintf (dump_file,
926 : "Cannot split: builtin_apply and va_start.\n");
927 : can_split = false;
928 : break;
929 9993 : case BUILT_IN_EH_POINTER:
930 9993 : if (dump_file && (dump_flags & TDF_DETAILS))
931 0 : fprintf (dump_file,
932 : "Cannot split: builtin_eh_pointer.\n");
933 : can_split = false;
934 : break;
935 : default:
936 : break;
937 : }
938 :
939 : /* Calls to functions (which have the warning or error
940 : attribute on them) should not be split off into another
941 : function. */
942 2357580 : if (lookup_attribute ("warning", DECL_ATTRIBUTES (decl))
943 2357580 : || lookup_attribute ("error", DECL_ATTRIBUTES (decl)))
944 : {
945 20 : if (dump_file && (dump_flags & TDF_DETAILS))
946 0 : fprintf (dump_file,
947 : "Cannot split: warning or error attribute.\n");
948 : can_split = false;
949 : }
950 : }
951 : }
952 :
953 20068849 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
954 6449596 : bitmap_set_bit (set_ssa_names, SSA_NAME_VERSION (op));
955 26710656 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
956 13091403 : bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
957 13619253 : can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
958 : mark_nonssa_use,
959 : mark_nonssa_use,
960 : mark_nonssa_use);
961 : }
962 5390077 : for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
963 927896 : gsi_next (&bsi))
964 : {
965 927896 : gphi *stmt = bsi.phi ();
966 927896 : unsigned int i;
967 :
968 1855792 : if (virtual_operand_p (gimple_phi_result (stmt)))
969 388716 : continue;
970 1078360 : bitmap_set_bit (set_ssa_names,
971 539180 : SSA_NAME_VERSION (gimple_phi_result (stmt)));
972 2318534 : for (i = 0; i < gimple_phi_num_args (stmt); i++)
973 : {
974 1240174 : tree op = gimple_phi_arg_def (stmt, i);
975 1240174 : if (TREE_CODE (op) == SSA_NAME)
976 837404 : bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
977 : }
978 539180 : can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
979 : mark_nonssa_use,
980 : mark_nonssa_use,
981 : mark_nonssa_use);
982 : }
983 : /* Record also uses coming from PHI operand in return BB. */
984 10393279 : FOR_EACH_EDGE (e, ei, bb->succs)
985 5931098 : if (e->dest == return_bb)
986 : {
987 1632704 : for (gphi_iterator bsi = gsi_start_phis (return_bb);
988 2397797 : !gsi_end_p (bsi);
989 765093 : gsi_next (&bsi))
990 : {
991 765093 : gphi *stmt = bsi.phi ();
992 765093 : tree op = gimple_phi_arg_def (stmt, e->dest_idx);
993 :
994 1530186 : if (virtual_operand_p (gimple_phi_result (stmt)))
995 450574 : continue;
996 314519 : if (TREE_CODE (op) == SSA_NAME)
997 142476 : bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
998 : else
999 172043 : can_split &= !mark_nonssa_use (stmt, op, op, non_ssa_vars);
1000 : }
1001 : }
1002 4462181 : return can_split;
1003 : }
1004 :
1005 : /* Stack entry for recursive DFS walk in find_split_point. */
1006 :
1007 5775008 : class stack_entry
1008 : {
1009 : public:
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 : sreal overall_time;
1031 : int overall_size;
1032 :
1033 : /* When false we cannot split on this BB. */
1034 : bool can_split;
1035 : };
1036 :
1037 :
1038 : /* Find all articulations and call consider_split on them.
1039 : OVERALL_TIME and OVERALL_SIZE is time and size of the function.
1040 :
1041 : We perform basic algorithm for finding an articulation in a graph
1042 : created from CFG by considering it to be an unoriented graph.
1043 :
1044 : The articulation is discovered via DFS walk. We collect earliest
1045 : basic block on stack that is reachable via backward edge. Articulation
1046 : is any basic block such that there is no backward edge bypassing it.
1047 : To reduce stack usage we maintain heap allocated stack in STACK vector.
1048 : AUX pointer of BB is set to index it appears in the stack or -1 once
1049 : it is visited and popped off the stack.
1050 :
1051 : The algorithm finds articulation after visiting the whole component
1052 : reachable by it. This makes it convenient to collect information about
1053 : the component used by consider_split. */
1054 :
1055 : static void
1056 1312827 : find_split_points (basic_block return_bb, sreal overall_time, int overall_size)
1057 : {
1058 1312827 : stack_entry first;
1059 1312827 : vec<stack_entry> stack = vNULL;
1060 1312827 : basic_block bb;
1061 1312827 : class split_point current;
1062 :
1063 1312827 : current.header_time = overall_time;
1064 1312827 : current.header_size = overall_size;
1065 1312827 : current.split_time = 0;
1066 1312827 : current.split_size = 0;
1067 1312827 : current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
1068 :
1069 1312827 : first.bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1070 1312827 : first.edge_num = 0;
1071 1312827 : first.overall_time = 0;
1072 1312827 : first.overall_size = 0;
1073 1312827 : first.earliest = INT_MAX;
1074 1312827 : first.set_ssa_names = 0;
1075 1312827 : first.used_ssa_names = 0;
1076 1312827 : first.non_ssa_vars = 0;
1077 1312827 : first.bbs_visited = 0;
1078 1312827 : first.can_split = false;
1079 1312827 : stack.safe_push (first);
1080 1312827 : ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = (void *)(intptr_t)-1;
1081 :
1082 19807598 : while (!stack.is_empty ())
1083 : {
1084 18494771 : stack_entry *entry = &stack.last ();
1085 :
1086 : /* We are walking an acyclic graph, so edge_num counts
1087 : succ and pred edges together. However when considering
1088 : articulation, we want to have processed everything reachable
1089 : from articulation but nothing that reaches into it. */
1090 18494771 : if (entry->edge_num == EDGE_COUNT (entry->bb->succs)
1091 18494771 : && entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1092 : {
1093 4462181 : int pos = stack.length ();
1094 4462181 : entry->can_split &= visit_bb (entry->bb, return_bb,
1095 : entry->set_ssa_names,
1096 : entry->used_ssa_names,
1097 : entry->non_ssa_vars);
1098 2958826 : if (pos <= entry->earliest && !entry->can_split
1099 4774283 : && dump_file && (dump_flags & TDF_DETAILS))
1100 0 : fprintf (dump_file,
1101 : "found articulation at bb %i but cannot split\n",
1102 0 : entry->bb->index);
1103 4462181 : if (pos <= entry->earliest && entry->can_split)
1104 : {
1105 2646724 : if (dump_file && (dump_flags & TDF_DETAILS))
1106 6 : fprintf (dump_file, "found articulation at bb %i\n",
1107 6 : entry->bb->index);
1108 2646724 : current.entry_bb = entry->bb;
1109 2646724 : current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
1110 2646724 : bitmap_and_compl (current.ssa_names_to_pass,
1111 2646724 : entry->used_ssa_names, entry->set_ssa_names);
1112 2646724 : current.header_time = overall_time - entry->overall_time;
1113 2646724 : current.header_size = overall_size - entry->overall_size;
1114 2646724 : current.split_time = entry->overall_time;
1115 2646724 : current.split_size = entry->overall_size;
1116 2646724 : current.split_bbs = entry->bbs_visited;
1117 2646724 : consider_split (¤t, entry->non_ssa_vars, return_bb);
1118 2646724 : BITMAP_FREE (current.ssa_names_to_pass);
1119 : }
1120 : }
1121 : /* Do actual DFS walk. */
1122 36989542 : if (entry->edge_num
1123 18494771 : < (EDGE_COUNT (entry->bb->succs)
1124 34363888 : + EDGE_COUNT (entry->bb->preds)))
1125 : {
1126 12719763 : edge e;
1127 12719763 : basic_block dest;
1128 12719763 : if (entry->edge_num < EDGE_COUNT (entry->bb->succs))
1129 : {
1130 7243925 : e = EDGE_SUCC (entry->bb, entry->edge_num);
1131 7243925 : dest = e->dest;
1132 : }
1133 : else
1134 : {
1135 5475838 : e = EDGE_PRED (entry->bb, entry->edge_num
1136 : - EDGE_COUNT (entry->bb->succs));
1137 5475838 : dest = e->src;
1138 : }
1139 :
1140 12719763 : entry->edge_num++;
1141 :
1142 : /* New BB to visit, push it to the stack. */
1143 12719763 : if (dest != return_bb && dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
1144 10951676 : && !dest->aux)
1145 : {
1146 4462181 : stack_entry new_entry;
1147 :
1148 4462181 : new_entry.bb = dest;
1149 4462181 : new_entry.edge_num = 0;
1150 4462181 : new_entry.overall_time
1151 4462181 : = bb_info_vec[dest->index].time;
1152 4462181 : new_entry.overall_size
1153 4462181 : = bb_info_vec[dest->index].size;
1154 4462181 : new_entry.earliest = INT_MAX;
1155 4462181 : new_entry.set_ssa_names = BITMAP_ALLOC (NULL);
1156 4462181 : new_entry.used_ssa_names = BITMAP_ALLOC (NULL);
1157 4462181 : new_entry.bbs_visited = BITMAP_ALLOC (NULL);
1158 4462181 : new_entry.non_ssa_vars = BITMAP_ALLOC (NULL);
1159 4462181 : new_entry.can_split = true;
1160 4462181 : bitmap_set_bit (new_entry.bbs_visited, dest->index);
1161 4462181 : stack.safe_push (new_entry);
1162 4462181 : dest->aux = (void *)(intptr_t)stack.length ();
1163 4462181 : }
1164 : /* Back edge found, record the earliest point. */
1165 8257582 : else if ((intptr_t)dest->aux > 0
1166 4298422 : && (intptr_t)dest->aux < entry->earliest)
1167 2796854 : entry->earliest = (intptr_t)dest->aux;
1168 : }
1169 : /* We are done with examining the edges. Pop off the value from stack
1170 : and merge stuff we accumulate during the walk. */
1171 5775008 : else if (entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1172 : {
1173 4462181 : stack_entry *prev = &stack[stack.length () - 2];
1174 :
1175 4462181 : entry->bb->aux = (void *)(intptr_t)-1;
1176 4462181 : prev->can_split &= entry->can_split;
1177 4462181 : if (prev->set_ssa_names)
1178 : {
1179 3284737 : bitmap_ior_into (prev->set_ssa_names, entry->set_ssa_names);
1180 3284737 : bitmap_ior_into (prev->used_ssa_names, entry->used_ssa_names);
1181 3284737 : bitmap_ior_into (prev->bbs_visited, entry->bbs_visited);
1182 3284737 : bitmap_ior_into (prev->non_ssa_vars, entry->non_ssa_vars);
1183 : }
1184 4462181 : if (prev->earliest > entry->earliest)
1185 2767357 : prev->earliest = entry->earliest;
1186 4462181 : prev->overall_time += entry->overall_time;
1187 4462181 : prev->overall_size += entry->overall_size;
1188 4462181 : BITMAP_FREE (entry->set_ssa_names);
1189 4462181 : BITMAP_FREE (entry->used_ssa_names);
1190 4462181 : BITMAP_FREE (entry->bbs_visited);
1191 4462181 : BITMAP_FREE (entry->non_ssa_vars);
1192 4462181 : stack.pop ();
1193 : }
1194 : else
1195 1312827 : stack.pop ();
1196 : }
1197 1312827 : ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = NULL;
1198 6183760 : FOR_EACH_BB_FN (bb, cfun)
1199 4870933 : bb->aux = NULL;
1200 1312827 : stack.release ();
1201 1312827 : BITMAP_FREE (current.ssa_names_to_pass);
1202 1312827 : }
1203 :
1204 : /* Split function at SPLIT_POINT. */
1205 :
1206 : static void
1207 52311 : split_function (basic_block return_bb, class split_point *split_point,
1208 : bool add_tsan_func_exit)
1209 : {
1210 52311 : vec<tree> args_to_pass = vNULL;
1211 52311 : bitmap args_to_skip;
1212 52311 : tree parm;
1213 52311 : int num = 0;
1214 52311 : cgraph_node *node, *cur_node = cgraph_node::get (current_function_decl);
1215 52311 : basic_block call_bb;
1216 52311 : gcall *call, *tsan_func_exit_call = NULL;
1217 52311 : edge e;
1218 52311 : edge_iterator ei;
1219 52311 : tree retval = NULL, real_retval = NULL;
1220 52311 : gimple *last_stmt = NULL;
1221 52311 : unsigned int i;
1222 52311 : tree arg, ddef;
1223 :
1224 52311 : if (dump_file)
1225 : {
1226 7 : fprintf (dump_file, "\n\nSplitting function at:\n");
1227 7 : dump_split_point (dump_file, split_point);
1228 : }
1229 :
1230 52311 : if (cur_node->can_change_signature)
1231 51555 : 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 52311 : for (parm = DECL_ARGUMENTS (current_function_decl);
1237 166293 : parm; parm = DECL_CHAIN (parm), num++)
1238 113982 : if (args_to_skip
1239 113982 : && (!is_gimple_reg (parm)
1240 108053 : || (ddef = ssa_default_def (cfun, parm)) == NULL_TREE
1241 105001 : || !bitmap_bit_p (split_point->ssa_names_to_pass,
1242 105001 : SSA_NAME_VERSION (ddef))))
1243 40816 : 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 73166 : if (is_gimple_reg (parm))
1249 73151 : arg = get_or_create_ssa_default_def (cfun, parm);
1250 : else
1251 : arg = parm;
1252 :
1253 73166 : if (!useless_type_conversion_p (DECL_ARG_TYPE (parm), TREE_TYPE (arg)))
1254 0 : arg = fold_convert (DECL_ARG_TYPE (parm), arg);
1255 73166 : args_to_pass.safe_push (arg);
1256 : }
1257 :
1258 : /* See if the split function will return. */
1259 52311 : bool split_part_return_p = false;
1260 260895 : FOR_EACH_EDGE (e, ei, return_bb->preds)
1261 : {
1262 208584 : if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1263 138599 : 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 52311 : if (!split_part_return_p)
1269 : ;
1270 : /* We have no return block, so nothing is needed. */
1271 37889 : 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 37764 : else if ((retval = find_retval (return_bb))
1279 37764 : && !split_point->split_part_set_retval)
1280 : {
1281 3782 : bool redirected = true;
1282 3782 : basic_block new_return_bb = create_basic_block (NULL, 0, return_bb);
1283 3782 : gimple_stmt_iterator gsi = gsi_start_bb (new_return_bb);
1284 3782 : gsi_insert_after (&gsi, gimple_build_return (NULL), GSI_NEW_STMT);
1285 3782 : new_return_bb->count = profile_count::zero ();
1286 16008 : while (redirected)
1287 : {
1288 8444 : redirected = false;
1289 21632 : FOR_EACH_EDGE (e, ei, return_bb->preds)
1290 17850 : if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1291 : {
1292 4662 : new_return_bb->count += e->count ();
1293 4662 : redirect_edge_and_branch (e, new_return_bb);
1294 4662 : redirected = true;
1295 4662 : break;
1296 : }
1297 : }
1298 3782 : e = make_single_succ_edge (new_return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1299 3782 : add_bb_to_loop (new_return_bb, current_loops->tree_root);
1300 3782 : bitmap_set_bit (split_point->split_bbs, new_return_bb->index);
1301 : }
1302 : /* When we pass around the value, use existing return block. */
1303 : else
1304 33982 : bitmap_set_bit (split_point->split_bbs, return_bb->index);
1305 :
1306 : /* If RETURN_BB has virtual operand PHIs, they must be removed and the
1307 : virtual operand marked for renaming as we change the CFG in a way that
1308 : tree-inline is not able to compensate for.
1309 :
1310 : Note this can happen whether or not we have a return value. If we have
1311 : a return value, then RETURN_BB may have PHIs for real operands too. */
1312 52311 : if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1313 : {
1314 44963 : bool phi_p = false;
1315 44963 : for (gphi_iterator gsi = gsi_start_phis (return_bb);
1316 91777 : !gsi_end_p (gsi);)
1317 : {
1318 46814 : gphi *stmt = gsi.phi ();
1319 93628 : if (!virtual_operand_p (gimple_phi_result (stmt)))
1320 : {
1321 10817 : gsi_next (&gsi);
1322 10817 : continue;
1323 : }
1324 35997 : mark_virtual_phi_result_for_renaming (stmt);
1325 35997 : remove_phi_node (&gsi, true);
1326 35997 : phi_p = true;
1327 : }
1328 : /* In reality we have to rename the reaching definition of the
1329 : virtual operand at return_bb as we will eventually release it
1330 : when we remove the code region we outlined.
1331 : So we have to rename all immediate virtual uses of that region
1332 : if we didn't see a PHI definition yet. */
1333 : /* ??? In real reality we want to set the reaching vdef of the
1334 : entry of the SESE region as the vuse of the call and the reaching
1335 : vdef of the exit of the SESE region as the vdef of the call. */
1336 44963 : if (!phi_p)
1337 17932 : for (gimple_stmt_iterator gsi = gsi_start_bb (return_bb);
1338 30740 : !gsi_end_p (gsi);
1339 21774 : gsi_next (&gsi))
1340 : {
1341 22525 : gimple *stmt = gsi_stmt (gsi);
1342 31491 : if (gimple_vuse (stmt))
1343 : {
1344 8966 : gimple_set_vuse (stmt, NULL_TREE);
1345 8966 : update_stmt (stmt);
1346 : }
1347 31491 : if (gimple_vdef (stmt))
1348 : break;
1349 : }
1350 : }
1351 :
1352 52311 : ipa_param_adjustments *adjustments;
1353 104622 : bool skip_return = (!split_part_return_p
1354 52311 : || !split_point->split_part_set_retval);
1355 : /* TODO: Perhaps get rid of args_to_skip entirely, after we make sure the
1356 : debug info generation and discrepancy avoiding works well too. */
1357 51555 : if ((args_to_skip && !bitmap_empty_p (args_to_skip))
1358 78999 : || skip_return)
1359 : {
1360 29012 : vec<ipa_adjusted_param, va_gc> *new_params = NULL;
1361 29012 : unsigned j;
1362 29012 : for (parm = DECL_ARGUMENTS (current_function_decl), j = 0;
1363 97246 : parm; parm = DECL_CHAIN (parm), j++)
1364 68234 : if (!args_to_skip || !bitmap_bit_p (args_to_skip, j))
1365 : {
1366 27418 : ipa_adjusted_param adj;
1367 27418 : memset (&adj, 0, sizeof (adj));
1368 27418 : adj.op = IPA_PARAM_OP_COPY;
1369 27418 : adj.base_index = j;
1370 27418 : adj.prev_clone_index = j;
1371 27418 : vec_safe_push (new_params, adj);
1372 : }
1373 29012 : adjustments = new ipa_param_adjustments (new_params, j, skip_return);
1374 : }
1375 : else
1376 : adjustments = NULL;
1377 :
1378 : /* Now create the actual clone. */
1379 52311 : cgraph_edge::rebuild_edges ();
1380 52311 : node = cur_node->create_version_clone_with_body
1381 52311 : (vNULL, NULL, adjustments,
1382 : split_point->split_bbs, split_point->entry_bb, "part");
1383 52311 : delete adjustments;
1384 52311 : node->split_part = true;
1385 :
1386 52311 : if (cur_node->same_comdat_group)
1387 : {
1388 : /* TODO: call is versionable if we make sure that all
1389 : callers are inside of a comdat group. */
1390 2282 : cur_node->calls_comdat_local = true;
1391 2282 : node->add_to_same_comdat_group (cur_node);
1392 : }
1393 :
1394 :
1395 : /* Let's take a time profile for split function. */
1396 52311 : if (cur_node->tp_first_run)
1397 2 : node->tp_first_run = cur_node->tp_first_run + 1;
1398 :
1399 : /* For usual cloning it is enough to clear builtin only when signature
1400 : changes. For partial inlining we however cannot expect the part
1401 : of builtin implementation to have same semantic as the whole. */
1402 52311 : if (fndecl_built_in_p (node->decl))
1403 32 : set_decl_built_in_function (node->decl, NOT_BUILT_IN, 0);
1404 :
1405 : /* Drop "clobber *this" attribute from first argument of the split
1406 : function if any. Code before that might be initializing the
1407 : members. */
1408 52311 : if (tree arg = DECL_ARGUMENTS (node->decl))
1409 37789 : if (lookup_attribute ("clobber *this", DECL_ATTRIBUTES (arg)))
1410 984 : DECL_ATTRIBUTES (arg)
1411 1968 : = remove_attribute ("clobber *this",
1412 984 : copy_list (DECL_ATTRIBUTES (arg)));
1413 :
1414 : /* If return_bb contains any clobbers that refer to SSA_NAMEs
1415 : set in the split part, remove them. Also reset debug stmts that
1416 : refer to SSA_NAMEs set in the split part. */
1417 52311 : if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1418 : {
1419 44963 : gimple_stmt_iterator gsi = gsi_start_bb (return_bb);
1420 139177 : while (!gsi_end_p (gsi))
1421 : {
1422 94214 : tree op;
1423 94214 : ssa_op_iter iter;
1424 94214 : gimple *stmt = gsi_stmt (gsi);
1425 94214 : bool remove = false;
1426 94214 : if (gimple_clobber_p (stmt) || is_gimple_debug (stmt))
1427 49088 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
1428 : {
1429 2578 : basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (op));
1430 2578 : if (op != retval
1431 2578 : && bb
1432 272 : && bb != return_bb
1433 2841 : && bitmap_bit_p (split_point->split_bbs, bb->index))
1434 : {
1435 0 : if (is_gimple_debug (stmt))
1436 : {
1437 0 : gimple_debug_bind_reset_value (stmt);
1438 0 : update_stmt (stmt);
1439 : }
1440 : else
1441 : remove = true;
1442 : break;
1443 : }
1444 : }
1445 0 : if (remove)
1446 0 : gsi_remove (&gsi, true);
1447 : else
1448 94214 : gsi_next (&gsi);
1449 : }
1450 : }
1451 :
1452 : /* If the original function is declared inline, there is no point in issuing
1453 : a warning for the non-inlinable part. */
1454 52311 : DECL_NO_INLINE_WARNING_P (node->decl) = 1;
1455 52311 : cur_node->remove_callees ();
1456 52311 : cur_node->remove_all_references ();
1457 52311 : if (!split_part_return_p)
1458 14422 : TREE_THIS_VOLATILE (node->decl) = 1;
1459 52311 : if (dump_file)
1460 7 : dump_function_to_file (node->decl, dump_file, dump_flags);
1461 :
1462 : /* Create the basic block we place call into. It is the entry basic block
1463 : split after last label and after the last eos clobber and debug stmt. */
1464 52311 : call_bb = split_point->entry_bb;
1465 211722 : for (gimple_stmt_iterator gsi = gsi_start_bb (call_bb); !gsi_end_p (gsi);)
1466 159380 : if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
1467 157748 : || gimple_clobber_p (gsi_stmt (gsi), CLOBBER_STORAGE_END)
1468 316976 : || is_gimple_debug (gsi_stmt (gsi)))
1469 : {
1470 107100 : last_stmt = gsi_stmt (gsi);
1471 107100 : gsi_next (&gsi);
1472 : }
1473 : else
1474 : break;
1475 :
1476 : /* Find the old return bb, it might contain some clobbers
1477 : which we want to copy back after the call. */
1478 52311 : basic_block old_return = nullptr;
1479 52311 : if (split_part_return_p)
1480 : {
1481 37889 : bool one_return_bb = true;
1482 123328 : FOR_EACH_EDGE (e, ei, return_bb->preds)
1483 96543 : if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1484 : {
1485 45211 : if (old_return != nullptr)
1486 : {
1487 : one_return_bb = false;
1488 : break;
1489 : }
1490 34107 : old_return = e->src;
1491 : }
1492 37889 : if (!one_return_bb)
1493 25526 : old_return = nullptr;
1494 : }
1495 :
1496 52311 : call_bb->count = split_point->count;
1497 52311 : e = split_block (split_point->entry_bb, last_stmt);
1498 52311 : if (old_return == e->src)
1499 20484 : old_return = e->dest;
1500 52311 : remove_edge (e);
1501 :
1502 : /* Produce the call statement. */
1503 52311 : gimple_stmt_iterator gsi = gsi_last_bb (call_bb);
1504 163266 : FOR_EACH_VEC_ELT (args_to_pass, i, arg)
1505 73166 : if (!is_gimple_val (arg))
1506 : {
1507 7 : arg = force_gimple_operand_gsi (&gsi, arg, true, NULL_TREE,
1508 : false, GSI_CONTINUE_LINKING);
1509 7 : args_to_pass[i] = arg;
1510 : }
1511 52311 : call = gimple_build_call_vec (node->decl, args_to_pass);
1512 52311 : if (cur_node->get_fun ()->has_musttail && !add_tsan_func_exit)
1513 4 : gimple_call_set_must_tail (call, true);
1514 52311 : gimple_set_block (call, DECL_INITIAL (current_function_decl));
1515 52311 : args_to_pass.release ();
1516 :
1517 : /* For optimized away parameters, add on the caller side
1518 : before the call
1519 : DEBUG D#X => parm_Y(D)
1520 : stmts and associate D#X with parm in decl_debug_args_lookup
1521 : vector to say for debug info that if parameter parm had been passed,
1522 : it would have value parm_Y(D). */
1523 52311 : if (args_to_skip)
1524 : {
1525 51555 : vec<tree, va_gc> **debug_args = NULL;
1526 51555 : unsigned i = 0, len = 0;
1527 51555 : if (MAY_HAVE_DEBUG_BIND_STMTS)
1528 : {
1529 44929 : debug_args = decl_debug_args_lookup (node->decl);
1530 44929 : if (debug_args)
1531 19574 : len = vec_safe_length (*debug_args);
1532 : }
1533 51555 : for (parm = DECL_ARGUMENTS (current_function_decl), num = 0;
1534 163062 : parm; parm = DECL_CHAIN (parm), num++)
1535 111507 : if (bitmap_bit_p (args_to_skip, num) && is_gimple_reg (parm))
1536 : {
1537 37362 : tree ddecl;
1538 37362 : gimple *def_temp;
1539 :
1540 : /* This needs to be done even without
1541 : MAY_HAVE_DEBUG_BIND_STMTS, otherwise if it didn't exist
1542 : before, we'd end up with different SSA_NAME_VERSIONs
1543 : between -g and -g0. */
1544 37362 : arg = get_or_create_ssa_default_def (cfun, parm);
1545 37362 : if (!MAY_HAVE_DEBUG_BIND_STMTS || debug_args == NULL)
1546 6576 : continue;
1547 :
1548 83996 : while (i < len && (**debug_args)[i] != DECL_ORIGIN (parm))
1549 11212 : i += 2;
1550 30786 : if (i >= len)
1551 0 : continue;
1552 30786 : ddecl = (**debug_args)[i + 1];
1553 30786 : def_temp
1554 30786 : = gimple_build_debug_bind (ddecl, unshare_expr (arg), call);
1555 30786 : gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
1556 : }
1557 51555 : BITMAP_FREE (args_to_skip);
1558 : }
1559 :
1560 : /* We avoid address being taken on any variable used by split part,
1561 : so return slot optimization is always possible. Moreover this is
1562 : required to make DECL_BY_REFERENCE work. */
1563 52311 : if (aggregate_value_p (DECL_RESULT (current_function_decl),
1564 52311 : TREE_TYPE (current_function_decl))
1565 52311 : && (!is_gimple_reg_type (TREE_TYPE (DECL_RESULT (current_function_decl)))
1566 155 : || DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))))
1567 455 : gimple_call_set_return_slot_opt (call, true);
1568 :
1569 52311 : if (add_tsan_func_exit)
1570 2 : tsan_func_exit_call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
1571 :
1572 : /* Update return value. This is bit tricky. When we do not return,
1573 : do nothing. When we return we might need to update return_bb
1574 : or produce a new return statement. */
1575 52311 : if (!split_part_return_p)
1576 : {
1577 14422 : gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1578 14422 : if (tsan_func_exit_call)
1579 0 : gsi_insert_after (&gsi, tsan_func_exit_call, GSI_NEW_STMT);
1580 : }
1581 : else
1582 : {
1583 75778 : e = make_single_succ_edge (call_bb, return_bb,
1584 37889 : return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
1585 37889 : ? 0 : EDGE_FALLTHRU);
1586 :
1587 : /* If there is return basic block, see what value we need to store
1588 : return value into and put call just before it. */
1589 37889 : if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
1590 : {
1591 37764 : real_retval = retval;
1592 37764 : if (real_retval && split_point->split_part_set_retval)
1593 : {
1594 12551 : gphi_iterator psi;
1595 :
1596 : /* See if we need new SSA_NAME for the result.
1597 : When DECL_BY_REFERENCE is true, retval is actually pointer to
1598 : return value and it is constant in whole function. */
1599 12551 : if (TREE_CODE (retval) == SSA_NAME
1600 12551 : && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1601 : {
1602 10406 : retval = copy_ssa_name (retval, call);
1603 :
1604 : /* See if there is PHI defining return value. */
1605 10406 : for (psi = gsi_start_phis (return_bb);
1606 10406 : !gsi_end_p (psi); gsi_next (&psi))
1607 20804 : if (!virtual_operand_p (gimple_phi_result (psi.phi ())))
1608 : break;
1609 :
1610 : /* When there is PHI, just update its value. */
1611 10406 : if (TREE_CODE (retval) == SSA_NAME
1612 10406 : && !gsi_end_p (psi))
1613 10402 : add_phi_arg (psi.phi (), retval, e, UNKNOWN_LOCATION);
1614 : /* Otherwise update the return BB itself.
1615 : find_return_bb allows at most one assignment to return value,
1616 : so update first statement. */
1617 : else
1618 : {
1619 4 : gimple_stmt_iterator bsi;
1620 4 : gassign *assign;
1621 12 : for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi);
1622 4 : gsi_next (&bsi))
1623 8 : if (greturn *return_stmt
1624 8 : = dyn_cast <greturn *> (gsi_stmt (bsi)))
1625 : {
1626 4 : gimple_return_set_retval (return_stmt, retval);
1627 4 : break;
1628 : }
1629 4 : else if ((assign = dyn_cast<gassign *> (gsi_stmt (bsi)))
1630 0 : && !gimple_clobber_p (assign))
1631 : {
1632 0 : gimple_assign_set_rhs1 (assign, retval);
1633 0 : break;
1634 : }
1635 4 : update_stmt (gsi_stmt (bsi));
1636 : /* Also adjust clobbers and debug stmts in return_bb. */
1637 16 : for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi);
1638 8 : gsi_next (&bsi))
1639 : {
1640 8 : gimple *stmt = gsi_stmt (bsi);
1641 8 : if (gimple_clobber_p (stmt)
1642 8 : || is_gimple_debug (stmt))
1643 : {
1644 2 : ssa_op_iter iter;
1645 2 : use_operand_p use_p;
1646 2 : bool update = false;
1647 2 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter,
1648 : SSA_OP_USE)
1649 0 : if (USE_FROM_PTR (use_p) == real_retval)
1650 : {
1651 0 : SET_USE (use_p, retval);
1652 0 : update = true;
1653 : }
1654 2 : if (update)
1655 0 : update_stmt (stmt);
1656 : }
1657 : }
1658 : }
1659 : }
1660 12551 : if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1661 : {
1662 0 : gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1663 0 : gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1664 : }
1665 : else
1666 : {
1667 12551 : tree restype;
1668 12551 : restype = TREE_TYPE (DECL_RESULT (current_function_decl));
1669 12551 : gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1670 12551 : if (!useless_type_conversion_p (TREE_TYPE (retval), restype))
1671 : {
1672 0 : gimple *cpy;
1673 0 : tree tem = create_tmp_reg (restype);
1674 0 : tem = make_ssa_name (tem, call);
1675 0 : cpy = gimple_build_assign (retval, NOP_EXPR, tem);
1676 0 : gsi_insert_after (&gsi, cpy, GSI_NEW_STMT);
1677 0 : retval = tem;
1678 : }
1679 12551 : gimple_call_set_lhs (call, retval);
1680 12551 : update_stmt (call);
1681 : }
1682 12551 : }
1683 : else
1684 25213 : gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1685 37764 : if (tsan_func_exit_call)
1686 2 : gsi_insert_after (&gsi, tsan_func_exit_call, GSI_NEW_STMT);
1687 : }
1688 : /* We don't use return block (there is either no return in function or
1689 : multiple of them). So create new basic block with return statement.
1690 : */
1691 : else
1692 : {
1693 125 : greturn *ret;
1694 125 : if (split_point->split_part_set_retval
1695 125 : && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1696 : {
1697 83 : retval = DECL_RESULT (current_function_decl);
1698 :
1699 : /* We use temporary register to hold value when aggregate_value_p
1700 : is false. Similarly for DECL_BY_REFERENCE we must avoid extra
1701 : copy. */
1702 83 : if (!aggregate_value_p (retval, TREE_TYPE (current_function_decl))
1703 83 : && !DECL_BY_REFERENCE (retval))
1704 83 : retval = create_tmp_reg (TREE_TYPE (retval));
1705 83 : if (is_gimple_reg (retval))
1706 : {
1707 : /* When returning by reference, there is only one SSA name
1708 : assigned to RESULT_DECL (that is pointer to return value).
1709 : Look it up or create new one if it is missing. */
1710 65 : if (DECL_BY_REFERENCE (retval))
1711 0 : retval = get_or_create_ssa_default_def (cfun, retval);
1712 : /* Otherwise produce new SSA name for return value. */
1713 : else
1714 65 : retval = make_ssa_name (retval, call);
1715 : }
1716 83 : if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1717 0 : gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1718 : else
1719 83 : gimple_call_set_lhs (call, retval);
1720 83 : gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1721 : }
1722 : else
1723 : {
1724 42 : gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1725 42 : if (retval
1726 0 : && is_gimple_reg_type (TREE_TYPE (retval))
1727 42 : && !is_gimple_val (retval))
1728 : {
1729 0 : gassign *g
1730 0 : = gimple_build_assign (make_ssa_name (TREE_TYPE (retval)),
1731 : retval);
1732 0 : retval = gimple_assign_lhs (g);
1733 0 : gsi_insert_after (&gsi, g, GSI_NEW_STMT);
1734 : }
1735 : }
1736 125 : if (tsan_func_exit_call)
1737 0 : gsi_insert_after (&gsi, tsan_func_exit_call, GSI_NEW_STMT);
1738 125 : ret = gimple_build_return (retval);
1739 125 : gsi_insert_after (&gsi, ret, GSI_NEW_STMT);
1740 : }
1741 : }
1742 :
1743 : /* Move the clobbers from the old return bb to after the call. */
1744 52311 : if (old_return)
1745 : {
1746 23003 : gimple_stmt_iterator ngsi = gsi_last_bb (call_bb);
1747 23003 : gsi_next (&ngsi);
1748 46006 : for (gimple_stmt_iterator ogsi = gsi_last_bb (old_return);
1749 59131 : !gsi_end_p (ogsi); )
1750 : {
1751 59060 : gimple *stmt = *ogsi;
1752 59060 : if (is_gimple_debug (stmt))
1753 : {
1754 33669 : gsi_prev (&ogsi);
1755 33669 : continue;
1756 : }
1757 25391 : if (!gimple_clobber_p (stmt, CLOBBER_STORAGE_END))
1758 : break;
1759 : /* Change the vdef/vuse of the clobber to be renamed. */
1760 2459 : unlink_stmt_vdef (stmt);
1761 4918 : release_ssa_name (gimple_vdef (stmt));
1762 2459 : gimple_set_vuse (stmt, gimple_vop (cfun));
1763 2459 : gimple_set_vdef (stmt, gimple_vop (cfun));
1764 2459 : gimple_stmt_iterator nogsi = ogsi;
1765 :
1766 : /* Move to the previous stmt before the move happens. */
1767 2459 : gsi_prev (&ogsi);
1768 2459 : gsi_move_before (&nogsi, &ngsi, GSI_NEW_STMT);
1769 2459 : update_stmt (stmt);
1770 : }
1771 : }
1772 52311 : free_dominance_info (CDI_DOMINATORS);
1773 52311 : free_dominance_info (CDI_POST_DOMINATORS);
1774 52311 : compute_fn_summary (node, true);
1775 52311 : }
1776 :
1777 : /* Execute function splitting pass. */
1778 :
1779 : static unsigned int
1780 2154659 : execute_split_functions (void)
1781 : {
1782 2154659 : gimple_stmt_iterator bsi;
1783 2154659 : basic_block bb;
1784 2154659 : sreal overall_time = 0;
1785 2154659 : int overall_size = 0;
1786 2154659 : int todo = 0;
1787 2154659 : struct cgraph_node *node = cgraph_node::get (current_function_decl);
1788 :
1789 2154659 : if (flags_from_decl_or_type (current_function_decl)
1790 2154659 : & (ECF_NORETURN|ECF_MALLOC|ECF_RETURNS_TWICE))
1791 : {
1792 53761 : if (dump_file)
1793 0 : fprintf (dump_file, "Not splitting: noreturn/malloc/returns_twice "
1794 : "function.\n");
1795 : return 0;
1796 : }
1797 2100898 : if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
1798 : {
1799 61491 : if (dump_file)
1800 3 : fprintf (dump_file, "Not splitting: main function.\n");
1801 : return 0;
1802 : }
1803 2039407 : if (node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
1804 : {
1805 727 : if (dump_file)
1806 0 : fprintf (dump_file, "Not splitting: function is unlikely executed.\n");
1807 : return 0;
1808 : }
1809 : /* This can be relaxed; function might become inlinable after splitting
1810 : away the uninlinable part. */
1811 2038680 : if (ipa_fn_summaries
1812 2038639 : && ipa_fn_summaries->get (node)
1813 2038635 : && !ipa_fn_summaries->get (node)->inlinable)
1814 : {
1815 192026 : if (dump_file)
1816 0 : fprintf (dump_file, "Not splitting: not inlinable.\n");
1817 : return 0;
1818 : }
1819 1846654 : if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1820 : {
1821 301684 : if (dump_file)
1822 0 : fprintf (dump_file, "Not splitting: disregarding inline limits.\n");
1823 : return 0;
1824 : }
1825 : /* This can be relaxed; most of versioning tests actually prevents
1826 : a duplication. */
1827 1544970 : if (!tree_versionable_function_p (current_function_decl))
1828 : {
1829 37 : if (dump_file)
1830 0 : fprintf (dump_file, "Not splitting: not versionable.\n");
1831 : return 0;
1832 : }
1833 : /* FIXME: we could support this. */
1834 1544933 : if (DECL_STRUCT_FUNCTION (current_function_decl)->static_chain_decl)
1835 : {
1836 1899 : if (dump_file)
1837 0 : fprintf (dump_file, "Not splitting: nested function.\n");
1838 : return 0;
1839 : }
1840 :
1841 : /* See if it makes sense to try to split.
1842 : It makes sense to split if we inline, that is if we have direct calls to
1843 : handle or direct calls are possibly going to appear as result of indirect
1844 : inlining or LTO. Also handle -fprofile-generate as LTO to allow non-LTO
1845 : training for LTO -fprofile-use build.
1846 :
1847 : Note that we are not completely conservative about disqualifying functions
1848 : called once. It is possible that the caller is called more then once and
1849 : then inlining would still benefit. */
1850 1543034 : if ((!node->callers
1851 : /* Local functions called once will be completely inlined most of time. */
1852 1088831 : || (!node->callers->next_caller && node->local))
1853 531332 : && !node->address_taken
1854 439282 : && !node->has_aliases_p ()
1855 1785805 : && (!flag_lto || !node->externally_visible))
1856 : {
1857 230018 : if (dump_file)
1858 21 : fprintf (dump_file, "Not splitting: not called directly "
1859 : "or called once.\n");
1860 : return 0;
1861 : }
1862 :
1863 : /* FIXME: We can actually split if splitting reduces call overhead. */
1864 1313016 : if (!flag_inline_small_functions
1865 1313016 : && !DECL_DECLARED_INLINE_P (current_function_decl))
1866 : {
1867 22 : if (dump_file)
1868 0 : fprintf (dump_file, "Not splitting: not autoinlining and function"
1869 : " is not inline.\n");
1870 : return 0;
1871 : }
1872 :
1873 1312994 : if (lookup_attribute ("noinline", DECL_ATTRIBUTES (current_function_decl)))
1874 : {
1875 0 : if (dump_file)
1876 0 : fprintf (dump_file, "Not splitting: function is noinline.\n");
1877 : return 0;
1878 : }
1879 1312994 : if (lookup_attribute ("section", DECL_ATTRIBUTES (current_function_decl)))
1880 : {
1881 15 : if (dump_file)
1882 0 : fprintf (dump_file, "Not splitting: function is in user defined "
1883 : "section.\n");
1884 : return 0;
1885 : }
1886 1312979 : if (!strub_splittable_p (node))
1887 : {
1888 136 : if (dump_file)
1889 0 : fprintf (dump_file, "Not splitting: function is a strub context.\n");
1890 : return 0;
1891 : }
1892 :
1893 : /* We enforce splitting after loop headers when profile info is not
1894 : available. */
1895 1312843 : if (profile_status_for_fn (cfun) != PROFILE_READ)
1896 1312765 : mark_dfs_back_edges ();
1897 :
1898 : /* Initialize bitmap to track forbidden calls. */
1899 1312843 : forbidden_dominators = BITMAP_ALLOC (NULL);
1900 1312843 : calculate_dominance_info (CDI_DOMINATORS);
1901 :
1902 : /* Compute local info about basic blocks and determine function size/time. */
1903 1312843 : bb_info_vec.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1, true);
1904 1312843 : best_split_point.split_bbs = NULL;
1905 1312843 : basic_block return_bb = find_return_bb ();
1906 1312843 : int tsan_exit_found = -1;
1907 6183835 : FOR_EACH_BB_FN (bb, cfun)
1908 : {
1909 4871008 : sreal time = 0;
1910 4871008 : int size = 0;
1911 4871008 : sreal freq = bb->count.to_sreal_scale
1912 4871008 : (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count);
1913 :
1914 4871008 : if (dump_file && (dump_flags & TDF_DETAILS))
1915 9 : fprintf (dump_file, "Basic block %i\n", bb->index);
1916 :
1917 51976670 : for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1918 : {
1919 42234670 : sreal this_time;
1920 42234670 : int this_size;
1921 42234670 : gimple *stmt = gsi_stmt (bsi);
1922 :
1923 42234670 : this_size = estimate_num_insns (stmt, &eni_size_weights);
1924 42234670 : this_time = (sreal)estimate_num_insns (stmt, &eni_time_weights)
1925 42234670 : * freq;
1926 42234670 : size += this_size;
1927 42234670 : time += this_time;
1928 42234670 : check_forbidden_calls (stmt);
1929 :
1930 42234670 : if (dump_file && (dump_flags & TDF_DETAILS))
1931 : {
1932 17 : fprintf (dump_file, " freq:%4.2f size:%3i time:%4.2f ",
1933 : freq.to_double (), this_size, this_time.to_double ());
1934 17 : print_gimple_stmt (dump_file, stmt, 0);
1935 : }
1936 :
1937 42234670 : if ((flag_sanitize & SANITIZE_THREAD)
1938 42234670 : && gimple_call_internal_p (stmt, IFN_TSAN_FUNC_EXIT))
1939 : {
1940 : /* We handle TSAN_FUNC_EXIT for splitting either in the
1941 : return_bb, or in its immediate predecessors. */
1942 121 : if ((bb != return_bb && !find_edge (bb, return_bb))
1943 255 : || (tsan_exit_found != -1
1944 2 : && tsan_exit_found != (bb != return_bb)))
1945 : {
1946 16 : if (dump_file)
1947 0 : fprintf (dump_file, "Not splitting: TSAN_FUNC_EXIT"
1948 : " in unexpected basic block.\n");
1949 16 : BITMAP_FREE (forbidden_dominators);
1950 16 : bb_info_vec.release ();
1951 16 : return 0;
1952 : }
1953 134 : tsan_exit_found = bb != return_bb;
1954 : }
1955 : }
1956 4870992 : overall_time += time;
1957 4870992 : overall_size += size;
1958 4870992 : bb_info_vec[bb->index].time = time;
1959 4870992 : bb_info_vec[bb->index].size = size;
1960 : }
1961 1312827 : find_split_points (return_bb, overall_time, overall_size);
1962 1312827 : if (best_split_point.split_bbs)
1963 : {
1964 52311 : split_function (return_bb, &best_split_point, tsan_exit_found == 1);
1965 52311 : BITMAP_FREE (best_split_point.ssa_names_to_pass);
1966 52311 : BITMAP_FREE (best_split_point.split_bbs);
1967 52311 : todo = TODO_update_ssa | TODO_cleanup_cfg;
1968 : }
1969 1312827 : BITMAP_FREE (forbidden_dominators);
1970 1312827 : bb_info_vec.release ();
1971 1312827 : return todo;
1972 : }
1973 :
1974 : namespace {
1975 :
1976 : const pass_data pass_data_split_functions =
1977 : {
1978 : GIMPLE_PASS, /* type */
1979 : "fnsplit", /* name */
1980 : OPTGROUP_NONE, /* optinfo_flags */
1981 : TV_IPA_FNSPLIT, /* tv_id */
1982 : PROP_cfg, /* properties_required */
1983 : 0, /* properties_provided */
1984 : 0, /* properties_destroyed */
1985 : 0, /* todo_flags_start */
1986 : 0, /* todo_flags_finish */
1987 : };
1988 :
1989 : class pass_split_functions : public gimple_opt_pass
1990 : {
1991 : public:
1992 302985 : pass_split_functions (gcc::context *ctxt)
1993 605970 : : gimple_opt_pass (pass_data_split_functions, ctxt)
1994 : {}
1995 :
1996 : /* opt_pass methods: */
1997 : bool gate (function *) final override;
1998 2154198 : unsigned int execute (function *) final override
1999 : {
2000 2154198 : return execute_split_functions ();
2001 : }
2002 :
2003 : }; // class pass_split_functions
2004 :
2005 : bool
2006 2527232 : pass_split_functions::gate (function *)
2007 : {
2008 : /* When doing profile feedback, we want to execute the pass after profiling
2009 : is read. So disable one in early optimization. */
2010 2527232 : return (flag_partial_inlining
2011 2156583 : && !profile_arc_flag
2012 2154839 : && !flag_branch_probabilities
2013 4681564 : && !flag_auto_profile);
2014 : }
2015 :
2016 : } // anon namespace
2017 :
2018 : gimple_opt_pass *
2019 302985 : make_pass_split_functions (gcc::context *ctxt)
2020 : {
2021 302985 : return new pass_split_functions (ctxt);
2022 : }
2023 :
2024 : /* Execute function splitting pass. */
2025 :
2026 : static unsigned int
2027 461 : execute_feedback_split_functions (void)
2028 : {
2029 0 : unsigned int retval = execute_split_functions ();
2030 461 : if (retval)
2031 2 : retval |= TODO_rebuild_cgraph_edges;
2032 461 : return retval;
2033 : }
2034 :
2035 : namespace {
2036 :
2037 : const pass_data pass_data_feedback_split_functions =
2038 : {
2039 : GIMPLE_PASS, /* type */
2040 : "feedback_fnsplit", /* name */
2041 : OPTGROUP_NONE, /* optinfo_flags */
2042 : TV_IPA_FNSPLIT, /* tv_id */
2043 : PROP_cfg, /* properties_required */
2044 : 0, /* properties_provided */
2045 : 0, /* properties_destroyed */
2046 : 0, /* todo_flags_start */
2047 : 0, /* todo_flags_finish */
2048 : };
2049 :
2050 : class pass_feedback_split_functions : public gimple_opt_pass
2051 : {
2052 : public:
2053 605970 : pass_feedback_split_functions (gcc::context *ctxt)
2054 1211940 : : gimple_opt_pass (pass_data_feedback_split_functions, ctxt)
2055 : {}
2056 :
2057 : /* opt_pass methods: */
2058 : bool gate (function *) final override;
2059 461 : unsigned int execute (function *) final override
2060 : {
2061 461 : return execute_feedback_split_functions ();
2062 : }
2063 :
2064 302985 : opt_pass * clone () final override
2065 : {
2066 302985 : return new pass_feedback_split_functions (m_ctxt);
2067 : }
2068 : }; // class pass_feedback_split_functions
2069 :
2070 : bool
2071 3051 : pass_feedback_split_functions::gate (function *)
2072 : {
2073 : /* We don't need to split when profiling at all, we are producing
2074 : lousy code anyway. */
2075 3051 : return (flag_partial_inlining
2076 3051 : && (flag_branch_probabilities || flag_auto_profile));
2077 : }
2078 :
2079 : } // anon namespace
2080 :
2081 : gimple_opt_pass *
2082 302985 : make_pass_feedback_split_functions (gcc::context *ctxt)
2083 : {
2084 302985 : return new pass_feedback_split_functions (ctxt);
2085 : }
|