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