PR target/16286
[official-gcc.git] / gcc / tree-ssa-dom.c
bloba7f0aaf0c6161fe3dbdd68622cf9084efbfee665
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. */
49 /* Structure for recording edge equivalences as well as any pending
50 edge redirections during the dominator optimizer.
52 Computing and storing the edge equivalences instead of creating
53 them on-demand can save significant amounts of time, particularly
54 for pathological cases involving switch statements.
56 These structures live for a single iteration of the dominator
57 optimizer in the edge's AUX field. At the end of an iteration we
58 free each of these structures and update the AUX field to point
59 to any requested redirection target (the code for updating the
60 CFG and SSA graph for edge redirection expects redirection edge
61 targets to be in the AUX field for each edge. */
63 struct edge_info
65 /* If this edge creates a simple equivalence, the LHS and RHS of
66 the equivalence will be stored here. */
67 tree lhs;
68 tree rhs;
70 /* Traversing an edge may also indicate one or more particular conditions
71 are true or false. The number of recorded conditions can vary, but
72 can be determined by the condition's code. So we have an array
73 and its maximum index rather than use a varray. */
74 tree *cond_equivalences;
75 unsigned int max_cond_equivalences;
77 /* If we can thread this edge this field records the new target. */
78 edge redirection_target;
82 /* Hash table with expressions made available during the renaming process.
83 When an assignment of the form X_i = EXPR is found, the statement is
84 stored in this table. If the same expression EXPR is later found on the
85 RHS of another statement, it is replaced with X_i (thus performing
86 global redundancy elimination). Similarly as we pass through conditionals
87 we record the conditional itself as having either a true or false value
88 in this table. */
89 static htab_t avail_exprs;
91 /* Stack of available expressions in AVAIL_EXPRs. Each block pushes any
92 expressions it enters into the hash table along with a marker entry
93 (null). When we finish processing the block, we pop off entries and
94 remove the expressions from the global hash table until we hit the
95 marker. */
96 static varray_type avail_exprs_stack;
98 /* Stack of trees used to restore the global currdefs to its original
99 state after completing optimization of a block and its dominator children.
101 An SSA_NAME indicates that the current definition of the underlying
102 variable should be set to the given SSA_NAME.
104 A _DECL node indicates that the underlying variable has no current
105 definition.
107 A NULL node is used to mark the last node associated with the
108 current block. */
109 varray_type block_defs_stack;
111 /* Stack of statements we need to rescan during finalization for newly
112 exposed variables.
114 Statement rescanning must occur after the current block's available
115 expressions are removed from AVAIL_EXPRS. Else we may change the
116 hash code for an expression and be unable to find/remove it from
117 AVAIL_EXPRS. */
118 varray_type stmts_to_rescan;
120 /* Structure for entries in the expression hash table.
122 This requires more memory for the hash table entries, but allows us
123 to avoid creating silly tree nodes and annotations for conditionals,
124 eliminates 2 global hash tables and two block local varrays.
126 It also allows us to reduce the number of hash table lookups we
127 have to perform in lookup_avail_expr and finally it allows us to
128 significantly reduce the number of calls into the hashing routine
129 itself. */
131 struct expr_hash_elt
133 /* The value (lhs) of this expression. */
134 tree lhs;
136 /* The expression (rhs) we want to record. */
137 tree rhs;
139 /* The annotation if this element corresponds to a statement. */
140 stmt_ann_t ann;
142 /* The hash value for RHS/ann. */
143 hashval_t hash;
146 /* Stack of dest,src pairs that need to be restored during finalization.
148 A NULL entry is used to mark the end of pairs which need to be
149 restored during finalization of this block. */
150 static varray_type const_and_copies_stack;
152 /* Bitmap of SSA_NAMEs known to have a nonzero value, even if we do not
153 know their exact value. */
154 static bitmap nonzero_vars;
156 /* Stack of SSA_NAMEs which need their NONZERO_VARS property cleared
157 when the current block is finalized.
159 A NULL entry is used to mark the end of names needing their
160 entry in NONZERO_VARS cleared during finalization of this block. */
161 static varray_type nonzero_vars_stack;
163 /* Track whether or not we have changed the control flow graph. */
164 static bool cfg_altered;
166 /* Bitmap of blocks that have had EH statements cleaned. We should
167 remove their dead edges eventually. */
168 static bitmap need_eh_cleanup;
170 /* Statistics for dominator optimizations. */
171 struct opt_stats_d
173 long num_stmts;
174 long num_exprs_considered;
175 long num_re;
178 static struct opt_stats_d opt_stats;
180 /* Value range propagation record. Each time we encounter a conditional
181 of the form SSA_NAME COND CONST we create a new vrp_element to record
182 how the condition affects the possible values SSA_NAME may have.
184 Each record contains the condition tested (COND), and the the range of
185 values the variable may legitimately have if COND is true. Note the
186 range of values may be a smaller range than COND specifies if we have
187 recorded other ranges for this variable. Each record also contains the
188 block in which the range was recorded for invalidation purposes.
190 Note that the current known range is computed lazily. This allows us
191 to avoid the overhead of computing ranges which are never queried.
193 When we encounter a conditional, we look for records which constrain
194 the SSA_NAME used in the condition. In some cases those records allow
195 us to determine the condition's result at compile time. In other cases
196 they may allow us to simplify the condition.
198 We also use value ranges to do things like transform signed div/mod
199 operations into unsigned div/mod or to simplify ABS_EXPRs.
201 Simple experiments have shown these optimizations to not be all that
202 useful on switch statements (much to my surprise). So switch statement
203 optimizations are not performed.
205 Note carefully we do not propagate information through each statement
206 in the block. i.e., if we know variable X has a value defined of
207 [0, 25] and we encounter Y = X + 1, we do not track a value range
208 for Y (which would be [1, 26] if we cared). Similarly we do not
209 constrain values as we encounter narrowing typecasts, etc. */
211 struct vrp_element
213 /* The highest and lowest values the variable in COND may contain when
214 COND is true. Note this may not necessarily be the same values
215 tested by COND if the same variable was used in earlier conditionals.
217 Note this is computed lazily and thus can be NULL indicating that
218 the values have not been computed yet. */
219 tree low;
220 tree high;
222 /* The actual conditional we recorded. This is needed since we compute
223 ranges lazily. */
224 tree cond;
226 /* The basic block where this record was created. We use this to determine
227 when to remove records. */
228 basic_block bb;
231 /* A hash table holding value range records (VRP_ELEMENTs) for a given
232 SSA_NAME. We used to use a varray indexed by SSA_NAME_VERSION, but
233 that gets awful wasteful, particularly since the density objects
234 with useful information is very low. */
235 static htab_t vrp_data;
237 /* An entry in the VRP_DATA hash table. We record the variable and a
238 varray of VRP_ELEMENT records associated with that variable. */
240 struct vrp_hash_elt
242 tree var;
243 varray_type records;
246 /* Array of variables which have their values constrained by operations
247 in this basic block. We use this during finalization to know
248 which variables need their VRP data updated. */
250 /* Stack of SSA_NAMEs which had their values constrainted by operations
251 in this basic block. During finalization of this block we use this
252 list to determine which variables need their VRP data updated.
254 A NULL entry marks the end of the SSA_NAMEs associated with this block. */
255 static varray_type vrp_variables_stack;
257 struct eq_expr_value
259 tree src;
260 tree dst;
263 /* Local functions. */
264 static void optimize_stmt (struct dom_walk_data *,
265 basic_block bb,
266 block_stmt_iterator);
267 static tree lookup_avail_expr (tree, bool);
268 static hashval_t vrp_hash (const void *);
269 static int vrp_eq (const void *, const void *);
270 static hashval_t avail_expr_hash (const void *);
271 static hashval_t real_avail_expr_hash (const void *);
272 static int avail_expr_eq (const void *, const void *);
273 static void htab_statistics (FILE *, htab_t);
274 static void record_cond (tree, tree);
275 static void record_const_or_copy (tree, tree);
276 static void record_equality (tree, tree);
277 static tree update_rhs_and_lookup_avail_expr (tree, tree, bool);
278 static tree simplify_rhs_and_lookup_avail_expr (struct dom_walk_data *,
279 tree, int);
280 static tree simplify_cond_and_lookup_avail_expr (tree, stmt_ann_t, int);
281 static tree simplify_switch_and_lookup_avail_expr (tree, int);
282 static tree find_equivalent_equality_comparison (tree);
283 static void record_range (tree, basic_block);
284 static bool extract_range_from_cond (tree, tree *, tree *, int *);
285 static void record_equivalences_from_phis (basic_block);
286 static void record_equivalences_from_incoming_edge (basic_block);
287 static bool eliminate_redundant_computations (struct dom_walk_data *,
288 tree, stmt_ann_t);
289 static void record_equivalences_from_stmt (tree, int, stmt_ann_t);
290 static void thread_across_edge (struct dom_walk_data *, edge);
291 static void dom_opt_finalize_block (struct dom_walk_data *, basic_block);
292 static void dom_opt_initialize_block (struct dom_walk_data *, basic_block);
293 static void propagate_to_outgoing_edges (struct dom_walk_data *, basic_block);
294 static void remove_local_expressions_from_table (void);
295 static void restore_vars_to_original_value (void);
296 static void restore_currdefs_to_original_value (void);
297 static void register_definitions_for_stmt (tree);
298 static edge single_incoming_edge_ignoring_loop_edges (basic_block);
299 static void restore_nonzero_vars_to_original_value (void);
300 static inline bool unsafe_associative_fp_binop (tree);
302 /* Local version of fold that doesn't introduce cruft. */
304 static tree
305 local_fold (tree t)
307 t = fold (t);
309 /* Strip away useless type conversions. Both the NON_LVALUE_EXPR that
310 may have been added by fold, and "useless" type conversions that might
311 now be apparent due to propagation. */
312 STRIP_USELESS_TYPE_CONVERSION (t);
314 return t;
317 /* Allocate an EDGE_INFO for edge E and attach it to E.
318 Return the new EDGE_INFO structure. */
320 static struct edge_info *
321 allocate_edge_info (edge e)
323 struct edge_info *edge_info;
325 edge_info = xcalloc (1, sizeof (struct edge_info));
327 e->aux = edge_info;
328 return edge_info;
331 /* Free all EDGE_INFO structures associated with edges in the CFG.
332 If a particular edge can be threaded, copy the redirection
333 target from the EDGE_INFO structure into the edge's AUX field
334 as required by code to update the CFG and SSA graph for
335 jump threading. */
337 static void
338 free_all_edge_infos (void)
340 basic_block bb;
341 edge_iterator ei;
342 edge e;
344 FOR_EACH_BB (bb)
346 FOR_EACH_EDGE (e, ei, bb->preds)
348 struct edge_info *edge_info = e->aux;
350 if (edge_info)
352 e->aux = edge_info->redirection_target;
353 if (edge_info->cond_equivalences)
354 free (edge_info->cond_equivalences);
355 free (edge_info);
361 /* Jump threading, redundancy elimination and const/copy propagation.
363 This pass may expose new symbols that need to be renamed into SSA. For
364 every new symbol exposed, its corresponding bit will be set in
365 VARS_TO_RENAME. */
367 static void
368 tree_ssa_dominator_optimize (void)
370 struct dom_walk_data walk_data;
371 unsigned int i;
373 memset (&opt_stats, 0, sizeof (opt_stats));
375 for (i = 0; i < num_referenced_vars; i++)
376 var_ann (referenced_var (i))->current_def = NULL;
378 /* Mark loop edges so we avoid threading across loop boundaries.
379 This may result in transforming natural loop into irreducible
380 region. */
381 mark_dfs_back_edges ();
383 /* Create our hash tables. */
384 avail_exprs = htab_create (1024, real_avail_expr_hash, avail_expr_eq, free);
385 vrp_data = htab_create (ceil_log2 (num_ssa_names), vrp_hash, vrp_eq, free);
386 VARRAY_TREE_INIT (avail_exprs_stack, 20, "Available expression stack");
387 VARRAY_TREE_INIT (block_defs_stack, 20, "Block DEFS stack");
388 VARRAY_TREE_INIT (const_and_copies_stack, 20, "Block const_and_copies stack");
389 VARRAY_TREE_INIT (nonzero_vars_stack, 20, "Block nonzero_vars stack");
390 VARRAY_TREE_INIT (vrp_variables_stack, 20, "Block vrp_variables stack");
391 VARRAY_TREE_INIT (stmts_to_rescan, 20, "Statements to rescan");
392 nonzero_vars = BITMAP_XMALLOC ();
393 need_eh_cleanup = BITMAP_XMALLOC ();
395 /* Setup callbacks for the generic dominator tree walker. */
396 walk_data.walk_stmts_backward = false;
397 walk_data.dom_direction = CDI_DOMINATORS;
398 walk_data.initialize_block_local_data = NULL;
399 walk_data.before_dom_children_before_stmts = dom_opt_initialize_block;
400 walk_data.before_dom_children_walk_stmts = optimize_stmt;
401 walk_data.before_dom_children_after_stmts = propagate_to_outgoing_edges;
402 walk_data.after_dom_children_before_stmts = NULL;
403 walk_data.after_dom_children_walk_stmts = NULL;
404 walk_data.after_dom_children_after_stmts = dom_opt_finalize_block;
405 /* Right now we only attach a dummy COND_EXPR to the global data pointer.
406 When we attach more stuff we'll need to fill this out with a real
407 structure. */
408 walk_data.global_data = NULL;
409 walk_data.block_local_data_size = 0;
411 /* Now initialize the dominator walker. */
412 init_walk_dominator_tree (&walk_data);
414 calculate_dominance_info (CDI_DOMINATORS);
416 /* If we prove certain blocks are unreachable, then we want to
417 repeat the dominator optimization process as PHI nodes may
418 have turned into copies which allows better propagation of
419 values. So we repeat until we do not identify any new unreachable
420 blocks. */
423 /* Optimize the dominator tree. */
424 cfg_altered = false;
426 /* Recursively walk the dominator tree optimizing statements. */
427 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
429 /* If we exposed any new variables, go ahead and put them into
430 SSA form now, before we handle jump threading. This simplifies
431 interactions between rewriting of _DECL nodes into SSA form
432 and rewriting SSA_NAME nodes into SSA form after block
433 duplication and CFG manipulation. */
434 if (!bitmap_empty_p (vars_to_rename))
436 rewrite_into_ssa (false);
437 bitmap_clear (vars_to_rename);
440 free_all_edge_infos ();
442 /* Thread jumps, creating duplicate blocks as needed. */
443 cfg_altered = thread_through_all_blocks ();
445 /* Removal of statements may make some EH edges dead. Purge
446 such edges from the CFG as needed. */
447 if (!bitmap_empty_p (need_eh_cleanup))
449 cfg_altered |= tree_purge_all_dead_eh_edges (need_eh_cleanup);
450 bitmap_zero (need_eh_cleanup);
453 free_dominance_info (CDI_DOMINATORS);
454 cfg_altered = cleanup_tree_cfg ();
455 calculate_dominance_info (CDI_DOMINATORS);
457 rewrite_ssa_into_ssa ();
459 /* Reinitialize the various tables. */
460 bitmap_clear (nonzero_vars);
461 htab_empty (avail_exprs);
462 htab_empty (vrp_data);
464 for (i = 0; i < num_referenced_vars; i++)
465 var_ann (referenced_var (i))->current_def = NULL;
467 while (cfg_altered);
469 /* Debugging dumps. */
470 if (dump_file && (dump_flags & TDF_STATS))
471 dump_dominator_optimization_stats (dump_file);
473 /* We emptied the hash table earlier, now delete it completely. */
474 htab_delete (avail_exprs);
475 htab_delete (vrp_data);
477 /* It is not necessary to clear CURRDEFS, REDIRECTION_EDGES, VRP_DATA,
478 CONST_AND_COPIES, and NONZERO_VARS as they all get cleared at the bottom
479 of the do-while loop above. */
481 /* And finalize the dominator walker. */
482 fini_walk_dominator_tree (&walk_data);
484 /* Free nonzero_vars. */
485 BITMAP_XFREE (nonzero_vars);
486 BITMAP_XFREE (need_eh_cleanup);
488 /* Finally, remove everything except invariants in SSA_NAME_VALUE.
490 Long term we will be able to let everything in SSA_NAME_VALUE
491 persist. However, for now, we know this is the safe thing to
492 do. */
493 for (i = 0; i < num_ssa_names; i++)
495 tree name = ssa_name (i);
496 tree value;
498 if (!name)
499 continue;
501 value = SSA_NAME_VALUE (name);
502 if (value && !is_gimple_min_invariant (value))
503 SSA_NAME_VALUE (name) = NULL;
507 static bool
508 gate_dominator (void)
510 return flag_tree_dom != 0;
513 struct tree_opt_pass pass_dominator =
515 "dom", /* name */
516 gate_dominator, /* gate */
517 tree_ssa_dominator_optimize, /* execute */
518 NULL, /* sub */
519 NULL, /* next */
520 0, /* static_pass_number */
521 TV_TREE_SSA_DOMINATOR_OPTS, /* tv_id */
522 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
523 0, /* properties_provided */
524 0, /* properties_destroyed */
525 0, /* todo_flags_start */
526 TODO_dump_func | TODO_rename_vars
527 | TODO_verify_ssa, /* todo_flags_finish */
528 0 /* letter */
532 /* We are exiting BB, see if the target block begins with a conditional
533 jump which has a known value when reached via BB. */
535 static void
536 thread_across_edge (struct dom_walk_data *walk_data, edge e)
538 block_stmt_iterator bsi;
539 tree stmt = NULL;
540 tree phi;
542 /* Each PHI creates a temporary equivalence, record them. */
543 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
545 tree src = PHI_ARG_DEF_FROM_EDGE (phi, e);
546 tree dst = PHI_RESULT (phi);
547 record_const_or_copy (dst, src);
548 register_new_def (dst, &block_defs_stack);
551 for (bsi = bsi_start (e->dest); ! bsi_end_p (bsi); bsi_next (&bsi))
553 tree lhs, cached_lhs;
555 stmt = bsi_stmt (bsi);
557 /* Ignore empty statements and labels. */
558 if (IS_EMPTY_STMT (stmt) || TREE_CODE (stmt) == LABEL_EXPR)
559 continue;
561 /* If this is not a MODIFY_EXPR which sets an SSA_NAME to a new
562 value, then stop our search here. Ideally when we stop a
563 search we stop on a COND_EXPR or SWITCH_EXPR. */
564 if (TREE_CODE (stmt) != MODIFY_EXPR
565 || TREE_CODE (TREE_OPERAND (stmt, 0)) != SSA_NAME)
566 break;
568 /* At this point we have a statement which assigns an RHS to an
569 SSA_VAR on the LHS. We want to prove that the RHS is already
570 available and that its value is held in the current definition
571 of the LHS -- meaning that this assignment is a NOP when
572 reached via edge E. */
573 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == SSA_NAME)
574 cached_lhs = TREE_OPERAND (stmt, 1);
575 else
576 cached_lhs = lookup_avail_expr (stmt, false);
578 lhs = TREE_OPERAND (stmt, 0);
580 /* This can happen if we thread around to the start of a loop. */
581 if (lhs == cached_lhs)
582 break;
584 /* If we did not find RHS in the hash table, then try again after
585 temporarily const/copy propagating the operands. */
586 if (!cached_lhs)
588 /* Copy the operands. */
589 stmt_ann_t ann = stmt_ann (stmt);
590 use_optype uses = USE_OPS (ann);
591 vuse_optype vuses = VUSE_OPS (ann);
592 tree *uses_copy = xcalloc (NUM_USES (uses), sizeof (tree));
593 tree *vuses_copy = xcalloc (NUM_VUSES (vuses), sizeof (tree));
594 unsigned int i;
596 /* Make a copy of the uses into USES_COPY, then cprop into
597 the use operands. */
598 for (i = 0; i < NUM_USES (uses); i++)
600 tree tmp = NULL;
602 uses_copy[i] = USE_OP (uses, i);
603 if (TREE_CODE (USE_OP (uses, i)) == SSA_NAME)
604 tmp = SSA_NAME_VALUE (USE_OP (uses, i));
605 if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
606 SET_USE_OP (uses, i, tmp);
609 /* Similarly for virtual uses. */
610 for (i = 0; i < NUM_VUSES (vuses); i++)
612 tree tmp = NULL;
614 vuses_copy[i] = VUSE_OP (vuses, i);
615 if (TREE_CODE (VUSE_OP (vuses, i)) == SSA_NAME)
616 tmp = SSA_NAME_VALUE (VUSE_OP (vuses, i));
617 if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
618 SET_VUSE_OP (vuses, i, tmp);
621 /* Try to lookup the new expression. */
622 cached_lhs = lookup_avail_expr (stmt, false);
624 /* Restore the statement's original uses/defs. */
625 for (i = 0; i < NUM_USES (uses); i++)
626 SET_USE_OP (uses, i, uses_copy[i]);
628 for (i = 0; i < NUM_VUSES (vuses); i++)
629 SET_VUSE_OP (vuses, i, vuses_copy[i]);
631 free (uses_copy);
632 free (vuses_copy);
634 /* If we still did not find the expression in the hash table,
635 then we can not ignore this statement. */
636 if (! cached_lhs)
637 break;
640 /* If the expression in the hash table was not assigned to an
641 SSA_NAME, then we can not ignore this statement. */
642 if (TREE_CODE (cached_lhs) != SSA_NAME)
643 break;
645 /* If we have different underlying variables, then we can not
646 ignore this statement. */
647 if (SSA_NAME_VAR (cached_lhs) != SSA_NAME_VAR (lhs))
648 break;
650 /* If CACHED_LHS does not represent the current value of the undering
651 variable in CACHED_LHS/LHS, then we can not ignore this statement. */
652 if (var_ann (SSA_NAME_VAR (lhs))->current_def != cached_lhs)
653 break;
655 /* If we got here, then we can ignore this statement and continue
656 walking through the statements in the block looking for a threadable
657 COND_EXPR.
659 We want to record an equivalence lhs = cache_lhs so that if
660 the result of this statement is used later we can copy propagate
661 suitably. */
662 record_const_or_copy (lhs, cached_lhs);
663 register_new_def (lhs, &block_defs_stack);
666 /* If we stopped at a COND_EXPR or SWITCH_EXPR, then see if we know which
667 arm will be taken. */
668 if (stmt
669 && (TREE_CODE (stmt) == COND_EXPR
670 || TREE_CODE (stmt) == SWITCH_EXPR))
672 tree cond, cached_lhs;
673 edge e1;
674 edge_iterator ei;
676 /* Do not forward entry edges into the loop. In the case loop
677 has multiple entry edges we may end up in constructing irreducible
678 region.
679 ??? We may consider forwarding the edges in the case all incoming
680 edges forward to the same destination block. */
681 if (!e->flags & EDGE_DFS_BACK)
683 FOR_EACH_EDGE (e1, ei, e->dest->preds)
684 if (e1->flags & EDGE_DFS_BACK)
685 break;
686 if (e1)
687 return;
690 /* Now temporarily cprop the operands and try to find the resulting
691 expression in the hash tables. */
692 if (TREE_CODE (stmt) == COND_EXPR)
693 cond = COND_EXPR_COND (stmt);
694 else
695 cond = SWITCH_COND (stmt);
697 if (COMPARISON_CLASS_P (cond))
699 tree dummy_cond, op0, op1;
700 enum tree_code cond_code;
702 op0 = TREE_OPERAND (cond, 0);
703 op1 = TREE_OPERAND (cond, 1);
704 cond_code = TREE_CODE (cond);
706 /* Get the current value of both operands. */
707 if (TREE_CODE (op0) == SSA_NAME)
709 tree tmp = SSA_NAME_VALUE (op0);
710 if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
711 op0 = tmp;
714 if (TREE_CODE (op1) == SSA_NAME)
716 tree tmp = SSA_NAME_VALUE (op1);
717 if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
718 op1 = tmp;
721 /* Stuff the operator and operands into our dummy conditional
722 expression, creating the dummy conditional if necessary. */
723 dummy_cond = walk_data->global_data;
724 if (! dummy_cond)
726 dummy_cond = build (cond_code, boolean_type_node, op0, op1);
727 dummy_cond = build (COND_EXPR, void_type_node,
728 dummy_cond, NULL, NULL);
729 walk_data->global_data = dummy_cond;
731 else
733 TREE_SET_CODE (TREE_OPERAND (dummy_cond, 0), cond_code);
734 TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 0) = op0;
735 TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 1) = op1;
738 /* If the conditional folds to an invariant, then we are done,
739 otherwise look it up in the hash tables. */
740 cached_lhs = local_fold (COND_EXPR_COND (dummy_cond));
741 if (! is_gimple_min_invariant (cached_lhs))
742 cached_lhs = lookup_avail_expr (dummy_cond, false);
743 if (!cached_lhs || ! is_gimple_min_invariant (cached_lhs))
745 cached_lhs = simplify_cond_and_lookup_avail_expr (dummy_cond,
746 NULL,
747 false);
750 /* We can have conditionals which just test the state of a
751 variable rather than use a relational operator. These are
752 simpler to handle. */
753 else if (TREE_CODE (cond) == SSA_NAME)
755 cached_lhs = cond;
756 cached_lhs = SSA_NAME_VALUE (cached_lhs);
757 if (cached_lhs && ! is_gimple_min_invariant (cached_lhs))
758 cached_lhs = 0;
760 else
761 cached_lhs = lookup_avail_expr (stmt, false);
763 if (cached_lhs)
765 edge taken_edge = find_taken_edge (e->dest, cached_lhs);
766 basic_block dest = (taken_edge ? taken_edge->dest : NULL);
768 if (dest == e->dest)
769 return;
771 /* If we have a known destination for the conditional, then
772 we can perform this optimization, which saves at least one
773 conditional jump each time it applies since we get to
774 bypass the conditional at our original destination. */
775 if (dest)
777 struct edge_info *edge_info;
779 update_bb_profile_for_threading (e->dest, EDGE_FREQUENCY (e),
780 e->count, taken_edge);
781 if (e->aux)
782 edge_info = e->aux;
783 else
784 edge_info = allocate_edge_info (e);
785 edge_info->redirection_target = taken_edge;
786 bb_ann (e->dest)->incoming_edge_threaded = true;
793 /* Initialize local stacks for this optimizer and record equivalences
794 upon entry to BB. Equivalences can come from the edge traversed to
795 reach BB or they may come from PHI nodes at the start of BB. */
797 static void
798 dom_opt_initialize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
799 basic_block bb)
801 if (dump_file && (dump_flags & TDF_DETAILS))
802 fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
804 /* Push a marker on the stacks of local information so that we know how
805 far to unwind when we finalize this block. */
806 VARRAY_PUSH_TREE (avail_exprs_stack, NULL_TREE);
807 VARRAY_PUSH_TREE (block_defs_stack, NULL_TREE);
808 VARRAY_PUSH_TREE (const_and_copies_stack, NULL_TREE);
809 VARRAY_PUSH_TREE (nonzero_vars_stack, NULL_TREE);
810 VARRAY_PUSH_TREE (vrp_variables_stack, NULL_TREE);
812 record_equivalences_from_incoming_edge (bb);
814 /* PHI nodes can create equivalences too. */
815 record_equivalences_from_phis (bb);
818 /* Given an expression EXPR (a relational expression or a statement),
819 initialize the hash table element pointed by by ELEMENT. */
821 static void
822 initialize_hash_element (tree expr, tree lhs, struct expr_hash_elt *element)
824 /* Hash table elements may be based on conditional expressions or statements.
826 For the former case, we have no annotation and we want to hash the
827 conditional expression. In the latter case we have an annotation and
828 we want to record the expression the statement evaluates. */
829 if (COMPARISON_CLASS_P (expr) || TREE_CODE (expr) == TRUTH_NOT_EXPR)
831 element->ann = NULL;
832 element->rhs = expr;
834 else if (TREE_CODE (expr) == COND_EXPR)
836 element->ann = stmt_ann (expr);
837 element->rhs = COND_EXPR_COND (expr);
839 else if (TREE_CODE (expr) == SWITCH_EXPR)
841 element->ann = stmt_ann (expr);
842 element->rhs = SWITCH_COND (expr);
844 else if (TREE_CODE (expr) == RETURN_EXPR && TREE_OPERAND (expr, 0))
846 element->ann = stmt_ann (expr);
847 element->rhs = TREE_OPERAND (TREE_OPERAND (expr, 0), 1);
849 else
851 element->ann = stmt_ann (expr);
852 element->rhs = TREE_OPERAND (expr, 1);
855 element->lhs = lhs;
856 element->hash = avail_expr_hash (element);
859 /* Remove all the expressions in LOCALS from TABLE, stopping when there are
860 LIMIT entries left in LOCALs. */
862 static void
863 remove_local_expressions_from_table (void)
865 /* Remove all the expressions made available in this block. */
866 while (VARRAY_ACTIVE_SIZE (avail_exprs_stack) > 0)
868 struct expr_hash_elt element;
869 tree expr = VARRAY_TOP_TREE (avail_exprs_stack);
870 VARRAY_POP (avail_exprs_stack);
872 if (expr == NULL_TREE)
873 break;
875 initialize_hash_element (expr, NULL, &element);
876 htab_remove_elt_with_hash (avail_exprs, &element, element.hash);
880 /* Use the SSA_NAMES in LOCALS to restore TABLE to its original
881 state, stopping when there are LIMIT entries left in LOCALs. */
883 static void
884 restore_nonzero_vars_to_original_value (void)
886 while (VARRAY_ACTIVE_SIZE (nonzero_vars_stack) > 0)
888 tree name = VARRAY_TOP_TREE (nonzero_vars_stack);
889 VARRAY_POP (nonzero_vars_stack);
891 if (name == NULL)
892 break;
894 bitmap_clear_bit (nonzero_vars, SSA_NAME_VERSION (name));
898 /* Use the source/dest pairs in CONST_AND_COPIES_STACK to restore
899 CONST_AND_COPIES to its original state, stopping when we hit a
900 NULL marker. */
902 static void
903 restore_vars_to_original_value (void)
905 while (VARRAY_ACTIVE_SIZE (const_and_copies_stack) > 0)
907 tree prev_value, dest;
909 dest = VARRAY_TOP_TREE (const_and_copies_stack);
910 VARRAY_POP (const_and_copies_stack);
912 if (dest == NULL)
913 break;
915 prev_value = VARRAY_TOP_TREE (const_and_copies_stack);
916 VARRAY_POP (const_and_copies_stack);
918 SSA_NAME_VALUE (dest) = prev_value;
922 /* Similar to restore_vars_to_original_value, except that it restores
923 CURRDEFS to its original value. */
924 static void
925 restore_currdefs_to_original_value (void)
927 /* Restore CURRDEFS to its original state. */
928 while (VARRAY_ACTIVE_SIZE (block_defs_stack) > 0)
930 tree tmp = VARRAY_TOP_TREE (block_defs_stack);
931 tree saved_def, var;
933 VARRAY_POP (block_defs_stack);
935 if (tmp == NULL_TREE)
936 break;
938 /* If we recorded an SSA_NAME, then make the SSA_NAME the current
939 definition of its underlying variable. If we recorded anything
940 else, it must have been an _DECL node and its current reaching
941 definition must have been NULL. */
942 if (TREE_CODE (tmp) == SSA_NAME)
944 saved_def = tmp;
945 var = SSA_NAME_VAR (saved_def);
947 else
949 saved_def = NULL;
950 var = tmp;
953 var_ann (var)->current_def = saved_def;
957 /* We have finished processing the dominator children of BB, perform
958 any finalization actions in preparation for leaving this node in
959 the dominator tree. */
961 static void
962 dom_opt_finalize_block (struct dom_walk_data *walk_data, basic_block bb)
964 tree last;
966 /* If we are at a leaf node in the dominator graph, see if we can thread
967 the edge from BB through its successor.
969 Do this before we remove entries from our equivalence tables. */
970 if (EDGE_COUNT (bb->succs) == 1
971 && (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL) == 0
972 && (get_immediate_dominator (CDI_DOMINATORS, EDGE_SUCC (bb, 0)->dest) != bb
973 || phi_nodes (EDGE_SUCC (bb, 0)->dest)))
976 thread_across_edge (walk_data, EDGE_SUCC (bb, 0));
978 else if ((last = last_stmt (bb))
979 && TREE_CODE (last) == COND_EXPR
980 && (COMPARISON_CLASS_P (COND_EXPR_COND (last))
981 || TREE_CODE (COND_EXPR_COND (last)) == SSA_NAME)
982 && EDGE_COUNT (bb->succs) == 2
983 && (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL) == 0
984 && (EDGE_SUCC (bb, 1)->flags & EDGE_ABNORMAL) == 0)
986 edge true_edge, false_edge;
988 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
990 /* If the THEN arm is the end of a dominator tree or has PHI nodes,
991 then try to thread through its edge. */
992 if (get_immediate_dominator (CDI_DOMINATORS, true_edge->dest) != bb
993 || phi_nodes (true_edge->dest))
995 struct edge_info *edge_info;
996 unsigned int i;
998 /* Push a marker onto the available expression stack so that we
999 unwind any expressions related to the TRUE arm before processing
1000 the false arm below. */
1001 VARRAY_PUSH_TREE (avail_exprs_stack, NULL_TREE);
1002 VARRAY_PUSH_TREE (block_defs_stack, NULL_TREE);
1003 VARRAY_PUSH_TREE (const_and_copies_stack, NULL_TREE);
1005 edge_info = true_edge->aux;
1007 /* If we have info associated with this edge, record it into
1008 our equivalency tables. */
1009 if (edge_info)
1011 tree *cond_equivalences = edge_info->cond_equivalences;
1012 tree lhs = edge_info->lhs;
1013 tree rhs = edge_info->rhs;
1015 /* If we have a simple NAME = VALUE equivalency record it.
1016 Until the jump threading selection code improves, only
1017 do this if both the name and value are SSA_NAMEs with
1018 the same underlying variable to avoid missing threading
1019 opportunities. */
1020 if (lhs
1021 && TREE_CODE (COND_EXPR_COND (last)) == SSA_NAME
1022 && TREE_CODE (edge_info->rhs) == SSA_NAME
1023 && SSA_NAME_VAR (lhs) == SSA_NAME_VAR (rhs))
1024 record_const_or_copy (lhs, rhs);
1026 /* If we have 0 = COND or 1 = COND equivalences, record them
1027 into our expression hash tables. */
1028 if (cond_equivalences)
1029 for (i = 0; i < edge_info->max_cond_equivalences; i += 2)
1031 tree expr = cond_equivalences[i];
1032 tree value = cond_equivalences[i + 1];
1034 record_cond (expr, value);
1038 /* Now thread the edge. */
1039 thread_across_edge (walk_data, true_edge);
1041 /* And restore the various tables to their state before
1042 we threaded this edge. */
1043 remove_local_expressions_from_table ();
1044 restore_vars_to_original_value ();
1045 restore_currdefs_to_original_value ();
1048 /* Similarly for the ELSE arm. */
1049 if (get_immediate_dominator (CDI_DOMINATORS, false_edge->dest) != bb
1050 || phi_nodes (false_edge->dest))
1052 struct edge_info *edge_info;
1053 unsigned int i;
1055 edge_info = false_edge->aux;
1057 /* If we have info associated with this edge, record it into
1058 our equivalency tables. */
1059 if (edge_info)
1061 tree *cond_equivalences = edge_info->cond_equivalences;
1062 tree lhs = edge_info->lhs;
1063 tree rhs = edge_info->rhs;
1065 /* If we have a simple NAME = VALUE equivalency record it.
1066 Until the jump threading selection code improves, only
1067 do this if both the name and value are SSA_NAMEs with
1068 the same underlying variable to avoid missing threading
1069 opportunities. */
1070 if (lhs
1071 && TREE_CODE (COND_EXPR_COND (last)) == SSA_NAME)
1072 record_const_or_copy (lhs, rhs);
1074 /* If we have 0 = COND or 1 = COND equivalences, record them
1075 into our expression hash tables. */
1076 if (cond_equivalences)
1077 for (i = 0; i < edge_info->max_cond_equivalences; i += 2)
1079 tree expr = cond_equivalences[i];
1080 tree value = cond_equivalences[i + 1];
1082 record_cond (expr, value);
1086 thread_across_edge (walk_data, false_edge);
1088 /* No need to remove local expressions from our tables
1089 or restore vars to their original value as that will
1090 be done immediately below. */
1094 remove_local_expressions_from_table ();
1095 restore_nonzero_vars_to_original_value ();
1096 restore_vars_to_original_value ();
1097 restore_currdefs_to_original_value ();
1099 /* Remove VRP records associated with this basic block. They are no
1100 longer valid.
1102 To be efficient, we note which variables have had their values
1103 constrained in this block. So walk over each variable in the
1104 VRP_VARIABLEs array. */
1105 while (VARRAY_ACTIVE_SIZE (vrp_variables_stack) > 0)
1107 tree var = VARRAY_TOP_TREE (vrp_variables_stack);
1108 struct vrp_hash_elt vrp_hash_elt, *vrp_hash_elt_p;
1109 void **slot;
1111 /* Each variable has a stack of value range records. We want to
1112 invalidate those associated with our basic block. So we walk
1113 the array backwards popping off records associated with our
1114 block. Once we hit a record not associated with our block
1115 we are done. */
1116 varray_type var_vrp_records;
1118 VARRAY_POP (vrp_variables_stack);
1120 if (var == NULL)
1121 break;
1123 vrp_hash_elt.var = var;
1124 vrp_hash_elt.records = NULL;
1126 slot = htab_find_slot (vrp_data, &vrp_hash_elt, NO_INSERT);
1128 vrp_hash_elt_p = (struct vrp_hash_elt *) *slot;
1129 var_vrp_records = vrp_hash_elt_p->records;
1131 while (VARRAY_ACTIVE_SIZE (var_vrp_records) > 0)
1133 struct vrp_element *element
1134 = (struct vrp_element *)VARRAY_TOP_GENERIC_PTR (var_vrp_records);
1136 if (element->bb != bb)
1137 break;
1139 VARRAY_POP (var_vrp_records);
1143 /* If we queued any statements to rescan in this block, then
1144 go ahead and rescan them now. */
1145 while (VARRAY_ACTIVE_SIZE (stmts_to_rescan) > 0)
1147 tree stmt = VARRAY_TOP_TREE (stmts_to_rescan);
1148 basic_block stmt_bb = bb_for_stmt (stmt);
1150 if (stmt_bb != bb)
1151 break;
1153 VARRAY_POP (stmts_to_rescan);
1154 mark_new_vars_to_rename (stmt, vars_to_rename);
1158 /* PHI nodes can create equivalences too.
1160 Ignoring any alternatives which are the same as the result, if
1161 all the alternatives are equal, then the PHI node creates an
1162 equivalence.
1164 Additionally, if all the PHI alternatives are known to have a nonzero
1165 value, then the result of this PHI is known to have a nonzero value,
1166 even if we do not know its exact value. */
1168 static void
1169 record_equivalences_from_phis (basic_block bb)
1171 tree phi;
1173 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1175 tree lhs = PHI_RESULT (phi);
1176 tree rhs = NULL;
1177 int i;
1179 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1181 tree t = PHI_ARG_DEF (phi, i);
1183 if (TREE_CODE (t) == SSA_NAME || is_gimple_min_invariant (t))
1185 /* Ignore alternatives which are the same as our LHS. */
1186 if (operand_equal_p (lhs, t, 0))
1187 continue;
1189 /* If we have not processed an alternative yet, then set
1190 RHS to this alternative. */
1191 if (rhs == NULL)
1192 rhs = t;
1193 /* If we have processed an alternative (stored in RHS), then
1194 see if it is equal to this one. If it isn't, then stop
1195 the search. */
1196 else if (! operand_equal_p (rhs, t, 0))
1197 break;
1199 else
1200 break;
1203 /* If we had no interesting alternatives, then all the RHS alternatives
1204 must have been the same as LHS. */
1205 if (!rhs)
1206 rhs = lhs;
1208 /* If we managed to iterate through each PHI alternative without
1209 breaking out of the loop, then we have a PHI which may create
1210 a useful equivalence. We do not need to record unwind data for
1211 this, since this is a true assignment and not an equivalence
1212 inferred from a comparison. All uses of this ssa name are dominated
1213 by this assignment, so unwinding just costs time and space. */
1214 if (i == PHI_NUM_ARGS (phi)
1215 && may_propagate_copy (lhs, rhs))
1216 SSA_NAME_VALUE (lhs) = rhs;
1218 /* Now see if we know anything about the nonzero property for the
1219 result of this PHI. */
1220 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1222 if (!PHI_ARG_NONZERO (phi, i))
1223 break;
1226 if (i == PHI_NUM_ARGS (phi))
1227 bitmap_set_bit (nonzero_vars, SSA_NAME_VERSION (PHI_RESULT (phi)));
1229 register_new_def (lhs, &block_defs_stack);
1233 /* Ignoring loop backedges, if BB has precisely one incoming edge then
1234 return that edge. Otherwise return NULL. */
1235 static edge
1236 single_incoming_edge_ignoring_loop_edges (basic_block bb)
1238 edge retval = NULL;
1239 edge e;
1240 edge_iterator ei;
1242 FOR_EACH_EDGE (e, ei, bb->preds)
1244 /* A loop back edge can be identified by the destination of
1245 the edge dominating the source of the edge. */
1246 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
1247 continue;
1249 /* If we have already seen a non-loop edge, then we must have
1250 multiple incoming non-loop edges and thus we return NULL. */
1251 if (retval)
1252 return NULL;
1254 /* This is the first non-loop incoming edge we have found. Record
1255 it. */
1256 retval = e;
1259 return retval;
1262 /* Record any equivalences created by the incoming edge to BB. If BB
1263 has more than one incoming edge, then no equivalence is created. */
1265 static void
1266 record_equivalences_from_incoming_edge (basic_block bb)
1268 edge e;
1269 basic_block parent;
1270 struct edge_info *edge_info;
1272 /* If our parent block ended with a control statment, then we may be
1273 able to record some equivalences based on which outgoing edge from
1274 the parent was followed. */
1275 parent = get_immediate_dominator (CDI_DOMINATORS, bb);
1277 e = single_incoming_edge_ignoring_loop_edges (bb);
1279 /* If we had a single incoming edge from our parent block, then enter
1280 any data associated with the edge into our tables. */
1281 if (e && e->src == parent)
1283 unsigned int i;
1285 edge_info = e->aux;
1287 if (edge_info)
1289 tree lhs = edge_info->lhs;
1290 tree rhs = edge_info->rhs;
1291 tree *cond_equivalences = edge_info->cond_equivalences;
1293 if (lhs)
1294 record_equality (lhs, rhs);
1296 if (cond_equivalences)
1298 bool recorded_range = false;
1299 for (i = 0; i < edge_info->max_cond_equivalences; i += 2)
1301 tree expr = cond_equivalences[i];
1302 tree value = cond_equivalences[i + 1];
1304 record_cond (expr, value);
1306 /* For the first true equivalence, record range
1307 information. We only do this for the first
1308 true equivalence as it should dominate any
1309 later true equivalences. */
1310 if (! recorded_range
1311 && COMPARISON_CLASS_P (expr)
1312 && value == boolean_true_node
1313 && TREE_CONSTANT (TREE_OPERAND (expr, 1)))
1315 record_range (expr, bb);
1316 recorded_range = true;
1324 /* Dump SSA statistics on FILE. */
1326 void
1327 dump_dominator_optimization_stats (FILE *file)
1329 long n_exprs;
1331 fprintf (file, "Total number of statements: %6ld\n\n",
1332 opt_stats.num_stmts);
1333 fprintf (file, "Exprs considered for dominator optimizations: %6ld\n",
1334 opt_stats.num_exprs_considered);
1336 n_exprs = opt_stats.num_exprs_considered;
1337 if (n_exprs == 0)
1338 n_exprs = 1;
1340 fprintf (file, " Redundant expressions eliminated: %6ld (%.0f%%)\n",
1341 opt_stats.num_re, PERCENT (opt_stats.num_re,
1342 n_exprs));
1344 fprintf (file, "\nHash table statistics:\n");
1346 fprintf (file, " avail_exprs: ");
1347 htab_statistics (file, avail_exprs);
1351 /* Dump SSA statistics on stderr. */
1353 void
1354 debug_dominator_optimization_stats (void)
1356 dump_dominator_optimization_stats (stderr);
1360 /* Dump statistics for the hash table HTAB. */
1362 static void
1363 htab_statistics (FILE *file, htab_t htab)
1365 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
1366 (long) htab_size (htab),
1367 (long) htab_elements (htab),
1368 htab_collisions (htab));
1371 /* Record the fact that VAR has a nonzero value, though we may not know
1372 its exact value. Note that if VAR is already known to have a nonzero
1373 value, then we do nothing. */
1375 static void
1376 record_var_is_nonzero (tree var)
1378 int indx = SSA_NAME_VERSION (var);
1380 if (bitmap_bit_p (nonzero_vars, indx))
1381 return;
1383 /* Mark it in the global table. */
1384 bitmap_set_bit (nonzero_vars, indx);
1386 /* Record this SSA_NAME so that we can reset the global table
1387 when we leave this block. */
1388 VARRAY_PUSH_TREE (nonzero_vars_stack, var);
1391 /* Enter a statement into the true/false expression hash table indicating
1392 that the condition COND has the value VALUE. */
1394 static void
1395 record_cond (tree cond, tree value)
1397 struct expr_hash_elt *element = xmalloc (sizeof (struct expr_hash_elt));
1398 void **slot;
1400 initialize_hash_element (cond, value, element);
1402 slot = htab_find_slot_with_hash (avail_exprs, (void *)element,
1403 element->hash, true);
1404 if (*slot == NULL)
1406 *slot = (void *) element;
1407 VARRAY_PUSH_TREE (avail_exprs_stack, cond);
1409 else
1410 free (element);
1413 /* Build a new conditional using NEW_CODE, OP0 and OP1 and store
1414 the new conditional into *p, then store a boolean_true_node
1415 into the the *(p + 1). */
1417 static void
1418 build_and_record_new_cond (enum tree_code new_code, tree op0, tree op1, tree *p)
1420 *p = build2 (new_code, boolean_type_node, op0, op1);
1421 p++;
1422 *p = boolean_true_node;
1425 /* Record that COND is true and INVERTED is false into the edge information
1426 structure. Also record that any conditions dominated by COND are true
1427 as well.
1429 For example, if a < b is true, then a <= b must also be true. */
1431 static void
1432 record_conditions (struct edge_info *edge_info, tree cond, tree inverted)
1434 tree op0, op1;
1436 if (!COMPARISON_CLASS_P (cond))
1437 return;
1439 op0 = TREE_OPERAND (cond, 0);
1440 op1 = TREE_OPERAND (cond, 1);
1442 switch (TREE_CODE (cond))
1444 case LT_EXPR:
1445 case GT_EXPR:
1446 edge_info->max_cond_equivalences = 12;
1447 edge_info->cond_equivalences = xmalloc (12 * sizeof (tree));
1448 build_and_record_new_cond ((TREE_CODE (cond) == LT_EXPR
1449 ? LE_EXPR : GE_EXPR),
1450 op0, op1, &edge_info->cond_equivalences[4]);
1451 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1452 &edge_info->cond_equivalences[6]);
1453 build_and_record_new_cond (NE_EXPR, op0, op1,
1454 &edge_info->cond_equivalences[8]);
1455 build_and_record_new_cond (LTGT_EXPR, op0, op1,
1456 &edge_info->cond_equivalences[10]);
1457 break;
1459 case GE_EXPR:
1460 case LE_EXPR:
1461 edge_info->max_cond_equivalences = 6;
1462 edge_info->cond_equivalences = xmalloc (6 * sizeof (tree));
1463 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1464 &edge_info->cond_equivalences[4]);
1465 break;
1467 case EQ_EXPR:
1468 edge_info->max_cond_equivalences = 10;
1469 edge_info->cond_equivalences = xmalloc (10 * sizeof (tree));
1470 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1471 &edge_info->cond_equivalences[4]);
1472 build_and_record_new_cond (LE_EXPR, op0, op1,
1473 &edge_info->cond_equivalences[6]);
1474 build_and_record_new_cond (GE_EXPR, op0, op1,
1475 &edge_info->cond_equivalences[8]);
1476 break;
1478 case UNORDERED_EXPR:
1479 edge_info->max_cond_equivalences = 16;
1480 edge_info->cond_equivalences = xmalloc (16 * sizeof (tree));
1481 build_and_record_new_cond (NE_EXPR, op0, op1,
1482 &edge_info->cond_equivalences[4]);
1483 build_and_record_new_cond (UNLE_EXPR, op0, op1,
1484 &edge_info->cond_equivalences[6]);
1485 build_and_record_new_cond (UNGE_EXPR, op0, op1,
1486 &edge_info->cond_equivalences[8]);
1487 build_and_record_new_cond (UNEQ_EXPR, op0, op1,
1488 &edge_info->cond_equivalences[10]);
1489 build_and_record_new_cond (UNLT_EXPR, op0, op1,
1490 &edge_info->cond_equivalences[12]);
1491 build_and_record_new_cond (UNGT_EXPR, op0, op1,
1492 &edge_info->cond_equivalences[14]);
1493 break;
1495 case UNLT_EXPR:
1496 case UNGT_EXPR:
1497 edge_info->max_cond_equivalences = 8;
1498 edge_info->cond_equivalences = xmalloc (8 * sizeof (tree));
1499 build_and_record_new_cond ((TREE_CODE (cond) == UNLT_EXPR
1500 ? UNLE_EXPR : UNGE_EXPR),
1501 op0, op1, &edge_info->cond_equivalences[4]);
1502 build_and_record_new_cond (NE_EXPR, op0, op1,
1503 &edge_info->cond_equivalences[6]);
1504 break;
1506 case UNEQ_EXPR:
1507 edge_info->max_cond_equivalences = 8;
1508 edge_info->cond_equivalences = xmalloc (8 * sizeof (tree));
1509 build_and_record_new_cond (UNLE_EXPR, op0, op1,
1510 &edge_info->cond_equivalences[4]);
1511 build_and_record_new_cond (UNGE_EXPR, op0, op1,
1512 &edge_info->cond_equivalences[6]);
1513 break;
1515 case LTGT_EXPR:
1516 edge_info->max_cond_equivalences = 8;
1517 edge_info->cond_equivalences = xmalloc (8 * sizeof (tree));
1518 build_and_record_new_cond (NE_EXPR, op0, op1,
1519 &edge_info->cond_equivalences[4]);
1520 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1521 &edge_info->cond_equivalences[6]);
1522 break;
1524 default:
1525 edge_info->max_cond_equivalences = 4;
1526 edge_info->cond_equivalences = xmalloc (4 * sizeof (tree));
1527 break;
1530 /* Now store the original true and false conditions into the first
1531 two slots. */
1532 edge_info->cond_equivalences[0] = cond;
1533 edge_info->cond_equivalences[1] = boolean_true_node;
1534 edge_info->cond_equivalences[2] = inverted;
1535 edge_info->cond_equivalences[3] = boolean_false_node;
1538 /* A helper function for record_const_or_copy and record_equality.
1539 Do the work of recording the value and undo info. */
1541 static void
1542 record_const_or_copy_1 (tree x, tree y, tree prev_x)
1544 SSA_NAME_VALUE (x) = y;
1546 VARRAY_PUSH_TREE (const_and_copies_stack, prev_x);
1547 VARRAY_PUSH_TREE (const_and_copies_stack, x);
1551 /* Return the loop depth of the basic block of the defining statement of X.
1552 This number should not be treated as absolutely correct because the loop
1553 information may not be completely up-to-date when dom runs. However, it
1554 will be relatively correct, and as more passes are taught to keep loop info
1555 up to date, the result will become more and more accurate. */
1557 static int
1558 loop_depth_of_name (tree x)
1560 tree defstmt;
1561 basic_block defbb;
1563 /* If it's not an SSA_NAME, we have no clue where the definition is. */
1564 if (TREE_CODE (x) != SSA_NAME)
1565 return 0;
1567 /* Otherwise return the loop depth of the defining statement's bb.
1568 Note that there may not actually be a bb for this statement, if the
1569 ssa_name is live on entry. */
1570 defstmt = SSA_NAME_DEF_STMT (x);
1571 defbb = bb_for_stmt (defstmt);
1572 if (!defbb)
1573 return 0;
1575 return defbb->loop_depth;
1579 /* Record that X is equal to Y in const_and_copies. Record undo
1580 information in the block-local varray. */
1582 static void
1583 record_const_or_copy (tree x, tree y)
1585 tree prev_x = SSA_NAME_VALUE (x);
1587 if (TREE_CODE (y) == SSA_NAME)
1589 tree tmp = SSA_NAME_VALUE (y);
1590 if (tmp)
1591 y = tmp;
1594 record_const_or_copy_1 (x, y, prev_x);
1597 /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1598 This constrains the cases in which we may treat this as assignment. */
1600 static void
1601 record_equality (tree x, tree y)
1603 tree prev_x = NULL, prev_y = NULL;
1605 if (TREE_CODE (x) == SSA_NAME)
1606 prev_x = SSA_NAME_VALUE (x);
1607 if (TREE_CODE (y) == SSA_NAME)
1608 prev_y = SSA_NAME_VALUE (y);
1610 /* If one of the previous values is invariant, or invariant in more loops
1611 (by depth), then use that.
1612 Otherwise it doesn't matter which value we choose, just so
1613 long as we canonicalize on one value. */
1614 if (TREE_INVARIANT (y))
1616 else if (TREE_INVARIANT (x) || (loop_depth_of_name (x) <= loop_depth_of_name (y)))
1617 prev_x = x, x = y, y = prev_x, prev_x = prev_y;
1618 else if (prev_x && TREE_INVARIANT (prev_x))
1619 x = y, y = prev_x, prev_x = prev_y;
1620 else if (prev_y && TREE_CODE (prev_y) != VALUE_HANDLE)
1621 y = prev_y;
1623 /* After the swapping, we must have one SSA_NAME. */
1624 if (TREE_CODE (x) != SSA_NAME)
1625 return;
1627 /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1628 variable compared against zero. If we're honoring signed zeros,
1629 then we cannot record this value unless we know that the value is
1630 nonzero. */
1631 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (x)))
1632 && (TREE_CODE (y) != REAL_CST
1633 || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (y))))
1634 return;
1636 record_const_or_copy_1 (x, y, prev_x);
1639 /* Return true, if it is ok to do folding of an associative expression.
1640 EXP is the tree for the associative expression. */
1642 static inline bool
1643 unsafe_associative_fp_binop (tree exp)
1645 enum tree_code code = TREE_CODE (exp);
1646 return !(!flag_unsafe_math_optimizations
1647 && (code == MULT_EXPR || code == PLUS_EXPR)
1648 && FLOAT_TYPE_P (TREE_TYPE (exp)));
1651 /* STMT is a MODIFY_EXPR for which we were unable to find RHS in the
1652 hash tables. Try to simplify the RHS using whatever equivalences
1653 we may have recorded.
1655 If we are able to simplify the RHS, then lookup the simplified form in
1656 the hash table and return the result. Otherwise return NULL. */
1658 static tree
1659 simplify_rhs_and_lookup_avail_expr (struct dom_walk_data *walk_data,
1660 tree stmt, int insert)
1662 tree rhs = TREE_OPERAND (stmt, 1);
1663 enum tree_code rhs_code = TREE_CODE (rhs);
1664 tree result = NULL;
1666 /* If we have lhs = ~x, look and see if we earlier had x = ~y.
1667 In which case we can change this statement to be lhs = y.
1668 Which can then be copy propagated.
1670 Similarly for negation. */
1671 if ((rhs_code == BIT_NOT_EXPR || rhs_code == NEGATE_EXPR)
1672 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
1674 /* Get the definition statement for our RHS. */
1675 tree rhs_def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs, 0));
1677 /* See if the RHS_DEF_STMT has the same form as our statement. */
1678 if (TREE_CODE (rhs_def_stmt) == MODIFY_EXPR
1679 && TREE_CODE (TREE_OPERAND (rhs_def_stmt, 1)) == rhs_code)
1681 tree rhs_def_operand;
1683 rhs_def_operand = TREE_OPERAND (TREE_OPERAND (rhs_def_stmt, 1), 0);
1685 /* Verify that RHS_DEF_OPERAND is a suitable SSA variable. */
1686 if (TREE_CODE (rhs_def_operand) == SSA_NAME
1687 && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs_def_operand))
1688 result = update_rhs_and_lookup_avail_expr (stmt,
1689 rhs_def_operand,
1690 insert);
1694 /* If we have z = (x OP C1), see if we earlier had x = y OP C2.
1695 If OP is associative, create and fold (y OP C2) OP C1 which
1696 should result in (y OP C3), use that as the RHS for the
1697 assignment. Add minus to this, as we handle it specially below. */
1698 if ((associative_tree_code (rhs_code) || rhs_code == MINUS_EXPR)
1699 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME
1700 && is_gimple_min_invariant (TREE_OPERAND (rhs, 1)))
1702 tree rhs_def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs, 0));
1704 /* See if the RHS_DEF_STMT has the same form as our statement. */
1705 if (TREE_CODE (rhs_def_stmt) == MODIFY_EXPR)
1707 tree rhs_def_rhs = TREE_OPERAND (rhs_def_stmt, 1);
1708 enum tree_code rhs_def_code = TREE_CODE (rhs_def_rhs);
1710 if ((rhs_code == rhs_def_code && unsafe_associative_fp_binop (rhs))
1711 || (rhs_code == PLUS_EXPR && rhs_def_code == MINUS_EXPR)
1712 || (rhs_code == MINUS_EXPR && rhs_def_code == PLUS_EXPR))
1714 tree def_stmt_op0 = TREE_OPERAND (rhs_def_rhs, 0);
1715 tree def_stmt_op1 = TREE_OPERAND (rhs_def_rhs, 1);
1717 if (TREE_CODE (def_stmt_op0) == SSA_NAME
1718 && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def_stmt_op0)
1719 && is_gimple_min_invariant (def_stmt_op1))
1721 tree outer_const = TREE_OPERAND (rhs, 1);
1722 tree type = TREE_TYPE (TREE_OPERAND (stmt, 0));
1723 tree t;
1725 /* If we care about correct floating point results, then
1726 don't fold x + c1 - c2. Note that we need to take both
1727 the codes and the signs to figure this out. */
1728 if (FLOAT_TYPE_P (type)
1729 && !flag_unsafe_math_optimizations
1730 && (rhs_def_code == PLUS_EXPR
1731 || rhs_def_code == MINUS_EXPR))
1733 bool neg = false;
1735 neg ^= (rhs_code == MINUS_EXPR);
1736 neg ^= (rhs_def_code == MINUS_EXPR);
1737 neg ^= real_isneg (TREE_REAL_CST_PTR (outer_const));
1738 neg ^= real_isneg (TREE_REAL_CST_PTR (def_stmt_op1));
1740 if (neg)
1741 goto dont_fold_assoc;
1744 /* Ho hum. So fold will only operate on the outermost
1745 thingy that we give it, so we have to build the new
1746 expression in two pieces. This requires that we handle
1747 combinations of plus and minus. */
1748 if (rhs_def_code != rhs_code)
1750 if (rhs_def_code == MINUS_EXPR)
1751 t = build (MINUS_EXPR, type, outer_const, def_stmt_op1);
1752 else
1753 t = build (MINUS_EXPR, type, def_stmt_op1, outer_const);
1754 rhs_code = PLUS_EXPR;
1756 else if (rhs_def_code == MINUS_EXPR)
1757 t = build (PLUS_EXPR, type, def_stmt_op1, outer_const);
1758 else
1759 t = build (rhs_def_code, type, def_stmt_op1, outer_const);
1760 t = local_fold (t);
1761 t = build (rhs_code, type, def_stmt_op0, t);
1762 t = local_fold (t);
1764 /* If the result is a suitable looking gimple expression,
1765 then use it instead of the original for STMT. */
1766 if (TREE_CODE (t) == SSA_NAME
1767 || (UNARY_CLASS_P (t)
1768 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME)
1769 || ((BINARY_CLASS_P (t) || COMPARISON_CLASS_P (t))
1770 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
1771 && is_gimple_val (TREE_OPERAND (t, 1))))
1772 result = update_rhs_and_lookup_avail_expr (stmt, t, insert);
1776 dont_fold_assoc:;
1779 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
1780 and BIT_AND_EXPR respectively if the first operand is greater
1781 than zero and the second operand is an exact power of two. */
1782 if ((rhs_code == TRUNC_DIV_EXPR || rhs_code == TRUNC_MOD_EXPR)
1783 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0)))
1784 && integer_pow2p (TREE_OPERAND (rhs, 1)))
1786 tree val;
1787 tree op = TREE_OPERAND (rhs, 0);
1789 if (TYPE_UNSIGNED (TREE_TYPE (op)))
1791 val = integer_one_node;
1793 else
1795 tree dummy_cond = walk_data->global_data;
1797 if (! dummy_cond)
1799 dummy_cond = build (GT_EXPR, boolean_type_node,
1800 op, integer_zero_node);
1801 dummy_cond = build (COND_EXPR, void_type_node,
1802 dummy_cond, NULL, NULL);
1803 walk_data->global_data = dummy_cond;
1805 else
1807 TREE_SET_CODE (TREE_OPERAND (dummy_cond, 0), GT_EXPR);
1808 TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 0) = op;
1809 TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 1)
1810 = integer_zero_node;
1812 val = simplify_cond_and_lookup_avail_expr (dummy_cond, NULL, false);
1815 if (val && integer_onep (val))
1817 tree t;
1818 tree op0 = TREE_OPERAND (rhs, 0);
1819 tree op1 = TREE_OPERAND (rhs, 1);
1821 if (rhs_code == TRUNC_DIV_EXPR)
1822 t = build (RSHIFT_EXPR, TREE_TYPE (op0), op0,
1823 build_int_cst (NULL_TREE, tree_log2 (op1)));
1824 else
1825 t = build (BIT_AND_EXPR, TREE_TYPE (op0), op0,
1826 local_fold (build (MINUS_EXPR, TREE_TYPE (op1),
1827 op1, integer_one_node)));
1829 result = update_rhs_and_lookup_avail_expr (stmt, t, insert);
1833 /* Transform ABS (X) into X or -X as appropriate. */
1834 if (rhs_code == ABS_EXPR
1835 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0))))
1837 tree val;
1838 tree op = TREE_OPERAND (rhs, 0);
1839 tree type = TREE_TYPE (op);
1841 if (TYPE_UNSIGNED (type))
1843 val = integer_zero_node;
1845 else
1847 tree dummy_cond = walk_data->global_data;
1849 if (! dummy_cond)
1851 dummy_cond = build (LE_EXPR, boolean_type_node,
1852 op, integer_zero_node);
1853 dummy_cond = build (COND_EXPR, void_type_node,
1854 dummy_cond, NULL, NULL);
1855 walk_data->global_data = dummy_cond;
1857 else
1859 TREE_SET_CODE (TREE_OPERAND (dummy_cond, 0), LE_EXPR);
1860 TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 0) = op;
1861 TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 1)
1862 = build_int_cst (type, 0);
1864 val = simplify_cond_and_lookup_avail_expr (dummy_cond, NULL, false);
1866 if (!val)
1868 TREE_SET_CODE (TREE_OPERAND (dummy_cond, 0), GE_EXPR);
1869 TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 0) = op;
1870 TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 1)
1871 = build_int_cst (type, 0);
1873 val = simplify_cond_and_lookup_avail_expr (dummy_cond,
1874 NULL, false);
1876 if (val)
1878 if (integer_zerop (val))
1879 val = integer_one_node;
1880 else if (integer_onep (val))
1881 val = integer_zero_node;
1886 if (val
1887 && (integer_onep (val) || integer_zerop (val)))
1889 tree t;
1891 if (integer_onep (val))
1892 t = build1 (NEGATE_EXPR, TREE_TYPE (op), op);
1893 else
1894 t = op;
1896 result = update_rhs_and_lookup_avail_expr (stmt, t, insert);
1900 /* Optimize *"foo" into 'f'. This is done here rather than
1901 in fold to avoid problems with stuff like &*"foo". */
1902 if (TREE_CODE (rhs) == INDIRECT_REF || TREE_CODE (rhs) == ARRAY_REF)
1904 tree t = fold_read_from_constant_string (rhs);
1906 if (t)
1907 result = update_rhs_and_lookup_avail_expr (stmt, t, insert);
1910 return result;
1913 /* COND is a condition of the form:
1915 x == const or x != const
1917 Look back to x's defining statement and see if x is defined as
1919 x = (type) y;
1921 If const is unchanged if we convert it to type, then we can build
1922 the equivalent expression:
1925 y == const or y != const
1927 Which may allow further optimizations.
1929 Return the equivalent comparison or NULL if no such equivalent comparison
1930 was found. */
1932 static tree
1933 find_equivalent_equality_comparison (tree cond)
1935 tree op0 = TREE_OPERAND (cond, 0);
1936 tree op1 = TREE_OPERAND (cond, 1);
1937 tree def_stmt = SSA_NAME_DEF_STMT (op0);
1939 /* OP0 might have been a parameter, so first make sure it
1940 was defined by a MODIFY_EXPR. */
1941 if (def_stmt && TREE_CODE (def_stmt) == MODIFY_EXPR)
1943 tree def_rhs = TREE_OPERAND (def_stmt, 1);
1945 /* Now make sure the RHS of the MODIFY_EXPR is a typecast. */
1946 if ((TREE_CODE (def_rhs) == NOP_EXPR
1947 || TREE_CODE (def_rhs) == CONVERT_EXPR)
1948 && TREE_CODE (TREE_OPERAND (def_rhs, 0)) == SSA_NAME)
1950 tree def_rhs_inner = TREE_OPERAND (def_rhs, 0);
1951 tree def_rhs_inner_type = TREE_TYPE (def_rhs_inner);
1952 tree new;
1954 if (TYPE_PRECISION (def_rhs_inner_type)
1955 > TYPE_PRECISION (TREE_TYPE (def_rhs)))
1956 return NULL;
1958 /* What we want to prove is that if we convert OP1 to
1959 the type of the object inside the NOP_EXPR that the
1960 result is still equivalent to SRC.
1962 If that is true, the build and return new equivalent
1963 condition which uses the source of the typecast and the
1964 new constant (which has only changed its type). */
1965 new = build1 (TREE_CODE (def_rhs), def_rhs_inner_type, op1);
1966 new = local_fold (new);
1967 if (is_gimple_val (new) && tree_int_cst_equal (new, op1))
1968 return build (TREE_CODE (cond), TREE_TYPE (cond),
1969 def_rhs_inner, new);
1972 return NULL;
1975 /* STMT is a COND_EXPR for which we could not trivially determine its
1976 result. This routine attempts to find equivalent forms of the
1977 condition which we may be able to optimize better. It also
1978 uses simple value range propagation to optimize conditionals. */
1980 static tree
1981 simplify_cond_and_lookup_avail_expr (tree stmt,
1982 stmt_ann_t ann,
1983 int insert)
1985 tree cond = COND_EXPR_COND (stmt);
1987 if (COMPARISON_CLASS_P (cond))
1989 tree op0 = TREE_OPERAND (cond, 0);
1990 tree op1 = TREE_OPERAND (cond, 1);
1992 if (TREE_CODE (op0) == SSA_NAME && is_gimple_min_invariant (op1))
1994 int limit;
1995 tree low, high, cond_low, cond_high;
1996 int lowequal, highequal, swapped, no_overlap, subset, cond_inverted;
1997 varray_type vrp_records;
1998 struct vrp_element *element;
1999 struct vrp_hash_elt vrp_hash_elt, *vrp_hash_elt_p;
2000 void **slot;
2002 /* First see if we have test of an SSA_NAME against a constant
2003 where the SSA_NAME is defined by an earlier typecast which
2004 is irrelevant when performing tests against the given
2005 constant. */
2006 if (TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
2008 tree new_cond = find_equivalent_equality_comparison (cond);
2010 if (new_cond)
2012 /* Update the statement to use the new equivalent
2013 condition. */
2014 COND_EXPR_COND (stmt) = new_cond;
2016 /* If this is not a real stmt, ann will be NULL and we
2017 avoid processing the operands. */
2018 if (ann)
2019 modify_stmt (stmt);
2021 /* Lookup the condition and return its known value if it
2022 exists. */
2023 new_cond = lookup_avail_expr (stmt, insert);
2024 if (new_cond)
2025 return new_cond;
2027 /* The operands have changed, so update op0 and op1. */
2028 op0 = TREE_OPERAND (cond, 0);
2029 op1 = TREE_OPERAND (cond, 1);
2033 /* Consult the value range records for this variable (if they exist)
2034 to see if we can eliminate or simplify this conditional.
2036 Note two tests are necessary to determine no records exist.
2037 First we have to see if the virtual array exists, if it
2038 exists, then we have to check its active size.
2040 Also note the vast majority of conditionals are not testing
2041 a variable which has had its range constrained by an earlier
2042 conditional. So this filter avoids a lot of unnecessary work. */
2043 vrp_hash_elt.var = op0;
2044 vrp_hash_elt.records = NULL;
2045 slot = htab_find_slot (vrp_data, &vrp_hash_elt, NO_INSERT);
2046 if (slot == NULL)
2047 return NULL;
2049 vrp_hash_elt_p = (struct vrp_hash_elt *) *slot;
2050 vrp_records = vrp_hash_elt_p->records;
2051 if (vrp_records == NULL)
2052 return NULL;
2054 limit = VARRAY_ACTIVE_SIZE (vrp_records);
2056 /* If we have no value range records for this variable, or we are
2057 unable to extract a range for this condition, then there is
2058 nothing to do. */
2059 if (limit == 0
2060 || ! extract_range_from_cond (cond, &cond_high,
2061 &cond_low, &cond_inverted))
2062 return NULL;
2064 /* We really want to avoid unnecessary computations of range
2065 info. So all ranges are computed lazily; this avoids a
2066 lot of unnecessary work. i.e., we record the conditional,
2067 but do not process how it constrains the variable's
2068 potential values until we know that processing the condition
2069 could be helpful.
2071 However, we do not want to have to walk a potentially long
2072 list of ranges, nor do we want to compute a variable's
2073 range more than once for a given path.
2075 Luckily, each time we encounter a conditional that can not
2076 be otherwise optimized we will end up here and we will
2077 compute the necessary range information for the variable
2078 used in this condition.
2080 Thus you can conclude that there will never be more than one
2081 conditional associated with a variable which has not been
2082 processed. So we never need to merge more than one new
2083 conditional into the current range.
2085 These properties also help us avoid unnecessary work. */
2086 element
2087 = (struct vrp_element *)VARRAY_GENERIC_PTR (vrp_records, limit - 1);
2089 if (element->high && element->low)
2091 /* The last element has been processed, so there is no range
2092 merging to do, we can simply use the high/low values
2093 recorded in the last element. */
2094 low = element->low;
2095 high = element->high;
2097 else
2099 tree tmp_high, tmp_low;
2100 int dummy;
2102 /* The last element has not been processed. Process it now. */
2103 extract_range_from_cond (element->cond, &tmp_high,
2104 &tmp_low, &dummy);
2106 /* If this is the only element, then no merging is necessary,
2107 the high/low values from extract_range_from_cond are all
2108 we need. */
2109 if (limit == 1)
2111 low = tmp_low;
2112 high = tmp_high;
2114 else
2116 /* Get the high/low value from the previous element. */
2117 struct vrp_element *prev
2118 = (struct vrp_element *)VARRAY_GENERIC_PTR (vrp_records,
2119 limit - 2);
2120 low = prev->low;
2121 high = prev->high;
2123 /* Merge in this element's range with the range from the
2124 previous element.
2126 The low value for the merged range is the maximum of
2127 the previous low value and the low value of this record.
2129 Similarly the high value for the merged range is the
2130 minimum of the previous high value and the high value of
2131 this record. */
2132 low = (tree_int_cst_compare (low, tmp_low) == 1
2133 ? low : tmp_low);
2134 high = (tree_int_cst_compare (high, tmp_high) == -1
2135 ? high : tmp_high);
2138 /* And record the computed range. */
2139 element->low = low;
2140 element->high = high;
2144 /* After we have constrained this variable's potential values,
2145 we try to determine the result of the given conditional.
2147 To simplify later tests, first determine if the current
2148 low value is the same low value as the conditional.
2149 Similarly for the current high value and the high value
2150 for the conditional. */
2151 lowequal = tree_int_cst_equal (low, cond_low);
2152 highequal = tree_int_cst_equal (high, cond_high);
2154 if (lowequal && highequal)
2155 return (cond_inverted ? boolean_false_node : boolean_true_node);
2157 /* To simplify the overlap/subset tests below we may want
2158 to swap the two ranges so that the larger of the two
2159 ranges occurs "first". */
2160 swapped = 0;
2161 if (tree_int_cst_compare (low, cond_low) == 1
2162 || (lowequal
2163 && tree_int_cst_compare (cond_high, high) == 1))
2165 tree temp;
2167 swapped = 1;
2168 temp = low;
2169 low = cond_low;
2170 cond_low = temp;
2171 temp = high;
2172 high = cond_high;
2173 cond_high = temp;
2176 /* Now determine if there is no overlap in the ranges
2177 or if the second range is a subset of the first range. */
2178 no_overlap = tree_int_cst_lt (high, cond_low);
2179 subset = tree_int_cst_compare (cond_high, high) != 1;
2181 /* If there was no overlap in the ranges, then this conditional
2182 always has a false value (unless we had to invert this
2183 conditional, in which case it always has a true value). */
2184 if (no_overlap)
2185 return (cond_inverted ? boolean_true_node : boolean_false_node);
2187 /* If the current range is a subset of the condition's range,
2188 then this conditional always has a true value (unless we
2189 had to invert this conditional, in which case it always
2190 has a true value). */
2191 if (subset && swapped)
2192 return (cond_inverted ? boolean_false_node : boolean_true_node);
2194 /* We were unable to determine the result of the conditional.
2195 However, we may be able to simplify the conditional. First
2196 merge the ranges in the same manner as range merging above. */
2197 low = tree_int_cst_compare (low, cond_low) == 1 ? low : cond_low;
2198 high = tree_int_cst_compare (high, cond_high) == -1 ? high : cond_high;
2200 /* If the range has converged to a single point, then turn this
2201 into an equality comparison. */
2202 if (TREE_CODE (cond) != EQ_EXPR
2203 && TREE_CODE (cond) != NE_EXPR
2204 && tree_int_cst_equal (low, high))
2206 TREE_SET_CODE (cond, EQ_EXPR);
2207 TREE_OPERAND (cond, 1) = high;
2211 return 0;
2214 /* STMT is a SWITCH_EXPR for which we could not trivially determine its
2215 result. This routine attempts to find equivalent forms of the
2216 condition which we may be able to optimize better. */
2218 static tree
2219 simplify_switch_and_lookup_avail_expr (tree stmt, int insert)
2221 tree cond = SWITCH_COND (stmt);
2222 tree def, to, ti;
2224 /* The optimization that we really care about is removing unnecessary
2225 casts. That will let us do much better in propagating the inferred
2226 constant at the switch target. */
2227 if (TREE_CODE (cond) == SSA_NAME)
2229 def = SSA_NAME_DEF_STMT (cond);
2230 if (TREE_CODE (def) == MODIFY_EXPR)
2232 def = TREE_OPERAND (def, 1);
2233 if (TREE_CODE (def) == NOP_EXPR)
2235 int need_precision;
2236 bool fail;
2238 def = TREE_OPERAND (def, 0);
2240 #ifdef ENABLE_CHECKING
2241 /* ??? Why was Jeff testing this? We are gimple... */
2242 gcc_assert (is_gimple_val (def));
2243 #endif
2245 to = TREE_TYPE (cond);
2246 ti = TREE_TYPE (def);
2248 /* If we have an extension that preserves value, then we
2249 can copy the source value into the switch. */
2251 need_precision = TYPE_PRECISION (ti);
2252 fail = false;
2253 if (TYPE_UNSIGNED (to) && !TYPE_UNSIGNED (ti))
2254 fail = true;
2255 else if (!TYPE_UNSIGNED (to) && TYPE_UNSIGNED (ti))
2256 need_precision += 1;
2257 if (TYPE_PRECISION (to) < need_precision)
2258 fail = true;
2260 if (!fail)
2262 SWITCH_COND (stmt) = def;
2263 modify_stmt (stmt);
2265 return lookup_avail_expr (stmt, insert);
2271 return 0;
2275 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
2276 known value for that SSA_NAME (or NULL if no value is known).
2278 NONZERO_VARS is the set SSA_NAMES known to have a nonzero value,
2279 even if we don't know their precise value.
2281 Propagate values from CONST_AND_COPIES and NONZERO_VARS into the PHI
2282 nodes of the successors of BB. */
2284 static void
2285 cprop_into_successor_phis (basic_block bb, bitmap nonzero_vars)
2287 edge e;
2288 edge_iterator ei;
2290 /* This can get rather expensive if the implementation is naive in
2291 how it finds the phi alternative associated with a particular edge. */
2292 FOR_EACH_EDGE (e, ei, bb->succs)
2294 tree phi;
2295 int phi_num_args;
2296 int hint;
2298 /* If this is an abnormal edge, then we do not want to copy propagate
2299 into the PHI alternative associated with this edge. */
2300 if (e->flags & EDGE_ABNORMAL)
2301 continue;
2303 phi = phi_nodes (e->dest);
2304 if (! phi)
2305 continue;
2307 /* There is no guarantee that for any two PHI nodes in a block that
2308 the phi alternative associated with a particular edge will be
2309 at the same index in the phi alternative array.
2311 However, it is very likely they will be the same. So we keep
2312 track of the index of the alternative where we found the edge in
2313 the previous phi node and check that index first in the next
2314 phi node. If that hint fails, then we actually search all
2315 the entries. */
2316 phi_num_args = PHI_NUM_ARGS (phi);
2317 hint = phi_num_args;
2318 for ( ; phi; phi = PHI_CHAIN (phi))
2320 int i;
2321 tree new;
2322 use_operand_p orig_p;
2323 tree orig;
2325 /* If the hint is valid (!= phi_num_args), see if it points
2326 us to the desired phi alternative. */
2327 if (hint != phi_num_args && PHI_ARG_EDGE (phi, hint) == e)
2329 else
2331 /* The hint was either invalid or did not point to the
2332 correct phi alternative. Search all the alternatives
2333 for the correct one. Update the hint. */
2334 for (i = 0; i < phi_num_args; i++)
2335 if (PHI_ARG_EDGE (phi, i) == e)
2336 break;
2337 hint = i;
2340 /* If we did not find the proper alternative, then something is
2341 horribly wrong. */
2342 gcc_assert (hint != phi_num_args);
2344 /* The alternative may be associated with a constant, so verify
2345 it is an SSA_NAME before doing anything with it. */
2346 orig_p = PHI_ARG_DEF_PTR (phi, hint);
2347 orig = USE_FROM_PTR (orig_p);
2348 if (TREE_CODE (orig) != SSA_NAME)
2349 continue;
2351 /* If the alternative is known to have a nonzero value, record
2352 that fact in the PHI node itself for future use. */
2353 if (bitmap_bit_p (nonzero_vars, SSA_NAME_VERSION (orig)))
2354 PHI_ARG_NONZERO (phi, hint) = true;
2356 /* If we have *ORIG_P in our constant/copy table, then replace
2357 ORIG_P with its value in our constant/copy table. */
2358 new = SSA_NAME_VALUE (orig);
2359 if (new
2360 && (TREE_CODE (new) == SSA_NAME
2361 || is_gimple_min_invariant (new))
2362 && may_propagate_copy (orig, new))
2364 propagate_value (orig_p, new);
2370 /* We have finished optimizing BB, record any information implied by
2371 taking a specific outgoing edge from BB. */
2373 static void
2374 record_edge_info (basic_block bb)
2376 block_stmt_iterator bsi = bsi_last (bb);
2377 struct edge_info *edge_info;
2379 if (! bsi_end_p (bsi))
2381 tree stmt = bsi_stmt (bsi);
2383 if (stmt && TREE_CODE (stmt) == SWITCH_EXPR)
2385 tree cond = SWITCH_COND (stmt);
2387 if (TREE_CODE (cond) == SSA_NAME)
2389 tree labels = SWITCH_LABELS (stmt);
2390 int i, n_labels = TREE_VEC_LENGTH (labels);
2391 tree *info = xcalloc (n_basic_blocks, sizeof (tree));
2392 edge e;
2393 edge_iterator ei;
2395 for (i = 0; i < n_labels; i++)
2397 tree label = TREE_VEC_ELT (labels, i);
2398 basic_block target_bb = label_to_block (CASE_LABEL (label));
2400 if (CASE_HIGH (label)
2401 || !CASE_LOW (label)
2402 || info[target_bb->index])
2403 info[target_bb->index] = error_mark_node;
2404 else
2405 info[target_bb->index] = label;
2408 FOR_EACH_EDGE (e, ei, bb->succs)
2410 basic_block target_bb = e->dest;
2411 tree node = info[target_bb->index];
2413 if (node != NULL && node != error_mark_node)
2415 tree x = fold_convert (TREE_TYPE (cond), CASE_LOW (node));
2416 edge_info = allocate_edge_info (e);
2417 edge_info->lhs = cond;
2418 edge_info->rhs = x;
2421 free (info);
2425 /* A COND_EXPR may create equivalences too. */
2426 if (stmt && TREE_CODE (stmt) == COND_EXPR)
2428 tree cond = COND_EXPR_COND (stmt);
2429 edge true_edge;
2430 edge false_edge;
2432 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2434 /* If the conditional is a single variable 'X', record 'X = 1'
2435 for the true edge and 'X = 0' on the false edge. */
2436 if (SSA_VAR_P (cond))
2438 struct edge_info *edge_info;
2440 edge_info = allocate_edge_info (true_edge);
2441 edge_info->lhs = cond;
2442 edge_info->rhs = constant_boolean_node (1, TREE_TYPE (cond));
2444 edge_info = allocate_edge_info (false_edge);
2445 edge_info->lhs = cond;
2446 edge_info->rhs = constant_boolean_node (0, TREE_TYPE (cond));
2448 /* Equality tests may create one or two equivalences. */
2449 else if (COMPARISON_CLASS_P (cond))
2451 tree op0 = TREE_OPERAND (cond, 0);
2452 tree op1 = TREE_OPERAND (cond, 1);
2454 /* Special case comparing booleans against a constant as we
2455 know the value of OP0 on both arms of the branch. i.e., we
2456 can record an equivalence for OP0 rather than COND. */
2457 if ((TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
2458 && TREE_CODE (op0) == SSA_NAME
2459 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
2460 && is_gimple_min_invariant (op1))
2462 if (TREE_CODE (cond) == EQ_EXPR)
2464 edge_info = allocate_edge_info (true_edge);
2465 edge_info->lhs = op0;
2466 edge_info->rhs = (integer_zerop (op1)
2467 ? boolean_false_node
2468 : boolean_true_node);
2470 edge_info = allocate_edge_info (false_edge);
2471 edge_info->lhs = op0;
2472 edge_info->rhs = (integer_zerop (op1)
2473 ? boolean_true_node
2474 : boolean_false_node);
2476 else
2478 edge_info = allocate_edge_info (true_edge);
2479 edge_info->lhs = op0;
2480 edge_info->rhs = (integer_zerop (op1)
2481 ? boolean_true_node
2482 : boolean_false_node);
2484 edge_info = allocate_edge_info (false_edge);
2485 edge_info->lhs = op0;
2486 edge_info->rhs = (integer_zerop (op1)
2487 ? boolean_false_node
2488 : boolean_true_node);
2492 if (is_gimple_min_invariant (op0)
2493 && (TREE_CODE (op1) == SSA_NAME
2494 || is_gimple_min_invariant (op1)))
2496 tree inverted = invert_truthvalue (cond);
2497 struct edge_info *edge_info;
2499 edge_info = allocate_edge_info (true_edge);
2500 record_conditions (edge_info, cond, inverted);
2502 if (TREE_CODE (cond) == EQ_EXPR)
2504 edge_info->lhs = op1;
2505 edge_info->rhs = op0;
2508 edge_info = allocate_edge_info (false_edge);
2509 record_conditions (edge_info, inverted, cond);
2511 if (TREE_CODE (cond) == NE_EXPR)
2513 edge_info->lhs = op1;
2514 edge_info->rhs = op0;
2518 if (TREE_CODE (op0) == SSA_NAME
2519 && (is_gimple_min_invariant (op1)
2520 || TREE_CODE (op1) == SSA_NAME))
2522 tree inverted = invert_truthvalue (cond);
2523 struct edge_info *edge_info;
2525 edge_info = allocate_edge_info (true_edge);
2526 record_conditions (edge_info, cond, inverted);
2528 if (TREE_CODE (cond) == EQ_EXPR)
2530 edge_info->lhs = op0;
2531 edge_info->rhs = op1;
2534 edge_info = allocate_edge_info (false_edge);
2535 record_conditions (edge_info, inverted, cond);
2537 if (TREE_CODE (cond) == NE_EXPR)
2539 edge_info->lhs = op0;
2540 edge_info->rhs = op1;
2545 /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
2550 /* Propagate information from BB to its outgoing edges.
2552 This can include equivalency information implied by control statements
2553 at the end of BB and const/copy propagation into PHIs in BB's
2554 successor blocks. */
2556 static void
2557 propagate_to_outgoing_edges (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
2558 basic_block bb)
2561 record_edge_info (bb);
2562 cprop_into_successor_phis (bb, nonzero_vars);
2565 /* Search for redundant computations in STMT. If any are found, then
2566 replace them with the variable holding the result of the computation.
2568 If safe, record this expression into the available expression hash
2569 table. */
2571 static bool
2572 eliminate_redundant_computations (struct dom_walk_data *walk_data,
2573 tree stmt, stmt_ann_t ann)
2575 v_may_def_optype v_may_defs = V_MAY_DEF_OPS (ann);
2576 tree *expr_p, def = NULL_TREE;
2577 bool insert = true;
2578 tree cached_lhs;
2579 bool retval = false;
2581 if (TREE_CODE (stmt) == MODIFY_EXPR)
2582 def = TREE_OPERAND (stmt, 0);
2584 /* Certain expressions on the RHS can be optimized away, but can not
2585 themselves be entered into the hash tables. */
2586 if (ann->makes_aliased_stores
2587 || ! def
2588 || TREE_CODE (def) != SSA_NAME
2589 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)
2590 || NUM_V_MAY_DEFS (v_may_defs) != 0)
2591 insert = false;
2593 /* Check if the expression has been computed before. */
2594 cached_lhs = lookup_avail_expr (stmt, insert);
2596 /* If this is an assignment and the RHS was not in the hash table,
2597 then try to simplify the RHS and lookup the new RHS in the
2598 hash table. */
2599 if (! cached_lhs && TREE_CODE (stmt) == MODIFY_EXPR)
2600 cached_lhs = simplify_rhs_and_lookup_avail_expr (walk_data, stmt, insert);
2601 /* Similarly if this is a COND_EXPR and we did not find its
2602 expression in the hash table, simplify the condition and
2603 try again. */
2604 else if (! cached_lhs && TREE_CODE (stmt) == COND_EXPR)
2605 cached_lhs = simplify_cond_and_lookup_avail_expr (stmt, ann, insert);
2606 /* Similarly for a SWITCH_EXPR. */
2607 else if (!cached_lhs && TREE_CODE (stmt) == SWITCH_EXPR)
2608 cached_lhs = simplify_switch_and_lookup_avail_expr (stmt, insert);
2610 opt_stats.num_exprs_considered++;
2612 /* Get a pointer to the expression we are trying to optimize. */
2613 if (TREE_CODE (stmt) == COND_EXPR)
2614 expr_p = &COND_EXPR_COND (stmt);
2615 else if (TREE_CODE (stmt) == SWITCH_EXPR)
2616 expr_p = &SWITCH_COND (stmt);
2617 else if (TREE_CODE (stmt) == RETURN_EXPR && TREE_OPERAND (stmt, 0))
2618 expr_p = &TREE_OPERAND (TREE_OPERAND (stmt, 0), 1);
2619 else
2620 expr_p = &TREE_OPERAND (stmt, 1);
2622 /* It is safe to ignore types here since we have already done
2623 type checking in the hashing and equality routines. In fact
2624 type checking here merely gets in the way of constant
2625 propagation. Also, make sure that it is safe to propagate
2626 CACHED_LHS into *EXPR_P. */
2627 if (cached_lhs
2628 && (TREE_CODE (cached_lhs) != SSA_NAME
2629 || may_propagate_copy (*expr_p, cached_lhs)))
2631 if (dump_file && (dump_flags & TDF_DETAILS))
2633 fprintf (dump_file, " Replaced redundant expr '");
2634 print_generic_expr (dump_file, *expr_p, dump_flags);
2635 fprintf (dump_file, "' with '");
2636 print_generic_expr (dump_file, cached_lhs, dump_flags);
2637 fprintf (dump_file, "'\n");
2640 opt_stats.num_re++;
2642 #if defined ENABLE_CHECKING
2643 gcc_assert (TREE_CODE (cached_lhs) == SSA_NAME
2644 || is_gimple_min_invariant (cached_lhs));
2645 #endif
2647 if (TREE_CODE (cached_lhs) == ADDR_EXPR
2648 || (POINTER_TYPE_P (TREE_TYPE (*expr_p))
2649 && is_gimple_min_invariant (cached_lhs)))
2650 retval = true;
2652 propagate_tree_value (expr_p, cached_lhs);
2653 modify_stmt (stmt);
2655 return retval;
2658 /* STMT, a MODIFY_EXPR, may create certain equivalences, in either
2659 the available expressions table or the const_and_copies table.
2660 Detect and record those equivalences. */
2662 static void
2663 record_equivalences_from_stmt (tree stmt,
2664 int may_optimize_p,
2665 stmt_ann_t ann)
2667 tree lhs = TREE_OPERAND (stmt, 0);
2668 enum tree_code lhs_code = TREE_CODE (lhs);
2669 int i;
2671 if (lhs_code == SSA_NAME)
2673 tree rhs = TREE_OPERAND (stmt, 1);
2675 /* Strip away any useless type conversions. */
2676 STRIP_USELESS_TYPE_CONVERSION (rhs);
2678 /* If the RHS of the assignment is a constant or another variable that
2679 may be propagated, register it in the CONST_AND_COPIES table. We
2680 do not need to record unwind data for this, since this is a true
2681 assignment and not an equivalence inferred from a comparison. All
2682 uses of this ssa name are dominated by this assignment, so unwinding
2683 just costs time and space. */
2684 if (may_optimize_p
2685 && (TREE_CODE (rhs) == SSA_NAME
2686 || is_gimple_min_invariant (rhs)))
2687 SSA_NAME_VALUE (lhs) = rhs;
2689 /* alloca never returns zero and the address of a non-weak symbol
2690 is never zero. NOP_EXPRs and CONVERT_EXPRs can be completely
2691 stripped as they do not affect this equivalence. */
2692 while (TREE_CODE (rhs) == NOP_EXPR
2693 || TREE_CODE (rhs) == CONVERT_EXPR)
2694 rhs = TREE_OPERAND (rhs, 0);
2696 if (alloca_call_p (rhs)
2697 || (TREE_CODE (rhs) == ADDR_EXPR
2698 && DECL_P (TREE_OPERAND (rhs, 0))
2699 && ! DECL_WEAK (TREE_OPERAND (rhs, 0))))
2700 record_var_is_nonzero (lhs);
2702 /* IOR of any value with a nonzero value will result in a nonzero
2703 value. Even if we do not know the exact result recording that
2704 the result is nonzero is worth the effort. */
2705 if (TREE_CODE (rhs) == BIT_IOR_EXPR
2706 && integer_nonzerop (TREE_OPERAND (rhs, 1)))
2707 record_var_is_nonzero (lhs);
2710 /* Look at both sides for pointer dereferences. If we find one, then
2711 the pointer must be nonnull and we can enter that equivalence into
2712 the hash tables. */
2713 if (flag_delete_null_pointer_checks)
2714 for (i = 0; i < 2; i++)
2716 tree t = TREE_OPERAND (stmt, i);
2718 /* Strip away any COMPONENT_REFs. */
2719 while (TREE_CODE (t) == COMPONENT_REF)
2720 t = TREE_OPERAND (t, 0);
2722 /* Now see if this is a pointer dereference. */
2723 if (INDIRECT_REF_P (t))
2725 tree op = TREE_OPERAND (t, 0);
2727 /* If the pointer is a SSA variable, then enter new
2728 equivalences into the hash table. */
2729 while (TREE_CODE (op) == SSA_NAME)
2731 tree def = SSA_NAME_DEF_STMT (op);
2733 record_var_is_nonzero (op);
2735 /* And walk up the USE-DEF chains noting other SSA_NAMEs
2736 which are known to have a nonzero value. */
2737 if (def
2738 && TREE_CODE (def) == MODIFY_EXPR
2739 && TREE_CODE (TREE_OPERAND (def, 1)) == NOP_EXPR)
2740 op = TREE_OPERAND (TREE_OPERAND (def, 1), 0);
2741 else
2742 break;
2747 /* A memory store, even an aliased store, creates a useful
2748 equivalence. By exchanging the LHS and RHS, creating suitable
2749 vops and recording the result in the available expression table,
2750 we may be able to expose more redundant loads. */
2751 if (!ann->has_volatile_ops
2752 && (TREE_CODE (TREE_OPERAND (stmt, 1)) == SSA_NAME
2753 || is_gimple_min_invariant (TREE_OPERAND (stmt, 1)))
2754 && !is_gimple_reg (lhs))
2756 tree rhs = TREE_OPERAND (stmt, 1);
2757 tree new;
2759 /* FIXME: If the LHS of the assignment is a bitfield and the RHS
2760 is a constant, we need to adjust the constant to fit into the
2761 type of the LHS. If the LHS is a bitfield and the RHS is not
2762 a constant, then we can not record any equivalences for this
2763 statement since we would need to represent the widening or
2764 narrowing of RHS. This fixes gcc.c-torture/execute/921016-1.c
2765 and should not be necessary if GCC represented bitfields
2766 properly. */
2767 if (lhs_code == COMPONENT_REF
2768 && DECL_BIT_FIELD (TREE_OPERAND (lhs, 1)))
2770 if (TREE_CONSTANT (rhs))
2771 rhs = widen_bitfield (rhs, TREE_OPERAND (lhs, 1), lhs);
2772 else
2773 rhs = NULL;
2775 /* If the value overflowed, then we can not use this equivalence. */
2776 if (rhs && ! is_gimple_min_invariant (rhs))
2777 rhs = NULL;
2780 if (rhs)
2782 /* Build a new statement with the RHS and LHS exchanged. */
2783 new = build (MODIFY_EXPR, TREE_TYPE (stmt), rhs, lhs);
2785 create_ssa_artficial_load_stmt (&(ann->operands), new);
2787 /* Finally enter the statement into the available expression
2788 table. */
2789 lookup_avail_expr (new, true);
2794 /* Replace *OP_P in STMT with any known equivalent value for *OP_P from
2795 CONST_AND_COPIES. */
2797 static bool
2798 cprop_operand (tree stmt, use_operand_p op_p)
2800 bool may_have_exposed_new_symbols = false;
2801 tree val;
2802 tree op = USE_FROM_PTR (op_p);
2804 /* If the operand has a known constant value or it is known to be a
2805 copy of some other variable, use the value or copy stored in
2806 CONST_AND_COPIES. */
2807 val = SSA_NAME_VALUE (op);
2808 if (val && TREE_CODE (val) != VALUE_HANDLE)
2810 tree op_type, val_type;
2812 /* Do not change the base variable in the virtual operand
2813 tables. That would make it impossible to reconstruct
2814 the renamed virtual operand if we later modify this
2815 statement. Also only allow the new value to be an SSA_NAME
2816 for propagation into virtual operands. */
2817 if (!is_gimple_reg (op)
2818 && (get_virtual_var (val) != get_virtual_var (op)
2819 || TREE_CODE (val) != SSA_NAME))
2820 return false;
2822 /* Do not replace hard register operands in asm statements. */
2823 if (TREE_CODE (stmt) == ASM_EXPR
2824 && !may_propagate_copy_into_asm (op))
2825 return false;
2827 /* Get the toplevel type of each operand. */
2828 op_type = TREE_TYPE (op);
2829 val_type = TREE_TYPE (val);
2831 /* While both types are pointers, get the type of the object
2832 pointed to. */
2833 while (POINTER_TYPE_P (op_type) && POINTER_TYPE_P (val_type))
2835 op_type = TREE_TYPE (op_type);
2836 val_type = TREE_TYPE (val_type);
2839 /* Make sure underlying types match before propagating a constant by
2840 converting the constant to the proper type. Note that convert may
2841 return a non-gimple expression, in which case we ignore this
2842 propagation opportunity. */
2843 if (TREE_CODE (val) != SSA_NAME)
2845 if (!lang_hooks.types_compatible_p (op_type, val_type))
2847 val = fold_convert (TREE_TYPE (op), val);
2848 if (!is_gimple_min_invariant (val))
2849 return false;
2853 /* Certain operands are not allowed to be copy propagated due
2854 to their interaction with exception handling and some GCC
2855 extensions. */
2856 else if (!may_propagate_copy (op, val))
2857 return false;
2859 /* Dump details. */
2860 if (dump_file && (dump_flags & TDF_DETAILS))
2862 fprintf (dump_file, " Replaced '");
2863 print_generic_expr (dump_file, op, dump_flags);
2864 fprintf (dump_file, "' with %s '",
2865 (TREE_CODE (val) != SSA_NAME ? "constant" : "variable"));
2866 print_generic_expr (dump_file, val, dump_flags);
2867 fprintf (dump_file, "'\n");
2870 /* If VAL is an ADDR_EXPR or a constant of pointer type, note
2871 that we may have exposed a new symbol for SSA renaming. */
2872 if (TREE_CODE (val) == ADDR_EXPR
2873 || (POINTER_TYPE_P (TREE_TYPE (op))
2874 && is_gimple_min_invariant (val)))
2875 may_have_exposed_new_symbols = true;
2877 propagate_value (op_p, val);
2879 /* And note that we modified this statement. This is now
2880 safe, even if we changed virtual operands since we will
2881 rescan the statement and rewrite its operands again. */
2882 modify_stmt (stmt);
2884 return may_have_exposed_new_symbols;
2887 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
2888 known value for that SSA_NAME (or NULL if no value is known).
2890 Propagate values from CONST_AND_COPIES into the uses, vuses and
2891 v_may_def_ops of STMT. */
2893 static bool
2894 cprop_into_stmt (tree stmt)
2896 bool may_have_exposed_new_symbols = false;
2897 use_operand_p op_p;
2898 ssa_op_iter iter;
2899 tree rhs;
2901 FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_ALL_USES)
2903 if (TREE_CODE (USE_FROM_PTR (op_p)) == SSA_NAME)
2904 may_have_exposed_new_symbols |= cprop_operand (stmt, op_p);
2907 if (may_have_exposed_new_symbols)
2909 rhs = get_rhs (stmt);
2910 if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
2911 recompute_tree_invarant_for_addr_expr (rhs);
2914 return may_have_exposed_new_symbols;
2918 /* Optimize the statement pointed by iterator SI.
2920 We try to perform some simplistic global redundancy elimination and
2921 constant propagation:
2923 1- To detect global redundancy, we keep track of expressions that have
2924 been computed in this block and its dominators. If we find that the
2925 same expression is computed more than once, we eliminate repeated
2926 computations by using the target of the first one.
2928 2- Constant values and copy assignments. This is used to do very
2929 simplistic constant and copy propagation. When a constant or copy
2930 assignment is found, we map the value on the RHS of the assignment to
2931 the variable in the LHS in the CONST_AND_COPIES table. */
2933 static void
2934 optimize_stmt (struct dom_walk_data *walk_data, basic_block bb,
2935 block_stmt_iterator si)
2937 stmt_ann_t ann;
2938 tree stmt;
2939 bool may_optimize_p;
2940 bool may_have_exposed_new_symbols = false;
2942 stmt = bsi_stmt (si);
2944 get_stmt_operands (stmt);
2945 ann = stmt_ann (stmt);
2946 opt_stats.num_stmts++;
2947 may_have_exposed_new_symbols = false;
2949 if (dump_file && (dump_flags & TDF_DETAILS))
2951 fprintf (dump_file, "Optimizing statement ");
2952 print_generic_stmt (dump_file, stmt, TDF_SLIM);
2955 /* Const/copy propagate into USES, VUSES and the RHS of V_MAY_DEFs. */
2956 may_have_exposed_new_symbols = cprop_into_stmt (stmt);
2958 /* If the statement has been modified with constant replacements,
2959 fold its RHS before checking for redundant computations. */
2960 if (ann->modified)
2962 /* Try to fold the statement making sure that STMT is kept
2963 up to date. */
2964 if (fold_stmt (bsi_stmt_ptr (si)))
2966 stmt = bsi_stmt (si);
2967 ann = stmt_ann (stmt);
2969 if (dump_file && (dump_flags & TDF_DETAILS))
2971 fprintf (dump_file, " Folded to: ");
2972 print_generic_stmt (dump_file, stmt, TDF_SLIM);
2976 /* Constant/copy propagation above may change the set of
2977 virtual operands associated with this statement. Folding
2978 may remove the need for some virtual operands.
2980 Indicate we will need to rescan and rewrite the statement. */
2981 may_have_exposed_new_symbols = true;
2984 /* Check for redundant computations. Do this optimization only
2985 for assignments that have no volatile ops and conditionals. */
2986 may_optimize_p = (!ann->has_volatile_ops
2987 && ((TREE_CODE (stmt) == RETURN_EXPR
2988 && TREE_OPERAND (stmt, 0)
2989 && TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR
2990 && ! (TREE_SIDE_EFFECTS
2991 (TREE_OPERAND (TREE_OPERAND (stmt, 0), 1))))
2992 || (TREE_CODE (stmt) == MODIFY_EXPR
2993 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (stmt, 1)))
2994 || TREE_CODE (stmt) == COND_EXPR
2995 || TREE_CODE (stmt) == SWITCH_EXPR));
2997 if (may_optimize_p)
2998 may_have_exposed_new_symbols
2999 |= eliminate_redundant_computations (walk_data, stmt, ann);
3001 /* Record any additional equivalences created by this statement. */
3002 if (TREE_CODE (stmt) == MODIFY_EXPR)
3003 record_equivalences_from_stmt (stmt,
3004 may_optimize_p,
3005 ann);
3007 register_definitions_for_stmt (stmt);
3009 /* If STMT is a COND_EXPR and it was modified, then we may know
3010 where it goes. If that is the case, then mark the CFG as altered.
3012 This will cause us to later call remove_unreachable_blocks and
3013 cleanup_tree_cfg when it is safe to do so. It is not safe to
3014 clean things up here since removal of edges and such can trigger
3015 the removal of PHI nodes, which in turn can release SSA_NAMEs to
3016 the manager.
3018 That's all fine and good, except that once SSA_NAMEs are released
3019 to the manager, we must not call create_ssa_name until all references
3020 to released SSA_NAMEs have been eliminated.
3022 All references to the deleted SSA_NAMEs can not be eliminated until
3023 we remove unreachable blocks.
3025 We can not remove unreachable blocks until after we have completed
3026 any queued jump threading.
3028 We can not complete any queued jump threads until we have taken
3029 appropriate variables out of SSA form. Taking variables out of
3030 SSA form can call create_ssa_name and thus we lose.
3032 Ultimately I suspect we're going to need to change the interface
3033 into the SSA_NAME manager. */
3035 if (ann->modified)
3037 tree val = NULL;
3039 if (TREE_CODE (stmt) == COND_EXPR)
3040 val = COND_EXPR_COND (stmt);
3041 else if (TREE_CODE (stmt) == SWITCH_EXPR)
3042 val = SWITCH_COND (stmt);
3044 if (val && TREE_CODE (val) == INTEGER_CST && find_taken_edge (bb, val))
3045 cfg_altered = true;
3047 /* If we simplified a statement in such a way as to be shown that it
3048 cannot trap, update the eh information and the cfg to match. */
3049 if (maybe_clean_eh_stmt (stmt))
3051 bitmap_set_bit (need_eh_cleanup, bb->index);
3052 if (dump_file && (dump_flags & TDF_DETAILS))
3053 fprintf (dump_file, " Flagged to clear EH edges.\n");
3057 if (may_have_exposed_new_symbols)
3058 VARRAY_PUSH_TREE (stmts_to_rescan, bsi_stmt (si));
3061 /* Replace the RHS of STMT with NEW_RHS. If RHS can be found in the
3062 available expression hashtable, then return the LHS from the hash
3063 table.
3065 If INSERT is true, then we also update the available expression
3066 hash table to account for the changes made to STMT. */
3068 static tree
3069 update_rhs_and_lookup_avail_expr (tree stmt, tree new_rhs, bool insert)
3071 tree cached_lhs = NULL;
3073 /* Remove the old entry from the hash table. */
3074 if (insert)
3076 struct expr_hash_elt element;
3078 initialize_hash_element (stmt, NULL, &element);
3079 htab_remove_elt_with_hash (avail_exprs, &element, element.hash);
3082 /* Now update the RHS of the assignment. */
3083 TREE_OPERAND (stmt, 1) = new_rhs;
3085 /* Now lookup the updated statement in the hash table. */
3086 cached_lhs = lookup_avail_expr (stmt, insert);
3088 /* We have now called lookup_avail_expr twice with two different
3089 versions of this same statement, once in optimize_stmt, once here.
3091 We know the call in optimize_stmt did not find an existing entry
3092 in the hash table, so a new entry was created. At the same time
3093 this statement was pushed onto the BLOCK_AVAIL_EXPRS varray.
3095 If this call failed to find an existing entry on the hash table,
3096 then the new version of this statement was entered into the
3097 hash table. And this statement was pushed onto BLOCK_AVAIL_EXPR
3098 for the second time. So there are two copies on BLOCK_AVAIL_EXPRs
3100 If this call succeeded, we still have one copy of this statement
3101 on the BLOCK_AVAIL_EXPRs varray.
3103 For both cases, we need to pop the most recent entry off the
3104 BLOCK_AVAIL_EXPRs varray. For the case where we never found this
3105 statement in the hash tables, that will leave precisely one
3106 copy of this statement on BLOCK_AVAIL_EXPRs. For the case where
3107 we found a copy of this statement in the second hash table lookup
3108 we want _no_ copies of this statement in BLOCK_AVAIL_EXPRs. */
3109 if (insert)
3110 VARRAY_POP (avail_exprs_stack);
3112 /* And make sure we record the fact that we modified this
3113 statement. */
3114 modify_stmt (stmt);
3116 return cached_lhs;
3119 /* Search for an existing instance of STMT in the AVAIL_EXPRS table. If
3120 found, return its LHS. Otherwise insert STMT in the table and return
3121 NULL_TREE.
3123 Also, when an expression is first inserted in the AVAIL_EXPRS table, it
3124 is also added to the stack pointed by BLOCK_AVAIL_EXPRS_P, so that they
3125 can be removed when we finish processing this block and its children.
3127 NOTE: This function assumes that STMT is a MODIFY_EXPR node that
3128 contains no CALL_EXPR on its RHS and makes no volatile nor
3129 aliased references. */
3131 static tree
3132 lookup_avail_expr (tree stmt, bool insert)
3134 void **slot;
3135 tree lhs;
3136 tree temp;
3137 struct expr_hash_elt *element = xcalloc (sizeof (struct expr_hash_elt), 1);
3139 lhs = TREE_CODE (stmt) == MODIFY_EXPR ? TREE_OPERAND (stmt, 0) : NULL;
3141 initialize_hash_element (stmt, lhs, element);
3143 /* Don't bother remembering constant assignments and copy operations.
3144 Constants and copy operations are handled by the constant/copy propagator
3145 in optimize_stmt. */
3146 if (TREE_CODE (element->rhs) == SSA_NAME
3147 || is_gimple_min_invariant (element->rhs))
3149 free (element);
3150 return NULL_TREE;
3153 /* If this is an equality test against zero, see if we have recorded a
3154 nonzero value for the variable in question. */
3155 if ((TREE_CODE (element->rhs) == EQ_EXPR
3156 || TREE_CODE (element->rhs) == NE_EXPR)
3157 && TREE_CODE (TREE_OPERAND (element->rhs, 0)) == SSA_NAME
3158 && integer_zerop (TREE_OPERAND (element->rhs, 1)))
3160 int indx = SSA_NAME_VERSION (TREE_OPERAND (element->rhs, 0));
3162 if (bitmap_bit_p (nonzero_vars, indx))
3164 tree t = element->rhs;
3165 free (element);
3167 if (TREE_CODE (t) == EQ_EXPR)
3168 return boolean_false_node;
3169 else
3170 return boolean_true_node;
3174 /* Finally try to find the expression in the main expression hash table. */
3175 slot = htab_find_slot_with_hash (avail_exprs, element, element->hash,
3176 (insert ? INSERT : NO_INSERT));
3177 if (slot == NULL)
3179 free (element);
3180 return NULL_TREE;
3183 if (*slot == NULL)
3185 *slot = (void *) element;
3186 VARRAY_PUSH_TREE (avail_exprs_stack, stmt ? stmt : element->rhs);
3187 return NULL_TREE;
3190 /* Extract the LHS of the assignment so that it can be used as the current
3191 definition of another variable. */
3192 lhs = ((struct expr_hash_elt *)*slot)->lhs;
3194 /* See if the LHS appears in the CONST_AND_COPIES table. If it does, then
3195 use the value from the const_and_copies table. */
3196 if (TREE_CODE (lhs) == SSA_NAME)
3198 temp = SSA_NAME_VALUE (lhs);
3199 if (temp && TREE_CODE (temp) != VALUE_HANDLE)
3200 lhs = temp;
3203 free (element);
3204 return lhs;
3207 /* Given a condition COND, record into HI_P, LO_P and INVERTED_P the
3208 range of values that result in the conditional having a true value.
3210 Return true if we are successful in extracting a range from COND and
3211 false if we are unsuccessful. */
3213 static bool
3214 extract_range_from_cond (tree cond, tree *hi_p, tree *lo_p, int *inverted_p)
3216 tree op1 = TREE_OPERAND (cond, 1);
3217 tree high, low, type;
3218 int inverted;
3220 /* Experiments have shown that it's rarely, if ever useful to
3221 record ranges for enumerations. Presumably this is due to
3222 the fact that they're rarely used directly. They are typically
3223 cast into an integer type and used that way. */
3224 if (TREE_CODE (TREE_TYPE (op1)) != INTEGER_TYPE)
3225 return 0;
3227 type = TREE_TYPE (op1);
3229 switch (TREE_CODE (cond))
3231 case EQ_EXPR:
3232 high = low = op1;
3233 inverted = 0;
3234 break;
3236 case NE_EXPR:
3237 high = low = op1;
3238 inverted = 1;
3239 break;
3241 case GE_EXPR:
3242 low = op1;
3243 high = TYPE_MAX_VALUE (type);
3244 inverted = 0;
3245 break;
3247 case GT_EXPR:
3248 low = int_const_binop (PLUS_EXPR, op1, integer_one_node, 1);
3249 high = TYPE_MAX_VALUE (type);
3250 inverted = 0;
3251 break;
3253 case LE_EXPR:
3254 high = op1;
3255 low = TYPE_MIN_VALUE (type);
3256 inverted = 0;
3257 break;
3259 case LT_EXPR:
3260 high = int_const_binop (MINUS_EXPR, op1, integer_one_node, 1);
3261 low = TYPE_MIN_VALUE (type);
3262 inverted = 0;
3263 break;
3265 default:
3266 return 0;
3269 *hi_p = high;
3270 *lo_p = low;
3271 *inverted_p = inverted;
3272 return 1;
3275 /* Record a range created by COND for basic block BB. */
3277 static void
3278 record_range (tree cond, basic_block bb)
3280 enum tree_code code = TREE_CODE (cond);
3282 /* We explicitly ignore NE_EXPRs and all the unordered comparisons.
3283 They rarely allow for meaningful range optimizations and significantly
3284 complicate the implementation. */
3285 if ((code == LT_EXPR || code == LE_EXPR || code == GT_EXPR
3286 || code == GE_EXPR || code == EQ_EXPR)
3287 && TREE_CODE (TREE_TYPE (TREE_OPERAND (cond, 1))) == INTEGER_TYPE)
3289 struct vrp_hash_elt *vrp_hash_elt;
3290 struct vrp_element *element;
3291 varray_type *vrp_records_p;
3292 void **slot;
3295 vrp_hash_elt = xmalloc (sizeof (struct vrp_hash_elt));
3296 vrp_hash_elt->var = TREE_OPERAND (cond, 0);
3297 vrp_hash_elt->records = NULL;
3298 slot = htab_find_slot (vrp_data, vrp_hash_elt, INSERT);
3300 if (*slot == NULL)
3301 *slot = (void *) vrp_hash_elt;
3302 else
3303 free (vrp_hash_elt);
3305 vrp_hash_elt = (struct vrp_hash_elt *) *slot;
3306 vrp_records_p = &vrp_hash_elt->records;
3308 element = ggc_alloc (sizeof (struct vrp_element));
3309 element->low = NULL;
3310 element->high = NULL;
3311 element->cond = cond;
3312 element->bb = bb;
3314 if (*vrp_records_p == NULL)
3315 VARRAY_GENERIC_PTR_INIT (*vrp_records_p, 2, "vrp records");
3317 VARRAY_PUSH_GENERIC_PTR (*vrp_records_p, element);
3318 VARRAY_PUSH_TREE (vrp_variables_stack, TREE_OPERAND (cond, 0));
3322 /* Hashing and equality functions for VRP_DATA.
3324 Since this hash table is addressed by SSA_NAMEs, we can hash on
3325 their version number and equality can be determined with a
3326 pointer comparison. */
3328 static hashval_t
3329 vrp_hash (const void *p)
3331 tree var = ((struct vrp_hash_elt *)p)->var;
3333 return SSA_NAME_VERSION (var);
3336 static int
3337 vrp_eq (const void *p1, const void *p2)
3339 tree var1 = ((struct vrp_hash_elt *)p1)->var;
3340 tree var2 = ((struct vrp_hash_elt *)p2)->var;
3342 return var1 == var2;
3345 /* Hashing and equality functions for AVAIL_EXPRS. The table stores
3346 MODIFY_EXPR statements. We compute a value number for expressions using
3347 the code of the expression and the SSA numbers of its operands. */
3349 static hashval_t
3350 avail_expr_hash (const void *p)
3352 stmt_ann_t ann = ((struct expr_hash_elt *)p)->ann;
3353 tree rhs = ((struct expr_hash_elt *)p)->rhs;
3354 hashval_t val = 0;
3355 size_t i;
3356 vuse_optype vuses;
3358 /* iterative_hash_expr knows how to deal with any expression and
3359 deals with commutative operators as well, so just use it instead
3360 of duplicating such complexities here. */
3361 val = iterative_hash_expr (rhs, val);
3363 /* If the hash table entry is not associated with a statement, then we
3364 can just hash the expression and not worry about virtual operands
3365 and such. */
3366 if (!ann)
3367 return val;
3369 /* Add the SSA version numbers of every vuse operand. This is important
3370 because compound variables like arrays are not renamed in the
3371 operands. Rather, the rename is done on the virtual variable
3372 representing all the elements of the array. */
3373 vuses = VUSE_OPS (ann);
3374 for (i = 0; i < NUM_VUSES (vuses); i++)
3375 val = iterative_hash_expr (VUSE_OP (vuses, i), val);
3377 return val;
3380 static hashval_t
3381 real_avail_expr_hash (const void *p)
3383 return ((const struct expr_hash_elt *)p)->hash;
3386 static int
3387 avail_expr_eq (const void *p1, const void *p2)
3389 stmt_ann_t ann1 = ((struct expr_hash_elt *)p1)->ann;
3390 tree rhs1 = ((struct expr_hash_elt *)p1)->rhs;
3391 stmt_ann_t ann2 = ((struct expr_hash_elt *)p2)->ann;
3392 tree rhs2 = ((struct expr_hash_elt *)p2)->rhs;
3394 /* If they are the same physical expression, return true. */
3395 if (rhs1 == rhs2 && ann1 == ann2)
3396 return true;
3398 /* If their codes are not equal, then quit now. */
3399 if (TREE_CODE (rhs1) != TREE_CODE (rhs2))
3400 return false;
3402 /* In case of a collision, both RHS have to be identical and have the
3403 same VUSE operands. */
3404 if ((TREE_TYPE (rhs1) == TREE_TYPE (rhs2)
3405 || lang_hooks.types_compatible_p (TREE_TYPE (rhs1), TREE_TYPE (rhs2)))
3406 && operand_equal_p (rhs1, rhs2, OEP_PURE_SAME))
3408 vuse_optype ops1 = NULL;
3409 vuse_optype ops2 = NULL;
3410 size_t num_ops1 = 0;
3411 size_t num_ops2 = 0;
3412 size_t i;
3414 if (ann1)
3416 ops1 = VUSE_OPS (ann1);
3417 num_ops1 = NUM_VUSES (ops1);
3420 if (ann2)
3422 ops2 = VUSE_OPS (ann2);
3423 num_ops2 = NUM_VUSES (ops2);
3426 /* If the number of virtual uses is different, then we consider
3427 them not equal. */
3428 if (num_ops1 != num_ops2)
3429 return false;
3431 for (i = 0; i < num_ops1; i++)
3432 if (VUSE_OP (ops1, i) != VUSE_OP (ops2, i))
3433 return false;
3435 gcc_assert (((struct expr_hash_elt *)p1)->hash
3436 == ((struct expr_hash_elt *)p2)->hash);
3437 return true;
3440 return false;
3443 /* Given STMT and a pointer to the block local definitions BLOCK_DEFS_P,
3444 register register all objects set by this statement into BLOCK_DEFS_P
3445 and CURRDEFS. */
3447 static void
3448 register_definitions_for_stmt (tree stmt)
3450 tree def;
3451 ssa_op_iter iter;
3453 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
3456 /* FIXME: We shouldn't be registering new defs if the variable
3457 doesn't need to be renamed. */
3458 register_new_def (def, &block_defs_stack);