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