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