]> gcc.gnu.org Git - gcc.git/blame - gcc/tree-ssa-dom.c
* Remove svn:executable property.
[gcc.git] / gcc / tree-ssa-dom.c
CommitLineData
6de9cd9a 1/* SSA Dominator optimizations for trees
939409af 2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
2090d6a0 3 Free Software Foundation, Inc.
6de9cd9a
DN
4 Contributed by Diego Novillo <dnovillo@redhat.com>
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
13GCC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GCC; see the file COPYING. If not, write to
366ccddb
KC
20the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21Boston, MA 02110-1301, USA. */
6de9cd9a
DN
22
23#include "config.h"
24#include "system.h"
25#include "coretypes.h"
26#include "tm.h"
27#include "tree.h"
28#include "flags.h"
29#include "rtl.h"
30#include "tm_p.h"
31#include "ggc.h"
32#include "basic-block.h"
d38ffc55 33#include "cfgloop.h"
6de9cd9a 34#include "output.h"
6de9cd9a
DN
35#include "expr.h"
36#include "function.h"
37#include "diagnostic.h"
38#include "timevar.h"
39#include "tree-dump.h"
40#include "tree-flow.h"
41#include "domwalk.h"
42#include "real.h"
43#include "tree-pass.h"
c7f90219 44#include "tree-ssa-propagate.h"
6de9cd9a 45#include "langhooks.h"
43f31be5 46#include "params.h"
6de9cd9a
DN
47
48/* This file implements optimizations on the dominator tree. */
49
efea75f9
JL
50
51/* Structure for recording edge equivalences as well as any pending
52 edge redirections during the dominator optimizer.
53
54 Computing and storing the edge equivalences instead of creating
55 them on-demand can save significant amounts of time, particularly
56 for pathological cases involving switch statements.
57
58 These structures live for a single iteration of the dominator
59 optimizer in the edge's AUX field. At the end of an iteration we
60 free each of these structures and update the AUX field to point
61 to any requested redirection target (the code for updating the
62 CFG and SSA graph for edge redirection expects redirection edge
63 targets to be in the AUX field for each edge. */
64
65struct edge_info
66{
67 /* If this edge creates a simple equivalence, the LHS and RHS of
68 the equivalence will be stored here. */
69 tree lhs;
70 tree rhs;
71
72 /* Traversing an edge may also indicate one or more particular conditions
73 are true or false. The number of recorded conditions can vary, but
74 can be determined by the condition's code. So we have an array
75 and its maximum index rather than use a varray. */
76 tree *cond_equivalences;
77 unsigned int max_cond_equivalences;
efea75f9
JL
78};
79
80
6de9cd9a
DN
81/* Hash table with expressions made available during the renaming process.
82 When an assignment of the form X_i = EXPR is found, the statement is
83 stored in this table. If the same expression EXPR is later found on the
84 RHS of another statement, it is replaced with X_i (thus performing
85 global redundancy elimination). Similarly as we pass through conditionals
86 we record the conditional itself as having either a true or false value
87 in this table. */
88static htab_t avail_exprs;
89
48732f23
JL
90/* Stack of available expressions in AVAIL_EXPRs. Each block pushes any
91 expressions it enters into the hash table along with a marker entry
b3a27618 92 (null). When we finish processing the block, we pop off entries and
48732f23
JL
93 remove the expressions from the global hash table until we hit the
94 marker. */
d4e6fecb 95static VEC(tree,heap) *avail_exprs_stack;
48732f23 96
a6e1aa26
JL
97/* Stack of statements we need to rescan during finalization for newly
98 exposed variables.
99
100 Statement rescanning must occur after the current block's available
101 expressions are removed from AVAIL_EXPRS. Else we may change the
102 hash code for an expression and be unable to find/remove it from
103 AVAIL_EXPRS. */
cfaab3a9
DN
104typedef tree *tree_p;
105DEF_VEC_P(tree_p);
106DEF_VEC_ALLOC_P(tree_p,heap);
107
108static VEC(tree_p,heap) *stmts_to_rescan;
a6e1aa26 109
6de9cd9a
DN
110/* Structure for entries in the expression hash table.
111
112 This requires more memory for the hash table entries, but allows us
113 to avoid creating silly tree nodes and annotations for conditionals,
114 eliminates 2 global hash tables and two block local varrays.
115
116 It also allows us to reduce the number of hash table lookups we
117 have to perform in lookup_avail_expr and finally it allows us to
118 significantly reduce the number of calls into the hashing routine
119 itself. */
56b043c8 120
6de9cd9a
DN
121struct expr_hash_elt
122{
123 /* The value (lhs) of this expression. */
124 tree lhs;
125
126 /* The expression (rhs) we want to record. */
127 tree rhs;
128
f47c96aa
AM
129 /* The stmt pointer if this element corresponds to a statement. */
130 tree stmt;
6de9cd9a
DN
131
132 /* The hash value for RHS/ann. */
133 hashval_t hash;
134};
135
b5fefcf6
JL
136/* Stack of dest,src pairs that need to be restored during finalization.
137
138 A NULL entry is used to mark the end of pairs which need to be
139 restored during finalization of this block. */
d4e6fecb 140static VEC(tree,heap) *const_and_copies_stack;
b5fefcf6 141
6de9cd9a
DN
142/* Track whether or not we have changed the control flow graph. */
143static bool cfg_altered;
144
1eaba2f2 145/* Bitmap of blocks that have had EH statements cleaned. We should
f6fe65dc 146 remove their dead edges eventually. */
1eaba2f2
RH
147static bitmap need_eh_cleanup;
148
6de9cd9a
DN
149/* Statistics for dominator optimizations. */
150struct opt_stats_d
151{
152 long num_stmts;
153 long num_exprs_considered;
154 long num_re;
0bca51f0
DN
155 long num_const_prop;
156 long num_copy_prop;
6de9cd9a
DN
157};
158
23530866
JL
159static struct opt_stats_d opt_stats;
160
6de9cd9a
DN
161struct eq_expr_value
162{
163 tree src;
164 tree dst;
165};
166
167/* Local functions. */
168static void optimize_stmt (struct dom_walk_data *,
169 basic_block bb,
170 block_stmt_iterator);
48732f23 171static tree lookup_avail_expr (tree, bool);
6de9cd9a 172static hashval_t avail_expr_hash (const void *);
940db2c8 173static hashval_t real_avail_expr_hash (const void *);
6de9cd9a
DN
174static int avail_expr_eq (const void *, const void *);
175static void htab_statistics (FILE *, htab_t);
48732f23 176static void record_cond (tree, tree);
b5fefcf6
JL
177static void record_const_or_copy (tree, tree);
178static void record_equality (tree, tree);
efea75f9
JL
179static void record_equivalences_from_phis (basic_block);
180static void record_equivalences_from_incoming_edge (basic_block);
487bf3e6 181static bool eliminate_redundant_computations (tree);
fdabe5c2 182static void record_equivalences_from_stmt (tree, int, stmt_ann_t);
2090d6a0 183static void dom_thread_across_edge (struct dom_walk_data *, edge);
6de9cd9a 184static void dom_opt_finalize_block (struct dom_walk_data *, basic_block);
6de9cd9a 185static void dom_opt_initialize_block (struct dom_walk_data *, basic_block);
efea75f9 186static void propagate_to_outgoing_edges (struct dom_walk_data *, basic_block);
48732f23 187static void remove_local_expressions_from_table (void);
b5fefcf6 188static void restore_vars_to_original_value (void);
28c008bb 189static edge single_incoming_edge_ignoring_loop_edges (basic_block);
6de9cd9a 190
0bca51f0 191
efea75f9
JL
192/* Allocate an EDGE_INFO for edge E and attach it to E.
193 Return the new EDGE_INFO structure. */
194
195static struct edge_info *
196allocate_edge_info (edge e)
197{
198 struct edge_info *edge_info;
199
e1111e8e 200 edge_info = XCNEW (struct edge_info);
efea75f9
JL
201
202 e->aux = edge_info;
203 return edge_info;
204}
205
206/* Free all EDGE_INFO structures associated with edges in the CFG.
cbb1cada 207 If a particular edge can be threaded, copy the redirection
efea75f9
JL
208 target from the EDGE_INFO structure into the edge's AUX field
209 as required by code to update the CFG and SSA graph for
210 jump threading. */
211
212static void
213free_all_edge_infos (void)
214{
215 basic_block bb;
216 edge_iterator ei;
217 edge e;
218
219 FOR_EACH_BB (bb)
220 {
221 FOR_EACH_EDGE (e, ei, bb->preds)
222 {
e1111e8e 223 struct edge_info *edge_info = (struct edge_info *) e->aux;
efea75f9
JL
224
225 if (edge_info)
226 {
efea75f9
JL
227 if (edge_info->cond_equivalences)
228 free (edge_info->cond_equivalences);
229 free (edge_info);
8702a557 230 e->aux = NULL;
efea75f9
JL
231 }
232 }
233 }
234}
235
6de9cd9a
DN
236/* Jump threading, redundancy elimination and const/copy propagation.
237
6de9cd9a
DN
238 This pass may expose new symbols that need to be renamed into SSA. For
239 every new symbol exposed, its corresponding bit will be set in
ff2ad0f7 240 VARS_TO_RENAME. */
6de9cd9a 241
c2924966 242static unsigned int
6de9cd9a
DN
243tree_ssa_dominator_optimize (void)
244{
6de9cd9a
DN
245 struct dom_walk_data walk_data;
246 unsigned int i;
247
fded8de7
DN
248 memset (&opt_stats, 0, sizeof (opt_stats));
249
6de9cd9a 250 /* Create our hash tables. */
940db2c8 251 avail_exprs = htab_create (1024, real_avail_expr_hash, avail_expr_eq, free);
d4e6fecb 252 avail_exprs_stack = VEC_alloc (tree, heap, 20);
d4e6fecb 253 const_and_copies_stack = VEC_alloc (tree, heap, 20);
cfaab3a9 254 stmts_to_rescan = VEC_alloc (tree_p, heap, 20);
8bdbfff5 255 need_eh_cleanup = BITMAP_ALLOC (NULL);
6de9cd9a
DN
256
257 /* Setup callbacks for the generic dominator tree walker. */
258 walk_data.walk_stmts_backward = false;
259 walk_data.dom_direction = CDI_DOMINATORS;
fdabe5c2 260 walk_data.initialize_block_local_data = NULL;
6de9cd9a
DN
261 walk_data.before_dom_children_before_stmts = dom_opt_initialize_block;
262 walk_data.before_dom_children_walk_stmts = optimize_stmt;
efea75f9 263 walk_data.before_dom_children_after_stmts = propagate_to_outgoing_edges;
6de9cd9a
DN
264 walk_data.after_dom_children_before_stmts = NULL;
265 walk_data.after_dom_children_walk_stmts = NULL;
266 walk_data.after_dom_children_after_stmts = dom_opt_finalize_block;
267 /* Right now we only attach a dummy COND_EXPR to the global data pointer.
268 When we attach more stuff we'll need to fill this out with a real
269 structure. */
270 walk_data.global_data = NULL;
fdabe5c2 271 walk_data.block_local_data_size = 0;
0bca51f0 272 walk_data.interesting_blocks = NULL;
6de9cd9a
DN
273
274 /* Now initialize the dominator walker. */
275 init_walk_dominator_tree (&walk_data);
276
6de9cd9a 277 calculate_dominance_info (CDI_DOMINATORS);
8d9d6561 278 cfg_altered = false;
6de9cd9a 279
d38ffc55
JL
280 /* We need to know which edges exit loops so that we can
281 aggressively thread through loop headers to an exit
282 edge. */
89f8f30f 283 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
598ec7bd
ZD
284 if (current_loops)
285 {
d73be268 286 mark_loop_exit_edges ();
598ec7bd
ZD
287 loop_optimizer_finalize ();
288 }
d38ffc55
JL
289
290 /* Clean up the CFG so that any forwarder blocks created by loop
291 canonicalization are removed. */
292 cleanup_tree_cfg ();
38965eb2 293 calculate_dominance_info (CDI_DOMINATORS);
d38ffc55 294
2090d6a0
JL
295 /* We need accurate information regarding back edges in the CFG
296 for jump threading. */
297 mark_dfs_back_edges ();
d38ffc55 298
2090d6a0
JL
299 /* Recursively walk the dominator tree optimizing statements. */
300 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
6de9cd9a 301
2090d6a0
JL
302 {
303 block_stmt_iterator bsi;
304 basic_block bb;
305 FOR_EACH_BB (bb)
f430bae8 306 {
2090d6a0
JL
307 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
308 update_stmt_if_modified (bsi_stmt (bsi));
f430bae8 309 }
2090d6a0 310 }
a3b609df 311
2090d6a0
JL
312 /* If we exposed any new variables, go ahead and put them into
313 SSA form now, before we handle jump threading. This simplifies
314 interactions between rewriting of _DECL nodes into SSA form
315 and rewriting SSA_NAME nodes into SSA form after block
316 duplication and CFG manipulation. */
317 update_ssa (TODO_update_ssa);
d38ffc55 318
2090d6a0 319 free_all_edge_infos ();
d38ffc55 320
2090d6a0
JL
321 /* Thread jumps, creating duplicate blocks as needed. */
322 cfg_altered |= thread_through_all_blocks ();
6de9cd9a 323
8d9d6561
EB
324 if (cfg_altered)
325 free_dominance_info (CDI_DOMINATORS);
326
2090d6a0
JL
327 /* Removal of statements may make some EH edges dead. Purge
328 such edges from the CFG as needed. */
329 if (!bitmap_empty_p (need_eh_cleanup))
330 {
4be1caf9 331 tree_purge_all_dead_eh_edges (need_eh_cleanup);
2090d6a0
JL
332 bitmap_zero (need_eh_cleanup);
333 }
6de9cd9a 334
2090d6a0 335 /* Finally, remove everything except invariants in SSA_NAME_VALUE.
0bd65483 336
2090d6a0
JL
337 Long term we will be able to let everything in SSA_NAME_VALUE
338 persist. However, for now, we know this is the safe thing to do. */
339 for (i = 0; i < num_ssa_names; i++)
340 {
341 tree name = ssa_name (i);
342 tree value;
0bd65483 343
2090d6a0
JL
344 if (!name)
345 continue;
0bd65483 346
2090d6a0
JL
347 value = SSA_NAME_VALUE (name);
348 if (value && !is_gimple_min_invariant (value))
349 SSA_NAME_VALUE (name) = NULL;
6de9cd9a 350 }
6de9cd9a 351
6de9cd9a
DN
352 /* Debugging dumps. */
353 if (dump_file && (dump_flags & TDF_STATS))
354 dump_dominator_optimization_stats (dump_file);
355
2090d6a0 356 /* Delete our main hashtable. */
6de9cd9a 357 htab_delete (avail_exprs);
6de9cd9a
DN
358
359 /* And finalize the dominator walker. */
360 fini_walk_dominator_tree (&walk_data);
cfa4cb00 361
b16caf72 362 /* Free asserted bitmaps and stacks. */
8bdbfff5 363 BITMAP_FREE (need_eh_cleanup);
3a2e4b46 364
d4e6fecb
NS
365 VEC_free (tree, heap, avail_exprs_stack);
366 VEC_free (tree, heap, const_and_copies_stack);
cfaab3a9 367 VEC_free (tree_p, heap, stmts_to_rescan);
c2924966 368 return 0;
6de9cd9a
DN
369}
370
371static bool
372gate_dominator (void)
373{
374 return flag_tree_dom != 0;
375}
376
377struct tree_opt_pass pass_dominator =
378{
379 "dom", /* name */
380 gate_dominator, /* gate */
381 tree_ssa_dominator_optimize, /* execute */
382 NULL, /* sub */
383 NULL, /* next */
384 0, /* static_pass_number */
385 TV_TREE_SSA_DOMINATOR_OPTS, /* tv_id */
c1b763fa 386 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
6de9cd9a 387 0, /* properties_provided */
ae07b463 388 0, /* properties_destroyed */
6de9cd9a 389 0, /* todo_flags_start */
0bca51f0
DN
390 TODO_dump_func
391 | TODO_update_ssa
2090d6a0 392 | TODO_cleanup_cfg
706ca88e 393 | TODO_verify_ssa, /* todo_flags_finish */
9f8628ba 394 0 /* letter */
6de9cd9a
DN
395};
396
397
0e0ed594
JL
398/* Given a stmt CONDSTMT containing a COND_EXPR, canonicalize the
399 COND_EXPR into a canonical form. */
400
401static void
402canonicalize_comparison (tree condstmt)
403{
404 tree cond = COND_EXPR_COND (condstmt);
405 tree op0;
406 tree op1;
407 enum tree_code code = TREE_CODE (cond);
408
409 if (!COMPARISON_CLASS_P (cond))
410 return;
411
412 op0 = TREE_OPERAND (cond, 0);
413 op1 = TREE_OPERAND (cond, 1);
414
415 /* If it would be profitable to swap the operands, then do so to
416 canonicalize the statement, enabling better optimization.
417
418 By placing canonicalization of such expressions here we
419 transparently keep statements in canonical form, even
420 when the statement is modified. */
421 if (tree_swap_operands_p (op0, op1, false))
422 {
423 /* For relationals we need to swap the operands
424 and change the code. */
425 if (code == LT_EXPR
426 || code == GT_EXPR
427 || code == LE_EXPR
428 || code == GE_EXPR)
429 {
430 TREE_SET_CODE (cond, swap_tree_comparison (code));
431 swap_tree_operands (condstmt,
432 &TREE_OPERAND (cond, 0),
433 &TREE_OPERAND (cond, 1));
434 /* If one operand was in the operand cache, but the other is
435 not, because it is a constant, this is a case that the
436 internal updating code of swap_tree_operands can't handle
437 properly. */
438 if (TREE_CODE_CLASS (TREE_CODE (op0))
439 != TREE_CODE_CLASS (TREE_CODE (op1)))
440 update_stmt (condstmt);
441 }
442 }
443}
6de9cd9a 444
6de9cd9a
DN
445/* Initialize local stacks for this optimizer and record equivalences
446 upon entry to BB. Equivalences can come from the edge traversed to
447 reach BB or they may come from PHI nodes at the start of BB. */
448
449static void
efea75f9
JL
450dom_opt_initialize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
451 basic_block bb)
6de9cd9a
DN
452{
453 if (dump_file && (dump_flags & TDF_DETAILS))
454 fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
455
9fae925b
JL
456 /* Push a marker on the stacks of local information so that we know how
457 far to unwind when we finalize this block. */
d4e6fecb 458 VEC_safe_push (tree, heap, avail_exprs_stack, NULL_TREE);
d4e6fecb 459 VEC_safe_push (tree, heap, const_and_copies_stack, NULL_TREE);
48732f23 460
efea75f9 461 record_equivalences_from_incoming_edge (bb);
6de9cd9a
DN
462
463 /* PHI nodes can create equivalences too. */
efea75f9 464 record_equivalences_from_phis (bb);
6de9cd9a
DN
465}
466
467/* Given an expression EXPR (a relational expression or a statement),
206048bd 468 initialize the hash table element pointed to by ELEMENT. */
6de9cd9a
DN
469
470static void
471initialize_hash_element (tree expr, tree lhs, struct expr_hash_elt *element)
472{
473 /* Hash table elements may be based on conditional expressions or statements.
474
475 For the former case, we have no annotation and we want to hash the
476 conditional expression. In the latter case we have an annotation and
477 we want to record the expression the statement evaluates. */
6615c446 478 if (COMPARISON_CLASS_P (expr) || TREE_CODE (expr) == TRUTH_NOT_EXPR)
6de9cd9a 479 {
f47c96aa 480 element->stmt = NULL;
6de9cd9a
DN
481 element->rhs = expr;
482 }
483 else if (TREE_CODE (expr) == COND_EXPR)
484 {
f47c96aa 485 element->stmt = expr;
6de9cd9a
DN
486 element->rhs = COND_EXPR_COND (expr);
487 }
488 else if (TREE_CODE (expr) == SWITCH_EXPR)
489 {
f47c96aa 490 element->stmt = expr;
6de9cd9a
DN
491 element->rhs = SWITCH_COND (expr);
492 }
493 else if (TREE_CODE (expr) == RETURN_EXPR && TREE_OPERAND (expr, 0))
494 {
f47c96aa 495 element->stmt = expr;
07beea0d 496 element->rhs = GIMPLE_STMT_OPERAND (TREE_OPERAND (expr, 0), 1);
6de9cd9a 497 }
74d2efc7
JL
498 else if (TREE_CODE (expr) == GOTO_EXPR)
499 {
f47c96aa 500 element->stmt = expr;
74d2efc7
JL
501 element->rhs = GOTO_DESTINATION (expr);
502 }
6de9cd9a
DN
503 else
504 {
f47c96aa 505 element->stmt = expr;
07beea0d 506 element->rhs = GENERIC_TREE_OPERAND (expr, 1);
6de9cd9a
DN
507 }
508
509 element->lhs = lhs;
510 element->hash = avail_expr_hash (element);
511}
512
513/* Remove all the expressions in LOCALS from TABLE, stopping when there are
514 LIMIT entries left in LOCALs. */
515
516static void
48732f23 517remove_local_expressions_from_table (void)
6de9cd9a 518{
6de9cd9a 519 /* Remove all the expressions made available in this block. */
d4e6fecb 520 while (VEC_length (tree, avail_exprs_stack) > 0)
6de9cd9a
DN
521 {
522 struct expr_hash_elt element;
d4e6fecb 523 tree expr = VEC_pop (tree, avail_exprs_stack);
48732f23
JL
524
525 if (expr == NULL_TREE)
526 break;
6de9cd9a
DN
527
528 initialize_hash_element (expr, NULL, &element);
48732f23 529 htab_remove_elt_with_hash (avail_exprs, &element, element.hash);
6de9cd9a
DN
530 }
531}
532
b5fefcf6
JL
533/* Use the source/dest pairs in CONST_AND_COPIES_STACK to restore
534 CONST_AND_COPIES to its original state, stopping when we hit a
535 NULL marker. */
6de9cd9a
DN
536
537static void
b5fefcf6 538restore_vars_to_original_value (void)
6de9cd9a 539{
d4e6fecb 540 while (VEC_length (tree, const_and_copies_stack) > 0)
6de9cd9a
DN
541 {
542 tree prev_value, dest;
543
d4e6fecb 544 dest = VEC_pop (tree, const_and_copies_stack);
6de9cd9a 545
b5fefcf6
JL
546 if (dest == NULL)
547 break;
548
d4e6fecb 549 prev_value = VEC_pop (tree, const_and_copies_stack);
3aecd08b 550 SSA_NAME_VALUE (dest) = prev_value;
6de9cd9a
DN
551 }
552}
553
2090d6a0
JL
554/* A trivial wrapper so that we can present the generic jump
555 threading code with a simple API for simplifying statements. */
556static tree
0c948c27 557simplify_stmt_for_jump_threading (tree stmt, tree within_stmt ATTRIBUTE_UNUSED)
2090d6a0
JL
558{
559 return lookup_avail_expr (stmt, false);
560}
561
562/* Wrapper for common code to attempt to thread an edge. For example,
563 it handles lazily building the dummy condition and the bookkeeping
564 when jump threading is successful. */
565
566static void
567dom_thread_across_edge (struct dom_walk_data *walk_data, edge e)
568{
569 /* If we don't already have a dummy condition, build it now. */
570 if (! walk_data->global_data)
571 {
bc4632d4 572 tree dummy_cond = build2 (NE_EXPR, boolean_type_node,
2090d6a0
JL
573 integer_zero_node, integer_zero_node);
574 dummy_cond = build3 (COND_EXPR, void_type_node, dummy_cond, NULL, NULL);
575 walk_data->global_data = dummy_cond;
576 }
577
578 thread_across_edge (walk_data->global_data, e, false,
579 &const_and_copies_stack,
580 simplify_stmt_for_jump_threading);
581}
582
6de9cd9a
DN
583/* We have finished processing the dominator children of BB, perform
584 any finalization actions in preparation for leaving this node in
585 the dominator tree. */
586
587static void
588dom_opt_finalize_block (struct dom_walk_data *walk_data, basic_block bb)
589{
6de9cd9a
DN
590 tree last;
591
2090d6a0 592
3e352c00
JL
593 /* If we have an outgoing edge to a block with multiple incoming and
594 outgoing edges, then we may be able to thread the edge. ie, we
595 may be able to statically determine which of the outgoing edges
596 will be traversed when the incoming edge from BB is traversed. */
c5cbcccf
ZD
597 if (single_succ_p (bb)
598 && (single_succ_edge (bb)->flags & EDGE_ABNORMAL) == 0
2090d6a0 599 && potentially_threadable_block (single_succ (bb)))
6de9cd9a 600 {
2090d6a0 601 dom_thread_across_edge (walk_data, single_succ_edge (bb));
6de9cd9a
DN
602 }
603 else if ((last = last_stmt (bb))
604 && TREE_CODE (last) == COND_EXPR
6615c446 605 && (COMPARISON_CLASS_P (COND_EXPR_COND (last))
6de9cd9a 606 || TREE_CODE (COND_EXPR_COND (last)) == SSA_NAME)
628f6a4e
BE
607 && EDGE_COUNT (bb->succs) == 2
608 && (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL) == 0
609 && (EDGE_SUCC (bb, 1)->flags & EDGE_ABNORMAL) == 0)
6de9cd9a
DN
610 {
611 edge true_edge, false_edge;
6de9cd9a
DN
612
613 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
614
3e352c00
JL
615 /* Only try to thread the edge if it reaches a target block with
616 more than one predecessor and more than one successor. */
2090d6a0 617 if (potentially_threadable_block (true_edge->dest))
6de9cd9a 618 {
efea75f9
JL
619 struct edge_info *edge_info;
620 unsigned int i;
621
48732f23
JL
622 /* Push a marker onto the available expression stack so that we
623 unwind any expressions related to the TRUE arm before processing
624 the false arm below. */
d4e6fecb 625 VEC_safe_push (tree, heap, avail_exprs_stack, NULL_TREE);
d4e6fecb 626 VEC_safe_push (tree, heap, const_and_copies_stack, NULL_TREE);
48732f23 627
e1111e8e 628 edge_info = (struct edge_info *) true_edge->aux;
efea75f9
JL
629
630 /* If we have info associated with this edge, record it into
631 our equivalency tables. */
632 if (edge_info)
6de9cd9a 633 {
efea75f9
JL
634 tree *cond_equivalences = edge_info->cond_equivalences;
635 tree lhs = edge_info->lhs;
636 tree rhs = edge_info->rhs;
637
74d2efc7
JL
638 /* If we have a simple NAME = VALUE equivalency record it. */
639 if (lhs && TREE_CODE (lhs) == SSA_NAME)
efea75f9
JL
640 record_const_or_copy (lhs, rhs);
641
642 /* If we have 0 = COND or 1 = COND equivalences, record them
643 into our expression hash tables. */
644 if (cond_equivalences)
645 for (i = 0; i < edge_info->max_cond_equivalences; i += 2)
646 {
647 tree expr = cond_equivalences[i];
648 tree value = cond_equivalences[i + 1];
649
650 record_cond (expr, value);
651 }
6de9cd9a 652 }
6de9cd9a 653
2090d6a0 654 dom_thread_across_edge (walk_data, true_edge);
6de9cd9a
DN
655
656 /* And restore the various tables to their state before
657 we threaded this edge. */
48732f23 658 remove_local_expressions_from_table ();
6de9cd9a
DN
659 }
660
661 /* Similarly for the ELSE arm. */
2090d6a0 662 if (potentially_threadable_block (false_edge->dest))
6de9cd9a 663 {
efea75f9
JL
664 struct edge_info *edge_info;
665 unsigned int i;
666
2090d6a0 667 VEC_safe_push (tree, heap, const_and_copies_stack, NULL_TREE);
e1111e8e 668 edge_info = (struct edge_info *) false_edge->aux;
efea75f9
JL
669
670 /* If we have info associated with this edge, record it into
671 our equivalency tables. */
672 if (edge_info)
6de9cd9a 673 {
efea75f9
JL
674 tree *cond_equivalences = edge_info->cond_equivalences;
675 tree lhs = edge_info->lhs;
676 tree rhs = edge_info->rhs;
677
74d2efc7
JL
678 /* If we have a simple NAME = VALUE equivalency record it. */
679 if (lhs && TREE_CODE (lhs) == SSA_NAME)
efea75f9
JL
680 record_const_or_copy (lhs, rhs);
681
682 /* If we have 0 = COND or 1 = COND equivalences, record them
683 into our expression hash tables. */
684 if (cond_equivalences)
685 for (i = 0; i < edge_info->max_cond_equivalences; i += 2)
686 {
687 tree expr = cond_equivalences[i];
688 tree value = cond_equivalences[i + 1];
689
690 record_cond (expr, value);
691 }
6de9cd9a 692 }
6de9cd9a 693
2090d6a0
JL
694 /* Now thread the edge. */
695 dom_thread_across_edge (walk_data, false_edge);
6de9cd9a
DN
696
697 /* No need to remove local expressions from our tables
698 or restore vars to their original value as that will
699 be done immediately below. */
700 }
701 }
702
48732f23 703 remove_local_expressions_from_table ();
b5fefcf6 704 restore_vars_to_original_value ();
6de9cd9a 705
a6e1aa26
JL
706 /* If we queued any statements to rescan in this block, then
707 go ahead and rescan them now. */
cfaab3a9 708 while (VEC_length (tree_p, stmts_to_rescan) > 0)
6de9cd9a 709 {
cfaab3a9
DN
710 tree *stmt_p = VEC_last (tree_p, stmts_to_rescan);
711 tree stmt = *stmt_p;
a6e1aa26
JL
712 basic_block stmt_bb = bb_for_stmt (stmt);
713
714 if (stmt_bb != bb)
715 break;
716
cfaab3a9
DN
717 VEC_pop (tree_p, stmts_to_rescan);
718 pop_stmt_changes (stmt_p);
6de9cd9a
DN
719 }
720}
721
722/* PHI nodes can create equivalences too.
723
724 Ignoring any alternatives which are the same as the result, if
725 all the alternatives are equal, then the PHI node creates an
b16caf72 726 equivalence. */
dd747311 727
6de9cd9a 728static void
efea75f9 729record_equivalences_from_phis (basic_block bb)
6de9cd9a 730{
6de9cd9a
DN
731 tree phi;
732
17192884 733 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
6de9cd9a
DN
734 {
735 tree lhs = PHI_RESULT (phi);
736 tree rhs = NULL;
737 int i;
738
739 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
740 {
741 tree t = PHI_ARG_DEF (phi, i);
742
6e38fea3
KH
743 /* Ignore alternatives which are the same as our LHS. Since
744 LHS is a PHI_RESULT, it is known to be a SSA_NAME, so we
745 can simply compare pointers. */
073b8140 746 if (lhs == t)
a18428f3
KH
747 continue;
748
749 /* If we have not processed an alternative yet, then set
750 RHS to this alternative. */
751 if (rhs == NULL)
752 rhs = t;
753 /* If we have processed an alternative (stored in RHS), then
754 see if it is equal to this one. If it isn't, then stop
755 the search. */
756 else if (! operand_equal_for_phi_arg_p (rhs, t))
6de9cd9a
DN
757 break;
758 }
759
760 /* If we had no interesting alternatives, then all the RHS alternatives
761 must have been the same as LHS. */
762 if (!rhs)
763 rhs = lhs;
764
765 /* If we managed to iterate through each PHI alternative without
766 breaking out of the loop, then we have a PHI which may create
767 a useful equivalence. We do not need to record unwind data for
768 this, since this is a true assignment and not an equivalence
1ea7e6ad 769 inferred from a comparison. All uses of this ssa name are dominated
6de9cd9a
DN
770 by this assignment, so unwinding just costs time and space. */
771 if (i == PHI_NUM_ARGS (phi)
772 && may_propagate_copy (lhs, rhs))
3aecd08b 773 SSA_NAME_VALUE (lhs) = rhs;
6de9cd9a
DN
774 }
775}
776
28c008bb
JL
777/* Ignoring loop backedges, if BB has precisely one incoming edge then
778 return that edge. Otherwise return NULL. */
779static edge
780single_incoming_edge_ignoring_loop_edges (basic_block bb)
781{
782 edge retval = NULL;
783 edge e;
628f6a4e 784 edge_iterator ei;
28c008bb 785
628f6a4e 786 FOR_EACH_EDGE (e, ei, bb->preds)
28c008bb
JL
787 {
788 /* A loop back edge can be identified by the destination of
789 the edge dominating the source of the edge. */
790 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
791 continue;
792
793 /* If we have already seen a non-loop edge, then we must have
794 multiple incoming non-loop edges and thus we return NULL. */
795 if (retval)
796 return NULL;
797
798 /* This is the first non-loop incoming edge we have found. Record
799 it. */
800 retval = e;
801 }
802
803 return retval;
804}
805
6de9cd9a
DN
806/* Record any equivalences created by the incoming edge to BB. If BB
807 has more than one incoming edge, then no equivalence is created. */
808
809static void
efea75f9 810record_equivalences_from_incoming_edge (basic_block bb)
6de9cd9a 811{
efea75f9 812 edge e;
6de9cd9a 813 basic_block parent;
efea75f9 814 struct edge_info *edge_info;
6de9cd9a 815
35fd3193 816 /* If our parent block ended with a control statement, then we may be
6de9cd9a
DN
817 able to record some equivalences based on which outgoing edge from
818 the parent was followed. */
819 parent = get_immediate_dominator (CDI_DOMINATORS, bb);
6de9cd9a 820
efea75f9 821 e = single_incoming_edge_ignoring_loop_edges (bb);
6de9cd9a 822
efea75f9
JL
823 /* If we had a single incoming edge from our parent block, then enter
824 any data associated with the edge into our tables. */
825 if (e && e->src == parent)
6de9cd9a 826 {
efea75f9 827 unsigned int i;
6de9cd9a 828
e1111e8e 829 edge_info = (struct edge_info *) e->aux;
6de9cd9a 830
efea75f9 831 if (edge_info)
6de9cd9a 832 {
efea75f9
JL
833 tree lhs = edge_info->lhs;
834 tree rhs = edge_info->rhs;
835 tree *cond_equivalences = edge_info->cond_equivalences;
836
837 if (lhs)
838 record_equality (lhs, rhs);
839
840 if (cond_equivalences)
6de9cd9a 841 {
efea75f9 842 for (i = 0; i < edge_info->max_cond_equivalences; i += 2)
6de9cd9a 843 {
efea75f9
JL
844 tree expr = cond_equivalences[i];
845 tree value = cond_equivalences[i + 1];
846
847 record_cond (expr, value);
6de9cd9a
DN
848 }
849 }
6de9cd9a
DN
850 }
851 }
6de9cd9a
DN
852}
853
854/* Dump SSA statistics on FILE. */
855
856void
857dump_dominator_optimization_stats (FILE *file)
858{
859 long n_exprs;
860
861 fprintf (file, "Total number of statements: %6ld\n\n",
862 opt_stats.num_stmts);
863 fprintf (file, "Exprs considered for dominator optimizations: %6ld\n",
864 opt_stats.num_exprs_considered);
865
866 n_exprs = opt_stats.num_exprs_considered;
867 if (n_exprs == 0)
868 n_exprs = 1;
869
870 fprintf (file, " Redundant expressions eliminated: %6ld (%.0f%%)\n",
871 opt_stats.num_re, PERCENT (opt_stats.num_re,
872 n_exprs));
0bca51f0
DN
873 fprintf (file, " Constants propagated: %6ld\n",
874 opt_stats.num_const_prop);
875 fprintf (file, " Copies propagated: %6ld\n",
876 opt_stats.num_copy_prop);
6de9cd9a
DN
877
878 fprintf (file, "\nHash table statistics:\n");
879
880 fprintf (file, " avail_exprs: ");
881 htab_statistics (file, avail_exprs);
882}
883
884
885/* Dump SSA statistics on stderr. */
886
887void
888debug_dominator_optimization_stats (void)
889{
890 dump_dominator_optimization_stats (stderr);
891}
892
893
894/* Dump statistics for the hash table HTAB. */
895
896static void
897htab_statistics (FILE *file, htab_t htab)
898{
899 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
900 (long) htab_size (htab),
901 (long) htab_elements (htab),
902 htab_collisions (htab));
903}
904
6de9cd9a
DN
905/* Enter a statement into the true/false expression hash table indicating
906 that the condition COND has the value VALUE. */
907
908static void
48732f23 909record_cond (tree cond, tree value)
6de9cd9a 910{
e1111e8e 911 struct expr_hash_elt *element = XCNEW (struct expr_hash_elt);
6de9cd9a
DN
912 void **slot;
913
914 initialize_hash_element (cond, value, element);
915
916 slot = htab_find_slot_with_hash (avail_exprs, (void *)element,
5746637c 917 element->hash, INSERT);
6de9cd9a
DN
918 if (*slot == NULL)
919 {
920 *slot = (void *) element;
d4e6fecb 921 VEC_safe_push (tree, heap, avail_exprs_stack, cond);
6de9cd9a
DN
922 }
923 else
924 free (element);
925}
926
efea75f9
JL
927/* Build a new conditional using NEW_CODE, OP0 and OP1 and store
928 the new conditional into *p, then store a boolean_true_node
3f117656 929 into *(p + 1). */
efea75f9
JL
930
931static void
932build_and_record_new_cond (enum tree_code new_code, tree op0, tree op1, tree *p)
933{
934 *p = build2 (new_code, boolean_type_node, op0, op1);
935 p++;
936 *p = boolean_true_node;
937}
938
939/* Record that COND is true and INVERTED is false into the edge information
940 structure. Also record that any conditions dominated by COND are true
941 as well.
d2d8936f
JL
942
943 For example, if a < b is true, then a <= b must also be true. */
944
945static void
efea75f9 946record_conditions (struct edge_info *edge_info, tree cond, tree inverted)
d2d8936f 947{
efea75f9
JL
948 tree op0, op1;
949
950 if (!COMPARISON_CLASS_P (cond))
951 return;
952
953 op0 = TREE_OPERAND (cond, 0);
954 op1 = TREE_OPERAND (cond, 1);
955
d2d8936f
JL
956 switch (TREE_CODE (cond))
957 {
958 case LT_EXPR:
d2d8936f 959 case GT_EXPR:
14b41b5f
RS
960 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
961 {
962 edge_info->max_cond_equivalences = 12;
963 edge_info->cond_equivalences = XNEWVEC (tree, 12);
964 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
965 &edge_info->cond_equivalences[8]);
966 build_and_record_new_cond (LTGT_EXPR, op0, op1,
967 &edge_info->cond_equivalences[10]);
968 }
969 else
970 {
971 edge_info->max_cond_equivalences = 8;
972 edge_info->cond_equivalences = XNEWVEC (tree, 8);
973 }
974
efea75f9
JL
975 build_and_record_new_cond ((TREE_CODE (cond) == LT_EXPR
976 ? LE_EXPR : GE_EXPR),
977 op0, op1, &edge_info->cond_equivalences[4]);
efea75f9 978 build_and_record_new_cond (NE_EXPR, op0, op1,
14b41b5f 979 &edge_info->cond_equivalences[6]);
d2d8936f
JL
980 break;
981
982 case GE_EXPR:
983 case LE_EXPR:
14b41b5f
RS
984 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
985 {
986 edge_info->max_cond_equivalences = 6;
987 edge_info->cond_equivalences = XNEWVEC (tree, 6);
988 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
989 &edge_info->cond_equivalences[4]);
990 }
991 else
992 {
993 edge_info->max_cond_equivalences = 4;
994 edge_info->cond_equivalences = XNEWVEC (tree, 4);
995 }
d2d8936f
JL
996 break;
997
998 case EQ_EXPR:
14b41b5f
RS
999 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
1000 {
1001 edge_info->max_cond_equivalences = 10;
1002 edge_info->cond_equivalences = XNEWVEC (tree, 10);
1003 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1004 &edge_info->cond_equivalences[8]);
1005 }
1006 else
1007 {
1008 edge_info->max_cond_equivalences = 8;
1009 edge_info->cond_equivalences = XNEWVEC (tree, 8);
1010 }
efea75f9 1011 build_and_record_new_cond (LE_EXPR, op0, op1,
14b41b5f 1012 &edge_info->cond_equivalences[4]);
efea75f9 1013 build_and_record_new_cond (GE_EXPR, op0, op1,
14b41b5f 1014 &edge_info->cond_equivalences[6]);
d2d8936f
JL
1015 break;
1016
1017 case UNORDERED_EXPR:
efea75f9 1018 edge_info->max_cond_equivalences = 16;
e1111e8e 1019 edge_info->cond_equivalences = XNEWVEC (tree, 16);
efea75f9
JL
1020 build_and_record_new_cond (NE_EXPR, op0, op1,
1021 &edge_info->cond_equivalences[4]);
1022 build_and_record_new_cond (UNLE_EXPR, op0, op1,
1023 &edge_info->cond_equivalences[6]);
1024 build_and_record_new_cond (UNGE_EXPR, op0, op1,
1025 &edge_info->cond_equivalences[8]);
1026 build_and_record_new_cond (UNEQ_EXPR, op0, op1,
1027 &edge_info->cond_equivalences[10]);
1028 build_and_record_new_cond (UNLT_EXPR, op0, op1,
1029 &edge_info->cond_equivalences[12]);
1030 build_and_record_new_cond (UNGT_EXPR, op0, op1,
1031 &edge_info->cond_equivalences[14]);
d2d8936f
JL
1032 break;
1033
1034 case UNLT_EXPR:
d2d8936f 1035 case UNGT_EXPR:
efea75f9 1036 edge_info->max_cond_equivalences = 8;
e1111e8e 1037 edge_info->cond_equivalences = XNEWVEC (tree, 8);
efea75f9
JL
1038 build_and_record_new_cond ((TREE_CODE (cond) == UNLT_EXPR
1039 ? UNLE_EXPR : UNGE_EXPR),
1040 op0, op1, &edge_info->cond_equivalences[4]);
1041 build_and_record_new_cond (NE_EXPR, op0, op1,
1042 &edge_info->cond_equivalences[6]);
d2d8936f
JL
1043 break;
1044
1045 case UNEQ_EXPR:
efea75f9 1046 edge_info->max_cond_equivalences = 8;
e1111e8e 1047 edge_info->cond_equivalences = XNEWVEC (tree, 8);
efea75f9
JL
1048 build_and_record_new_cond (UNLE_EXPR, op0, op1,
1049 &edge_info->cond_equivalences[4]);
1050 build_and_record_new_cond (UNGE_EXPR, op0, op1,
1051 &edge_info->cond_equivalences[6]);
d2d8936f
JL
1052 break;
1053
1054 case LTGT_EXPR:
efea75f9 1055 edge_info->max_cond_equivalences = 8;
e1111e8e 1056 edge_info->cond_equivalences = XNEWVEC (tree, 8);
efea75f9
JL
1057 build_and_record_new_cond (NE_EXPR, op0, op1,
1058 &edge_info->cond_equivalences[4]);
1059 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1060 &edge_info->cond_equivalences[6]);
1061 break;
d2d8936f
JL
1062
1063 default:
efea75f9 1064 edge_info->max_cond_equivalences = 4;
e1111e8e 1065 edge_info->cond_equivalences = XNEWVEC (tree, 4);
d2d8936f
JL
1066 break;
1067 }
efea75f9
JL
1068
1069 /* Now store the original true and false conditions into the first
1070 two slots. */
1071 edge_info->cond_equivalences[0] = cond;
1072 edge_info->cond_equivalences[1] = boolean_true_node;
1073 edge_info->cond_equivalences[2] = inverted;
1074 edge_info->cond_equivalences[3] = boolean_false_node;
d2d8936f
JL
1075}
1076
6de9cd9a
DN
1077/* A helper function for record_const_or_copy and record_equality.
1078 Do the work of recording the value and undo info. */
1079
1080static void
b5fefcf6 1081record_const_or_copy_1 (tree x, tree y, tree prev_x)
6de9cd9a 1082{
3aecd08b 1083 SSA_NAME_VALUE (x) = y;
6de9cd9a 1084
d4e6fecb
NS
1085 VEC_reserve (tree, heap, const_and_copies_stack, 2);
1086 VEC_quick_push (tree, const_and_copies_stack, prev_x);
1087 VEC_quick_push (tree, const_and_copies_stack, x);
6de9cd9a
DN
1088}
1089
84dd478f
DB
1090
1091/* Return the loop depth of the basic block of the defining statement of X.
1092 This number should not be treated as absolutely correct because the loop
1093 information may not be completely up-to-date when dom runs. However, it
1094 will be relatively correct, and as more passes are taught to keep loop info
1095 up to date, the result will become more and more accurate. */
1096
0bca51f0 1097int
84dd478f
DB
1098loop_depth_of_name (tree x)
1099{
1100 tree defstmt;
1101 basic_block defbb;
1102
1103 /* If it's not an SSA_NAME, we have no clue where the definition is. */
1104 if (TREE_CODE (x) != SSA_NAME)
1105 return 0;
1106
1107 /* Otherwise return the loop depth of the defining statement's bb.
1108 Note that there may not actually be a bb for this statement, if the
1109 ssa_name is live on entry. */
1110 defstmt = SSA_NAME_DEF_STMT (x);
1111 defbb = bb_for_stmt (defstmt);
1112 if (!defbb)
1113 return 0;
1114
1115 return defbb->loop_depth;
1116}
1117
1118
6de9cd9a 1119/* Record that X is equal to Y in const_and_copies. Record undo
ceb7eb8f 1120 information in the block-local vector. */
6de9cd9a
DN
1121
1122static void
b5fefcf6 1123record_const_or_copy (tree x, tree y)
6de9cd9a 1124{
3aecd08b 1125 tree prev_x = SSA_NAME_VALUE (x);
6de9cd9a
DN
1126
1127 if (TREE_CODE (y) == SSA_NAME)
1128 {
3aecd08b 1129 tree tmp = SSA_NAME_VALUE (y);
6de9cd9a
DN
1130 if (tmp)
1131 y = tmp;
1132 }
1133
b5fefcf6 1134 record_const_or_copy_1 (x, y, prev_x);
6de9cd9a
DN
1135}
1136
1137/* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1138 This constrains the cases in which we may treat this as assignment. */
1139
1140static void
b5fefcf6 1141record_equality (tree x, tree y)
6de9cd9a
DN
1142{
1143 tree prev_x = NULL, prev_y = NULL;
1144
1145 if (TREE_CODE (x) == SSA_NAME)
3aecd08b 1146 prev_x = SSA_NAME_VALUE (x);
6de9cd9a 1147 if (TREE_CODE (y) == SSA_NAME)
3aecd08b 1148 prev_y = SSA_NAME_VALUE (y);
6de9cd9a 1149
84dd478f
DB
1150 /* If one of the previous values is invariant, or invariant in more loops
1151 (by depth), then use that.
6de9cd9a
DN
1152 Otherwise it doesn't matter which value we choose, just so
1153 long as we canonicalize on one value. */
1154 if (TREE_INVARIANT (y))
1155 ;
84dd478f 1156 else if (TREE_INVARIANT (x) || (loop_depth_of_name (x) <= loop_depth_of_name (y)))
6de9cd9a
DN
1157 prev_x = x, x = y, y = prev_x, prev_x = prev_y;
1158 else if (prev_x && TREE_INVARIANT (prev_x))
1159 x = y, y = prev_x, prev_x = prev_y;
3aecd08b 1160 else if (prev_y && TREE_CODE (prev_y) != VALUE_HANDLE)
6de9cd9a
DN
1161 y = prev_y;
1162
1163 /* After the swapping, we must have one SSA_NAME. */
1164 if (TREE_CODE (x) != SSA_NAME)
1165 return;
1166
1167 /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1168 variable compared against zero. If we're honoring signed zeros,
1169 then we cannot record this value unless we know that the value is
1ea7e6ad 1170 nonzero. */
6de9cd9a
DN
1171 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (x)))
1172 && (TREE_CODE (y) != REAL_CST
1173 || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (y))))
1174 return;
1175
b5fefcf6 1176 record_const_or_copy_1 (x, y, prev_x);
6de9cd9a
DN
1177}
1178
f67e783f
ZD
1179/* Returns true when STMT is a simple iv increment. It detects the
1180 following situation:
1181
1182 i_1 = phi (..., i_2)
1183 i_2 = i_1 +/- ... */
1184
1185static bool
1186simple_iv_increment_p (tree stmt)
1187{
1188 tree lhs, rhs, preinc, phi;
1189 unsigned i;
1190
07beea0d 1191 if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
f67e783f
ZD
1192 return false;
1193
07beea0d 1194 lhs = GIMPLE_STMT_OPERAND (stmt, 0);
f67e783f
ZD
1195 if (TREE_CODE (lhs) != SSA_NAME)
1196 return false;
1197
07beea0d 1198 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
f67e783f
ZD
1199
1200 if (TREE_CODE (rhs) != PLUS_EXPR
1201 && TREE_CODE (rhs) != MINUS_EXPR)
1202 return false;
1203
1204 preinc = TREE_OPERAND (rhs, 0);
1205 if (TREE_CODE (preinc) != SSA_NAME)
1206 return false;
1207
1208 phi = SSA_NAME_DEF_STMT (preinc);
1209 if (TREE_CODE (phi) != PHI_NODE)
1210 return false;
1211
1212 for (i = 0; i < (unsigned) PHI_NUM_ARGS (phi); i++)
1213 if (PHI_ARG_DEF (phi, i) == lhs)
1214 return true;
1215
1216 return false;
1217}
1218
ff2ad0f7
DN
1219/* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
1220 known value for that SSA_NAME (or NULL if no value is known).
1221
b16caf72
JL
1222 Propagate values from CONST_AND_COPIES into the PHI nodes of the
1223 successors of BB. */
ff2ad0f7
DN
1224
1225static void
b16caf72 1226cprop_into_successor_phis (basic_block bb)
ff2ad0f7
DN
1227{
1228 edge e;
628f6a4e 1229 edge_iterator ei;
ff2ad0f7 1230
628f6a4e 1231 FOR_EACH_EDGE (e, ei, bb->succs)
ff2ad0f7
DN
1232 {
1233 tree phi;
0492baf2 1234 int indx;
ff2ad0f7
DN
1235
1236 /* If this is an abnormal edge, then we do not want to copy propagate
1237 into the PHI alternative associated with this edge. */
1238 if (e->flags & EDGE_ABNORMAL)
1239 continue;
1240
1241 phi = phi_nodes (e->dest);
1242 if (! phi)
1243 continue;
1244
0492baf2 1245 indx = e->dest_idx;
ff2ad0f7
DN
1246 for ( ; phi; phi = PHI_CHAIN (phi))
1247 {
ff2ad0f7
DN
1248 tree new;
1249 use_operand_p orig_p;
1250 tree orig;
1251
ff2ad0f7
DN
1252 /* The alternative may be associated with a constant, so verify
1253 it is an SSA_NAME before doing anything with it. */
0492baf2 1254 orig_p = PHI_ARG_DEF_PTR (phi, indx);
ff2ad0f7
DN
1255 orig = USE_FROM_PTR (orig_p);
1256 if (TREE_CODE (orig) != SSA_NAME)
1257 continue;
1258
ff2ad0f7
DN
1259 /* If we have *ORIG_P in our constant/copy table, then replace
1260 ORIG_P with its value in our constant/copy table. */
3aecd08b 1261 new = SSA_NAME_VALUE (orig);
ff2ad0f7 1262 if (new
0bca51f0 1263 && new != orig
ff2ad0f7
DN
1264 && (TREE_CODE (new) == SSA_NAME
1265 || is_gimple_min_invariant (new))
1266 && may_propagate_copy (orig, new))
0bca51f0 1267 propagate_value (orig_p, new);
ff2ad0f7
DN
1268 }
1269 }
1270}
1271
efea75f9
JL
1272/* We have finished optimizing BB, record any information implied by
1273 taking a specific outgoing edge from BB. */
1274
1275static void
1276record_edge_info (basic_block bb)
1277{
1278 block_stmt_iterator bsi = bsi_last (bb);
1279 struct edge_info *edge_info;
1280
1281 if (! bsi_end_p (bsi))
1282 {
1283 tree stmt = bsi_stmt (bsi);
1284
1285 if (stmt && TREE_CODE (stmt) == SWITCH_EXPR)
1286 {
1287 tree cond = SWITCH_COND (stmt);
1288
1289 if (TREE_CODE (cond) == SSA_NAME)
1290 {
1291 tree labels = SWITCH_LABELS (stmt);
1292 int i, n_labels = TREE_VEC_LENGTH (labels);
e1111e8e 1293 tree *info = XCNEWVEC (tree, last_basic_block);
efea75f9
JL
1294 edge e;
1295 edge_iterator ei;
1296
1297 for (i = 0; i < n_labels; i++)
1298 {
1299 tree label = TREE_VEC_ELT (labels, i);
1300 basic_block target_bb = label_to_block (CASE_LABEL (label));
1301
1302 if (CASE_HIGH (label)
1303 || !CASE_LOW (label)
1304 || info[target_bb->index])
1305 info[target_bb->index] = error_mark_node;
1306 else
1307 info[target_bb->index] = label;
1308 }
1309
1310 FOR_EACH_EDGE (e, ei, bb->succs)
1311 {
1312 basic_block target_bb = e->dest;
1313 tree node = info[target_bb->index];
ff2ad0f7 1314
efea75f9
JL
1315 if (node != NULL && node != error_mark_node)
1316 {
1317 tree x = fold_convert (TREE_TYPE (cond), CASE_LOW (node));
1318 edge_info = allocate_edge_info (e);
1319 edge_info->lhs = cond;
1320 edge_info->rhs = x;
1321 }
1322 }
1323 free (info);
1324 }
1325 }
1326
1327 /* A COND_EXPR may create equivalences too. */
1328 if (stmt && TREE_CODE (stmt) == COND_EXPR)
1329 {
1330 tree cond = COND_EXPR_COND (stmt);
1331 edge true_edge;
1332 edge false_edge;
1333
1334 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1335
cbb1cada 1336 /* If the conditional is a single variable 'X', record 'X = 1'
efea75f9
JL
1337 for the true edge and 'X = 0' on the false edge. */
1338 if (SSA_VAR_P (cond))
1339 {
1340 struct edge_info *edge_info;
1341
1342 edge_info = allocate_edge_info (true_edge);
1343 edge_info->lhs = cond;
1344 edge_info->rhs = constant_boolean_node (1, TREE_TYPE (cond));
1345
1346 edge_info = allocate_edge_info (false_edge);
1347 edge_info->lhs = cond;
1348 edge_info->rhs = constant_boolean_node (0, TREE_TYPE (cond));
1349 }
1350 /* Equality tests may create one or two equivalences. */
1351 else if (COMPARISON_CLASS_P (cond))
1352 {
1353 tree op0 = TREE_OPERAND (cond, 0);
1354 tree op1 = TREE_OPERAND (cond, 1);
1355
1356 /* Special case comparing booleans against a constant as we
1357 know the value of OP0 on both arms of the branch. i.e., we
1358 can record an equivalence for OP0 rather than COND. */
1359 if ((TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
1360 && TREE_CODE (op0) == SSA_NAME
1361 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
1362 && is_gimple_min_invariant (op1))
1363 {
1364 if (TREE_CODE (cond) == EQ_EXPR)
1365 {
1366 edge_info = allocate_edge_info (true_edge);
1367 edge_info->lhs = op0;
1368 edge_info->rhs = (integer_zerop (op1)
1369 ? boolean_false_node
1370 : boolean_true_node);
1371
1372 edge_info = allocate_edge_info (false_edge);
1373 edge_info->lhs = op0;
1374 edge_info->rhs = (integer_zerop (op1)
1375 ? boolean_true_node
1376 : boolean_false_node);
1377 }
1378 else
1379 {
1380 edge_info = allocate_edge_info (true_edge);
1381 edge_info->lhs = op0;
1382 edge_info->rhs = (integer_zerop (op1)
1383 ? boolean_true_node
1384 : boolean_false_node);
1385
1386 edge_info = allocate_edge_info (false_edge);
1387 edge_info->lhs = op0;
1388 edge_info->rhs = (integer_zerop (op1)
1389 ? boolean_false_node
1390 : boolean_true_node);
1391 }
1392 }
1393
3bed147c
KH
1394 else if (is_gimple_min_invariant (op0)
1395 && (TREE_CODE (op1) == SSA_NAME
1396 || is_gimple_min_invariant (op1)))
efea75f9
JL
1397 {
1398 tree inverted = invert_truthvalue (cond);
1399 struct edge_info *edge_info;
1400
1401 edge_info = allocate_edge_info (true_edge);
1402 record_conditions (edge_info, cond, inverted);
1403
1404 if (TREE_CODE (cond) == EQ_EXPR)
1405 {
1406 edge_info->lhs = op1;
1407 edge_info->rhs = op0;
1408 }
1409
1410 edge_info = allocate_edge_info (false_edge);
1411 record_conditions (edge_info, inverted, cond);
1412
1413 if (TREE_CODE (cond) == NE_EXPR)
1414 {
1415 edge_info->lhs = op1;
1416 edge_info->rhs = op0;
1417 }
1418 }
1419
3bed147c
KH
1420 else if (TREE_CODE (op0) == SSA_NAME
1421 && (is_gimple_min_invariant (op1)
1422 || TREE_CODE (op1) == SSA_NAME))
efea75f9
JL
1423 {
1424 tree inverted = invert_truthvalue (cond);
1425 struct edge_info *edge_info;
1426
1427 edge_info = allocate_edge_info (true_edge);
1428 record_conditions (edge_info, cond, inverted);
1429
1430 if (TREE_CODE (cond) == EQ_EXPR)
1431 {
1432 edge_info->lhs = op0;
1433 edge_info->rhs = op1;
1434 }
1435
1436 edge_info = allocate_edge_info (false_edge);
1437 record_conditions (edge_info, inverted, cond);
1438
1439 if (TREE_CODE (cond) == NE_EXPR)
1440 {
1441 edge_info->lhs = op0;
1442 edge_info->rhs = op1;
1443 }
1444 }
1445 }
1446
1447 /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
1448 }
1449 }
1450}
1451
1452/* Propagate information from BB to its outgoing edges.
1453
1454 This can include equivalency information implied by control statements
1455 at the end of BB and const/copy propagation into PHIs in BB's
1456 successor blocks. */
6de9cd9a
DN
1457
1458static void
efea75f9
JL
1459propagate_to_outgoing_edges (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1460 basic_block bb)
6de9cd9a 1461{
efea75f9 1462 record_edge_info (bb);
b16caf72 1463 cprop_into_successor_phis (bb);
6de9cd9a
DN
1464}
1465
1466/* Search for redundant computations in STMT. If any are found, then
1467 replace them with the variable holding the result of the computation.
1468
1469 If safe, record this expression into the available expression hash
1470 table. */
1471
1472static bool
487bf3e6 1473eliminate_redundant_computations (tree stmt)
6de9cd9a 1474{
6de9cd9a
DN
1475 tree *expr_p, def = NULL_TREE;
1476 bool insert = true;
1477 tree cached_lhs;
1478 bool retval = false;
019b02f1 1479 bool modify_expr_p = false;
6de9cd9a 1480
07beea0d
AH
1481 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
1482 def = GIMPLE_STMT_OPERAND (stmt, 0);
6de9cd9a
DN
1483
1484 /* Certain expressions on the RHS can be optimized away, but can not
471854f8 1485 themselves be entered into the hash tables. */
ff88c5aa 1486 if (! def
6de9cd9a
DN
1487 || TREE_CODE (def) != SSA_NAME
1488 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)
38635499 1489 || !ZERO_SSA_OPERANDS (stmt, SSA_OP_VDEF)
f67e783f
ZD
1490 /* Do not record equivalences for increments of ivs. This would create
1491 overlapping live ranges for a very questionable gain. */
1492 || simple_iv_increment_p (stmt))
6de9cd9a
DN
1493 insert = false;
1494
1495 /* Check if the expression has been computed before. */
48732f23 1496 cached_lhs = lookup_avail_expr (stmt, insert);
6de9cd9a 1497
6de9cd9a
DN
1498 opt_stats.num_exprs_considered++;
1499
1500 /* Get a pointer to the expression we are trying to optimize. */
1501 if (TREE_CODE (stmt) == COND_EXPR)
1502 expr_p = &COND_EXPR_COND (stmt);
1503 else if (TREE_CODE (stmt) == SWITCH_EXPR)
1504 expr_p = &SWITCH_COND (stmt);
1505 else if (TREE_CODE (stmt) == RETURN_EXPR && TREE_OPERAND (stmt, 0))
019b02f1 1506 {
07beea0d 1507 expr_p = &GIMPLE_STMT_OPERAND (TREE_OPERAND (stmt, 0), 1);
019b02f1
AP
1508 modify_expr_p = true;
1509 }
6de9cd9a 1510 else
019b02f1 1511 {
07beea0d 1512 expr_p = &GENERIC_TREE_OPERAND (stmt, 1);
019b02f1
AP
1513 modify_expr_p = true;
1514 }
6de9cd9a
DN
1515
1516 /* It is safe to ignore types here since we have already done
1517 type checking in the hashing and equality routines. In fact
1518 type checking here merely gets in the way of constant
1519 propagation. Also, make sure that it is safe to propagate
1520 CACHED_LHS into *EXPR_P. */
1521 if (cached_lhs
019b02f1
AP
1522 && ((TREE_CODE (cached_lhs) != SSA_NAME
1523 && (modify_expr_p
1524 || tree_ssa_useless_type_conversion_1 (TREE_TYPE (*expr_p),
1525 TREE_TYPE (cached_lhs))))
ff2ad0f7 1526 || may_propagate_copy (*expr_p, cached_lhs)))
6de9cd9a
DN
1527 {
1528 if (dump_file && (dump_flags & TDF_DETAILS))
1529 {
1530 fprintf (dump_file, " Replaced redundant expr '");
1531 print_generic_expr (dump_file, *expr_p, dump_flags);
1532 fprintf (dump_file, "' with '");
1533 print_generic_expr (dump_file, cached_lhs, dump_flags);
1534 fprintf (dump_file, "'\n");
1535 }
1536
1537 opt_stats.num_re++;
1538
1539#if defined ENABLE_CHECKING
1e128c5f
GB
1540 gcc_assert (TREE_CODE (cached_lhs) == SSA_NAME
1541 || is_gimple_min_invariant (cached_lhs));
6de9cd9a
DN
1542#endif
1543
1544 if (TREE_CODE (cached_lhs) == ADDR_EXPR
1545 || (POINTER_TYPE_P (TREE_TYPE (*expr_p))
1546 && is_gimple_min_invariant (cached_lhs)))
1547 retval = true;
019b02f1
AP
1548
1549 if (modify_expr_p
1550 && !tree_ssa_useless_type_conversion_1 (TREE_TYPE (*expr_p),
1551 TREE_TYPE (cached_lhs)))
1552 cached_lhs = fold_convert (TREE_TYPE (*expr_p), cached_lhs);
6de9cd9a 1553
d00ad49b 1554 propagate_tree_value (expr_p, cached_lhs);
f430bae8 1555 mark_stmt_modified (stmt);
6de9cd9a
DN
1556 }
1557 return retval;
1558}
1559
07beea0d 1560/* STMT, a GIMPLE_MODIFY_STMT, may create certain equivalences, in either
6de9cd9a
DN
1561 the available expressions table or the const_and_copies table.
1562 Detect and record those equivalences. */
1563
1564static void
cfaab3a9 1565record_equivalences_from_stmt (tree stmt, int may_optimize_p, stmt_ann_t ann)
6de9cd9a 1566{
07beea0d 1567 tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
6de9cd9a 1568 enum tree_code lhs_code = TREE_CODE (lhs);
6de9cd9a
DN
1569
1570 if (lhs_code == SSA_NAME)
1571 {
07beea0d 1572 tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
6de9cd9a
DN
1573
1574 /* Strip away any useless type conversions. */
1575 STRIP_USELESS_TYPE_CONVERSION (rhs);
1576
1577 /* If the RHS of the assignment is a constant or another variable that
1578 may be propagated, register it in the CONST_AND_COPIES table. We
1579 do not need to record unwind data for this, since this is a true
1ea7e6ad 1580 assignment and not an equivalence inferred from a comparison. All
6de9cd9a
DN
1581 uses of this ssa name are dominated by this assignment, so unwinding
1582 just costs time and space. */
1583 if (may_optimize_p
1584 && (TREE_CODE (rhs) == SSA_NAME
1585 || is_gimple_min_invariant (rhs)))
3aecd08b 1586 SSA_NAME_VALUE (lhs) = rhs;
6de9cd9a
DN
1587 }
1588
6de9cd9a
DN
1589 /* A memory store, even an aliased store, creates a useful
1590 equivalence. By exchanging the LHS and RHS, creating suitable
1591 vops and recording the result in the available expression table,
1592 we may be able to expose more redundant loads. */
1593 if (!ann->has_volatile_ops
cfaab3a9 1594 && stmt_references_memory_p (stmt)
07beea0d
AH
1595 && (TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) == SSA_NAME
1596 || is_gimple_min_invariant (GIMPLE_STMT_OPERAND (stmt, 1)))
6de9cd9a
DN
1597 && !is_gimple_reg (lhs))
1598 {
07beea0d 1599 tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
6de9cd9a 1600 tree new;
6de9cd9a
DN
1601
1602 /* FIXME: If the LHS of the assignment is a bitfield and the RHS
1603 is a constant, we need to adjust the constant to fit into the
1604 type of the LHS. If the LHS is a bitfield and the RHS is not
1605 a constant, then we can not record any equivalences for this
1606 statement since we would need to represent the widening or
1607 narrowing of RHS. This fixes gcc.c-torture/execute/921016-1.c
1608 and should not be necessary if GCC represented bitfields
1609 properly. */
1610 if (lhs_code == COMPONENT_REF
1611 && DECL_BIT_FIELD (TREE_OPERAND (lhs, 1)))
1612 {
1613 if (TREE_CONSTANT (rhs))
1614 rhs = widen_bitfield (rhs, TREE_OPERAND (lhs, 1), lhs);
1615 else
1616 rhs = NULL;
1617
1618 /* If the value overflowed, then we can not use this equivalence. */
1619 if (rhs && ! is_gimple_min_invariant (rhs))
1620 rhs = NULL;
1621 }
1622
1623 if (rhs)
1624 {
6de9cd9a 1625 /* Build a new statement with the RHS and LHS exchanged. */
939409af 1626 new = build_gimple_modify_stmt (rhs, lhs);
6de9cd9a 1627
cfaab3a9 1628 create_ssa_artificial_load_stmt (new, stmt);
6de9cd9a
DN
1629
1630 /* Finally enter the statement into the available expression
1631 table. */
48732f23 1632 lookup_avail_expr (new, true);
6de9cd9a
DN
1633 }
1634 }
1635}
1636
ff2ad0f7
DN
1637/* Replace *OP_P in STMT with any known equivalent value for *OP_P from
1638 CONST_AND_COPIES. */
1639
1640static bool
6f2aec07 1641cprop_operand (tree stmt, use_operand_p op_p)
ff2ad0f7
DN
1642{
1643 bool may_have_exposed_new_symbols = false;
1644 tree val;
1645 tree op = USE_FROM_PTR (op_p);
1646
1647 /* If the operand has a known constant value or it is known to be a
1648 copy of some other variable, use the value or copy stored in
1649 CONST_AND_COPIES. */
3aecd08b 1650 val = SSA_NAME_VALUE (op);
0bca51f0 1651 if (val && val != op && TREE_CODE (val) != VALUE_HANDLE)
ff2ad0f7
DN
1652 {
1653 tree op_type, val_type;
1654
1655 /* Do not change the base variable in the virtual operand
1656 tables. That would make it impossible to reconstruct
1657 the renamed virtual operand if we later modify this
1658 statement. Also only allow the new value to be an SSA_NAME
1659 for propagation into virtual operands. */
1660 if (!is_gimple_reg (op)
0bca51f0
DN
1661 && (TREE_CODE (val) != SSA_NAME
1662 || is_gimple_reg (val)
1663 || get_virtual_var (val) != get_virtual_var (op)))
ff2ad0f7
DN
1664 return false;
1665
aa24864c
RH
1666 /* Do not replace hard register operands in asm statements. */
1667 if (TREE_CODE (stmt) == ASM_EXPR
1668 && !may_propagate_copy_into_asm (op))
1669 return false;
1670
ff2ad0f7
DN
1671 /* Get the toplevel type of each operand. */
1672 op_type = TREE_TYPE (op);
1673 val_type = TREE_TYPE (val);
1674
1675 /* While both types are pointers, get the type of the object
1676 pointed to. */
1677 while (POINTER_TYPE_P (op_type) && POINTER_TYPE_P (val_type))
1678 {
1679 op_type = TREE_TYPE (op_type);
1680 val_type = TREE_TYPE (val_type);
1681 }
1682
63b88252
RH
1683 /* Make sure underlying types match before propagating a constant by
1684 converting the constant to the proper type. Note that convert may
1685 return a non-gimple expression, in which case we ignore this
1686 propagation opportunity. */
1687 if (TREE_CODE (val) != SSA_NAME)
ff2ad0f7 1688 {
63b88252
RH
1689 if (!lang_hooks.types_compatible_p (op_type, val_type))
1690 {
1691 val = fold_convert (TREE_TYPE (op), val);
1692 if (!is_gimple_min_invariant (val))
1693 return false;
1694 }
ff2ad0f7
DN
1695 }
1696
1697 /* Certain operands are not allowed to be copy propagated due
1698 to their interaction with exception handling and some GCC
1699 extensions. */
63b88252 1700 else if (!may_propagate_copy (op, val))
ff2ad0f7 1701 return false;
111e0c9f
DB
1702
1703 /* Do not propagate copies if the propagated value is at a deeper loop
1704 depth than the propagatee. Otherwise, this may move loop variant
1705 variables outside of their loops and prevent coalescing
1706 opportunities. If the value was loop invariant, it will be hoisted
1707 by LICM and exposed for copy propagation. */
1708 if (loop_depth_of_name (val) > loop_depth_of_name (op))
1709 return false;
ff2ad0f7
DN
1710
1711 /* Dump details. */
1712 if (dump_file && (dump_flags & TDF_DETAILS))
1713 {
1714 fprintf (dump_file, " Replaced '");
1715 print_generic_expr (dump_file, op, dump_flags);
1716 fprintf (dump_file, "' with %s '",
1717 (TREE_CODE (val) != SSA_NAME ? "constant" : "variable"));
1718 print_generic_expr (dump_file, val, dump_flags);
1719 fprintf (dump_file, "'\n");
1720 }
1721
1722 /* If VAL is an ADDR_EXPR or a constant of pointer type, note
1723 that we may have exposed a new symbol for SSA renaming. */
1724 if (TREE_CODE (val) == ADDR_EXPR
1725 || (POINTER_TYPE_P (TREE_TYPE (op))
1726 && is_gimple_min_invariant (val)))
1727 may_have_exposed_new_symbols = true;
1728
0bca51f0
DN
1729 if (TREE_CODE (val) != SSA_NAME)
1730 opt_stats.num_const_prop++;
1731 else
1732 opt_stats.num_copy_prop++;
1733
ff2ad0f7
DN
1734 propagate_value (op_p, val);
1735
1736 /* And note that we modified this statement. This is now
1737 safe, even if we changed virtual operands since we will
1738 rescan the statement and rewrite its operands again. */
f430bae8 1739 mark_stmt_modified (stmt);
ff2ad0f7
DN
1740 }
1741 return may_have_exposed_new_symbols;
1742}
1743
1744/* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
1745 known value for that SSA_NAME (or NULL if no value is known).
1746
1747 Propagate values from CONST_AND_COPIES into the uses, vuses and
cfaab3a9 1748 vdef_ops of STMT. */
ff2ad0f7
DN
1749
1750static bool
6f2aec07 1751cprop_into_stmt (tree stmt)
ff2ad0f7
DN
1752{
1753 bool may_have_exposed_new_symbols = false;
4c124b4c
AM
1754 use_operand_p op_p;
1755 ssa_op_iter iter;
ff2ad0f7 1756
4c124b4c 1757 FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_ALL_USES)
ff2ad0f7 1758 {
ff2ad0f7 1759 if (TREE_CODE (USE_FROM_PTR (op_p)) == SSA_NAME)
6f2aec07 1760 may_have_exposed_new_symbols |= cprop_operand (stmt, op_p);
ff2ad0f7
DN
1761 }
1762
ff2ad0f7
DN
1763 return may_have_exposed_new_symbols;
1764}
1765
1766
206048bd 1767/* Optimize the statement pointed to by iterator SI.
6de9cd9a
DN
1768
1769 We try to perform some simplistic global redundancy elimination and
1770 constant propagation:
1771
1772 1- To detect global redundancy, we keep track of expressions that have
1773 been computed in this block and its dominators. If we find that the
1774 same expression is computed more than once, we eliminate repeated
1775 computations by using the target of the first one.
1776
1777 2- Constant values and copy assignments. This is used to do very
1778 simplistic constant and copy propagation. When a constant or copy
1779 assignment is found, we map the value on the RHS of the assignment to
1780 the variable in the LHS in the CONST_AND_COPIES table. */
1781
1782static void
a513fe88
JL
1783optimize_stmt (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1784 basic_block bb, block_stmt_iterator si)
6de9cd9a
DN
1785{
1786 stmt_ann_t ann;
af47810a 1787 tree stmt, old_stmt;
6de9cd9a
DN
1788 bool may_optimize_p;
1789 bool may_have_exposed_new_symbols = false;
6de9cd9a 1790
af47810a 1791 old_stmt = stmt = bsi_stmt (si);
0e0ed594
JL
1792
1793 if (TREE_CODE (stmt) == COND_EXPR)
1794 canonicalize_comparison (stmt);
1795
f430bae8 1796 update_stmt_if_modified (stmt);
6de9cd9a 1797 ann = stmt_ann (stmt);
6de9cd9a
DN
1798 opt_stats.num_stmts++;
1799 may_have_exposed_new_symbols = false;
cfaab3a9 1800 push_stmt_changes (bsi_stmt_ptr (si));
6de9cd9a
DN
1801
1802 if (dump_file && (dump_flags & TDF_DETAILS))
1803 {
1804 fprintf (dump_file, "Optimizing statement ");
1805 print_generic_stmt (dump_file, stmt, TDF_SLIM);
1806 }
1807
cfaab3a9 1808 /* Const/copy propagate into USES, VUSES and the RHS of VDEFs. */
6f2aec07 1809 may_have_exposed_new_symbols = cprop_into_stmt (stmt);
6de9cd9a
DN
1810
1811 /* If the statement has been modified with constant replacements,
1812 fold its RHS before checking for redundant computations. */
1813 if (ann->modified)
1814 {
6cedb4ac
JL
1815 tree rhs;
1816
6de9cd9a
DN
1817 /* Try to fold the statement making sure that STMT is kept
1818 up to date. */
1819 if (fold_stmt (bsi_stmt_ptr (si)))
1820 {
1821 stmt = bsi_stmt (si);
1822 ann = stmt_ann (stmt);
1823
1824 if (dump_file && (dump_flags & TDF_DETAILS))
1825 {
1826 fprintf (dump_file, " Folded to: ");
1827 print_generic_stmt (dump_file, stmt, TDF_SLIM);
1828 }
1829 }
1830
6cedb4ac
JL
1831 rhs = get_rhs (stmt);
1832 if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
127203ac 1833 recompute_tree_invariant_for_addr_expr (rhs);
6cedb4ac 1834
6de9cd9a
DN
1835 /* Constant/copy propagation above may change the set of
1836 virtual operands associated with this statement. Folding
1837 may remove the need for some virtual operands.
1838
1839 Indicate we will need to rescan and rewrite the statement. */
1840 may_have_exposed_new_symbols = true;
1841 }
1842
1843 /* Check for redundant computations. Do this optimization only
1844 for assignments that have no volatile ops and conditionals. */
1845 may_optimize_p = (!ann->has_volatile_ops
1846 && ((TREE_CODE (stmt) == RETURN_EXPR
1847 && TREE_OPERAND (stmt, 0)
07beea0d
AH
1848 && TREE_CODE (TREE_OPERAND (stmt, 0))
1849 == GIMPLE_MODIFY_STMT
6de9cd9a 1850 && ! (TREE_SIDE_EFFECTS
07beea0d
AH
1851 (GIMPLE_STMT_OPERAND
1852 (TREE_OPERAND (stmt, 0), 1))))
1853 || (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
1854 && ! TREE_SIDE_EFFECTS (GIMPLE_STMT_OPERAND (stmt,
1855 1)))
6de9cd9a
DN
1856 || TREE_CODE (stmt) == COND_EXPR
1857 || TREE_CODE (stmt) == SWITCH_EXPR));
1858
1859 if (may_optimize_p)
487bf3e6 1860 may_have_exposed_new_symbols |= eliminate_redundant_computations (stmt);
6de9cd9a
DN
1861
1862 /* Record any additional equivalences created by this statement. */
07beea0d 1863 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
cfaab3a9 1864 record_equivalences_from_stmt (stmt, may_optimize_p, ann);
6de9cd9a 1865
6de9cd9a
DN
1866 /* If STMT is a COND_EXPR and it was modified, then we may know
1867 where it goes. If that is the case, then mark the CFG as altered.
1868
1869 This will cause us to later call remove_unreachable_blocks and
1870 cleanup_tree_cfg when it is safe to do so. It is not safe to
1871 clean things up here since removal of edges and such can trigger
1872 the removal of PHI nodes, which in turn can release SSA_NAMEs to
1873 the manager.
1874
1875 That's all fine and good, except that once SSA_NAMEs are released
1876 to the manager, we must not call create_ssa_name until all references
1877 to released SSA_NAMEs have been eliminated.
1878
1879 All references to the deleted SSA_NAMEs can not be eliminated until
1880 we remove unreachable blocks.
1881
1882 We can not remove unreachable blocks until after we have completed
1883 any queued jump threading.
1884
1885 We can not complete any queued jump threads until we have taken
1886 appropriate variables out of SSA form. Taking variables out of
1887 SSA form can call create_ssa_name and thus we lose.
1888
1889 Ultimately I suspect we're going to need to change the interface
1890 into the SSA_NAME manager. */
6de9cd9a
DN
1891 if (ann->modified)
1892 {
1893 tree val = NULL;
1894
1895 if (TREE_CODE (stmt) == COND_EXPR)
1896 val = COND_EXPR_COND (stmt);
1897 else if (TREE_CODE (stmt) == SWITCH_EXPR)
1898 val = SWITCH_COND (stmt);
1899
1eaba2f2 1900 if (val && TREE_CODE (val) == INTEGER_CST && find_taken_edge (bb, val))
6de9cd9a 1901 cfg_altered = true;
1eaba2f2
RH
1902
1903 /* If we simplified a statement in such a way as to be shown that it
1904 cannot trap, update the eh information and the cfg to match. */
af47810a 1905 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
1eaba2f2
RH
1906 {
1907 bitmap_set_bit (need_eh_cleanup, bb->index);
1908 if (dump_file && (dump_flags & TDF_DETAILS))
1909 fprintf (dump_file, " Flagged to clear EH edges.\n");
1910 }
6de9cd9a 1911 }
1eaba2f2 1912
6de9cd9a 1913 if (may_have_exposed_new_symbols)
cfaab3a9
DN
1914 {
1915 /* Queue the statement to be re-scanned after all the
1916 AVAIL_EXPRS have been processed. The change buffer stack for
1917 all the pushed statements will be processed when this queue
1918 is emptied. */
1919 VEC_safe_push (tree_p, heap, stmts_to_rescan, bsi_stmt_ptr (si));
1920 }
1921 else
1922 {
1923 /* Otherwise, just discard the recently pushed change buffer. If
1924 not, the STMTS_TO_RESCAN queue will get out of synch with the
1925 change buffer stack. */
1926 discard_stmt_changes (bsi_stmt_ptr (si));
1927 }
6de9cd9a
DN
1928}
1929
6de9cd9a
DN
1930/* Search for an existing instance of STMT in the AVAIL_EXPRS table. If
1931 found, return its LHS. Otherwise insert STMT in the table and return
1932 NULL_TREE.
1933
1934 Also, when an expression is first inserted in the AVAIL_EXPRS table, it
206048bd 1935 is also added to the stack pointed to by BLOCK_AVAIL_EXPRS_P, so that they
6de9cd9a
DN
1936 can be removed when we finish processing this block and its children.
1937
07beea0d 1938 NOTE: This function assumes that STMT is a GIMPLE_MODIFY_STMT node that
6de9cd9a
DN
1939 contains no CALL_EXPR on its RHS and makes no volatile nor
1940 aliased references. */
1941
1942static tree
48732f23 1943lookup_avail_expr (tree stmt, bool insert)
6de9cd9a
DN
1944{
1945 void **slot;
1946 tree lhs;
1947 tree temp;
e1111e8e 1948 struct expr_hash_elt *element = XNEW (struct expr_hash_elt);
6de9cd9a 1949
07beea0d
AH
1950 lhs = TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
1951 ? GIMPLE_STMT_OPERAND (stmt, 0) : NULL;
6de9cd9a
DN
1952
1953 initialize_hash_element (stmt, lhs, element);
1954
1955 /* Don't bother remembering constant assignments and copy operations.
1956 Constants and copy operations are handled by the constant/copy propagator
1957 in optimize_stmt. */
1958 if (TREE_CODE (element->rhs) == SSA_NAME
1959 || is_gimple_min_invariant (element->rhs))
1960 {
1961 free (element);
1962 return NULL_TREE;
1963 }
1964
6de9cd9a
DN
1965 /* Finally try to find the expression in the main expression hash table. */
1966 slot = htab_find_slot_with_hash (avail_exprs, element, element->hash,
1967 (insert ? INSERT : NO_INSERT));
1968 if (slot == NULL)
1969 {
1970 free (element);
1971 return NULL_TREE;
1972 }
1973
1974 if (*slot == NULL)
1975 {
1976 *slot = (void *) element;
d4e6fecb 1977 VEC_safe_push (tree, heap, avail_exprs_stack,
ceb7eb8f 1978 stmt ? stmt : element->rhs);
6de9cd9a
DN
1979 return NULL_TREE;
1980 }
1981
1982 /* Extract the LHS of the assignment so that it can be used as the current
1983 definition of another variable. */
1984 lhs = ((struct expr_hash_elt *)*slot)->lhs;
1985
1986 /* See if the LHS appears in the CONST_AND_COPIES table. If it does, then
1987 use the value from the const_and_copies table. */
1988 if (TREE_CODE (lhs) == SSA_NAME)
1989 {
3aecd08b
JL
1990 temp = SSA_NAME_VALUE (lhs);
1991 if (temp && TREE_CODE (temp) != VALUE_HANDLE)
6de9cd9a
DN
1992 lhs = temp;
1993 }
1994
1995 free (element);
1996 return lhs;
1997}
1998
6de9cd9a 1999/* Hashing and equality functions for AVAIL_EXPRS. The table stores
07beea0d
AH
2000 GIMPLE_MODIFY_STMT statements. We compute a value number for expressions
2001 using the code of the expression and the SSA numbers of its operands. */
6de9cd9a
DN
2002
2003static hashval_t
2004avail_expr_hash (const void *p)
2005{
f47c96aa 2006 tree stmt = ((struct expr_hash_elt *)p)->stmt;
6de9cd9a 2007 tree rhs = ((struct expr_hash_elt *)p)->rhs;
f47c96aa
AM
2008 tree vuse;
2009 ssa_op_iter iter;
6de9cd9a 2010 hashval_t val = 0;
6de9cd9a
DN
2011
2012 /* iterative_hash_expr knows how to deal with any expression and
2013 deals with commutative operators as well, so just use it instead
2014 of duplicating such complexities here. */
2015 val = iterative_hash_expr (rhs, val);
2016
2017 /* If the hash table entry is not associated with a statement, then we
2018 can just hash the expression and not worry about virtual operands
2019 and such. */
f47c96aa 2020 if (!stmt || !stmt_ann (stmt))
6de9cd9a
DN
2021 return val;
2022
2023 /* Add the SSA version numbers of every vuse operand. This is important
2024 because compound variables like arrays are not renamed in the
2025 operands. Rather, the rename is done on the virtual variable
2026 representing all the elements of the array. */
f47c96aa
AM
2027 FOR_EACH_SSA_TREE_OPERAND (vuse, stmt, iter, SSA_OP_VUSE)
2028 val = iterative_hash_expr (vuse, val);
6de9cd9a
DN
2029
2030 return val;
2031}
2032
940db2c8
RH
2033static hashval_t
2034real_avail_expr_hash (const void *p)
2035{
2036 return ((const struct expr_hash_elt *)p)->hash;
2037}
6de9cd9a
DN
2038
2039static int
2040avail_expr_eq (const void *p1, const void *p2)
2041{
f47c96aa 2042 tree stmt1 = ((struct expr_hash_elt *)p1)->stmt;
6de9cd9a 2043 tree rhs1 = ((struct expr_hash_elt *)p1)->rhs;
f47c96aa 2044 tree stmt2 = ((struct expr_hash_elt *)p2)->stmt;
6de9cd9a
DN
2045 tree rhs2 = ((struct expr_hash_elt *)p2)->rhs;
2046
2047 /* If they are the same physical expression, return true. */
f47c96aa 2048 if (rhs1 == rhs2 && stmt1 == stmt2)
6de9cd9a
DN
2049 return true;
2050
2051 /* If their codes are not equal, then quit now. */
2052 if (TREE_CODE (rhs1) != TREE_CODE (rhs2))
2053 return false;
2054
2055 /* In case of a collision, both RHS have to be identical and have the
2056 same VUSE operands. */
2057 if ((TREE_TYPE (rhs1) == TREE_TYPE (rhs2)
2058 || lang_hooks.types_compatible_p (TREE_TYPE (rhs1), TREE_TYPE (rhs2)))
2059 && operand_equal_p (rhs1, rhs2, OEP_PURE_SAME))
2060 {
f47c96aa
AM
2061 bool ret = compare_ssa_operands_equal (stmt1, stmt2, SSA_OP_VUSE);
2062 gcc_assert (!ret || ((struct expr_hash_elt *)p1)->hash
1e128c5f 2063 == ((struct expr_hash_elt *)p2)->hash);
f47c96aa 2064 return ret;
6de9cd9a
DN
2065 }
2066
2067 return false;
2068}
e67c25c7
JL
2069
2070/* PHI-ONLY copy and constant propagation. This pass is meant to clean
2071 up degenerate PHIs created by or exposed by jump threading. */
2072
2073/* Given PHI, return its RHS if the PHI is a degenerate, otherwise return
2074 NULL. */
2075
2076static tree
2077degenerate_phi_result (tree phi)
2078{
2079 tree lhs = PHI_RESULT (phi);
2080 tree val = NULL;
2081 int i;
2082
2083 /* Ignoring arguments which are the same as LHS, if all the remaining
2084 arguments are the same, then the PHI is a degenerate and has the
2085 value of that common argument. */
2086 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
2087 {
2088 tree arg = PHI_ARG_DEF (phi, i);
2089
2090 if (arg == lhs)
2091 continue;
2092 else if (!val)
2093 val = arg;
2094 else if (!operand_equal_p (arg, val, 0))
2095 break;
2096 }
2097 return (i == PHI_NUM_ARGS (phi) ? val : NULL);
2098}
2099
07beea0d 2100/* Given a tree node T, which is either a PHI_NODE or GIMPLE_MODIFY_STMT,
e67c25c7
JL
2101 remove it from the IL. */
2102
2103static void
2104remove_stmt_or_phi (tree t)
2105{
2106 if (TREE_CODE (t) == PHI_NODE)
9b3b55a1 2107 remove_phi_node (t, NULL, true);
e67c25c7
JL
2108 else
2109 {
2110 block_stmt_iterator bsi = bsi_for_stmt (t);
2111 bsi_remove (&bsi, true);
ca15e365 2112 release_defs (t);
e67c25c7
JL
2113 }
2114}
2115
07beea0d 2116/* Given a tree node T, which is either a PHI_NODE or GIMPLE_MODIFY_STMT,
e67c25c7
JL
2117 return the "rhs" of the node, in the case of a non-degenerate
2118 PHI, NULL is returned. */
2119
2120static tree
2121get_rhs_or_phi_arg (tree t)
2122{
2123 if (TREE_CODE (t) == PHI_NODE)
2124 return degenerate_phi_result (t);
07beea0d
AH
2125 else if (TREE_CODE (t) == GIMPLE_MODIFY_STMT)
2126 return GIMPLE_STMT_OPERAND (t, 1);
e67c25c7
JL
2127 gcc_unreachable ();
2128}
2129
2130
07beea0d 2131/* Given a tree node T, which is either a PHI_NODE or a GIMPLE_MODIFY_STMT,
e67c25c7
JL
2132 return the "lhs" of the node. */
2133
2134static tree
2135get_lhs_or_phi_result (tree t)
2136{
2137 if (TREE_CODE (t) == PHI_NODE)
2138 return PHI_RESULT (t);
07beea0d
AH
2139 else if (TREE_CODE (t) == GIMPLE_MODIFY_STMT)
2140 return GIMPLE_STMT_OPERAND (t, 0);
e67c25c7
JL
2141 gcc_unreachable ();
2142}
2143
2144/* Propagate RHS into all uses of LHS (when possible).
2145
2146 RHS and LHS are derived from STMT, which is passed in solely so
2147 that we can remove it if propagation is successful.
2148
2149 When propagating into a PHI node or into a statement which turns
2150 into a trivial copy or constant initialization, set the
2151 appropriate bit in INTERESTING_NAMEs so that we will visit those
2152 nodes as well in an effort to pick up secondary optimization
2153 opportunities. */
2154
2155static void
2156propagate_rhs_into_lhs (tree stmt, tree lhs, tree rhs, bitmap interesting_names)
2157{
2158 /* First verify that propagation is valid and isn't going to move a
2159 loop variant variable outside its loop. */
2160 if (! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs)
2161 && (TREE_CODE (rhs) != SSA_NAME
2162 || ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs))
2163 && may_propagate_copy (lhs, rhs)
2164 && loop_depth_of_name (lhs) >= loop_depth_of_name (rhs))
2165 {
2166 use_operand_p use_p;
2167 imm_use_iterator iter;
6c00f606 2168 tree use_stmt;
e67c25c7
JL
2169 bool all = true;
2170
2171 /* Dump details. */
2172 if (dump_file && (dump_flags & TDF_DETAILS))
2173 {
2174 fprintf (dump_file, " Replacing '");
2175 print_generic_expr (dump_file, lhs, dump_flags);
2176 fprintf (dump_file, "' with %s '",
2177 (TREE_CODE (rhs) != SSA_NAME ? "constant" : "variable"));
2178 print_generic_expr (dump_file, rhs, dump_flags);
2179 fprintf (dump_file, "'\n");
2180 }
2181
2182 /* Walk over every use of LHS and try to replace the use with RHS.
2183 At this point the only reason why such a propagation would not
2184 be successful would be if the use occurs in an ASM_EXPR. */
6c00f606 2185 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
e67c25c7 2186 {
e67c25c7
JL
2187
2188 /* It's not always safe to propagate into an ASM_EXPR. */
2189 if (TREE_CODE (use_stmt) == ASM_EXPR
2190 && ! may_propagate_copy_into_asm (lhs))
2191 {
2192 all = false;
2193 continue;
2194 }
2195
2196 /* Dump details. */
2197 if (dump_file && (dump_flags & TDF_DETAILS))
2198 {
2199 fprintf (dump_file, " Original statement:");
2200 print_generic_expr (dump_file, use_stmt, dump_flags);
2201 fprintf (dump_file, "\n");
2202 }
2203
cfaab3a9
DN
2204 push_stmt_changes (&use_stmt);
2205
cbc75e62 2206 /* Propagate the RHS into this use of the LHS. */
6c00f606
AM
2207 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
2208 propagate_value (use_p, rhs);
cbc75e62
JL
2209
2210 /* Special cases to avoid useless calls into the folding
2211 routines, operand scanning, etc.
2212
2213 First, propagation into a PHI may cause the PHI to become
2214 a degenerate, so mark the PHI as interesting. No other
2215 actions are necessary.
2216
2217 Second, if we're propagating a virtual operand and the
2218 propagation does not change the underlying _DECL node for
2219 the virtual operand, then no further actions are necessary. */
2220 if (TREE_CODE (use_stmt) == PHI_NODE
2221 || (! is_gimple_reg (lhs)
2222 && TREE_CODE (rhs) == SSA_NAME
2223 && SSA_NAME_VAR (lhs) == SSA_NAME_VAR (rhs)))
2224 {
2225 /* Dump details. */
2226 if (dump_file && (dump_flags & TDF_DETAILS))
2227 {
2228 fprintf (dump_file, " Updated statement:");
2229 print_generic_expr (dump_file, use_stmt, dump_flags);
2230 fprintf (dump_file, "\n");
2231 }
2232
2233 /* Propagation into a PHI may expose new degenerate PHIs,
2234 so mark the result of the PHI as interesting. */
2235 if (TREE_CODE (use_stmt) == PHI_NODE)
2236 {
2237 tree result = get_lhs_or_phi_result (use_stmt);
2238 bitmap_set_bit (interesting_names, SSA_NAME_VERSION (result));
2239 }
cfaab3a9
DN
2240
2241 discard_stmt_changes (&use_stmt);
cbc75e62
JL
2242 continue;
2243 }
2244
2245 /* From this point onward we are propagating into a
2246 real statement. Folding may (or may not) be possible,
2247 we may expose new operands, expose dead EH edges,
2248 etc. */
e67c25c7 2249 fold_stmt_inplace (use_stmt);
3ae194cd
JL
2250
2251 /* Sometimes propagation can expose new operands to the
2252 renamer. Note this will call update_stmt at the
2253 appropriate time. */
cfaab3a9 2254 pop_stmt_changes (&use_stmt);
e67c25c7
JL
2255
2256 /* Dump details. */
2257 if (dump_file && (dump_flags & TDF_DETAILS))
2258 {
2259 fprintf (dump_file, " Updated statement:");
2260 print_generic_expr (dump_file, use_stmt, dump_flags);
2261 fprintf (dump_file, "\n");
2262 }
2263
e67c25c7
JL
2264 /* If we replaced a variable index with a constant, then
2265 we would need to update the invariant flag for ADDR_EXPRs. */
07beea0d
AH
2266 if (TREE_CODE (use_stmt) == GIMPLE_MODIFY_STMT
2267 && TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt, 1)) == ADDR_EXPR)
2268 recompute_tree_invariant_for_addr_expr
2269 (GIMPLE_STMT_OPERAND (use_stmt, 1));
e67c25c7
JL
2270
2271 /* If we cleaned up EH information from the statement,
72922229 2272 mark its containing block as needing EH cleanups. */
e67c25c7 2273 if (maybe_clean_or_replace_eh_stmt (use_stmt, use_stmt))
72922229
JL
2274 {
2275 bitmap_set_bit (need_eh_cleanup, bb_for_stmt (use_stmt)->index);
2276 if (dump_file && (dump_flags & TDF_DETAILS))
2277 fprintf (dump_file, " Flagged to clear EH edges.\n");
2278 }
e67c25c7 2279
cbc75e62
JL
2280 /* Propagation may expose new trivial copy/constant propagation
2281 opportunities. */
07beea0d
AH
2282 if (TREE_CODE (use_stmt) == GIMPLE_MODIFY_STMT
2283 && TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt, 0)) == SSA_NAME
2284 && (TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt, 1)) == SSA_NAME
2285 || is_gimple_min_invariant (GIMPLE_STMT_OPERAND (use_stmt,
2286 1))))
e67c25c7
JL
2287 {
2288 tree result = get_lhs_or_phi_result (use_stmt);
2289 bitmap_set_bit (interesting_names, SSA_NAME_VERSION (result));
2290 }
2291
2292 /* Propagation into these nodes may make certain edges in
2293 the CFG unexecutable. We want to identify them as PHI nodes
2294 at the destination of those unexecutable edges may become
2295 degenerates. */
2296 else if (TREE_CODE (use_stmt) == COND_EXPR
2297 || TREE_CODE (use_stmt) == SWITCH_EXPR
2298 || TREE_CODE (use_stmt) == GOTO_EXPR)
2299 {
2300 tree val;
2301
2302 if (TREE_CODE (use_stmt) == COND_EXPR)
2303 val = COND_EXPR_COND (use_stmt);
2304 else if (TREE_CODE (use_stmt) == SWITCH_EXPR)
2305 val = SWITCH_COND (use_stmt);
2306 else
2307 val = GOTO_DESTINATION (use_stmt);
2308
2309 if (is_gimple_min_invariant (val))
2310 {
2311 basic_block bb = bb_for_stmt (use_stmt);
2312 edge te = find_taken_edge (bb, val);
2313 edge_iterator ei;
2314 edge e;
2315 block_stmt_iterator bsi;
2316
2317 /* Remove all outgoing edges except TE. */
2318 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei));)
2319 {
2320 if (e != te)
2321 {
2322 tree phi;
2323
2324 /* Mark all the PHI nodes at the destination of
2325 the unexecutable edge as interesting. */
2326 for (phi = phi_nodes (e->dest);
2327 phi;
2328 phi = PHI_CHAIN (phi))
2329 {
2330 tree result = PHI_RESULT (phi);
2331 int version = SSA_NAME_VERSION (result);
2332
2333 bitmap_set_bit (interesting_names, version);
2334 }
2335
2336 te->probability += e->probability;
2337
2338 te->count += e->count;
2339 remove_edge (e);
8d9d6561 2340 cfg_altered = true;
e67c25c7
JL
2341 }
2342 else
2343 ei_next (&ei);
2344 }
2345
2346 bsi = bsi_last (bb_for_stmt (use_stmt));
2347 bsi_remove (&bsi, true);
2348
2349 /* And fixup the flags on the single remaining edge. */
2350 te->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
2351 te->flags &= ~EDGE_ABNORMAL;
2352 te->flags |= EDGE_FALLTHRU;
2353 if (te->probability > REG_BR_PROB_BASE)
2354 te->probability = REG_BR_PROB_BASE;
2355 }
2356 }
2357 }
2358
6c00f606 2359 /* Ensure there is nothing else to do. */
a5f84464 2360 gcc_assert (!all || has_zero_uses (lhs));
243cc836 2361
e67c25c7
JL
2362 /* If we were able to propagate away all uses of LHS, then
2363 we can remove STMT. */
2364 if (all)
2365 remove_stmt_or_phi (stmt);
2366 }
2367}
2368
917f1b7e 2369/* T is either a PHI node (potentially a degenerate PHI node) or
e67c25c7
JL
2370 a statement that is a trivial copy or constant initialization.
2371
2372 Attempt to eliminate T by propagating its RHS into all uses of
2373 its LHS. This may in turn set new bits in INTERESTING_NAMES
2374 for nodes we want to revisit later.
2375
2376 All exit paths should clear INTERESTING_NAMES for the result
2377 of T. */
2378
2379static void
2380eliminate_const_or_copy (tree t, bitmap interesting_names)
2381{
2382 tree lhs = get_lhs_or_phi_result (t);
2383 tree rhs;
2384 int version = SSA_NAME_VERSION (lhs);
2385
2386 /* If the LHS of this statement or PHI has no uses, then we can
2387 just eliminate it. This can occur if, for example, the PHI
2388 was created by block duplication due to threading and its only
2389 use was in the conditional at the end of the block which was
2390 deleted. */
2391 if (has_zero_uses (lhs))
2392 {
2393 bitmap_clear_bit (interesting_names, version);
2394 remove_stmt_or_phi (t);
2395 return;
2396 }
2397
2398 /* Get the RHS of the assignment or PHI node if the PHI is a
2399 degenerate. */
2400 rhs = get_rhs_or_phi_arg (t);
2401 if (!rhs)
2402 {
2403 bitmap_clear_bit (interesting_names, version);
2404 return;
2405 }
2406
2407 propagate_rhs_into_lhs (t, lhs, rhs, interesting_names);
2408
2409 /* Note that T may well have been deleted by now, so do
2410 not access it, instead use the saved version # to clear
2411 T's entry in the worklist. */
2412 bitmap_clear_bit (interesting_names, version);
2413}
2414
2415/* The first phase in degenerate PHI elimination.
2416
2417 Eliminate the degenerate PHIs in BB, then recurse on the
2418 dominator children of BB. */
2419
2420static void
2421eliminate_degenerate_phis_1 (basic_block bb, bitmap interesting_names)
2422{
2423 tree phi, next;
2424 basic_block son;
2425
2426 for (phi = phi_nodes (bb); phi; phi = next)
2427 {
2428 next = PHI_CHAIN (phi);
2429 eliminate_const_or_copy (phi, interesting_names);
2430 }
2431
2432 /* Recurse into the dominator children of BB. */
2433 for (son = first_dom_son (CDI_DOMINATORS, bb);
2434 son;
2435 son = next_dom_son (CDI_DOMINATORS, son))
2436 eliminate_degenerate_phis_1 (son, interesting_names);
2437}
2438
2439
2440/* A very simple pass to eliminate degenerate PHI nodes from the
2441 IL. This is meant to be fast enough to be able to be run several
2442 times in the optimization pipeline.
2443
2444 Certain optimizations, particularly those which duplicate blocks
2445 or remove edges from the CFG can create or expose PHIs which are
2446 trivial copies or constant initializations.
2447
2448 While we could pick up these optimizations in DOM or with the
2449 combination of copy-prop and CCP, those solutions are far too
2450 heavy-weight for our needs.
2451
2452 This implementation has two phases so that we can efficiently
2453 eliminate the first order degenerate PHIs and second order
2454 degenerate PHIs.
2455
2456 The first phase performs a dominator walk to identify and eliminate
2457 the vast majority of the degenerate PHIs. When a degenerate PHI
2458 is identified and eliminated any affected statements or PHIs
2459 are put on a worklist.
2460
2461 The second phase eliminates degenerate PHIs and trivial copies
2462 or constant initializations using the worklist. This is how we
2463 pick up the secondary optimization opportunities with minimal
2464 cost. */
2465
2466static unsigned int
2467eliminate_degenerate_phis (void)
2468{
2469 bitmap interesting_names;
1f70491b 2470 bitmap interesting_names1;
e67c25c7 2471
72922229
JL
2472 /* Bitmap of blocks which need EH information updated. We can not
2473 update it on-the-fly as doing so invalidates the dominator tree. */
2474 need_eh_cleanup = BITMAP_ALLOC (NULL);
2475
e67c25c7
JL
2476 /* INTERESTING_NAMES is effectively our worklist, indexed by
2477 SSA_NAME_VERSION.
2478
2479 A set bit indicates that the statement or PHI node which
2480 defines the SSA_NAME should be (re)examined to determine if
66a4ad37 2481 it has become a degenerate PHI or trivial const/copy propagation
e67c25c7
JL
2482 opportunity.
2483
2484 Experiments have show we generally get better compilation
2485 time behavior with bitmaps rather than sbitmaps. */
2486 interesting_names = BITMAP_ALLOC (NULL);
1f70491b 2487 interesting_names1 = BITMAP_ALLOC (NULL);
e67c25c7 2488
8d9d6561
EB
2489 calculate_dominance_info (CDI_DOMINATORS);
2490 cfg_altered = false;
2491
917f1b7e 2492 /* First phase. Eliminate degenerate PHIs via a dominator
e67c25c7
JL
2493 walk of the CFG.
2494
2495 Experiments have indicated that we generally get better
2496 compile-time behavior by visiting blocks in the first
2497 phase in dominator order. Presumably this is because walking
2498 in dominator order leaves fewer PHIs for later examination
2499 by the worklist phase. */
e67c25c7
JL
2500 eliminate_degenerate_phis_1 (ENTRY_BLOCK_PTR, interesting_names);
2501
917f1b7e 2502 /* Second phase. Eliminate second order degenerate PHIs as well
e67c25c7
JL
2503 as trivial copies or constant initializations identified by
2504 the first phase or this phase. Basically we keep iterating
2505 until our set of INTERESTING_NAMEs is empty. */
2506 while (!bitmap_empty_p (interesting_names))
2507 {
2508 unsigned int i;
2509 bitmap_iterator bi;
2510
1f70491b
AP
2511 /* EXECUTE_IF_SET_IN_BITMAP does not like its bitmap
2512 changed during the loop. Copy it to another bitmap and
2513 use that. */
2514 bitmap_copy (interesting_names1, interesting_names);
2515
2516 EXECUTE_IF_SET_IN_BITMAP (interesting_names1, 0, i, bi)
e67c25c7
JL
2517 {
2518 tree name = ssa_name (i);
2519
2520 /* Ignore SSA_NAMEs that have been released because
2521 their defining statement was deleted (unreachable). */
2522 if (name)
2523 eliminate_const_or_copy (SSA_NAME_DEF_STMT (ssa_name (i)),
2524 interesting_names);
2525 }
2526 }
72922229 2527
8d9d6561
EB
2528 if (cfg_altered)
2529 free_dominance_info (CDI_DOMINATORS);
2530
72922229
JL
2531 /* Propagation of const and copies may make some EH edges dead. Purge
2532 such edges from the CFG as needed. */
2533 if (!bitmap_empty_p (need_eh_cleanup))
2534 {
4be1caf9 2535 tree_purge_all_dead_eh_edges (need_eh_cleanup);
72922229
JL
2536 BITMAP_FREE (need_eh_cleanup);
2537 }
e67c25c7
JL
2538
2539 BITMAP_FREE (interesting_names);
1f70491b 2540 BITMAP_FREE (interesting_names1);
e67c25c7
JL
2541 return 0;
2542}
2543
2544struct tree_opt_pass pass_phi_only_cprop =
2545{
2546 "phicprop", /* name */
2547 gate_dominator, /* gate */
2548 eliminate_degenerate_phis, /* execute */
2549 NULL, /* sub */
2550 NULL, /* next */
2551 0, /* static_pass_number */
b6313dcf 2552 TV_TREE_PHI_CPROP, /* tv_id */
e67c25c7
JL
2553 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
2554 0, /* properties_provided */
ae07b463 2555 0, /* properties_destroyed */
e67c25c7 2556 0, /* todo_flags_start */
706ca88e
DN
2557 TODO_cleanup_cfg
2558 | TODO_dump_func
2559 | TODO_ggc_collect
2560 | TODO_verify_ssa
2561 | TODO_verify_stmts
2562 | TODO_update_ssa, /* todo_flags_finish */
e67c25c7
JL
2563 0 /* letter */
2564};
This page took 1.632496 seconds and 5 git commands to generate.