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