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