1 /* Routines for performing Temporary Expression Replacement (TER) in SSA trees.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Andrew MacLeod <amacleod@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
25 #include "coretypes.h"
28 #include "diagnostic.h"
29 #include "tree-pretty-print.h"
30 #include "gimple-pretty-print.h"
32 #include "tree-flow.h"
33 #include "tree-dump.h"
34 #include "tree-ssa-live.h"
38 /* Temporary Expression Replacement (TER)
40 Replace SSA version variables during out-of-ssa with their defining
41 expression if there is only one use of the variable.
43 This pass is required in order for the RTL expansion pass to see larger
44 chunks of code. This allows it to make better choices on RTL pattern
45 selection. When expand is rewritten and merged with out-of-ssa, and
46 understands SSA, this should be eliminated.
48 A pass is made through the function, one block at a time. No cross block
49 information is tracked.
51 Variables which only have one use, and whose defining stmt is considered
52 a replaceable expression (see is_replaceable_p) are tracked to see whether
53 they can be replaced at their use location.
59 if there are the only use of n_12 and a_2, TER will substitute in their
60 expressions in v_9, and end up with:
62 v_9 = (b_5 + 6) * (C * 10)
64 which will then have the ssa_name assigned to regular variables, and the
65 resulting code which will be passed to the expander looks something like:
67 v = (b + 6) * (C * 10)
70 This requires ensuring that none of the variables used by the expression
71 change between the def point and where it is used. Furthermore, if any
72 of the ssa_names used in this expression are themselves replaceable, we
73 have to ensure none of that expressions' arguments change either.
74 Although SSA_NAMES themselves don't change, this pass is performed after
75 coalescing has coalesced different SSA_NAMES together, so there could be a
76 definition of an SSA_NAME which is coalesced with a use that causes a
79 PHI b_5 = <b_8(2), b_14(1)>
86 If b_5, b_8 and b_14 are all coalesced together...
87 The expression b_5 + 6 CANNOT replace the use in the statement defining v_9
88 because b_8 is in fact killing the value of b_5 since they share a partition
89 and will be assigned the same memory or register location.
91 TER implements this but stepping through the instructions in a block and
92 tracking potential expressions for replacement, and the partitions they are
93 dependent on. Expressions are represented by the SSA_NAME_VERSION of the
94 DEF on the LHS of a GIMPLE_ASSIGN and the expression is the RHS.
96 When a stmt is determined to be a possible replacement expression, the
97 following steps are taken:
99 EXPR_DECL_UID bitmap is allocated and set to the base variable UID of the
100 def and any uses in the expression. non-NULL means the expression is being
101 tracked. The UID's themselves are used to prevent TER substitution into
102 accumulating sequences, i.e.,
108 this can result in very large expressions which don't accomplish anything
109 see PR tree-optimization/17549.
111 PARTITION_DEPENDENCIES is another bitmap array, and it has a bit set for any
112 partition which is used in the expression. This is primarily used to remove
113 an expression from the partition kill lists when a decision is made whether
114 to replace it or not. This is indexed by ssa version number as well, and
115 indicates a partition number. virtual operands are not tracked individually,
116 but they are summarized by an artificial partition called VIRTUAL_PARTITION.
117 This means a MAY or MUST def will kill *ALL* expressions that are dependent
118 on a virtual operand.
119 Note that the EXPR_DECL_UID and this bitmap represent very similar
120 information, but the info in one is not easy to obtain from the other.
122 KILL_LIST is yet another bitmap array, this time it is indexed by partition
123 number, and represents a list of active expressions which will will no
124 longer be valid if a definition into this partition takes place.
126 PARTITION_IN_USE is simply a bitmap which is used to track which partitions
127 currently have something in their kill list. This is used at the end of
128 a block to clear out the KILL_LIST bitmaps at the end of each block.
130 NEW_REPLACEABLE_DEPENDENCIES is used as a temporary place to store
131 dependencies which will be reused by the current definition. All the uses
132 on an expression are processed before anything else is done. If a use is
133 determined to be a replaceable expression AND the current stmt is also going
134 to be replaceable, all the dependencies of this replaceable use will be
135 picked up by the current stmt's expression. Rather than recreate them, they
136 are simply copied here and then copied into the new expression when it is
142 a_2's expression 'b_5 + 6' is determined to be replaceable at the use
143 location. It is dependent on the partition 'b_5' is in. This is cached into
144 the NEW_REPLACEABLE_DEPENDENCIES bitmap, and when v_8 is examined for
145 replaceability, it is a candidate, and it is dependent on the partition
146 b_5 is in *NOT* a_2, as well as c_4's partition.
148 if v_8 is also replaceable:
152 x_9 is dependent on partitions b_5, and c_4
154 if a statement is found which has either of those partitions written to
155 before x_9 is used, then x_9 itself is NOT replaceable. */
158 /* Temporary Expression Replacement (TER) table information. */
160 typedef struct temp_expr_table_d
163 bitmap *partition_dependencies; /* Partitions expr is dependent on. */
164 bitmap replaceable_expressions; /* Replacement expression table. */
165 bitmap *expr_decl_uids; /* Base uids of exprs. */
166 bitmap *kill_list; /* Expr's killed by a partition. */
167 int virtual_partition; /* Pseudo partition for virtual ops. */
168 bitmap partition_in_use; /* Partitions with kill entries. */
169 bitmap new_replaceable_dependencies; /* Holding place for pending dep's. */
170 int *num_in_part; /* # of ssa_names in a partition. */
171 } *temp_expr_table_p;
173 /* Used to indicate a dependency on VDEFs. */
174 #define VIRTUAL_PARTITION(table) (table->virtual_partition)
176 #ifdef ENABLE_CHECKING
177 extern void debug_ter (FILE *, temp_expr_table_p);
181 /* Create a new TER table for MAP. */
183 static temp_expr_table_p
184 new_temp_expr_table (var_map map)
189 t = XNEW (struct temp_expr_table_d);
192 t->partition_dependencies = XCNEWVEC (bitmap, num_ssa_names + 1);
193 t->expr_decl_uids = XCNEWVEC (bitmap, num_ssa_names + 1);
194 t->kill_list = XCNEWVEC (bitmap, num_var_partitions (map) + 1);
196 t->partition_in_use = BITMAP_ALLOC (NULL);
198 t->virtual_partition = num_var_partitions (map);
199 t->new_replaceable_dependencies = BITMAP_ALLOC (NULL);
201 t->replaceable_expressions = NULL;
202 t->num_in_part = XCNEWVEC (int, num_var_partitions (map));
203 for (x = 1; x < num_ssa_names; x++)
206 tree name = ssa_name (x);
209 p = var_to_partition (map, name);
210 if (p != NO_PARTITION)
218 /* Free TER table T. If there are valid replacements, return the expression
222 free_temp_expr_table (temp_expr_table_p t)
226 #ifdef ENABLE_CHECKING
228 for (x = 0; x <= num_var_partitions (t->map); x++)
229 gcc_assert (!t->kill_list[x]);
230 for (x = 0; x < num_ssa_names; x++)
232 gcc_assert (t->expr_decl_uids[x] == NULL);
233 gcc_assert (t->partition_dependencies[x] == NULL);
237 BITMAP_FREE (t->partition_in_use);
238 BITMAP_FREE (t->new_replaceable_dependencies);
240 free (t->expr_decl_uids);
242 free (t->partition_dependencies);
243 free (t->num_in_part);
245 if (t->replaceable_expressions)
246 ret = t->replaceable_expressions;
253 /* Return TRUE if VERSION is to be replaced by an expression in TAB. */
256 version_to_be_replaced_p (temp_expr_table_p tab, int version)
258 if (!tab->replaceable_expressions)
260 return bitmap_bit_p (tab->replaceable_expressions, version);
264 /* Add partition P to the list if partitions VERSION is dependent on. TAB is
265 the expression table */
268 make_dependent_on_partition (temp_expr_table_p tab, int version, int p)
270 if (!tab->partition_dependencies[version])
271 tab->partition_dependencies[version] = BITMAP_ALLOC (NULL);
273 bitmap_set_bit (tab->partition_dependencies[version], p);
277 /* Add VER to the kill list for P. TAB is the expression table */
280 add_to_partition_kill_list (temp_expr_table_p tab, int p, int ver)
282 if (!tab->kill_list[p])
284 tab->kill_list[p] = BITMAP_ALLOC (NULL);
285 bitmap_set_bit (tab->partition_in_use, p);
287 bitmap_set_bit (tab->kill_list[p], ver);
291 /* Remove VER from the partition kill list for P. TAB is the expression
295 remove_from_partition_kill_list (temp_expr_table_p tab, int p, int version)
297 #ifdef ENABLE_CHECKING
298 gcc_assert (tab->kill_list[p]);
300 bitmap_clear_bit (tab->kill_list[p], version);
301 if (bitmap_empty_p (tab->kill_list[p]))
303 bitmap_clear_bit (tab->partition_in_use, p);
304 BITMAP_FREE (tab->kill_list[p]);
309 /* Add a dependency between the def of ssa VERSION and VAR. If VAR is
310 replaceable by an expression, add a dependence each of the elements of the
311 expression. These are contained in the new_replaceable list. TAB is the
315 add_dependence (temp_expr_table_p tab, int version, tree var)
321 i = SSA_NAME_VERSION (var);
322 if (version_to_be_replaced_p (tab, i))
324 if (!bitmap_empty_p (tab->new_replaceable_dependencies))
326 /* Version will now be killed by a write to any partition the
327 substituted expression would have been killed by. */
328 EXECUTE_IF_SET_IN_BITMAP (tab->new_replaceable_dependencies, 0, x, bi)
329 add_to_partition_kill_list (tab, x, version);
331 /* Rather than set partition_dependencies and in_use lists bit by
332 bit, simply OR in the new_replaceable_dependencies bits. */
333 if (!tab->partition_dependencies[version])
334 tab->partition_dependencies[version] = BITMAP_ALLOC (NULL);
335 bitmap_ior_into (tab->partition_dependencies[version],
336 tab->new_replaceable_dependencies);
337 bitmap_ior_into (tab->partition_in_use,
338 tab->new_replaceable_dependencies);
339 /* It is only necessary to add these once. */
340 bitmap_clear (tab->new_replaceable_dependencies);
345 i = var_to_partition (tab->map, var);
346 #ifdef ENABLE_CHECKING
347 gcc_assert (i != NO_PARTITION);
348 gcc_assert (tab->num_in_part[i] != 0);
350 /* Only dependencies on ssa_names which are coalesced with something need
351 to be tracked. Partitions with containing only a single SSA_NAME
352 *cannot* have their value changed. */
353 if (tab->num_in_part[i] > 1)
355 add_to_partition_kill_list (tab, i, version);
356 make_dependent_on_partition (tab, version, i);
362 /* Return TRUE if expression STMT is suitable for replacement. */
365 is_replaceable_p (gimple stmt)
370 location_t locus1, locus2;
373 /* Only consider modify stmts. */
374 if (!is_gimple_assign (stmt))
377 /* If the statement may throw an exception, it cannot be replaced. */
378 if (stmt_could_throw_p (stmt))
381 /* Punt if there is more than 1 def. */
382 def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF);
386 /* Only consider definitions which have a single use. */
387 if (!single_imm_use (def, &use_p, &use_stmt))
390 /* If the use isn't in this block, it wont be replaced either. */
391 if (gimple_bb (use_stmt) != gimple_bb (stmt))
394 locus1 = gimple_location (stmt);
395 block1 = gimple_block (stmt);
397 if (gimple_code (use_stmt) == GIMPLE_PHI)
404 locus2 = gimple_location (use_stmt);
405 block2 = gimple_block (use_stmt);
409 && ((locus1 && locus1 != locus2) || (block1 && block1 != block2)))
412 /* Used in this block, but at the TOP of the block, not the end. */
413 if (gimple_code (use_stmt) == GIMPLE_PHI)
416 /* There must be no VDEFs. */
417 if (gimple_vdef (stmt))
420 /* Without alias info we can't move around loads. */
421 if (gimple_references_memory_p (stmt) && !optimize)
424 /* Float expressions must go through memory if float-store is on. */
426 && FLOAT_TYPE_P (gimple_expr_type (stmt)))
429 /* An assignment with a register variable on the RHS is not
431 if (gimple_assign_rhs_code (stmt) == VAR_DECL
432 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt)))
435 /* No function calls can be replaced. */
436 if (is_gimple_call (stmt))
439 /* Leave any stmt with volatile operands alone as well. */
440 if (gimple_has_volatile_ops (stmt))
447 /* This function will remove the expression for VERSION from replacement
448 consideration in table TAB. If FREE_EXPR is true, then remove the
449 expression from consideration as well by freeing the decl uid bitmap. */
452 finished_with_expr (temp_expr_table_p tab, int version, bool free_expr)
457 /* Remove this expression from its dependent lists. The partition dependence
458 list is retained and transfered later to whomever uses this version. */
459 if (tab->partition_dependencies[version])
461 EXECUTE_IF_SET_IN_BITMAP (tab->partition_dependencies[version], 0, i, bi)
462 remove_from_partition_kill_list (tab, i, version);
463 BITMAP_FREE (tab->partition_dependencies[version]);
466 BITMAP_FREE (tab->expr_decl_uids[version]);
470 /* Create an expression entry for a replaceable expression. */
473 process_replaceable (temp_expr_table_p tab, gimple stmt)
475 tree var, def, basevar;
478 bitmap def_vars, use_vars;
480 #ifdef ENABLE_CHECKING
481 gcc_assert (is_replaceable_p (stmt));
484 def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF);
485 version = SSA_NAME_VERSION (def);
486 basevar = SSA_NAME_VAR (def);
487 def_vars = BITMAP_ALLOC (NULL);
489 bitmap_set_bit (def_vars, DECL_UID (basevar));
491 /* Add this expression to the dependency list for each use partition. */
492 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
494 int var_version = SSA_NAME_VERSION (var);
496 use_vars = tab->expr_decl_uids[var_version];
497 add_dependence (tab, version, var);
500 bitmap_ior_into (def_vars, use_vars);
501 BITMAP_FREE (tab->expr_decl_uids[var_version]);
504 bitmap_set_bit (def_vars, DECL_UID (SSA_NAME_VAR (var)));
506 tab->expr_decl_uids[version] = def_vars;
508 /* If there are VUSES, add a dependence on virtual defs. */
509 if (gimple_vuse (stmt))
511 make_dependent_on_partition (tab, version, VIRTUAL_PARTITION (tab));
512 add_to_partition_kill_list (tab, VIRTUAL_PARTITION (tab), version);
517 /* This function removes any expression in TAB which is dependent on PARTITION
518 from consideration, making it not replaceable. */
521 kill_expr (temp_expr_table_p tab, int partition)
525 /* Mark every active expr dependent on this var as not replaceable.
526 finished_with_expr can modify the bitmap, so we can't execute over it. */
527 while (tab->kill_list[partition])
529 version = bitmap_first_set_bit (tab->kill_list[partition]);
530 finished_with_expr (tab, version, true);
533 #ifdef ENABLE_CHECKING
534 gcc_assert (!tab->kill_list[partition]);
539 /* This function kills all expressions in TAB which are dependent on virtual
543 kill_virtual_exprs (temp_expr_table_p tab)
545 kill_expr (tab, VIRTUAL_PARTITION (tab));
549 /* Mark the expression associated with VAR as replaceable, and enter
550 the defining stmt into the partition_dependencies table TAB. If
551 MORE_REPLACING is true, accumulate the pending partition dependencies. */
554 mark_replaceable (temp_expr_table_p tab, tree var, bool more_replacing)
556 int version = SSA_NAME_VERSION (var);
558 /* Move the dependence list to the pending listpending. */
559 if (more_replacing && tab->partition_dependencies[version])
560 bitmap_ior_into (tab->new_replaceable_dependencies,
561 tab->partition_dependencies[version]);
563 finished_with_expr (tab, version, !more_replacing);
565 /* Set the replaceable expression. */
566 if (!tab->replaceable_expressions)
567 tab->replaceable_expressions = BITMAP_ALLOC (NULL);
568 bitmap_set_bit (tab->replaceable_expressions, version);
572 /* This function processes basic block BB, and looks for variables which can
573 be replaced by their expressions. Results are stored in the table TAB. */
576 find_replaceable_in_bb (temp_expr_table_p tab, basic_block bb)
578 gimple_stmt_iterator bsi;
582 var_map map = tab->map;
584 bool stmt_replaceable;
586 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
588 stmt = gsi_stmt (bsi);
590 if (is_gimple_debug (stmt))
593 stmt_replaceable = is_replaceable_p (stmt);
595 /* Determine if this stmt finishes an existing expression. */
596 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
598 unsigned ver = SSA_NAME_VERSION (use);
600 /* If this use is a potential replacement variable, process it. */
601 if (tab->expr_decl_uids[ver])
603 bool same_root_var = false;
605 bitmap vars = tab->expr_decl_uids[ver];
607 /* See if the root variables are the same. If they are, we
608 do not want to do the replacement to avoid problems with
609 code size, see PR tree-optimization/17549. */
610 if (!bitmap_empty_p (vars))
611 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter2, SSA_OP_DEF)
613 if (bitmap_bit_p (vars, DECL_UID (SSA_NAME_VAR (def))))
615 same_root_var = true;
620 /* Mark expression as replaceable unless stmt is volatile or the
621 def variable has the same root variable as something in the
622 substitution list. */
623 if (gimple_has_volatile_ops (stmt) || same_root_var)
624 finished_with_expr (tab, ver, true);
626 mark_replaceable (tab, use, stmt_replaceable);
630 /* Next, see if this stmt kills off an active expression. */
631 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
633 partition = var_to_partition (map, def);
634 if (partition != NO_PARTITION && tab->kill_list[partition])
635 kill_expr (tab, partition);
638 /* Now see if we are creating a new expression or not. */
639 if (stmt_replaceable)
640 process_replaceable (tab, stmt);
642 /* Free any unused dependency lists. */
643 bitmap_clear (tab->new_replaceable_dependencies);
645 /* A V_{MAY,MUST}_DEF kills any expression using a virtual operand,
646 including the current stmt. */
647 if (gimple_vdef (stmt))
648 kill_virtual_exprs (tab);
653 /* This function is the driver routine for replacement of temporary expressions
654 in the SSA->normal phase, operating on MAP. If there are replaceable
655 expressions, a table is returned which maps SSA versions to the
656 expressions they should be replaced with. A NULL_TREE indicates no
657 replacement should take place. If there are no replacements at all,
658 NULL is returned by the function, otherwise an expression vector indexed
659 by SSA_NAME version numbers. */
662 find_replaceable_exprs (var_map map)
665 temp_expr_table_p table;
668 table = new_temp_expr_table (map);
671 find_replaceable_in_bb (table, bb);
672 #ifdef ENABLE_CHECKING
673 gcc_assert (bitmap_empty_p (table->partition_in_use));
677 ret = free_temp_expr_table (table);
681 /* Dump TER expression table EXPR to file F. */
684 dump_replaceable_exprs (FILE *f, bitmap expr)
689 fprintf (f, "\nReplacing Expressions\n");
690 for (x = 0; x < num_ssa_names; x++)
691 if (bitmap_bit_p (expr, x))
694 print_generic_expr (f, var, TDF_SLIM);
695 fprintf (f, " replace with --> ");
696 print_gimple_stmt (f, SSA_NAME_DEF_STMT (var), 0, TDF_SLIM);
703 #ifdef ENABLE_CHECKING
704 /* Dump the status of the various tables in the expression table. This is used
705 exclusively to debug TER. F is the place to send debug info and T is the
706 table being debugged. */
709 debug_ter (FILE *f, temp_expr_table_p t)
714 fprintf (f, "\nDumping current state of TER\n virtual partition = %d\n",
715 VIRTUAL_PARTITION (t));
716 if (t->replaceable_expressions)
717 dump_replaceable_exprs (f, t->replaceable_expressions);
718 fprintf (f, "Currently tracking the following expressions:\n");
720 for (x = 1; x < num_ssa_names; x++)
721 if (t->expr_decl_uids[x])
723 print_generic_expr (stderr, ssa_name (x), TDF_SLIM);
724 fprintf (f, " dep-parts : ");
725 if (t->partition_dependencies[x]
726 && !bitmap_empty_p (t->partition_dependencies[x]))
728 EXECUTE_IF_SET_IN_BITMAP (t->partition_dependencies[x], 0, y, bi)
729 fprintf (f, "P%d ",y);
731 fprintf (stderr, " basedecls: ");
732 EXECUTE_IF_SET_IN_BITMAP (t->expr_decl_uids[x], 0, y, bi)
733 fprintf (f, "%d ",y);
734 fprintf (stderr, "\n");
737 bitmap_print (f, t->partition_in_use, "Partitions in use ",
738 "\npartition KILL lists:\n");
740 for (x = 0; x <= num_var_partitions (t->map); x++)
743 fprintf (f, "Partition %d : ", x);
744 EXECUTE_IF_SET_IN_BITMAP (t->kill_list[x], 0, y, bi)
745 fprintf (f, "_%d ",y);
748 fprintf (f, "\n----------\n");