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