]>
Commit | Line | Data |
---|---|---|
ec1e9f7c | 1 | /* Code sinking for trees |
23a5b65a | 2 | Copyright (C) 2001-2014 Free Software Foundation, Inc. |
ec1e9f7c DB |
3 | Contributed by Daniel Berlin <dan@dberlin.org> |
4 | ||
5 | This file is part of GCC. | |
6 | ||
7 | GCC is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9dcd6f09 | 9 | the Free Software Foundation; either version 3, or (at your option) |
ec1e9f7c DB |
10 | any later version. |
11 | ||
12 | GCC is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
ec1e9f7c DB |
20 | |
21 | #include "config.h" | |
22 | #include "system.h" | |
23 | #include "coretypes.h" | |
24 | #include "tm.h" | |
ec1e9f7c | 25 | #include "tree.h" |
d8a2d370 | 26 | #include "stor-layout.h" |
ec1e9f7c | 27 | #include "basic-block.h" |
cf835838 | 28 | #include "gimple-pretty-print.h" |
ec1e9f7c | 29 | #include "tree-inline.h" |
2fb9a547 AM |
30 | #include "tree-ssa-alias.h" |
31 | #include "internal-fn.h" | |
32 | #include "gimple-expr.h" | |
33 | #include "is-a.h" | |
726a989a | 34 | #include "gimple.h" |
5be5c238 | 35 | #include "gimple-iterator.h" |
442b4905 AM |
36 | #include "gimple-ssa.h" |
37 | #include "tree-cfg.h" | |
38 | #include "tree-phinodes.h" | |
39 | #include "ssa-iterators.h" | |
ec1e9f7c DB |
40 | #include "hashtab.h" |
41 | #include "tree-iterator.h" | |
ec1e9f7c DB |
42 | #include "alloc-pool.h" |
43 | #include "tree-pass.h" | |
44 | #include "flags.h" | |
ec1e9f7c | 45 | #include "cfgloop.h" |
1cc17820 | 46 | #include "params.h" |
ec1e9f7c DB |
47 | |
48 | /* TODO: | |
49 | 1. Sinking store only using scalar promotion (IE without moving the RHS): | |
50 | ||
51 | *q = p; | |
52 | p = p + 1; | |
53 | if (something) | |
54 | *q = <not p>; | |
55 | else | |
56 | y = *q; | |
57 | ||
b8698a0f | 58 | |
ec1e9f7c DB |
59 | should become |
60 | sinktemp = p; | |
61 | p = p + 1; | |
62 | if (something) | |
63 | *q = <not p>; | |
64 | else | |
65 | { | |
66 | *q = sinktemp; | |
67 | y = *q | |
68 | } | |
69 | Store copy propagation will take care of the store elimination above. | |
b8698a0f | 70 | |
ec1e9f7c DB |
71 | |
72 | 2. Sinking using Partial Dead Code Elimination. */ | |
73 | ||
74 | ||
75 | static struct | |
b8698a0f | 76 | { |
6c6cfbfd | 77 | /* The number of statements sunk down the flowgraph by code sinking. */ |
ec1e9f7c | 78 | int sunk; |
b8698a0f | 79 | |
ec1e9f7c DB |
80 | } sink_stats; |
81 | ||
82 | ||
f652d14b | 83 | /* Given a PHI, and one of its arguments (DEF), find the edge for |
ec1e9f7c DB |
84 | that argument and return it. If the argument occurs twice in the PHI node, |
85 | we return NULL. */ | |
86 | ||
87 | static basic_block | |
726a989a | 88 | find_bb_for_arg (gimple phi, tree def) |
ec1e9f7c | 89 | { |
726a989a | 90 | size_t i; |
ec1e9f7c DB |
91 | bool foundone = false; |
92 | basic_block result = NULL; | |
726a989a | 93 | for (i = 0; i < gimple_phi_num_args (phi); i++) |
ec1e9f7c DB |
94 | if (PHI_ARG_DEF (phi, i) == def) |
95 | { | |
96 | if (foundone) | |
97 | return NULL; | |
98 | foundone = true; | |
726a989a | 99 | result = gimple_phi_arg_edge (phi, i)->src; |
ec1e9f7c DB |
100 | } |
101 | return result; | |
102 | } | |
103 | ||
104 | /* When the first immediate use is in a statement, then return true if all | |
105 | immediate uses in IMM are in the same statement. | |
106 | We could also do the case where the first immediate use is in a phi node, | |
107 | and all the other uses are in phis in the same basic block, but this | |
108 | requires some expensive checking later (you have to make sure no def/vdef | |
109 | in the statement occurs for multiple edges in the various phi nodes it's | |
6c6cfbfd | 110 | used in, so that you only have one place you can sink it to. */ |
ec1e9f7c DB |
111 | |
112 | static bool | |
726a989a | 113 | all_immediate_uses_same_place (gimple stmt) |
ec1e9f7c | 114 | { |
726a989a | 115 | gimple firstuse = NULL; |
f430bae8 AM |
116 | ssa_op_iter op_iter; |
117 | imm_use_iterator imm_iter; | |
118 | use_operand_p use_p; | |
119 | tree var; | |
ec1e9f7c | 120 | |
f430bae8 | 121 | FOR_EACH_SSA_TREE_OPERAND (var, stmt, op_iter, SSA_OP_ALL_DEFS) |
ec1e9f7c | 122 | { |
f430bae8 AM |
123 | FOR_EACH_IMM_USE_FAST (use_p, imm_iter, var) |
124 | { | |
b5b8b0ac AO |
125 | if (is_gimple_debug (USE_STMT (use_p))) |
126 | continue; | |
726a989a | 127 | if (firstuse == NULL) |
f430bae8 AM |
128 | firstuse = USE_STMT (use_p); |
129 | else | |
130 | if (firstuse != USE_STMT (use_p)) | |
131 | return false; | |
132 | } | |
ec1e9f7c | 133 | } |
f430bae8 | 134 | |
ec1e9f7c DB |
135 | return true; |
136 | } | |
137 | ||
ec1e9f7c DB |
138 | /* Find the nearest common dominator of all of the immediate uses in IMM. */ |
139 | ||
140 | static basic_block | |
b5b8b0ac | 141 | nearest_common_dominator_of_uses (gimple stmt, bool *debug_stmts) |
b8698a0f | 142 | { |
ec1e9f7c DB |
143 | bitmap blocks = BITMAP_ALLOC (NULL); |
144 | basic_block commondom; | |
ec1e9f7c DB |
145 | unsigned int j; |
146 | bitmap_iterator bi; | |
f430bae8 AM |
147 | ssa_op_iter op_iter; |
148 | imm_use_iterator imm_iter; | |
149 | use_operand_p use_p; | |
150 | tree var; | |
151 | ||
ec1e9f7c | 152 | bitmap_clear (blocks); |
f430bae8 | 153 | FOR_EACH_SSA_TREE_OPERAND (var, stmt, op_iter, SSA_OP_ALL_DEFS) |
ec1e9f7c | 154 | { |
f430bae8 AM |
155 | FOR_EACH_IMM_USE_FAST (use_p, imm_iter, var) |
156 | { | |
726a989a | 157 | gimple usestmt = USE_STMT (use_p); |
f430bae8 | 158 | basic_block useblock; |
000b62dc | 159 | |
726a989a | 160 | if (gimple_code (usestmt) == GIMPLE_PHI) |
f430bae8 | 161 | { |
55b12f0d | 162 | int idx = PHI_ARG_INDEX_FROM_USE (use_p); |
ab798313 | 163 | |
726a989a | 164 | useblock = gimple_phi_arg_edge (usestmt, idx)->src; |
f430bae8 | 165 | } |
b5b8b0ac AO |
166 | else if (is_gimple_debug (usestmt)) |
167 | { | |
168 | *debug_stmts = true; | |
169 | continue; | |
170 | } | |
f430bae8 | 171 | else |
ec1e9f7c | 172 | { |
726a989a | 173 | useblock = gimple_bb (usestmt); |
000b62dc | 174 | } |
f430bae8 | 175 | |
000b62dc | 176 | /* Short circuit. Nothing dominates the entry block. */ |
fefa31b5 | 177 | if (useblock == ENTRY_BLOCK_PTR_FOR_FN (cfun)) |
000b62dc KH |
178 | { |
179 | BITMAP_FREE (blocks); | |
180 | return NULL; | |
ec1e9f7c | 181 | } |
000b62dc | 182 | bitmap_set_bit (blocks, useblock->index); |
ec1e9f7c | 183 | } |
ec1e9f7c | 184 | } |
06e28de2 | 185 | commondom = BASIC_BLOCK_FOR_FN (cfun, bitmap_first_set_bit (blocks)); |
ec1e9f7c | 186 | EXECUTE_IF_SET_IN_BITMAP (blocks, 0, j, bi) |
b8698a0f | 187 | commondom = nearest_common_dominator (CDI_DOMINATORS, commondom, |
06e28de2 | 188 | BASIC_BLOCK_FOR_FN (cfun, j)); |
ec1e9f7c DB |
189 | BITMAP_FREE (blocks); |
190 | return commondom; | |
191 | } | |
192 | ||
1cc17820 JL |
193 | /* Given EARLY_BB and LATE_BB, two blocks in a path through the dominator |
194 | tree, return the best basic block between them (inclusive) to place | |
195 | statements. | |
196 | ||
197 | We want the most control dependent block in the shallowest loop nest. | |
198 | ||
199 | If the resulting block is in a shallower loop nest, then use it. Else | |
200 | only use the resulting block if it has significantly lower execution | |
201 | frequency than EARLY_BB to avoid gratutious statement movement. We | |
202 | consider statements with VOPS more desirable to move. | |
203 | ||
204 | This pass would obviously benefit from PDO as it utilizes block | |
205 | frequencies. It would also benefit from recomputing frequencies | |
206 | if profile data is not available since frequencies often get out | |
207 | of sync with reality. */ | |
208 | ||
209 | static basic_block | |
210 | select_best_block (basic_block early_bb, | |
211 | basic_block late_bb, | |
212 | gimple stmt) | |
213 | { | |
214 | basic_block best_bb = late_bb; | |
215 | basic_block temp_bb = late_bb; | |
216 | int threshold; | |
217 | ||
218 | while (temp_bb != early_bb) | |
219 | { | |
220 | /* If we've moved into a lower loop nest, then that becomes | |
221 | our best block. */ | |
391886c8 | 222 | if (bb_loop_depth (temp_bb) < bb_loop_depth (best_bb)) |
1cc17820 JL |
223 | best_bb = temp_bb; |
224 | ||
225 | /* Walk up the dominator tree, hopefully we'll find a shallower | |
226 | loop nest. */ | |
227 | temp_bb = get_immediate_dominator (CDI_DOMINATORS, temp_bb); | |
228 | } | |
229 | ||
230 | /* If we found a shallower loop nest, then we always consider that | |
231 | a win. This will always give us the most control dependent block | |
232 | within that loop nest. */ | |
391886c8 | 233 | if (bb_loop_depth (best_bb) < bb_loop_depth (early_bb)) |
1cc17820 JL |
234 | return best_bb; |
235 | ||
236 | /* Get the sinking threshold. If the statement to be moved has memory | |
237 | operands, then increase the threshold by 7% as those are even more | |
238 | profitable to avoid, clamping at 100%. */ | |
239 | threshold = PARAM_VALUE (PARAM_SINK_FREQUENCY_THRESHOLD); | |
240 | if (gimple_vuse (stmt) || gimple_vdef (stmt)) | |
241 | { | |
242 | threshold += 7; | |
243 | if (threshold > 100) | |
244 | threshold = 100; | |
245 | } | |
246 | ||
247 | /* If BEST_BB is at the same nesting level, then require it to have | |
248 | significantly lower execution frequency to avoid gratutious movement. */ | |
391886c8 | 249 | if (bb_loop_depth (best_bb) == bb_loop_depth (early_bb) |
1cc17820 JL |
250 | && best_bb->frequency < (early_bb->frequency * threshold / 100.0)) |
251 | return best_bb; | |
252 | ||
253 | /* No better block found, so return EARLY_BB, which happens to be the | |
254 | statement's original block. */ | |
255 | return early_bb; | |
256 | } | |
257 | ||
b8698a0f | 258 | /* Given a statement (STMT) and the basic block it is currently in (FROMBB), |
ec1e9f7c | 259 | determine the location to sink the statement to, if any. |
726a989a RB |
260 | Returns true if there is such location; in that case, TOGSI points to the |
261 | statement before that STMT should be moved. */ | |
ec1e9f7c | 262 | |
18965703 | 263 | static bool |
726a989a RB |
264 | statement_sink_location (gimple stmt, basic_block frombb, |
265 | gimple_stmt_iterator *togsi) | |
ec1e9f7c | 266 | { |
726a989a | 267 | gimple use; |
f430bae8 | 268 | use_operand_p one_use = NULL_USE_OPERAND_P; |
ec1e9f7c DB |
269 | basic_block sinkbb; |
270 | use_operand_p use_p; | |
271 | def_operand_p def_p; | |
272 | ssa_op_iter iter; | |
f430bae8 AM |
273 | imm_use_iterator imm_iter; |
274 | ||
e106efc7 RG |
275 | /* We only can sink assignments. */ |
276 | if (!is_gimple_assign (stmt)) | |
277 | return false; | |
ec1e9f7c | 278 | |
e106efc7 RG |
279 | /* We only can sink stmts with a single definition. */ |
280 | def_p = single_ssa_def_operand (stmt, SSA_OP_ALL_DEFS); | |
281 | if (def_p == NULL_DEF_OPERAND_P) | |
18965703 | 282 | return false; |
ec1e9f7c | 283 | |
e106efc7 RG |
284 | /* Return if there are no immediate uses of this stmt. */ |
285 | if (has_zero_uses (DEF_FROM_PTR (def_p))) | |
18965703 | 286 | return false; |
ec1e9f7c DB |
287 | |
288 | /* There are a few classes of things we can't or don't move, some because we | |
289 | don't have code to handle it, some because it's not profitable and some | |
b8698a0f L |
290 | because it's not legal. |
291 | ||
ec1e9f7c DB |
292 | We can't sink things that may be global stores, at least not without |
293 | calculating a lot more information, because we may cause it to no longer | |
294 | be seen by an external routine that needs it depending on where it gets | |
b8698a0f L |
295 | moved to. |
296 | ||
ec1e9f7c DB |
297 | We don't want to sink loads from memory. |
298 | ||
299 | We can't sink statements that end basic blocks without splitting the | |
300 | incoming edge for the sink location to place it there. | |
301 | ||
b8698a0f | 302 | We can't sink statements that have volatile operands. |
ec1e9f7c DB |
303 | |
304 | We don't want to sink dead code, so anything with 0 immediate uses is not | |
fc3103e7 JJ |
305 | sunk. |
306 | ||
307 | Don't sink BLKmode assignments if current function has any local explicit | |
308 | register variables, as BLKmode assignments may involve memcpy or memset | |
309 | calls or, on some targets, inline expansion thereof that sometimes need | |
310 | to use specific hard registers. | |
ec1e9f7c DB |
311 | |
312 | */ | |
f47c96aa | 313 | if (stmt_ends_bb_p (stmt) |
726a989a | 314 | || gimple_has_side_effects (stmt) |
726a989a | 315 | || gimple_has_volatile_ops (stmt) |
e106efc7 | 316 | || (gimple_vuse (stmt) && !gimple_vdef (stmt)) |
fc3103e7 JJ |
317 | || (cfun->has_local_explicit_reg_vars |
318 | && TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt))) == BLKmode)) | |
18965703 | 319 | return false; |
b8698a0f | 320 | |
e106efc7 RG |
321 | if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (DEF_FROM_PTR (def_p))) |
322 | return false; | |
b8698a0f | 323 | |
ec1e9f7c DB |
324 | FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES) |
325 | { | |
326 | tree use = USE_FROM_PTR (use_p); | |
327 | if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use)) | |
18965703 | 328 | return false; |
ec1e9f7c | 329 | } |
b8698a0f | 330 | |
e106efc7 RG |
331 | use = NULL; |
332 | ||
333 | /* If stmt is a store the one and only use needs to be the VOP | |
334 | merging PHI node. */ | |
335 | if (gimple_vdef (stmt)) | |
336 | { | |
337 | FOR_EACH_IMM_USE_FAST (use_p, imm_iter, DEF_FROM_PTR (def_p)) | |
338 | { | |
339 | gimple use_stmt = USE_STMT (use_p); | |
340 | ||
341 | /* A killing definition is not a use. */ | |
7ec67e2a RB |
342 | if ((gimple_has_lhs (use_stmt) |
343 | && operand_equal_p (gimple_assign_lhs (stmt), | |
344 | gimple_get_lhs (use_stmt), 0)) | |
345 | || stmt_kills_ref_p (use_stmt, gimple_assign_lhs (stmt))) | |
346 | { | |
347 | /* If use_stmt is or might be a nop assignment then USE_STMT | |
348 | acts as a use as well as definition. */ | |
349 | if (stmt != use_stmt | |
350 | && ref_maybe_used_by_stmt_p (use_stmt, | |
351 | gimple_assign_lhs (stmt))) | |
352 | return false; | |
353 | continue; | |
354 | } | |
e106efc7 RG |
355 | |
356 | if (gimple_code (use_stmt) != GIMPLE_PHI) | |
357 | return false; | |
358 | ||
359 | if (use | |
360 | && use != use_stmt) | |
361 | return false; | |
362 | ||
363 | use = use_stmt; | |
364 | } | |
365 | if (!use) | |
366 | return false; | |
367 | } | |
ec1e9f7c DB |
368 | /* If all the immediate uses are not in the same place, find the nearest |
369 | common dominator of all the immediate uses. For PHI nodes, we have to | |
370 | find the nearest common dominator of all of the predecessor blocks, since | |
371 | that is where insertion would have to take place. */ | |
e106efc7 | 372 | else if (!all_immediate_uses_same_place (stmt)) |
ec1e9f7c | 373 | { |
b5b8b0ac AO |
374 | bool debug_stmts = false; |
375 | basic_block commondom = nearest_common_dominator_of_uses (stmt, | |
376 | &debug_stmts); | |
b8698a0f | 377 | |
ec1e9f7c | 378 | if (commondom == frombb) |
18965703 | 379 | return false; |
ec1e9f7c DB |
380 | |
381 | /* Our common dominator has to be dominated by frombb in order to be a | |
382 | trivially safe place to put this statement, since it has multiple | |
b8698a0f | 383 | uses. */ |
ec1e9f7c | 384 | if (!dominated_by_p (CDI_DOMINATORS, commondom, frombb)) |
18965703 | 385 | return false; |
b8698a0f | 386 | |
1cc17820 | 387 | commondom = select_best_block (frombb, commondom, stmt); |
ec1e9f7c | 388 | |
1cc17820 JL |
389 | if (commondom == frombb) |
390 | return false; | |
b5b8b0ac | 391 | |
726a989a | 392 | *togsi = gsi_after_labels (commondom); |
b5b8b0ac | 393 | |
18965703 | 394 | return true; |
ec1e9f7c | 395 | } |
e106efc7 | 396 | else |
ec1e9f7c | 397 | { |
e106efc7 RG |
398 | FOR_EACH_IMM_USE_FAST (one_use, imm_iter, DEF_FROM_PTR (def_p)) |
399 | { | |
400 | if (is_gimple_debug (USE_STMT (one_use))) | |
401 | continue; | |
402 | break; | |
403 | } | |
404 | use = USE_STMT (one_use); | |
726a989a | 405 | |
e106efc7 RG |
406 | if (gimple_code (use) != GIMPLE_PHI) |
407 | { | |
408 | sinkbb = gimple_bb (use); | |
1cc17820 | 409 | sinkbb = select_best_block (frombb, gimple_bb (use), stmt); |
791b59e3 | 410 | |
1cc17820 | 411 | if (sinkbb == frombb) |
e106efc7 | 412 | return false; |
b5b8b0ac | 413 | |
e106efc7 | 414 | *togsi = gsi_for_stmt (use); |
ec1e9f7c | 415 | |
e106efc7 RG |
416 | return true; |
417 | } | |
418 | } | |
f47c96aa | 419 | |
e106efc7 | 420 | sinkbb = find_bb_for_arg (use, DEF_FROM_PTR (def_p)); |
ec1e9f7c | 421 | |
1cc17820 JL |
422 | /* This can happen if there are multiple uses in a PHI. */ |
423 | if (!sinkbb) | |
18965703 | 424 | return false; |
1cc17820 JL |
425 | |
426 | sinkbb = select_best_block (frombb, sinkbb, stmt); | |
427 | if (!sinkbb || sinkbb == frombb) | |
18965703 ZD |
428 | return false; |
429 | ||
3834917d MM |
430 | /* If the latch block is empty, don't make it non-empty by sinking |
431 | something into it. */ | |
432 | if (sinkbb == frombb->loop_father->latch | |
433 | && empty_block_p (sinkbb)) | |
434 | return false; | |
435 | ||
726a989a | 436 | *togsi = gsi_after_labels (sinkbb); |
ec1e9f7c | 437 | |
18965703 | 438 | return true; |
ec1e9f7c DB |
439 | } |
440 | ||
441 | /* Perform code sinking on BB */ | |
442 | ||
443 | static void | |
444 | sink_code_in_bb (basic_block bb) | |
445 | { | |
446 | basic_block son; | |
726a989a | 447 | gimple_stmt_iterator gsi; |
ec1e9f7c DB |
448 | edge_iterator ei; |
449 | edge e; | |
9a287593 | 450 | bool last = true; |
b8698a0f | 451 | |
ec1e9f7c DB |
452 | /* If this block doesn't dominate anything, there can't be any place to sink |
453 | the statements to. */ | |
454 | if (first_dom_son (CDI_DOMINATORS, bb) == NULL) | |
455 | goto earlyout; | |
456 | ||
457 | /* We can't move things across abnormal edges, so don't try. */ | |
458 | FOR_EACH_EDGE (e, ei, bb->succs) | |
459 | if (e->flags & EDGE_ABNORMAL) | |
460 | goto earlyout; | |
461 | ||
726a989a | 462 | for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi);) |
ec1e9f7c | 463 | { |
b8698a0f | 464 | gimple stmt = gsi_stmt (gsi); |
726a989a | 465 | gimple_stmt_iterator togsi; |
18965703 | 466 | |
726a989a | 467 | if (!statement_sink_location (stmt, bb, &togsi)) |
ec1e9f7c | 468 | { |
726a989a RB |
469 | if (!gsi_end_p (gsi)) |
470 | gsi_prev (&gsi); | |
9a287593 | 471 | last = false; |
ec1e9f7c | 472 | continue; |
b8698a0f | 473 | } |
ec1e9f7c DB |
474 | if (dump_file) |
475 | { | |
476 | fprintf (dump_file, "Sinking "); | |
726a989a | 477 | print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS); |
ec1e9f7c | 478 | fprintf (dump_file, " from bb %d to bb %d\n", |
726a989a | 479 | bb->index, (gsi_bb (togsi))->index); |
ec1e9f7c | 480 | } |
b8698a0f | 481 | |
ef13324e RG |
482 | /* Update virtual operands of statements in the path we |
483 | do not sink to. */ | |
e106efc7 RG |
484 | if (gimple_vdef (stmt)) |
485 | { | |
ef13324e RG |
486 | imm_use_iterator iter; |
487 | use_operand_p use_p; | |
488 | gimple vuse_stmt; | |
489 | ||
490 | FOR_EACH_IMM_USE_STMT (vuse_stmt, iter, gimple_vdef (stmt)) | |
491 | if (gimple_code (vuse_stmt) != GIMPLE_PHI) | |
492 | FOR_EACH_IMM_USE_ON_STMT (use_p, iter) | |
493 | SET_USE (use_p, gimple_vuse (stmt)); | |
e106efc7 RG |
494 | } |
495 | ||
ec1e9f7c DB |
496 | /* If this is the end of the basic block, we need to insert at the end |
497 | of the basic block. */ | |
726a989a RB |
498 | if (gsi_end_p (togsi)) |
499 | gsi_move_to_bb_end (&gsi, gsi_bb (togsi)); | |
ec1e9f7c | 500 | else |
726a989a | 501 | gsi_move_before (&gsi, &togsi); |
ec1e9f7c DB |
502 | |
503 | sink_stats.sunk++; | |
9a287593 AO |
504 | |
505 | /* If we've just removed the last statement of the BB, the | |
726a989a | 506 | gsi_end_p() test below would fail, but gsi_prev() would have |
9a287593 AO |
507 | succeeded, and we want it to succeed. So we keep track of |
508 | whether we're at the last statement and pick up the new last | |
509 | statement. */ | |
510 | if (last) | |
511 | { | |
726a989a | 512 | gsi = gsi_last_bb (bb); |
9a287593 AO |
513 | continue; |
514 | } | |
515 | ||
516 | last = false; | |
726a989a RB |
517 | if (!gsi_end_p (gsi)) |
518 | gsi_prev (&gsi); | |
b8698a0f | 519 | |
ec1e9f7c DB |
520 | } |
521 | earlyout: | |
522 | for (son = first_dom_son (CDI_POST_DOMINATORS, bb); | |
523 | son; | |
524 | son = next_dom_son (CDI_POST_DOMINATORS, son)) | |
525 | { | |
526 | sink_code_in_bb (son); | |
527 | } | |
b8698a0f | 528 | } |
ec1e9f7c DB |
529 | |
530 | /* Perform code sinking. | |
531 | This moves code down the flowgraph when we know it would be | |
532 | profitable to do so, or it wouldn't increase the number of | |
533 | executions of the statement. | |
534 | ||
535 | IE given | |
b8698a0f | 536 | |
ec1e9f7c DB |
537 | a_1 = b + c; |
538 | if (<something>) | |
539 | { | |
540 | } | |
541 | else | |
542 | { | |
543 | foo (&b, &c); | |
544 | a_5 = b + c; | |
545 | } | |
546 | a_6 = PHI (a_5, a_1); | |
547 | USE a_6. | |
548 | ||
549 | we'll transform this into: | |
550 | ||
551 | if (<something>) | |
552 | { | |
553 | a_1 = b + c; | |
554 | } | |
555 | else | |
556 | { | |
557 | foo (&b, &c); | |
558 | a_5 = b + c; | |
559 | } | |
560 | a_6 = PHI (a_5, a_1); | |
561 | USE a_6. | |
562 | ||
563 | Note that this reduces the number of computations of a = b + c to 1 | |
564 | when we take the else edge, instead of 2. | |
565 | */ | |
27a4cd48 DM |
566 | namespace { |
567 | ||
568 | const pass_data pass_data_sink_code = | |
ec1e9f7c | 569 | { |
27a4cd48 DM |
570 | GIMPLE_PASS, /* type */ |
571 | "sink", /* name */ | |
572 | OPTGROUP_NONE, /* optinfo_flags */ | |
27a4cd48 DM |
573 | true, /* has_execute */ |
574 | TV_TREE_SINK, /* tv_id */ | |
91db3537 | 575 | /* PROP_no_crit_edges is ensured by running split_critical_edges in |
be55bfe6 | 576 | pass_data_sink_code::execute (). */ |
91db3537 | 577 | ( PROP_cfg | PROP_ssa ), /* properties_required */ |
27a4cd48 DM |
578 | 0, /* properties_provided */ |
579 | 0, /* properties_destroyed */ | |
580 | 0, /* todo_flags_start */ | |
581 | ( TODO_update_ssa | TODO_verify_ssa | |
582 | | TODO_verify_flow ), /* todo_flags_finish */ | |
ec1e9f7c | 583 | }; |
27a4cd48 DM |
584 | |
585 | class pass_sink_code : public gimple_opt_pass | |
586 | { | |
587 | public: | |
c3284718 RS |
588 | pass_sink_code (gcc::context *ctxt) |
589 | : gimple_opt_pass (pass_data_sink_code, ctxt) | |
27a4cd48 DM |
590 | {} |
591 | ||
592 | /* opt_pass methods: */ | |
1a3d085c | 593 | virtual bool gate (function *) { return flag_tree_sink != 0; } |
be55bfe6 | 594 | virtual unsigned int execute (function *); |
27a4cd48 DM |
595 | |
596 | }; // class pass_sink_code | |
597 | ||
be55bfe6 TS |
598 | unsigned int |
599 | pass_sink_code::execute (function *fun) | |
600 | { | |
601 | loop_optimizer_init (LOOPS_NORMAL); | |
602 | split_critical_edges (); | |
603 | connect_infinite_loops_to_exit (); | |
604 | memset (&sink_stats, 0, sizeof (sink_stats)); | |
605 | calculate_dominance_info (CDI_DOMINATORS); | |
606 | calculate_dominance_info (CDI_POST_DOMINATORS); | |
607 | sink_code_in_bb (EXIT_BLOCK_PTR_FOR_FN (fun)); | |
608 | statistics_counter_event (fun, "Sunk statements", sink_stats.sunk); | |
609 | free_dominance_info (CDI_POST_DOMINATORS); | |
610 | remove_fake_exit_edges (); | |
611 | loop_optimizer_finalize (); | |
612 | ||
613 | return 0; | |
614 | } | |
615 | ||
27a4cd48 DM |
616 | } // anon namespace |
617 | ||
618 | gimple_opt_pass * | |
619 | make_pass_sink_code (gcc::context *ctxt) | |
620 | { | |
621 | return new pass_sink_code (ctxt); | |
622 | } |