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