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