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