]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-ssa-sink.c
i386.h (TARGET_ABM): New define.
[thirdparty/gcc.git] / gcc / tree-ssa-sink.c
CommitLineData
ec1e9f7c
DB
1/* Code sinking for trees
2 Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dan@dberlin.org>
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING. If not, write to
366ccddb
KC
19the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20Boston, MA 02110-1301, USA. */
ec1e9f7c
DB
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
25#include "tm.h"
ec1e9f7c
DB
26#include "ggc.h"
27#include "tree.h"
28#include "basic-block.h"
29#include "diagnostic.h"
30#include "tree-inline.h"
31#include "tree-flow.h"
32#include "tree-gimple.h"
33#include "tree-dump.h"
34#include "timevar.h"
35#include "fibheap.h"
36#include "hashtab.h"
37#include "tree-iterator.h"
38#include "real.h"
39#include "alloc-pool.h"
40#include "tree-pass.h"
41#include "flags.h"
42#include "bitmap.h"
43#include "langhooks.h"
44#include "cfgloop.h"
45
46/* TODO:
47 1. Sinking store only using scalar promotion (IE without moving the RHS):
48
49 *q = p;
50 p = p + 1;
51 if (something)
52 *q = <not p>;
53 else
54 y = *q;
55
56
57 should become
58 sinktemp = p;
59 p = p + 1;
60 if (something)
61 *q = <not p>;
62 else
63 {
64 *q = sinktemp;
65 y = *q
66 }
67 Store copy propagation will take care of the store elimination above.
68
69
70 2. Sinking using Partial Dead Code Elimination. */
71
72
73static struct
74{
6c6cfbfd 75 /* The number of statements sunk down the flowgraph by code sinking. */
ec1e9f7c
DB
76 int sunk;
77
78} sink_stats;
79
80
f652d14b 81/* Given a PHI, and one of its arguments (DEF), find the edge for
ec1e9f7c
DB
82 that argument and return it. If the argument occurs twice in the PHI node,
83 we return NULL. */
84
85static basic_block
86find_bb_for_arg (tree phi, tree def)
87{
88 int i;
89 bool foundone = false;
90 basic_block result = NULL;
91 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
92 if (PHI_ARG_DEF (phi, i) == def)
93 {
94 if (foundone)
95 return NULL;
96 foundone = true;
97 result = PHI_ARG_EDGE (phi, i)->src;
98 }
99 return result;
100}
101
102/* When the first immediate use is in a statement, then return true if all
103 immediate uses in IMM are in the same statement.
104 We could also do the case where the first immediate use is in a phi node,
105 and all the other uses are in phis in the same basic block, but this
106 requires some expensive checking later (you have to make sure no def/vdef
107 in the statement occurs for multiple edges in the various phi nodes it's
6c6cfbfd 108 used in, so that you only have one place you can sink it to. */
ec1e9f7c
DB
109
110static bool
f430bae8 111all_immediate_uses_same_place (tree stmt)
ec1e9f7c 112{
f430bae8
AM
113 tree firstuse = NULL_TREE;
114 ssa_op_iter op_iter;
115 imm_use_iterator imm_iter;
116 use_operand_p use_p;
117 tree var;
ec1e9f7c 118
f430bae8 119 FOR_EACH_SSA_TREE_OPERAND (var, stmt, op_iter, SSA_OP_ALL_DEFS)
ec1e9f7c 120 {
f430bae8
AM
121 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, var)
122 {
123 if (firstuse == NULL_TREE)
124 firstuse = USE_STMT (use_p);
125 else
126 if (firstuse != USE_STMT (use_p))
127 return false;
128 }
ec1e9f7c 129 }
f430bae8 130
ec1e9f7c
DB
131 return true;
132}
133
38635499 134/* Some global stores don't necessarily have VDEF's of global variables,
ec1e9f7c
DB
135 but we still must avoid moving them around. */
136
137bool
138is_hidden_global_store (tree stmt)
139{
ec1e9f7c
DB
140 /* Check virtual definitions. If we get here, the only virtual
141 definitions we should see are those generated by assignment
142 statements. */
f47c96aa 143 if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_VIRTUAL_DEFS))
ec1e9f7c
DB
144 {
145 tree lhs;
146
07beea0d 147 gcc_assert (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT);
ec1e9f7c
DB
148
149 /* Note that we must not check the individual virtual operands
150 here. In particular, if this is an aliased store, we could
151 end up with something like the following (SSA notation
152 redacted for brevity):
153
154 foo (int *p, int i)
155 {
156 int x;
157 p_1 = (i_2 > 3) ? &x : p;
158
38635499 159 # x_4 = VDEF <x_3>
ec1e9f7c
DB
160 *p_1 = 5;
161
162 return 2;
163 }
164
165 Notice that the store to '*p_1' should be preserved, if we
166 were to check the virtual definitions in that store, we would
167 not mark it needed. This is because 'x' is not a global
168 variable.
169
170 Therefore, we check the base address of the LHS. If the
18cd8a03 171 address is a pointer, we check if its name tag or symbol tag is
ec1e9f7c
DB
172 a global variable. Otherwise, we check if the base variable
173 is a global. */
07beea0d 174 lhs = GIMPLE_STMT_OPERAND (stmt, 0);
ec1e9f7c
DB
175 if (REFERENCE_CLASS_P (lhs))
176 lhs = get_base_address (lhs);
177
178 if (lhs == NULL_TREE)
179 {
180 /* If LHS is NULL, it means that we couldn't get the base
181 address of the reference. In which case, we should not
182 move this store. */
183 return true;
184 }
185 else if (DECL_P (lhs))
186 {
187 /* If the store is to a global symbol, we need to keep it. */
188 if (is_global_var (lhs))
189 return true;
190
191 }
192 else if (INDIRECT_REF_P (lhs))
193 {
194 tree ptr = TREE_OPERAND (lhs, 0);
195 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
196 tree nmt = (pi) ? pi->name_mem_tag : NULL_TREE;
38635499 197 tree smt = symbol_mem_tag (SSA_NAME_VAR (ptr));
ec1e9f7c 198
18cd8a03 199 /* If either the name tag or the symbol tag for PTR is a
ec1e9f7c
DB
200 global variable, then the store is necessary. */
201 if ((nmt && is_global_var (nmt))
18cd8a03 202 || (smt && is_global_var (smt)))
ec1e9f7c
DB
203 {
204 return true;
205 }
206 }
207 else
208 gcc_unreachable ();
209 }
38635499 210
ec1e9f7c
DB
211 return false;
212}
213
214/* Find the nearest common dominator of all of the immediate uses in IMM. */
215
216static basic_block
f430bae8 217nearest_common_dominator_of_uses (tree stmt)
ec1e9f7c
DB
218{
219 bitmap blocks = BITMAP_ALLOC (NULL);
220 basic_block commondom;
ec1e9f7c
DB
221 unsigned int j;
222 bitmap_iterator bi;
f430bae8
AM
223 ssa_op_iter op_iter;
224 imm_use_iterator imm_iter;
225 use_operand_p use_p;
226 tree var;
227
ec1e9f7c 228 bitmap_clear (blocks);
f430bae8 229 FOR_EACH_SSA_TREE_OPERAND (var, stmt, op_iter, SSA_OP_ALL_DEFS)
ec1e9f7c 230 {
f430bae8
AM
231 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, var)
232 {
233 tree usestmt = USE_STMT (use_p);
234 basic_block useblock;
000b62dc 235
f430bae8
AM
236 if (TREE_CODE (usestmt) == PHI_NODE)
237 {
55b12f0d 238 int idx = PHI_ARG_INDEX_FROM_USE (use_p);
ab798313
KH
239
240 useblock = PHI_ARG_EDGE (usestmt, idx)->src;
f430bae8
AM
241 }
242 else
ec1e9f7c 243 {
f430bae8 244 useblock = bb_for_stmt (usestmt);
000b62dc 245 }
f430bae8 246
000b62dc
KH
247 /* Short circuit. Nothing dominates the entry block. */
248 if (useblock == ENTRY_BLOCK_PTR)
249 {
250 BITMAP_FREE (blocks);
251 return NULL;
ec1e9f7c 252 }
000b62dc 253 bitmap_set_bit (blocks, useblock->index);
ec1e9f7c 254 }
ec1e9f7c
DB
255 }
256 commondom = BASIC_BLOCK (bitmap_first_set_bit (blocks));
257 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, j, bi)
258 commondom = nearest_common_dominator (CDI_DOMINATORS, commondom,
259 BASIC_BLOCK (j));
260 BITMAP_FREE (blocks);
261 return commondom;
262}
263
264/* Given a statement (STMT) and the basic block it is currently in (FROMBB),
265 determine the location to sink the statement to, if any.
266 Return the basic block to sink it to, or NULL if we should not sink
6c6cfbfd 267 it. */
ec1e9f7c
DB
268
269static tree
270statement_sink_location (tree stmt, basic_block frombb)
271{
ec1e9f7c 272 tree use, def;
f430bae8 273 use_operand_p one_use = NULL_USE_OPERAND_P;
ec1e9f7c
DB
274 basic_block sinkbb;
275 use_operand_p use_p;
276 def_operand_p def_p;
277 ssa_op_iter iter;
278 stmt_ann_t ann;
279 tree rhs;
f430bae8
AM
280 imm_use_iterator imm_iter;
281
282 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
283 {
284 FOR_EACH_IMM_USE_FAST (one_use, imm_iter, def)
285 {
286 break;
287 }
288 if (one_use != NULL_USE_OPERAND_P)
289 break;
290 }
ec1e9f7c 291
f430bae8
AM
292 /* Return if there are no immediate uses of this stmt. */
293 if (one_use == NULL_USE_OPERAND_P)
ec1e9f7c
DB
294 return NULL;
295
07beea0d 296 if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
ec1e9f7c 297 return NULL;
07beea0d 298 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
ec1e9f7c
DB
299
300 /* There are a few classes of things we can't or don't move, some because we
301 don't have code to handle it, some because it's not profitable and some
302 because it's not legal.
303
304 We can't sink things that may be global stores, at least not without
305 calculating a lot more information, because we may cause it to no longer
306 be seen by an external routine that needs it depending on where it gets
307 moved to.
308
309 We don't want to sink loads from memory.
310
311 We can't sink statements that end basic blocks without splitting the
312 incoming edge for the sink location to place it there.
313
314 We can't sink statements that have volatile operands.
315
316 We don't want to sink dead code, so anything with 0 immediate uses is not
317 sunk.
318
319 */
320 ann = stmt_ann (stmt);
f47c96aa 321 if (stmt_ends_bb_p (stmt)
ec1e9f7c
DB
322 || TREE_SIDE_EFFECTS (rhs)
323 || TREE_CODE (rhs) == EXC_PTR_EXPR
324 || TREE_CODE (rhs) == FILTER_EXPR
325 || is_hidden_global_store (stmt)
f47c96aa
AM
326 || ann->has_volatile_ops
327 || !ZERO_SSA_OPERANDS (stmt, SSA_OP_VUSE))
ec1e9f7c
DB
328 return NULL;
329
330 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
331 {
332 tree def = DEF_FROM_PTR (def_p);
333 if (is_global_var (SSA_NAME_VAR (def))
334 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def))
335 return NULL;
336 }
337
338 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
339 {
340 tree use = USE_FROM_PTR (use_p);
341 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use))
342 return NULL;
343 }
344
345 /* If all the immediate uses are not in the same place, find the nearest
346 common dominator of all the immediate uses. For PHI nodes, we have to
347 find the nearest common dominator of all of the predecessor blocks, since
348 that is where insertion would have to take place. */
f430bae8 349 if (!all_immediate_uses_same_place (stmt))
ec1e9f7c 350 {
f430bae8 351 basic_block commondom = nearest_common_dominator_of_uses (stmt);
ec1e9f7c
DB
352
353 if (commondom == frombb)
354 return NULL;
355
356 /* Our common dominator has to be dominated by frombb in order to be a
357 trivially safe place to put this statement, since it has multiple
358 uses. */
359 if (!dominated_by_p (CDI_DOMINATORS, commondom, frombb))
360 return NULL;
361
362 /* It doesn't make sense to move to a dominator that post-dominates
363 frombb, because it means we've just moved it into a path that always
364 executes if frombb executes, instead of reducing the number of
365 executions . */
366 if (dominated_by_p (CDI_POST_DOMINATORS, frombb, commondom))
367 {
368 if (dump_file && (dump_flags & TDF_DETAILS))
369 fprintf (dump_file, "Not moving store, common dominator post-dominates from block.\n");
370 return NULL;
371 }
372
373 if (commondom == frombb || commondom->loop_depth > frombb->loop_depth)
374 return NULL;
375 if (dump_file && (dump_flags & TDF_DETAILS))
376 {
377 fprintf (dump_file, "Common dominator of all uses is %d\n",
378 commondom->index);
379 }
380 return first_stmt (commondom);
381 }
382
f430bae8 383 use = USE_STMT (one_use);
ec1e9f7c
DB
384 if (TREE_CODE (use) != PHI_NODE)
385 {
386 sinkbb = bb_for_stmt (use);
387 if (sinkbb == frombb || sinkbb->loop_depth > frombb->loop_depth
388 || sinkbb->loop_father != frombb->loop_father)
389 return NULL;
390 return use;
391 }
392
393 /* Note that at this point, all uses must be in the same statement, so it
f47c96aa
AM
394 doesn't matter which def op we choose, pick the first one. */
395 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
396 break;
397
ec1e9f7c
DB
398
399 sinkbb = find_bb_for_arg (use, def);
400 if (!sinkbb)
401 return NULL;
402
403 /* This will happen when you have
404 a_3 = PHI <a_13, a_26>
405
38635499 406 a_26 = VDEF <a_3>
ec1e9f7c
DB
407
408 If the use is a phi, and is in the same bb as the def,
409 we can't sink it. */
410
411 if (bb_for_stmt (use) == frombb)
412 return NULL;
413 if (sinkbb == frombb || sinkbb->loop_depth > frombb->loop_depth
414 || sinkbb->loop_father != frombb->loop_father)
415 return NULL;
416
417 return first_stmt (sinkbb);
418}
419
420/* Perform code sinking on BB */
421
422static void
423sink_code_in_bb (basic_block bb)
424{
425 basic_block son;
426 block_stmt_iterator bsi;
427 edge_iterator ei;
428 edge e;
429
430 /* If this block doesn't dominate anything, there can't be any place to sink
431 the statements to. */
432 if (first_dom_son (CDI_DOMINATORS, bb) == NULL)
433 goto earlyout;
434
435 /* We can't move things across abnormal edges, so don't try. */
436 FOR_EACH_EDGE (e, ei, bb->succs)
437 if (e->flags & EDGE_ABNORMAL)
438 goto earlyout;
439
440 for (bsi = bsi_last (bb); !bsi_end_p (bsi);)
441 {
442 tree stmt = bsi_stmt (bsi);
443 block_stmt_iterator tobsi;
444 tree sinkstmt;
ec1e9f7c
DB
445
446 sinkstmt = statement_sink_location (stmt, bb);
447 if (!sinkstmt)
448 {
449 if (!bsi_end_p (bsi))
450 bsi_prev (&bsi);
451 continue;
452 }
453 if (dump_file)
454 {
455 fprintf (dump_file, "Sinking ");
456 print_generic_expr (dump_file, stmt, TDF_VOPS);
457 fprintf (dump_file, " from bb %d to bb %d\n",
458 bb->index, bb_for_stmt (sinkstmt)->index);
459 }
460 tobsi = bsi_for_stmt (sinkstmt);
461 /* Find the first non-label. */
462 while (!bsi_end_p (tobsi)
463 && TREE_CODE (bsi_stmt (tobsi)) == LABEL_EXPR)
464 bsi_next (&tobsi);
465
466 /* If this is the end of the basic block, we need to insert at the end
467 of the basic block. */
468 if (bsi_end_p (tobsi))
469 bsi_move_to_bb_end (&bsi, bb_for_stmt (sinkstmt));
470 else
471 bsi_move_before (&bsi, &tobsi);
472
473 sink_stats.sunk++;
474 if (!bsi_end_p (bsi))
475 bsi_prev (&bsi);
476
477 }
478 earlyout:
479 for (son = first_dom_son (CDI_POST_DOMINATORS, bb);
480 son;
481 son = next_dom_son (CDI_POST_DOMINATORS, son))
482 {
483 sink_code_in_bb (son);
484 }
485}
486
487/* Perform code sinking.
488 This moves code down the flowgraph when we know it would be
489 profitable to do so, or it wouldn't increase the number of
490 executions of the statement.
491
492 IE given
493
494 a_1 = b + c;
495 if (<something>)
496 {
497 }
498 else
499 {
500 foo (&b, &c);
501 a_5 = b + c;
502 }
503 a_6 = PHI (a_5, a_1);
504 USE a_6.
505
506 we'll transform this into:
507
508 if (<something>)
509 {
510 a_1 = b + c;
511 }
512 else
513 {
514 foo (&b, &c);
515 a_5 = b + c;
516 }
517 a_6 = PHI (a_5, a_1);
518 USE a_6.
519
520 Note that this reduces the number of computations of a = b + c to 1
521 when we take the else edge, instead of 2.
522*/
523static void
524execute_sink_code (void)
525{
598ec7bd 526 loop_optimizer_init (LOOPS_NORMAL);
10d22567 527
ec1e9f7c
DB
528 connect_infinite_loops_to_exit ();
529 memset (&sink_stats, 0, sizeof (sink_stats));
3b5ee6a4
RG
530 calculate_dominance_info (CDI_DOMINATORS);
531 calculate_dominance_info (CDI_POST_DOMINATORS);
ec1e9f7c
DB
532 sink_code_in_bb (EXIT_BLOCK_PTR);
533 if (dump_file && (dump_flags & TDF_STATS))
534 fprintf (dump_file, "Sunk statements:%d\n", sink_stats.sunk);
535 free_dominance_info (CDI_POST_DOMINATORS);
536 remove_fake_exit_edges ();
598ec7bd 537 loop_optimizer_finalize ();
ec1e9f7c
DB
538}
539
540/* Gate and execute functions for PRE. */
541
c2924966 542static unsigned int
ec1e9f7c
DB
543do_sink (void)
544{
545 execute_sink_code ();
c2924966 546 return 0;
ec1e9f7c
DB
547}
548
549static bool
550gate_sink (void)
551{
552 return flag_tree_sink != 0;
553}
554
555struct tree_opt_pass pass_sink_code =
556{
557 "sink", /* name */
558 gate_sink, /* gate */
559 do_sink, /* execute */
560 NULL, /* sub */
561 NULL, /* next */
562 0, /* static_pass_number */
563 TV_TREE_SINK, /* tv_id */
564 PROP_no_crit_edges | PROP_cfg
565 | PROP_ssa | PROP_alias, /* properties_required */
566 0, /* properties_provided */
567 0, /* properties_destroyed */
568 0, /* todo_flags_start */
0bca51f0
DN
569 TODO_update_ssa
570 | TODO_dump_func
571 | TODO_ggc_collect
572 | TODO_verify_ssa, /* todo_flags_finish */
ec1e9f7c
DB
573 0 /* letter */
574};