* [SH] Miscellaneous changes for LRA.
[official-gcc.git] / gcc / tree-ssa-ter.c
blob96b3959e9a82f09b532411e6404bef6b0677726c
1 /* Routines for performing Temporary Expression Replacement (TER) in SSA trees.
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
5 This file is part of GCC.
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.
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.
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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "gimple-pretty-print.h"
28 #include "bitmap.h"
29 #include "predict.h"
30 #include "vec.h"
31 #include "hashtab.h"
32 #include "hash-set.h"
33 #include "machmode.h"
34 #include "hard-reg-set.h"
35 #include "input.h"
36 #include "function.h"
37 #include "dominance.h"
38 #include "cfg.h"
39 #include "basic-block.h"
40 #include "tree-ssa-alias.h"
41 #include "internal-fn.h"
42 #include "gimple-expr.h"
43 #include "is-a.h"
44 #include "gimple.h"
45 #include "gimple-iterator.h"
46 #include "gimple-ssa.h"
47 #include "tree-phinodes.h"
48 #include "ssa-iterators.h"
49 #include "stringpool.h"
50 #include "tree-ssanames.h"
51 #include "dumpfile.h"
52 #include "tree-ssa-live.h"
53 #include "tree-ssa-ter.h"
54 #include "tree-outof-ssa.h"
55 #include "flags.h"
56 #include "gimple-walk.h"
59 /* Temporary Expression Replacement (TER)
61 Replace SSA version variables during out-of-ssa with their defining
62 expression if there is only one use of the variable.
64 This pass is required in order for the RTL expansion pass to see larger
65 chunks of code. This allows it to make better choices on RTL pattern
66 selection. When expand is rewritten and merged with out-of-ssa, and
67 understands SSA, this should be eliminated.
69 A pass is made through the function, one block at a time. No cross block
70 information is tracked.
72 Variables which only have one use, and whose defining stmt is considered
73 a replaceable expression (see ssa_is_replaceable_p) are tracked to see whether
74 they can be replaced at their use location.
76 n_12 = C * 10
77 a_2 = b_5 + 6
78 v_9 = a_2 * n_12
80 if there are the only use of n_12 and a_2, TER will substitute in their
81 expressions in v_9, and end up with:
83 v_9 = (b_5 + 6) * (C * 10)
85 which will then have the ssa_name assigned to regular variables, and the
86 resulting code which will be passed to the expander looks something like:
88 v = (b + 6) * (C * 10)
91 This requires ensuring that none of the variables used by the expression
92 change between the def point and where it is used. Furthermore, if any
93 of the ssa_names used in this expression are themselves replaceable, we
94 have to ensure none of that expressions' arguments change either.
95 Although SSA_NAMES themselves don't change, this pass is performed after
96 coalescing has coalesced different SSA_NAMES together, so there could be a
97 definition of an SSA_NAME which is coalesced with a use that causes a
98 problem, i.e.,
100 PHI b_5 = <b_8(2), b_14(1)>
101 <...>
102 a_2 = b_5 + 6
103 b_8 = c_4 + 4
104 v_9 = a_2 * n_12
105 <...>
107 If b_5, b_8 and b_14 are all coalesced together...
108 The expression b_5 + 6 CANNOT replace the use in the statement defining v_9
109 because b_8 is in fact killing the value of b_5 since they share a partition
110 and will be assigned the same memory or register location.
112 TER implements this but stepping through the instructions in a block and
113 tracking potential expressions for replacement, and the partitions they are
114 dependent on. Expressions are represented by the SSA_NAME_VERSION of the
115 DEF on the LHS of a GIMPLE_ASSIGN and the expression is the RHS.
117 When a stmt is determined to be a possible replacement expression, the
118 following steps are taken:
120 EXPR_DECL_UID bitmap is allocated and set to the base variable UID of the
121 def and any uses in the expression. non-NULL means the expression is being
122 tracked. The UID's themselves are used to prevent TER substitution into
123 accumulating sequences, i.e.,
125 x = x + y
126 x = x + z
127 x = x + w
128 etc.
129 this can result in very large expressions which don't accomplish anything
130 see PR tree-optimization/17549.
132 PARTITION_DEPENDENCIES is another bitmap array, and it has a bit set for any
133 partition which is used in the expression. This is primarily used to remove
134 an expression from the partition kill lists when a decision is made whether
135 to replace it or not. This is indexed by ssa version number as well, and
136 indicates a partition number. virtual operands are not tracked individually,
137 but they are summarized by an artificial partition called VIRTUAL_PARTITION.
138 This means a MAY or MUST def will kill *ALL* expressions that are dependent
139 on a virtual operand.
140 Note that the EXPR_DECL_UID and this bitmap represent very similar
141 information, but the info in one is not easy to obtain from the other.
143 KILL_LIST is yet another bitmap array, this time it is indexed by partition
144 number, and represents a list of active expressions which will will no
145 longer be valid if a definition into this partition takes place.
147 PARTITION_IN_USE is simply a bitmap which is used to track which partitions
148 currently have something in their kill list. This is used at the end of
149 a block to clear out the KILL_LIST bitmaps at the end of each block.
151 NEW_REPLACEABLE_DEPENDENCIES is used as a temporary place to store
152 dependencies which will be reused by the current definition. All the uses
153 on an expression are processed before anything else is done. If a use is
154 determined to be a replaceable expression AND the current stmt is also going
155 to be replaceable, all the dependencies of this replaceable use will be
156 picked up by the current stmt's expression. Rather than recreate them, they
157 are simply copied here and then copied into the new expression when it is
158 processed.
160 a_2 = b_5 + 6
161 v_8 = a_2 + c_4
163 a_2's expression 'b_5 + 6' is determined to be replaceable at the use
164 location. It is dependent on the partition 'b_5' is in. This is cached into
165 the NEW_REPLACEABLE_DEPENDENCIES bitmap, and when v_8 is examined for
166 replaceability, it is a candidate, and it is dependent on the partition
167 b_5 is in *NOT* a_2, as well as c_4's partition.
169 if v_8 is also replaceable:
171 x_9 = v_8 * 5
173 x_9 is dependent on partitions b_5, and c_4
175 if a statement is found which has either of those partitions written to
176 before x_9 is used, then x_9 itself is NOT replaceable. */
179 /* Temporary Expression Replacement (TER) table information. */
181 typedef struct temp_expr_table_d
183 var_map map;
184 bitmap *partition_dependencies; /* Partitions expr is dependent on. */
185 bitmap replaceable_expressions; /* Replacement expression table. */
186 bitmap *expr_decl_uids; /* Base uids of exprs. */
187 bitmap *kill_list; /* Expr's killed by a partition. */
188 int virtual_partition; /* Pseudo partition for virtual ops. */
189 bitmap partition_in_use; /* Partitions with kill entries. */
190 bitmap new_replaceable_dependencies; /* Holding place for pending dep's. */
191 int *num_in_part; /* # of ssa_names in a partition. */
192 int *call_cnt; /* Call count at definition. */
193 } *temp_expr_table_p;
195 /* Used to indicate a dependency on VDEFs. */
196 #define VIRTUAL_PARTITION(table) (table->virtual_partition)
198 /* A place for the many, many bitmaps we create. */
199 static bitmap_obstack ter_bitmap_obstack;
201 #ifdef ENABLE_CHECKING
202 extern void debug_ter (FILE *, temp_expr_table_p);
203 #endif
206 /* Create a new TER table for MAP. */
208 static temp_expr_table_p
209 new_temp_expr_table (var_map map)
211 temp_expr_table_p t;
212 unsigned x;
214 t = XNEW (struct temp_expr_table_d);
215 t->map = map;
217 t->partition_dependencies = XCNEWVEC (bitmap, num_ssa_names + 1);
218 t->expr_decl_uids = XCNEWVEC (bitmap, num_ssa_names + 1);
219 t->kill_list = XCNEWVEC (bitmap, num_var_partitions (map) + 1);
221 t->partition_in_use = BITMAP_ALLOC (&ter_bitmap_obstack);
223 t->virtual_partition = num_var_partitions (map);
224 t->new_replaceable_dependencies = BITMAP_ALLOC (&ter_bitmap_obstack);
226 t->replaceable_expressions = NULL;
227 t->num_in_part = XCNEWVEC (int, num_var_partitions (map));
228 for (x = 1; x < num_ssa_names; x++)
230 int p;
231 tree name = ssa_name (x);
232 if (!name)
233 continue;
234 p = var_to_partition (map, name);
235 if (p != NO_PARTITION)
236 t->num_in_part[p]++;
238 t->call_cnt = XCNEWVEC (int, num_ssa_names + 1);
240 return t;
244 /* Free TER table T. If there are valid replacements, return the expression
245 vector. */
247 static bitmap
248 free_temp_expr_table (temp_expr_table_p t)
250 bitmap ret = NULL;
252 #ifdef ENABLE_CHECKING
253 unsigned x;
254 for (x = 0; x <= num_var_partitions (t->map); x++)
255 gcc_assert (!t->kill_list[x]);
256 for (x = 0; x < num_ssa_names; x++)
258 gcc_assert (t->expr_decl_uids[x] == NULL);
259 gcc_assert (t->partition_dependencies[x] == NULL);
261 #endif
263 BITMAP_FREE (t->partition_in_use);
264 BITMAP_FREE (t->new_replaceable_dependencies);
266 free (t->expr_decl_uids);
267 free (t->kill_list);
268 free (t->partition_dependencies);
269 free (t->num_in_part);
270 free (t->call_cnt);
272 if (t->replaceable_expressions)
273 ret = t->replaceable_expressions;
275 free (t);
276 return ret;
280 /* Return TRUE if VERSION is to be replaced by an expression in TAB. */
282 static inline bool
283 version_to_be_replaced_p (temp_expr_table_p tab, int version)
285 if (!tab->replaceable_expressions)
286 return false;
287 return bitmap_bit_p (tab->replaceable_expressions, version);
291 /* Add partition P to the list if partitions VERSION is dependent on. TAB is
292 the expression table */
294 static inline void
295 make_dependent_on_partition (temp_expr_table_p tab, int version, int p)
297 if (!tab->partition_dependencies[version])
298 tab->partition_dependencies[version] = BITMAP_ALLOC (&ter_bitmap_obstack);
300 bitmap_set_bit (tab->partition_dependencies[version], p);
304 /* Add VER to the kill list for P. TAB is the expression table */
306 static inline void
307 add_to_partition_kill_list (temp_expr_table_p tab, int p, int ver)
309 if (!tab->kill_list[p])
311 tab->kill_list[p] = BITMAP_ALLOC (&ter_bitmap_obstack);
312 bitmap_set_bit (tab->partition_in_use, p);
314 bitmap_set_bit (tab->kill_list[p], ver);
318 /* Remove VER from the partition kill list for P. TAB is the expression
319 table. */
321 static inline void
322 remove_from_partition_kill_list (temp_expr_table_p tab, int p, int version)
324 gcc_checking_assert (tab->kill_list[p]);
325 bitmap_clear_bit (tab->kill_list[p], version);
326 if (bitmap_empty_p (tab->kill_list[p]))
328 bitmap_clear_bit (tab->partition_in_use, p);
329 BITMAP_FREE (tab->kill_list[p]);
334 /* Add a dependency between the def of ssa VERSION and VAR. If VAR is
335 replaceable by an expression, add a dependence each of the elements of the
336 expression. These are contained in the new_replaceable list. TAB is the
337 expression table. */
339 static void
340 add_dependence (temp_expr_table_p tab, int version, tree var)
342 int i;
343 bitmap_iterator bi;
344 unsigned x;
346 i = SSA_NAME_VERSION (var);
347 if (version_to_be_replaced_p (tab, i))
349 if (!bitmap_empty_p (tab->new_replaceable_dependencies))
351 /* Version will now be killed by a write to any partition the
352 substituted expression would have been killed by. */
353 EXECUTE_IF_SET_IN_BITMAP (tab->new_replaceable_dependencies, 0, x, bi)
354 add_to_partition_kill_list (tab, x, version);
356 /* Rather than set partition_dependencies and in_use lists bit by
357 bit, simply OR in the new_replaceable_dependencies bits. */
358 if (!tab->partition_dependencies[version])
359 tab->partition_dependencies[version] =
360 BITMAP_ALLOC (&ter_bitmap_obstack);
361 bitmap_ior_into (tab->partition_dependencies[version],
362 tab->new_replaceable_dependencies);
363 bitmap_ior_into (tab->partition_in_use,
364 tab->new_replaceable_dependencies);
365 /* It is only necessary to add these once. */
366 bitmap_clear (tab->new_replaceable_dependencies);
369 else
371 i = var_to_partition (tab->map, var);
372 gcc_checking_assert (i != NO_PARTITION);
373 gcc_checking_assert (tab->num_in_part[i] != 0);
374 /* Only dependencies on ssa_names which are coalesced with something need
375 to be tracked. Partitions with containing only a single SSA_NAME
376 *cannot* have their value changed. */
377 if (tab->num_in_part[i] > 1)
379 add_to_partition_kill_list (tab, i, version);
380 make_dependent_on_partition (tab, version, i);
386 /* This function will remove the expression for VERSION from replacement
387 consideration in table TAB. If FREE_EXPR is true, then remove the
388 expression from consideration as well by freeing the decl uid bitmap. */
390 static void
391 finished_with_expr (temp_expr_table_p tab, int version, bool free_expr)
393 unsigned i;
394 bitmap_iterator bi;
396 /* Remove this expression from its dependent lists. The partition dependence
397 list is retained and transferred later to whomever uses this version. */
398 if (tab->partition_dependencies[version])
400 EXECUTE_IF_SET_IN_BITMAP (tab->partition_dependencies[version], 0, i, bi)
401 remove_from_partition_kill_list (tab, i, version);
402 BITMAP_FREE (tab->partition_dependencies[version]);
404 if (free_expr)
405 BITMAP_FREE (tab->expr_decl_uids[version]);
409 /* Return TRUE if expression STMT is suitable for replacement.
410 In addition to ssa_is_replaceable_p, require the same bb, and for -O0
411 same locus and same BLOCK), Considers memory loads as replaceable if aliasing
412 is available. */
414 static inline bool
415 ter_is_replaceable_p (gimple stmt)
418 if (ssa_is_replaceable_p (stmt))
420 use_operand_p use_p;
421 tree def;
422 gimple use_stmt;
423 location_t locus1, locus2;
424 tree block1, block2;
426 /* Only consider definitions which have a single use. ssa_is_replaceable_p
427 already performed this check, but the use stmt pointer is required for
428 further checks. */
429 def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF);
430 if (!single_imm_use (def, &use_p, &use_stmt))
431 return false;
433 /* If the use isn't in this block, it wont be replaced either. */
434 if (gimple_bb (use_stmt) != gimple_bb (stmt))
435 return false;
437 locus1 = gimple_location (stmt);
438 block1 = LOCATION_BLOCK (locus1);
439 locus1 = LOCATION_LOCUS (locus1);
441 if (gphi *phi = dyn_cast <gphi *> (use_stmt))
442 locus2 = gimple_phi_arg_location (phi,
443 PHI_ARG_INDEX_FROM_USE (use_p));
444 else
445 locus2 = gimple_location (use_stmt);
446 block2 = LOCATION_BLOCK (locus2);
447 locus2 = LOCATION_LOCUS (locus2);
449 if ((!optimize || optimize_debug)
450 && ((locus1 != UNKNOWN_LOCATION && locus1 != locus2)
451 || (block1 != NULL_TREE && block1 != block2)))
452 return false;
454 return true;
456 return false;
460 /* Create an expression entry for a replaceable expression. */
462 static void
463 process_replaceable (temp_expr_table_p tab, gimple stmt, int call_cnt)
465 tree var, def, basevar;
466 int version;
467 ssa_op_iter iter;
468 bitmap def_vars, use_vars;
470 gcc_checking_assert (ter_is_replaceable_p (stmt));
472 def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF);
473 version = SSA_NAME_VERSION (def);
474 def_vars = BITMAP_ALLOC (&ter_bitmap_obstack);
476 basevar = SSA_NAME_VAR (def);
477 if (basevar)
478 bitmap_set_bit (def_vars, DECL_UID (basevar));
480 /* Add this expression to the dependency list for each use partition. */
481 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
483 int var_version = SSA_NAME_VERSION (var);
485 use_vars = tab->expr_decl_uids[var_version];
486 add_dependence (tab, version, var);
487 if (use_vars)
489 bitmap_ior_into (def_vars, use_vars);
490 BITMAP_FREE (tab->expr_decl_uids[var_version]);
492 else if (SSA_NAME_VAR (var))
493 bitmap_set_bit (def_vars, DECL_UID (SSA_NAME_VAR (var)));
495 tab->expr_decl_uids[version] = def_vars;
497 /* If there are VUSES, add a dependence on virtual defs. */
498 if (gimple_vuse (stmt))
500 make_dependent_on_partition (tab, version, VIRTUAL_PARTITION (tab));
501 add_to_partition_kill_list (tab, VIRTUAL_PARTITION (tab), version);
504 tab->call_cnt[version] = call_cnt;
508 /* This function removes any expression in TAB which is dependent on PARTITION
509 from consideration, making it not replaceable. */
511 static inline void
512 kill_expr (temp_expr_table_p tab, int partition)
514 unsigned version;
516 /* Mark every active expr dependent on this var as not replaceable.
517 finished_with_expr can modify the bitmap, so we can't execute over it. */
518 while (tab->kill_list[partition])
520 version = bitmap_first_set_bit (tab->kill_list[partition]);
521 finished_with_expr (tab, version, true);
524 gcc_checking_assert (!tab->kill_list[partition]);
528 /* This function kills all expressions in TAB which are dependent on virtual
529 partitions. */
531 static inline void
532 kill_virtual_exprs (temp_expr_table_p tab)
534 kill_expr (tab, VIRTUAL_PARTITION (tab));
538 /* Mark the expression associated with VAR as replaceable, and enter
539 the defining stmt into the partition_dependencies table TAB. If
540 MORE_REPLACING is true, accumulate the pending partition dependencies. */
542 static void
543 mark_replaceable (temp_expr_table_p tab, tree var, bool more_replacing)
545 int version = SSA_NAME_VERSION (var);
547 /* Move the dependence list to the pending listpending. */
548 if (more_replacing && tab->partition_dependencies[version])
549 bitmap_ior_into (tab->new_replaceable_dependencies,
550 tab->partition_dependencies[version]);
552 finished_with_expr (tab, version, !more_replacing);
554 /* Set the replaceable expression.
555 The bitmap for this "escapes" from this file so it's allocated
556 on the default obstack. */
557 if (!tab->replaceable_expressions)
558 tab->replaceable_expressions = BITMAP_ALLOC (NULL);
559 bitmap_set_bit (tab->replaceable_expressions, version);
563 /* Helper function for find_ssaname_in_stores. Called via walk_tree to
564 find a SSA_NAME DATA somewhere in *TP. */
566 static tree
567 find_ssaname (tree *tp, int *walk_subtrees, void *data)
569 tree var = (tree) data;
570 if (*tp == var)
571 return var;
572 else if (IS_TYPE_OR_DECL_P (*tp))
573 *walk_subtrees = 0;
574 return NULL_TREE;
577 /* Helper function for find_replaceable_in_bb. Return true if SSA_NAME DATA
578 is used somewhere in T, which is a store in the statement. Called via
579 walk_stmt_load_store_addr_ops. */
581 static bool
582 find_ssaname_in_store (gimple, tree, tree t, void *data)
584 return walk_tree (&t, find_ssaname, data, NULL) != NULL_TREE;
587 /* This function processes basic block BB, and looks for variables which can
588 be replaced by their expressions. Results are stored in the table TAB. */
590 static void
591 find_replaceable_in_bb (temp_expr_table_p tab, basic_block bb)
593 gimple_stmt_iterator bsi;
594 gimple stmt;
595 tree def, use, fndecl;
596 int partition;
597 var_map map = tab->map;
598 ssa_op_iter iter;
599 bool stmt_replaceable;
600 int cur_call_cnt = 0;
602 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
604 stmt = gsi_stmt (bsi);
606 if (is_gimple_debug (stmt))
607 continue;
609 stmt_replaceable = ter_is_replaceable_p (stmt);
611 /* Determine if this stmt finishes an existing expression. */
612 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
614 unsigned ver = SSA_NAME_VERSION (use);
616 /* If this use is a potential replacement variable, process it. */
617 if (tab->expr_decl_uids[ver])
619 bool same_root_var = false;
620 ssa_op_iter iter2;
621 bitmap vars = tab->expr_decl_uids[ver];
623 /* See if the root variables are the same. If they are, we
624 do not want to do the replacement to avoid problems with
625 code size, see PR tree-optimization/17549. */
626 if (!bitmap_empty_p (vars))
627 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter2, SSA_OP_DEF)
629 if (SSA_NAME_VAR (def)
630 && bitmap_bit_p (vars, DECL_UID (SSA_NAME_VAR (def))))
632 same_root_var = true;
633 break;
637 /* If the stmt does a memory store and the replacement
638 is a load aliasing it avoid creating overlapping
639 assignments which we cannot expand correctly. */
640 if (gimple_vdef (stmt))
642 gimple def_stmt = SSA_NAME_DEF_STMT (use);
643 while (is_gimple_assign (def_stmt)
644 && gimple_assign_rhs_code (def_stmt) == SSA_NAME)
645 def_stmt
646 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (def_stmt));
647 if (gimple_vuse (def_stmt)
648 && gimple_assign_single_p (def_stmt)
649 && stmt_may_clobber_ref_p (stmt,
650 gimple_assign_rhs1 (def_stmt)))
652 /* For calls, it is not a problem if USE is among
653 call's arguments or say OBJ_TYPE_REF argument,
654 all those necessarily need to be evaluated before
655 the call that may clobber the memory. But if
656 LHS of the call refers to USE, expansion might
657 evaluate it after the call, prevent TER in that
658 case.
659 For inline asm, allow TER of loads into input
660 arguments, but disallow TER for USEs that occur
661 somewhere in outputs. */
662 if (is_gimple_call (stmt)
663 || gimple_code (stmt) == GIMPLE_ASM)
665 if (walk_stmt_load_store_ops (stmt, use, NULL,
666 find_ssaname_in_store))
667 same_root_var = true;
669 else
670 same_root_var = true;
674 /* Mark expression as replaceable unless stmt is volatile, or the
675 def variable has the same root variable as something in the
676 substitution list, or the def and use span a call such that
677 we'll expand lifetimes across a call. */
678 if (gimple_has_volatile_ops (stmt) || same_root_var
679 || (tab->call_cnt[ver] != cur_call_cnt
680 && SINGLE_SSA_USE_OPERAND (SSA_NAME_DEF_STMT (use), SSA_OP_USE)
681 == NULL_USE_OPERAND_P))
682 finished_with_expr (tab, ver, true);
683 else
684 mark_replaceable (tab, use, stmt_replaceable);
688 /* Next, see if this stmt kills off an active expression. */
689 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
691 partition = var_to_partition (map, def);
692 if (partition != NO_PARTITION && tab->kill_list[partition])
693 kill_expr (tab, partition);
696 /* Increment counter if this is a non BUILT_IN call. We allow
697 replacement over BUILT_IN calls since many will expand to inline
698 insns instead of a true call. */
699 if (is_gimple_call (stmt)
700 && !((fndecl = gimple_call_fndecl (stmt))
701 && DECL_BUILT_IN (fndecl)))
702 cur_call_cnt++;
704 /* Now see if we are creating a new expression or not. */
705 if (stmt_replaceable)
706 process_replaceable (tab, stmt, cur_call_cnt);
708 /* Free any unused dependency lists. */
709 bitmap_clear (tab->new_replaceable_dependencies);
711 /* A V_{MAY,MUST}_DEF kills any expression using a virtual operand,
712 including the current stmt. */
713 if (gimple_vdef (stmt))
714 kill_virtual_exprs (tab);
719 /* This function is the driver routine for replacement of temporary expressions
720 in the SSA->normal phase, operating on MAP. If there are replaceable
721 expressions, a table is returned which maps SSA versions to the
722 expressions they should be replaced with. A NULL_TREE indicates no
723 replacement should take place. If there are no replacements at all,
724 NULL is returned by the function, otherwise an expression vector indexed
725 by SSA_NAME version numbers. */
727 bitmap
728 find_replaceable_exprs (var_map map)
730 basic_block bb;
731 temp_expr_table_p table;
732 bitmap ret;
734 bitmap_obstack_initialize (&ter_bitmap_obstack);
735 table = new_temp_expr_table (map);
736 FOR_EACH_BB_FN (bb, cfun)
738 find_replaceable_in_bb (table, bb);
739 gcc_checking_assert (bitmap_empty_p (table->partition_in_use));
741 ret = free_temp_expr_table (table);
742 bitmap_obstack_release (&ter_bitmap_obstack);
743 return ret;
746 /* Dump TER expression table EXPR to file F. */
748 void
749 dump_replaceable_exprs (FILE *f, bitmap expr)
751 tree var;
752 unsigned x;
754 fprintf (f, "\nReplacing Expressions\n");
755 for (x = 0; x < num_ssa_names; x++)
756 if (bitmap_bit_p (expr, x))
758 var = ssa_name (x);
759 print_generic_expr (f, var, TDF_SLIM);
760 fprintf (f, " replace with --> ");
761 print_gimple_stmt (f, SSA_NAME_DEF_STMT (var), 0, TDF_SLIM);
762 fprintf (f, "\n");
764 fprintf (f, "\n");
768 #ifdef ENABLE_CHECKING
769 /* Dump the status of the various tables in the expression table. This is used
770 exclusively to debug TER. F is the place to send debug info and T is the
771 table being debugged. */
773 DEBUG_FUNCTION void
774 debug_ter (FILE *f, temp_expr_table_p t)
776 unsigned x, y;
777 bitmap_iterator bi;
779 fprintf (f, "\nDumping current state of TER\n virtual partition = %d\n",
780 VIRTUAL_PARTITION (t));
781 if (t->replaceable_expressions)
782 dump_replaceable_exprs (f, t->replaceable_expressions);
783 fprintf (f, "Currently tracking the following expressions:\n");
785 for (x = 1; x < num_ssa_names; x++)
786 if (t->expr_decl_uids[x])
788 print_generic_expr (f, ssa_name (x), TDF_SLIM);
789 fprintf (f, " dep-parts : ");
790 if (t->partition_dependencies[x]
791 && !bitmap_empty_p (t->partition_dependencies[x]))
793 EXECUTE_IF_SET_IN_BITMAP (t->partition_dependencies[x], 0, y, bi)
794 fprintf (f, "P%d ",y);
796 fprintf (f, " basedecls: ");
797 EXECUTE_IF_SET_IN_BITMAP (t->expr_decl_uids[x], 0, y, bi)
798 fprintf (f, "%d ",y);
799 fprintf (f, " call_cnt : %d",t->call_cnt[x]);
800 fprintf (f, "\n");
803 bitmap_print (f, t->partition_in_use, "Partitions in use ",
804 "\npartition KILL lists:\n");
806 for (x = 0; x <= num_var_partitions (t->map); x++)
807 if (t->kill_list[x])
809 fprintf (f, "Partition %d : ", x);
810 EXECUTE_IF_SET_IN_BITMAP (t->kill_list[x], 0, y, bi)
811 fprintf (f, "_%d ",y);
814 fprintf (f, "\n----------\n");
816 #endif