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