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