1 | /* Function splitting pass |
2 | Copyright (C) 2010-2024 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 | 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 | class split_point |
125 | { |
126 | public: |
127 | /* Size of the partitions. */ |
128 | sreal , split_time; |
129 | unsigned int , 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 | test_nonssa_use (gimple *, tree t, tree, void *data) |
164 | { |
165 | t = get_base_address (t); |
166 | |
167 | if (!t || is_gimple_reg (t)) |
168 | return false; |
169 | |
170 | if (TREE_CODE (t) == PARM_DECL |
171 | || (VAR_P (t) |
172 | && auto_var_in_fn_p (t, current_function_decl)) |
173 | || 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 | || (TREE_CODE (t) == LABEL_DECL |
178 | && FORCED_LABEL (t))) |
179 | 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 | if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t)) |
184 | && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME |
185 | && SSA_NAME_VAR (TREE_OPERAND (t, 0)) |
186 | && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL |
187 | && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))) |
188 | return |
189 | bitmap_bit_p ((bitmap)data, |
190 | DECL_UID (DECL_RESULT (current_function_decl))); |
191 | |
192 | return false; |
193 | } |
194 | |
195 | /* Dump split point CURRENT. */ |
196 | |
197 | static void |
198 | dump_split_point (FILE * file, class split_point *current) |
199 | { |
200 | fprintf (stream: file, |
201 | format: "Split point at BB %i\n" |
202 | " header time: %f header size: %i\n" |
203 | " split time: %f split size: %i\n bbs: " , |
204 | current->entry_bb->index, current->header_time.to_double (), |
205 | current->header_size, current->split_time.to_double (), |
206 | current->split_size); |
207 | dump_bitmap (file, map: current->split_bbs); |
208 | fprintf (stream: file, format: " SSA names to pass: " ); |
209 | dump_bitmap (file, map: current->ssa_names_to_pass); |
210 | } |
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 | verify_non_ssa_vars (class split_point *current, bitmap non_ssa_vars, |
218 | basic_block return_bb) |
219 | { |
220 | bitmap seen = BITMAP_ALLOC (NULL); |
221 | vec<basic_block> worklist = vNULL; |
222 | edge e; |
223 | edge_iterator ei; |
224 | bool ok = true; |
225 | basic_block bb; |
226 | |
227 | FOR_EACH_EDGE (e, ei, current->entry_bb->preds) |
228 | if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun) |
229 | && !bitmap_bit_p (current->split_bbs, e->src->index)) |
230 | { |
231 | worklist.safe_push (obj: e->src); |
232 | bitmap_set_bit (seen, e->src->index); |
233 | } |
234 | |
235 | while (!worklist.is_empty ()) |
236 | { |
237 | bb = worklist.pop (); |
238 | FOR_EACH_EDGE (e, ei, bb->preds) |
239 | if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun) |
240 | && bitmap_set_bit (seen, e->src->index)) |
241 | { |
242 | gcc_checking_assert (!bitmap_bit_p (current->split_bbs, |
243 | e->src->index)); |
244 | worklist.safe_push (obj: e->src); |
245 | } |
246 | for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (i: bsi); |
247 | gsi_next (i: &bsi)) |
248 | { |
249 | gimple *stmt = gsi_stmt (i: bsi); |
250 | if (is_gimple_debug (gs: stmt)) |
251 | continue; |
252 | if (walk_stmt_load_store_addr_ops |
253 | (stmt, non_ssa_vars, test_nonssa_use, test_nonssa_use, |
254 | test_nonssa_use)) |
255 | { |
256 | ok = false; |
257 | goto done; |
258 | } |
259 | if (glabel *label_stmt = dyn_cast <glabel *> (p: stmt)) |
260 | if (test_nonssa_use (stmt, t: gimple_label_label (gs: label_stmt), |
261 | NULL_TREE, data: non_ssa_vars)) |
262 | { |
263 | ok = false; |
264 | goto done; |
265 | } |
266 | } |
267 | for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (i: bsi); |
268 | gsi_next (i: &bsi)) |
269 | { |
270 | if (walk_stmt_load_store_addr_ops |
271 | (gsi_stmt (i: bsi), non_ssa_vars, test_nonssa_use, test_nonssa_use, |
272 | test_nonssa_use)) |
273 | { |
274 | ok = false; |
275 | goto done; |
276 | } |
277 | } |
278 | FOR_EACH_EDGE (e, ei, bb->succs) |
279 | { |
280 | if (e->dest != return_bb) |
281 | continue; |
282 | for (gphi_iterator bsi = gsi_start_phis (return_bb); |
283 | !gsi_end_p (i: bsi); |
284 | gsi_next (i: &bsi)) |
285 | { |
286 | gphi *stmt = bsi.phi (); |
287 | tree op = gimple_phi_arg_def (gs: stmt, index: e->dest_idx); |
288 | |
289 | if (virtual_operand_p (op: gimple_phi_result (gs: stmt))) |
290 | continue; |
291 | if (TREE_CODE (op) != SSA_NAME |
292 | && test_nonssa_use (stmt, t: op, op, data: non_ssa_vars)) |
293 | { |
294 | ok = false; |
295 | 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 | FOR_EACH_BB_FN (bb, cfun) |
304 | if (!bitmap_bit_p (current->split_bbs, bb->index) |
305 | && !bitmap_bit_p (seen, bb->index)) |
306 | { |
307 | gimple_stmt_iterator bsi; |
308 | for (bsi = gsi_start_bb (bb); !gsi_end_p (i: bsi); gsi_next (i: &bsi)) |
309 | if (glabel *label_stmt = dyn_cast <glabel *> (p: gsi_stmt (i: bsi))) |
310 | { |
311 | if (test_nonssa_use (label_stmt, |
312 | t: gimple_label_label (gs: label_stmt), |
313 | NULL_TREE, data: non_ssa_vars)) |
314 | { |
315 | ok = false; |
316 | goto done; |
317 | } |
318 | } |
319 | else |
320 | break; |
321 | } |
322 | |
323 | done: |
324 | BITMAP_FREE (seen); |
325 | worklist.release (); |
326 | 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 | check_forbidden_calls (gimple *stmt) |
340 | { |
341 | imm_use_iterator use_iter; |
342 | use_operand_p use_p; |
343 | tree lhs; |
344 | |
345 | /* At the moment, __builtin_constant_p is the only forbidden |
346 | predicate function call (see PR49642). */ |
347 | if (!gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P)) |
348 | return; |
349 | |
350 | lhs = gimple_call_lhs (gs: stmt); |
351 | |
352 | if (!lhs || TREE_CODE (lhs) != SSA_NAME) |
353 | return; |
354 | |
355 | FOR_EACH_IMM_USE_FAST (use_p, use_iter, lhs) |
356 | { |
357 | tree op1; |
358 | basic_block use_bb, forbidden_bb; |
359 | enum tree_code code; |
360 | edge true_edge, false_edge; |
361 | gcond *use_stmt; |
362 | |
363 | use_stmt = dyn_cast <gcond *> (USE_STMT (use_p)); |
364 | if (!use_stmt) |
365 | continue; |
366 | |
367 | /* Assuming canonical form for GIMPLE_COND here, with constant |
368 | in second position. */ |
369 | op1 = gimple_cond_rhs (gs: use_stmt); |
370 | code = gimple_cond_code (gs: use_stmt); |
371 | use_bb = gimple_bb (g: use_stmt); |
372 | |
373 | 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 | if (!integer_zerop (op1) || code == LE_EXPR || code == GE_EXPR) |
378 | continue; |
379 | |
380 | if (code == EQ_EXPR) |
381 | forbidden_bb = false_edge->dest; |
382 | else |
383 | forbidden_bb = true_edge->dest; |
384 | |
385 | bitmap_set_bit (forbidden_dominators, forbidden_bb->index); |
386 | } |
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 | dominated_by_forbidden (basic_block bb) |
394 | { |
395 | unsigned dom_bb; |
396 | bitmap_iterator bi; |
397 | |
398 | EXECUTE_IF_SET_IN_BITMAP (forbidden_dominators, 1, dom_bb, bi) |
399 | { |
400 | if (dominated_by_p (CDI_DOMINATORS, bb, |
401 | 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 | split_part_set_ssa_name_p (tree val, class split_point *current, |
412 | basic_block return_bb) |
413 | { |
414 | if (TREE_CODE (val) != SSA_NAME) |
415 | return false; |
416 | |
417 | return (!SSA_NAME_IS_DEFAULT_DEF (val) |
418 | && (bitmap_bit_p (current->split_bbs, |
419 | gimple_bb (SSA_NAME_DEF_STMT (val))->index) |
420 | || 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 | consider_split (class split_point *current, bitmap non_ssa_vars, |
429 | basic_block return_bb) |
430 | { |
431 | tree parm; |
432 | unsigned int num_args = 0; |
433 | unsigned int call_overhead; |
434 | edge e; |
435 | edge_iterator ei; |
436 | gphi_iterator bsi; |
437 | unsigned int i; |
438 | tree retval; |
439 | bool back_edge = false; |
440 | |
441 | if (dump_file && (dump_flags & TDF_DETAILS)) |
442 | dump_split_point (file: dump_file, current); |
443 | |
444 | current->count = profile_count::zero (); |
445 | FOR_EACH_EDGE (e, ei, current->entry_bb->preds) |
446 | { |
447 | if (e->flags & EDGE_DFS_BACK) |
448 | back_edge = true; |
449 | if (!bitmap_bit_p (current->split_bbs, e->src->index)) |
450 | 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 | if (!(current->count |
457 | < (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.apply_scale |
458 | (param_partial_inlining_entry_probability, den: 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 | if (back_edge |
466 | && profile_status_for_fn (cfun) != PROFILE_READ |
467 | && current->count |
468 | < ENTRY_BLOCK_PTR_FOR_FN (cfun)->count) |
469 | { |
470 | if (dump_file && (dump_flags & TDF_DETAILS)) |
471 | { |
472 | fprintf (stream: dump_file, |
473 | format: " Split before loop, accepting despite low counts" ); |
474 | current->count.dump (f: dump_file); |
475 | fprintf (stream: dump_file, format: " " ); |
476 | ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.dump (f: dump_file); |
477 | } |
478 | } |
479 | else |
480 | { |
481 | if (dump_file && (dump_flags & TDF_DETAILS)) |
482 | fprintf (stream: dump_file, |
483 | format: " Refused: incoming frequency is too large.\n" ); |
484 | return; |
485 | } |
486 | } |
487 | |
488 | if (!current->header_size) |
489 | { |
490 | if (dump_file && (dump_flags & TDF_DETAILS)) |
491 | fprintf (stream: dump_file, format: " 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 | for (bsi = gsi_start_phis (current->entry_bb); !gsi_end_p (i: bsi); gsi_next (i: &bsi)) |
498 | { |
499 | gphi *stmt = bsi.phi (); |
500 | tree val = NULL; |
501 | |
502 | if (virtual_operand_p (op: gimple_phi_result (gs: stmt))) |
503 | continue; |
504 | for (i = 0; i < gimple_phi_num_args (gs: stmt); i++) |
505 | { |
506 | edge e = gimple_phi_arg_edge (phi: stmt, i); |
507 | if (!bitmap_bit_p (current->split_bbs, e->src->index)) |
508 | { |
509 | tree edge_val = gimple_phi_arg_def (gs: stmt, index: i); |
510 | if (val && edge_val != val) |
511 | { |
512 | if (dump_file && (dump_flags & TDF_DETAILS)) |
513 | fprintf (stream: dump_file, |
514 | format: " 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 | call_overhead = eni_size_weights.call_cost; |
526 | for (parm = DECL_ARGUMENTS (current_function_decl); parm; |
527 | parm = DECL_CHAIN (parm)) |
528 | { |
529 | if (!is_gimple_reg (parm)) |
530 | { |
531 | if (bitmap_bit_p (non_ssa_vars, DECL_UID (parm))) |
532 | { |
533 | if (dump_file && (dump_flags & TDF_DETAILS)) |
534 | fprintf (stream: dump_file, |
535 | format: " Refused: need to pass non-ssa param values\n" ); |
536 | return; |
537 | } |
538 | } |
539 | else |
540 | { |
541 | tree ddef = ssa_default_def (cfun, parm); |
542 | if (ddef |
543 | && bitmap_bit_p (current->ssa_names_to_pass, |
544 | SSA_NAME_VERSION (ddef))) |
545 | { |
546 | if (!VOID_TYPE_P (TREE_TYPE (parm))) |
547 | call_overhead += estimate_move_cost (TREE_TYPE (parm), false); |
548 | num_args++; |
549 | } |
550 | } |
551 | } |
552 | if (!VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))) |
553 | call_overhead += estimate_move_cost (TREE_TYPE (TREE_TYPE |
554 | (current_function_decl)), |
555 | false); |
556 | |
557 | if (current->split_size <= call_overhead) |
558 | { |
559 | if (dump_file && (dump_flags & TDF_DETAILS)) |
560 | fprintf (stream: dump_file, |
561 | format: " 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 | if (current->header_size + call_overhead |
568 | >= (unsigned int)(DECL_DECLARED_INLINE_P (current_function_decl) |
569 | ? param_max_inline_insns_single |
570 | : param_max_inline_insns_auto) + 10) |
571 | { |
572 | if (dump_file && (dump_flags & TDF_DETAILS)) |
573 | fprintf (stream: dump_file, |
574 | format: " 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 | if (DECL_ONE_ONLY (current_function_decl) |
583 | && current->split_size >= (unsigned int) param_max_inline_insns_auto + 10) |
584 | { |
585 | if (dump_file && (dump_flags & TDF_DETAILS)) |
586 | fprintf (stream: dump_file, |
587 | format: " 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 | if (DECL_ONE_ONLY (current_function_decl) |
594 | && current->split_size |
595 | <= (unsigned int) param_early_inlining_insns / 2) |
596 | { |
597 | if (dump_file && (dump_flags & TDF_DETAILS)) |
598 | fprintf (stream: dump_file, |
599 | format: " 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 | if (num_args != bitmap_count_bits (current->ssa_names_to_pass)) |
607 | { |
608 | |
609 | if (dump_file && (dump_flags & TDF_DETAILS)) |
610 | fprintf (stream: dump_file, |
611 | format: " 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 | if (!bitmap_empty_p (map: non_ssa_vars) |
619 | && !verify_non_ssa_vars (current, non_ssa_vars, return_bb)) |
620 | { |
621 | if (dump_file && (dump_flags & TDF_DETAILS)) |
622 | fprintf (stream: dump_file, |
623 | format: " 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 | if (!bitmap_empty_p (map: forbidden_dominators) |
630 | && dominated_by_forbidden (bb: current->entry_bb)) |
631 | { |
632 | if (dump_file && (dump_flags & TDF_DETAILS)) |
633 | fprintf (stream: dump_file, |
634 | format: " 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 | retval = find_retval (return_bb); |
649 | 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 | if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun) |
656 | && DECL_RESULT (current_function_decl) |
657 | && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))) |
658 | current->split_part_set_retval = false; |
659 | else |
660 | current->split_part_set_retval = true; |
661 | } |
662 | else if (is_gimple_min_invariant (retval)) |
663 | 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 | else if (TREE_CODE (retval) == SSA_NAME |
667 | && SSA_NAME_VAR (retval) |
668 | && TREE_CODE (SSA_NAME_VAR (retval)) == RESULT_DECL |
669 | && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))) |
670 | current->split_part_set_retval |
671 | = bitmap_bit_p (non_ssa_vars, DECL_UID (SSA_NAME_VAR (retval))); |
672 | else if (TREE_CODE (retval) == SSA_NAME) |
673 | current->split_part_set_retval |
674 | = split_part_set_ssa_name_p (val: retval, current, return_bb); |
675 | else if (TREE_CODE (retval) == PARM_DECL) |
676 | current->split_part_set_retval = false; |
677 | else if (VAR_P (retval) |
678 | || TREE_CODE (retval) == RESULT_DECL) |
679 | current->split_part_set_retval |
680 | = bitmap_bit_p (non_ssa_vars, DECL_UID (retval)); |
681 | else |
682 | 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 | if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun)) |
687 | { |
688 | gphi_iterator psi; |
689 | |
690 | for (psi = gsi_start_phis (return_bb); !gsi_end_p (i: psi); gsi_next (i: &psi)) |
691 | if (!virtual_operand_p (op: gimple_phi_result (gs: psi.phi ())) |
692 | && !(retval |
693 | && current->split_part_set_retval |
694 | && TREE_CODE (retval) == SSA_NAME |
695 | && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)) |
696 | && SSA_NAME_DEF_STMT (retval) == psi.phi ())) |
697 | { |
698 | if (dump_file && (dump_flags & TDF_DETAILS)) |
699 | fprintf (stream: dump_file, |
700 | format: " Refused: return bb has extra PHIs\n" ); |
701 | return; |
702 | } |
703 | } |
704 | |
705 | if (dump_file && (dump_flags & TDF_DETAILS)) |
706 | fprintf (stream: dump_file, format: " 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 | if (!best_split_point.split_bbs |
712 | || best_split_point.count |
713 | > current->count |
714 | || (best_split_point.count == current->count |
715 | && best_split_point.split_size < current->split_size)) |
716 | |
717 | { |
718 | if (dump_file && (dump_flags & TDF_DETAILS)) |
719 | fprintf (stream: dump_file, format: " New best split point!\n" ); |
720 | if (best_split_point.ssa_names_to_pass) |
721 | { |
722 | BITMAP_FREE (best_split_point.ssa_names_to_pass); |
723 | BITMAP_FREE (best_split_point.split_bbs); |
724 | } |
725 | best_split_point = *current; |
726 | best_split_point.ssa_names_to_pass = BITMAP_ALLOC (NULL); |
727 | bitmap_copy (best_split_point.ssa_names_to_pass, |
728 | current->ssa_names_to_pass); |
729 | best_split_point.split_bbs = BITMAP_ALLOC (NULL); |
730 | 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 | find_return_bb (void) |
753 | { |
754 | edge e; |
755 | basic_block return_bb = EXIT_BLOCK_PTR_FOR_FN (cfun); |
756 | gimple_stmt_iterator bsi; |
757 | bool found_return = false; |
758 | tree retval = NULL_TREE; |
759 | |
760 | if (!single_pred_p (EXIT_BLOCK_PTR_FOR_FN (cfun))) |
761 | return return_bb; |
762 | |
763 | e = single_pred_edge (EXIT_BLOCK_PTR_FOR_FN (cfun)); |
764 | for (bsi = gsi_last_bb (bb: e->src); !gsi_end_p (i: bsi); gsi_prev (i: &bsi)) |
765 | { |
766 | gimple *stmt = gsi_stmt (i: bsi); |
767 | if (gimple_code (g: stmt) == GIMPLE_LABEL |
768 | || is_gimple_debug (gs: stmt) |
769 | || gimple_clobber_p (s: stmt)) |
770 | ; |
771 | else if (gimple_code (g: stmt) == GIMPLE_ASSIGN |
772 | && found_return |
773 | && gimple_assign_single_p (gs: stmt) |
774 | && (auto_var_in_fn_p (gimple_assign_rhs1 (gs: stmt), |
775 | current_function_decl) |
776 | || is_gimple_min_invariant (gimple_assign_rhs1 (gs: stmt))) |
777 | && retval == gimple_assign_lhs (gs: stmt)) |
778 | ; |
779 | else if (greturn *return_stmt = dyn_cast <greturn *> (p: stmt)) |
780 | { |
781 | found_return = true; |
782 | retval = gimple_return_retval (gs: return_stmt); |
783 | } |
784 | /* For -fsanitize=thread, allow also TSAN_FUNC_EXIT () in the return |
785 | bb. */ |
786 | else if ((flag_sanitize & SANITIZE_THREAD) |
787 | && gimple_call_internal_p (gs: stmt, fn: IFN_TSAN_FUNC_EXIT)) |
788 | ; |
789 | else |
790 | break; |
791 | } |
792 | if (gsi_end_p (i: bsi) && found_return) |
793 | return_bb = e->src; |
794 | |
795 | return return_bb; |
796 | } |
797 | |
798 | /* Given return basic block RETURN_BB, see where return value is really |
799 | stored. */ |
800 | static tree |
801 | find_retval (basic_block return_bb) |
802 | { |
803 | gimple_stmt_iterator bsi; |
804 | for (bsi = gsi_start_bb (bb: return_bb); !gsi_end_p (i: bsi); gsi_next (i: &bsi)) |
805 | if (greturn *return_stmt = dyn_cast <greturn *> (p: gsi_stmt (i: bsi))) |
806 | return gimple_return_retval (gs: return_stmt); |
807 | else if (gimple_code (g: gsi_stmt (i: bsi)) == GIMPLE_ASSIGN |
808 | && !gimple_clobber_p (s: gsi_stmt (i: bsi))) |
809 | return gimple_assign_rhs1 (gs: gsi_stmt (i: bsi)); |
810 | return NULL; |
811 | } |
812 | |
813 | /* Callback for walk_stmt_load_store_addr_ops. If T is non-SSA automatic |
814 | variable, mark it as used in bitmap passed via DATA. |
815 | Return true when access to T prevents splitting the function. */ |
816 | |
817 | static bool |
818 | mark_nonssa_use (gimple *, tree t, tree, void *data) |
819 | { |
820 | t = get_base_address (t); |
821 | |
822 | if (!t || is_gimple_reg (t)) |
823 | return false; |
824 | |
825 | /* At present we can't pass non-SSA arguments to split function. |
826 | FIXME: this can be relaxed by passing references to arguments. */ |
827 | if (TREE_CODE (t) == PARM_DECL) |
828 | { |
829 | if (dump_file && (dump_flags & TDF_DETAILS)) |
830 | fprintf (stream: dump_file, |
831 | format: "Cannot split: use of non-ssa function parameter.\n" ); |
832 | return true; |
833 | } |
834 | |
835 | if ((VAR_P (t) && auto_var_in_fn_p (t, current_function_decl)) |
836 | || TREE_CODE (t) == RESULT_DECL |
837 | || (TREE_CODE (t) == LABEL_DECL && FORCED_LABEL (t))) |
838 | bitmap_set_bit ((bitmap)data, DECL_UID (t)); |
839 | |
840 | /* For DECL_BY_REFERENCE, the return value is actually a pointer. We want |
841 | to pretend that the value pointed to is actual result decl. */ |
842 | if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t)) |
843 | && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME |
844 | && SSA_NAME_VAR (TREE_OPERAND (t, 0)) |
845 | && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL |
846 | && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))) |
847 | return |
848 | bitmap_bit_p ((bitmap)data, |
849 | DECL_UID (DECL_RESULT (current_function_decl))); |
850 | |
851 | return false; |
852 | } |
853 | |
854 | /* Compute local properties of basic block BB we collect when looking for |
855 | split points. We look for ssa defs and store them in SET_SSA_NAMES, |
856 | for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic |
857 | vars stored in NON_SSA_VARS. |
858 | |
859 | When BB has edge to RETURN_BB, collect uses in RETURN_BB too. |
860 | |
861 | Return false when BB contains something that prevents it from being put into |
862 | split function. */ |
863 | |
864 | static bool |
865 | visit_bb (basic_block bb, basic_block return_bb, |
866 | bitmap set_ssa_names, bitmap used_ssa_names, |
867 | bitmap non_ssa_vars) |
868 | { |
869 | edge e; |
870 | edge_iterator ei; |
871 | bool can_split = true; |
872 | |
873 | for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (i: bsi); |
874 | gsi_next (i: &bsi)) |
875 | { |
876 | gimple *stmt = gsi_stmt (i: bsi); |
877 | tree op; |
878 | ssa_op_iter iter; |
879 | |
880 | if (is_gimple_debug (gs: stmt)) |
881 | continue; |
882 | |
883 | if (gimple_clobber_p (s: stmt)) |
884 | continue; |
885 | |
886 | /* FIXME: We can split regions containing EH. We cannot however |
887 | split RESX, EH_DISPATCH and EH_POINTER referring to same region |
888 | into different partitions. This would require tracking of |
889 | EH regions and checking in consider_split_point if they |
890 | are not used elsewhere. */ |
891 | if (gimple_code (g: stmt) == GIMPLE_RESX) |
892 | { |
893 | if (dump_file && (dump_flags & TDF_DETAILS)) |
894 | fprintf (stream: dump_file, format: "Cannot split: resx.\n" ); |
895 | can_split = false; |
896 | } |
897 | if (gimple_code (g: stmt) == GIMPLE_EH_DISPATCH) |
898 | { |
899 | if (dump_file && (dump_flags & TDF_DETAILS)) |
900 | fprintf (stream: dump_file, format: "Cannot split: eh dispatch.\n" ); |
901 | can_split = false; |
902 | } |
903 | |
904 | /* Check calls that would prevent splitting. */ |
905 | if (gimple_code (g: stmt) == GIMPLE_CALL) |
906 | { |
907 | if (tree decl = gimple_call_fndecl (gs: stmt)) |
908 | { |
909 | /* Check builtins that would prevent splitting. */ |
910 | if (fndecl_built_in_p (node: decl, klass: BUILT_IN_NORMAL)) |
911 | switch (DECL_FUNCTION_CODE (decl)) |
912 | { |
913 | /* FIXME: once we will allow passing non-parm values to |
914 | split part, we need to be sure to handle correct |
915 | builtin_stack_save and builtin_stack_restore. At the |
916 | moment we are safe; there is no way to store |
917 | builtin_stack_save result in non-SSA variable since all |
918 | calls to those are compiler generated. */ |
919 | case BUILT_IN_APPLY: |
920 | case BUILT_IN_APPLY_ARGS: |
921 | case BUILT_IN_VA_START: |
922 | if (dump_file && (dump_flags & TDF_DETAILS)) |
923 | fprintf (stream: dump_file, |
924 | format: "Cannot split: builtin_apply and va_start.\n" ); |
925 | can_split = false; |
926 | break; |
927 | case BUILT_IN_EH_POINTER: |
928 | if (dump_file && (dump_flags & TDF_DETAILS)) |
929 | fprintf (stream: dump_file, |
930 | format: "Cannot split: builtin_eh_pointer.\n" ); |
931 | can_split = false; |
932 | break; |
933 | default: |
934 | break; |
935 | } |
936 | |
937 | /* Calls to functions (which have the warning or error |
938 | attribute on them) should not be split off into another |
939 | function. */ |
940 | if (lookup_attribute (attr_name: "warning" , DECL_ATTRIBUTES (decl)) |
941 | || lookup_attribute (attr_name: "error" , DECL_ATTRIBUTES (decl))) |
942 | { |
943 | if (dump_file && (dump_flags & TDF_DETAILS)) |
944 | fprintf (stream: dump_file, |
945 | format: "Cannot split: warning or error attribute.\n" ); |
946 | can_split = false; |
947 | } |
948 | } |
949 | } |
950 | |
951 | FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF) |
952 | bitmap_set_bit (set_ssa_names, SSA_NAME_VERSION (op)); |
953 | FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE) |
954 | bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op)); |
955 | can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars, |
956 | mark_nonssa_use, |
957 | mark_nonssa_use, |
958 | mark_nonssa_use); |
959 | } |
960 | for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (i: bsi); |
961 | gsi_next (i: &bsi)) |
962 | { |
963 | gphi *stmt = bsi.phi (); |
964 | unsigned int i; |
965 | |
966 | if (virtual_operand_p (op: gimple_phi_result (gs: stmt))) |
967 | continue; |
968 | bitmap_set_bit (set_ssa_names, |
969 | SSA_NAME_VERSION (gimple_phi_result (stmt))); |
970 | for (i = 0; i < gimple_phi_num_args (gs: stmt); i++) |
971 | { |
972 | tree op = gimple_phi_arg_def (gs: stmt, index: i); |
973 | if (TREE_CODE (op) == SSA_NAME) |
974 | bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op)); |
975 | } |
976 | can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars, |
977 | mark_nonssa_use, |
978 | mark_nonssa_use, |
979 | mark_nonssa_use); |
980 | } |
981 | /* Record also uses coming from PHI operand in return BB. */ |
982 | FOR_EACH_EDGE (e, ei, bb->succs) |
983 | if (e->dest == return_bb) |
984 | { |
985 | for (gphi_iterator bsi = gsi_start_phis (return_bb); |
986 | !gsi_end_p (i: bsi); |
987 | gsi_next (i: &bsi)) |
988 | { |
989 | gphi *stmt = bsi.phi (); |
990 | tree op = gimple_phi_arg_def (gs: stmt, index: e->dest_idx); |
991 | |
992 | if (virtual_operand_p (op: gimple_phi_result (gs: stmt))) |
993 | continue; |
994 | if (TREE_CODE (op) == SSA_NAME) |
995 | bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op)); |
996 | else |
997 | can_split &= !mark_nonssa_use (stmt, t: op, op, data: non_ssa_vars); |
998 | } |
999 | } |
1000 | return can_split; |
1001 | } |
1002 | |
1003 | /* Stack entry for recursive DFS walk in find_split_point. */ |
1004 | |
1005 | class stack_entry |
1006 | { |
1007 | public: |
1008 | /* Basic block we are examining. */ |
1009 | basic_block bb; |
1010 | |
1011 | /* SSA names set and used by the BB and all BBs reachable |
1012 | from it via DFS walk. */ |
1013 | bitmap set_ssa_names, used_ssa_names; |
1014 | bitmap non_ssa_vars; |
1015 | |
1016 | /* All BBS visited from this BB via DFS walk. */ |
1017 | bitmap bbs_visited; |
1018 | |
1019 | /* Last examined edge in DFS walk. Since we walk unoriented graph, |
1020 | the value is up to sum of incoming and outgoing edges of BB. */ |
1021 | unsigned int edge_num; |
1022 | |
1023 | /* Stack entry index of earliest BB reachable from current BB |
1024 | or any BB visited later in DFS walk. */ |
1025 | int earliest; |
1026 | |
1027 | /* Overall time and size of all BBs reached from this BB in DFS walk. */ |
1028 | sreal overall_time; |
1029 | int overall_size; |
1030 | |
1031 | /* When false we cannot split on this BB. */ |
1032 | bool can_split; |
1033 | }; |
1034 | |
1035 | |
1036 | /* Find all articulations and call consider_split on them. |
1037 | OVERALL_TIME and OVERALL_SIZE is time and size of the function. |
1038 | |
1039 | We perform basic algorithm for finding an articulation in a graph |
1040 | created from CFG by considering it to be an unoriented graph. |
1041 | |
1042 | The articulation is discovered via DFS walk. We collect earliest |
1043 | basic block on stack that is reachable via backward edge. Articulation |
1044 | is any basic block such that there is no backward edge bypassing it. |
1045 | To reduce stack usage we maintain heap allocated stack in STACK vector. |
1046 | AUX pointer of BB is set to index it appears in the stack or -1 once |
1047 | it is visited and popped off the stack. |
1048 | |
1049 | The algorithm finds articulation after visiting the whole component |
1050 | reachable by it. This makes it convenient to collect information about |
1051 | the component used by consider_split. */ |
1052 | |
1053 | static void |
1054 | find_split_points (basic_block return_bb, sreal overall_time, int overall_size) |
1055 | { |
1056 | stack_entry first; |
1057 | vec<stack_entry> stack = vNULL; |
1058 | basic_block bb; |
1059 | class split_point current; |
1060 | |
1061 | current.header_time = overall_time; |
1062 | current.header_size = overall_size; |
1063 | current.split_time = 0; |
1064 | current.split_size = 0; |
1065 | current.ssa_names_to_pass = BITMAP_ALLOC (NULL); |
1066 | |
1067 | first.bb = ENTRY_BLOCK_PTR_FOR_FN (cfun); |
1068 | first.edge_num = 0; |
1069 | first.overall_time = 0; |
1070 | first.overall_size = 0; |
1071 | first.earliest = INT_MAX; |
1072 | first.set_ssa_names = 0; |
1073 | first.used_ssa_names = 0; |
1074 | first.non_ssa_vars = 0; |
1075 | first.bbs_visited = 0; |
1076 | first.can_split = false; |
1077 | stack.safe_push (obj: first); |
1078 | ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = (void *)(intptr_t)-1; |
1079 | |
1080 | while (!stack.is_empty ()) |
1081 | { |
1082 | stack_entry *entry = &stack.last (); |
1083 | |
1084 | /* We are walking an acyclic graph, so edge_num counts |
1085 | succ and pred edges together. However when considering |
1086 | articulation, we want to have processed everything reachable |
1087 | from articulation but nothing that reaches into it. */ |
1088 | if (entry->edge_num == EDGE_COUNT (entry->bb->succs) |
1089 | && entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)) |
1090 | { |
1091 | int pos = stack.length (); |
1092 | entry->can_split &= visit_bb (bb: entry->bb, return_bb, |
1093 | set_ssa_names: entry->set_ssa_names, |
1094 | used_ssa_names: entry->used_ssa_names, |
1095 | non_ssa_vars: entry->non_ssa_vars); |
1096 | if (pos <= entry->earliest && !entry->can_split |
1097 | && dump_file && (dump_flags & TDF_DETAILS)) |
1098 | fprintf (stream: dump_file, |
1099 | format: "found articulation at bb %i but cannot split\n" , |
1100 | entry->bb->index); |
1101 | if (pos <= entry->earliest && entry->can_split) |
1102 | { |
1103 | if (dump_file && (dump_flags & TDF_DETAILS)) |
1104 | fprintf (stream: dump_file, format: "found articulation at bb %i\n" , |
1105 | entry->bb->index); |
1106 | current.entry_bb = entry->bb; |
1107 | current.ssa_names_to_pass = BITMAP_ALLOC (NULL); |
1108 | bitmap_and_compl (current.ssa_names_to_pass, |
1109 | entry->used_ssa_names, entry->set_ssa_names); |
1110 | current.header_time = overall_time - entry->overall_time; |
1111 | current.header_size = overall_size - entry->overall_size; |
1112 | current.split_time = entry->overall_time; |
1113 | current.split_size = entry->overall_size; |
1114 | current.split_bbs = entry->bbs_visited; |
1115 | consider_split (current: ¤t, non_ssa_vars: entry->non_ssa_vars, return_bb); |
1116 | BITMAP_FREE (current.ssa_names_to_pass); |
1117 | } |
1118 | } |
1119 | /* Do actual DFS walk. */ |
1120 | if (entry->edge_num |
1121 | < (EDGE_COUNT (entry->bb->succs) |
1122 | + EDGE_COUNT (entry->bb->preds))) |
1123 | { |
1124 | edge e; |
1125 | basic_block dest; |
1126 | if (entry->edge_num < EDGE_COUNT (entry->bb->succs)) |
1127 | { |
1128 | e = EDGE_SUCC (entry->bb, entry->edge_num); |
1129 | dest = e->dest; |
1130 | } |
1131 | else |
1132 | { |
1133 | e = EDGE_PRED (entry->bb, entry->edge_num |
1134 | - EDGE_COUNT (entry->bb->succs)); |
1135 | dest = e->src; |
1136 | } |
1137 | |
1138 | entry->edge_num++; |
1139 | |
1140 | /* New BB to visit, push it to the stack. */ |
1141 | if (dest != return_bb && dest != EXIT_BLOCK_PTR_FOR_FN (cfun) |
1142 | && !dest->aux) |
1143 | { |
1144 | stack_entry new_entry; |
1145 | |
1146 | new_entry.bb = dest; |
1147 | new_entry.edge_num = 0; |
1148 | new_entry.overall_time |
1149 | = bb_info_vec[dest->index].time; |
1150 | new_entry.overall_size |
1151 | = bb_info_vec[dest->index].size; |
1152 | new_entry.earliest = INT_MAX; |
1153 | new_entry.set_ssa_names = BITMAP_ALLOC (NULL); |
1154 | new_entry.used_ssa_names = BITMAP_ALLOC (NULL); |
1155 | new_entry.bbs_visited = BITMAP_ALLOC (NULL); |
1156 | new_entry.non_ssa_vars = BITMAP_ALLOC (NULL); |
1157 | new_entry.can_split = true; |
1158 | bitmap_set_bit (new_entry.bbs_visited, dest->index); |
1159 | stack.safe_push (obj: new_entry); |
1160 | dest->aux = (void *)(intptr_t)stack.length (); |
1161 | } |
1162 | /* Back edge found, record the earliest point. */ |
1163 | else if ((intptr_t)dest->aux > 0 |
1164 | && (intptr_t)dest->aux < entry->earliest) |
1165 | entry->earliest = (intptr_t)dest->aux; |
1166 | } |
1167 | /* We are done with examining the edges. Pop off the value from stack |
1168 | and merge stuff we accumulate during the walk. */ |
1169 | else if (entry->bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)) |
1170 | { |
1171 | stack_entry *prev = &stack[stack.length () - 2]; |
1172 | |
1173 | entry->bb->aux = (void *)(intptr_t)-1; |
1174 | prev->can_split &= entry->can_split; |
1175 | if (prev->set_ssa_names) |
1176 | { |
1177 | bitmap_ior_into (prev->set_ssa_names, entry->set_ssa_names); |
1178 | bitmap_ior_into (prev->used_ssa_names, entry->used_ssa_names); |
1179 | bitmap_ior_into (prev->bbs_visited, entry->bbs_visited); |
1180 | bitmap_ior_into (prev->non_ssa_vars, entry->non_ssa_vars); |
1181 | } |
1182 | if (prev->earliest > entry->earliest) |
1183 | prev->earliest = entry->earliest; |
1184 | prev->overall_time += entry->overall_time; |
1185 | prev->overall_size += entry->overall_size; |
1186 | BITMAP_FREE (entry->set_ssa_names); |
1187 | BITMAP_FREE (entry->used_ssa_names); |
1188 | BITMAP_FREE (entry->bbs_visited); |
1189 | BITMAP_FREE (entry->non_ssa_vars); |
1190 | stack.pop (); |
1191 | } |
1192 | else |
1193 | stack.pop (); |
1194 | } |
1195 | ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = NULL; |
1196 | FOR_EACH_BB_FN (bb, cfun) |
1197 | bb->aux = NULL; |
1198 | stack.release (); |
1199 | BITMAP_FREE (current.ssa_names_to_pass); |
1200 | } |
1201 | |
1202 | /* Split function at SPLIT_POINT. */ |
1203 | |
1204 | static void |
1205 | split_function (basic_block return_bb, class split_point *split_point, |
1206 | bool add_tsan_func_exit) |
1207 | { |
1208 | vec<tree> args_to_pass = vNULL; |
1209 | bitmap args_to_skip; |
1210 | tree parm; |
1211 | int num = 0; |
1212 | cgraph_node *node, *cur_node = cgraph_node::get (decl: current_function_decl); |
1213 | basic_block call_bb; |
1214 | gcall *call, *tsan_func_exit_call = NULL; |
1215 | edge e; |
1216 | edge_iterator ei; |
1217 | tree retval = NULL, real_retval = NULL; |
1218 | gimple *last_stmt = NULL; |
1219 | unsigned int i; |
1220 | tree arg, ddef; |
1221 | |
1222 | if (dump_file) |
1223 | { |
1224 | fprintf (stream: dump_file, format: "\n\nSplitting function at:\n" ); |
1225 | dump_split_point (file: dump_file, current: split_point); |
1226 | } |
1227 | |
1228 | if (cur_node->can_change_signature) |
1229 | args_to_skip = BITMAP_ALLOC (NULL); |
1230 | else |
1231 | args_to_skip = NULL; |
1232 | |
1233 | /* Collect the parameters of new function and args_to_skip bitmap. */ |
1234 | for (parm = DECL_ARGUMENTS (current_function_decl); |
1235 | parm; parm = DECL_CHAIN (parm), num++) |
1236 | if (args_to_skip |
1237 | && (!is_gimple_reg (parm) |
1238 | || (ddef = ssa_default_def (cfun, parm)) == NULL_TREE |
1239 | || !bitmap_bit_p (split_point->ssa_names_to_pass, |
1240 | SSA_NAME_VERSION (ddef)))) |
1241 | bitmap_set_bit (args_to_skip, num); |
1242 | else |
1243 | { |
1244 | /* This parm might not have been used up to now, but is going to be |
1245 | used, hence register it. */ |
1246 | if (is_gimple_reg (parm)) |
1247 | arg = get_or_create_ssa_default_def (cfun, parm); |
1248 | else |
1249 | arg = parm; |
1250 | |
1251 | if (!useless_type_conversion_p (DECL_ARG_TYPE (parm), TREE_TYPE (arg))) |
1252 | arg = fold_convert (DECL_ARG_TYPE (parm), arg); |
1253 | args_to_pass.safe_push (obj: arg); |
1254 | } |
1255 | |
1256 | /* See if the split function will return. */ |
1257 | bool split_part_return_p = false; |
1258 | FOR_EACH_EDGE (e, ei, return_bb->preds) |
1259 | { |
1260 | if (bitmap_bit_p (split_point->split_bbs, e->src->index)) |
1261 | split_part_return_p = true; |
1262 | } |
1263 | |
1264 | /* Add return block to what will become the split function. |
1265 | We do not return; no return block is needed. */ |
1266 | if (!split_part_return_p) |
1267 | ; |
1268 | /* We have no return block, so nothing is needed. */ |
1269 | else if (return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)) |
1270 | ; |
1271 | /* When we do not want to return value, we need to construct |
1272 | new return block with empty return statement. |
1273 | FIXME: Once we are able to change return type, we should change function |
1274 | to return void instead of just outputting function with undefined return |
1275 | value. For structures this affects quality of codegen. */ |
1276 | else if ((retval = find_retval (return_bb)) |
1277 | && !split_point->split_part_set_retval) |
1278 | { |
1279 | bool redirected = true; |
1280 | basic_block new_return_bb = create_basic_block (NULL, 0, return_bb); |
1281 | gimple_stmt_iterator gsi = gsi_start_bb (bb: new_return_bb); |
1282 | gsi_insert_after (&gsi, gimple_build_return (NULL), GSI_NEW_STMT); |
1283 | new_return_bb->count = profile_count::zero (); |
1284 | while (redirected) |
1285 | { |
1286 | redirected = false; |
1287 | FOR_EACH_EDGE (e, ei, return_bb->preds) |
1288 | if (bitmap_bit_p (split_point->split_bbs, e->src->index)) |
1289 | { |
1290 | new_return_bb->count += e->count (); |
1291 | redirect_edge_and_branch (e, new_return_bb); |
1292 | redirected = true; |
1293 | break; |
1294 | } |
1295 | } |
1296 | e = make_single_succ_edge (new_return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0); |
1297 | add_bb_to_loop (new_return_bb, current_loops->tree_root); |
1298 | bitmap_set_bit (split_point->split_bbs, new_return_bb->index); |
1299 | } |
1300 | /* When we pass around the value, use existing return block. */ |
1301 | else |
1302 | bitmap_set_bit (split_point->split_bbs, return_bb->index); |
1303 | |
1304 | /* If RETURN_BB has virtual operand PHIs, they must be removed and the |
1305 | virtual operand marked for renaming as we change the CFG in a way that |
1306 | tree-inline is not able to compensate for. |
1307 | |
1308 | Note this can happen whether or not we have a return value. If we have |
1309 | a return value, then RETURN_BB may have PHIs for real operands too. */ |
1310 | if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun)) |
1311 | { |
1312 | bool phi_p = false; |
1313 | for (gphi_iterator gsi = gsi_start_phis (return_bb); |
1314 | !gsi_end_p (i: gsi);) |
1315 | { |
1316 | gphi *stmt = gsi.phi (); |
1317 | if (!virtual_operand_p (op: gimple_phi_result (gs: stmt))) |
1318 | { |
1319 | gsi_next (i: &gsi); |
1320 | continue; |
1321 | } |
1322 | mark_virtual_phi_result_for_renaming (stmt); |
1323 | remove_phi_node (&gsi, true); |
1324 | phi_p = true; |
1325 | } |
1326 | /* In reality we have to rename the reaching definition of the |
1327 | virtual operand at return_bb as we will eventually release it |
1328 | when we remove the code region we outlined. |
1329 | So we have to rename all immediate virtual uses of that region |
1330 | if we didn't see a PHI definition yet. */ |
1331 | /* ??? In real reality we want to set the reaching vdef of the |
1332 | entry of the SESE region as the vuse of the call and the reaching |
1333 | vdef of the exit of the SESE region as the vdef of the call. */ |
1334 | if (!phi_p) |
1335 | for (gimple_stmt_iterator gsi = gsi_start_bb (bb: return_bb); |
1336 | !gsi_end_p (i: gsi); |
1337 | gsi_next (i: &gsi)) |
1338 | { |
1339 | gimple *stmt = gsi_stmt (i: gsi); |
1340 | if (gimple_vuse (g: stmt)) |
1341 | { |
1342 | gimple_set_vuse (g: stmt, NULL_TREE); |
1343 | update_stmt (s: stmt); |
1344 | } |
1345 | if (gimple_vdef (g: stmt)) |
1346 | break; |
1347 | } |
1348 | } |
1349 | |
1350 | ipa_param_adjustments *adjustments; |
1351 | bool skip_return = (!split_part_return_p |
1352 | || !split_point->split_part_set_retval); |
1353 | /* TODO: Perhaps get rid of args_to_skip entirely, after we make sure the |
1354 | debug info generation and discrepancy avoiding works well too. */ |
1355 | if ((args_to_skip && !bitmap_empty_p (map: args_to_skip)) |
1356 | || skip_return) |
1357 | { |
1358 | vec<ipa_adjusted_param, va_gc> *new_params = NULL; |
1359 | unsigned j; |
1360 | for (parm = DECL_ARGUMENTS (current_function_decl), j = 0; |
1361 | parm; parm = DECL_CHAIN (parm), j++) |
1362 | if (!args_to_skip || !bitmap_bit_p (args_to_skip, j)) |
1363 | { |
1364 | ipa_adjusted_param adj; |
1365 | memset (s: &adj, c: 0, n: sizeof (adj)); |
1366 | adj.op = IPA_PARAM_OP_COPY; |
1367 | adj.base_index = j; |
1368 | adj.prev_clone_index = j; |
1369 | vec_safe_push (v&: new_params, obj: adj); |
1370 | } |
1371 | adjustments = new ipa_param_adjustments (new_params, j, skip_return); |
1372 | } |
1373 | else |
1374 | adjustments = NULL; |
1375 | |
1376 | /* Now create the actual clone. */ |
1377 | cgraph_edge::rebuild_edges (); |
1378 | node = cur_node->create_version_clone_with_body |
1379 | (redirect_callers: vNULL, NULL, param_adjustments: adjustments, |
1380 | bbs_to_copy: split_point->split_bbs, new_entry_block: split_point->entry_bb, clone_name: "part" ); |
1381 | delete adjustments; |
1382 | node->split_part = true; |
1383 | |
1384 | if (cur_node->same_comdat_group) |
1385 | { |
1386 | /* TODO: call is versionable if we make sure that all |
1387 | callers are inside of a comdat group. */ |
1388 | cur_node->calls_comdat_local = true; |
1389 | node->add_to_same_comdat_group (old_node: cur_node); |
1390 | } |
1391 | |
1392 | |
1393 | /* Let's take a time profile for splitted function. */ |
1394 | if (cur_node->tp_first_run) |
1395 | node->tp_first_run = cur_node->tp_first_run + 1; |
1396 | |
1397 | /* For usual cloning it is enough to clear builtin only when signature |
1398 | changes. For partial inlining we however cannot expect the part |
1399 | of builtin implementation to have same semantic as the whole. */ |
1400 | if (fndecl_built_in_p (node: node->decl)) |
1401 | set_decl_built_in_function (decl: node->decl, fclass: NOT_BUILT_IN, fcode: 0); |
1402 | |
1403 | /* If return_bb contains any clobbers that refer to SSA_NAMEs |
1404 | set in the split part, remove them. Also reset debug stmts that |
1405 | refer to SSA_NAMEs set in the split part. */ |
1406 | if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun)) |
1407 | { |
1408 | gimple_stmt_iterator gsi = gsi_start_bb (bb: return_bb); |
1409 | while (!gsi_end_p (i: gsi)) |
1410 | { |
1411 | tree op; |
1412 | ssa_op_iter iter; |
1413 | gimple *stmt = gsi_stmt (i: gsi); |
1414 | bool remove = false; |
1415 | if (gimple_clobber_p (s: stmt) || is_gimple_debug (gs: stmt)) |
1416 | FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE) |
1417 | { |
1418 | basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (op)); |
1419 | if (op != retval |
1420 | && bb |
1421 | && bb != return_bb |
1422 | && bitmap_bit_p (split_point->split_bbs, bb->index)) |
1423 | { |
1424 | if (is_gimple_debug (gs: stmt)) |
1425 | { |
1426 | gimple_debug_bind_reset_value (dbg: stmt); |
1427 | update_stmt (s: stmt); |
1428 | } |
1429 | else |
1430 | remove = true; |
1431 | break; |
1432 | } |
1433 | } |
1434 | if (remove) |
1435 | gsi_remove (&gsi, true); |
1436 | else |
1437 | gsi_next (i: &gsi); |
1438 | } |
1439 | } |
1440 | |
1441 | /* If the original function is declared inline, there is no point in issuing |
1442 | a warning for the non-inlinable part. */ |
1443 | DECL_NO_INLINE_WARNING_P (node->decl) = 1; |
1444 | cur_node->remove_callees (); |
1445 | cur_node->remove_all_references (); |
1446 | if (!split_part_return_p) |
1447 | TREE_THIS_VOLATILE (node->decl) = 1; |
1448 | if (dump_file) |
1449 | dump_function_to_file (node->decl, dump_file, dump_flags); |
1450 | |
1451 | /* Create the basic block we place call into. It is the entry basic block |
1452 | split after last label. */ |
1453 | call_bb = split_point->entry_bb; |
1454 | for (gimple_stmt_iterator gsi = gsi_start_bb (bb: call_bb); !gsi_end_p (i: gsi);) |
1455 | if (gimple_code (g: gsi_stmt (i: gsi)) == GIMPLE_LABEL) |
1456 | { |
1457 | last_stmt = gsi_stmt (i: gsi); |
1458 | gsi_next (i: &gsi); |
1459 | } |
1460 | else |
1461 | break; |
1462 | call_bb->count = split_point->count; |
1463 | e = split_block (split_point->entry_bb, last_stmt); |
1464 | remove_edge (e); |
1465 | |
1466 | /* Produce the call statement. */ |
1467 | gimple_stmt_iterator gsi = gsi_last_bb (bb: call_bb); |
1468 | FOR_EACH_VEC_ELT (args_to_pass, i, arg) |
1469 | if (!is_gimple_val (arg)) |
1470 | { |
1471 | arg = force_gimple_operand_gsi (&gsi, arg, true, NULL_TREE, |
1472 | false, GSI_CONTINUE_LINKING); |
1473 | args_to_pass[i] = arg; |
1474 | } |
1475 | call = gimple_build_call_vec (node->decl, args_to_pass); |
1476 | gimple_set_block (g: call, DECL_INITIAL (current_function_decl)); |
1477 | args_to_pass.release (); |
1478 | |
1479 | /* For optimized away parameters, add on the caller side |
1480 | before the call |
1481 | DEBUG D#X => parm_Y(D) |
1482 | stmts and associate D#X with parm in decl_debug_args_lookup |
1483 | vector to say for debug info that if parameter parm had been passed, |
1484 | it would have value parm_Y(D). */ |
1485 | if (args_to_skip) |
1486 | { |
1487 | vec<tree, va_gc> **debug_args = NULL; |
1488 | unsigned i = 0, len = 0; |
1489 | if (MAY_HAVE_DEBUG_BIND_STMTS) |
1490 | { |
1491 | debug_args = decl_debug_args_lookup (node->decl); |
1492 | if (debug_args) |
1493 | len = vec_safe_length (v: *debug_args); |
1494 | } |
1495 | for (parm = DECL_ARGUMENTS (current_function_decl), num = 0; |
1496 | parm; parm = DECL_CHAIN (parm), num++) |
1497 | if (bitmap_bit_p (args_to_skip, num) && is_gimple_reg (parm)) |
1498 | { |
1499 | tree ddecl; |
1500 | gimple *def_temp; |
1501 | |
1502 | /* This needs to be done even without |
1503 | MAY_HAVE_DEBUG_BIND_STMTS, otherwise if it didn't exist |
1504 | before, we'd end up with different SSA_NAME_VERSIONs |
1505 | between -g and -g0. */ |
1506 | arg = get_or_create_ssa_default_def (cfun, parm); |
1507 | if (!MAY_HAVE_DEBUG_BIND_STMTS || debug_args == NULL) |
1508 | continue; |
1509 | |
1510 | while (i < len && (**debug_args)[i] != DECL_ORIGIN (parm)) |
1511 | i += 2; |
1512 | if (i >= len) |
1513 | continue; |
1514 | ddecl = (**debug_args)[i + 1]; |
1515 | def_temp |
1516 | = gimple_build_debug_bind (ddecl, unshare_expr (arg), call); |
1517 | gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT); |
1518 | } |
1519 | BITMAP_FREE (args_to_skip); |
1520 | } |
1521 | |
1522 | /* We avoid address being taken on any variable used by split part, |
1523 | so return slot optimization is always possible. Moreover this is |
1524 | required to make DECL_BY_REFERENCE work. */ |
1525 | if (aggregate_value_p (DECL_RESULT (current_function_decl), |
1526 | TREE_TYPE (current_function_decl)) |
1527 | && (!is_gimple_reg_type (TREE_TYPE (DECL_RESULT (current_function_decl))) |
1528 | || DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))) |
1529 | gimple_call_set_return_slot_opt (s: call, return_slot_opt_p: true); |
1530 | |
1531 | if (add_tsan_func_exit) |
1532 | tsan_func_exit_call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0); |
1533 | |
1534 | /* Update return value. This is bit tricky. When we do not return, |
1535 | do nothing. When we return we might need to update return_bb |
1536 | or produce a new return statement. */ |
1537 | if (!split_part_return_p) |
1538 | { |
1539 | gsi_insert_after (&gsi, call, GSI_NEW_STMT); |
1540 | if (tsan_func_exit_call) |
1541 | gsi_insert_after (&gsi, tsan_func_exit_call, GSI_NEW_STMT); |
1542 | } |
1543 | else |
1544 | { |
1545 | e = make_single_succ_edge (call_bb, return_bb, |
1546 | return_bb == EXIT_BLOCK_PTR_FOR_FN (cfun) |
1547 | ? 0 : EDGE_FALLTHRU); |
1548 | |
1549 | /* If there is return basic block, see what value we need to store |
1550 | return value into and put call just before it. */ |
1551 | if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun)) |
1552 | { |
1553 | real_retval = retval; |
1554 | if (real_retval && split_point->split_part_set_retval) |
1555 | { |
1556 | gphi_iterator psi; |
1557 | |
1558 | /* See if we need new SSA_NAME for the result. |
1559 | When DECL_BY_REFERENCE is true, retval is actually pointer to |
1560 | return value and it is constant in whole function. */ |
1561 | if (TREE_CODE (retval) == SSA_NAME |
1562 | && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))) |
1563 | { |
1564 | retval = copy_ssa_name (var: retval, stmt: call); |
1565 | |
1566 | /* See if there is PHI defining return value. */ |
1567 | for (psi = gsi_start_phis (return_bb); |
1568 | !gsi_end_p (i: psi); gsi_next (i: &psi)) |
1569 | if (!virtual_operand_p (op: gimple_phi_result (gs: psi.phi ()))) |
1570 | break; |
1571 | |
1572 | /* When there is PHI, just update its value. */ |
1573 | if (TREE_CODE (retval) == SSA_NAME |
1574 | && !gsi_end_p (i: psi)) |
1575 | add_phi_arg (psi.phi (), retval, e, UNKNOWN_LOCATION); |
1576 | /* Otherwise update the return BB itself. |
1577 | find_return_bb allows at most one assignment to return value, |
1578 | so update first statement. */ |
1579 | else |
1580 | { |
1581 | gimple_stmt_iterator bsi; |
1582 | for (bsi = gsi_start_bb (bb: return_bb); !gsi_end_p (i: bsi); |
1583 | gsi_next (i: &bsi)) |
1584 | if (greturn *return_stmt |
1585 | = dyn_cast <greturn *> (p: gsi_stmt (i: bsi))) |
1586 | { |
1587 | gimple_return_set_retval (gs: return_stmt, retval); |
1588 | break; |
1589 | } |
1590 | else if (gimple_code (g: gsi_stmt (i: bsi)) == GIMPLE_ASSIGN |
1591 | && !gimple_clobber_p (s: gsi_stmt (i: bsi))) |
1592 | { |
1593 | gimple_assign_set_rhs1 (gs: gsi_stmt (i: bsi), rhs: retval); |
1594 | break; |
1595 | } |
1596 | update_stmt (s: gsi_stmt (i: bsi)); |
1597 | /* Also adjust clobbers and debug stmts in return_bb. */ |
1598 | for (bsi = gsi_start_bb (bb: return_bb); !gsi_end_p (i: bsi); |
1599 | gsi_next (i: &bsi)) |
1600 | { |
1601 | gimple *stmt = gsi_stmt (i: bsi); |
1602 | if (gimple_clobber_p (s: stmt) |
1603 | || is_gimple_debug (gs: stmt)) |
1604 | { |
1605 | ssa_op_iter iter; |
1606 | use_operand_p use_p; |
1607 | bool update = false; |
1608 | FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, |
1609 | SSA_OP_USE) |
1610 | if (USE_FROM_PTR (use_p) == real_retval) |
1611 | { |
1612 | SET_USE (use_p, retval); |
1613 | update = true; |
1614 | } |
1615 | if (update) |
1616 | update_stmt (s: stmt); |
1617 | } |
1618 | } |
1619 | } |
1620 | } |
1621 | if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))) |
1622 | { |
1623 | gimple_call_set_lhs (gs: call, build_simple_mem_ref (retval)); |
1624 | gsi_insert_after (&gsi, call, GSI_NEW_STMT); |
1625 | } |
1626 | else |
1627 | { |
1628 | tree restype; |
1629 | restype = TREE_TYPE (DECL_RESULT (current_function_decl)); |
1630 | gsi_insert_after (&gsi, call, GSI_NEW_STMT); |
1631 | if (!useless_type_conversion_p (TREE_TYPE (retval), restype)) |
1632 | { |
1633 | gimple *cpy; |
1634 | tree tem = create_tmp_reg (restype); |
1635 | tem = make_ssa_name (var: tem, stmt: call); |
1636 | cpy = gimple_build_assign (retval, NOP_EXPR, tem); |
1637 | gsi_insert_after (&gsi, cpy, GSI_NEW_STMT); |
1638 | retval = tem; |
1639 | } |
1640 | gimple_call_set_lhs (gs: call, lhs: retval); |
1641 | update_stmt (s: call); |
1642 | } |
1643 | } |
1644 | else |
1645 | gsi_insert_after (&gsi, call, GSI_NEW_STMT); |
1646 | if (tsan_func_exit_call) |
1647 | gsi_insert_after (&gsi, tsan_func_exit_call, GSI_NEW_STMT); |
1648 | } |
1649 | /* We don't use return block (there is either no return in function or |
1650 | multiple of them). So create new basic block with return statement. |
1651 | */ |
1652 | else |
1653 | { |
1654 | greturn *ret; |
1655 | if (split_point->split_part_set_retval |
1656 | && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))) |
1657 | { |
1658 | retval = DECL_RESULT (current_function_decl); |
1659 | |
1660 | /* We use temporary register to hold value when aggregate_value_p |
1661 | is false. Similarly for DECL_BY_REFERENCE we must avoid extra |
1662 | copy. */ |
1663 | if (!aggregate_value_p (retval, TREE_TYPE (current_function_decl)) |
1664 | && !DECL_BY_REFERENCE (retval)) |
1665 | retval = create_tmp_reg (TREE_TYPE (retval)); |
1666 | if (is_gimple_reg (retval)) |
1667 | { |
1668 | /* When returning by reference, there is only one SSA name |
1669 | assigned to RESULT_DECL (that is pointer to return value). |
1670 | Look it up or create new one if it is missing. */ |
1671 | if (DECL_BY_REFERENCE (retval)) |
1672 | retval = get_or_create_ssa_default_def (cfun, retval); |
1673 | /* Otherwise produce new SSA name for return value. */ |
1674 | else |
1675 | retval = make_ssa_name (var: retval, stmt: call); |
1676 | } |
1677 | if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))) |
1678 | gimple_call_set_lhs (gs: call, build_simple_mem_ref (retval)); |
1679 | else |
1680 | gimple_call_set_lhs (gs: call, lhs: retval); |
1681 | gsi_insert_after (&gsi, call, GSI_NEW_STMT); |
1682 | } |
1683 | else |
1684 | { |
1685 | gsi_insert_after (&gsi, call, GSI_NEW_STMT); |
1686 | if (retval |
1687 | && is_gimple_reg_type (TREE_TYPE (retval)) |
1688 | && !is_gimple_val (retval)) |
1689 | { |
1690 | gassign *g |
1691 | = gimple_build_assign (make_ssa_name (TREE_TYPE (retval)), |
1692 | retval); |
1693 | retval = gimple_assign_lhs (gs: g); |
1694 | gsi_insert_after (&gsi, g, GSI_NEW_STMT); |
1695 | } |
1696 | } |
1697 | if (tsan_func_exit_call) |
1698 | gsi_insert_after (&gsi, tsan_func_exit_call, GSI_NEW_STMT); |
1699 | ret = gimple_build_return (retval); |
1700 | gsi_insert_after (&gsi, ret, GSI_NEW_STMT); |
1701 | } |
1702 | } |
1703 | free_dominance_info (CDI_DOMINATORS); |
1704 | free_dominance_info (CDI_POST_DOMINATORS); |
1705 | compute_fn_summary (node, true); |
1706 | } |
1707 | |
1708 | /* Execute function splitting pass. */ |
1709 | |
1710 | static unsigned int |
1711 | execute_split_functions (void) |
1712 | { |
1713 | gimple_stmt_iterator bsi; |
1714 | basic_block bb; |
1715 | sreal overall_time = 0; |
1716 | int overall_size = 0; |
1717 | int todo = 0; |
1718 | struct cgraph_node *node = cgraph_node::get (decl: current_function_decl); |
1719 | |
1720 | if (flags_from_decl_or_type (current_function_decl) |
1721 | & (ECF_NORETURN|ECF_MALLOC|ECF_RETURNS_TWICE)) |
1722 | { |
1723 | if (dump_file) |
1724 | fprintf (stream: dump_file, format: "Not splitting: noreturn/malloc/returns_twice " |
1725 | "function.\n" ); |
1726 | return 0; |
1727 | } |
1728 | if (MAIN_NAME_P (DECL_NAME (current_function_decl))) |
1729 | { |
1730 | if (dump_file) |
1731 | fprintf (stream: dump_file, format: "Not splitting: main function.\n" ); |
1732 | return 0; |
1733 | } |
1734 | if (node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED) |
1735 | { |
1736 | if (dump_file) |
1737 | fprintf (stream: dump_file, format: "Not splitting: function is unlikely executed.\n" ); |
1738 | return 0; |
1739 | } |
1740 | /* This can be relaxed; function might become inlinable after splitting |
1741 | away the uninlinable part. */ |
1742 | if (ipa_fn_summaries |
1743 | && ipa_fn_summaries->get (node) |
1744 | && !ipa_fn_summaries->get (node)->inlinable) |
1745 | { |
1746 | if (dump_file) |
1747 | fprintf (stream: dump_file, format: "Not splitting: not inlinable.\n" ); |
1748 | return 0; |
1749 | } |
1750 | if (DECL_DISREGARD_INLINE_LIMITS (node->decl)) |
1751 | { |
1752 | if (dump_file) |
1753 | fprintf (stream: dump_file, format: "Not splitting: disregarding inline limits.\n" ); |
1754 | return 0; |
1755 | } |
1756 | /* This can be relaxed; most of versioning tests actually prevents |
1757 | a duplication. */ |
1758 | if (!tree_versionable_function_p (current_function_decl)) |
1759 | { |
1760 | if (dump_file) |
1761 | fprintf (stream: dump_file, format: "Not splitting: not versionable.\n" ); |
1762 | return 0; |
1763 | } |
1764 | /* FIXME: we could support this. */ |
1765 | if (DECL_STRUCT_FUNCTION (current_function_decl)->static_chain_decl) |
1766 | { |
1767 | if (dump_file) |
1768 | fprintf (stream: dump_file, format: "Not splitting: nested function.\n" ); |
1769 | return 0; |
1770 | } |
1771 | |
1772 | /* See if it makes sense to try to split. |
1773 | It makes sense to split if we inline, that is if we have direct calls to |
1774 | handle or direct calls are possibly going to appear as result of indirect |
1775 | inlining or LTO. Also handle -fprofile-generate as LTO to allow non-LTO |
1776 | training for LTO -fprofile-use build. |
1777 | |
1778 | Note that we are not completely conservative about disqualifying functions |
1779 | called once. It is possible that the caller is called more then once and |
1780 | then inlining would still benefit. */ |
1781 | if ((!node->callers |
1782 | /* Local functions called once will be completely inlined most of time. */ |
1783 | || (!node->callers->next_caller && node->local)) |
1784 | && !node->address_taken |
1785 | && !node->has_aliases_p () |
1786 | && (!flag_lto || !node->externally_visible)) |
1787 | { |
1788 | if (dump_file) |
1789 | fprintf (stream: dump_file, format: "Not splitting: not called directly " |
1790 | "or called once.\n" ); |
1791 | return 0; |
1792 | } |
1793 | |
1794 | /* FIXME: We can actually split if splitting reduces call overhead. */ |
1795 | if (!flag_inline_small_functions |
1796 | && !DECL_DECLARED_INLINE_P (current_function_decl)) |
1797 | { |
1798 | if (dump_file) |
1799 | fprintf (stream: dump_file, format: "Not splitting: not autoinlining and function" |
1800 | " is not inline.\n" ); |
1801 | return 0; |
1802 | } |
1803 | |
1804 | if (lookup_attribute (attr_name: "noinline" , DECL_ATTRIBUTES (current_function_decl))) |
1805 | { |
1806 | if (dump_file) |
1807 | fprintf (stream: dump_file, format: "Not splitting: function is noinline.\n" ); |
1808 | return 0; |
1809 | } |
1810 | if (lookup_attribute (attr_name: "section" , DECL_ATTRIBUTES (current_function_decl))) |
1811 | { |
1812 | if (dump_file) |
1813 | fprintf (stream: dump_file, format: "Not splitting: function is in user defined " |
1814 | "section.\n" ); |
1815 | return 0; |
1816 | } |
1817 | if (!strub_splittable_p (node)) |
1818 | { |
1819 | if (dump_file) |
1820 | fprintf (stream: dump_file, format: "Not splitting: function is a strub context.\n" ); |
1821 | return 0; |
1822 | } |
1823 | |
1824 | /* We enforce splitting after loop headers when profile info is not |
1825 | available. */ |
1826 | if (profile_status_for_fn (cfun) != PROFILE_READ) |
1827 | mark_dfs_back_edges (); |
1828 | |
1829 | /* Initialize bitmap to track forbidden calls. */ |
1830 | forbidden_dominators = BITMAP_ALLOC (NULL); |
1831 | calculate_dominance_info (CDI_DOMINATORS); |
1832 | |
1833 | /* Compute local info about basic blocks and determine function size/time. */ |
1834 | bb_info_vec.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1, exact: true); |
1835 | best_split_point.split_bbs = NULL; |
1836 | basic_block return_bb = find_return_bb (); |
1837 | int tsan_exit_found = -1; |
1838 | FOR_EACH_BB_FN (bb, cfun) |
1839 | { |
1840 | sreal time = 0; |
1841 | int size = 0; |
1842 | sreal freq = bb->count.to_sreal_scale |
1843 | (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count); |
1844 | |
1845 | if (dump_file && (dump_flags & TDF_DETAILS)) |
1846 | fprintf (stream: dump_file, format: "Basic block %i\n" , bb->index); |
1847 | |
1848 | for (bsi = gsi_start_bb (bb); !gsi_end_p (i: bsi); gsi_next (i: &bsi)) |
1849 | { |
1850 | sreal this_time; |
1851 | int this_size; |
1852 | gimple *stmt = gsi_stmt (i: bsi); |
1853 | |
1854 | this_size = estimate_num_insns (stmt, &eni_size_weights); |
1855 | this_time = (sreal)estimate_num_insns (stmt, &eni_time_weights) |
1856 | * freq; |
1857 | size += this_size; |
1858 | time += this_time; |
1859 | check_forbidden_calls (stmt); |
1860 | |
1861 | if (dump_file && (dump_flags & TDF_DETAILS)) |
1862 | { |
1863 | fprintf (stream: dump_file, format: " freq:%4.2f size:%3i time:%4.2f " , |
1864 | freq.to_double (), this_size, this_time.to_double ()); |
1865 | print_gimple_stmt (dump_file, stmt, 0); |
1866 | } |
1867 | |
1868 | if ((flag_sanitize & SANITIZE_THREAD) |
1869 | && gimple_call_internal_p (gs: stmt, fn: IFN_TSAN_FUNC_EXIT)) |
1870 | { |
1871 | /* We handle TSAN_FUNC_EXIT for splitting either in the |
1872 | return_bb, or in its immediate predecessors. */ |
1873 | if ((bb != return_bb && !find_edge (bb, return_bb)) |
1874 | || (tsan_exit_found != -1 |
1875 | && tsan_exit_found != (bb != return_bb))) |
1876 | { |
1877 | if (dump_file) |
1878 | fprintf (stream: dump_file, format: "Not splitting: TSAN_FUNC_EXIT" |
1879 | " in unexpected basic block.\n" ); |
1880 | BITMAP_FREE (forbidden_dominators); |
1881 | bb_info_vec.release (); |
1882 | return 0; |
1883 | } |
1884 | tsan_exit_found = bb != return_bb; |
1885 | } |
1886 | } |
1887 | overall_time += time; |
1888 | overall_size += size; |
1889 | bb_info_vec[bb->index].time = time; |
1890 | bb_info_vec[bb->index].size = size; |
1891 | } |
1892 | find_split_points (return_bb, overall_time, overall_size); |
1893 | if (best_split_point.split_bbs) |
1894 | { |
1895 | split_function (return_bb, split_point: &best_split_point, add_tsan_func_exit: tsan_exit_found == 1); |
1896 | BITMAP_FREE (best_split_point.ssa_names_to_pass); |
1897 | BITMAP_FREE (best_split_point.split_bbs); |
1898 | todo = TODO_update_ssa | TODO_cleanup_cfg; |
1899 | } |
1900 | BITMAP_FREE (forbidden_dominators); |
1901 | bb_info_vec.release (); |
1902 | return todo; |
1903 | } |
1904 | |
1905 | namespace { |
1906 | |
1907 | const pass_data pass_data_split_functions = |
1908 | { |
1909 | .type: GIMPLE_PASS, /* type */ |
1910 | .name: "fnsplit" , /* name */ |
1911 | .optinfo_flags: OPTGROUP_NONE, /* optinfo_flags */ |
1912 | .tv_id: TV_IPA_FNSPLIT, /* tv_id */ |
1913 | PROP_cfg, /* properties_required */ |
1914 | .properties_provided: 0, /* properties_provided */ |
1915 | .properties_destroyed: 0, /* properties_destroyed */ |
1916 | .todo_flags_start: 0, /* todo_flags_start */ |
1917 | .todo_flags_finish: 0, /* todo_flags_finish */ |
1918 | }; |
1919 | |
1920 | class pass_split_functions : public gimple_opt_pass |
1921 | { |
1922 | public: |
1923 | pass_split_functions (gcc::context *ctxt) |
1924 | : gimple_opt_pass (pass_data_split_functions, ctxt) |
1925 | {} |
1926 | |
1927 | /* opt_pass methods: */ |
1928 | bool gate (function *) final override; |
1929 | unsigned int execute (function *) final override |
1930 | { |
1931 | return execute_split_functions (); |
1932 | } |
1933 | |
1934 | }; // class pass_split_functions |
1935 | |
1936 | bool |
1937 | pass_split_functions::gate (function *) |
1938 | { |
1939 | /* When doing profile feedback, we want to execute the pass after profiling |
1940 | is read. So disable one in early optimization. */ |
1941 | return (flag_partial_inlining |
1942 | && !profile_arc_flag && !flag_branch_probabilities); |
1943 | } |
1944 | |
1945 | } // anon namespace |
1946 | |
1947 | gimple_opt_pass * |
1948 | make_pass_split_functions (gcc::context *ctxt) |
1949 | { |
1950 | return new pass_split_functions (ctxt); |
1951 | } |
1952 | |
1953 | /* Execute function splitting pass. */ |
1954 | |
1955 | static unsigned int |
1956 | execute_feedback_split_functions (void) |
1957 | { |
1958 | unsigned int retval = execute_split_functions (); |
1959 | if (retval) |
1960 | retval |= TODO_rebuild_cgraph_edges; |
1961 | return retval; |
1962 | } |
1963 | |
1964 | namespace { |
1965 | |
1966 | const pass_data pass_data_feedback_split_functions = |
1967 | { |
1968 | .type: GIMPLE_PASS, /* type */ |
1969 | .name: "feedback_fnsplit" , /* name */ |
1970 | .optinfo_flags: OPTGROUP_NONE, /* optinfo_flags */ |
1971 | .tv_id: TV_IPA_FNSPLIT, /* tv_id */ |
1972 | PROP_cfg, /* properties_required */ |
1973 | .properties_provided: 0, /* properties_provided */ |
1974 | .properties_destroyed: 0, /* properties_destroyed */ |
1975 | .todo_flags_start: 0, /* todo_flags_start */ |
1976 | .todo_flags_finish: 0, /* todo_flags_finish */ |
1977 | }; |
1978 | |
1979 | class pass_feedback_split_functions : public gimple_opt_pass |
1980 | { |
1981 | public: |
1982 | pass_feedback_split_functions (gcc::context *ctxt) |
1983 | : gimple_opt_pass (pass_data_feedback_split_functions, ctxt) |
1984 | {} |
1985 | |
1986 | /* opt_pass methods: */ |
1987 | bool gate (function *) final override; |
1988 | unsigned int execute (function *) final override |
1989 | { |
1990 | return execute_feedback_split_functions (); |
1991 | } |
1992 | |
1993 | }; // class pass_feedback_split_functions |
1994 | |
1995 | bool |
1996 | pass_feedback_split_functions::gate (function *) |
1997 | { |
1998 | /* We don't need to split when profiling at all, we are producing |
1999 | lousy code anyway. */ |
2000 | return (flag_partial_inlining |
2001 | && flag_branch_probabilities); |
2002 | } |
2003 | |
2004 | } // anon namespace |
2005 | |
2006 | gimple_opt_pass * |
2007 | make_pass_feedback_split_functions (gcc::context *ctxt) |
2008 | { |
2009 | return new pass_feedback_split_functions (ctxt); |
2010 | } |
2011 | |