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