* Makefile.in (tree-update-ssa.o): Add.
[official-gcc.git] / gcc / tree-ssa-dom.c
blob6f82bde3482a6a57512382bb60643572c5dd70d1
1 /* SSA Dominator optimizations for trees
2 Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
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 "output.h"
33 #include "errors.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"
46 /* This file implements optimizations on the dominator tree. */
48 /* Hash table with expressions made available during the renaming process.
49 When an assignment of the form X_i = EXPR is found, the statement is
50 stored in this table. If the same expression EXPR is later found on the
51 RHS of another statement, it is replaced with X_i (thus performing
52 global redundancy elimination). Similarly as we pass through conditionals
53 we record the conditional itself as having either a true or false value
54 in this table. */
55 static htab_t avail_exprs;
57 /* Stack of available expressions in AVAIL_EXPRs. Each block pushes any
58 expressions it enters into the hash table along with a marker entry
59 (null). When we finish processing the block, we pop off entries and
60 remove the expressions from the global hash table until we hit the
61 marker. */
62 static varray_type avail_exprs_stack;
64 /* Stack of trees used to restore the global currdefs to its original
65 state after completing optimization of a block and its dominator children.
67 An SSA_NAME indicates that the current definition of the underlying
68 variable should be set to the given SSA_NAME.
70 A _DECL node indicates that the underlying variable has no current
71 definition.
73 A NULL node is used to mark the last node associated with the
74 current block. */
75 varray_type block_defs_stack;
77 /* Stack of statements we need to rescan during finalization for newly
78 exposed variables.
80 Statement rescanning must occur after the current block's available
81 expressions are removed from AVAIL_EXPRS. Else we may change the
82 hash code for an expression and be unable to find/remove it from
83 AVAIL_EXPRS. */
84 varray_type stmts_to_rescan;
86 /* Structure for entries in the expression hash table.
88 This requires more memory for the hash table entries, but allows us
89 to avoid creating silly tree nodes and annotations for conditionals,
90 eliminates 2 global hash tables and two block local varrays.
92 It also allows us to reduce the number of hash table lookups we
93 have to perform in lookup_avail_expr and finally it allows us to
94 significantly reduce the number of calls into the hashing routine
95 itself. */
97 struct expr_hash_elt
99 /* The value (lhs) of this expression. */
100 tree lhs;
102 /* The expression (rhs) we want to record. */
103 tree rhs;
105 /* The annotation if this element corresponds to a statement. */
106 stmt_ann_t ann;
108 /* The hash value for RHS/ann. */
109 hashval_t hash;
112 /* Stack of dest,src pairs that need to be restored during finalization.
114 A NULL entry is used to mark the end of pairs which need to be
115 restored during finalization of this block. */
116 static varray_type const_and_copies_stack;
118 /* Bitmap of SSA_NAMEs known to have a nonzero value, even if we do not
119 know their exact value. */
120 static bitmap nonzero_vars;
122 /* Stack of SSA_NAMEs which need their NONZERO_VARS property cleared
123 when the current block is finalized.
125 A NULL entry is used to mark the end of names needing their
126 entry in NONZERO_VARS cleared during finalization of this block. */
127 static varray_type nonzero_vars_stack;
129 /* Track whether or not we have changed the control flow graph. */
130 static bool cfg_altered;
132 /* Bitmap of blocks that have had EH statements cleaned. We should
133 remove their dead edges eventually. */
134 static bitmap need_eh_cleanup;
136 /* Statistics for dominator optimizations. */
137 struct opt_stats_d
139 long num_stmts;
140 long num_exprs_considered;
141 long num_re;
144 static struct opt_stats_d opt_stats;
146 /* Value range propagation record. Each time we encounter a conditional
147 of the form SSA_NAME COND CONST we create a new vrp_element to record
148 how the condition affects the possible values SSA_NAME may have.
150 Each record contains the condition tested (COND), and the the range of
151 values the variable may legitimately have if COND is true. Note the
152 range of values may be a smaller range than COND specifies if we have
153 recorded other ranges for this variable. Each record also contains the
154 block in which the range was recorded for invalidation purposes.
156 Note that the current known range is computed lazily. This allows us
157 to avoid the overhead of computing ranges which are never queried.
159 When we encounter a conditional, we look for records which constrain
160 the SSA_NAME used in the condition. In some cases those records allow
161 us to determine the condition's result at compile time. In other cases
162 they may allow us to simplify the condition.
164 We also use value ranges to do things like transform signed div/mod
165 operations into unsigned div/mod or to simplify ABS_EXPRs.
167 Simple experiments have shown these optimizations to not be all that
168 useful on switch statements (much to my surprise). So switch statement
169 optimizations are not performed.
171 Note carefully we do not propagate information through each statement
172 in the block. i.e., if we know variable X has a value defined of
173 [0, 25] and we encounter Y = X + 1, we do not track a value range
174 for Y (which would be [1, 26] if we cared). Similarly we do not
175 constrain values as we encounter narrowing typecasts, etc. */
177 struct vrp_element
179 /* The highest and lowest values the variable in COND may contain when
180 COND is true. Note this may not necessarily be the same values
181 tested by COND if the same variable was used in earlier conditionals.
183 Note this is computed lazily and thus can be NULL indicating that
184 the values have not been computed yet. */
185 tree low;
186 tree high;
188 /* The actual conditional we recorded. This is needed since we compute
189 ranges lazily. */
190 tree cond;
192 /* The basic block where this record was created. We use this to determine
193 when to remove records. */
194 basic_block bb;
197 /* A hash table holding value range records (VRP_ELEMENTs) for a given
198 SSA_NAME. We used to use a varray indexed by SSA_NAME_VERSION, but
199 that gets awful wasteful, particularly since the density objects
200 with useful information is very low. */
201 static htab_t vrp_data;
203 /* An entry in the VRP_DATA hash table. We record the variable and a
204 varray of VRP_ELEMENT records associated with that variable. */
206 struct vrp_hash_elt
208 tree var;
209 varray_type records;
212 /* Array of variables which have their values constrained by operations
213 in this basic block. We use this during finalization to know
214 which variables need their VRP data updated. */
216 /* Stack of SSA_NAMEs which had their values constrainted by operations
217 in this basic block. During finalization of this block we use this
218 list to determine which variables need their VRP data updated.
220 A NULL entry marks the end of the SSA_NAMEs associated with this block. */
221 static varray_type vrp_variables_stack;
223 struct eq_expr_value
225 tree src;
226 tree dst;
229 /* Local functions. */
230 static void optimize_stmt (struct dom_walk_data *,
231 basic_block bb,
232 block_stmt_iterator);
233 static tree lookup_avail_expr (tree, bool);
234 static struct eq_expr_value get_eq_expr_value (tree, int, basic_block);
235 static hashval_t vrp_hash (const void *);
236 static int vrp_eq (const void *, const void *);
237 static hashval_t avail_expr_hash (const void *);
238 static hashval_t real_avail_expr_hash (const void *);
239 static int avail_expr_eq (const void *, const void *);
240 static void htab_statistics (FILE *, htab_t);
241 static void record_cond (tree, tree);
242 static void record_dominating_conditions (tree);
243 static void record_const_or_copy (tree, tree);
244 static void record_equality (tree, tree);
245 static tree update_rhs_and_lookup_avail_expr (tree, tree, bool);
246 static tree simplify_rhs_and_lookup_avail_expr (struct dom_walk_data *,
247 tree, int);
248 static tree simplify_cond_and_lookup_avail_expr (tree, stmt_ann_t, int);
249 static tree simplify_switch_and_lookup_avail_expr (tree, int);
250 static tree find_equivalent_equality_comparison (tree);
251 static void record_range (tree, basic_block);
252 static bool extract_range_from_cond (tree, tree *, tree *, int *);
253 static void record_equivalences_from_phis (struct dom_walk_data *, basic_block);
254 static void record_equivalences_from_incoming_edge (struct dom_walk_data *,
255 basic_block);
256 static bool eliminate_redundant_computations (struct dom_walk_data *,
257 tree, stmt_ann_t);
258 static void record_equivalences_from_stmt (tree, int, stmt_ann_t);
259 static void thread_across_edge (struct dom_walk_data *, edge);
260 static void dom_opt_finalize_block (struct dom_walk_data *, basic_block);
261 static void dom_opt_initialize_block (struct dom_walk_data *, basic_block);
262 static void cprop_into_phis (struct dom_walk_data *, basic_block);
263 static void remove_local_expressions_from_table (void);
264 static void restore_vars_to_original_value (void);
265 static void restore_currdefs_to_original_value (void);
266 static void register_definitions_for_stmt (tree);
267 static edge single_incoming_edge_ignoring_loop_edges (basic_block);
268 static void restore_nonzero_vars_to_original_value (void);
270 /* Local version of fold that doesn't introduce cruft. */
272 static tree
273 local_fold (tree t)
275 t = fold (t);
277 /* Strip away useless type conversions. Both the NON_LVALUE_EXPR that
278 may have been added by fold, and "useless" type conversions that might
279 now be apparent due to propagation. */
280 STRIP_USELESS_TYPE_CONVERSION (t);
282 return t;
285 /* Jump threading, redundancy elimination and const/copy propagation.
287 This pass may expose new symbols that need to be renamed into SSA. For
288 every new symbol exposed, its corresponding bit will be set in
289 VARS_TO_RENAME. */
291 static void
292 tree_ssa_dominator_optimize (void)
294 struct dom_walk_data walk_data;
295 unsigned int i;
297 memset (&opt_stats, 0, sizeof (opt_stats));
299 for (i = 0; i < num_referenced_vars; i++)
300 var_ann (referenced_var (i))->current_def = NULL;
302 /* Mark loop edges so we avoid threading across loop boundaries.
303 This may result in transforming natural loop into irreducible
304 region. */
305 mark_dfs_back_edges ();
307 /* Create our hash tables. */
308 avail_exprs = htab_create (1024, real_avail_expr_hash, avail_expr_eq, free);
309 vrp_data = htab_create (ceil_log2 (num_ssa_names), vrp_hash, vrp_eq, free);
310 VARRAY_TREE_INIT (avail_exprs_stack, 20, "Available expression stack");
311 VARRAY_TREE_INIT (block_defs_stack, 20, "Block DEFS stack");
312 VARRAY_TREE_INIT (const_and_copies_stack, 20, "Block const_and_copies stack");
313 VARRAY_TREE_INIT (nonzero_vars_stack, 20, "Block nonzero_vars stack");
314 VARRAY_TREE_INIT (vrp_variables_stack, 20, "Block vrp_variables stack");
315 VARRAY_TREE_INIT (stmts_to_rescan, 20, "Statements to rescan");
316 nonzero_vars = BITMAP_XMALLOC ();
317 need_eh_cleanup = BITMAP_XMALLOC ();
319 /* Setup callbacks for the generic dominator tree walker. */
320 walk_data.walk_stmts_backward = false;
321 walk_data.dom_direction = CDI_DOMINATORS;
322 walk_data.initialize_block_local_data = NULL;
323 walk_data.before_dom_children_before_stmts = dom_opt_initialize_block;
324 walk_data.before_dom_children_walk_stmts = optimize_stmt;
325 walk_data.before_dom_children_after_stmts = cprop_into_phis;
326 walk_data.after_dom_children_before_stmts = NULL;
327 walk_data.after_dom_children_walk_stmts = NULL;
328 walk_data.after_dom_children_after_stmts = dom_opt_finalize_block;
329 /* Right now we only attach a dummy COND_EXPR to the global data pointer.
330 When we attach more stuff we'll need to fill this out with a real
331 structure. */
332 walk_data.global_data = NULL;
333 walk_data.block_local_data_size = 0;
335 /* Now initialize the dominator walker. */
336 init_walk_dominator_tree (&walk_data);
338 calculate_dominance_info (CDI_DOMINATORS);
340 /* If we prove certain blocks are unreachable, then we want to
341 repeat the dominator optimization process as PHI nodes may
342 have turned into copies which allows better propagation of
343 values. So we repeat until we do not identify any new unreachable
344 blocks. */
347 /* Optimize the dominator tree. */
348 cfg_altered = false;
350 /* Recursively walk the dominator tree optimizing statements. */
351 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
353 /* If we exposed any new variables, go ahead and put them into
354 SSA form now, before we handle jump threading. This simplifies
355 interactions between rewriting of _DECL nodes into SSA form
356 and rewriting SSA_NAME nodes into SSA form after block
357 duplication and CFG manipulation. */
358 if (bitmap_first_set_bit (vars_to_rename) >= 0)
360 rewrite_into_ssa (false);
361 bitmap_clear (vars_to_rename);
365 block_stmt_iterator bsi;
366 basic_block bb;
367 FOR_EACH_BB (bb)
369 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
371 update_stmt_if_modified (bsi_stmt (bsi));
375 /* Thread jumps, creating duplicate blocks as needed. */
376 cfg_altered = thread_through_all_blocks ();
378 /* Removal of statements may make some EH edges dead. Purge
379 such edges from the CFG as needed. */
380 if (bitmap_first_set_bit (need_eh_cleanup) >= 0)
382 cfg_altered |= tree_purge_all_dead_eh_edges (need_eh_cleanup);
383 bitmap_zero (need_eh_cleanup);
386 free_dominance_info (CDI_DOMINATORS);
387 cfg_altered = cleanup_tree_cfg ();
388 calculate_dominance_info (CDI_DOMINATORS);
390 update_ssa_form_for_registered_defs (0);
392 /* Reinitialize the various tables. */
393 bitmap_clear (nonzero_vars);
394 htab_empty (avail_exprs);
395 htab_empty (vrp_data);
397 for (i = 0; i < num_referenced_vars; i++)
398 var_ann (referenced_var (i))->current_def = NULL;
400 while (cfg_altered);
402 /* Debugging dumps. */
403 if (dump_file && (dump_flags & TDF_STATS))
404 dump_dominator_optimization_stats (dump_file);
406 /* We emptied the hash table earlier, now delete it completely. */
407 htab_delete (avail_exprs);
408 htab_delete (vrp_data);
410 /* It is not necessary to clear CURRDEFS, REDIRECTION_EDGES, VRP_DATA,
411 CONST_AND_COPIES, and NONZERO_VARS as they all get cleared at the bottom
412 of the do-while loop above. */
414 /* And finalize the dominator walker. */
415 fini_walk_dominator_tree (&walk_data);
417 /* Free nonzero_vars. */
418 BITMAP_XFREE (nonzero_vars);
419 BITMAP_XFREE (need_eh_cleanup);
421 /* Finally, remove everything except invariants in SSA_NAME_VALUE.
423 Long term we will be able to let everything in SSA_NAME_VALUE
424 persist. However, for now, we know this is the safe thing to
425 do. */
426 for (i = 0; i < num_ssa_names; i++)
428 tree name = ssa_name (i);
429 tree value;
431 if (!name)
432 continue;
434 value = SSA_NAME_VALUE (name);
435 if (value && !is_gimple_min_invariant (value))
436 SSA_NAME_VALUE (name) = NULL;
443 static bool
444 gate_dominator (void)
446 return flag_tree_dom != 0;
449 struct tree_opt_pass pass_dominator =
451 "dom", /* name */
452 gate_dominator, /* gate */
453 tree_ssa_dominator_optimize, /* execute */
454 NULL, /* sub */
455 NULL, /* next */
456 0, /* static_pass_number */
457 TV_TREE_SSA_DOMINATOR_OPTS, /* tv_id */
458 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
459 0, /* properties_provided */
460 0, /* properties_destroyed */
461 0, /* todo_flags_start */
462 TODO_dump_func | TODO_rename_vars
463 | TODO_verify_ssa, /* todo_flags_finish */
464 0 /* letter */
468 /* We are exiting BB, see if the target block begins with a conditional
469 jump which has a known value when reached via BB. */
471 static void
472 thread_across_edge (struct dom_walk_data *walk_data, edge e)
474 block_stmt_iterator bsi;
475 tree stmt = NULL;
476 tree phi;
478 /* Each PHI creates a temporary equivalence, record them. */
479 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
481 tree src = PHI_ARG_DEF_FROM_EDGE (phi, e);
482 tree dst = PHI_RESULT (phi);
483 record_const_or_copy (dst, src);
484 register_new_def (dst, &block_defs_stack);
487 for (bsi = bsi_start (e->dest); ! bsi_end_p (bsi); bsi_next (&bsi))
489 tree lhs, cached_lhs;
491 stmt = bsi_stmt (bsi);
493 /* Ignore empty statements and labels. */
494 if (IS_EMPTY_STMT (stmt) || TREE_CODE (stmt) == LABEL_EXPR)
495 continue;
497 /* If this is not a MODIFY_EXPR which sets an SSA_NAME to a new
498 value, then stop our search here. Ideally when we stop a
499 search we stop on a COND_EXPR or SWITCH_EXPR. */
500 if (TREE_CODE (stmt) != MODIFY_EXPR
501 || TREE_CODE (TREE_OPERAND (stmt, 0)) != SSA_NAME)
502 break;
504 /* At this point we have a statement which assigns an RHS to an
505 SSA_VAR on the LHS. We want to prove that the RHS is already
506 available and that its value is held in the current definition
507 of the LHS -- meaning that this assignment is a NOP when
508 reached via edge E. */
509 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == SSA_NAME)
510 cached_lhs = TREE_OPERAND (stmt, 1);
511 else
512 cached_lhs = lookup_avail_expr (stmt, false);
514 lhs = TREE_OPERAND (stmt, 0);
516 /* This can happen if we thread around to the start of a loop. */
517 if (lhs == cached_lhs)
518 break;
520 /* If we did not find RHS in the hash table, then try again after
521 temporarily const/copy propagating the operands. */
522 if (!cached_lhs)
524 /* Copy the operands. */
525 stmt_ann_t ann = stmt_ann (stmt);
526 use_optype uses = USE_OPS (ann);
527 vuse_optype vuses = VUSE_OPS (ann);
528 tree *uses_copy = xcalloc (NUM_USES (uses), sizeof (tree));
529 tree *vuses_copy = xcalloc (NUM_VUSES (vuses), sizeof (tree));
530 unsigned int i;
532 /* Make a copy of the uses into USES_COPY, then cprop into
533 the use operands. */
534 for (i = 0; i < NUM_USES (uses); i++)
536 tree tmp = NULL;
538 uses_copy[i] = USE_OP (uses, i);
539 if (TREE_CODE (USE_OP (uses, i)) == SSA_NAME)
540 tmp = SSA_NAME_VALUE (USE_OP (uses, i));
541 if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
542 SET_USE_OP (uses, i, tmp);
545 /* Similarly for virtual uses. */
546 for (i = 0; i < NUM_VUSES (vuses); i++)
548 tree tmp = NULL;
550 vuses_copy[i] = VUSE_OP (vuses, i);
551 if (TREE_CODE (VUSE_OP (vuses, i)) == SSA_NAME)
552 tmp = SSA_NAME_VALUE (VUSE_OP (vuses, i));
553 if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
554 SET_VUSE_OP (vuses, i, tmp);
557 /* Try to lookup the new expression. */
558 cached_lhs = lookup_avail_expr (stmt, false);
560 /* Restore the statement's original uses/defs. */
561 for (i = 0; i < NUM_USES (uses); i++)
562 SET_USE_OP (uses, i, uses_copy[i]);
564 for (i = 0; i < NUM_VUSES (vuses); i++)
565 SET_VUSE_OP (vuses, i, vuses_copy[i]);
567 free (uses_copy);
568 free (vuses_copy);
570 /* If we still did not find the expression in the hash table,
571 then we can not ignore this statement. */
572 if (! cached_lhs)
573 break;
576 /* If the expression in the hash table was not assigned to an
577 SSA_NAME, then we can not ignore this statement. */
578 if (TREE_CODE (cached_lhs) != SSA_NAME)
579 break;
581 /* If we have different underlying variables, then we can not
582 ignore this statement. */
583 if (SSA_NAME_VAR (cached_lhs) != SSA_NAME_VAR (lhs))
584 break;
586 /* If CACHED_LHS does not represent the current value of the undering
587 variable in CACHED_LHS/LHS, then we can not ignore this statement. */
588 if (var_ann (SSA_NAME_VAR (lhs))->current_def != cached_lhs)
589 break;
591 /* If we got here, then we can ignore this statement and continue
592 walking through the statements in the block looking for a threadable
593 COND_EXPR.
595 We want to record an equivalence lhs = cache_lhs so that if
596 the result of this statement is used later we can copy propagate
597 suitably. */
598 record_const_or_copy (lhs, cached_lhs);
599 register_new_def (lhs, &block_defs_stack);
602 /* If we stopped at a COND_EXPR or SWITCH_EXPR, then see if we know which
603 arm will be taken. */
604 if (stmt
605 && (TREE_CODE (stmt) == COND_EXPR
606 || TREE_CODE (stmt) == SWITCH_EXPR))
608 tree cond, cached_lhs;
609 edge e1;
610 edge_iterator ei;
612 /* Do not forward entry edges into the loop. In the case loop
613 has multiple entry edges we may end up in constructing irreducible
614 region.
615 ??? We may consider forwarding the edges in the case all incoming
616 edges forward to the same destination block. */
617 if (!e->flags & EDGE_DFS_BACK)
619 FOR_EACH_EDGE (e1, ei, e->dest->preds)
620 if (e1->flags & EDGE_DFS_BACK)
621 break;
622 if (e1)
623 return;
626 /* Now temporarily cprop the operands and try to find the resulting
627 expression in the hash tables. */
628 if (TREE_CODE (stmt) == COND_EXPR)
629 cond = COND_EXPR_COND (stmt);
630 else
631 cond = SWITCH_COND (stmt);
633 if (COMPARISON_CLASS_P (cond))
635 tree dummy_cond, op0, op1;
636 enum tree_code cond_code;
638 op0 = TREE_OPERAND (cond, 0);
639 op1 = TREE_OPERAND (cond, 1);
640 cond_code = TREE_CODE (cond);
642 /* Get the current value of both operands. */
643 if (TREE_CODE (op0) == SSA_NAME)
645 tree tmp = SSA_NAME_VALUE (op0);
646 if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
647 op0 = tmp;
650 if (TREE_CODE (op1) == SSA_NAME)
652 tree tmp = SSA_NAME_VALUE (op1);
653 if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
654 op1 = tmp;
657 /* Stuff the operator and operands into our dummy conditional
658 expression, creating the dummy conditional if necessary. */
659 dummy_cond = walk_data->global_data;
660 if (! dummy_cond)
662 dummy_cond = build (cond_code, boolean_type_node, op0, op1);
663 dummy_cond = build (COND_EXPR, void_type_node,
664 dummy_cond, NULL, NULL);
665 walk_data->global_data = dummy_cond;
667 else
669 TREE_SET_CODE (TREE_OPERAND (dummy_cond, 0), cond_code);
670 TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 0) = op0;
671 TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 1) = op1;
674 /* If the conditional folds to an invariant, then we are done,
675 otherwise look it up in the hash tables. */
676 cached_lhs = local_fold (COND_EXPR_COND (dummy_cond));
677 if (! is_gimple_min_invariant (cached_lhs))
678 cached_lhs = lookup_avail_expr (dummy_cond, false);
679 if (!cached_lhs || ! is_gimple_min_invariant (cached_lhs))
681 cached_lhs = simplify_cond_and_lookup_avail_expr (dummy_cond,
682 NULL,
683 false);
686 /* We can have conditionals which just test the state of a
687 variable rather than use a relational operator. These are
688 simpler to handle. */
689 else if (TREE_CODE (cond) == SSA_NAME)
691 cached_lhs = cond;
692 cached_lhs = SSA_NAME_VALUE (cached_lhs);
693 if (cached_lhs && ! is_gimple_min_invariant (cached_lhs))
694 cached_lhs = 0;
696 else
697 cached_lhs = lookup_avail_expr (stmt, false);
699 if (cached_lhs)
701 edge taken_edge = find_taken_edge (e->dest, cached_lhs);
702 basic_block dest = (taken_edge ? taken_edge->dest : NULL);
704 if (dest == e->dest)
705 return;
707 /* If we have a known destination for the conditional, then
708 we can perform this optimization, which saves at least one
709 conditional jump each time it applies since we get to
710 bypass the conditional at our original destination. */
711 if (dest)
713 update_bb_profile_for_threading (e->dest, EDGE_FREQUENCY (e),
714 e->count, taken_edge);
715 e->aux = taken_edge;
716 bb_ann (e->dest)->incoming_edge_threaded = true;
723 /* Initialize local stacks for this optimizer and record equivalences
724 upon entry to BB. Equivalences can come from the edge traversed to
725 reach BB or they may come from PHI nodes at the start of BB. */
727 static void
728 dom_opt_initialize_block (struct dom_walk_data *walk_data, basic_block bb)
730 if (dump_file && (dump_flags & TDF_DETAILS))
731 fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
733 /* Push a marker on the stacks of local information so that we know how
734 far to unwind when we finalize this block. */
735 VARRAY_PUSH_TREE (avail_exprs_stack, NULL_TREE);
736 VARRAY_PUSH_TREE (block_defs_stack, NULL_TREE);
737 VARRAY_PUSH_TREE (const_and_copies_stack, NULL_TREE);
738 VARRAY_PUSH_TREE (nonzero_vars_stack, NULL_TREE);
739 VARRAY_PUSH_TREE (vrp_variables_stack, NULL_TREE);
741 record_equivalences_from_incoming_edge (walk_data, bb);
743 /* PHI nodes can create equivalences too. */
744 record_equivalences_from_phis (walk_data, bb);
747 /* Given an expression EXPR (a relational expression or a statement),
748 initialize the hash table element pointed by by ELEMENT. */
750 static void
751 initialize_hash_element (tree expr, tree lhs, struct expr_hash_elt *element)
753 /* Hash table elements may be based on conditional expressions or statements.
755 For the former case, we have no annotation and we want to hash the
756 conditional expression. In the latter case we have an annotation and
757 we want to record the expression the statement evaluates. */
758 if (COMPARISON_CLASS_P (expr) || TREE_CODE (expr) == TRUTH_NOT_EXPR)
760 element->ann = NULL;
761 element->rhs = expr;
763 else if (TREE_CODE (expr) == COND_EXPR)
765 element->ann = stmt_ann (expr);
766 element->rhs = COND_EXPR_COND (expr);
768 else if (TREE_CODE (expr) == SWITCH_EXPR)
770 element->ann = stmt_ann (expr);
771 element->rhs = SWITCH_COND (expr);
773 else if (TREE_CODE (expr) == RETURN_EXPR && TREE_OPERAND (expr, 0))
775 element->ann = stmt_ann (expr);
776 element->rhs = TREE_OPERAND (TREE_OPERAND (expr, 0), 1);
778 else
780 element->ann = stmt_ann (expr);
781 element->rhs = TREE_OPERAND (expr, 1);
784 element->lhs = lhs;
785 element->hash = avail_expr_hash (element);
788 /* Remove all the expressions in LOCALS from TABLE, stopping when there are
789 LIMIT entries left in LOCALs. */
791 static void
792 remove_local_expressions_from_table (void)
794 /* Remove all the expressions made available in this block. */
795 while (VARRAY_ACTIVE_SIZE (avail_exprs_stack) > 0)
797 struct expr_hash_elt element;
798 tree expr = VARRAY_TOP_TREE (avail_exprs_stack);
799 VARRAY_POP (avail_exprs_stack);
801 if (expr == NULL_TREE)
802 break;
804 initialize_hash_element (expr, NULL, &element);
805 htab_remove_elt_with_hash (avail_exprs, &element, element.hash);
809 /* Use the SSA_NAMES in LOCALS to restore TABLE to its original
810 state, stopping when there are LIMIT entries left in LOCALs. */
812 static void
813 restore_nonzero_vars_to_original_value (void)
815 while (VARRAY_ACTIVE_SIZE (nonzero_vars_stack) > 0)
817 tree name = VARRAY_TOP_TREE (nonzero_vars_stack);
818 VARRAY_POP (nonzero_vars_stack);
820 if (name == NULL)
821 break;
823 bitmap_clear_bit (nonzero_vars, SSA_NAME_VERSION (name));
827 /* Use the source/dest pairs in CONST_AND_COPIES_STACK to restore
828 CONST_AND_COPIES to its original state, stopping when we hit a
829 NULL marker. */
831 static void
832 restore_vars_to_original_value (void)
834 while (VARRAY_ACTIVE_SIZE (const_and_copies_stack) > 0)
836 tree prev_value, dest;
838 dest = VARRAY_TOP_TREE (const_and_copies_stack);
839 VARRAY_POP (const_and_copies_stack);
841 if (dest == NULL)
842 break;
844 prev_value = VARRAY_TOP_TREE (const_and_copies_stack);
845 VARRAY_POP (const_and_copies_stack);
847 SSA_NAME_VALUE (dest) = prev_value;
851 /* Similar to restore_vars_to_original_value, except that it restores
852 CURRDEFS to its original value. */
853 static void
854 restore_currdefs_to_original_value (void)
856 /* Restore CURRDEFS to its original state. */
857 while (VARRAY_ACTIVE_SIZE (block_defs_stack) > 0)
859 tree tmp = VARRAY_TOP_TREE (block_defs_stack);
860 tree saved_def, var;
862 VARRAY_POP (block_defs_stack);
864 if (tmp == NULL_TREE)
865 break;
867 /* If we recorded an SSA_NAME, then make the SSA_NAME the current
868 definition of its underlying variable. If we recorded anything
869 else, it must have been an _DECL node and its current reaching
870 definition must have been NULL. */
871 if (TREE_CODE (tmp) == SSA_NAME)
873 saved_def = tmp;
874 var = SSA_NAME_VAR (saved_def);
876 else
878 saved_def = NULL;
879 var = tmp;
882 var_ann (var)->current_def = saved_def;
886 /* We have finished processing the dominator children of BB, perform
887 any finalization actions in preparation for leaving this node in
888 the dominator tree. */
890 static void
891 dom_opt_finalize_block (struct dom_walk_data *walk_data, basic_block bb)
893 tree last;
895 /* If we are at a leaf node in the dominator graph, see if we can thread
896 the edge from BB through its successor.
898 Do this before we remove entries from our equivalence tables. */
899 if (EDGE_COUNT (bb->succs) == 1
900 && (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL) == 0
901 && (get_immediate_dominator (CDI_DOMINATORS, EDGE_SUCC (bb, 0)->dest) != bb
902 || phi_nodes (EDGE_SUCC (bb, 0)->dest)))
905 thread_across_edge (walk_data, EDGE_SUCC (bb, 0));
907 else if ((last = last_stmt (bb))
908 && TREE_CODE (last) == COND_EXPR
909 && (COMPARISON_CLASS_P (COND_EXPR_COND (last))
910 || TREE_CODE (COND_EXPR_COND (last)) == SSA_NAME)
911 && EDGE_COUNT (bb->succs) == 2
912 && (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL) == 0
913 && (EDGE_SUCC (bb, 1)->flags & EDGE_ABNORMAL) == 0)
915 edge true_edge, false_edge;
916 tree cond, inverted = NULL;
917 enum tree_code cond_code;
919 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
921 cond = COND_EXPR_COND (last);
922 cond_code = TREE_CODE (cond);
924 if (TREE_CODE_CLASS (cond_code) == tcc_comparison)
925 inverted = invert_truthvalue (cond);
927 /* If the THEN arm is the end of a dominator tree or has PHI nodes,
928 then try to thread through its edge. */
929 if (get_immediate_dominator (CDI_DOMINATORS, true_edge->dest) != bb
930 || phi_nodes (true_edge->dest))
932 /* Push a marker onto the available expression stack so that we
933 unwind any expressions related to the TRUE arm before processing
934 the false arm below. */
935 VARRAY_PUSH_TREE (avail_exprs_stack, NULL_TREE);
936 VARRAY_PUSH_TREE (block_defs_stack, NULL_TREE);
937 VARRAY_PUSH_TREE (const_and_copies_stack, NULL_TREE);
939 /* Record any equivalences created by following this edge. */
940 if (TREE_CODE_CLASS (cond_code) == tcc_comparison)
942 record_cond (cond, boolean_true_node);
943 record_dominating_conditions (cond);
944 record_cond (inverted, boolean_false_node);
946 else if (cond_code == SSA_NAME)
947 record_const_or_copy (cond, boolean_true_node);
949 /* Now thread the edge. */
950 thread_across_edge (walk_data, true_edge);
952 /* And restore the various tables to their state before
953 we threaded this edge. */
954 remove_local_expressions_from_table ();
955 restore_vars_to_original_value ();
956 restore_currdefs_to_original_value ();
959 /* Similarly for the ELSE arm. */
960 if (get_immediate_dominator (CDI_DOMINATORS, false_edge->dest) != bb
961 || phi_nodes (false_edge->dest))
963 /* Record any equivalences created by following this edge. */
964 if (TREE_CODE_CLASS (cond_code) == tcc_comparison)
966 record_cond (cond, boolean_false_node);
967 record_cond (inverted, boolean_true_node);
968 record_dominating_conditions (inverted);
970 else if (cond_code == SSA_NAME)
971 record_const_or_copy (cond, boolean_false_node);
973 thread_across_edge (walk_data, false_edge);
975 /* No need to remove local expressions from our tables
976 or restore vars to their original value as that will
977 be done immediately below. */
981 remove_local_expressions_from_table ();
982 restore_nonzero_vars_to_original_value ();
983 restore_vars_to_original_value ();
984 restore_currdefs_to_original_value ();
986 /* Remove VRP records associated with this basic block. They are no
987 longer valid.
989 To be efficient, we note which variables have had their values
990 constrained in this block. So walk over each variable in the
991 VRP_VARIABLEs array. */
992 while (VARRAY_ACTIVE_SIZE (vrp_variables_stack) > 0)
994 tree var = VARRAY_TOP_TREE (vrp_variables_stack);
995 struct vrp_hash_elt vrp_hash_elt, *vrp_hash_elt_p;
996 void **slot;
998 /* Each variable has a stack of value range records. We want to
999 invalidate those associated with our basic block. So we walk
1000 the array backwards popping off records associated with our
1001 block. Once we hit a record not associated with our block
1002 we are done. */
1003 varray_type var_vrp_records;
1005 VARRAY_POP (vrp_variables_stack);
1007 if (var == NULL)
1008 break;
1010 vrp_hash_elt.var = var;
1011 vrp_hash_elt.records = NULL;
1013 slot = htab_find_slot (vrp_data, &vrp_hash_elt, NO_INSERT);
1015 vrp_hash_elt_p = (struct vrp_hash_elt *) *slot;
1016 var_vrp_records = vrp_hash_elt_p->records;
1018 while (VARRAY_ACTIVE_SIZE (var_vrp_records) > 0)
1020 struct vrp_element *element
1021 = (struct vrp_element *)VARRAY_TOP_GENERIC_PTR (var_vrp_records);
1023 if (element->bb != bb)
1024 break;
1026 VARRAY_POP (var_vrp_records);
1030 /* If we queued any statements to rescan in this block, then
1031 go ahead and rescan them now. */
1032 while (VARRAY_ACTIVE_SIZE (stmts_to_rescan) > 0)
1034 tree stmt = VARRAY_TOP_TREE (stmts_to_rescan);
1035 basic_block stmt_bb = bb_for_stmt (stmt);
1037 if (stmt_bb != bb)
1038 break;
1040 VARRAY_POP (stmts_to_rescan);
1041 mark_new_vars_to_rename (stmt, vars_to_rename);
1045 /* PHI nodes can create equivalences too.
1047 Ignoring any alternatives which are the same as the result, if
1048 all the alternatives are equal, then the PHI node creates an
1049 equivalence.
1051 Additionally, if all the PHI alternatives are known to have a nonzero
1052 value, then the result of this PHI is known to have a nonzero value,
1053 even if we do not know its exact value. */
1055 static void
1056 record_equivalences_from_phis (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1057 basic_block bb)
1059 tree phi;
1061 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1063 tree lhs = PHI_RESULT (phi);
1064 tree rhs = NULL;
1065 int i;
1067 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1069 tree t = PHI_ARG_DEF (phi, i);
1071 if (TREE_CODE (t) == SSA_NAME || is_gimple_min_invariant (t))
1073 /* Ignore alternatives which are the same as our LHS. */
1074 if (operand_equal_p (lhs, t, 0))
1075 continue;
1077 /* If we have not processed an alternative yet, then set
1078 RHS to this alternative. */
1079 if (rhs == NULL)
1080 rhs = t;
1081 /* If we have processed an alternative (stored in RHS), then
1082 see if it is equal to this one. If it isn't, then stop
1083 the search. */
1084 else if (! operand_equal_p (rhs, t, 0))
1085 break;
1087 else
1088 break;
1091 /* If we had no interesting alternatives, then all the RHS alternatives
1092 must have been the same as LHS. */
1093 if (!rhs)
1094 rhs = lhs;
1096 /* If we managed to iterate through each PHI alternative without
1097 breaking out of the loop, then we have a PHI which may create
1098 a useful equivalence. We do not need to record unwind data for
1099 this, since this is a true assignment and not an equivalence
1100 inferred from a comparison. All uses of this ssa name are dominated
1101 by this assignment, so unwinding just costs time and space. */
1102 if (i == PHI_NUM_ARGS (phi)
1103 && may_propagate_copy (lhs, rhs))
1104 SSA_NAME_VALUE (lhs) = rhs;
1106 /* Now see if we know anything about the nonzero property for the
1107 result of this PHI. */
1108 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1110 if (!PHI_ARG_NONZERO (phi, i))
1111 break;
1114 if (i == PHI_NUM_ARGS (phi))
1115 bitmap_set_bit (nonzero_vars, SSA_NAME_VERSION (PHI_RESULT (phi)));
1117 register_new_def (lhs, &block_defs_stack);
1121 /* Ignoring loop backedges, if BB has precisely one incoming edge then
1122 return that edge. Otherwise return NULL. */
1123 static edge
1124 single_incoming_edge_ignoring_loop_edges (basic_block bb)
1126 edge retval = NULL;
1127 edge e;
1128 edge_iterator ei;
1130 FOR_EACH_EDGE (e, ei, bb->preds)
1132 /* A loop back edge can be identified by the destination of
1133 the edge dominating the source of the edge. */
1134 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
1135 continue;
1137 /* If we have already seen a non-loop edge, then we must have
1138 multiple incoming non-loop edges and thus we return NULL. */
1139 if (retval)
1140 return NULL;
1142 /* This is the first non-loop incoming edge we have found. Record
1143 it. */
1144 retval = e;
1147 return retval;
1150 /* Record any equivalences created by the incoming edge to BB. If BB
1151 has more than one incoming edge, then no equivalence is created. */
1153 static void
1154 record_equivalences_from_incoming_edge (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1155 basic_block bb)
1157 int edge_flags;
1158 basic_block parent;
1159 struct eq_expr_value eq_expr_value;
1160 tree parent_block_last_stmt = NULL;
1162 /* If our parent block ended with a control statment, then we may be
1163 able to record some equivalences based on which outgoing edge from
1164 the parent was followed. */
1165 parent = get_immediate_dominator (CDI_DOMINATORS, bb);
1166 if (parent)
1168 parent_block_last_stmt = last_stmt (parent);
1169 if (parent_block_last_stmt && !is_ctrl_stmt (parent_block_last_stmt))
1170 parent_block_last_stmt = NULL;
1173 eq_expr_value.src = NULL;
1174 eq_expr_value.dst = NULL;
1176 /* If we have a single predecessor (ignoring loop backedges), then extract
1177 EDGE_FLAGS from the single incoming edge. Otherwise just return as
1178 there is nothing to do. */
1179 if (EDGE_COUNT (bb->preds) >= 1
1180 && parent_block_last_stmt)
1182 edge e = single_incoming_edge_ignoring_loop_edges (bb);
1183 if (e && bb_for_stmt (parent_block_last_stmt) == e->src)
1184 edge_flags = e->flags;
1185 else
1186 return;
1188 else
1189 return;
1191 /* If our parent block ended in a COND_EXPR, add any equivalences
1192 created by the COND_EXPR to the hash table and initialize
1193 EQ_EXPR_VALUE appropriately.
1195 EQ_EXPR_VALUE is an assignment expression created when BB's immediate
1196 dominator ends in a COND_EXPR statement whose predicate is of the form
1197 'VAR == VALUE', where VALUE may be another variable or a constant.
1198 This is used to propagate VALUE on the THEN_CLAUSE of that
1199 conditional. This assignment is inserted in CONST_AND_COPIES so that
1200 the copy and constant propagator can find more propagation
1201 opportunities. */
1202 if (TREE_CODE (parent_block_last_stmt) == COND_EXPR
1203 && (edge_flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
1204 eq_expr_value = get_eq_expr_value (parent_block_last_stmt,
1205 (edge_flags & EDGE_TRUE_VALUE) != 0,
1206 bb);
1207 /* Similarly when the parent block ended in a SWITCH_EXPR.
1208 We can only know the value of the switch's condition if the dominator
1209 parent is also the only predecessor of this block. */
1210 else if (EDGE_PRED (bb, 0)->src == parent
1211 && TREE_CODE (parent_block_last_stmt) == SWITCH_EXPR)
1213 tree switch_cond = SWITCH_COND (parent_block_last_stmt);
1215 /* If the switch's condition is an SSA variable, then we may
1216 know its value at each of the case labels. */
1217 if (TREE_CODE (switch_cond) == SSA_NAME)
1219 tree switch_vec = SWITCH_LABELS (parent_block_last_stmt);
1220 size_t i, n = TREE_VEC_LENGTH (switch_vec);
1221 int case_count = 0;
1222 tree match_case = NULL_TREE;
1224 /* Search the case labels for those whose destination is
1225 the current basic block. */
1226 for (i = 0; i < n; ++i)
1228 tree elt = TREE_VEC_ELT (switch_vec, i);
1229 if (label_to_block (CASE_LABEL (elt)) == bb)
1231 if (++case_count > 1 || CASE_HIGH (elt))
1232 break;
1233 match_case = elt;
1237 /* If we encountered precisely one CASE_LABEL_EXPR and it
1238 was not the default case, or a case range, then we know
1239 the exact value of SWITCH_COND which caused us to get to
1240 this block. Record that equivalence in EQ_EXPR_VALUE. */
1241 if (case_count == 1
1242 && match_case
1243 && CASE_LOW (match_case)
1244 && !CASE_HIGH (match_case))
1246 eq_expr_value.dst = switch_cond;
1247 eq_expr_value.src = fold_convert (TREE_TYPE (switch_cond),
1248 CASE_LOW (match_case));
1253 /* If EQ_EXPR_VALUE (VAR == VALUE) is given, register the VALUE as a
1254 new value for VAR, so that occurrences of VAR can be replaced with
1255 VALUE while re-writing the THEN arm of a COND_EXPR. */
1256 if (eq_expr_value.src && eq_expr_value.dst)
1257 record_equality (eq_expr_value.dst, eq_expr_value.src);
1260 /* Dump SSA statistics on FILE. */
1262 void
1263 dump_dominator_optimization_stats (FILE *file)
1265 long n_exprs;
1267 fprintf (file, "Total number of statements: %6ld\n\n",
1268 opt_stats.num_stmts);
1269 fprintf (file, "Exprs considered for dominator optimizations: %6ld\n",
1270 opt_stats.num_exprs_considered);
1272 n_exprs = opt_stats.num_exprs_considered;
1273 if (n_exprs == 0)
1274 n_exprs = 1;
1276 fprintf (file, " Redundant expressions eliminated: %6ld (%.0f%%)\n",
1277 opt_stats.num_re, PERCENT (opt_stats.num_re,
1278 n_exprs));
1280 fprintf (file, "\nHash table statistics:\n");
1282 fprintf (file, " avail_exprs: ");
1283 htab_statistics (file, avail_exprs);
1287 /* Dump SSA statistics on stderr. */
1289 void
1290 debug_dominator_optimization_stats (void)
1292 dump_dominator_optimization_stats (stderr);
1296 /* Dump statistics for the hash table HTAB. */
1298 static void
1299 htab_statistics (FILE *file, htab_t htab)
1301 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
1302 (long) htab_size (htab),
1303 (long) htab_elements (htab),
1304 htab_collisions (htab));
1307 /* Record the fact that VAR has a nonzero value, though we may not know
1308 its exact value. Note that if VAR is already known to have a nonzero
1309 value, then we do nothing. */
1311 static void
1312 record_var_is_nonzero (tree var)
1314 int indx = SSA_NAME_VERSION (var);
1316 if (bitmap_bit_p (nonzero_vars, indx))
1317 return;
1319 /* Mark it in the global table. */
1320 bitmap_set_bit (nonzero_vars, indx);
1322 /* Record this SSA_NAME so that we can reset the global table
1323 when we leave this block. */
1324 VARRAY_PUSH_TREE (nonzero_vars_stack, var);
1327 /* Enter a statement into the true/false expression hash table indicating
1328 that the condition COND has the value VALUE. */
1330 static void
1331 record_cond (tree cond, tree value)
1333 struct expr_hash_elt *element = xmalloc (sizeof (struct expr_hash_elt));
1334 void **slot;
1336 initialize_hash_element (cond, value, element);
1338 slot = htab_find_slot_with_hash (avail_exprs, (void *)element,
1339 element->hash, true);
1340 if (*slot == NULL)
1342 *slot = (void *) element;
1343 VARRAY_PUSH_TREE (avail_exprs_stack, cond);
1345 else
1346 free (element);
1349 /* COND is a condition which is known to be true. Record variants of
1350 COND which must also be true.
1352 For example, if a < b is true, then a <= b must also be true. */
1354 static void
1355 record_dominating_conditions (tree cond)
1357 switch (TREE_CODE (cond))
1359 case LT_EXPR:
1360 record_cond (build2 (LE_EXPR, boolean_type_node,
1361 TREE_OPERAND (cond, 0),
1362 TREE_OPERAND (cond, 1)),
1363 boolean_true_node);
1364 record_cond (build2 (ORDERED_EXPR, boolean_type_node,
1365 TREE_OPERAND (cond, 0),
1366 TREE_OPERAND (cond, 1)),
1367 boolean_true_node);
1368 record_cond (build2 (NE_EXPR, boolean_type_node,
1369 TREE_OPERAND (cond, 0),
1370 TREE_OPERAND (cond, 1)),
1371 boolean_true_node);
1372 record_cond (build2 (LTGT_EXPR, boolean_type_node,
1373 TREE_OPERAND (cond, 0),
1374 TREE_OPERAND (cond, 1)),
1375 boolean_true_node);
1376 break;
1378 case GT_EXPR:
1379 record_cond (build2 (GE_EXPR, boolean_type_node,
1380 TREE_OPERAND (cond, 0),
1381 TREE_OPERAND (cond, 1)),
1382 boolean_true_node);
1383 record_cond (build2 (ORDERED_EXPR, boolean_type_node,
1384 TREE_OPERAND (cond, 0),
1385 TREE_OPERAND (cond, 1)),
1386 boolean_true_node);
1387 record_cond (build2 (NE_EXPR, boolean_type_node,
1388 TREE_OPERAND (cond, 0),
1389 TREE_OPERAND (cond, 1)),
1390 boolean_true_node);
1391 record_cond (build2 (LTGT_EXPR, boolean_type_node,
1392 TREE_OPERAND (cond, 0),
1393 TREE_OPERAND (cond, 1)),
1394 boolean_true_node);
1395 break;
1397 case GE_EXPR:
1398 case LE_EXPR:
1399 record_cond (build2 (ORDERED_EXPR, boolean_type_node,
1400 TREE_OPERAND (cond, 0),
1401 TREE_OPERAND (cond, 1)),
1402 boolean_true_node);
1403 break;
1405 case EQ_EXPR:
1406 record_cond (build2 (ORDERED_EXPR, boolean_type_node,
1407 TREE_OPERAND (cond, 0),
1408 TREE_OPERAND (cond, 1)),
1409 boolean_true_node);
1410 record_cond (build2 (LE_EXPR, boolean_type_node,
1411 TREE_OPERAND (cond, 0),
1412 TREE_OPERAND (cond, 1)),
1413 boolean_true_node);
1414 record_cond (build2 (GE_EXPR, boolean_type_node,
1415 TREE_OPERAND (cond, 0),
1416 TREE_OPERAND (cond, 1)),
1417 boolean_true_node);
1418 break;
1420 case UNORDERED_EXPR:
1421 record_cond (build2 (NE_EXPR, boolean_type_node,
1422 TREE_OPERAND (cond, 0),
1423 TREE_OPERAND (cond, 1)),
1424 boolean_true_node);
1425 record_cond (build2 (UNLE_EXPR, boolean_type_node,
1426 TREE_OPERAND (cond, 0),
1427 TREE_OPERAND (cond, 1)),
1428 boolean_true_node);
1429 record_cond (build2 (UNGE_EXPR, boolean_type_node,
1430 TREE_OPERAND (cond, 0),
1431 TREE_OPERAND (cond, 1)),
1432 boolean_true_node);
1433 record_cond (build2 (UNEQ_EXPR, boolean_type_node,
1434 TREE_OPERAND (cond, 0),
1435 TREE_OPERAND (cond, 1)),
1436 boolean_true_node);
1437 record_cond (build2 (UNLT_EXPR, boolean_type_node,
1438 TREE_OPERAND (cond, 0),
1439 TREE_OPERAND (cond, 1)),
1440 boolean_true_node);
1441 record_cond (build2 (UNGT_EXPR, boolean_type_node,
1442 TREE_OPERAND (cond, 0),
1443 TREE_OPERAND (cond, 1)),
1444 boolean_true_node);
1445 break;
1447 case UNLT_EXPR:
1448 record_cond (build2 (UNLE_EXPR, boolean_type_node,
1449 TREE_OPERAND (cond, 0),
1450 TREE_OPERAND (cond, 1)),
1451 boolean_true_node);
1452 record_cond (build2 (NE_EXPR, boolean_type_node,
1453 TREE_OPERAND (cond, 0),
1454 TREE_OPERAND (cond, 1)),
1455 boolean_true_node);
1456 break;
1458 case UNGT_EXPR:
1459 record_cond (build2 (UNGE_EXPR, boolean_type_node,
1460 TREE_OPERAND (cond, 0),
1461 TREE_OPERAND (cond, 1)),
1462 boolean_true_node);
1463 record_cond (build2 (NE_EXPR, boolean_type_node,
1464 TREE_OPERAND (cond, 0),
1465 TREE_OPERAND (cond, 1)),
1466 boolean_true_node);
1467 break;
1469 case UNEQ_EXPR:
1470 record_cond (build2 (UNLE_EXPR, boolean_type_node,
1471 TREE_OPERAND (cond, 0),
1472 TREE_OPERAND (cond, 1)),
1473 boolean_true_node);
1474 record_cond (build2 (UNGE_EXPR, boolean_type_node,
1475 TREE_OPERAND (cond, 0),
1476 TREE_OPERAND (cond, 1)),
1477 boolean_true_node);
1478 break;
1480 case LTGT_EXPR:
1481 record_cond (build2 (NE_EXPR, boolean_type_node,
1482 TREE_OPERAND (cond, 0),
1483 TREE_OPERAND (cond, 1)),
1484 boolean_true_node);
1485 record_cond (build2 (ORDERED_EXPR, boolean_type_node,
1486 TREE_OPERAND (cond, 0),
1487 TREE_OPERAND (cond, 1)),
1488 boolean_true_node);
1490 default:
1491 break;
1495 /* A helper function for record_const_or_copy and record_equality.
1496 Do the work of recording the value and undo info. */
1498 static void
1499 record_const_or_copy_1 (tree x, tree y, tree prev_x)
1501 SSA_NAME_VALUE (x) = y;
1503 VARRAY_PUSH_TREE (const_and_copies_stack, prev_x);
1504 VARRAY_PUSH_TREE (const_and_copies_stack, x);
1507 /* Record that X is equal to Y in const_and_copies. Record undo
1508 information in the block-local varray. */
1510 static void
1511 record_const_or_copy (tree x, tree y)
1513 tree prev_x = SSA_NAME_VALUE (x);
1515 if (TREE_CODE (y) == SSA_NAME)
1517 tree tmp = SSA_NAME_VALUE (y);
1518 if (tmp)
1519 y = tmp;
1522 record_const_or_copy_1 (x, y, prev_x);
1525 /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1526 This constrains the cases in which we may treat this as assignment. */
1528 static void
1529 record_equality (tree x, tree y)
1531 tree prev_x = NULL, prev_y = NULL;
1533 if (TREE_CODE (x) == SSA_NAME)
1534 prev_x = SSA_NAME_VALUE (x);
1535 if (TREE_CODE (y) == SSA_NAME)
1536 prev_y = SSA_NAME_VALUE (y);
1538 /* If one of the previous values is invariant, then use that.
1539 Otherwise it doesn't matter which value we choose, just so
1540 long as we canonicalize on one value. */
1541 if (TREE_INVARIANT (y))
1543 else if (TREE_INVARIANT (x))
1544 prev_x = x, x = y, y = prev_x, prev_x = prev_y;
1545 else if (prev_x && TREE_INVARIANT (prev_x))
1546 x = y, y = prev_x, prev_x = prev_y;
1547 else if (prev_y && TREE_CODE (prev_y) != VALUE_HANDLE)
1548 y = prev_y;
1550 /* After the swapping, we must have one SSA_NAME. */
1551 if (TREE_CODE (x) != SSA_NAME)
1552 return;
1554 /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1555 variable compared against zero. If we're honoring signed zeros,
1556 then we cannot record this value unless we know that the value is
1557 nonzero. */
1558 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (x)))
1559 && (TREE_CODE (y) != REAL_CST
1560 || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (y))))
1561 return;
1563 record_const_or_copy_1 (x, y, prev_x);
1566 /* STMT is a MODIFY_EXPR for which we were unable to find RHS in the
1567 hash tables. Try to simplify the RHS using whatever equivalences
1568 we may have recorded.
1570 If we are able to simplify the RHS, then lookup the simplified form in
1571 the hash table and return the result. Otherwise return NULL. */
1573 static tree
1574 simplify_rhs_and_lookup_avail_expr (struct dom_walk_data *walk_data,
1575 tree stmt, int insert)
1577 tree rhs = TREE_OPERAND (stmt, 1);
1578 enum tree_code rhs_code = TREE_CODE (rhs);
1579 tree result = NULL;
1581 /* If we have lhs = ~x, look and see if we earlier had x = ~y.
1582 In which case we can change this statement to be lhs = y.
1583 Which can then be copy propagated.
1585 Similarly for negation. */
1586 if ((rhs_code == BIT_NOT_EXPR || rhs_code == NEGATE_EXPR)
1587 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
1589 /* Get the definition statement for our RHS. */
1590 tree rhs_def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs, 0));
1592 /* See if the RHS_DEF_STMT has the same form as our statement. */
1593 if (TREE_CODE (rhs_def_stmt) == MODIFY_EXPR
1594 && TREE_CODE (TREE_OPERAND (rhs_def_stmt, 1)) == rhs_code)
1596 tree rhs_def_operand;
1598 rhs_def_operand = TREE_OPERAND (TREE_OPERAND (rhs_def_stmt, 1), 0);
1600 /* Verify that RHS_DEF_OPERAND is a suitable SSA variable. */
1601 if (TREE_CODE (rhs_def_operand) == SSA_NAME
1602 && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs_def_operand))
1603 result = update_rhs_and_lookup_avail_expr (stmt,
1604 rhs_def_operand,
1605 insert);
1609 /* If we have z = (x OP C1), see if we earlier had x = y OP C2.
1610 If OP is associative, create and fold (y OP C2) OP C1 which
1611 should result in (y OP C3), use that as the RHS for the
1612 assignment. Add minus to this, as we handle it specially below. */
1613 if ((associative_tree_code (rhs_code) || rhs_code == MINUS_EXPR)
1614 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME
1615 && is_gimple_min_invariant (TREE_OPERAND (rhs, 1)))
1617 tree rhs_def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs, 0));
1619 /* See if the RHS_DEF_STMT has the same form as our statement. */
1620 if (TREE_CODE (rhs_def_stmt) == MODIFY_EXPR)
1622 tree rhs_def_rhs = TREE_OPERAND (rhs_def_stmt, 1);
1623 enum tree_code rhs_def_code = TREE_CODE (rhs_def_rhs);
1625 if (rhs_code == rhs_def_code
1626 || (rhs_code == PLUS_EXPR && rhs_def_code == MINUS_EXPR)
1627 || (rhs_code == MINUS_EXPR && rhs_def_code == PLUS_EXPR))
1629 tree def_stmt_op0 = TREE_OPERAND (rhs_def_rhs, 0);
1630 tree def_stmt_op1 = TREE_OPERAND (rhs_def_rhs, 1);
1632 if (TREE_CODE (def_stmt_op0) == SSA_NAME
1633 && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def_stmt_op0)
1634 && is_gimple_min_invariant (def_stmt_op1))
1636 tree outer_const = TREE_OPERAND (rhs, 1);
1637 tree type = TREE_TYPE (TREE_OPERAND (stmt, 0));
1638 tree t;
1640 /* If we care about correct floating point results, then
1641 don't fold x + c1 - c2. Note that we need to take both
1642 the codes and the signs to figure this out. */
1643 if (FLOAT_TYPE_P (type)
1644 && !flag_unsafe_math_optimizations
1645 && (rhs_def_code == PLUS_EXPR
1646 || rhs_def_code == MINUS_EXPR))
1648 bool neg = false;
1650 neg ^= (rhs_code == MINUS_EXPR);
1651 neg ^= (rhs_def_code == MINUS_EXPR);
1652 neg ^= real_isneg (TREE_REAL_CST_PTR (outer_const));
1653 neg ^= real_isneg (TREE_REAL_CST_PTR (def_stmt_op1));
1655 if (neg)
1656 goto dont_fold_assoc;
1659 /* Ho hum. So fold will only operate on the outermost
1660 thingy that we give it, so we have to build the new
1661 expression in two pieces. This requires that we handle
1662 combinations of plus and minus. */
1663 if (rhs_def_code != rhs_code)
1665 if (rhs_def_code == MINUS_EXPR)
1666 t = build (MINUS_EXPR, type, outer_const, def_stmt_op1);
1667 else
1668 t = build (MINUS_EXPR, type, def_stmt_op1, outer_const);
1669 rhs_code = PLUS_EXPR;
1671 else if (rhs_def_code == MINUS_EXPR)
1672 t = build (PLUS_EXPR, type, def_stmt_op1, outer_const);
1673 else
1674 t = build (rhs_def_code, type, def_stmt_op1, outer_const);
1675 t = local_fold (t);
1676 t = build (rhs_code, type, def_stmt_op0, t);
1677 t = local_fold (t);
1679 /* If the result is a suitable looking gimple expression,
1680 then use it instead of the original for STMT. */
1681 if (TREE_CODE (t) == SSA_NAME
1682 || (UNARY_CLASS_P (t)
1683 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME)
1684 || ((BINARY_CLASS_P (t) || COMPARISON_CLASS_P (t))
1685 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
1686 && is_gimple_val (TREE_OPERAND (t, 1))))
1687 result = update_rhs_and_lookup_avail_expr (stmt, t, insert);
1691 dont_fold_assoc:;
1694 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
1695 and BIT_AND_EXPR respectively if the first operand is greater
1696 than zero and the second operand is an exact power of two. */
1697 if ((rhs_code == TRUNC_DIV_EXPR || rhs_code == TRUNC_MOD_EXPR)
1698 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0)))
1699 && integer_pow2p (TREE_OPERAND (rhs, 1)))
1701 tree val;
1702 tree op = TREE_OPERAND (rhs, 0);
1704 if (TYPE_UNSIGNED (TREE_TYPE (op)))
1706 val = integer_one_node;
1708 else
1710 tree dummy_cond = walk_data->global_data;
1712 if (! dummy_cond)
1714 dummy_cond = build (GT_EXPR, boolean_type_node,
1715 op, integer_zero_node);
1716 dummy_cond = build (COND_EXPR, void_type_node,
1717 dummy_cond, NULL, NULL);
1718 walk_data->global_data = dummy_cond;
1720 else
1722 TREE_SET_CODE (TREE_OPERAND (dummy_cond, 0), GT_EXPR);
1723 TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 0) = op;
1724 TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 1)
1725 = integer_zero_node;
1727 val = simplify_cond_and_lookup_avail_expr (dummy_cond, NULL, false);
1730 if (val && integer_onep (val))
1732 tree t;
1733 tree op0 = TREE_OPERAND (rhs, 0);
1734 tree op1 = TREE_OPERAND (rhs, 1);
1736 if (rhs_code == TRUNC_DIV_EXPR)
1737 t = build (RSHIFT_EXPR, TREE_TYPE (op0), op0,
1738 build_int_cst (NULL_TREE, tree_log2 (op1)));
1739 else
1740 t = build (BIT_AND_EXPR, TREE_TYPE (op0), op0,
1741 local_fold (build (MINUS_EXPR, TREE_TYPE (op1),
1742 op1, integer_one_node)));
1744 result = update_rhs_and_lookup_avail_expr (stmt, t, insert);
1748 /* Transform ABS (X) into X or -X as appropriate. */
1749 if (rhs_code == ABS_EXPR
1750 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0))))
1752 tree val;
1753 tree op = TREE_OPERAND (rhs, 0);
1754 tree type = TREE_TYPE (op);
1756 if (TYPE_UNSIGNED (type))
1758 val = integer_zero_node;
1760 else
1762 tree dummy_cond = walk_data->global_data;
1764 if (! dummy_cond)
1766 dummy_cond = build (LE_EXPR, boolean_type_node,
1767 op, integer_zero_node);
1768 dummy_cond = build (COND_EXPR, void_type_node,
1769 dummy_cond, NULL, NULL);
1770 walk_data->global_data = dummy_cond;
1772 else
1774 TREE_SET_CODE (TREE_OPERAND (dummy_cond, 0), LE_EXPR);
1775 TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 0) = op;
1776 TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 1)
1777 = build_int_cst (type, 0);
1779 val = simplify_cond_and_lookup_avail_expr (dummy_cond, NULL, false);
1781 if (!val)
1783 TREE_SET_CODE (TREE_OPERAND (dummy_cond, 0), GE_EXPR);
1784 TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 0) = op;
1785 TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 1)
1786 = build_int_cst (type, 0);
1788 val = simplify_cond_and_lookup_avail_expr (dummy_cond,
1789 NULL, false);
1791 if (val)
1793 if (integer_zerop (val))
1794 val = integer_one_node;
1795 else if (integer_onep (val))
1796 val = integer_zero_node;
1801 if (val
1802 && (integer_onep (val) || integer_zerop (val)))
1804 tree t;
1806 if (integer_onep (val))
1807 t = build1 (NEGATE_EXPR, TREE_TYPE (op), op);
1808 else
1809 t = op;
1811 result = update_rhs_and_lookup_avail_expr (stmt, t, insert);
1815 /* Optimize *"foo" into 'f'. This is done here rather than
1816 in fold to avoid problems with stuff like &*"foo". */
1817 if (TREE_CODE (rhs) == INDIRECT_REF || TREE_CODE (rhs) == ARRAY_REF)
1819 tree t = fold_read_from_constant_string (rhs);
1821 if (t)
1822 result = update_rhs_and_lookup_avail_expr (stmt, t, insert);
1825 return result;
1828 /* COND is a condition of the form:
1830 x == const or x != const
1832 Look back to x's defining statement and see if x is defined as
1834 x = (type) y;
1836 If const is unchanged if we convert it to type, then we can build
1837 the equivalent expression:
1840 y == const or y != const
1842 Which may allow further optimizations.
1844 Return the equivalent comparison or NULL if no such equivalent comparison
1845 was found. */
1847 static tree
1848 find_equivalent_equality_comparison (tree cond)
1850 tree op0 = TREE_OPERAND (cond, 0);
1851 tree op1 = TREE_OPERAND (cond, 1);
1852 tree def_stmt = SSA_NAME_DEF_STMT (op0);
1854 /* OP0 might have been a parameter, so first make sure it
1855 was defined by a MODIFY_EXPR. */
1856 if (def_stmt && TREE_CODE (def_stmt) == MODIFY_EXPR)
1858 tree def_rhs = TREE_OPERAND (def_stmt, 1);
1860 /* Now make sure the RHS of the MODIFY_EXPR is a typecast. */
1861 if ((TREE_CODE (def_rhs) == NOP_EXPR
1862 || TREE_CODE (def_rhs) == CONVERT_EXPR)
1863 && TREE_CODE (TREE_OPERAND (def_rhs, 0)) == SSA_NAME)
1865 tree def_rhs_inner = TREE_OPERAND (def_rhs, 0);
1866 tree def_rhs_inner_type = TREE_TYPE (def_rhs_inner);
1867 tree new;
1869 if (TYPE_PRECISION (def_rhs_inner_type)
1870 > TYPE_PRECISION (TREE_TYPE (def_rhs)))
1871 return NULL;
1873 /* What we want to prove is that if we convert OP1 to
1874 the type of the object inside the NOP_EXPR that the
1875 result is still equivalent to SRC.
1877 If that is true, the build and return new equivalent
1878 condition which uses the source of the typecast and the
1879 new constant (which has only changed its type). */
1880 new = build1 (TREE_CODE (def_rhs), def_rhs_inner_type, op1);
1881 new = local_fold (new);
1882 if (is_gimple_val (new) && tree_int_cst_equal (new, op1))
1883 return build (TREE_CODE (cond), TREE_TYPE (cond),
1884 def_rhs_inner, new);
1887 return NULL;
1890 /* STMT is a COND_EXPR for which we could not trivially determine its
1891 result. This routine attempts to find equivalent forms of the
1892 condition which we may be able to optimize better. It also
1893 uses simple value range propagation to optimize conditionals. */
1895 static tree
1896 simplify_cond_and_lookup_avail_expr (tree stmt,
1897 stmt_ann_t ann,
1898 int insert)
1900 tree cond = COND_EXPR_COND (stmt);
1902 if (COMPARISON_CLASS_P (cond))
1904 tree op0 = TREE_OPERAND (cond, 0);
1905 tree op1 = TREE_OPERAND (cond, 1);
1907 if (TREE_CODE (op0) == SSA_NAME && is_gimple_min_invariant (op1))
1909 int limit;
1910 tree low, high, cond_low, cond_high;
1911 int lowequal, highequal, swapped, no_overlap, subset, cond_inverted;
1912 varray_type vrp_records;
1913 struct vrp_element *element;
1914 struct vrp_hash_elt vrp_hash_elt, *vrp_hash_elt_p;
1915 void **slot;
1917 /* First see if we have test of an SSA_NAME against a constant
1918 where the SSA_NAME is defined by an earlier typecast which
1919 is irrelevant when performing tests against the given
1920 constant. */
1921 if (TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
1923 tree new_cond = find_equivalent_equality_comparison (cond);
1925 if (new_cond)
1927 /* Update the statement to use the new equivalent
1928 condition. */
1929 COND_EXPR_COND (stmt) = new_cond;
1931 /* If this is not a real stmt, ann will be NULL and we
1932 avoid processing the operands. */
1933 if (ann)
1934 mark_stmt_modified (stmt);
1936 /* Lookup the condition and return its known value if it
1937 exists. */
1938 new_cond = lookup_avail_expr (stmt, insert);
1939 if (new_cond)
1940 return new_cond;
1942 /* The operands have changed, so update op0 and op1. */
1943 op0 = TREE_OPERAND (cond, 0);
1944 op1 = TREE_OPERAND (cond, 1);
1948 /* Consult the value range records for this variable (if they exist)
1949 to see if we can eliminate or simplify this conditional.
1951 Note two tests are necessary to determine no records exist.
1952 First we have to see if the virtual array exists, if it
1953 exists, then we have to check its active size.
1955 Also note the vast majority of conditionals are not testing
1956 a variable which has had its range constrained by an earlier
1957 conditional. So this filter avoids a lot of unnecessary work. */
1958 vrp_hash_elt.var = op0;
1959 vrp_hash_elt.records = NULL;
1960 slot = htab_find_slot (vrp_data, &vrp_hash_elt, NO_INSERT);
1961 if (slot == NULL)
1962 return NULL;
1964 vrp_hash_elt_p = (struct vrp_hash_elt *) *slot;
1965 vrp_records = vrp_hash_elt_p->records;
1966 if (vrp_records == NULL)
1967 return NULL;
1969 limit = VARRAY_ACTIVE_SIZE (vrp_records);
1971 /* If we have no value range records for this variable, or we are
1972 unable to extract a range for this condition, then there is
1973 nothing to do. */
1974 if (limit == 0
1975 || ! extract_range_from_cond (cond, &cond_high,
1976 &cond_low, &cond_inverted))
1977 return NULL;
1979 /* We really want to avoid unnecessary computations of range
1980 info. So all ranges are computed lazily; this avoids a
1981 lot of unnecessary work. i.e., we record the conditional,
1982 but do not process how it constrains the variable's
1983 potential values until we know that processing the condition
1984 could be helpful.
1986 However, we do not want to have to walk a potentially long
1987 list of ranges, nor do we want to compute a variable's
1988 range more than once for a given path.
1990 Luckily, each time we encounter a conditional that can not
1991 be otherwise optimized we will end up here and we will
1992 compute the necessary range information for the variable
1993 used in this condition.
1995 Thus you can conclude that there will never be more than one
1996 conditional associated with a variable which has not been
1997 processed. So we never need to merge more than one new
1998 conditional into the current range.
2000 These properties also help us avoid unnecessary work. */
2001 element
2002 = (struct vrp_element *)VARRAY_GENERIC_PTR (vrp_records, limit - 1);
2004 if (element->high && element->low)
2006 /* The last element has been processed, so there is no range
2007 merging to do, we can simply use the high/low values
2008 recorded in the last element. */
2009 low = element->low;
2010 high = element->high;
2012 else
2014 tree tmp_high, tmp_low;
2015 int dummy;
2017 /* The last element has not been processed. Process it now. */
2018 extract_range_from_cond (element->cond, &tmp_high,
2019 &tmp_low, &dummy);
2021 /* If this is the only element, then no merging is necessary,
2022 the high/low values from extract_range_from_cond are all
2023 we need. */
2024 if (limit == 1)
2026 low = tmp_low;
2027 high = tmp_high;
2029 else
2031 /* Get the high/low value from the previous element. */
2032 struct vrp_element *prev
2033 = (struct vrp_element *)VARRAY_GENERIC_PTR (vrp_records,
2034 limit - 2);
2035 low = prev->low;
2036 high = prev->high;
2038 /* Merge in this element's range with the range from the
2039 previous element.
2041 The low value for the merged range is the maximum of
2042 the previous low value and the low value of this record.
2044 Similarly the high value for the merged range is the
2045 minimum of the previous high value and the high value of
2046 this record. */
2047 low = (tree_int_cst_compare (low, tmp_low) == 1
2048 ? low : tmp_low);
2049 high = (tree_int_cst_compare (high, tmp_high) == -1
2050 ? high : tmp_high);
2053 /* And record the computed range. */
2054 element->low = low;
2055 element->high = high;
2059 /* After we have constrained this variable's potential values,
2060 we try to determine the result of the given conditional.
2062 To simplify later tests, first determine if the current
2063 low value is the same low value as the conditional.
2064 Similarly for the current high value and the high value
2065 for the conditional. */
2066 lowequal = tree_int_cst_equal (low, cond_low);
2067 highequal = tree_int_cst_equal (high, cond_high);
2069 if (lowequal && highequal)
2070 return (cond_inverted ? boolean_false_node : boolean_true_node);
2072 /* To simplify the overlap/subset tests below we may want
2073 to swap the two ranges so that the larger of the two
2074 ranges occurs "first". */
2075 swapped = 0;
2076 if (tree_int_cst_compare (low, cond_low) == 1
2077 || (lowequal
2078 && tree_int_cst_compare (cond_high, high) == 1))
2080 tree temp;
2082 swapped = 1;
2083 temp = low;
2084 low = cond_low;
2085 cond_low = temp;
2086 temp = high;
2087 high = cond_high;
2088 cond_high = temp;
2091 /* Now determine if there is no overlap in the ranges
2092 or if the second range is a subset of the first range. */
2093 no_overlap = tree_int_cst_lt (high, cond_low);
2094 subset = tree_int_cst_compare (cond_high, high) != 1;
2096 /* If there was no overlap in the ranges, then this conditional
2097 always has a false value (unless we had to invert this
2098 conditional, in which case it always has a true value). */
2099 if (no_overlap)
2100 return (cond_inverted ? boolean_true_node : boolean_false_node);
2102 /* If the current range is a subset of the condition's range,
2103 then this conditional always has a true value (unless we
2104 had to invert this conditional, in which case it always
2105 has a true value). */
2106 if (subset && swapped)
2107 return (cond_inverted ? boolean_false_node : boolean_true_node);
2109 /* We were unable to determine the result of the conditional.
2110 However, we may be able to simplify the conditional. First
2111 merge the ranges in the same manner as range merging above. */
2112 low = tree_int_cst_compare (low, cond_low) == 1 ? low : cond_low;
2113 high = tree_int_cst_compare (high, cond_high) == -1 ? high : cond_high;
2115 /* If the range has converged to a single point, then turn this
2116 into an equality comparison. */
2117 if (TREE_CODE (cond) != EQ_EXPR
2118 && TREE_CODE (cond) != NE_EXPR
2119 && tree_int_cst_equal (low, high))
2121 TREE_SET_CODE (cond, EQ_EXPR);
2122 TREE_OPERAND (cond, 1) = high;
2126 return 0;
2129 /* STMT is a SWITCH_EXPR for which we could not trivially determine its
2130 result. This routine attempts to find equivalent forms of the
2131 condition which we may be able to optimize better. */
2133 static tree
2134 simplify_switch_and_lookup_avail_expr (tree stmt, int insert)
2136 tree cond = SWITCH_COND (stmt);
2137 tree def, to, ti;
2139 /* The optimization that we really care about is removing unnecessary
2140 casts. That will let us do much better in propagating the inferred
2141 constant at the switch target. */
2142 if (TREE_CODE (cond) == SSA_NAME)
2144 def = SSA_NAME_DEF_STMT (cond);
2145 if (TREE_CODE (def) == MODIFY_EXPR)
2147 def = TREE_OPERAND (def, 1);
2148 if (TREE_CODE (def) == NOP_EXPR)
2150 int need_precision;
2151 bool fail;
2153 def = TREE_OPERAND (def, 0);
2155 #ifdef ENABLE_CHECKING
2156 /* ??? Why was Jeff testing this? We are gimple... */
2157 gcc_assert (is_gimple_val (def));
2158 #endif
2160 to = TREE_TYPE (cond);
2161 ti = TREE_TYPE (def);
2163 /* If we have an extension that preserves value, then we
2164 can copy the source value into the switch. */
2166 need_precision = TYPE_PRECISION (ti);
2167 fail = false;
2168 if (TYPE_UNSIGNED (to) && !TYPE_UNSIGNED (ti))
2169 fail = true;
2170 else if (!TYPE_UNSIGNED (to) && TYPE_UNSIGNED (ti))
2171 need_precision += 1;
2172 if (TYPE_PRECISION (to) < need_precision)
2173 fail = true;
2175 if (!fail)
2177 SWITCH_COND (stmt) = def;
2178 mark_stmt_modified (stmt);
2180 return lookup_avail_expr (stmt, insert);
2186 return 0;
2190 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
2191 known value for that SSA_NAME (or NULL if no value is known).
2193 NONZERO_VARS is the set SSA_NAMES known to have a nonzero value,
2194 even if we don't know their precise value.
2196 Propagate values from CONST_AND_COPIES and NONZERO_VARS into the PHI
2197 nodes of the successors of BB. */
2199 static void
2200 cprop_into_successor_phis (basic_block bb, bitmap nonzero_vars)
2202 edge e;
2203 edge_iterator ei;
2205 /* This can get rather expensive if the implementation is naive in
2206 how it finds the phi alternative associated with a particular edge. */
2207 FOR_EACH_EDGE (e, ei, bb->succs)
2209 tree phi;
2210 int phi_num_args;
2211 int hint;
2213 /* If this is an abnormal edge, then we do not want to copy propagate
2214 into the PHI alternative associated with this edge. */
2215 if (e->flags & EDGE_ABNORMAL)
2216 continue;
2218 phi = phi_nodes (e->dest);
2219 if (! phi)
2220 continue;
2222 /* There is no guarantee that for any two PHI nodes in a block that
2223 the phi alternative associated with a particular edge will be
2224 at the same index in the phi alternative array.
2226 However, it is very likely they will be the same. So we keep
2227 track of the index of the alternative where we found the edge in
2228 the previous phi node and check that index first in the next
2229 phi node. If that hint fails, then we actually search all
2230 the entries. */
2231 phi_num_args = PHI_NUM_ARGS (phi);
2232 hint = phi_num_args;
2233 for ( ; phi; phi = PHI_CHAIN (phi))
2235 int i;
2236 tree new;
2237 use_operand_p orig_p;
2238 tree orig;
2240 /* If the hint is valid (!= phi_num_args), see if it points
2241 us to the desired phi alternative. */
2242 if (hint != phi_num_args && PHI_ARG_EDGE (phi, hint) == e)
2244 else
2246 /* The hint was either invalid or did not point to the
2247 correct phi alternative. Search all the alternatives
2248 for the correct one. Update the hint. */
2249 for (i = 0; i < phi_num_args; i++)
2250 if (PHI_ARG_EDGE (phi, i) == e)
2251 break;
2252 hint = i;
2255 /* If we did not find the proper alternative, then something is
2256 horribly wrong. */
2257 gcc_assert (hint != phi_num_args);
2259 /* The alternative may be associated with a constant, so verify
2260 it is an SSA_NAME before doing anything with it. */
2261 orig_p = PHI_ARG_DEF_PTR (phi, hint);
2262 orig = USE_FROM_PTR (orig_p);
2263 if (TREE_CODE (orig) != SSA_NAME)
2264 continue;
2266 /* If the alternative is known to have a nonzero value, record
2267 that fact in the PHI node itself for future use. */
2268 if (bitmap_bit_p (nonzero_vars, SSA_NAME_VERSION (orig)))
2269 PHI_ARG_NONZERO (phi, hint) = true;
2271 /* If we have *ORIG_P in our constant/copy table, then replace
2272 ORIG_P with its value in our constant/copy table. */
2273 new = SSA_NAME_VALUE (orig);
2274 if (new
2275 && (TREE_CODE (new) == SSA_NAME
2276 || is_gimple_min_invariant (new))
2277 && may_propagate_copy (orig, new))
2279 propagate_value (orig_p, new);
2286 /* Propagate known constants/copies into PHI nodes of BB's successor
2287 blocks. */
2289 static void
2290 cprop_into_phis (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
2291 basic_block bb)
2293 cprop_into_successor_phis (bb, nonzero_vars);
2296 /* Search for redundant computations in STMT. If any are found, then
2297 replace them with the variable holding the result of the computation.
2299 If safe, record this expression into the available expression hash
2300 table. */
2302 static bool
2303 eliminate_redundant_computations (struct dom_walk_data *walk_data,
2304 tree stmt, stmt_ann_t ann)
2306 v_may_def_optype v_may_defs = V_MAY_DEF_OPS (ann);
2307 tree *expr_p, def = NULL_TREE;
2308 bool insert = true;
2309 tree cached_lhs;
2310 bool retval = false;
2312 if (TREE_CODE (stmt) == MODIFY_EXPR)
2313 def = TREE_OPERAND (stmt, 0);
2315 /* Certain expressions on the RHS can be optimized away, but can not
2316 themselves be entered into the hash tables. */
2317 if (ann->makes_aliased_stores
2318 || ! def
2319 || TREE_CODE (def) != SSA_NAME
2320 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)
2321 || NUM_V_MAY_DEFS (v_may_defs) != 0)
2322 insert = false;
2324 /* Check if the expression has been computed before. */
2325 cached_lhs = lookup_avail_expr (stmt, insert);
2327 /* If this is an assignment and the RHS was not in the hash table,
2328 then try to simplify the RHS and lookup the new RHS in the
2329 hash table. */
2330 if (! cached_lhs && TREE_CODE (stmt) == MODIFY_EXPR)
2331 cached_lhs = simplify_rhs_and_lookup_avail_expr (walk_data, stmt, insert);
2332 /* Similarly if this is a COND_EXPR and we did not find its
2333 expression in the hash table, simplify the condition and
2334 try again. */
2335 else if (! cached_lhs && TREE_CODE (stmt) == COND_EXPR)
2336 cached_lhs = simplify_cond_and_lookup_avail_expr (stmt, ann, insert);
2337 /* Similarly for a SWITCH_EXPR. */
2338 else if (!cached_lhs && TREE_CODE (stmt) == SWITCH_EXPR)
2339 cached_lhs = simplify_switch_and_lookup_avail_expr (stmt, insert);
2341 opt_stats.num_exprs_considered++;
2343 /* Get a pointer to the expression we are trying to optimize. */
2344 if (TREE_CODE (stmt) == COND_EXPR)
2345 expr_p = &COND_EXPR_COND (stmt);
2346 else if (TREE_CODE (stmt) == SWITCH_EXPR)
2347 expr_p = &SWITCH_COND (stmt);
2348 else if (TREE_CODE (stmt) == RETURN_EXPR && TREE_OPERAND (stmt, 0))
2349 expr_p = &TREE_OPERAND (TREE_OPERAND (stmt, 0), 1);
2350 else
2351 expr_p = &TREE_OPERAND (stmt, 1);
2353 /* It is safe to ignore types here since we have already done
2354 type checking in the hashing and equality routines. In fact
2355 type checking here merely gets in the way of constant
2356 propagation. Also, make sure that it is safe to propagate
2357 CACHED_LHS into *EXPR_P. */
2358 if (cached_lhs
2359 && (TREE_CODE (cached_lhs) != SSA_NAME
2360 || may_propagate_copy (*expr_p, cached_lhs)))
2362 if (dump_file && (dump_flags & TDF_DETAILS))
2364 fprintf (dump_file, " Replaced redundant expr '");
2365 print_generic_expr (dump_file, *expr_p, dump_flags);
2366 fprintf (dump_file, "' with '");
2367 print_generic_expr (dump_file, cached_lhs, dump_flags);
2368 fprintf (dump_file, "'\n");
2371 opt_stats.num_re++;
2373 #if defined ENABLE_CHECKING
2374 gcc_assert (TREE_CODE (cached_lhs) == SSA_NAME
2375 || is_gimple_min_invariant (cached_lhs));
2376 #endif
2378 if (TREE_CODE (cached_lhs) == ADDR_EXPR
2379 || (POINTER_TYPE_P (TREE_TYPE (*expr_p))
2380 && is_gimple_min_invariant (cached_lhs)))
2381 retval = true;
2383 propagate_tree_value (expr_p, cached_lhs);
2384 mark_stmt_modified (stmt);
2386 return retval;
2389 /* STMT, a MODIFY_EXPR, may create certain equivalences, in either
2390 the available expressions table or the const_and_copies table.
2391 Detect and record those equivalences. */
2393 static void
2394 record_equivalences_from_stmt (tree stmt,
2395 int may_optimize_p,
2396 stmt_ann_t ann)
2398 tree lhs = TREE_OPERAND (stmt, 0);
2399 enum tree_code lhs_code = TREE_CODE (lhs);
2400 int i;
2402 if (lhs_code == SSA_NAME)
2404 tree rhs = TREE_OPERAND (stmt, 1);
2406 /* Strip away any useless type conversions. */
2407 STRIP_USELESS_TYPE_CONVERSION (rhs);
2409 /* If the RHS of the assignment is a constant or another variable that
2410 may be propagated, register it in the CONST_AND_COPIES table. We
2411 do not need to record unwind data for this, since this is a true
2412 assignment and not an equivalence inferred from a comparison. All
2413 uses of this ssa name are dominated by this assignment, so unwinding
2414 just costs time and space. */
2415 if (may_optimize_p
2416 && (TREE_CODE (rhs) == SSA_NAME
2417 || is_gimple_min_invariant (rhs)))
2418 SSA_NAME_VALUE (lhs) = rhs;
2420 /* alloca never returns zero and the address of a non-weak symbol
2421 is never zero. NOP_EXPRs and CONVERT_EXPRs can be completely
2422 stripped as they do not affect this equivalence. */
2423 while (TREE_CODE (rhs) == NOP_EXPR
2424 || TREE_CODE (rhs) == CONVERT_EXPR)
2425 rhs = TREE_OPERAND (rhs, 0);
2427 if (alloca_call_p (rhs)
2428 || (TREE_CODE (rhs) == ADDR_EXPR
2429 && DECL_P (TREE_OPERAND (rhs, 0))
2430 && ! DECL_WEAK (TREE_OPERAND (rhs, 0))))
2431 record_var_is_nonzero (lhs);
2433 /* IOR of any value with a nonzero value will result in a nonzero
2434 value. Even if we do not know the exact result recording that
2435 the result is nonzero is worth the effort. */
2436 if (TREE_CODE (rhs) == BIT_IOR_EXPR
2437 && integer_nonzerop (TREE_OPERAND (rhs, 1)))
2438 record_var_is_nonzero (lhs);
2441 /* Look at both sides for pointer dereferences. If we find one, then
2442 the pointer must be nonnull and we can enter that equivalence into
2443 the hash tables. */
2444 if (flag_delete_null_pointer_checks)
2445 for (i = 0; i < 2; i++)
2447 tree t = TREE_OPERAND (stmt, i);
2449 /* Strip away any COMPONENT_REFs. */
2450 while (TREE_CODE (t) == COMPONENT_REF)
2451 t = TREE_OPERAND (t, 0);
2453 /* Now see if this is a pointer dereference. */
2454 if (INDIRECT_REF_P (t))
2456 tree op = TREE_OPERAND (t, 0);
2458 /* If the pointer is a SSA variable, then enter new
2459 equivalences into the hash table. */
2460 while (TREE_CODE (op) == SSA_NAME)
2462 tree def = SSA_NAME_DEF_STMT (op);
2464 record_var_is_nonzero (op);
2466 /* And walk up the USE-DEF chains noting other SSA_NAMEs
2467 which are known to have a nonzero value. */
2468 if (def
2469 && TREE_CODE (def) == MODIFY_EXPR
2470 && TREE_CODE (TREE_OPERAND (def, 1)) == NOP_EXPR)
2471 op = TREE_OPERAND (TREE_OPERAND (def, 1), 0);
2472 else
2473 break;
2478 /* A memory store, even an aliased store, creates a useful
2479 equivalence. By exchanging the LHS and RHS, creating suitable
2480 vops and recording the result in the available expression table,
2481 we may be able to expose more redundant loads. */
2482 if (!ann->has_volatile_ops
2483 && (TREE_CODE (TREE_OPERAND (stmt, 1)) == SSA_NAME
2484 || is_gimple_min_invariant (TREE_OPERAND (stmt, 1)))
2485 && !is_gimple_reg (lhs))
2487 tree rhs = TREE_OPERAND (stmt, 1);
2488 tree new;
2490 /* FIXME: If the LHS of the assignment is a bitfield and the RHS
2491 is a constant, we need to adjust the constant to fit into the
2492 type of the LHS. If the LHS is a bitfield and the RHS is not
2493 a constant, then we can not record any equivalences for this
2494 statement since we would need to represent the widening or
2495 narrowing of RHS. This fixes gcc.c-torture/execute/921016-1.c
2496 and should not be necessary if GCC represented bitfields
2497 properly. */
2498 if (lhs_code == COMPONENT_REF
2499 && DECL_BIT_FIELD (TREE_OPERAND (lhs, 1)))
2501 if (TREE_CONSTANT (rhs))
2502 rhs = widen_bitfield (rhs, TREE_OPERAND (lhs, 1), lhs);
2503 else
2504 rhs = NULL;
2506 /* If the value overflowed, then we can not use this equivalence. */
2507 if (rhs && ! is_gimple_min_invariant (rhs))
2508 rhs = NULL;
2511 if (rhs)
2513 /* Build a new statement with the RHS and LHS exchanged. */
2514 new = build (MODIFY_EXPR, TREE_TYPE (stmt), rhs, lhs);
2516 create_ssa_artficial_load_stmt (&(ann->operands), new);
2518 /* Finally enter the statement into the available expression
2519 table. */
2520 lookup_avail_expr (new, true);
2525 /* Replace *OP_P in STMT with any known equivalent value for *OP_P from
2526 CONST_AND_COPIES. */
2528 static bool
2529 cprop_operand (tree stmt, use_operand_p op_p)
2531 bool may_have_exposed_new_symbols = false;
2532 tree val;
2533 tree op = USE_FROM_PTR (op_p);
2535 /* If the operand has a known constant value or it is known to be a
2536 copy of some other variable, use the value or copy stored in
2537 CONST_AND_COPIES. */
2538 val = SSA_NAME_VALUE (op);
2539 if (val && TREE_CODE (val) != VALUE_HANDLE)
2541 tree op_type, val_type;
2543 /* Do not change the base variable in the virtual operand
2544 tables. That would make it impossible to reconstruct
2545 the renamed virtual operand if we later modify this
2546 statement. Also only allow the new value to be an SSA_NAME
2547 for propagation into virtual operands. */
2548 if (!is_gimple_reg (op)
2549 && (get_virtual_var (val) != get_virtual_var (op)
2550 || TREE_CODE (val) != SSA_NAME))
2551 return false;
2553 /* Do not replace hard register operands in asm statements. */
2554 if (TREE_CODE (stmt) == ASM_EXPR
2555 && !may_propagate_copy_into_asm (op))
2556 return false;
2558 /* Get the toplevel type of each operand. */
2559 op_type = TREE_TYPE (op);
2560 val_type = TREE_TYPE (val);
2562 /* While both types are pointers, get the type of the object
2563 pointed to. */
2564 while (POINTER_TYPE_P (op_type) && POINTER_TYPE_P (val_type))
2566 op_type = TREE_TYPE (op_type);
2567 val_type = TREE_TYPE (val_type);
2570 /* Make sure underlying types match before propagating a constant by
2571 converting the constant to the proper type. Note that convert may
2572 return a non-gimple expression, in which case we ignore this
2573 propagation opportunity. */
2574 if (TREE_CODE (val) != SSA_NAME)
2576 if (!lang_hooks.types_compatible_p (op_type, val_type))
2578 val = fold_convert (TREE_TYPE (op), val);
2579 if (!is_gimple_min_invariant (val))
2580 return false;
2584 /* Certain operands are not allowed to be copy propagated due
2585 to their interaction with exception handling and some GCC
2586 extensions. */
2587 else if (!may_propagate_copy (op, val))
2588 return false;
2590 /* Dump details. */
2591 if (dump_file && (dump_flags & TDF_DETAILS))
2593 fprintf (dump_file, " Replaced '");
2594 print_generic_expr (dump_file, op, dump_flags);
2595 fprintf (dump_file, "' with %s '",
2596 (TREE_CODE (val) != SSA_NAME ? "constant" : "variable"));
2597 print_generic_expr (dump_file, val, dump_flags);
2598 fprintf (dump_file, "'\n");
2601 /* If VAL is an ADDR_EXPR or a constant of pointer type, note
2602 that we may have exposed a new symbol for SSA renaming. */
2603 if (TREE_CODE (val) == ADDR_EXPR
2604 || (POINTER_TYPE_P (TREE_TYPE (op))
2605 && is_gimple_min_invariant (val)))
2606 may_have_exposed_new_symbols = true;
2608 propagate_value (op_p, val);
2610 /* And note that we modified this statement. This is now
2611 safe, even if we changed virtual operands since we will
2612 rescan the statement and rewrite its operands again. */
2613 mark_stmt_modified (stmt);
2615 return may_have_exposed_new_symbols;
2618 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
2619 known value for that SSA_NAME (or NULL if no value is known).
2621 Propagate values from CONST_AND_COPIES into the uses, vuses and
2622 v_may_def_ops of STMT. */
2624 static bool
2625 cprop_into_stmt (tree stmt)
2627 bool may_have_exposed_new_symbols = false;
2628 use_operand_p op_p;
2629 ssa_op_iter iter;
2630 tree rhs;
2632 FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_ALL_USES)
2634 if (TREE_CODE (USE_FROM_PTR (op_p)) == SSA_NAME)
2635 may_have_exposed_new_symbols |= cprop_operand (stmt, op_p);
2638 if (may_have_exposed_new_symbols)
2640 rhs = get_rhs (stmt);
2641 if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
2642 recompute_tree_invarant_for_addr_expr (rhs);
2645 return may_have_exposed_new_symbols;
2649 /* Optimize the statement pointed by iterator SI.
2651 We try to perform some simplistic global redundancy elimination and
2652 constant propagation:
2654 1- To detect global redundancy, we keep track of expressions that have
2655 been computed in this block and its dominators. If we find that the
2656 same expression is computed more than once, we eliminate repeated
2657 computations by using the target of the first one.
2659 2- Constant values and copy assignments. This is used to do very
2660 simplistic constant and copy propagation. When a constant or copy
2661 assignment is found, we map the value on the RHS of the assignment to
2662 the variable in the LHS in the CONST_AND_COPIES table. */
2664 static void
2665 optimize_stmt (struct dom_walk_data *walk_data, basic_block bb,
2666 block_stmt_iterator si)
2668 stmt_ann_t ann;
2669 tree stmt;
2670 bool may_optimize_p;
2671 bool may_have_exposed_new_symbols = false;
2673 stmt = bsi_stmt (si);
2675 update_stmt_if_modified (stmt);
2676 ann = stmt_ann (stmt);
2677 opt_stats.num_stmts++;
2678 may_have_exposed_new_symbols = false;
2680 if (dump_file && (dump_flags & TDF_DETAILS))
2682 fprintf (dump_file, "Optimizing statement ");
2683 print_generic_stmt (dump_file, stmt, TDF_SLIM);
2686 /* Const/copy propagate into USES, VUSES and the RHS of V_MAY_DEFs. */
2687 may_have_exposed_new_symbols = cprop_into_stmt (stmt);
2689 /* If the statement has been modified with constant replacements,
2690 fold its RHS before checking for redundant computations. */
2691 if (ann->modified)
2693 /* Try to fold the statement making sure that STMT is kept
2694 up to date. */
2695 if (fold_stmt (bsi_stmt_ptr (si)))
2697 stmt = bsi_stmt (si);
2698 ann = stmt_ann (stmt);
2700 if (dump_file && (dump_flags & TDF_DETAILS))
2702 fprintf (dump_file, " Folded to: ");
2703 print_generic_stmt (dump_file, stmt, TDF_SLIM);
2707 /* Constant/copy propagation above may change the set of
2708 virtual operands associated with this statement. Folding
2709 may remove the need for some virtual operands.
2711 Indicate we will need to rescan and rewrite the statement. */
2712 may_have_exposed_new_symbols = true;
2715 /* Check for redundant computations. Do this optimization only
2716 for assignments that have no volatile ops and conditionals. */
2717 may_optimize_p = (!ann->has_volatile_ops
2718 && ((TREE_CODE (stmt) == RETURN_EXPR
2719 && TREE_OPERAND (stmt, 0)
2720 && TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR
2721 && ! (TREE_SIDE_EFFECTS
2722 (TREE_OPERAND (TREE_OPERAND (stmt, 0), 1))))
2723 || (TREE_CODE (stmt) == MODIFY_EXPR
2724 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (stmt, 1)))
2725 || TREE_CODE (stmt) == COND_EXPR
2726 || TREE_CODE (stmt) == SWITCH_EXPR));
2728 if (may_optimize_p)
2729 may_have_exposed_new_symbols
2730 |= eliminate_redundant_computations (walk_data, stmt, ann);
2732 /* Record any additional equivalences created by this statement. */
2733 if (TREE_CODE (stmt) == MODIFY_EXPR)
2734 record_equivalences_from_stmt (stmt,
2735 may_optimize_p,
2736 ann);
2738 register_definitions_for_stmt (stmt);
2740 /* If STMT is a COND_EXPR and it was modified, then we may know
2741 where it goes. If that is the case, then mark the CFG as altered.
2743 This will cause us to later call remove_unreachable_blocks and
2744 cleanup_tree_cfg when it is safe to do so. It is not safe to
2745 clean things up here since removal of edges and such can trigger
2746 the removal of PHI nodes, which in turn can release SSA_NAMEs to
2747 the manager.
2749 That's all fine and good, except that once SSA_NAMEs are released
2750 to the manager, we must not call create_ssa_name until all references
2751 to released SSA_NAMEs have been eliminated.
2753 All references to the deleted SSA_NAMEs can not be eliminated until
2754 we remove unreachable blocks.
2756 We can not remove unreachable blocks until after we have completed
2757 any queued jump threading.
2759 We can not complete any queued jump threads until we have taken
2760 appropriate variables out of SSA form. Taking variables out of
2761 SSA form can call create_ssa_name and thus we lose.
2763 Ultimately I suspect we're going to need to change the interface
2764 into the SSA_NAME manager. */
2766 if (ann->modified)
2768 tree val = NULL;
2770 if (TREE_CODE (stmt) == COND_EXPR)
2771 val = COND_EXPR_COND (stmt);
2772 else if (TREE_CODE (stmt) == SWITCH_EXPR)
2773 val = SWITCH_COND (stmt);
2775 if (val && TREE_CODE (val) == INTEGER_CST && find_taken_edge (bb, val))
2776 cfg_altered = true;
2778 /* If we simplified a statement in such a way as to be shown that it
2779 cannot trap, update the eh information and the cfg to match. */
2780 if (maybe_clean_eh_stmt (stmt))
2782 bitmap_set_bit (need_eh_cleanup, bb->index);
2783 if (dump_file && (dump_flags & TDF_DETAILS))
2784 fprintf (dump_file, " Flagged to clear EH edges.\n");
2788 if (may_have_exposed_new_symbols)
2789 VARRAY_PUSH_TREE (stmts_to_rescan, bsi_stmt (si));
2792 /* Replace the RHS of STMT with NEW_RHS. If RHS can be found in the
2793 available expression hashtable, then return the LHS from the hash
2794 table.
2796 If INSERT is true, then we also update the available expression
2797 hash table to account for the changes made to STMT. */
2799 static tree
2800 update_rhs_and_lookup_avail_expr (tree stmt, tree new_rhs, bool insert)
2802 tree cached_lhs = NULL;
2804 /* Remove the old entry from the hash table. */
2805 if (insert)
2807 struct expr_hash_elt element;
2809 initialize_hash_element (stmt, NULL, &element);
2810 htab_remove_elt_with_hash (avail_exprs, &element, element.hash);
2813 /* Now update the RHS of the assignment. */
2814 TREE_OPERAND (stmt, 1) = new_rhs;
2816 /* Now lookup the updated statement in the hash table. */
2817 cached_lhs = lookup_avail_expr (stmt, insert);
2819 /* We have now called lookup_avail_expr twice with two different
2820 versions of this same statement, once in optimize_stmt, once here.
2822 We know the call in optimize_stmt did not find an existing entry
2823 in the hash table, so a new entry was created. At the same time
2824 this statement was pushed onto the BLOCK_AVAIL_EXPRS varray.
2826 If this call failed to find an existing entry on the hash table,
2827 then the new version of this statement was entered into the
2828 hash table. And this statement was pushed onto BLOCK_AVAIL_EXPR
2829 for the second time. So there are two copies on BLOCK_AVAIL_EXPRs
2831 If this call succeeded, we still have one copy of this statement
2832 on the BLOCK_AVAIL_EXPRs varray.
2834 For both cases, we need to pop the most recent entry off the
2835 BLOCK_AVAIL_EXPRs varray. For the case where we never found this
2836 statement in the hash tables, that will leave precisely one
2837 copy of this statement on BLOCK_AVAIL_EXPRs. For the case where
2838 we found a copy of this statement in the second hash table lookup
2839 we want _no_ copies of this statement in BLOCK_AVAIL_EXPRs. */
2840 if (insert)
2841 VARRAY_POP (avail_exprs_stack);
2843 /* And make sure we record the fact that we modified this
2844 statement. */
2845 mark_stmt_modified (stmt);
2847 return cached_lhs;
2850 /* Search for an existing instance of STMT in the AVAIL_EXPRS table. If
2851 found, return its LHS. Otherwise insert STMT in the table and return
2852 NULL_TREE.
2854 Also, when an expression is first inserted in the AVAIL_EXPRS table, it
2855 is also added to the stack pointed by BLOCK_AVAIL_EXPRS_P, so that they
2856 can be removed when we finish processing this block and its children.
2858 NOTE: This function assumes that STMT is a MODIFY_EXPR node that
2859 contains no CALL_EXPR on its RHS and makes no volatile nor
2860 aliased references. */
2862 static tree
2863 lookup_avail_expr (tree stmt, bool insert)
2865 void **slot;
2866 tree lhs;
2867 tree temp;
2868 struct expr_hash_elt *element = xcalloc (sizeof (struct expr_hash_elt), 1);
2870 lhs = TREE_CODE (stmt) == MODIFY_EXPR ? TREE_OPERAND (stmt, 0) : NULL;
2872 initialize_hash_element (stmt, lhs, element);
2874 /* Don't bother remembering constant assignments and copy operations.
2875 Constants and copy operations are handled by the constant/copy propagator
2876 in optimize_stmt. */
2877 if (TREE_CODE (element->rhs) == SSA_NAME
2878 || is_gimple_min_invariant (element->rhs))
2880 free (element);
2881 return NULL_TREE;
2884 /* If this is an equality test against zero, see if we have recorded a
2885 nonzero value for the variable in question. */
2886 if ((TREE_CODE (element->rhs) == EQ_EXPR
2887 || TREE_CODE (element->rhs) == NE_EXPR)
2888 && TREE_CODE (TREE_OPERAND (element->rhs, 0)) == SSA_NAME
2889 && integer_zerop (TREE_OPERAND (element->rhs, 1)))
2891 int indx = SSA_NAME_VERSION (TREE_OPERAND (element->rhs, 0));
2893 if (bitmap_bit_p (nonzero_vars, indx))
2895 tree t = element->rhs;
2896 free (element);
2898 if (TREE_CODE (t) == EQ_EXPR)
2899 return boolean_false_node;
2900 else
2901 return boolean_true_node;
2905 /* Finally try to find the expression in the main expression hash table. */
2906 slot = htab_find_slot_with_hash (avail_exprs, element, element->hash,
2907 (insert ? INSERT : NO_INSERT));
2908 if (slot == NULL)
2910 free (element);
2911 return NULL_TREE;
2914 if (*slot == NULL)
2916 *slot = (void *) element;
2917 VARRAY_PUSH_TREE (avail_exprs_stack, stmt ? stmt : element->rhs);
2918 return NULL_TREE;
2921 /* Extract the LHS of the assignment so that it can be used as the current
2922 definition of another variable. */
2923 lhs = ((struct expr_hash_elt *)*slot)->lhs;
2925 /* See if the LHS appears in the CONST_AND_COPIES table. If it does, then
2926 use the value from the const_and_copies table. */
2927 if (TREE_CODE (lhs) == SSA_NAME)
2929 temp = SSA_NAME_VALUE (lhs);
2930 if (temp && TREE_CODE (temp) != VALUE_HANDLE)
2931 lhs = temp;
2934 free (element);
2935 return lhs;
2938 /* Given a condition COND, record into HI_P, LO_P and INVERTED_P the
2939 range of values that result in the conditional having a true value.
2941 Return true if we are successful in extracting a range from COND and
2942 false if we are unsuccessful. */
2944 static bool
2945 extract_range_from_cond (tree cond, tree *hi_p, tree *lo_p, int *inverted_p)
2947 tree op1 = TREE_OPERAND (cond, 1);
2948 tree high, low, type;
2949 int inverted;
2951 /* Experiments have shown that it's rarely, if ever useful to
2952 record ranges for enumerations. Presumably this is due to
2953 the fact that they're rarely used directly. They are typically
2954 cast into an integer type and used that way. */
2955 if (TREE_CODE (TREE_TYPE (op1)) != INTEGER_TYPE)
2956 return 0;
2958 type = TREE_TYPE (op1);
2960 switch (TREE_CODE (cond))
2962 case EQ_EXPR:
2963 high = low = op1;
2964 inverted = 0;
2965 break;
2967 case NE_EXPR:
2968 high = low = op1;
2969 inverted = 1;
2970 break;
2972 case GE_EXPR:
2973 low = op1;
2974 high = TYPE_MAX_VALUE (type);
2975 inverted = 0;
2976 break;
2978 case GT_EXPR:
2979 low = int_const_binop (PLUS_EXPR, op1, integer_one_node, 1);
2980 high = TYPE_MAX_VALUE (type);
2981 inverted = 0;
2982 break;
2984 case LE_EXPR:
2985 high = op1;
2986 low = TYPE_MIN_VALUE (type);
2987 inverted = 0;
2988 break;
2990 case LT_EXPR:
2991 high = int_const_binop (MINUS_EXPR, op1, integer_one_node, 1);
2992 low = TYPE_MIN_VALUE (type);
2993 inverted = 0;
2994 break;
2996 default:
2997 return 0;
3000 *hi_p = high;
3001 *lo_p = low;
3002 *inverted_p = inverted;
3003 return 1;
3006 /* Record a range created by COND for basic block BB. */
3008 static void
3009 record_range (tree cond, basic_block bb)
3011 /* We explicitly ignore NE_EXPRs. They rarely allow for meaningful
3012 range optimizations and significantly complicate the implementation. */
3013 if (COMPARISON_CLASS_P (cond)
3014 && TREE_CODE (cond) != NE_EXPR
3015 && TREE_CODE (TREE_TYPE (TREE_OPERAND (cond, 1))) == INTEGER_TYPE)
3017 struct vrp_hash_elt *vrp_hash_elt;
3018 struct vrp_element *element;
3019 varray_type *vrp_records_p;
3020 void **slot;
3023 vrp_hash_elt = xmalloc (sizeof (struct vrp_hash_elt));
3024 vrp_hash_elt->var = TREE_OPERAND (cond, 0);
3025 vrp_hash_elt->records = NULL;
3026 slot = htab_find_slot (vrp_data, vrp_hash_elt, INSERT);
3028 if (*slot == NULL)
3029 *slot = (void *) vrp_hash_elt;
3031 vrp_hash_elt = (struct vrp_hash_elt *) *slot;
3032 vrp_records_p = &vrp_hash_elt->records;
3034 element = ggc_alloc (sizeof (struct vrp_element));
3035 element->low = NULL;
3036 element->high = NULL;
3037 element->cond = cond;
3038 element->bb = bb;
3040 if (*vrp_records_p == NULL)
3041 VARRAY_GENERIC_PTR_INIT (*vrp_records_p, 2, "vrp records");
3043 VARRAY_PUSH_GENERIC_PTR (*vrp_records_p, element);
3044 VARRAY_PUSH_TREE (vrp_variables_stack, TREE_OPERAND (cond, 0));
3048 /* Given a conditional statement IF_STMT, return the assignment 'X = Y'
3049 known to be true depending on which arm of IF_STMT is taken.
3051 Not all conditional statements will result in a useful assignment.
3052 Return NULL_TREE in that case.
3054 Also enter into the available expression table statements of
3055 the form:
3057 TRUE ARM FALSE ARM
3058 1 = cond 1 = cond'
3059 0 = cond' 0 = cond
3061 This allows us to lookup the condition in a dominated block and
3062 get back a constant indicating if the condition is true. */
3064 static struct eq_expr_value
3065 get_eq_expr_value (tree if_stmt,
3066 int true_arm,
3067 basic_block bb)
3069 tree cond;
3070 struct eq_expr_value retval;
3072 cond = COND_EXPR_COND (if_stmt);
3073 retval.src = NULL;
3074 retval.dst = NULL;
3076 /* If the conditional is a single variable 'X', return 'X = 1' for
3077 the true arm and 'X = 0' on the false arm. */
3078 if (TREE_CODE (cond) == SSA_NAME)
3080 retval.dst = cond;
3081 retval.src = constant_boolean_node (true_arm, TREE_TYPE (cond));
3082 return retval;
3085 /* If we have a comparison expression, then record its result into
3086 the available expression table. */
3087 if (COMPARISON_CLASS_P (cond))
3089 tree op0 = TREE_OPERAND (cond, 0);
3090 tree op1 = TREE_OPERAND (cond, 1);
3092 /* Special case comparing booleans against a constant as we know
3093 the value of OP0 on both arms of the branch. i.e., we can record
3094 an equivalence for OP0 rather than COND. */
3095 if ((TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
3096 && TREE_CODE (op0) == SSA_NAME
3097 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
3098 && is_gimple_min_invariant (op1))
3100 if ((TREE_CODE (cond) == EQ_EXPR && true_arm)
3101 || (TREE_CODE (cond) == NE_EXPR && ! true_arm))
3103 retval.src = op1;
3105 else
3107 if (integer_zerop (op1))
3108 retval.src = boolean_true_node;
3109 else
3110 retval.src = boolean_false_node;
3112 retval.dst = op0;
3113 return retval;
3116 if (TREE_CODE (op0) == SSA_NAME
3117 && (is_gimple_min_invariant (op1) || TREE_CODE (op1) == SSA_NAME))
3119 tree inverted = invert_truthvalue (cond);
3121 /* When we find an available expression in the hash table, we replace
3122 the expression with the LHS of the statement in the hash table.
3124 So, we want to build statements such as "1 = <condition>" on the
3125 true arm and "0 = <condition>" on the false arm. That way if we
3126 find the expression in the table, we will replace it with its
3127 known constant value. Also insert inversions of the result and
3128 condition into the hash table. */
3129 if (true_arm)
3131 record_cond (cond, boolean_true_node);
3132 record_dominating_conditions (cond);
3133 record_cond (inverted, boolean_false_node);
3135 if (TREE_CONSTANT (op1))
3136 record_range (cond, bb);
3138 /* If the conditional is of the form 'X == Y', return 'X = Y'
3139 for the true arm. */
3140 if (TREE_CODE (cond) == EQ_EXPR)
3142 retval.dst = op0;
3143 retval.src = op1;
3144 return retval;
3147 else
3150 record_cond (inverted, boolean_true_node);
3151 record_dominating_conditions (inverted);
3152 record_cond (cond, boolean_false_node);
3154 if (TREE_CONSTANT (op1))
3155 record_range (inverted, bb);
3157 /* If the conditional is of the form 'X != Y', return 'X = Y'
3158 for the false arm. */
3159 if (TREE_CODE (cond) == NE_EXPR)
3161 retval.dst = op0;
3162 retval.src = op1;
3163 return retval;
3169 return retval;
3172 /* Hashing and equality functions for VRP_DATA.
3174 Since this hash table is addressed by SSA_NAMEs, we can hash on
3175 their version number and equality can be determined with a
3176 pointer comparison. */
3178 static hashval_t
3179 vrp_hash (const void *p)
3181 tree var = ((struct vrp_hash_elt *)p)->var;
3183 return SSA_NAME_VERSION (var);
3186 static int
3187 vrp_eq (const void *p1, const void *p2)
3189 tree var1 = ((struct vrp_hash_elt *)p1)->var;
3190 tree var2 = ((struct vrp_hash_elt *)p2)->var;
3192 return var1 == var2;
3195 /* Hashing and equality functions for AVAIL_EXPRS. The table stores
3196 MODIFY_EXPR statements. We compute a value number for expressions using
3197 the code of the expression and the SSA numbers of its operands. */
3199 static hashval_t
3200 avail_expr_hash (const void *p)
3202 stmt_ann_t ann = ((struct expr_hash_elt *)p)->ann;
3203 tree rhs = ((struct expr_hash_elt *)p)->rhs;
3204 hashval_t val = 0;
3205 size_t i;
3206 vuse_optype vuses;
3208 /* iterative_hash_expr knows how to deal with any expression and
3209 deals with commutative operators as well, so just use it instead
3210 of duplicating such complexities here. */
3211 val = iterative_hash_expr (rhs, val);
3213 /* If the hash table entry is not associated with a statement, then we
3214 can just hash the expression and not worry about virtual operands
3215 and such. */
3216 if (!ann)
3217 return val;
3219 /* Add the SSA version numbers of every vuse operand. This is important
3220 because compound variables like arrays are not renamed in the
3221 operands. Rather, the rename is done on the virtual variable
3222 representing all the elements of the array. */
3223 vuses = VUSE_OPS (ann);
3224 for (i = 0; i < NUM_VUSES (vuses); i++)
3225 val = iterative_hash_expr (VUSE_OP (vuses, i), val);
3227 return val;
3230 static hashval_t
3231 real_avail_expr_hash (const void *p)
3233 return ((const struct expr_hash_elt *)p)->hash;
3236 static int
3237 avail_expr_eq (const void *p1, const void *p2)
3239 stmt_ann_t ann1 = ((struct expr_hash_elt *)p1)->ann;
3240 tree rhs1 = ((struct expr_hash_elt *)p1)->rhs;
3241 stmt_ann_t ann2 = ((struct expr_hash_elt *)p2)->ann;
3242 tree rhs2 = ((struct expr_hash_elt *)p2)->rhs;
3244 /* If they are the same physical expression, return true. */
3245 if (rhs1 == rhs2 && ann1 == ann2)
3246 return true;
3248 /* If their codes are not equal, then quit now. */
3249 if (TREE_CODE (rhs1) != TREE_CODE (rhs2))
3250 return false;
3252 /* In case of a collision, both RHS have to be identical and have the
3253 same VUSE operands. */
3254 if ((TREE_TYPE (rhs1) == TREE_TYPE (rhs2)
3255 || lang_hooks.types_compatible_p (TREE_TYPE (rhs1), TREE_TYPE (rhs2)))
3256 && operand_equal_p (rhs1, rhs2, OEP_PURE_SAME))
3258 vuse_optype ops1 = NULL;
3259 vuse_optype ops2 = NULL;
3260 size_t num_ops1 = 0;
3261 size_t num_ops2 = 0;
3262 size_t i;
3264 if (ann1)
3266 ops1 = VUSE_OPS (ann1);
3267 num_ops1 = NUM_VUSES (ops1);
3270 if (ann2)
3272 ops2 = VUSE_OPS (ann2);
3273 num_ops2 = NUM_VUSES (ops2);
3276 /* If the number of virtual uses is different, then we consider
3277 them not equal. */
3278 if (num_ops1 != num_ops2)
3279 return false;
3281 for (i = 0; i < num_ops1; i++)
3282 if (VUSE_OP (ops1, i) != VUSE_OP (ops2, i))
3283 return false;
3285 gcc_assert (((struct expr_hash_elt *)p1)->hash
3286 == ((struct expr_hash_elt *)p2)->hash);
3287 return true;
3290 return false;
3293 /* Given STMT and a pointer to the block local definitions BLOCK_DEFS_P,
3294 register register all objects set by this statement into BLOCK_DEFS_P
3295 and CURRDEFS. */
3297 static void
3298 register_definitions_for_stmt (tree stmt)
3300 tree def;
3301 ssa_op_iter iter;
3303 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
3306 /* FIXME: We shouldn't be registering new defs if the variable
3307 doesn't need to be renamed. */
3308 register_new_def (def, &block_defs_stack);