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