]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-ssa-dom.c
use scan-hidden
[thirdparty/gcc.git] / gcc / tree-ssa-dom.c
CommitLineData
4ee9c684 1/* SSA Dominator optimizations for trees
62b180e1 2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
3 Free Software Foundation, Inc.
4ee9c684 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
67ce556b 20the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21Boston, MA 02110-1301, USA. */
4ee9c684 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"
388d1fc1 33#include "cfgloop.h"
4ee9c684 34#include "output.h"
4ee9c684 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"
7d564439 44#include "tree-ssa-propagate.h"
4ee9c684 45#include "langhooks.h"
cf024d22 46#include "params.h"
4ee9c684 47
48/* This file implements optimizations on the dominator tree. */
49
2f0993e7 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;
2f0993e7 78};
79
80
4ee9c684 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
9c629f0e 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
73645111 92 (null). When we finish processing the block, we pop off entries and
9c629f0e 93 remove the expressions from the global hash table until we hit the
94 marker. */
046bfc77 95static VEC(tree,heap) *avail_exprs_stack;
9c629f0e 96
a721131d 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. */
046bfc77 104static VEC(tree,heap) *stmts_to_rescan;
a721131d 105
4ee9c684 106/* Structure for entries in the expression hash table.
107
108 This requires more memory for the hash table entries, but allows us
109 to avoid creating silly tree nodes and annotations for conditionals,
110 eliminates 2 global hash tables and two block local varrays.
111
112 It also allows us to reduce the number of hash table lookups we
113 have to perform in lookup_avail_expr and finally it allows us to
114 significantly reduce the number of calls into the hashing routine
115 itself. */
a8046f60 116
4ee9c684 117struct expr_hash_elt
118{
119 /* The value (lhs) of this expression. */
120 tree lhs;
121
122 /* The expression (rhs) we want to record. */
123 tree rhs;
124
b66731e8 125 /* The stmt pointer if this element corresponds to a statement. */
126 tree stmt;
4ee9c684 127
128 /* The hash value for RHS/ann. */
129 hashval_t hash;
130};
131
da43203c 132/* Stack of dest,src pairs that need to be restored during finalization.
133
134 A NULL entry is used to mark the end of pairs which need to be
135 restored during finalization of this block. */
046bfc77 136static VEC(tree,heap) *const_and_copies_stack;
da43203c 137
4ee9c684 138/* Track whether or not we have changed the control flow graph. */
139static bool cfg_altered;
140
35c15734 141/* Bitmap of blocks that have had EH statements cleaned. We should
0870fd6e 142 remove their dead edges eventually. */
35c15734 143static bitmap need_eh_cleanup;
144
4ee9c684 145/* Statistics for dominator optimizations. */
146struct opt_stats_d
147{
148 long num_stmts;
149 long num_exprs_considered;
150 long num_re;
88dbf20f 151 long num_const_prop;
152 long num_copy_prop;
4ee9c684 153};
154
d0d897b6 155static struct opt_stats_d opt_stats;
156
4ee9c684 157struct eq_expr_value
158{
159 tree src;
160 tree dst;
161};
162
163/* Local functions. */
164static void optimize_stmt (struct dom_walk_data *,
165 basic_block bb,
166 block_stmt_iterator);
9c629f0e 167static tree lookup_avail_expr (tree, bool);
4ee9c684 168static hashval_t avail_expr_hash (const void *);
23ace16d 169static hashval_t real_avail_expr_hash (const void *);
4ee9c684 170static int avail_expr_eq (const void *, const void *);
171static void htab_statistics (FILE *, htab_t);
9c629f0e 172static void record_cond (tree, tree);
da43203c 173static void record_const_or_copy (tree, tree);
174static void record_equality (tree, tree);
2f0993e7 175static void record_equivalences_from_phis (basic_block);
176static void record_equivalences_from_incoming_edge (basic_block);
8f628ee8 177static bool eliminate_redundant_computations (tree);
180d0339 178static void record_equivalences_from_stmt (tree, int, stmt_ann_t);
62b180e1 179static void dom_thread_across_edge (struct dom_walk_data *, edge);
4ee9c684 180static void dom_opt_finalize_block (struct dom_walk_data *, basic_block);
4ee9c684 181static void dom_opt_initialize_block (struct dom_walk_data *, basic_block);
2f0993e7 182static void propagate_to_outgoing_edges (struct dom_walk_data *, basic_block);
9c629f0e 183static void remove_local_expressions_from_table (void);
da43203c 184static void restore_vars_to_original_value (void);
c0735efa 185static edge single_incoming_edge_ignoring_loop_edges (basic_block);
4ee9c684 186
88dbf20f 187
2f0993e7 188/* Allocate an EDGE_INFO for edge E and attach it to E.
189 Return the new EDGE_INFO structure. */
190
191static struct edge_info *
192allocate_edge_info (edge e)
193{
194 struct edge_info *edge_info;
195
945865c5 196 edge_info = XCNEW (struct edge_info);
2f0993e7 197
198 e->aux = edge_info;
199 return edge_info;
200}
201
202/* Free all EDGE_INFO structures associated with edges in the CFG.
640e9781 203 If a particular edge can be threaded, copy the redirection
2f0993e7 204 target from the EDGE_INFO structure into the edge's AUX field
205 as required by code to update the CFG and SSA graph for
206 jump threading. */
207
208static void
209free_all_edge_infos (void)
210{
211 basic_block bb;
212 edge_iterator ei;
213 edge e;
214
215 FOR_EACH_BB (bb)
216 {
217 FOR_EACH_EDGE (e, ei, bb->preds)
218 {
945865c5 219 struct edge_info *edge_info = (struct edge_info *) e->aux;
2f0993e7 220
221 if (edge_info)
222 {
2f0993e7 223 if (edge_info->cond_equivalences)
224 free (edge_info->cond_equivalences);
225 free (edge_info);
3cebc9d2 226 e->aux = NULL;
2f0993e7 227 }
228 }
229 }
230}
231
4ee9c684 232/* Jump threading, redundancy elimination and const/copy propagation.
233
4ee9c684 234 This pass may expose new symbols that need to be renamed into SSA. For
235 every new symbol exposed, its corresponding bit will be set in
591c2a30 236 VARS_TO_RENAME. */
4ee9c684 237
2a1990e9 238static unsigned int
4ee9c684 239tree_ssa_dominator_optimize (void)
240{
4ee9c684 241 struct dom_walk_data walk_data;
242 unsigned int i;
388d1fc1 243 struct loops loops_info;
4ee9c684 244
03ec6c0e 245 memset (&opt_stats, 0, sizeof (opt_stats));
246
4ee9c684 247 /* Create our hash tables. */
23ace16d 248 avail_exprs = htab_create (1024, real_avail_expr_hash, avail_expr_eq, free);
046bfc77 249 avail_exprs_stack = VEC_alloc (tree, heap, 20);
046bfc77 250 const_and_copies_stack = VEC_alloc (tree, heap, 20);
046bfc77 251 stmts_to_rescan = VEC_alloc (tree, heap, 20);
27335ffd 252 need_eh_cleanup = BITMAP_ALLOC (NULL);
4ee9c684 253
254 /* Setup callbacks for the generic dominator tree walker. */
255 walk_data.walk_stmts_backward = false;
256 walk_data.dom_direction = CDI_DOMINATORS;
180d0339 257 walk_data.initialize_block_local_data = NULL;
4ee9c684 258 walk_data.before_dom_children_before_stmts = dom_opt_initialize_block;
259 walk_data.before_dom_children_walk_stmts = optimize_stmt;
2f0993e7 260 walk_data.before_dom_children_after_stmts = propagate_to_outgoing_edges;
4ee9c684 261 walk_data.after_dom_children_before_stmts = NULL;
262 walk_data.after_dom_children_walk_stmts = NULL;
263 walk_data.after_dom_children_after_stmts = dom_opt_finalize_block;
264 /* Right now we only attach a dummy COND_EXPR to the global data pointer.
265 When we attach more stuff we'll need to fill this out with a real
266 structure. */
267 walk_data.global_data = NULL;
180d0339 268 walk_data.block_local_data_size = 0;
88dbf20f 269 walk_data.interesting_blocks = NULL;
4ee9c684 270
271 /* Now initialize the dominator walker. */
272 init_walk_dominator_tree (&walk_data);
273
4ee9c684 274 calculate_dominance_info (CDI_DOMINATORS);
275
388d1fc1 276 /* We need to know which edges exit loops so that we can
277 aggressively thread through loop headers to an exit
278 edge. */
279 flow_loops_find (&loops_info);
280 mark_loop_exit_edges (&loops_info);
281 flow_loops_free (&loops_info);
282
283 /* Clean up the CFG so that any forwarder blocks created by loop
284 canonicalization are removed. */
285 cleanup_tree_cfg ();
8171a1dd 286 calculate_dominance_info (CDI_DOMINATORS);
388d1fc1 287
62b180e1 288 /* We need accurate information regarding back edges in the CFG
289 for jump threading. */
290 mark_dfs_back_edges ();
388d1fc1 291
62b180e1 292 /* Recursively walk the dominator tree optimizing statements. */
293 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
4ee9c684 294
62b180e1 295 {
296 block_stmt_iterator bsi;
297 basic_block bb;
298 FOR_EACH_BB (bb)
22aa74c4 299 {
62b180e1 300 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
301 update_stmt_if_modified (bsi_stmt (bsi));
22aa74c4 302 }
62b180e1 303 }
0638045e 304
62b180e1 305 /* If we exposed any new variables, go ahead and put them into
306 SSA form now, before we handle jump threading. This simplifies
307 interactions between rewriting of _DECL nodes into SSA form
308 and rewriting SSA_NAME nodes into SSA form after block
309 duplication and CFG manipulation. */
310 update_ssa (TODO_update_ssa);
388d1fc1 311
62b180e1 312 free_all_edge_infos ();
388d1fc1 313
62b180e1 314 /* Thread jumps, creating duplicate blocks as needed. */
315 cfg_altered |= thread_through_all_blocks ();
4ee9c684 316
62b180e1 317 /* Removal of statements may make some EH edges dead. Purge
318 such edges from the CFG as needed. */
319 if (!bitmap_empty_p (need_eh_cleanup))
320 {
321 cfg_altered |= tree_purge_all_dead_eh_edges (need_eh_cleanup);
322 bitmap_zero (need_eh_cleanup);
323 }
4ee9c684 324
62b180e1 325 if (cfg_altered)
326 free_dominance_info (CDI_DOMINATORS);
7414901f 327
62b180e1 328 /* Finally, remove everything except invariants in SSA_NAME_VALUE.
7414901f 329
62b180e1 330 Long term we will be able to let everything in SSA_NAME_VALUE
331 persist. However, for now, we know this is the safe thing to do. */
332 for (i = 0; i < num_ssa_names; i++)
333 {
334 tree name = ssa_name (i);
335 tree value;
7414901f 336
62b180e1 337 if (!name)
338 continue;
7414901f 339
62b180e1 340 value = SSA_NAME_VALUE (name);
341 if (value && !is_gimple_min_invariant (value))
342 SSA_NAME_VALUE (name) = NULL;
4ee9c684 343 }
4ee9c684 344
4ee9c684 345 /* Debugging dumps. */
346 if (dump_file && (dump_flags & TDF_STATS))
347 dump_dominator_optimization_stats (dump_file);
348
62b180e1 349 /* Delete our main hashtable. */
4ee9c684 350 htab_delete (avail_exprs);
4ee9c684 351
352 /* And finalize the dominator walker. */
353 fini_walk_dominator_tree (&walk_data);
a8ddfbad 354
8dbf774a 355 /* Free asserted bitmaps and stacks. */
27335ffd 356 BITMAP_FREE (need_eh_cleanup);
486b57c7 357
046bfc77 358 VEC_free (tree, heap, avail_exprs_stack);
359 VEC_free (tree, heap, const_and_copies_stack);
046bfc77 360 VEC_free (tree, heap, stmts_to_rescan);
2a1990e9 361 return 0;
4ee9c684 362}
363
364static bool
365gate_dominator (void)
366{
367 return flag_tree_dom != 0;
368}
369
370struct tree_opt_pass pass_dominator =
371{
372 "dom", /* name */
373 gate_dominator, /* gate */
374 tree_ssa_dominator_optimize, /* execute */
375 NULL, /* sub */
376 NULL, /* next */
377 0, /* static_pass_number */
378 TV_TREE_SSA_DOMINATOR_OPTS, /* tv_id */
f45a1ca1 379 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
4ee9c684 380 0, /* properties_provided */
eff665b7 381 PROP_smt_usage, /* properties_destroyed */
4ee9c684 382 0, /* todo_flags_start */
88dbf20f 383 TODO_dump_func
384 | TODO_update_ssa
62b180e1 385 | TODO_cleanup_cfg
de08b454 386 | TODO_verify_ssa
eff665b7 387 | TODO_update_smt_usage, /* todo_flags_finish */
0f9005dd 388 0 /* letter */
4ee9c684 389};
390
391
54aceb26 392/* Given a stmt CONDSTMT containing a COND_EXPR, canonicalize the
393 COND_EXPR into a canonical form. */
394
395static void
396canonicalize_comparison (tree condstmt)
397{
398 tree cond = COND_EXPR_COND (condstmt);
399 tree op0;
400 tree op1;
401 enum tree_code code = TREE_CODE (cond);
402
403 if (!COMPARISON_CLASS_P (cond))
404 return;
405
406 op0 = TREE_OPERAND (cond, 0);
407 op1 = TREE_OPERAND (cond, 1);
408
409 /* If it would be profitable to swap the operands, then do so to
410 canonicalize the statement, enabling better optimization.
411
412 By placing canonicalization of such expressions here we
413 transparently keep statements in canonical form, even
414 when the statement is modified. */
415 if (tree_swap_operands_p (op0, op1, false))
416 {
417 /* For relationals we need to swap the operands
418 and change the code. */
419 if (code == LT_EXPR
420 || code == GT_EXPR
421 || code == LE_EXPR
422 || code == GE_EXPR)
423 {
424 TREE_SET_CODE (cond, swap_tree_comparison (code));
425 swap_tree_operands (condstmt,
426 &TREE_OPERAND (cond, 0),
427 &TREE_OPERAND (cond, 1));
428 /* If one operand was in the operand cache, but the other is
429 not, because it is a constant, this is a case that the
430 internal updating code of swap_tree_operands can't handle
431 properly. */
432 if (TREE_CODE_CLASS (TREE_CODE (op0))
433 != TREE_CODE_CLASS (TREE_CODE (op1)))
434 update_stmt (condstmt);
435 }
436 }
437}
4ee9c684 438
4ee9c684 439/* Initialize local stacks for this optimizer and record equivalences
440 upon entry to BB. Equivalences can come from the edge traversed to
441 reach BB or they may come from PHI nodes at the start of BB. */
442
443static void
2f0993e7 444dom_opt_initialize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
445 basic_block bb)
4ee9c684 446{
447 if (dump_file && (dump_flags & TDF_DETAILS))
448 fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
449
dd2d357d 450 /* Push a marker on the stacks of local information so that we know how
451 far to unwind when we finalize this block. */
046bfc77 452 VEC_safe_push (tree, heap, avail_exprs_stack, NULL_TREE);
046bfc77 453 VEC_safe_push (tree, heap, const_and_copies_stack, NULL_TREE);
9c629f0e 454
2f0993e7 455 record_equivalences_from_incoming_edge (bb);
4ee9c684 456
457 /* PHI nodes can create equivalences too. */
2f0993e7 458 record_equivalences_from_phis (bb);
4ee9c684 459}
460
461/* Given an expression EXPR (a relational expression or a statement),
5206b159 462 initialize the hash table element pointed to by ELEMENT. */
4ee9c684 463
464static void
465initialize_hash_element (tree expr, tree lhs, struct expr_hash_elt *element)
466{
467 /* Hash table elements may be based on conditional expressions or statements.
468
469 For the former case, we have no annotation and we want to hash the
470 conditional expression. In the latter case we have an annotation and
471 we want to record the expression the statement evaluates. */
ce45a448 472 if (COMPARISON_CLASS_P (expr) || TREE_CODE (expr) == TRUTH_NOT_EXPR)
4ee9c684 473 {
b66731e8 474 element->stmt = NULL;
4ee9c684 475 element->rhs = expr;
476 }
477 else if (TREE_CODE (expr) == COND_EXPR)
478 {
b66731e8 479 element->stmt = expr;
4ee9c684 480 element->rhs = COND_EXPR_COND (expr);
481 }
482 else if (TREE_CODE (expr) == SWITCH_EXPR)
483 {
b66731e8 484 element->stmt = expr;
4ee9c684 485 element->rhs = SWITCH_COND (expr);
486 }
487 else if (TREE_CODE (expr) == RETURN_EXPR && TREE_OPERAND (expr, 0))
488 {
b66731e8 489 element->stmt = expr;
4ee9c684 490 element->rhs = TREE_OPERAND (TREE_OPERAND (expr, 0), 1);
491 }
a01d0a8b 492 else if (TREE_CODE (expr) == GOTO_EXPR)
493 {
b66731e8 494 element->stmt = expr;
a01d0a8b 495 element->rhs = GOTO_DESTINATION (expr);
496 }
4ee9c684 497 else
498 {
b66731e8 499 element->stmt = expr;
4ee9c684 500 element->rhs = TREE_OPERAND (expr, 1);
501 }
502
503 element->lhs = lhs;
504 element->hash = avail_expr_hash (element);
505}
506
507/* Remove all the expressions in LOCALS from TABLE, stopping when there are
508 LIMIT entries left in LOCALs. */
509
510static void
9c629f0e 511remove_local_expressions_from_table (void)
4ee9c684 512{
4ee9c684 513 /* Remove all the expressions made available in this block. */
046bfc77 514 while (VEC_length (tree, avail_exprs_stack) > 0)
4ee9c684 515 {
516 struct expr_hash_elt element;
046bfc77 517 tree expr = VEC_pop (tree, avail_exprs_stack);
9c629f0e 518
519 if (expr == NULL_TREE)
520 break;
4ee9c684 521
522 initialize_hash_element (expr, NULL, &element);
9c629f0e 523 htab_remove_elt_with_hash (avail_exprs, &element, element.hash);
4ee9c684 524 }
525}
526
da43203c 527/* Use the source/dest pairs in CONST_AND_COPIES_STACK to restore
528 CONST_AND_COPIES to its original state, stopping when we hit a
529 NULL marker. */
4ee9c684 530
531static void
da43203c 532restore_vars_to_original_value (void)
4ee9c684 533{
046bfc77 534 while (VEC_length (tree, const_and_copies_stack) > 0)
4ee9c684 535 {
536 tree prev_value, dest;
537
046bfc77 538 dest = VEC_pop (tree, const_and_copies_stack);
4ee9c684 539
da43203c 540 if (dest == NULL)
541 break;
542
046bfc77 543 prev_value = VEC_pop (tree, const_and_copies_stack);
4c7a0518 544 SSA_NAME_VALUE (dest) = prev_value;
4ee9c684 545 }
546}
547
62b180e1 548/* A trivial wrapper so that we can present the generic jump
549 threading code with a simple API for simplifying statements. */
550static tree
551simplify_stmt_for_jump_threading (tree stmt)
552{
553 return lookup_avail_expr (stmt, false);
554}
555
556/* Wrapper for common code to attempt to thread an edge. For example,
557 it handles lazily building the dummy condition and the bookkeeping
558 when jump threading is successful. */
559
560static void
561dom_thread_across_edge (struct dom_walk_data *walk_data, edge e)
562{
563 /* If we don't already have a dummy condition, build it now. */
564 if (! walk_data->global_data)
565 {
15348a37 566 tree dummy_cond = build2 (NE_EXPR, boolean_type_node,
62b180e1 567 integer_zero_node, integer_zero_node);
568 dummy_cond = build3 (COND_EXPR, void_type_node, dummy_cond, NULL, NULL);
569 walk_data->global_data = dummy_cond;
570 }
571
572 thread_across_edge (walk_data->global_data, e, false,
573 &const_and_copies_stack,
574 simplify_stmt_for_jump_threading);
575}
576
4ee9c684 577/* We have finished processing the dominator children of BB, perform
578 any finalization actions in preparation for leaving this node in
579 the dominator tree. */
580
581static void
582dom_opt_finalize_block (struct dom_walk_data *walk_data, basic_block bb)
583{
4ee9c684 584 tree last;
585
62b180e1 586
2080a2d5 587 /* If we have an outgoing edge to a block with multiple incoming and
588 outgoing edges, then we may be able to thread the edge. ie, we
589 may be able to statically determine which of the outgoing edges
590 will be traversed when the incoming edge from BB is traversed. */
ea091dfd 591 if (single_succ_p (bb)
592 && (single_succ_edge (bb)->flags & EDGE_ABNORMAL) == 0
62b180e1 593 && potentially_threadable_block (single_succ (bb)))
4ee9c684 594 {
62b180e1 595 dom_thread_across_edge (walk_data, single_succ_edge (bb));
4ee9c684 596 }
597 else if ((last = last_stmt (bb))
598 && TREE_CODE (last) == COND_EXPR
ce45a448 599 && (COMPARISON_CLASS_P (COND_EXPR_COND (last))
4ee9c684 600 || TREE_CODE (COND_EXPR_COND (last)) == SSA_NAME)
cd665a06 601 && EDGE_COUNT (bb->succs) == 2
602 && (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL) == 0
603 && (EDGE_SUCC (bb, 1)->flags & EDGE_ABNORMAL) == 0)
4ee9c684 604 {
605 edge true_edge, false_edge;
4ee9c684 606
607 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
608
2080a2d5 609 /* Only try to thread the edge if it reaches a target block with
610 more than one predecessor and more than one successor. */
62b180e1 611 if (potentially_threadable_block (true_edge->dest))
4ee9c684 612 {
2f0993e7 613 struct edge_info *edge_info;
614 unsigned int i;
615
9c629f0e 616 /* Push a marker onto the available expression stack so that we
617 unwind any expressions related to the TRUE arm before processing
618 the false arm below. */
046bfc77 619 VEC_safe_push (tree, heap, avail_exprs_stack, NULL_TREE);
046bfc77 620 VEC_safe_push (tree, heap, const_and_copies_stack, NULL_TREE);
9c629f0e 621
945865c5 622 edge_info = (struct edge_info *) true_edge->aux;
2f0993e7 623
624 /* If we have info associated with this edge, record it into
625 our equivalency tables. */
626 if (edge_info)
4ee9c684 627 {
2f0993e7 628 tree *cond_equivalences = edge_info->cond_equivalences;
629 tree lhs = edge_info->lhs;
630 tree rhs = edge_info->rhs;
631
a01d0a8b 632 /* If we have a simple NAME = VALUE equivalency record it. */
633 if (lhs && TREE_CODE (lhs) == SSA_NAME)
2f0993e7 634 record_const_or_copy (lhs, rhs);
635
636 /* If we have 0 = COND or 1 = COND equivalences, record them
637 into our expression hash tables. */
638 if (cond_equivalences)
639 for (i = 0; i < edge_info->max_cond_equivalences; i += 2)
640 {
641 tree expr = cond_equivalences[i];
642 tree value = cond_equivalences[i + 1];
643
644 record_cond (expr, value);
645 }
4ee9c684 646 }
4ee9c684 647
62b180e1 648 dom_thread_across_edge (walk_data, true_edge);
4ee9c684 649
650 /* And restore the various tables to their state before
651 we threaded this edge. */
9c629f0e 652 remove_local_expressions_from_table ();
4ee9c684 653 }
654
655 /* Similarly for the ELSE arm. */
62b180e1 656 if (potentially_threadable_block (false_edge->dest))
4ee9c684 657 {
2f0993e7 658 struct edge_info *edge_info;
659 unsigned int i;
660
62b180e1 661 VEC_safe_push (tree, heap, const_and_copies_stack, NULL_TREE);
945865c5 662 edge_info = (struct edge_info *) false_edge->aux;
2f0993e7 663
664 /* If we have info associated with this edge, record it into
665 our equivalency tables. */
666 if (edge_info)
4ee9c684 667 {
2f0993e7 668 tree *cond_equivalences = edge_info->cond_equivalences;
669 tree lhs = edge_info->lhs;
670 tree rhs = edge_info->rhs;
671
a01d0a8b 672 /* If we have a simple NAME = VALUE equivalency record it. */
673 if (lhs && TREE_CODE (lhs) == SSA_NAME)
2f0993e7 674 record_const_or_copy (lhs, rhs);
675
676 /* If we have 0 = COND or 1 = COND equivalences, record them
677 into our expression hash tables. */
678 if (cond_equivalences)
679 for (i = 0; i < edge_info->max_cond_equivalences; i += 2)
680 {
681 tree expr = cond_equivalences[i];
682 tree value = cond_equivalences[i + 1];
683
684 record_cond (expr, value);
685 }
4ee9c684 686 }
4ee9c684 687
62b180e1 688 /* Now thread the edge. */
689 dom_thread_across_edge (walk_data, false_edge);
4ee9c684 690
691 /* No need to remove local expressions from our tables
692 or restore vars to their original value as that will
693 be done immediately below. */
694 }
695 }
696
9c629f0e 697 remove_local_expressions_from_table ();
da43203c 698 restore_vars_to_original_value ();
4ee9c684 699
a721131d 700 /* If we queued any statements to rescan in this block, then
701 go ahead and rescan them now. */
046bfc77 702 while (VEC_length (tree, stmts_to_rescan) > 0)
4ee9c684 703 {
046bfc77 704 tree stmt = VEC_last (tree, stmts_to_rescan);
a721131d 705 basic_block stmt_bb = bb_for_stmt (stmt);
706
707 if (stmt_bb != bb)
708 break;
709
046bfc77 710 VEC_pop (tree, stmts_to_rescan);
88dbf20f 711 mark_new_vars_to_rename (stmt);
4ee9c684 712 }
713}
714
715/* PHI nodes can create equivalences too.
716
717 Ignoring any alternatives which are the same as the result, if
718 all the alternatives are equal, then the PHI node creates an
8dbf774a 719 equivalence. */
6e9a4371 720
4ee9c684 721static void
2f0993e7 722record_equivalences_from_phis (basic_block bb)
4ee9c684 723{
4ee9c684 724 tree phi;
725
04f8eea3 726 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
4ee9c684 727 {
728 tree lhs = PHI_RESULT (phi);
729 tree rhs = NULL;
730 int i;
731
732 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
733 {
734 tree t = PHI_ARG_DEF (phi, i);
735
2fb4af30 736 /* Ignore alternatives which are the same as our LHS. Since
737 LHS is a PHI_RESULT, it is known to be a SSA_NAME, so we
738 can simply compare pointers. */
fcf57fc2 739 if (lhs == t)
92527855 740 continue;
741
742 /* If we have not processed an alternative yet, then set
743 RHS to this alternative. */
744 if (rhs == NULL)
745 rhs = t;
746 /* If we have processed an alternative (stored in RHS), then
747 see if it is equal to this one. If it isn't, then stop
748 the search. */
749 else if (! operand_equal_for_phi_arg_p (rhs, t))
4ee9c684 750 break;
751 }
752
753 /* If we had no interesting alternatives, then all the RHS alternatives
754 must have been the same as LHS. */
755 if (!rhs)
756 rhs = lhs;
757
758 /* If we managed to iterate through each PHI alternative without
759 breaking out of the loop, then we have a PHI which may create
760 a useful equivalence. We do not need to record unwind data for
761 this, since this is a true assignment and not an equivalence
365db11e 762 inferred from a comparison. All uses of this ssa name are dominated
4ee9c684 763 by this assignment, so unwinding just costs time and space. */
764 if (i == PHI_NUM_ARGS (phi)
765 && may_propagate_copy (lhs, rhs))
4c7a0518 766 SSA_NAME_VALUE (lhs) = rhs;
4ee9c684 767 }
768}
769
c0735efa 770/* Ignoring loop backedges, if BB has precisely one incoming edge then
771 return that edge. Otherwise return NULL. */
772static edge
773single_incoming_edge_ignoring_loop_edges (basic_block bb)
774{
775 edge retval = NULL;
776 edge e;
cd665a06 777 edge_iterator ei;
c0735efa 778
cd665a06 779 FOR_EACH_EDGE (e, ei, bb->preds)
c0735efa 780 {
781 /* A loop back edge can be identified by the destination of
782 the edge dominating the source of the edge. */
783 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
784 continue;
785
786 /* If we have already seen a non-loop edge, then we must have
787 multiple incoming non-loop edges and thus we return NULL. */
788 if (retval)
789 return NULL;
790
791 /* This is the first non-loop incoming edge we have found. Record
792 it. */
793 retval = e;
794 }
795
796 return retval;
797}
798
4ee9c684 799/* Record any equivalences created by the incoming edge to BB. If BB
800 has more than one incoming edge, then no equivalence is created. */
801
802static void
2f0993e7 803record_equivalences_from_incoming_edge (basic_block bb)
4ee9c684 804{
2f0993e7 805 edge e;
4ee9c684 806 basic_block parent;
2f0993e7 807 struct edge_info *edge_info;
4ee9c684 808
0975351b 809 /* If our parent block ended with a control statement, then we may be
4ee9c684 810 able to record some equivalences based on which outgoing edge from
811 the parent was followed. */
812 parent = get_immediate_dominator (CDI_DOMINATORS, bb);
4ee9c684 813
2f0993e7 814 e = single_incoming_edge_ignoring_loop_edges (bb);
4ee9c684 815
2f0993e7 816 /* If we had a single incoming edge from our parent block, then enter
817 any data associated with the edge into our tables. */
818 if (e && e->src == parent)
4ee9c684 819 {
2f0993e7 820 unsigned int i;
4ee9c684 821
945865c5 822 edge_info = (struct edge_info *) e->aux;
4ee9c684 823
2f0993e7 824 if (edge_info)
4ee9c684 825 {
2f0993e7 826 tree lhs = edge_info->lhs;
827 tree rhs = edge_info->rhs;
828 tree *cond_equivalences = edge_info->cond_equivalences;
829
830 if (lhs)
831 record_equality (lhs, rhs);
832
833 if (cond_equivalences)
4ee9c684 834 {
2f0993e7 835 for (i = 0; i < edge_info->max_cond_equivalences; i += 2)
4ee9c684 836 {
2f0993e7 837 tree expr = cond_equivalences[i];
838 tree value = cond_equivalences[i + 1];
839
840 record_cond (expr, value);
4ee9c684 841 }
842 }
4ee9c684 843 }
844 }
4ee9c684 845}
846
847/* Dump SSA statistics on FILE. */
848
849void
850dump_dominator_optimization_stats (FILE *file)
851{
852 long n_exprs;
853
854 fprintf (file, "Total number of statements: %6ld\n\n",
855 opt_stats.num_stmts);
856 fprintf (file, "Exprs considered for dominator optimizations: %6ld\n",
857 opt_stats.num_exprs_considered);
858
859 n_exprs = opt_stats.num_exprs_considered;
860 if (n_exprs == 0)
861 n_exprs = 1;
862
863 fprintf (file, " Redundant expressions eliminated: %6ld (%.0f%%)\n",
864 opt_stats.num_re, PERCENT (opt_stats.num_re,
865 n_exprs));
88dbf20f 866 fprintf (file, " Constants propagated: %6ld\n",
867 opt_stats.num_const_prop);
868 fprintf (file, " Copies propagated: %6ld\n",
869 opt_stats.num_copy_prop);
4ee9c684 870
871 fprintf (file, "\nHash table statistics:\n");
872
873 fprintf (file, " avail_exprs: ");
874 htab_statistics (file, avail_exprs);
875}
876
877
878/* Dump SSA statistics on stderr. */
879
880void
881debug_dominator_optimization_stats (void)
882{
883 dump_dominator_optimization_stats (stderr);
884}
885
886
887/* Dump statistics for the hash table HTAB. */
888
889static void
890htab_statistics (FILE *file, htab_t htab)
891{
892 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
893 (long) htab_size (htab),
894 (long) htab_elements (htab),
895 htab_collisions (htab));
896}
897
4ee9c684 898/* Enter a statement into the true/false expression hash table indicating
899 that the condition COND has the value VALUE. */
900
901static void
9c629f0e 902record_cond (tree cond, tree value)
4ee9c684 903{
945865c5 904 struct expr_hash_elt *element = XCNEW (struct expr_hash_elt);
4ee9c684 905 void **slot;
906
907 initialize_hash_element (cond, value, element);
908
909 slot = htab_find_slot_with_hash (avail_exprs, (void *)element,
67c4f309 910 element->hash, INSERT);
4ee9c684 911 if (*slot == NULL)
912 {
913 *slot = (void *) element;
046bfc77 914 VEC_safe_push (tree, heap, avail_exprs_stack, cond);
4ee9c684 915 }
916 else
917 free (element);
918}
919
2f0993e7 920/* Build a new conditional using NEW_CODE, OP0 and OP1 and store
921 the new conditional into *p, then store a boolean_true_node
822e391f 922 into *(p + 1). */
2f0993e7 923
924static void
925build_and_record_new_cond (enum tree_code new_code, tree op0, tree op1, tree *p)
926{
927 *p = build2 (new_code, boolean_type_node, op0, op1);
928 p++;
929 *p = boolean_true_node;
930}
931
932/* Record that COND is true and INVERTED is false into the edge information
933 structure. Also record that any conditions dominated by COND are true
934 as well.
043d0665 935
936 For example, if a < b is true, then a <= b must also be true. */
937
938static void
2f0993e7 939record_conditions (struct edge_info *edge_info, tree cond, tree inverted)
043d0665 940{
2f0993e7 941 tree op0, op1;
942
943 if (!COMPARISON_CLASS_P (cond))
944 return;
945
946 op0 = TREE_OPERAND (cond, 0);
947 op1 = TREE_OPERAND (cond, 1);
948
043d0665 949 switch (TREE_CODE (cond))
950 {
951 case LT_EXPR:
043d0665 952 case GT_EXPR:
2f0993e7 953 edge_info->max_cond_equivalences = 12;
945865c5 954 edge_info->cond_equivalences = XNEWVEC (tree, 12);
2f0993e7 955 build_and_record_new_cond ((TREE_CODE (cond) == LT_EXPR
956 ? LE_EXPR : GE_EXPR),
957 op0, op1, &edge_info->cond_equivalences[4]);
958 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
959 &edge_info->cond_equivalences[6]);
960 build_and_record_new_cond (NE_EXPR, op0, op1,
961 &edge_info->cond_equivalences[8]);
962 build_and_record_new_cond (LTGT_EXPR, op0, op1,
963 &edge_info->cond_equivalences[10]);
043d0665 964 break;
965
966 case GE_EXPR:
967 case LE_EXPR:
2f0993e7 968 edge_info->max_cond_equivalences = 6;
945865c5 969 edge_info->cond_equivalences = XNEWVEC (tree, 6);
2f0993e7 970 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
971 &edge_info->cond_equivalences[4]);
043d0665 972 break;
973
974 case EQ_EXPR:
2f0993e7 975 edge_info->max_cond_equivalences = 10;
945865c5 976 edge_info->cond_equivalences = XNEWVEC (tree, 10);
2f0993e7 977 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
978 &edge_info->cond_equivalences[4]);
979 build_and_record_new_cond (LE_EXPR, op0, op1,
980 &edge_info->cond_equivalences[6]);
981 build_and_record_new_cond (GE_EXPR, op0, op1,
982 &edge_info->cond_equivalences[8]);
043d0665 983 break;
984
985 case UNORDERED_EXPR:
2f0993e7 986 edge_info->max_cond_equivalences = 16;
945865c5 987 edge_info->cond_equivalences = XNEWVEC (tree, 16);
2f0993e7 988 build_and_record_new_cond (NE_EXPR, op0, op1,
989 &edge_info->cond_equivalences[4]);
990 build_and_record_new_cond (UNLE_EXPR, op0, op1,
991 &edge_info->cond_equivalences[6]);
992 build_and_record_new_cond (UNGE_EXPR, op0, op1,
993 &edge_info->cond_equivalences[8]);
994 build_and_record_new_cond (UNEQ_EXPR, op0, op1,
995 &edge_info->cond_equivalences[10]);
996 build_and_record_new_cond (UNLT_EXPR, op0, op1,
997 &edge_info->cond_equivalences[12]);
998 build_and_record_new_cond (UNGT_EXPR, op0, op1,
999 &edge_info->cond_equivalences[14]);
043d0665 1000 break;
1001
1002 case UNLT_EXPR:
043d0665 1003 case UNGT_EXPR:
2f0993e7 1004 edge_info->max_cond_equivalences = 8;
945865c5 1005 edge_info->cond_equivalences = XNEWVEC (tree, 8);
2f0993e7 1006 build_and_record_new_cond ((TREE_CODE (cond) == UNLT_EXPR
1007 ? UNLE_EXPR : UNGE_EXPR),
1008 op0, op1, &edge_info->cond_equivalences[4]);
1009 build_and_record_new_cond (NE_EXPR, op0, op1,
1010 &edge_info->cond_equivalences[6]);
043d0665 1011 break;
1012
1013 case UNEQ_EXPR:
2f0993e7 1014 edge_info->max_cond_equivalences = 8;
945865c5 1015 edge_info->cond_equivalences = XNEWVEC (tree, 8);
2f0993e7 1016 build_and_record_new_cond (UNLE_EXPR, op0, op1,
1017 &edge_info->cond_equivalences[4]);
1018 build_and_record_new_cond (UNGE_EXPR, op0, op1,
1019 &edge_info->cond_equivalences[6]);
043d0665 1020 break;
1021
1022 case LTGT_EXPR:
2f0993e7 1023 edge_info->max_cond_equivalences = 8;
945865c5 1024 edge_info->cond_equivalences = XNEWVEC (tree, 8);
2f0993e7 1025 build_and_record_new_cond (NE_EXPR, op0, op1,
1026 &edge_info->cond_equivalences[4]);
1027 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1028 &edge_info->cond_equivalences[6]);
1029 break;
043d0665 1030
1031 default:
2f0993e7 1032 edge_info->max_cond_equivalences = 4;
945865c5 1033 edge_info->cond_equivalences = XNEWVEC (tree, 4);
043d0665 1034 break;
1035 }
2f0993e7 1036
1037 /* Now store the original true and false conditions into the first
1038 two slots. */
1039 edge_info->cond_equivalences[0] = cond;
1040 edge_info->cond_equivalences[1] = boolean_true_node;
1041 edge_info->cond_equivalences[2] = inverted;
1042 edge_info->cond_equivalences[3] = boolean_false_node;
043d0665 1043}
1044
4ee9c684 1045/* A helper function for record_const_or_copy and record_equality.
1046 Do the work of recording the value and undo info. */
1047
1048static void
da43203c 1049record_const_or_copy_1 (tree x, tree y, tree prev_x)
4ee9c684 1050{
4c7a0518 1051 SSA_NAME_VALUE (x) = y;
4ee9c684 1052
046bfc77 1053 VEC_reserve (tree, heap, const_and_copies_stack, 2);
1054 VEC_quick_push (tree, const_and_copies_stack, prev_x);
1055 VEC_quick_push (tree, const_and_copies_stack, x);
4ee9c684 1056}
1057
ba4c299c 1058
1059/* Return the loop depth of the basic block of the defining statement of X.
1060 This number should not be treated as absolutely correct because the loop
1061 information may not be completely up-to-date when dom runs. However, it
1062 will be relatively correct, and as more passes are taught to keep loop info
1063 up to date, the result will become more and more accurate. */
1064
88dbf20f 1065int
ba4c299c 1066loop_depth_of_name (tree x)
1067{
1068 tree defstmt;
1069 basic_block defbb;
1070
1071 /* If it's not an SSA_NAME, we have no clue where the definition is. */
1072 if (TREE_CODE (x) != SSA_NAME)
1073 return 0;
1074
1075 /* Otherwise return the loop depth of the defining statement's bb.
1076 Note that there may not actually be a bb for this statement, if the
1077 ssa_name is live on entry. */
1078 defstmt = SSA_NAME_DEF_STMT (x);
1079 defbb = bb_for_stmt (defstmt);
1080 if (!defbb)
1081 return 0;
1082
1083 return defbb->loop_depth;
1084}
1085
1086
4ee9c684 1087/* Record that X is equal to Y in const_and_copies. Record undo
f0458177 1088 information in the block-local vector. */
4ee9c684 1089
1090static void
da43203c 1091record_const_or_copy (tree x, tree y)
4ee9c684 1092{
4c7a0518 1093 tree prev_x = SSA_NAME_VALUE (x);
4ee9c684 1094
1095 if (TREE_CODE (y) == SSA_NAME)
1096 {
4c7a0518 1097 tree tmp = SSA_NAME_VALUE (y);
4ee9c684 1098 if (tmp)
1099 y = tmp;
1100 }
1101
da43203c 1102 record_const_or_copy_1 (x, y, prev_x);
4ee9c684 1103}
1104
1105/* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1106 This constrains the cases in which we may treat this as assignment. */
1107
1108static void
da43203c 1109record_equality (tree x, tree y)
4ee9c684 1110{
1111 tree prev_x = NULL, prev_y = NULL;
1112
1113 if (TREE_CODE (x) == SSA_NAME)
4c7a0518 1114 prev_x = SSA_NAME_VALUE (x);
4ee9c684 1115 if (TREE_CODE (y) == SSA_NAME)
4c7a0518 1116 prev_y = SSA_NAME_VALUE (y);
4ee9c684 1117
ba4c299c 1118 /* If one of the previous values is invariant, or invariant in more loops
1119 (by depth), then use that.
4ee9c684 1120 Otherwise it doesn't matter which value we choose, just so
1121 long as we canonicalize on one value. */
1122 if (TREE_INVARIANT (y))
1123 ;
ba4c299c 1124 else if (TREE_INVARIANT (x) || (loop_depth_of_name (x) <= loop_depth_of_name (y)))
4ee9c684 1125 prev_x = x, x = y, y = prev_x, prev_x = prev_y;
1126 else if (prev_x && TREE_INVARIANT (prev_x))
1127 x = y, y = prev_x, prev_x = prev_y;
4c7a0518 1128 else if (prev_y && TREE_CODE (prev_y) != VALUE_HANDLE)
4ee9c684 1129 y = prev_y;
1130
1131 /* After the swapping, we must have one SSA_NAME. */
1132 if (TREE_CODE (x) != SSA_NAME)
1133 return;
1134
1135 /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1136 variable compared against zero. If we're honoring signed zeros,
1137 then we cannot record this value unless we know that the value is
365db11e 1138 nonzero. */
4ee9c684 1139 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (x)))
1140 && (TREE_CODE (y) != REAL_CST
1141 || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (y))))
1142 return;
1143
da43203c 1144 record_const_or_copy_1 (x, y, prev_x);
4ee9c684 1145}
1146
119a0489 1147/* Returns true when STMT is a simple iv increment. It detects the
1148 following situation:
1149
1150 i_1 = phi (..., i_2)
1151 i_2 = i_1 +/- ... */
1152
1153static bool
1154simple_iv_increment_p (tree stmt)
1155{
1156 tree lhs, rhs, preinc, phi;
1157 unsigned i;
1158
1159 if (TREE_CODE (stmt) != MODIFY_EXPR)
1160 return false;
1161
1162 lhs = TREE_OPERAND (stmt, 0);
1163 if (TREE_CODE (lhs) != SSA_NAME)
1164 return false;
1165
1166 rhs = TREE_OPERAND (stmt, 1);
1167
1168 if (TREE_CODE (rhs) != PLUS_EXPR
1169 && TREE_CODE (rhs) != MINUS_EXPR)
1170 return false;
1171
1172 preinc = TREE_OPERAND (rhs, 0);
1173 if (TREE_CODE (preinc) != SSA_NAME)
1174 return false;
1175
1176 phi = SSA_NAME_DEF_STMT (preinc);
1177 if (TREE_CODE (phi) != PHI_NODE)
1178 return false;
1179
1180 for (i = 0; i < (unsigned) PHI_NUM_ARGS (phi); i++)
1181 if (PHI_ARG_DEF (phi, i) == lhs)
1182 return true;
1183
1184 return false;
1185}
1186
591c2a30 1187/* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
1188 known value for that SSA_NAME (or NULL if no value is known).
1189
8dbf774a 1190 Propagate values from CONST_AND_COPIES into the PHI nodes of the
1191 successors of BB. */
591c2a30 1192
1193static void
8dbf774a 1194cprop_into_successor_phis (basic_block bb)
591c2a30 1195{
1196 edge e;
cd665a06 1197 edge_iterator ei;
591c2a30 1198
cd665a06 1199 FOR_EACH_EDGE (e, ei, bb->succs)
591c2a30 1200 {
1201 tree phi;
5f50f9bf 1202 int indx;
591c2a30 1203
1204 /* If this is an abnormal edge, then we do not want to copy propagate
1205 into the PHI alternative associated with this edge. */
1206 if (e->flags & EDGE_ABNORMAL)
1207 continue;
1208
1209 phi = phi_nodes (e->dest);
1210 if (! phi)
1211 continue;
1212
5f50f9bf 1213 indx = e->dest_idx;
591c2a30 1214 for ( ; phi; phi = PHI_CHAIN (phi))
1215 {
591c2a30 1216 tree new;
1217 use_operand_p orig_p;
1218 tree orig;
1219
591c2a30 1220 /* The alternative may be associated with a constant, so verify
1221 it is an SSA_NAME before doing anything with it. */
5f50f9bf 1222 orig_p = PHI_ARG_DEF_PTR (phi, indx);
591c2a30 1223 orig = USE_FROM_PTR (orig_p);
1224 if (TREE_CODE (orig) != SSA_NAME)
1225 continue;
1226
591c2a30 1227 /* If we have *ORIG_P in our constant/copy table, then replace
1228 ORIG_P with its value in our constant/copy table. */
4c7a0518 1229 new = SSA_NAME_VALUE (orig);
591c2a30 1230 if (new
88dbf20f 1231 && new != orig
591c2a30 1232 && (TREE_CODE (new) == SSA_NAME
1233 || is_gimple_min_invariant (new))
1234 && may_propagate_copy (orig, new))
88dbf20f 1235 propagate_value (orig_p, new);
591c2a30 1236 }
1237 }
1238}
1239
2f0993e7 1240/* We have finished optimizing BB, record any information implied by
1241 taking a specific outgoing edge from BB. */
1242
1243static void
1244record_edge_info (basic_block bb)
1245{
1246 block_stmt_iterator bsi = bsi_last (bb);
1247 struct edge_info *edge_info;
1248
1249 if (! bsi_end_p (bsi))
1250 {
1251 tree stmt = bsi_stmt (bsi);
1252
1253 if (stmt && TREE_CODE (stmt) == SWITCH_EXPR)
1254 {
1255 tree cond = SWITCH_COND (stmt);
1256
1257 if (TREE_CODE (cond) == SSA_NAME)
1258 {
1259 tree labels = SWITCH_LABELS (stmt);
1260 int i, n_labels = TREE_VEC_LENGTH (labels);
945865c5 1261 tree *info = XCNEWVEC (tree, last_basic_block);
2f0993e7 1262 edge e;
1263 edge_iterator ei;
1264
1265 for (i = 0; i < n_labels; i++)
1266 {
1267 tree label = TREE_VEC_ELT (labels, i);
1268 basic_block target_bb = label_to_block (CASE_LABEL (label));
1269
1270 if (CASE_HIGH (label)
1271 || !CASE_LOW (label)
1272 || info[target_bb->index])
1273 info[target_bb->index] = error_mark_node;
1274 else
1275 info[target_bb->index] = label;
1276 }
1277
1278 FOR_EACH_EDGE (e, ei, bb->succs)
1279 {
1280 basic_block target_bb = e->dest;
1281 tree node = info[target_bb->index];
591c2a30 1282
2f0993e7 1283 if (node != NULL && node != error_mark_node)
1284 {
1285 tree x = fold_convert (TREE_TYPE (cond), CASE_LOW (node));
1286 edge_info = allocate_edge_info (e);
1287 edge_info->lhs = cond;
1288 edge_info->rhs = x;
1289 }
1290 }
1291 free (info);
1292 }
1293 }
1294
1295 /* A COND_EXPR may create equivalences too. */
1296 if (stmt && TREE_CODE (stmt) == COND_EXPR)
1297 {
1298 tree cond = COND_EXPR_COND (stmt);
1299 edge true_edge;
1300 edge false_edge;
1301
1302 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1303
640e9781 1304 /* If the conditional is a single variable 'X', record 'X = 1'
2f0993e7 1305 for the true edge and 'X = 0' on the false edge. */
1306 if (SSA_VAR_P (cond))
1307 {
1308 struct edge_info *edge_info;
1309
1310 edge_info = allocate_edge_info (true_edge);
1311 edge_info->lhs = cond;
1312 edge_info->rhs = constant_boolean_node (1, TREE_TYPE (cond));
1313
1314 edge_info = allocate_edge_info (false_edge);
1315 edge_info->lhs = cond;
1316 edge_info->rhs = constant_boolean_node (0, TREE_TYPE (cond));
1317 }
1318 /* Equality tests may create one or two equivalences. */
1319 else if (COMPARISON_CLASS_P (cond))
1320 {
1321 tree op0 = TREE_OPERAND (cond, 0);
1322 tree op1 = TREE_OPERAND (cond, 1);
1323
1324 /* Special case comparing booleans against a constant as we
1325 know the value of OP0 on both arms of the branch. i.e., we
1326 can record an equivalence for OP0 rather than COND. */
1327 if ((TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
1328 && TREE_CODE (op0) == SSA_NAME
1329 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
1330 && is_gimple_min_invariant (op1))
1331 {
1332 if (TREE_CODE (cond) == EQ_EXPR)
1333 {
1334 edge_info = allocate_edge_info (true_edge);
1335 edge_info->lhs = op0;
1336 edge_info->rhs = (integer_zerop (op1)
1337 ? boolean_false_node
1338 : boolean_true_node);
1339
1340 edge_info = allocate_edge_info (false_edge);
1341 edge_info->lhs = op0;
1342 edge_info->rhs = (integer_zerop (op1)
1343 ? boolean_true_node
1344 : boolean_false_node);
1345 }
1346 else
1347 {
1348 edge_info = allocate_edge_info (true_edge);
1349 edge_info->lhs = op0;
1350 edge_info->rhs = (integer_zerop (op1)
1351 ? boolean_true_node
1352 : boolean_false_node);
1353
1354 edge_info = allocate_edge_info (false_edge);
1355 edge_info->lhs = op0;
1356 edge_info->rhs = (integer_zerop (op1)
1357 ? boolean_false_node
1358 : boolean_true_node);
1359 }
1360 }
1361
a07a7473 1362 else if (is_gimple_min_invariant (op0)
1363 && (TREE_CODE (op1) == SSA_NAME
1364 || is_gimple_min_invariant (op1)))
2f0993e7 1365 {
1366 tree inverted = invert_truthvalue (cond);
1367 struct edge_info *edge_info;
1368
1369 edge_info = allocate_edge_info (true_edge);
1370 record_conditions (edge_info, cond, inverted);
1371
1372 if (TREE_CODE (cond) == EQ_EXPR)
1373 {
1374 edge_info->lhs = op1;
1375 edge_info->rhs = op0;
1376 }
1377
1378 edge_info = allocate_edge_info (false_edge);
1379 record_conditions (edge_info, inverted, cond);
1380
1381 if (TREE_CODE (cond) == NE_EXPR)
1382 {
1383 edge_info->lhs = op1;
1384 edge_info->rhs = op0;
1385 }
1386 }
1387
a07a7473 1388 else if (TREE_CODE (op0) == SSA_NAME
1389 && (is_gimple_min_invariant (op1)
1390 || TREE_CODE (op1) == SSA_NAME))
2f0993e7 1391 {
1392 tree inverted = invert_truthvalue (cond);
1393 struct edge_info *edge_info;
1394
1395 edge_info = allocate_edge_info (true_edge);
1396 record_conditions (edge_info, cond, inverted);
1397
1398 if (TREE_CODE (cond) == EQ_EXPR)
1399 {
1400 edge_info->lhs = op0;
1401 edge_info->rhs = op1;
1402 }
1403
1404 edge_info = allocate_edge_info (false_edge);
1405 record_conditions (edge_info, inverted, cond);
1406
1407 if (TREE_CODE (cond) == NE_EXPR)
1408 {
1409 edge_info->lhs = op0;
1410 edge_info->rhs = op1;
1411 }
1412 }
1413 }
1414
1415 /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
1416 }
1417 }
1418}
1419
1420/* Propagate information from BB to its outgoing edges.
1421
1422 This can include equivalency information implied by control statements
1423 at the end of BB and const/copy propagation into PHIs in BB's
1424 successor blocks. */
4ee9c684 1425
1426static void
2f0993e7 1427propagate_to_outgoing_edges (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1428 basic_block bb)
4ee9c684 1429{
2f0993e7 1430 record_edge_info (bb);
8dbf774a 1431 cprop_into_successor_phis (bb);
4ee9c684 1432}
1433
1434/* Search for redundant computations in STMT. If any are found, then
1435 replace them with the variable holding the result of the computation.
1436
1437 If safe, record this expression into the available expression hash
1438 table. */
1439
1440static bool
8f628ee8 1441eliminate_redundant_computations (tree stmt)
4ee9c684 1442{
4ee9c684 1443 tree *expr_p, def = NULL_TREE;
1444 bool insert = true;
1445 tree cached_lhs;
1446 bool retval = false;
f6be5aa5 1447 bool modify_expr_p = false;
4ee9c684 1448
1449 if (TREE_CODE (stmt) == MODIFY_EXPR)
1450 def = TREE_OPERAND (stmt, 0);
1451
1452 /* Certain expressions on the RHS can be optimized away, but can not
dac49aa5 1453 themselves be entered into the hash tables. */
9d637cc5 1454 if (! def
4ee9c684 1455 || TREE_CODE (def) != SSA_NAME
1456 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)
b66731e8 1457 || !ZERO_SSA_OPERANDS (stmt, SSA_OP_VMAYDEF)
119a0489 1458 /* Do not record equivalences for increments of ivs. This would create
1459 overlapping live ranges for a very questionable gain. */
1460 || simple_iv_increment_p (stmt))
4ee9c684 1461 insert = false;
1462
1463 /* Check if the expression has been computed before. */
9c629f0e 1464 cached_lhs = lookup_avail_expr (stmt, insert);
4ee9c684 1465
4ee9c684 1466 opt_stats.num_exprs_considered++;
1467
1468 /* Get a pointer to the expression we are trying to optimize. */
1469 if (TREE_CODE (stmt) == COND_EXPR)
1470 expr_p = &COND_EXPR_COND (stmt);
1471 else if (TREE_CODE (stmt) == SWITCH_EXPR)
1472 expr_p = &SWITCH_COND (stmt);
1473 else if (TREE_CODE (stmt) == RETURN_EXPR && TREE_OPERAND (stmt, 0))
f6be5aa5 1474 {
1475 expr_p = &TREE_OPERAND (TREE_OPERAND (stmt, 0), 1);
1476 modify_expr_p = true;
1477 }
4ee9c684 1478 else
f6be5aa5 1479 {
1480 expr_p = &TREE_OPERAND (stmt, 1);
1481 modify_expr_p = true;
1482 }
4ee9c684 1483
1484 /* It is safe to ignore types here since we have already done
1485 type checking in the hashing and equality routines. In fact
1486 type checking here merely gets in the way of constant
1487 propagation. Also, make sure that it is safe to propagate
1488 CACHED_LHS into *EXPR_P. */
1489 if (cached_lhs
f6be5aa5 1490 && ((TREE_CODE (cached_lhs) != SSA_NAME
1491 && (modify_expr_p
1492 || tree_ssa_useless_type_conversion_1 (TREE_TYPE (*expr_p),
1493 TREE_TYPE (cached_lhs))))
591c2a30 1494 || may_propagate_copy (*expr_p, cached_lhs)))
4ee9c684 1495 {
1496 if (dump_file && (dump_flags & TDF_DETAILS))
1497 {
1498 fprintf (dump_file, " Replaced redundant expr '");
1499 print_generic_expr (dump_file, *expr_p, dump_flags);
1500 fprintf (dump_file, "' with '");
1501 print_generic_expr (dump_file, cached_lhs, dump_flags);
1502 fprintf (dump_file, "'\n");
1503 }
1504
1505 opt_stats.num_re++;
1506
1507#if defined ENABLE_CHECKING
8c0963c4 1508 gcc_assert (TREE_CODE (cached_lhs) == SSA_NAME
1509 || is_gimple_min_invariant (cached_lhs));
4ee9c684 1510#endif
1511
1512 if (TREE_CODE (cached_lhs) == ADDR_EXPR
1513 || (POINTER_TYPE_P (TREE_TYPE (*expr_p))
1514 && is_gimple_min_invariant (cached_lhs)))
1515 retval = true;
f6be5aa5 1516
1517 if (modify_expr_p
1518 && !tree_ssa_useless_type_conversion_1 (TREE_TYPE (*expr_p),
1519 TREE_TYPE (cached_lhs)))
1520 cached_lhs = fold_convert (TREE_TYPE (*expr_p), cached_lhs);
4ee9c684 1521
56004dc5 1522 propagate_tree_value (expr_p, cached_lhs);
22aa74c4 1523 mark_stmt_modified (stmt);
4ee9c684 1524 }
1525 return retval;
1526}
1527
1528/* STMT, a MODIFY_EXPR, may create certain equivalences, in either
1529 the available expressions table or the const_and_copies table.
1530 Detect and record those equivalences. */
1531
1532static void
1533record_equivalences_from_stmt (tree stmt,
4ee9c684 1534 int may_optimize_p,
1535 stmt_ann_t ann)
1536{
1537 tree lhs = TREE_OPERAND (stmt, 0);
1538 enum tree_code lhs_code = TREE_CODE (lhs);
4ee9c684 1539
1540 if (lhs_code == SSA_NAME)
1541 {
1542 tree rhs = TREE_OPERAND (stmt, 1);
1543
1544 /* Strip away any useless type conversions. */
1545 STRIP_USELESS_TYPE_CONVERSION (rhs);
1546
1547 /* If the RHS of the assignment is a constant or another variable that
1548 may be propagated, register it in the CONST_AND_COPIES table. We
1549 do not need to record unwind data for this, since this is a true
365db11e 1550 assignment and not an equivalence inferred from a comparison. All
4ee9c684 1551 uses of this ssa name are dominated by this assignment, so unwinding
1552 just costs time and space. */
1553 if (may_optimize_p
1554 && (TREE_CODE (rhs) == SSA_NAME
1555 || is_gimple_min_invariant (rhs)))
4c7a0518 1556 SSA_NAME_VALUE (lhs) = rhs;
4ee9c684 1557 }
1558
4ee9c684 1559 /* A memory store, even an aliased store, creates a useful
1560 equivalence. By exchanging the LHS and RHS, creating suitable
1561 vops and recording the result in the available expression table,
1562 we may be able to expose more redundant loads. */
1563 if (!ann->has_volatile_ops
1564 && (TREE_CODE (TREE_OPERAND (stmt, 1)) == SSA_NAME
1565 || is_gimple_min_invariant (TREE_OPERAND (stmt, 1)))
1566 && !is_gimple_reg (lhs))
1567 {
1568 tree rhs = TREE_OPERAND (stmt, 1);
1569 tree new;
4ee9c684 1570
1571 /* FIXME: If the LHS of the assignment is a bitfield and the RHS
1572 is a constant, we need to adjust the constant to fit into the
1573 type of the LHS. If the LHS is a bitfield and the RHS is not
1574 a constant, then we can not record any equivalences for this
1575 statement since we would need to represent the widening or
1576 narrowing of RHS. This fixes gcc.c-torture/execute/921016-1.c
1577 and should not be necessary if GCC represented bitfields
1578 properly. */
1579 if (lhs_code == COMPONENT_REF
1580 && DECL_BIT_FIELD (TREE_OPERAND (lhs, 1)))
1581 {
1582 if (TREE_CONSTANT (rhs))
1583 rhs = widen_bitfield (rhs, TREE_OPERAND (lhs, 1), lhs);
1584 else
1585 rhs = NULL;
1586
1587 /* If the value overflowed, then we can not use this equivalence. */
1588 if (rhs && ! is_gimple_min_invariant (rhs))
1589 rhs = NULL;
1590 }
1591
1592 if (rhs)
1593 {
4ee9c684 1594 /* Build a new statement with the RHS and LHS exchanged. */
40b19772 1595 new = build2 (MODIFY_EXPR, TREE_TYPE (stmt), rhs, lhs);
4ee9c684 1596
b66731e8 1597 create_ssa_artficial_load_stmt (new, stmt);
4ee9c684 1598
1599 /* Finally enter the statement into the available expression
1600 table. */
9c629f0e 1601 lookup_avail_expr (new, true);
4ee9c684 1602 }
1603 }
1604}
1605
591c2a30 1606/* Replace *OP_P in STMT with any known equivalent value for *OP_P from
1607 CONST_AND_COPIES. */
1608
1609static bool
fa0f49c6 1610cprop_operand (tree stmt, use_operand_p op_p)
591c2a30 1611{
1612 bool may_have_exposed_new_symbols = false;
1613 tree val;
1614 tree op = USE_FROM_PTR (op_p);
1615
1616 /* If the operand has a known constant value or it is known to be a
1617 copy of some other variable, use the value or copy stored in
1618 CONST_AND_COPIES. */
4c7a0518 1619 val = SSA_NAME_VALUE (op);
88dbf20f 1620 if (val && val != op && TREE_CODE (val) != VALUE_HANDLE)
591c2a30 1621 {
1622 tree op_type, val_type;
1623
1624 /* Do not change the base variable in the virtual operand
1625 tables. That would make it impossible to reconstruct
1626 the renamed virtual operand if we later modify this
1627 statement. Also only allow the new value to be an SSA_NAME
1628 for propagation into virtual operands. */
1629 if (!is_gimple_reg (op)
88dbf20f 1630 && (TREE_CODE (val) != SSA_NAME
1631 || is_gimple_reg (val)
1632 || get_virtual_var (val) != get_virtual_var (op)))
591c2a30 1633 return false;
1634
93b4f514 1635 /* Do not replace hard register operands in asm statements. */
1636 if (TREE_CODE (stmt) == ASM_EXPR
1637 && !may_propagate_copy_into_asm (op))
1638 return false;
1639
591c2a30 1640 /* Get the toplevel type of each operand. */
1641 op_type = TREE_TYPE (op);
1642 val_type = TREE_TYPE (val);
1643
1644 /* While both types are pointers, get the type of the object
1645 pointed to. */
1646 while (POINTER_TYPE_P (op_type) && POINTER_TYPE_P (val_type))
1647 {
1648 op_type = TREE_TYPE (op_type);
1649 val_type = TREE_TYPE (val_type);
1650 }
1651
4f7f73c8 1652 /* Make sure underlying types match before propagating a constant by
1653 converting the constant to the proper type. Note that convert may
1654 return a non-gimple expression, in which case we ignore this
1655 propagation opportunity. */
1656 if (TREE_CODE (val) != SSA_NAME)
591c2a30 1657 {
4f7f73c8 1658 if (!lang_hooks.types_compatible_p (op_type, val_type))
1659 {
1660 val = fold_convert (TREE_TYPE (op), val);
1661 if (!is_gimple_min_invariant (val))
1662 return false;
1663 }
591c2a30 1664 }
1665
1666 /* Certain operands are not allowed to be copy propagated due
1667 to their interaction with exception handling and some GCC
1668 extensions. */
4f7f73c8 1669 else if (!may_propagate_copy (op, val))
591c2a30 1670 return false;
652a5bec 1671
1672 /* Do not propagate copies if the propagated value is at a deeper loop
1673 depth than the propagatee. Otherwise, this may move loop variant
1674 variables outside of their loops and prevent coalescing
1675 opportunities. If the value was loop invariant, it will be hoisted
1676 by LICM and exposed for copy propagation. */
1677 if (loop_depth_of_name (val) > loop_depth_of_name (op))
1678 return false;
591c2a30 1679
1680 /* Dump details. */
1681 if (dump_file && (dump_flags & TDF_DETAILS))
1682 {
1683 fprintf (dump_file, " Replaced '");
1684 print_generic_expr (dump_file, op, dump_flags);
1685 fprintf (dump_file, "' with %s '",
1686 (TREE_CODE (val) != SSA_NAME ? "constant" : "variable"));
1687 print_generic_expr (dump_file, val, dump_flags);
1688 fprintf (dump_file, "'\n");
1689 }
1690
1691 /* If VAL is an ADDR_EXPR or a constant of pointer type, note
1692 that we may have exposed a new symbol for SSA renaming. */
1693 if (TREE_CODE (val) == ADDR_EXPR
1694 || (POINTER_TYPE_P (TREE_TYPE (op))
1695 && is_gimple_min_invariant (val)))
1696 may_have_exposed_new_symbols = true;
1697
88dbf20f 1698 if (TREE_CODE (val) != SSA_NAME)
1699 opt_stats.num_const_prop++;
1700 else
1701 opt_stats.num_copy_prop++;
1702
591c2a30 1703 propagate_value (op_p, val);
1704
1705 /* And note that we modified this statement. This is now
1706 safe, even if we changed virtual operands since we will
1707 rescan the statement and rewrite its operands again. */
22aa74c4 1708 mark_stmt_modified (stmt);
591c2a30 1709 }
1710 return may_have_exposed_new_symbols;
1711}
1712
1713/* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
1714 known value for that SSA_NAME (or NULL if no value is known).
1715
1716 Propagate values from CONST_AND_COPIES into the uses, vuses and
1717 v_may_def_ops of STMT. */
1718
1719static bool
fa0f49c6 1720cprop_into_stmt (tree stmt)
591c2a30 1721{
1722 bool may_have_exposed_new_symbols = false;
43daa21e 1723 use_operand_p op_p;
1724 ssa_op_iter iter;
591c2a30 1725
43daa21e 1726 FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_ALL_USES)
591c2a30 1727 {
591c2a30 1728 if (TREE_CODE (USE_FROM_PTR (op_p)) == SSA_NAME)
fa0f49c6 1729 may_have_exposed_new_symbols |= cprop_operand (stmt, op_p);
591c2a30 1730 }
1731
591c2a30 1732 return may_have_exposed_new_symbols;
1733}
1734
1735
5206b159 1736/* Optimize the statement pointed to by iterator SI.
4ee9c684 1737
1738 We try to perform some simplistic global redundancy elimination and
1739 constant propagation:
1740
1741 1- To detect global redundancy, we keep track of expressions that have
1742 been computed in this block and its dominators. If we find that the
1743 same expression is computed more than once, we eliminate repeated
1744 computations by using the target of the first one.
1745
1746 2- Constant values and copy assignments. This is used to do very
1747 simplistic constant and copy propagation. When a constant or copy
1748 assignment is found, we map the value on the RHS of the assignment to
1749 the variable in the LHS in the CONST_AND_COPIES table. */
1750
1751static void
15ea1735 1752optimize_stmt (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1753 basic_block bb, block_stmt_iterator si)
4ee9c684 1754{
1755 stmt_ann_t ann;
4c27dd45 1756 tree stmt, old_stmt;
4ee9c684 1757 bool may_optimize_p;
1758 bool may_have_exposed_new_symbols = false;
4ee9c684 1759
4c27dd45 1760 old_stmt = stmt = bsi_stmt (si);
54aceb26 1761
1762 if (TREE_CODE (stmt) == COND_EXPR)
1763 canonicalize_comparison (stmt);
1764
22aa74c4 1765 update_stmt_if_modified (stmt);
4ee9c684 1766 ann = stmt_ann (stmt);
4ee9c684 1767 opt_stats.num_stmts++;
1768 may_have_exposed_new_symbols = false;
1769
1770 if (dump_file && (dump_flags & TDF_DETAILS))
1771 {
1772 fprintf (dump_file, "Optimizing statement ");
1773 print_generic_stmt (dump_file, stmt, TDF_SLIM);
1774 }
1775
2cf24776 1776 /* Const/copy propagate into USES, VUSES and the RHS of V_MAY_DEFs. */
fa0f49c6 1777 may_have_exposed_new_symbols = cprop_into_stmt (stmt);
4ee9c684 1778
1779 /* If the statement has been modified with constant replacements,
1780 fold its RHS before checking for redundant computations. */
1781 if (ann->modified)
1782 {
f2fae51f 1783 tree rhs;
1784
4ee9c684 1785 /* Try to fold the statement making sure that STMT is kept
1786 up to date. */
1787 if (fold_stmt (bsi_stmt_ptr (si)))
1788 {
1789 stmt = bsi_stmt (si);
1790 ann = stmt_ann (stmt);
1791
1792 if (dump_file && (dump_flags & TDF_DETAILS))
1793 {
1794 fprintf (dump_file, " Folded to: ");
1795 print_generic_stmt (dump_file, stmt, TDF_SLIM);
1796 }
1797 }
1798
f2fae51f 1799 rhs = get_rhs (stmt);
1800 if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
750ad201 1801 recompute_tree_invariant_for_addr_expr (rhs);
f2fae51f 1802
4ee9c684 1803 /* Constant/copy propagation above may change the set of
1804 virtual operands associated with this statement. Folding
1805 may remove the need for some virtual operands.
1806
1807 Indicate we will need to rescan and rewrite the statement. */
1808 may_have_exposed_new_symbols = true;
1809 }
1810
1811 /* Check for redundant computations. Do this optimization only
1812 for assignments that have no volatile ops and conditionals. */
1813 may_optimize_p = (!ann->has_volatile_ops
1814 && ((TREE_CODE (stmt) == RETURN_EXPR
1815 && TREE_OPERAND (stmt, 0)
1816 && TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR
1817 && ! (TREE_SIDE_EFFECTS
1818 (TREE_OPERAND (TREE_OPERAND (stmt, 0), 1))))
1819 || (TREE_CODE (stmt) == MODIFY_EXPR
1820 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (stmt, 1)))
1821 || TREE_CODE (stmt) == COND_EXPR
1822 || TREE_CODE (stmt) == SWITCH_EXPR));
1823
1824 if (may_optimize_p)
8f628ee8 1825 may_have_exposed_new_symbols |= eliminate_redundant_computations (stmt);
4ee9c684 1826
1827 /* Record any additional equivalences created by this statement. */
1828 if (TREE_CODE (stmt) == MODIFY_EXPR)
1829 record_equivalences_from_stmt (stmt,
4ee9c684 1830 may_optimize_p,
1831 ann);
1832
4ee9c684 1833 /* If STMT is a COND_EXPR and it was modified, then we may know
1834 where it goes. If that is the case, then mark the CFG as altered.
1835
1836 This will cause us to later call remove_unreachable_blocks and
1837 cleanup_tree_cfg when it is safe to do so. It is not safe to
1838 clean things up here since removal of edges and such can trigger
1839 the removal of PHI nodes, which in turn can release SSA_NAMEs to
1840 the manager.
1841
1842 That's all fine and good, except that once SSA_NAMEs are released
1843 to the manager, we must not call create_ssa_name until all references
1844 to released SSA_NAMEs have been eliminated.
1845
1846 All references to the deleted SSA_NAMEs can not be eliminated until
1847 we remove unreachable blocks.
1848
1849 We can not remove unreachable blocks until after we have completed
1850 any queued jump threading.
1851
1852 We can not complete any queued jump threads until we have taken
1853 appropriate variables out of SSA form. Taking variables out of
1854 SSA form can call create_ssa_name and thus we lose.
1855
1856 Ultimately I suspect we're going to need to change the interface
1857 into the SSA_NAME manager. */
1858
1859 if (ann->modified)
1860 {
1861 tree val = NULL;
1862
1863 if (TREE_CODE (stmt) == COND_EXPR)
1864 val = COND_EXPR_COND (stmt);
1865 else if (TREE_CODE (stmt) == SWITCH_EXPR)
1866 val = SWITCH_COND (stmt);
1867
35c15734 1868 if (val && TREE_CODE (val) == INTEGER_CST && find_taken_edge (bb, val))
4ee9c684 1869 cfg_altered = true;
35c15734 1870
1871 /* If we simplified a statement in such a way as to be shown that it
1872 cannot trap, update the eh information and the cfg to match. */
4c27dd45 1873 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
35c15734 1874 {
1875 bitmap_set_bit (need_eh_cleanup, bb->index);
1876 if (dump_file && (dump_flags & TDF_DETAILS))
1877 fprintf (dump_file, " Flagged to clear EH edges.\n");
1878 }
4ee9c684 1879 }
35c15734 1880
4ee9c684 1881 if (may_have_exposed_new_symbols)
046bfc77 1882 VEC_safe_push (tree, heap, stmts_to_rescan, bsi_stmt (si));
4ee9c684 1883}
1884
4ee9c684 1885/* Search for an existing instance of STMT in the AVAIL_EXPRS table. If
1886 found, return its LHS. Otherwise insert STMT in the table and return
1887 NULL_TREE.
1888
1889 Also, when an expression is first inserted in the AVAIL_EXPRS table, it
5206b159 1890 is also added to the stack pointed to by BLOCK_AVAIL_EXPRS_P, so that they
4ee9c684 1891 can be removed when we finish processing this block and its children.
1892
1893 NOTE: This function assumes that STMT is a MODIFY_EXPR node that
1894 contains no CALL_EXPR on its RHS and makes no volatile nor
1895 aliased references. */
1896
1897static tree
9c629f0e 1898lookup_avail_expr (tree stmt, bool insert)
4ee9c684 1899{
1900 void **slot;
1901 tree lhs;
1902 tree temp;
945865c5 1903 struct expr_hash_elt *element = XNEW (struct expr_hash_elt);
4ee9c684 1904
1905 lhs = TREE_CODE (stmt) == MODIFY_EXPR ? TREE_OPERAND (stmt, 0) : NULL;
1906
1907 initialize_hash_element (stmt, lhs, element);
1908
1909 /* Don't bother remembering constant assignments and copy operations.
1910 Constants and copy operations are handled by the constant/copy propagator
1911 in optimize_stmt. */
1912 if (TREE_CODE (element->rhs) == SSA_NAME
1913 || is_gimple_min_invariant (element->rhs))
1914 {
1915 free (element);
1916 return NULL_TREE;
1917 }
1918
4ee9c684 1919 /* Finally try to find the expression in the main expression hash table. */
1920 slot = htab_find_slot_with_hash (avail_exprs, element, element->hash,
1921 (insert ? INSERT : NO_INSERT));
1922 if (slot == NULL)
1923 {
1924 free (element);
1925 return NULL_TREE;
1926 }
1927
1928 if (*slot == NULL)
1929 {
1930 *slot = (void *) element;
046bfc77 1931 VEC_safe_push (tree, heap, avail_exprs_stack,
f0458177 1932 stmt ? stmt : element->rhs);
4ee9c684 1933 return NULL_TREE;
1934 }
1935
1936 /* Extract the LHS of the assignment so that it can be used as the current
1937 definition of another variable. */
1938 lhs = ((struct expr_hash_elt *)*slot)->lhs;
1939
1940 /* See if the LHS appears in the CONST_AND_COPIES table. If it does, then
1941 use the value from the const_and_copies table. */
1942 if (TREE_CODE (lhs) == SSA_NAME)
1943 {
4c7a0518 1944 temp = SSA_NAME_VALUE (lhs);
1945 if (temp && TREE_CODE (temp) != VALUE_HANDLE)
4ee9c684 1946 lhs = temp;
1947 }
1948
1949 free (element);
1950 return lhs;
1951}
1952
4ee9c684 1953/* Hashing and equality functions for AVAIL_EXPRS. The table stores
1954 MODIFY_EXPR statements. We compute a value number for expressions using
1955 the code of the expression and the SSA numbers of its operands. */
1956
1957static hashval_t
1958avail_expr_hash (const void *p)
1959{
b66731e8 1960 tree stmt = ((struct expr_hash_elt *)p)->stmt;
4ee9c684 1961 tree rhs = ((struct expr_hash_elt *)p)->rhs;
b66731e8 1962 tree vuse;
1963 ssa_op_iter iter;
4ee9c684 1964 hashval_t val = 0;
4ee9c684 1965
1966 /* iterative_hash_expr knows how to deal with any expression and
1967 deals with commutative operators as well, so just use it instead
1968 of duplicating such complexities here. */
1969 val = iterative_hash_expr (rhs, val);
1970
1971 /* If the hash table entry is not associated with a statement, then we
1972 can just hash the expression and not worry about virtual operands
1973 and such. */
b66731e8 1974 if (!stmt || !stmt_ann (stmt))
4ee9c684 1975 return val;
1976
1977 /* Add the SSA version numbers of every vuse operand. This is important
1978 because compound variables like arrays are not renamed in the
1979 operands. Rather, the rename is done on the virtual variable
1980 representing all the elements of the array. */
b66731e8 1981 FOR_EACH_SSA_TREE_OPERAND (vuse, stmt, iter, SSA_OP_VUSE)
1982 val = iterative_hash_expr (vuse, val);
4ee9c684 1983
1984 return val;
1985}
1986
23ace16d 1987static hashval_t
1988real_avail_expr_hash (const void *p)
1989{
1990 return ((const struct expr_hash_elt *)p)->hash;
1991}
4ee9c684 1992
1993static int
1994avail_expr_eq (const void *p1, const void *p2)
1995{
b66731e8 1996 tree stmt1 = ((struct expr_hash_elt *)p1)->stmt;
4ee9c684 1997 tree rhs1 = ((struct expr_hash_elt *)p1)->rhs;
b66731e8 1998 tree stmt2 = ((struct expr_hash_elt *)p2)->stmt;
4ee9c684 1999 tree rhs2 = ((struct expr_hash_elt *)p2)->rhs;
2000
2001 /* If they are the same physical expression, return true. */
b66731e8 2002 if (rhs1 == rhs2 && stmt1 == stmt2)
4ee9c684 2003 return true;
2004
2005 /* If their codes are not equal, then quit now. */
2006 if (TREE_CODE (rhs1) != TREE_CODE (rhs2))
2007 return false;
2008
2009 /* In case of a collision, both RHS have to be identical and have the
2010 same VUSE operands. */
2011 if ((TREE_TYPE (rhs1) == TREE_TYPE (rhs2)
2012 || lang_hooks.types_compatible_p (TREE_TYPE (rhs1), TREE_TYPE (rhs2)))
2013 && operand_equal_p (rhs1, rhs2, OEP_PURE_SAME))
2014 {
b66731e8 2015 bool ret = compare_ssa_operands_equal (stmt1, stmt2, SSA_OP_VUSE);
2016 gcc_assert (!ret || ((struct expr_hash_elt *)p1)->hash
8c0963c4 2017 == ((struct expr_hash_elt *)p2)->hash);
b66731e8 2018 return ret;
4ee9c684 2019 }
2020
2021 return false;
2022}