gcc:
[official-gcc.git] / gcc / tree-ssa-dom.c
blobb37df7797f888fcf6a526518ef4e8302ed8ae738
1 /* SSA Dominator optimizations for trees
2 Copyright (C) 2001, 2002, 2003, 2004, 2005 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, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, 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 "cfgloop.h"
33 #include "output.h"
34 #include "expr.h"
35 #include "function.h"
36 #include "diagnostic.h"
37 #include "timevar.h"
38 #include "tree-dump.h"
39 #include "tree-flow.h"
40 #include "domwalk.h"
41 #include "real.h"
42 #include "tree-pass.h"
43 #include "tree-ssa-propagate.h"
44 #include "langhooks.h"
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 VEC(tree,heap) *avail_exprs_stack;
98 /* Stack of statements we need to rescan during finalization for newly
99 exposed variables.
101 Statement rescanning must occur after the current block's available
102 expressions are removed from AVAIL_EXPRS. Else we may change the
103 hash code for an expression and be unable to find/remove it from
104 AVAIL_EXPRS. */
105 static VEC(tree,heap) *stmts_to_rescan;
107 /* Structure for entries in the expression hash table.
109 This requires more memory for the hash table entries, but allows us
110 to avoid creating silly tree nodes and annotations for conditionals,
111 eliminates 2 global hash tables and two block local varrays.
113 It also allows us to reduce the number of hash table lookups we
114 have to perform in lookup_avail_expr and finally it allows us to
115 significantly reduce the number of calls into the hashing routine
116 itself. */
118 struct expr_hash_elt
120 /* The value (lhs) of this expression. */
121 tree lhs;
123 /* The expression (rhs) we want to record. */
124 tree rhs;
126 /* The stmt pointer if this element corresponds to a statement. */
127 tree stmt;
129 /* The hash value for RHS/ann. */
130 hashval_t hash;
133 /* Stack of dest,src pairs that need to be restored during finalization.
135 A NULL entry is used to mark the end of pairs which need to be
136 restored during finalization of this block. */
137 static VEC(tree,heap) *const_and_copies_stack;
139 /* Bitmap of SSA_NAMEs known to have a nonzero value, even if we do not
140 know their exact value. */
141 static bitmap nonzero_vars;
143 /* Bitmap of blocks that are scheduled to be threaded through. This
144 is used to communicate with thread_through_blocks. */
145 static bitmap threaded_blocks;
147 /* Stack of SSA_NAMEs which need their NONZERO_VARS property cleared
148 when the current block is finalized.
150 A NULL entry is used to mark the end of names needing their
151 entry in NONZERO_VARS cleared during finalization of this block. */
152 static VEC(tree,heap) *nonzero_vars_stack;
154 /* Track whether or not we have changed the control flow graph. */
155 static bool cfg_altered;
157 /* Bitmap of blocks that have had EH statements cleaned. We should
158 remove their dead edges eventually. */
159 static bitmap need_eh_cleanup;
161 /* Statistics for dominator optimizations. */
162 struct opt_stats_d
164 long num_stmts;
165 long num_exprs_considered;
166 long num_re;
167 long num_const_prop;
168 long num_copy_prop;
169 long num_iterations;
172 static struct opt_stats_d opt_stats;
174 /* Value range propagation record. Each time we encounter a conditional
175 of the form SSA_NAME COND CONST we create a new vrp_element to record
176 how the condition affects the possible values SSA_NAME may have.
178 Each record contains the condition tested (COND), and the range of
179 values the variable may legitimately have if COND is true. Note the
180 range of values may be a smaller range than COND specifies if we have
181 recorded other ranges for this variable. Each record also contains the
182 block in which the range was recorded for invalidation purposes.
184 Note that the current known range is computed lazily. This allows us
185 to avoid the overhead of computing ranges which are never queried.
187 When we encounter a conditional, we look for records which constrain
188 the SSA_NAME used in the condition. In some cases those records allow
189 us to determine the condition's result at compile time. In other cases
190 they may allow us to simplify the condition.
192 We also use value ranges to do things like transform signed div/mod
193 operations into unsigned div/mod or to simplify ABS_EXPRs.
195 Simple experiments have shown these optimizations to not be all that
196 useful on switch statements (much to my surprise). So switch statement
197 optimizations are not performed.
199 Note carefully we do not propagate information through each statement
200 in the block. i.e., if we know variable X has a value defined of
201 [0, 25] and we encounter Y = X + 1, we do not track a value range
202 for Y (which would be [1, 26] if we cared). Similarly we do not
203 constrain values as we encounter narrowing typecasts, etc. */
205 struct vrp_element
207 /* The highest and lowest values the variable in COND may contain when
208 COND is true. Note this may not necessarily be the same values
209 tested by COND if the same variable was used in earlier conditionals.
211 Note this is computed lazily and thus can be NULL indicating that
212 the values have not been computed yet. */
213 tree low;
214 tree high;
216 /* The actual conditional we recorded. This is needed since we compute
217 ranges lazily. */
218 tree cond;
220 /* The basic block where this record was created. We use this to determine
221 when to remove records. */
222 basic_block bb;
225 /* A hash table holding value range records (VRP_ELEMENTs) for a given
226 SSA_NAME. We used to use a varray indexed by SSA_NAME_VERSION, but
227 that gets awful wasteful, particularly since the density objects
228 with useful information is very low. */
229 static htab_t vrp_data;
231 typedef struct vrp_element *vrp_element_p;
233 DEF_VEC_P(vrp_element_p);
234 DEF_VEC_ALLOC_P(vrp_element_p,heap);
236 /* An entry in the VRP_DATA hash table. We record the variable and a
237 varray of VRP_ELEMENT records associated with that variable. */
238 struct vrp_hash_elt
240 tree var;
241 VEC(vrp_element_p,heap) *records;
244 /* Array of variables which have their values constrained by operations
245 in this basic block. We use this during finalization to know
246 which variables need their VRP data updated. */
248 /* Stack of SSA_NAMEs which had their values constrained by operations
249 in this basic block. During finalization of this block we use this
250 list to determine which variables need their VRP data updated.
252 A NULL entry marks the end of the SSA_NAMEs associated with this block. */
253 static VEC(tree,heap) *vrp_variables_stack;
255 struct eq_expr_value
257 tree src;
258 tree dst;
261 /* Local functions. */
262 static void optimize_stmt (struct dom_walk_data *,
263 basic_block bb,
264 block_stmt_iterator);
265 static tree lookup_avail_expr (tree, bool);
266 static hashval_t vrp_hash (const void *);
267 static int vrp_eq (const void *, const void *);
268 static hashval_t avail_expr_hash (const void *);
269 static hashval_t real_avail_expr_hash (const void *);
270 static int avail_expr_eq (const void *, const void *);
271 static void htab_statistics (FILE *, htab_t);
272 static void record_cond (tree, tree);
273 static void record_const_or_copy (tree, tree);
274 static void record_equality (tree, tree);
275 static tree update_rhs_and_lookup_avail_expr (tree, tree, bool);
276 static tree simplify_rhs_and_lookup_avail_expr (tree, int);
277 static tree simplify_cond_and_lookup_avail_expr (tree, stmt_ann_t, int);
278 static tree simplify_switch_and_lookup_avail_expr (tree, int);
279 static tree find_equivalent_equality_comparison (tree);
280 static void record_range (tree, basic_block);
281 static bool extract_range_from_cond (tree, tree *, tree *, int *);
282 static void record_equivalences_from_phis (basic_block);
283 static void record_equivalences_from_incoming_edge (basic_block);
284 static bool eliminate_redundant_computations (tree, stmt_ann_t);
285 static void record_equivalences_from_stmt (tree, int, stmt_ann_t);
286 static void thread_across_edge (struct dom_walk_data *, edge);
287 static void dom_opt_finalize_block (struct dom_walk_data *, basic_block);
288 static void dom_opt_initialize_block (struct dom_walk_data *, basic_block);
289 static void propagate_to_outgoing_edges (struct dom_walk_data *, basic_block);
290 static void remove_local_expressions_from_table (void);
291 static void restore_vars_to_original_value (void);
292 static edge single_incoming_edge_ignoring_loop_edges (basic_block);
293 static void restore_nonzero_vars_to_original_value (void);
294 static inline bool unsafe_associative_fp_binop (tree);
297 /* Local version of fold that doesn't introduce cruft. */
299 static tree
300 local_fold (tree t)
302 t = fold (t);
304 /* Strip away useless type conversions. Both the NON_LVALUE_EXPR that
305 may have been added by fold, and "useless" type conversions that might
306 now be apparent due to propagation. */
307 STRIP_USELESS_TYPE_CONVERSION (t);
309 return t;
312 /* Allocate an EDGE_INFO for edge E and attach it to E.
313 Return the new EDGE_INFO structure. */
315 static struct edge_info *
316 allocate_edge_info (edge e)
318 struct edge_info *edge_info;
320 edge_info = xcalloc (1, sizeof (struct edge_info));
322 e->aux = edge_info;
323 return edge_info;
326 /* Free all EDGE_INFO structures associated with edges in the CFG.
327 If a particular edge can be threaded, copy the redirection
328 target from the EDGE_INFO structure into the edge's AUX field
329 as required by code to update the CFG and SSA graph for
330 jump threading. */
332 static void
333 free_all_edge_infos (void)
335 basic_block bb;
336 edge_iterator ei;
337 edge e;
339 FOR_EACH_BB (bb)
341 FOR_EACH_EDGE (e, ei, bb->preds)
343 struct edge_info *edge_info = e->aux;
345 if (edge_info)
347 e->aux = edge_info->redirection_target;
348 if (edge_info->cond_equivalences)
349 free (edge_info->cond_equivalences);
350 free (edge_info);
356 /* Free an instance of vrp_hash_elt. */
358 static void
359 vrp_free (void *data)
361 struct vrp_hash_elt *elt = data;
362 struct VEC(vrp_element_p,heap) **vrp_elt = &elt->records;
364 VEC_free (vrp_element_p, heap, *vrp_elt);
365 free (elt);
368 /* Jump threading, redundancy elimination and const/copy propagation.
370 This pass may expose new symbols that need to be renamed into SSA. For
371 every new symbol exposed, its corresponding bit will be set in
372 VARS_TO_RENAME. */
374 static void
375 tree_ssa_dominator_optimize (void)
377 struct dom_walk_data walk_data;
378 unsigned int i;
379 struct loops loops_info;
381 memset (&opt_stats, 0, sizeof (opt_stats));
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,
386 vrp_free);
387 avail_exprs_stack = VEC_alloc (tree, heap, 20);
388 const_and_copies_stack = VEC_alloc (tree, heap, 20);
389 nonzero_vars_stack = VEC_alloc (tree, heap, 20);
390 vrp_variables_stack = VEC_alloc (tree, heap, 20);
391 stmts_to_rescan = VEC_alloc (tree, heap, 20);
392 nonzero_vars = BITMAP_ALLOC (NULL);
393 threaded_blocks = BITMAP_ALLOC (NULL);
394 need_eh_cleanup = BITMAP_ALLOC (NULL);
396 /* Setup callbacks for the generic dominator tree walker. */
397 walk_data.walk_stmts_backward = false;
398 walk_data.dom_direction = CDI_DOMINATORS;
399 walk_data.initialize_block_local_data = NULL;
400 walk_data.before_dom_children_before_stmts = dom_opt_initialize_block;
401 walk_data.before_dom_children_walk_stmts = optimize_stmt;
402 walk_data.before_dom_children_after_stmts = propagate_to_outgoing_edges;
403 walk_data.after_dom_children_before_stmts = NULL;
404 walk_data.after_dom_children_walk_stmts = NULL;
405 walk_data.after_dom_children_after_stmts = dom_opt_finalize_block;
406 /* Right now we only attach a dummy COND_EXPR to the global data pointer.
407 When we attach more stuff we'll need to fill this out with a real
408 structure. */
409 walk_data.global_data = NULL;
410 walk_data.block_local_data_size = 0;
411 walk_data.interesting_blocks = NULL;
413 /* Now initialize the dominator walker. */
414 init_walk_dominator_tree (&walk_data);
416 calculate_dominance_info (CDI_DOMINATORS);
418 /* We need to know which edges exit loops so that we can
419 aggressively thread through loop headers to an exit
420 edge. */
421 flow_loops_find (&loops_info);
422 mark_loop_exit_edges (&loops_info);
423 flow_loops_free (&loops_info);
425 /* Clean up the CFG so that any forwarder blocks created by loop
426 canonicalization are removed. */
427 cleanup_tree_cfg ();
428 calculate_dominance_info (CDI_DOMINATORS);
430 /* If we prove certain blocks are unreachable, then we want to
431 repeat the dominator optimization process as PHI nodes may
432 have turned into copies which allows better propagation of
433 values. So we repeat until we do not identify any new unreachable
434 blocks. */
437 /* Optimize the dominator tree. */
438 cfg_altered = false;
440 /* We need accurate information regarding back edges in the CFG
441 for jump threading. */
442 mark_dfs_back_edges ();
444 /* Recursively walk the dominator tree optimizing statements. */
445 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
448 block_stmt_iterator bsi;
449 basic_block bb;
450 FOR_EACH_BB (bb)
452 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
454 update_stmt_if_modified (bsi_stmt (bsi));
459 /* If we exposed any new variables, go ahead and put them into
460 SSA form now, before we handle jump threading. This simplifies
461 interactions between rewriting of _DECL nodes into SSA form
462 and rewriting SSA_NAME nodes into SSA form after block
463 duplication and CFG manipulation. */
464 update_ssa (TODO_update_ssa);
466 free_all_edge_infos ();
468 /* Thread jumps, creating duplicate blocks as needed. */
469 cfg_altered |= thread_through_all_blocks (threaded_blocks);
471 /* Removal of statements may make some EH edges dead. Purge
472 such edges from the CFG as needed. */
473 if (!bitmap_empty_p (need_eh_cleanup))
475 cfg_altered |= tree_purge_all_dead_eh_edges (need_eh_cleanup);
476 bitmap_zero (need_eh_cleanup);
479 if (cfg_altered)
480 free_dominance_info (CDI_DOMINATORS);
482 /* Only iterate if we threaded jumps AND the CFG cleanup did
483 something interesting. Other cases generate far fewer
484 optimization opportunities and thus are not worth another
485 full DOM iteration. */
486 cfg_altered &= cleanup_tree_cfg ();
488 if (rediscover_loops_after_threading)
490 /* Rerun basic loop analysis to discover any newly
491 created loops and update the set of exit edges. */
492 rediscover_loops_after_threading = false;
493 flow_loops_find (&loops_info);
494 mark_loop_exit_edges (&loops_info);
495 flow_loops_free (&loops_info);
497 /* Remove any forwarder blocks inserted by loop
498 header canonicalization. */
499 cleanup_tree_cfg ();
502 calculate_dominance_info (CDI_DOMINATORS);
504 update_ssa (TODO_update_ssa);
506 /* Reinitialize the various tables. */
507 bitmap_clear (nonzero_vars);
508 bitmap_clear (threaded_blocks);
509 htab_empty (avail_exprs);
510 htab_empty (vrp_data);
512 /* Finally, remove everything except invariants in SSA_NAME_VALUE.
514 This must be done before we iterate as we might have a
515 reference to an SSA_NAME which was removed by the call to
516 update_ssa.
518 Long term we will be able to let everything in SSA_NAME_VALUE
519 persist. However, for now, we know this is the safe thing to do. */
520 for (i = 0; i < num_ssa_names; i++)
522 tree name = ssa_name (i);
523 tree value;
525 if (!name)
526 continue;
528 value = SSA_NAME_VALUE (name);
529 if (value && !is_gimple_min_invariant (value))
530 SSA_NAME_VALUE (name) = NULL;
533 opt_stats.num_iterations++;
535 while (optimize > 1 && cfg_altered);
537 /* Debugging dumps. */
538 if (dump_file && (dump_flags & TDF_STATS))
539 dump_dominator_optimization_stats (dump_file);
541 /* We emptied the hash table earlier, now delete it completely. */
542 htab_delete (avail_exprs);
543 htab_delete (vrp_data);
545 /* It is not necessary to clear CURRDEFS, REDIRECTION_EDGES, VRP_DATA,
546 CONST_AND_COPIES, and NONZERO_VARS as they all get cleared at the bottom
547 of the do-while loop above. */
549 /* And finalize the dominator walker. */
550 fini_walk_dominator_tree (&walk_data);
552 /* Free nonzero_vars. */
553 BITMAP_FREE (nonzero_vars);
554 BITMAP_FREE (threaded_blocks);
555 BITMAP_FREE (need_eh_cleanup);
557 VEC_free (tree, heap, avail_exprs_stack);
558 VEC_free (tree, heap, const_and_copies_stack);
559 VEC_free (tree, heap, nonzero_vars_stack);
560 VEC_free (tree, heap, vrp_variables_stack);
561 VEC_free (tree, heap, stmts_to_rescan);
564 static bool
565 gate_dominator (void)
567 return flag_tree_dom != 0;
570 struct tree_opt_pass pass_dominator =
572 "dom", /* name */
573 gate_dominator, /* gate */
574 tree_ssa_dominator_optimize, /* execute */
575 NULL, /* sub */
576 NULL, /* next */
577 0, /* static_pass_number */
578 TV_TREE_SSA_DOMINATOR_OPTS, /* tv_id */
579 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
580 0, /* properties_provided */
581 0, /* properties_destroyed */
582 0, /* todo_flags_start */
583 TODO_dump_func
584 | TODO_update_ssa
585 | TODO_verify_ssa, /* todo_flags_finish */
586 0 /* letter */
590 /* We are exiting E->src, see if E->dest ends with a conditional
591 jump which has a known value when reached via E.
593 Special care is necessary if E is a back edge in the CFG as we
594 will have already recorded equivalences for E->dest into our
595 various tables, including the result of the conditional at
596 the end of E->dest. Threading opportunities are severely
597 limited in that case to avoid short-circuiting the loop
598 incorrectly.
600 Note it is quite common for the first block inside a loop to
601 end with a conditional which is either always true or always
602 false when reached via the loop backedge. Thus we do not want
603 to blindly disable threading across a loop backedge. */
605 static void
606 thread_across_edge (struct dom_walk_data *walk_data, edge e)
608 block_stmt_iterator bsi;
609 tree stmt = NULL;
610 tree phi;
612 /* If E->dest does not end with a conditional, then there is
613 nothing to do. */
614 bsi = bsi_last (e->dest);
615 if (bsi_end_p (bsi)
616 || ! bsi_stmt (bsi)
617 || (TREE_CODE (bsi_stmt (bsi)) != COND_EXPR
618 && TREE_CODE (bsi_stmt (bsi)) != GOTO_EXPR
619 && TREE_CODE (bsi_stmt (bsi)) != SWITCH_EXPR))
620 return;
622 /* The basic idea here is to use whatever knowledge we have
623 from our dominator walk to simplify statements in E->dest,
624 with the ultimate goal being to simplify the conditional
625 at the end of E->dest.
627 Note that we must undo any changes we make to the underlying
628 statements as the simplifications we are making are control
629 flow sensitive (ie, the simplifications are valid when we
630 traverse E, but may not be valid on other paths to E->dest. */
632 /* Each PHI creates a temporary equivalence, record them. Again
633 these are context sensitive equivalences and will be removed
634 by our caller. */
635 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
637 tree src = PHI_ARG_DEF_FROM_EDGE (phi, e);
638 tree dst = PHI_RESULT (phi);
640 /* If the desired argument is not the same as this PHI's result
641 and it is set by a PHI in E->dest, then we can not thread
642 through E->dest. */
643 if (src != dst
644 && TREE_CODE (src) == SSA_NAME
645 && TREE_CODE (SSA_NAME_DEF_STMT (src)) == PHI_NODE
646 && bb_for_stmt (SSA_NAME_DEF_STMT (src)) == e->dest)
647 return;
649 record_const_or_copy (dst, src);
652 /* Try to simplify each statement in E->dest, ultimately leading to
653 a simplification of the COND_EXPR at the end of E->dest.
655 We might consider marking just those statements which ultimately
656 feed the COND_EXPR. It's not clear if the overhead of bookkeeping
657 would be recovered by trying to simplify fewer statements.
659 If we are able to simplify a statement into the form
660 SSA_NAME = (SSA_NAME | gimple invariant), then we can record
661 a context sensitive equivalency which may help us simplify
662 later statements in E->dest.
664 Failure to simplify into the form above merely means that the
665 statement provides no equivalences to help simplify later
666 statements. This does not prevent threading through E->dest. */
667 for (bsi = bsi_start (e->dest); ! bsi_end_p (bsi); bsi_next (&bsi))
669 tree cached_lhs = NULL;
671 stmt = bsi_stmt (bsi);
673 /* Ignore empty statements and labels. */
674 if (IS_EMPTY_STMT (stmt) || TREE_CODE (stmt) == LABEL_EXPR)
675 continue;
677 /* Safely handle threading across loop backedges. This is
678 over conservative, but still allows us to capture the
679 majority of the cases where we can thread across a loop
680 backedge. */
681 if ((e->flags & EDGE_DFS_BACK) != 0
682 && TREE_CODE (stmt) != COND_EXPR
683 && TREE_CODE (stmt) != SWITCH_EXPR)
684 return;
686 /* If the statement has volatile operands, then we assume we
687 can not thread through this block. This is overly
688 conservative in some ways. */
689 if (TREE_CODE (stmt) == ASM_EXPR && ASM_VOLATILE_P (stmt))
690 return;
692 /* If this is not a MODIFY_EXPR which sets an SSA_NAME to a new
693 value, then do not try to simplify this statement as it will
694 not simplify in any way that is helpful for jump threading. */
695 if (TREE_CODE (stmt) != MODIFY_EXPR
696 || TREE_CODE (TREE_OPERAND (stmt, 0)) != SSA_NAME)
697 continue;
699 /* At this point we have a statement which assigns an RHS to an
700 SSA_VAR on the LHS. We want to try and simplify this statement
701 to expose more context sensitive equivalences which in turn may
702 allow us to simplify the condition at the end of the loop. */
703 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == SSA_NAME)
704 cached_lhs = TREE_OPERAND (stmt, 1);
705 else
707 /* Copy the operands. */
708 tree *copy, pre_fold_expr;
709 ssa_op_iter iter;
710 use_operand_p use_p;
711 unsigned int num, i = 0;
713 num = NUM_SSA_OPERANDS (stmt, (SSA_OP_USE | SSA_OP_VUSE));
714 copy = xcalloc (num, sizeof (tree));
716 /* Make a copy of the uses & vuses into USES_COPY, then cprop into
717 the operands. */
718 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE | SSA_OP_VUSE)
720 tree tmp = NULL;
721 tree use = USE_FROM_PTR (use_p);
723 copy[i++] = use;
724 if (TREE_CODE (use) == SSA_NAME)
725 tmp = SSA_NAME_VALUE (use);
726 if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
727 SET_USE (use_p, tmp);
730 /* Try to fold/lookup the new expression. Inserting the
731 expression into the hash table is unlikely to help
732 Sadly, we have to handle conditional assignments specially
733 here, because fold expects all the operands of an expression
734 to be folded before the expression itself is folded, but we
735 can't just substitute the folded condition here. */
736 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == COND_EXPR)
738 tree cond = COND_EXPR_COND (TREE_OPERAND (stmt, 1));
739 cond = fold (cond);
740 if (cond == boolean_true_node)
741 pre_fold_expr = COND_EXPR_THEN (TREE_OPERAND (stmt, 1));
742 else if (cond == boolean_false_node)
743 pre_fold_expr = COND_EXPR_ELSE (TREE_OPERAND (stmt, 1));
744 else
745 pre_fold_expr = TREE_OPERAND (stmt, 1);
747 else
748 pre_fold_expr = TREE_OPERAND (stmt, 1);
750 if (pre_fold_expr)
752 cached_lhs = fold (pre_fold_expr);
753 if (TREE_CODE (cached_lhs) != SSA_NAME
754 && !is_gimple_min_invariant (cached_lhs))
755 cached_lhs = lookup_avail_expr (stmt, false);
758 /* Restore the statement's original uses/defs. */
759 i = 0;
760 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE | SSA_OP_VUSE)
761 SET_USE (use_p, copy[i++]);
763 free (copy);
766 /* Record the context sensitive equivalence if we were able
767 to simplify this statement. */
768 if (cached_lhs
769 && (TREE_CODE (cached_lhs) == SSA_NAME
770 || is_gimple_min_invariant (cached_lhs)))
771 record_const_or_copy (TREE_OPERAND (stmt, 0), cached_lhs);
774 /* If we stopped at a COND_EXPR or SWITCH_EXPR, see if we know which arm
775 will be taken. */
776 if (stmt
777 && (TREE_CODE (stmt) == COND_EXPR
778 || TREE_CODE (stmt) == GOTO_EXPR
779 || TREE_CODE (stmt) == SWITCH_EXPR))
781 tree cond, cached_lhs;
783 /* Now temporarily cprop the operands and try to find the resulting
784 expression in the hash tables. */
785 if (TREE_CODE (stmt) == COND_EXPR)
786 cond = COND_EXPR_COND (stmt);
787 else if (TREE_CODE (stmt) == GOTO_EXPR)
788 cond = GOTO_DESTINATION (stmt);
789 else
790 cond = SWITCH_COND (stmt);
792 if (COMPARISON_CLASS_P (cond))
794 tree dummy_cond, op0, op1;
795 enum tree_code cond_code;
797 op0 = TREE_OPERAND (cond, 0);
798 op1 = TREE_OPERAND (cond, 1);
799 cond_code = TREE_CODE (cond);
801 /* Get the current value of both operands. */
802 if (TREE_CODE (op0) == SSA_NAME)
804 tree tmp = SSA_NAME_VALUE (op0);
805 if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
806 op0 = tmp;
809 if (TREE_CODE (op1) == SSA_NAME)
811 tree tmp = SSA_NAME_VALUE (op1);
812 if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
813 op1 = tmp;
816 /* Stuff the operator and operands into our dummy conditional
817 expression, creating the dummy conditional if necessary. */
818 dummy_cond = walk_data->global_data;
819 if (! dummy_cond)
821 dummy_cond = build (cond_code, boolean_type_node, op0, op1);
822 dummy_cond = build (COND_EXPR, void_type_node,
823 dummy_cond, NULL, NULL);
824 walk_data->global_data = dummy_cond;
826 else
828 TREE_SET_CODE (COND_EXPR_COND (dummy_cond), cond_code);
829 TREE_OPERAND (COND_EXPR_COND (dummy_cond), 0) = op0;
830 TREE_OPERAND (COND_EXPR_COND (dummy_cond), 1) = op1;
833 /* If the conditional folds to an invariant, then we are done,
834 otherwise look it up in the hash tables. */
835 cached_lhs = local_fold (COND_EXPR_COND (dummy_cond));
836 if (! is_gimple_min_invariant (cached_lhs))
838 cached_lhs = lookup_avail_expr (dummy_cond, false);
839 if (!cached_lhs || ! is_gimple_min_invariant (cached_lhs))
840 cached_lhs = simplify_cond_and_lookup_avail_expr (dummy_cond,
841 NULL,
842 false);
845 /* We can have conditionals which just test the state of a
846 variable rather than use a relational operator. These are
847 simpler to handle. */
848 else if (TREE_CODE (cond) == SSA_NAME)
850 cached_lhs = cond;
851 cached_lhs = SSA_NAME_VALUE (cached_lhs);
852 if (cached_lhs && ! is_gimple_min_invariant (cached_lhs))
853 cached_lhs = NULL;
855 else
856 cached_lhs = lookup_avail_expr (stmt, false);
858 if (cached_lhs)
860 edge taken_edge = find_taken_edge (e->dest, cached_lhs);
861 basic_block dest = (taken_edge ? taken_edge->dest : NULL);
863 if (dest == e->dest)
864 return;
866 /* If we have a known destination for the conditional, then
867 we can perform this optimization, which saves at least one
868 conditional jump each time it applies since we get to
869 bypass the conditional at our original destination. */
870 if (dest)
872 struct edge_info *edge_info;
874 if (e->aux)
875 edge_info = e->aux;
876 else
877 edge_info = allocate_edge_info (e);
878 edge_info->redirection_target = taken_edge;
879 bitmap_set_bit (threaded_blocks, e->dest->index);
886 /* Initialize local stacks for this optimizer and record equivalences
887 upon entry to BB. Equivalences can come from the edge traversed to
888 reach BB or they may come from PHI nodes at the start of BB. */
890 static void
891 dom_opt_initialize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
892 basic_block bb)
894 if (dump_file && (dump_flags & TDF_DETAILS))
895 fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
897 /* Push a marker on the stacks of local information so that we know how
898 far to unwind when we finalize this block. */
899 VEC_safe_push (tree, heap, avail_exprs_stack, NULL_TREE);
900 VEC_safe_push (tree, heap, const_and_copies_stack, NULL_TREE);
901 VEC_safe_push (tree, heap, nonzero_vars_stack, NULL_TREE);
902 VEC_safe_push (tree, heap, vrp_variables_stack, NULL_TREE);
904 record_equivalences_from_incoming_edge (bb);
906 /* PHI nodes can create equivalences too. */
907 record_equivalences_from_phis (bb);
910 /* Given an expression EXPR (a relational expression or a statement),
911 initialize the hash table element pointed to by ELEMENT. */
913 static void
914 initialize_hash_element (tree expr, tree lhs, struct expr_hash_elt *element)
916 /* Hash table elements may be based on conditional expressions or statements.
918 For the former case, we have no annotation and we want to hash the
919 conditional expression. In the latter case we have an annotation and
920 we want to record the expression the statement evaluates. */
921 if (COMPARISON_CLASS_P (expr) || TREE_CODE (expr) == TRUTH_NOT_EXPR)
923 element->stmt = NULL;
924 element->rhs = expr;
926 else if (TREE_CODE (expr) == COND_EXPR)
928 element->stmt = expr;
929 element->rhs = COND_EXPR_COND (expr);
931 else if (TREE_CODE (expr) == SWITCH_EXPR)
933 element->stmt = expr;
934 element->rhs = SWITCH_COND (expr);
936 else if (TREE_CODE (expr) == RETURN_EXPR && TREE_OPERAND (expr, 0))
938 element->stmt = expr;
939 element->rhs = TREE_OPERAND (TREE_OPERAND (expr, 0), 1);
941 else if (TREE_CODE (expr) == GOTO_EXPR)
943 element->stmt = expr;
944 element->rhs = GOTO_DESTINATION (expr);
946 else
948 element->stmt = expr;
949 element->rhs = TREE_OPERAND (expr, 1);
952 element->lhs = lhs;
953 element->hash = avail_expr_hash (element);
956 /* Remove all the expressions in LOCALS from TABLE, stopping when there are
957 LIMIT entries left in LOCALs. */
959 static void
960 remove_local_expressions_from_table (void)
962 /* Remove all the expressions made available in this block. */
963 while (VEC_length (tree, avail_exprs_stack) > 0)
965 struct expr_hash_elt element;
966 tree expr = VEC_pop (tree, avail_exprs_stack);
968 if (expr == NULL_TREE)
969 break;
971 initialize_hash_element (expr, NULL, &element);
972 htab_remove_elt_with_hash (avail_exprs, &element, element.hash);
976 /* Use the SSA_NAMES in LOCALS to restore TABLE to its original
977 state, stopping when there are LIMIT entries left in LOCALs. */
979 static void
980 restore_nonzero_vars_to_original_value (void)
982 while (VEC_length (tree, nonzero_vars_stack) > 0)
984 tree name = VEC_pop (tree, nonzero_vars_stack);
986 if (name == NULL)
987 break;
989 bitmap_clear_bit (nonzero_vars, SSA_NAME_VERSION (name));
993 /* Use the source/dest pairs in CONST_AND_COPIES_STACK to restore
994 CONST_AND_COPIES to its original state, stopping when we hit a
995 NULL marker. */
997 static void
998 restore_vars_to_original_value (void)
1000 while (VEC_length (tree, const_and_copies_stack) > 0)
1002 tree prev_value, dest;
1004 dest = VEC_pop (tree, const_and_copies_stack);
1006 if (dest == NULL)
1007 break;
1009 prev_value = VEC_pop (tree, const_and_copies_stack);
1010 SSA_NAME_VALUE (dest) = prev_value;
1014 /* We have finished processing the dominator children of BB, perform
1015 any finalization actions in preparation for leaving this node in
1016 the dominator tree. */
1018 static void
1019 dom_opt_finalize_block (struct dom_walk_data *walk_data, basic_block bb)
1021 tree last;
1023 /* If we have an outgoing edge to a block with multiple incoming and
1024 outgoing edges, then we may be able to thread the edge. ie, we
1025 may be able to statically determine which of the outgoing edges
1026 will be traversed when the incoming edge from BB is traversed. */
1027 if (single_succ_p (bb)
1028 && (single_succ_edge (bb)->flags & EDGE_ABNORMAL) == 0
1029 && !single_pred_p (single_succ (bb))
1030 && !single_succ_p (single_succ (bb)))
1033 thread_across_edge (walk_data, single_succ_edge (bb));
1035 else if ((last = last_stmt (bb))
1036 && TREE_CODE (last) == COND_EXPR
1037 && (COMPARISON_CLASS_P (COND_EXPR_COND (last))
1038 || TREE_CODE (COND_EXPR_COND (last)) == SSA_NAME)
1039 && EDGE_COUNT (bb->succs) == 2
1040 && (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL) == 0
1041 && (EDGE_SUCC (bb, 1)->flags & EDGE_ABNORMAL) == 0)
1043 edge true_edge, false_edge;
1045 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1047 /* Only try to thread the edge if it reaches a target block with
1048 more than one predecessor and more than one successor. */
1049 if (!single_pred_p (true_edge->dest) && !single_succ_p (true_edge->dest))
1051 struct edge_info *edge_info;
1052 unsigned int i;
1054 /* Push a marker onto the available expression stack so that we
1055 unwind any expressions related to the TRUE arm before processing
1056 the false arm below. */
1057 VEC_safe_push (tree, heap, avail_exprs_stack, NULL_TREE);
1058 VEC_safe_push (tree, heap, const_and_copies_stack, NULL_TREE);
1060 edge_info = true_edge->aux;
1062 /* If we have info associated with this edge, record it into
1063 our equivalency tables. */
1064 if (edge_info)
1066 tree *cond_equivalences = edge_info->cond_equivalences;
1067 tree lhs = edge_info->lhs;
1068 tree rhs = edge_info->rhs;
1070 /* If we have a simple NAME = VALUE equivalency record it. */
1071 if (lhs && TREE_CODE (lhs) == 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 /* Now thread the edge. */
1087 thread_across_edge (walk_data, true_edge);
1089 /* And restore the various tables to their state before
1090 we threaded this edge. */
1091 remove_local_expressions_from_table ();
1092 restore_vars_to_original_value ();
1095 /* Similarly for the ELSE arm. */
1096 if (!single_pred_p (false_edge->dest) && !single_succ_p (false_edge->dest))
1098 struct edge_info *edge_info;
1099 unsigned int i;
1101 edge_info = false_edge->aux;
1103 /* If we have info associated with this edge, record it into
1104 our equivalency tables. */
1105 if (edge_info)
1107 tree *cond_equivalences = edge_info->cond_equivalences;
1108 tree lhs = edge_info->lhs;
1109 tree rhs = edge_info->rhs;
1111 /* If we have a simple NAME = VALUE equivalency record it. */
1112 if (lhs && TREE_CODE (lhs) == SSA_NAME)
1113 record_const_or_copy (lhs, rhs);
1115 /* If we have 0 = COND or 1 = COND equivalences, record them
1116 into our expression hash tables. */
1117 if (cond_equivalences)
1118 for (i = 0; i < edge_info->max_cond_equivalences; i += 2)
1120 tree expr = cond_equivalences[i];
1121 tree value = cond_equivalences[i + 1];
1123 record_cond (expr, value);
1127 thread_across_edge (walk_data, false_edge);
1129 /* No need to remove local expressions from our tables
1130 or restore vars to their original value as that will
1131 be done immediately below. */
1135 remove_local_expressions_from_table ();
1136 restore_nonzero_vars_to_original_value ();
1137 restore_vars_to_original_value ();
1139 /* Remove VRP records associated with this basic block. They are no
1140 longer valid.
1142 To be efficient, we note which variables have had their values
1143 constrained in this block. So walk over each variable in the
1144 VRP_VARIABLEs array. */
1145 while (VEC_length (tree, vrp_variables_stack) > 0)
1147 tree var = VEC_pop (tree, vrp_variables_stack);
1148 struct vrp_hash_elt vrp_hash_elt, *vrp_hash_elt_p;
1149 void **slot;
1151 /* Each variable has a stack of value range records. We want to
1152 invalidate those associated with our basic block. So we walk
1153 the array backwards popping off records associated with our
1154 block. Once we hit a record not associated with our block
1155 we are done. */
1156 VEC(vrp_element_p,heap) **var_vrp_records;
1158 if (var == NULL)
1159 break;
1161 vrp_hash_elt.var = var;
1162 vrp_hash_elt.records = NULL;
1164 slot = htab_find_slot (vrp_data, &vrp_hash_elt, NO_INSERT);
1166 vrp_hash_elt_p = (struct vrp_hash_elt *) *slot;
1167 var_vrp_records = &vrp_hash_elt_p->records;
1169 while (VEC_length (vrp_element_p, *var_vrp_records) > 0)
1171 struct vrp_element *element
1172 = VEC_last (vrp_element_p, *var_vrp_records);
1174 if (element->bb != bb)
1175 break;
1177 VEC_pop (vrp_element_p, *var_vrp_records);
1181 /* If we queued any statements to rescan in this block, then
1182 go ahead and rescan them now. */
1183 while (VEC_length (tree, stmts_to_rescan) > 0)
1185 tree stmt = VEC_last (tree, stmts_to_rescan);
1186 basic_block stmt_bb = bb_for_stmt (stmt);
1188 if (stmt_bb != bb)
1189 break;
1191 VEC_pop (tree, stmts_to_rescan);
1192 mark_new_vars_to_rename (stmt);
1196 /* PHI nodes can create equivalences too.
1198 Ignoring any alternatives which are the same as the result, if
1199 all the alternatives are equal, then the PHI node creates an
1200 equivalence.
1202 Additionally, if all the PHI alternatives are known to have a nonzero
1203 value, then the result of this PHI is known to have a nonzero value,
1204 even if we do not know its exact value. */
1206 static void
1207 record_equivalences_from_phis (basic_block bb)
1209 tree phi;
1211 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1213 tree lhs = PHI_RESULT (phi);
1214 tree rhs = NULL;
1215 int i;
1217 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1219 tree t = PHI_ARG_DEF (phi, i);
1221 /* Ignore alternatives which are the same as our LHS. Since
1222 LHS is a PHI_RESULT, it is known to be a SSA_NAME, so we
1223 can simply compare pointers. */
1224 if (lhs == t)
1225 continue;
1227 /* If we have not processed an alternative yet, then set
1228 RHS to this alternative. */
1229 if (rhs == NULL)
1230 rhs = t;
1231 /* If we have processed an alternative (stored in RHS), then
1232 see if it is equal to this one. If it isn't, then stop
1233 the search. */
1234 else if (! operand_equal_for_phi_arg_p (rhs, t))
1235 break;
1238 /* If we had no interesting alternatives, then all the RHS alternatives
1239 must have been the same as LHS. */
1240 if (!rhs)
1241 rhs = lhs;
1243 /* If we managed to iterate through each PHI alternative without
1244 breaking out of the loop, then we have a PHI which may create
1245 a useful equivalence. We do not need to record unwind data for
1246 this, since this is a true assignment and not an equivalence
1247 inferred from a comparison. All uses of this ssa name are dominated
1248 by this assignment, so unwinding just costs time and space. */
1249 if (i == PHI_NUM_ARGS (phi)
1250 && may_propagate_copy (lhs, rhs))
1251 SSA_NAME_VALUE (lhs) = rhs;
1253 /* Now see if we know anything about the nonzero property for the
1254 result of this PHI. */
1255 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1257 if (!PHI_ARG_NONZERO (phi, i))
1258 break;
1261 if (i == PHI_NUM_ARGS (phi))
1262 bitmap_set_bit (nonzero_vars, SSA_NAME_VERSION (PHI_RESULT (phi)));
1266 /* Ignoring loop backedges, if BB has precisely one incoming edge then
1267 return that edge. Otherwise return NULL. */
1268 static edge
1269 single_incoming_edge_ignoring_loop_edges (basic_block bb)
1271 edge retval = NULL;
1272 edge e;
1273 edge_iterator ei;
1275 FOR_EACH_EDGE (e, ei, bb->preds)
1277 /* A loop back edge can be identified by the destination of
1278 the edge dominating the source of the edge. */
1279 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
1280 continue;
1282 /* If we have already seen a non-loop edge, then we must have
1283 multiple incoming non-loop edges and thus we return NULL. */
1284 if (retval)
1285 return NULL;
1287 /* This is the first non-loop incoming edge we have found. Record
1288 it. */
1289 retval = e;
1292 return retval;
1295 /* Record any equivalences created by the incoming edge to BB. If BB
1296 has more than one incoming edge, then no equivalence is created. */
1298 static void
1299 record_equivalences_from_incoming_edge (basic_block bb)
1301 edge e;
1302 basic_block parent;
1303 struct edge_info *edge_info;
1305 /* If our parent block ended with a control statement, then we may be
1306 able to record some equivalences based on which outgoing edge from
1307 the parent was followed. */
1308 parent = get_immediate_dominator (CDI_DOMINATORS, bb);
1310 e = single_incoming_edge_ignoring_loop_edges (bb);
1312 /* If we had a single incoming edge from our parent block, then enter
1313 any data associated with the edge into our tables. */
1314 if (e && e->src == parent)
1316 unsigned int i;
1318 edge_info = e->aux;
1320 if (edge_info)
1322 tree lhs = edge_info->lhs;
1323 tree rhs = edge_info->rhs;
1324 tree *cond_equivalences = edge_info->cond_equivalences;
1326 if (lhs)
1327 record_equality (lhs, rhs);
1329 if (cond_equivalences)
1331 bool recorded_range = false;
1332 for (i = 0; i < edge_info->max_cond_equivalences; i += 2)
1334 tree expr = cond_equivalences[i];
1335 tree value = cond_equivalences[i + 1];
1337 record_cond (expr, value);
1339 /* For the first true equivalence, record range
1340 information. We only do this for the first
1341 true equivalence as it should dominate any
1342 later true equivalences. */
1343 if (! recorded_range
1344 && COMPARISON_CLASS_P (expr)
1345 && value == boolean_true_node
1346 && TREE_CONSTANT (TREE_OPERAND (expr, 1)))
1348 record_range (expr, bb);
1349 recorded_range = true;
1357 /* Dump SSA statistics on FILE. */
1359 void
1360 dump_dominator_optimization_stats (FILE *file)
1362 long n_exprs;
1364 fprintf (file, "Total number of statements: %6ld\n\n",
1365 opt_stats.num_stmts);
1366 fprintf (file, "Exprs considered for dominator optimizations: %6ld\n",
1367 opt_stats.num_exprs_considered);
1369 n_exprs = opt_stats.num_exprs_considered;
1370 if (n_exprs == 0)
1371 n_exprs = 1;
1373 fprintf (file, " Redundant expressions eliminated: %6ld (%.0f%%)\n",
1374 opt_stats.num_re, PERCENT (opt_stats.num_re,
1375 n_exprs));
1376 fprintf (file, " Constants propagated: %6ld\n",
1377 opt_stats.num_const_prop);
1378 fprintf (file, " Copies propagated: %6ld\n",
1379 opt_stats.num_copy_prop);
1381 fprintf (file, "\nTotal number of DOM iterations: %6ld\n",
1382 opt_stats.num_iterations);
1384 fprintf (file, "\nHash table statistics:\n");
1386 fprintf (file, " avail_exprs: ");
1387 htab_statistics (file, avail_exprs);
1391 /* Dump SSA statistics on stderr. */
1393 void
1394 debug_dominator_optimization_stats (void)
1396 dump_dominator_optimization_stats (stderr);
1400 /* Dump statistics for the hash table HTAB. */
1402 static void
1403 htab_statistics (FILE *file, htab_t htab)
1405 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
1406 (long) htab_size (htab),
1407 (long) htab_elements (htab),
1408 htab_collisions (htab));
1411 /* Record the fact that VAR has a nonzero value, though we may not know
1412 its exact value. Note that if VAR is already known to have a nonzero
1413 value, then we do nothing. */
1415 static void
1416 record_var_is_nonzero (tree var)
1418 int indx = SSA_NAME_VERSION (var);
1420 if (bitmap_bit_p (nonzero_vars, indx))
1421 return;
1423 /* Mark it in the global table. */
1424 bitmap_set_bit (nonzero_vars, indx);
1426 /* Record this SSA_NAME so that we can reset the global table
1427 when we leave this block. */
1428 VEC_safe_push (tree, heap, nonzero_vars_stack, var);
1431 /* Enter a statement into the true/false expression hash table indicating
1432 that the condition COND has the value VALUE. */
1434 static void
1435 record_cond (tree cond, tree value)
1437 struct expr_hash_elt *element = xmalloc (sizeof (struct expr_hash_elt));
1438 void **slot;
1440 initialize_hash_element (cond, value, element);
1442 slot = htab_find_slot_with_hash (avail_exprs, (void *)element,
1443 element->hash, INSERT);
1444 if (*slot == NULL)
1446 *slot = (void *) element;
1447 VEC_safe_push (tree, heap, avail_exprs_stack, cond);
1449 else
1450 free (element);
1453 /* Build a new conditional using NEW_CODE, OP0 and OP1 and store
1454 the new conditional into *p, then store a boolean_true_node
1455 into *(p + 1). */
1457 static void
1458 build_and_record_new_cond (enum tree_code new_code, tree op0, tree op1, tree *p)
1460 *p = build2 (new_code, boolean_type_node, op0, op1);
1461 p++;
1462 *p = boolean_true_node;
1465 /* Record that COND is true and INVERTED is false into the edge information
1466 structure. Also record that any conditions dominated by COND are true
1467 as well.
1469 For example, if a < b is true, then a <= b must also be true. */
1471 static void
1472 record_conditions (struct edge_info *edge_info, tree cond, tree inverted)
1474 tree op0, op1;
1476 if (!COMPARISON_CLASS_P (cond))
1477 return;
1479 op0 = TREE_OPERAND (cond, 0);
1480 op1 = TREE_OPERAND (cond, 1);
1482 switch (TREE_CODE (cond))
1484 case LT_EXPR:
1485 case GT_EXPR:
1486 edge_info->max_cond_equivalences = 12;
1487 edge_info->cond_equivalences = xmalloc (12 * sizeof (tree));
1488 build_and_record_new_cond ((TREE_CODE (cond) == LT_EXPR
1489 ? LE_EXPR : GE_EXPR),
1490 op0, op1, &edge_info->cond_equivalences[4]);
1491 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1492 &edge_info->cond_equivalences[6]);
1493 build_and_record_new_cond (NE_EXPR, op0, op1,
1494 &edge_info->cond_equivalences[8]);
1495 build_and_record_new_cond (LTGT_EXPR, op0, op1,
1496 &edge_info->cond_equivalences[10]);
1497 break;
1499 case GE_EXPR:
1500 case LE_EXPR:
1501 edge_info->max_cond_equivalences = 6;
1502 edge_info->cond_equivalences = xmalloc (6 * sizeof (tree));
1503 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1504 &edge_info->cond_equivalences[4]);
1505 break;
1507 case EQ_EXPR:
1508 edge_info->max_cond_equivalences = 10;
1509 edge_info->cond_equivalences = xmalloc (10 * sizeof (tree));
1510 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1511 &edge_info->cond_equivalences[4]);
1512 build_and_record_new_cond (LE_EXPR, op0, op1,
1513 &edge_info->cond_equivalences[6]);
1514 build_and_record_new_cond (GE_EXPR, op0, op1,
1515 &edge_info->cond_equivalences[8]);
1516 break;
1518 case UNORDERED_EXPR:
1519 edge_info->max_cond_equivalences = 16;
1520 edge_info->cond_equivalences = xmalloc (16 * sizeof (tree));
1521 build_and_record_new_cond (NE_EXPR, op0, op1,
1522 &edge_info->cond_equivalences[4]);
1523 build_and_record_new_cond (UNLE_EXPR, op0, op1,
1524 &edge_info->cond_equivalences[6]);
1525 build_and_record_new_cond (UNGE_EXPR, op0, op1,
1526 &edge_info->cond_equivalences[8]);
1527 build_and_record_new_cond (UNEQ_EXPR, op0, op1,
1528 &edge_info->cond_equivalences[10]);
1529 build_and_record_new_cond (UNLT_EXPR, op0, op1,
1530 &edge_info->cond_equivalences[12]);
1531 build_and_record_new_cond (UNGT_EXPR, op0, op1,
1532 &edge_info->cond_equivalences[14]);
1533 break;
1535 case UNLT_EXPR:
1536 case UNGT_EXPR:
1537 edge_info->max_cond_equivalences = 8;
1538 edge_info->cond_equivalences = xmalloc (8 * sizeof (tree));
1539 build_and_record_new_cond ((TREE_CODE (cond) == UNLT_EXPR
1540 ? UNLE_EXPR : UNGE_EXPR),
1541 op0, op1, &edge_info->cond_equivalences[4]);
1542 build_and_record_new_cond (NE_EXPR, op0, op1,
1543 &edge_info->cond_equivalences[6]);
1544 break;
1546 case UNEQ_EXPR:
1547 edge_info->max_cond_equivalences = 8;
1548 edge_info->cond_equivalences = xmalloc (8 * sizeof (tree));
1549 build_and_record_new_cond (UNLE_EXPR, op0, op1,
1550 &edge_info->cond_equivalences[4]);
1551 build_and_record_new_cond (UNGE_EXPR, op0, op1,
1552 &edge_info->cond_equivalences[6]);
1553 break;
1555 case LTGT_EXPR:
1556 edge_info->max_cond_equivalences = 8;
1557 edge_info->cond_equivalences = xmalloc (8 * sizeof (tree));
1558 build_and_record_new_cond (NE_EXPR, op0, op1,
1559 &edge_info->cond_equivalences[4]);
1560 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1561 &edge_info->cond_equivalences[6]);
1562 break;
1564 default:
1565 edge_info->max_cond_equivalences = 4;
1566 edge_info->cond_equivalences = xmalloc (4 * sizeof (tree));
1567 break;
1570 /* Now store the original true and false conditions into the first
1571 two slots. */
1572 edge_info->cond_equivalences[0] = cond;
1573 edge_info->cond_equivalences[1] = boolean_true_node;
1574 edge_info->cond_equivalences[2] = inverted;
1575 edge_info->cond_equivalences[3] = boolean_false_node;
1578 /* A helper function for record_const_or_copy and record_equality.
1579 Do the work of recording the value and undo info. */
1581 static void
1582 record_const_or_copy_1 (tree x, tree y, tree prev_x)
1584 SSA_NAME_VALUE (x) = y;
1586 VEC_reserve (tree, heap, const_and_copies_stack, 2);
1587 VEC_quick_push (tree, const_and_copies_stack, prev_x);
1588 VEC_quick_push (tree, const_and_copies_stack, x);
1592 /* Return the loop depth of the basic block of the defining statement of X.
1593 This number should not be treated as absolutely correct because the loop
1594 information may not be completely up-to-date when dom runs. However, it
1595 will be relatively correct, and as more passes are taught to keep loop info
1596 up to date, the result will become more and more accurate. */
1599 loop_depth_of_name (tree x)
1601 tree defstmt;
1602 basic_block defbb;
1604 /* If it's not an SSA_NAME, we have no clue where the definition is. */
1605 if (TREE_CODE (x) != SSA_NAME)
1606 return 0;
1608 /* Otherwise return the loop depth of the defining statement's bb.
1609 Note that there may not actually be a bb for this statement, if the
1610 ssa_name is live on entry. */
1611 defstmt = SSA_NAME_DEF_STMT (x);
1612 defbb = bb_for_stmt (defstmt);
1613 if (!defbb)
1614 return 0;
1616 return defbb->loop_depth;
1620 /* Record that X is equal to Y in const_and_copies. Record undo
1621 information in the block-local vector. */
1623 static void
1624 record_const_or_copy (tree x, tree y)
1626 tree prev_x = SSA_NAME_VALUE (x);
1628 if (TREE_CODE (y) == SSA_NAME)
1630 tree tmp = SSA_NAME_VALUE (y);
1631 if (tmp)
1632 y = tmp;
1635 record_const_or_copy_1 (x, y, prev_x);
1638 /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1639 This constrains the cases in which we may treat this as assignment. */
1641 static void
1642 record_equality (tree x, tree y)
1644 tree prev_x = NULL, prev_y = NULL;
1646 if (TREE_CODE (x) == SSA_NAME)
1647 prev_x = SSA_NAME_VALUE (x);
1648 if (TREE_CODE (y) == SSA_NAME)
1649 prev_y = SSA_NAME_VALUE (y);
1651 /* If one of the previous values is invariant, or invariant in more loops
1652 (by depth), then use that.
1653 Otherwise it doesn't matter which value we choose, just so
1654 long as we canonicalize on one value. */
1655 if (TREE_INVARIANT (y))
1657 else if (TREE_INVARIANT (x) || (loop_depth_of_name (x) <= loop_depth_of_name (y)))
1658 prev_x = x, x = y, y = prev_x, prev_x = prev_y;
1659 else if (prev_x && TREE_INVARIANT (prev_x))
1660 x = y, y = prev_x, prev_x = prev_y;
1661 else if (prev_y && TREE_CODE (prev_y) != VALUE_HANDLE)
1662 y = prev_y;
1664 /* After the swapping, we must have one SSA_NAME. */
1665 if (TREE_CODE (x) != SSA_NAME)
1666 return;
1668 /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1669 variable compared against zero. If we're honoring signed zeros,
1670 then we cannot record this value unless we know that the value is
1671 nonzero. */
1672 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (x)))
1673 && (TREE_CODE (y) != REAL_CST
1674 || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (y))))
1675 return;
1677 record_const_or_copy_1 (x, y, prev_x);
1680 /* Return true, if it is ok to do folding of an associative expression.
1681 EXP is the tree for the associative expression. */
1683 static inline bool
1684 unsafe_associative_fp_binop (tree exp)
1686 enum tree_code code = TREE_CODE (exp);
1687 return !(!flag_unsafe_math_optimizations
1688 && (code == MULT_EXPR || code == PLUS_EXPR
1689 || code == MINUS_EXPR)
1690 && FLOAT_TYPE_P (TREE_TYPE (exp)));
1693 /* Returns true when STMT is a simple iv increment. It detects the
1694 following situation:
1696 i_1 = phi (..., i_2)
1697 i_2 = i_1 +/- ... */
1699 static bool
1700 simple_iv_increment_p (tree stmt)
1702 tree lhs, rhs, preinc, phi;
1703 unsigned i;
1705 if (TREE_CODE (stmt) != MODIFY_EXPR)
1706 return false;
1708 lhs = TREE_OPERAND (stmt, 0);
1709 if (TREE_CODE (lhs) != SSA_NAME)
1710 return false;
1712 rhs = TREE_OPERAND (stmt, 1);
1714 if (TREE_CODE (rhs) != PLUS_EXPR
1715 && TREE_CODE (rhs) != MINUS_EXPR)
1716 return false;
1718 preinc = TREE_OPERAND (rhs, 0);
1719 if (TREE_CODE (preinc) != SSA_NAME)
1720 return false;
1722 phi = SSA_NAME_DEF_STMT (preinc);
1723 if (TREE_CODE (phi) != PHI_NODE)
1724 return false;
1726 for (i = 0; i < (unsigned) PHI_NUM_ARGS (phi); i++)
1727 if (PHI_ARG_DEF (phi, i) == lhs)
1728 return true;
1730 return false;
1733 /* STMT is a MODIFY_EXPR for which we were unable to find RHS in the
1734 hash tables. Try to simplify the RHS using whatever equivalences
1735 we may have recorded.
1737 If we are able to simplify the RHS, then lookup the simplified form in
1738 the hash table and return the result. Otherwise return NULL. */
1740 static tree
1741 simplify_rhs_and_lookup_avail_expr (tree stmt, int insert)
1743 tree rhs = TREE_OPERAND (stmt, 1);
1744 enum tree_code rhs_code = TREE_CODE (rhs);
1745 tree result = NULL;
1747 /* If we have lhs = ~x, look and see if we earlier had x = ~y.
1748 In which case we can change this statement to be lhs = y.
1749 Which can then be copy propagated.
1751 Similarly for negation. */
1752 if ((rhs_code == BIT_NOT_EXPR || rhs_code == NEGATE_EXPR)
1753 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
1755 /* Get the definition statement for our RHS. */
1756 tree rhs_def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs, 0));
1758 /* See if the RHS_DEF_STMT has the same form as our statement. */
1759 if (TREE_CODE (rhs_def_stmt) == MODIFY_EXPR
1760 && TREE_CODE (TREE_OPERAND (rhs_def_stmt, 1)) == rhs_code)
1762 tree rhs_def_operand;
1764 rhs_def_operand = TREE_OPERAND (TREE_OPERAND (rhs_def_stmt, 1), 0);
1766 /* Verify that RHS_DEF_OPERAND is a suitable SSA variable. */
1767 if (TREE_CODE (rhs_def_operand) == SSA_NAME
1768 && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs_def_operand))
1769 result = update_rhs_and_lookup_avail_expr (stmt,
1770 rhs_def_operand,
1771 insert);
1775 /* If we have z = (x OP C1), see if we earlier had x = y OP C2.
1776 If OP is associative, create and fold (y OP C2) OP C1 which
1777 should result in (y OP C3), use that as the RHS for the
1778 assignment. Add minus to this, as we handle it specially below. */
1779 if ((associative_tree_code (rhs_code) || rhs_code == MINUS_EXPR)
1780 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME
1781 && is_gimple_min_invariant (TREE_OPERAND (rhs, 1)))
1783 tree rhs_def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs, 0));
1785 /* If the statement defines an induction variable, do not propagate
1786 its value, so that we do not create overlapping life ranges. */
1787 if (simple_iv_increment_p (rhs_def_stmt))
1788 goto dont_fold_assoc;
1790 /* See if the RHS_DEF_STMT has the same form as our statement. */
1791 if (TREE_CODE (rhs_def_stmt) == MODIFY_EXPR)
1793 tree rhs_def_rhs = TREE_OPERAND (rhs_def_stmt, 1);
1794 enum tree_code rhs_def_code = TREE_CODE (rhs_def_rhs);
1796 if ((rhs_code == rhs_def_code && unsafe_associative_fp_binop (rhs))
1797 || (rhs_code == PLUS_EXPR && rhs_def_code == MINUS_EXPR)
1798 || (rhs_code == MINUS_EXPR && rhs_def_code == PLUS_EXPR))
1800 tree def_stmt_op0 = TREE_OPERAND (rhs_def_rhs, 0);
1801 tree def_stmt_op1 = TREE_OPERAND (rhs_def_rhs, 1);
1803 if (TREE_CODE (def_stmt_op0) == SSA_NAME
1804 && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def_stmt_op0)
1805 && is_gimple_min_invariant (def_stmt_op1))
1807 tree outer_const = TREE_OPERAND (rhs, 1);
1808 tree type = TREE_TYPE (TREE_OPERAND (stmt, 0));
1809 tree t;
1811 /* If we care about correct floating point results, then
1812 don't fold x + c1 - c2. Note that we need to take both
1813 the codes and the signs to figure this out. */
1814 if (FLOAT_TYPE_P (type)
1815 && !flag_unsafe_math_optimizations
1816 && (rhs_def_code == PLUS_EXPR
1817 || rhs_def_code == MINUS_EXPR))
1819 bool neg = false;
1821 neg ^= (rhs_code == MINUS_EXPR);
1822 neg ^= (rhs_def_code == MINUS_EXPR);
1823 neg ^= real_isneg (TREE_REAL_CST_PTR (outer_const));
1824 neg ^= real_isneg (TREE_REAL_CST_PTR (def_stmt_op1));
1826 if (neg)
1827 goto dont_fold_assoc;
1830 /* Ho hum. So fold will only operate on the outermost
1831 thingy that we give it, so we have to build the new
1832 expression in two pieces. This requires that we handle
1833 combinations of plus and minus. */
1834 if (rhs_def_code != rhs_code)
1836 if (rhs_def_code == MINUS_EXPR)
1837 t = build (MINUS_EXPR, type, outer_const, def_stmt_op1);
1838 else
1839 t = build (MINUS_EXPR, type, def_stmt_op1, outer_const);
1840 rhs_code = PLUS_EXPR;
1842 else if (rhs_def_code == MINUS_EXPR)
1843 t = build (PLUS_EXPR, type, def_stmt_op1, outer_const);
1844 else
1845 t = build (rhs_def_code, type, def_stmt_op1, outer_const);
1846 t = local_fold (t);
1847 t = build (rhs_code, type, def_stmt_op0, t);
1848 t = local_fold (t);
1850 /* If the result is a suitable looking gimple expression,
1851 then use it instead of the original for STMT. */
1852 if (TREE_CODE (t) == SSA_NAME
1853 || (UNARY_CLASS_P (t)
1854 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME)
1855 || ((BINARY_CLASS_P (t) || COMPARISON_CLASS_P (t))
1856 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
1857 && is_gimple_val (TREE_OPERAND (t, 1))))
1858 result = update_rhs_and_lookup_avail_expr (stmt, t, insert);
1862 dont_fold_assoc:;
1865 /* Optimize *"foo" into 'f'. This is done here rather than
1866 in fold to avoid problems with stuff like &*"foo". */
1867 if (TREE_CODE (rhs) == INDIRECT_REF || TREE_CODE (rhs) == ARRAY_REF)
1869 tree t = fold_read_from_constant_string (rhs);
1871 if (t)
1872 result = update_rhs_and_lookup_avail_expr (stmt, t, insert);
1875 return result;
1878 /* COND is a condition of the form:
1880 x == const or x != const
1882 Look back to x's defining statement and see if x is defined as
1884 x = (type) y;
1886 If const is unchanged if we convert it to type, then we can build
1887 the equivalent expression:
1890 y == const or y != const
1892 Which may allow further optimizations.
1894 Return the equivalent comparison or NULL if no such equivalent comparison
1895 was found. */
1897 static tree
1898 find_equivalent_equality_comparison (tree cond)
1900 tree op0 = TREE_OPERAND (cond, 0);
1901 tree op1 = TREE_OPERAND (cond, 1);
1902 tree def_stmt = SSA_NAME_DEF_STMT (op0);
1904 /* OP0 might have been a parameter, so first make sure it
1905 was defined by a MODIFY_EXPR. */
1906 if (def_stmt && TREE_CODE (def_stmt) == MODIFY_EXPR)
1908 tree def_rhs = TREE_OPERAND (def_stmt, 1);
1911 /* If either operand to the comparison is a pointer to
1912 a function, then we can not apply this optimization
1913 as some targets require function pointers to be
1914 canonicalized and in this case this optimization would
1915 eliminate a necessary canonicalization. */
1916 if ((POINTER_TYPE_P (TREE_TYPE (op0))
1917 && TREE_CODE (TREE_TYPE (TREE_TYPE (op0))) == FUNCTION_TYPE)
1918 || (POINTER_TYPE_P (TREE_TYPE (op1))
1919 && TREE_CODE (TREE_TYPE (TREE_TYPE (op1))) == FUNCTION_TYPE))
1920 return NULL;
1922 /* Now make sure the RHS of the MODIFY_EXPR is a typecast. */
1923 if ((TREE_CODE (def_rhs) == NOP_EXPR
1924 || TREE_CODE (def_rhs) == CONVERT_EXPR)
1925 && TREE_CODE (TREE_OPERAND (def_rhs, 0)) == SSA_NAME)
1927 tree def_rhs_inner = TREE_OPERAND (def_rhs, 0);
1928 tree def_rhs_inner_type = TREE_TYPE (def_rhs_inner);
1929 tree new;
1931 if (TYPE_PRECISION (def_rhs_inner_type)
1932 > TYPE_PRECISION (TREE_TYPE (def_rhs)))
1933 return NULL;
1935 /* If the inner type of the conversion is a pointer to
1936 a function, then we can not apply this optimization
1937 as some targets require function pointers to be
1938 canonicalized. This optimization would result in
1939 canonicalization of the pointer when it was not originally
1940 needed/intended. */
1941 if (POINTER_TYPE_P (def_rhs_inner_type)
1942 && TREE_CODE (TREE_TYPE (def_rhs_inner_type)) == FUNCTION_TYPE)
1943 return NULL;
1945 /* What we want to prove is that if we convert OP1 to
1946 the type of the object inside the NOP_EXPR that the
1947 result is still equivalent to SRC.
1949 If that is true, the build and return new equivalent
1950 condition which uses the source of the typecast and the
1951 new constant (which has only changed its type). */
1952 new = build1 (TREE_CODE (def_rhs), def_rhs_inner_type, op1);
1953 new = local_fold (new);
1954 if (is_gimple_val (new) && tree_int_cst_equal (new, op1))
1955 return build (TREE_CODE (cond), TREE_TYPE (cond),
1956 def_rhs_inner, new);
1959 return NULL;
1962 /* STMT is a COND_EXPR for which we could not trivially determine its
1963 result. This routine attempts to find equivalent forms of the
1964 condition which we may be able to optimize better. It also
1965 uses simple value range propagation to optimize conditionals. */
1967 static tree
1968 simplify_cond_and_lookup_avail_expr (tree stmt,
1969 stmt_ann_t ann,
1970 int insert)
1972 tree cond = COND_EXPR_COND (stmt);
1974 if (COMPARISON_CLASS_P (cond))
1976 tree op0 = TREE_OPERAND (cond, 0);
1977 tree op1 = TREE_OPERAND (cond, 1);
1979 if (TREE_CODE (op0) == SSA_NAME && is_gimple_min_invariant (op1))
1981 int limit;
1982 tree low, high, cond_low, cond_high;
1983 int lowequal, highequal, swapped, no_overlap, subset, cond_inverted;
1984 VEC(vrp_element_p,heap) **vrp_records;
1985 struct vrp_element *element;
1986 struct vrp_hash_elt vrp_hash_elt, *vrp_hash_elt_p;
1987 void **slot;
1989 /* First see if we have test of an SSA_NAME against a constant
1990 where the SSA_NAME is defined by an earlier typecast which
1991 is irrelevant when performing tests against the given
1992 constant. */
1993 if (TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
1995 tree new_cond = find_equivalent_equality_comparison (cond);
1997 if (new_cond)
1999 /* Update the statement to use the new equivalent
2000 condition. */
2001 COND_EXPR_COND (stmt) = new_cond;
2003 /* If this is not a real stmt, ann will be NULL and we
2004 avoid processing the operands. */
2005 if (ann)
2006 mark_stmt_modified (stmt);
2008 /* Lookup the condition and return its known value if it
2009 exists. */
2010 new_cond = lookup_avail_expr (stmt, insert);
2011 if (new_cond)
2012 return new_cond;
2014 /* The operands have changed, so update op0 and op1. */
2015 op0 = TREE_OPERAND (cond, 0);
2016 op1 = TREE_OPERAND (cond, 1);
2020 /* Consult the value range records for this variable (if they exist)
2021 to see if we can eliminate or simplify this conditional.
2023 Note two tests are necessary to determine no records exist.
2024 First we have to see if the virtual array exists, if it
2025 exists, then we have to check its active size.
2027 Also note the vast majority of conditionals are not testing
2028 a variable which has had its range constrained by an earlier
2029 conditional. So this filter avoids a lot of unnecessary work. */
2030 vrp_hash_elt.var = op0;
2031 vrp_hash_elt.records = NULL;
2032 slot = htab_find_slot (vrp_data, &vrp_hash_elt, NO_INSERT);
2033 if (slot == NULL)
2034 return NULL;
2036 vrp_hash_elt_p = (struct vrp_hash_elt *) *slot;
2037 vrp_records = &vrp_hash_elt_p->records;
2039 limit = VEC_length (vrp_element_p, *vrp_records);
2041 /* If we have no value range records for this variable, or we are
2042 unable to extract a range for this condition, then there is
2043 nothing to do. */
2044 if (limit == 0
2045 || ! extract_range_from_cond (cond, &cond_high,
2046 &cond_low, &cond_inverted))
2047 return NULL;
2049 /* We really want to avoid unnecessary computations of range
2050 info. So all ranges are computed lazily; this avoids a
2051 lot of unnecessary work. i.e., we record the conditional,
2052 but do not process how it constrains the variable's
2053 potential values until we know that processing the condition
2054 could be helpful.
2056 However, we do not want to have to walk a potentially long
2057 list of ranges, nor do we want to compute a variable's
2058 range more than once for a given path.
2060 Luckily, each time we encounter a conditional that can not
2061 be otherwise optimized we will end up here and we will
2062 compute the necessary range information for the variable
2063 used in this condition.
2065 Thus you can conclude that there will never be more than one
2066 conditional associated with a variable which has not been
2067 processed. So we never need to merge more than one new
2068 conditional into the current range.
2070 These properties also help us avoid unnecessary work. */
2071 element = VEC_last (vrp_element_p, *vrp_records);
2073 if (element->high && element->low)
2075 /* The last element has been processed, so there is no range
2076 merging to do, we can simply use the high/low values
2077 recorded in the last element. */
2078 low = element->low;
2079 high = element->high;
2081 else
2083 tree tmp_high, tmp_low;
2084 int dummy;
2086 /* The last element has not been processed. Process it now.
2087 record_range should ensure for cond inverted is not set.
2088 This call can only fail if cond is x < min or x > max,
2089 which fold should have optimized into false.
2090 If that doesn't happen, just pretend all values are
2091 in the range. */
2092 if (! extract_range_from_cond (element->cond, &tmp_high,
2093 &tmp_low, &dummy))
2094 gcc_unreachable ();
2095 else
2096 gcc_assert (dummy == 0);
2098 /* If this is the only element, then no merging is necessary,
2099 the high/low values from extract_range_from_cond are all
2100 we need. */
2101 if (limit == 1)
2103 low = tmp_low;
2104 high = tmp_high;
2106 else
2108 /* Get the high/low value from the previous element. */
2109 struct vrp_element *prev
2110 = VEC_index (vrp_element_p, *vrp_records, limit - 2);
2111 low = prev->low;
2112 high = prev->high;
2114 /* Merge in this element's range with the range from the
2115 previous element.
2117 The low value for the merged range is the maximum of
2118 the previous low value and the low value of this record.
2120 Similarly the high value for the merged range is the
2121 minimum of the previous high value and the high value of
2122 this record. */
2123 low = (low && tree_int_cst_compare (low, tmp_low) == 1
2124 ? low : tmp_low);
2125 high = (high && tree_int_cst_compare (high, tmp_high) == -1
2126 ? high : tmp_high);
2129 /* And record the computed range. */
2130 element->low = low;
2131 element->high = high;
2135 /* After we have constrained this variable's potential values,
2136 we try to determine the result of the given conditional.
2138 To simplify later tests, first determine if the current
2139 low value is the same low value as the conditional.
2140 Similarly for the current high value and the high value
2141 for the conditional. */
2142 lowequal = tree_int_cst_equal (low, cond_low);
2143 highequal = tree_int_cst_equal (high, cond_high);
2145 if (lowequal && highequal)
2146 return (cond_inverted ? boolean_false_node : boolean_true_node);
2148 /* To simplify the overlap/subset tests below we may want
2149 to swap the two ranges so that the larger of the two
2150 ranges occurs "first". */
2151 swapped = 0;
2152 if (tree_int_cst_compare (low, cond_low) == 1
2153 || (lowequal
2154 && tree_int_cst_compare (cond_high, high) == 1))
2156 tree temp;
2158 swapped = 1;
2159 temp = low;
2160 low = cond_low;
2161 cond_low = temp;
2162 temp = high;
2163 high = cond_high;
2164 cond_high = temp;
2167 /* Now determine if there is no overlap in the ranges
2168 or if the second range is a subset of the first range. */
2169 no_overlap = tree_int_cst_lt (high, cond_low);
2170 subset = tree_int_cst_compare (cond_high, high) != 1;
2172 /* If there was no overlap in the ranges, then this conditional
2173 always has a false value (unless we had to invert this
2174 conditional, in which case it always has a true value). */
2175 if (no_overlap)
2176 return (cond_inverted ? boolean_true_node : boolean_false_node);
2178 /* If the current range is a subset of the condition's range,
2179 then this conditional always has a true value (unless we
2180 had to invert this conditional, in which case it always
2181 has a true value). */
2182 if (subset && swapped)
2183 return (cond_inverted ? boolean_false_node : boolean_true_node);
2185 /* We were unable to determine the result of the conditional.
2186 However, we may be able to simplify the conditional. First
2187 merge the ranges in the same manner as range merging above. */
2188 low = tree_int_cst_compare (low, cond_low) == 1 ? low : cond_low;
2189 high = tree_int_cst_compare (high, cond_high) == -1 ? high : cond_high;
2191 /* If the range has converged to a single point, then turn this
2192 into an equality comparison. */
2193 if (TREE_CODE (cond) != EQ_EXPR
2194 && TREE_CODE (cond) != NE_EXPR
2195 && tree_int_cst_equal (low, high))
2197 TREE_SET_CODE (cond, EQ_EXPR);
2198 TREE_OPERAND (cond, 1) = high;
2202 return 0;
2205 /* STMT is a SWITCH_EXPR for which we could not trivially determine its
2206 result. This routine attempts to find equivalent forms of the
2207 condition which we may be able to optimize better. */
2209 static tree
2210 simplify_switch_and_lookup_avail_expr (tree stmt, int insert)
2212 tree cond = SWITCH_COND (stmt);
2213 tree def, to, ti;
2215 /* The optimization that we really care about is removing unnecessary
2216 casts. That will let us do much better in propagating the inferred
2217 constant at the switch target. */
2218 if (TREE_CODE (cond) == SSA_NAME)
2220 def = SSA_NAME_DEF_STMT (cond);
2221 if (TREE_CODE (def) == MODIFY_EXPR)
2223 def = TREE_OPERAND (def, 1);
2224 if (TREE_CODE (def) == NOP_EXPR)
2226 int need_precision;
2227 bool fail;
2229 def = TREE_OPERAND (def, 0);
2231 #ifdef ENABLE_CHECKING
2232 /* ??? Why was Jeff testing this? We are gimple... */
2233 gcc_assert (is_gimple_val (def));
2234 #endif
2236 to = TREE_TYPE (cond);
2237 ti = TREE_TYPE (def);
2239 /* If we have an extension that preserves value, then we
2240 can copy the source value into the switch. */
2242 need_precision = TYPE_PRECISION (ti);
2243 fail = false;
2244 if (TYPE_UNSIGNED (to) && !TYPE_UNSIGNED (ti))
2245 fail = true;
2246 else if (!TYPE_UNSIGNED (to) && TYPE_UNSIGNED (ti))
2247 need_precision += 1;
2248 if (TYPE_PRECISION (to) < need_precision)
2249 fail = true;
2251 if (!fail)
2253 SWITCH_COND (stmt) = def;
2254 mark_stmt_modified (stmt);
2256 return lookup_avail_expr (stmt, insert);
2262 return 0;
2266 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
2267 known value for that SSA_NAME (or NULL if no value is known).
2269 NONZERO_VARS is the set SSA_NAMES known to have a nonzero value,
2270 even if we don't know their precise value.
2272 Propagate values from CONST_AND_COPIES and NONZERO_VARS into the PHI
2273 nodes of the successors of BB. */
2275 static void
2276 cprop_into_successor_phis (basic_block bb, bitmap nonzero_vars)
2278 edge e;
2279 edge_iterator ei;
2281 FOR_EACH_EDGE (e, ei, bb->succs)
2283 tree phi;
2284 int indx;
2286 /* If this is an abnormal edge, then we do not want to copy propagate
2287 into the PHI alternative associated with this edge. */
2288 if (e->flags & EDGE_ABNORMAL)
2289 continue;
2291 phi = phi_nodes (e->dest);
2292 if (! phi)
2293 continue;
2295 indx = e->dest_idx;
2296 for ( ; phi; phi = PHI_CHAIN (phi))
2298 tree new;
2299 use_operand_p orig_p;
2300 tree orig;
2302 /* The alternative may be associated with a constant, so verify
2303 it is an SSA_NAME before doing anything with it. */
2304 orig_p = PHI_ARG_DEF_PTR (phi, indx);
2305 orig = USE_FROM_PTR (orig_p);
2306 if (TREE_CODE (orig) != SSA_NAME)
2307 continue;
2309 /* If the alternative is known to have a nonzero value, record
2310 that fact in the PHI node itself for future use. */
2311 if (bitmap_bit_p (nonzero_vars, SSA_NAME_VERSION (orig)))
2312 PHI_ARG_NONZERO (phi, indx) = true;
2314 /* If we have *ORIG_P in our constant/copy table, then replace
2315 ORIG_P with its value in our constant/copy table. */
2316 new = SSA_NAME_VALUE (orig);
2317 if (new
2318 && new != orig
2319 && (TREE_CODE (new) == SSA_NAME
2320 || is_gimple_min_invariant (new))
2321 && may_propagate_copy (orig, new))
2322 propagate_value (orig_p, new);
2327 /* We have finished optimizing BB, record any information implied by
2328 taking a specific outgoing edge from BB. */
2330 static void
2331 record_edge_info (basic_block bb)
2333 block_stmt_iterator bsi = bsi_last (bb);
2334 struct edge_info *edge_info;
2336 if (! bsi_end_p (bsi))
2338 tree stmt = bsi_stmt (bsi);
2340 if (stmt && TREE_CODE (stmt) == SWITCH_EXPR)
2342 tree cond = SWITCH_COND (stmt);
2344 if (TREE_CODE (cond) == SSA_NAME)
2346 tree labels = SWITCH_LABELS (stmt);
2347 int i, n_labels = TREE_VEC_LENGTH (labels);
2348 tree *info = xcalloc (last_basic_block, sizeof (tree));
2349 edge e;
2350 edge_iterator ei;
2352 for (i = 0; i < n_labels; i++)
2354 tree label = TREE_VEC_ELT (labels, i);
2355 basic_block target_bb = label_to_block (CASE_LABEL (label));
2357 if (CASE_HIGH (label)
2358 || !CASE_LOW (label)
2359 || info[target_bb->index])
2360 info[target_bb->index] = error_mark_node;
2361 else
2362 info[target_bb->index] = label;
2365 FOR_EACH_EDGE (e, ei, bb->succs)
2367 basic_block target_bb = e->dest;
2368 tree node = info[target_bb->index];
2370 if (node != NULL && node != error_mark_node)
2372 tree x = fold_convert (TREE_TYPE (cond), CASE_LOW (node));
2373 edge_info = allocate_edge_info (e);
2374 edge_info->lhs = cond;
2375 edge_info->rhs = x;
2378 free (info);
2382 /* A COND_EXPR may create equivalences too. */
2383 if (stmt && TREE_CODE (stmt) == COND_EXPR)
2385 tree cond = COND_EXPR_COND (stmt);
2386 edge true_edge;
2387 edge false_edge;
2389 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2391 /* If the conditional is a single variable 'X', record 'X = 1'
2392 for the true edge and 'X = 0' on the false edge. */
2393 if (SSA_VAR_P (cond))
2395 struct edge_info *edge_info;
2397 edge_info = allocate_edge_info (true_edge);
2398 edge_info->lhs = cond;
2399 edge_info->rhs = constant_boolean_node (1, TREE_TYPE (cond));
2401 edge_info = allocate_edge_info (false_edge);
2402 edge_info->lhs = cond;
2403 edge_info->rhs = constant_boolean_node (0, TREE_TYPE (cond));
2405 /* Equality tests may create one or two equivalences. */
2406 else if (COMPARISON_CLASS_P (cond))
2408 tree op0 = TREE_OPERAND (cond, 0);
2409 tree op1 = TREE_OPERAND (cond, 1);
2411 /* Special case comparing booleans against a constant as we
2412 know the value of OP0 on both arms of the branch. i.e., we
2413 can record an equivalence for OP0 rather than COND. */
2414 if ((TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
2415 && TREE_CODE (op0) == SSA_NAME
2416 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
2417 && is_gimple_min_invariant (op1))
2419 if (TREE_CODE (cond) == EQ_EXPR)
2421 edge_info = allocate_edge_info (true_edge);
2422 edge_info->lhs = op0;
2423 edge_info->rhs = (integer_zerop (op1)
2424 ? boolean_false_node
2425 : boolean_true_node);
2427 edge_info = allocate_edge_info (false_edge);
2428 edge_info->lhs = op0;
2429 edge_info->rhs = (integer_zerop (op1)
2430 ? boolean_true_node
2431 : boolean_false_node);
2433 else
2435 edge_info = allocate_edge_info (true_edge);
2436 edge_info->lhs = op0;
2437 edge_info->rhs = (integer_zerop (op1)
2438 ? boolean_true_node
2439 : boolean_false_node);
2441 edge_info = allocate_edge_info (false_edge);
2442 edge_info->lhs = op0;
2443 edge_info->rhs = (integer_zerop (op1)
2444 ? boolean_false_node
2445 : boolean_true_node);
2449 else if (is_gimple_min_invariant (op0)
2450 && (TREE_CODE (op1) == SSA_NAME
2451 || is_gimple_min_invariant (op1)))
2453 tree inverted = invert_truthvalue (cond);
2454 struct edge_info *edge_info;
2456 edge_info = allocate_edge_info (true_edge);
2457 record_conditions (edge_info, cond, inverted);
2459 if (TREE_CODE (cond) == EQ_EXPR)
2461 edge_info->lhs = op1;
2462 edge_info->rhs = op0;
2465 edge_info = allocate_edge_info (false_edge);
2466 record_conditions (edge_info, inverted, cond);
2468 if (TREE_CODE (cond) == NE_EXPR)
2470 edge_info->lhs = op1;
2471 edge_info->rhs = op0;
2475 else if (TREE_CODE (op0) == SSA_NAME
2476 && (is_gimple_min_invariant (op1)
2477 || TREE_CODE (op1) == SSA_NAME))
2479 tree inverted = invert_truthvalue (cond);
2480 struct edge_info *edge_info;
2482 edge_info = allocate_edge_info (true_edge);
2483 record_conditions (edge_info, cond, inverted);
2485 if (TREE_CODE (cond) == EQ_EXPR)
2487 edge_info->lhs = op0;
2488 edge_info->rhs = op1;
2491 edge_info = allocate_edge_info (false_edge);
2492 record_conditions (edge_info, inverted, cond);
2494 if (TREE_CODE (cond) == NE_EXPR)
2496 edge_info->lhs = op0;
2497 edge_info->rhs = op1;
2502 /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
2507 /* Propagate information from BB to its outgoing edges.
2509 This can include equivalency information implied by control statements
2510 at the end of BB and const/copy propagation into PHIs in BB's
2511 successor blocks. */
2513 static void
2514 propagate_to_outgoing_edges (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
2515 basic_block bb)
2517 record_edge_info (bb);
2518 cprop_into_successor_phis (bb, nonzero_vars);
2521 /* Search for redundant computations in STMT. If any are found, then
2522 replace them with the variable holding the result of the computation.
2524 If safe, record this expression into the available expression hash
2525 table. */
2527 static bool
2528 eliminate_redundant_computations (tree stmt, stmt_ann_t ann)
2530 tree *expr_p, def = NULL_TREE;
2531 bool insert = true;
2532 tree cached_lhs;
2533 bool retval = false;
2534 bool modify_expr_p = false;
2536 if (TREE_CODE (stmt) == MODIFY_EXPR)
2537 def = TREE_OPERAND (stmt, 0);
2539 /* Certain expressions on the RHS can be optimized away, but can not
2540 themselves be entered into the hash tables. */
2541 if (ann->makes_aliased_stores
2542 || ! def
2543 || TREE_CODE (def) != SSA_NAME
2544 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)
2545 || !ZERO_SSA_OPERANDS (stmt, SSA_OP_VMAYDEF)
2546 /* Do not record equivalences for increments of ivs. This would create
2547 overlapping live ranges for a very questionable gain. */
2548 || simple_iv_increment_p (stmt))
2549 insert = false;
2551 /* Check if the expression has been computed before. */
2552 cached_lhs = lookup_avail_expr (stmt, insert);
2554 /* If this is an assignment and the RHS was not in the hash table,
2555 then try to simplify the RHS and lookup the new RHS in the
2556 hash table. */
2557 if (! cached_lhs && TREE_CODE (stmt) == MODIFY_EXPR)
2558 cached_lhs = simplify_rhs_and_lookup_avail_expr (stmt, insert);
2559 /* Similarly if this is a COND_EXPR and we did not find its
2560 expression in the hash table, simplify the condition and
2561 try again. */
2562 else if (! cached_lhs && TREE_CODE (stmt) == COND_EXPR)
2563 cached_lhs = simplify_cond_and_lookup_avail_expr (stmt, ann, insert);
2564 /* Similarly for a SWITCH_EXPR. */
2565 else if (!cached_lhs && TREE_CODE (stmt) == SWITCH_EXPR)
2566 cached_lhs = simplify_switch_and_lookup_avail_expr (stmt, insert);
2568 opt_stats.num_exprs_considered++;
2570 /* Get a pointer to the expression we are trying to optimize. */
2571 if (TREE_CODE (stmt) == COND_EXPR)
2572 expr_p = &COND_EXPR_COND (stmt);
2573 else if (TREE_CODE (stmt) == SWITCH_EXPR)
2574 expr_p = &SWITCH_COND (stmt);
2575 else if (TREE_CODE (stmt) == RETURN_EXPR && TREE_OPERAND (stmt, 0))
2577 expr_p = &TREE_OPERAND (TREE_OPERAND (stmt, 0), 1);
2578 modify_expr_p = true;
2580 else
2582 expr_p = &TREE_OPERAND (stmt, 1);
2583 modify_expr_p = true;
2586 /* It is safe to ignore types here since we have already done
2587 type checking in the hashing and equality routines. In fact
2588 type checking here merely gets in the way of constant
2589 propagation. Also, make sure that it is safe to propagate
2590 CACHED_LHS into *EXPR_P. */
2591 if (cached_lhs
2592 && ((TREE_CODE (cached_lhs) != SSA_NAME
2593 && (modify_expr_p
2594 || tree_ssa_useless_type_conversion_1 (TREE_TYPE (*expr_p),
2595 TREE_TYPE (cached_lhs))))
2596 || may_propagate_copy (*expr_p, cached_lhs)))
2598 if (dump_file && (dump_flags & TDF_DETAILS))
2600 fprintf (dump_file, " Replaced redundant expr '");
2601 print_generic_expr (dump_file, *expr_p, dump_flags);
2602 fprintf (dump_file, "' with '");
2603 print_generic_expr (dump_file, cached_lhs, dump_flags);
2604 fprintf (dump_file, "'\n");
2607 opt_stats.num_re++;
2609 #if defined ENABLE_CHECKING
2610 gcc_assert (TREE_CODE (cached_lhs) == SSA_NAME
2611 || is_gimple_min_invariant (cached_lhs));
2612 #endif
2614 if (TREE_CODE (cached_lhs) == ADDR_EXPR
2615 || (POINTER_TYPE_P (TREE_TYPE (*expr_p))
2616 && is_gimple_min_invariant (cached_lhs)))
2617 retval = true;
2619 if (modify_expr_p
2620 && !tree_ssa_useless_type_conversion_1 (TREE_TYPE (*expr_p),
2621 TREE_TYPE (cached_lhs)))
2622 cached_lhs = fold_convert (TREE_TYPE (*expr_p), cached_lhs);
2624 propagate_tree_value (expr_p, cached_lhs);
2625 mark_stmt_modified (stmt);
2627 return retval;
2630 /* STMT, a MODIFY_EXPR, may create certain equivalences, in either
2631 the available expressions table or the const_and_copies table.
2632 Detect and record those equivalences. */
2634 static void
2635 record_equivalences_from_stmt (tree stmt,
2636 int may_optimize_p,
2637 stmt_ann_t ann)
2639 tree lhs = TREE_OPERAND (stmt, 0);
2640 enum tree_code lhs_code = TREE_CODE (lhs);
2641 int i;
2643 if (lhs_code == SSA_NAME)
2645 tree rhs = TREE_OPERAND (stmt, 1);
2647 /* Strip away any useless type conversions. */
2648 STRIP_USELESS_TYPE_CONVERSION (rhs);
2650 /* If the RHS of the assignment is a constant or another variable that
2651 may be propagated, register it in the CONST_AND_COPIES table. We
2652 do not need to record unwind data for this, since this is a true
2653 assignment and not an equivalence inferred from a comparison. All
2654 uses of this ssa name are dominated by this assignment, so unwinding
2655 just costs time and space. */
2656 if (may_optimize_p
2657 && (TREE_CODE (rhs) == SSA_NAME
2658 || is_gimple_min_invariant (rhs)))
2659 SSA_NAME_VALUE (lhs) = rhs;
2661 if (tree_expr_nonzero_p (rhs))
2662 record_var_is_nonzero (lhs);
2665 /* Look at both sides for pointer dereferences. If we find one, then
2666 the pointer must be nonnull and we can enter that equivalence into
2667 the hash tables. */
2668 if (flag_delete_null_pointer_checks)
2669 for (i = 0; i < 2; i++)
2671 tree t = TREE_OPERAND (stmt, i);
2673 /* Strip away any COMPONENT_REFs. */
2674 while (TREE_CODE (t) == COMPONENT_REF)
2675 t = TREE_OPERAND (t, 0);
2677 /* Now see if this is a pointer dereference. */
2678 if (INDIRECT_REF_P (t))
2680 tree op = TREE_OPERAND (t, 0);
2682 /* If the pointer is a SSA variable, then enter new
2683 equivalences into the hash table. */
2684 while (TREE_CODE (op) == SSA_NAME)
2686 tree def = SSA_NAME_DEF_STMT (op);
2688 record_var_is_nonzero (op);
2690 /* And walk up the USE-DEF chains noting other SSA_NAMEs
2691 which are known to have a nonzero value. */
2692 if (def
2693 && TREE_CODE (def) == MODIFY_EXPR
2694 && TREE_CODE (TREE_OPERAND (def, 1)) == NOP_EXPR)
2695 op = TREE_OPERAND (TREE_OPERAND (def, 1), 0);
2696 else
2697 break;
2702 /* A memory store, even an aliased store, creates a useful
2703 equivalence. By exchanging the LHS and RHS, creating suitable
2704 vops and recording the result in the available expression table,
2705 we may be able to expose more redundant loads. */
2706 if (!ann->has_volatile_ops
2707 && (TREE_CODE (TREE_OPERAND (stmt, 1)) == SSA_NAME
2708 || is_gimple_min_invariant (TREE_OPERAND (stmt, 1)))
2709 && !is_gimple_reg (lhs))
2711 tree rhs = TREE_OPERAND (stmt, 1);
2712 tree new;
2714 /* FIXME: If the LHS of the assignment is a bitfield and the RHS
2715 is a constant, we need to adjust the constant to fit into the
2716 type of the LHS. If the LHS is a bitfield and the RHS is not
2717 a constant, then we can not record any equivalences for this
2718 statement since we would need to represent the widening or
2719 narrowing of RHS. This fixes gcc.c-torture/execute/921016-1.c
2720 and should not be necessary if GCC represented bitfields
2721 properly. */
2722 if (lhs_code == COMPONENT_REF
2723 && DECL_BIT_FIELD (TREE_OPERAND (lhs, 1)))
2725 if (TREE_CONSTANT (rhs))
2726 rhs = widen_bitfield (rhs, TREE_OPERAND (lhs, 1), lhs);
2727 else
2728 rhs = NULL;
2730 /* If the value overflowed, then we can not use this equivalence. */
2731 if (rhs && ! is_gimple_min_invariant (rhs))
2732 rhs = NULL;
2735 if (rhs)
2737 /* Build a new statement with the RHS and LHS exchanged. */
2738 new = build (MODIFY_EXPR, TREE_TYPE (stmt), rhs, lhs);
2740 create_ssa_artficial_load_stmt (new, stmt);
2742 /* Finally enter the statement into the available expression
2743 table. */
2744 lookup_avail_expr (new, true);
2749 /* Replace *OP_P in STMT with any known equivalent value for *OP_P from
2750 CONST_AND_COPIES. */
2752 static bool
2753 cprop_operand (tree stmt, use_operand_p op_p)
2755 bool may_have_exposed_new_symbols = false;
2756 tree val;
2757 tree op = USE_FROM_PTR (op_p);
2759 /* If the operand has a known constant value or it is known to be a
2760 copy of some other variable, use the value or copy stored in
2761 CONST_AND_COPIES. */
2762 val = SSA_NAME_VALUE (op);
2763 if (val && val != op && TREE_CODE (val) != VALUE_HANDLE)
2765 tree op_type, val_type;
2767 /* Do not change the base variable in the virtual operand
2768 tables. That would make it impossible to reconstruct
2769 the renamed virtual operand if we later modify this
2770 statement. Also only allow the new value to be an SSA_NAME
2771 for propagation into virtual operands. */
2772 if (!is_gimple_reg (op)
2773 && (TREE_CODE (val) != SSA_NAME
2774 || is_gimple_reg (val)
2775 || get_virtual_var (val) != get_virtual_var (op)))
2776 return false;
2778 /* Do not replace hard register operands in asm statements. */
2779 if (TREE_CODE (stmt) == ASM_EXPR
2780 && !may_propagate_copy_into_asm (op))
2781 return false;
2783 /* Get the toplevel type of each operand. */
2784 op_type = TREE_TYPE (op);
2785 val_type = TREE_TYPE (val);
2787 /* While both types are pointers, get the type of the object
2788 pointed to. */
2789 while (POINTER_TYPE_P (op_type) && POINTER_TYPE_P (val_type))
2791 op_type = TREE_TYPE (op_type);
2792 val_type = TREE_TYPE (val_type);
2795 /* Make sure underlying types match before propagating a constant by
2796 converting the constant to the proper type. Note that convert may
2797 return a non-gimple expression, in which case we ignore this
2798 propagation opportunity. */
2799 if (TREE_CODE (val) != SSA_NAME)
2801 if (!lang_hooks.types_compatible_p (op_type, val_type))
2803 val = fold_convert (TREE_TYPE (op), val);
2804 if (!is_gimple_min_invariant (val))
2805 return false;
2809 /* Certain operands are not allowed to be copy propagated due
2810 to their interaction with exception handling and some GCC
2811 extensions. */
2812 else if (!may_propagate_copy (op, val))
2813 return false;
2815 /* Do not propagate copies if the propagated value is at a deeper loop
2816 depth than the propagatee. Otherwise, this may move loop variant
2817 variables outside of their loops and prevent coalescing
2818 opportunities. If the value was loop invariant, it will be hoisted
2819 by LICM and exposed for copy propagation. */
2820 if (loop_depth_of_name (val) > loop_depth_of_name (op))
2821 return false;
2823 /* Dump details. */
2824 if (dump_file && (dump_flags & TDF_DETAILS))
2826 fprintf (dump_file, " Replaced '");
2827 print_generic_expr (dump_file, op, dump_flags);
2828 fprintf (dump_file, "' with %s '",
2829 (TREE_CODE (val) != SSA_NAME ? "constant" : "variable"));
2830 print_generic_expr (dump_file, val, dump_flags);
2831 fprintf (dump_file, "'\n");
2834 /* If VAL is an ADDR_EXPR or a constant of pointer type, note
2835 that we may have exposed a new symbol for SSA renaming. */
2836 if (TREE_CODE (val) == ADDR_EXPR
2837 || (POINTER_TYPE_P (TREE_TYPE (op))
2838 && is_gimple_min_invariant (val)))
2839 may_have_exposed_new_symbols = true;
2841 if (TREE_CODE (val) != SSA_NAME)
2842 opt_stats.num_const_prop++;
2843 else
2844 opt_stats.num_copy_prop++;
2846 propagate_value (op_p, val);
2848 /* And note that we modified this statement. This is now
2849 safe, even if we changed virtual operands since we will
2850 rescan the statement and rewrite its operands again. */
2851 mark_stmt_modified (stmt);
2853 return may_have_exposed_new_symbols;
2856 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
2857 known value for that SSA_NAME (or NULL if no value is known).
2859 Propagate values from CONST_AND_COPIES into the uses, vuses and
2860 v_may_def_ops of STMT. */
2862 static bool
2863 cprop_into_stmt (tree stmt)
2865 bool may_have_exposed_new_symbols = false;
2866 use_operand_p op_p;
2867 ssa_op_iter iter;
2869 FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_ALL_USES)
2871 if (TREE_CODE (USE_FROM_PTR (op_p)) == SSA_NAME)
2872 may_have_exposed_new_symbols |= cprop_operand (stmt, op_p);
2875 return may_have_exposed_new_symbols;
2879 /* Optimize the statement pointed to by iterator SI.
2881 We try to perform some simplistic global redundancy elimination and
2882 constant propagation:
2884 1- To detect global redundancy, we keep track of expressions that have
2885 been computed in this block and its dominators. If we find that the
2886 same expression is computed more than once, we eliminate repeated
2887 computations by using the target of the first one.
2889 2- Constant values and copy assignments. This is used to do very
2890 simplistic constant and copy propagation. When a constant or copy
2891 assignment is found, we map the value on the RHS of the assignment to
2892 the variable in the LHS in the CONST_AND_COPIES table. */
2894 static void
2895 optimize_stmt (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
2896 basic_block bb, block_stmt_iterator si)
2898 stmt_ann_t ann;
2899 tree stmt, old_stmt;
2900 bool may_optimize_p;
2901 bool may_have_exposed_new_symbols = false;
2903 old_stmt = stmt = bsi_stmt (si);
2905 update_stmt_if_modified (stmt);
2906 ann = stmt_ann (stmt);
2907 opt_stats.num_stmts++;
2908 may_have_exposed_new_symbols = false;
2910 if (dump_file && (dump_flags & TDF_DETAILS))
2912 fprintf (dump_file, "Optimizing statement ");
2913 print_generic_stmt (dump_file, stmt, TDF_SLIM);
2916 /* Const/copy propagate into USES, VUSES and the RHS of V_MAY_DEFs. */
2917 may_have_exposed_new_symbols = cprop_into_stmt (stmt);
2919 /* If the statement has been modified with constant replacements,
2920 fold its RHS before checking for redundant computations. */
2921 if (ann->modified)
2923 tree rhs;
2925 /* Try to fold the statement making sure that STMT is kept
2926 up to date. */
2927 if (fold_stmt (bsi_stmt_ptr (si)))
2929 stmt = bsi_stmt (si);
2930 ann = stmt_ann (stmt);
2932 if (dump_file && (dump_flags & TDF_DETAILS))
2934 fprintf (dump_file, " Folded to: ");
2935 print_generic_stmt (dump_file, stmt, TDF_SLIM);
2939 rhs = get_rhs (stmt);
2940 if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
2941 recompute_tree_invarant_for_addr_expr (rhs);
2943 /* Constant/copy propagation above may change the set of
2944 virtual operands associated with this statement. Folding
2945 may remove the need for some virtual operands.
2947 Indicate we will need to rescan and rewrite the statement. */
2948 may_have_exposed_new_symbols = true;
2951 /* Check for redundant computations. Do this optimization only
2952 for assignments that have no volatile ops and conditionals. */
2953 may_optimize_p = (!ann->has_volatile_ops
2954 && ((TREE_CODE (stmt) == RETURN_EXPR
2955 && TREE_OPERAND (stmt, 0)
2956 && TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR
2957 && ! (TREE_SIDE_EFFECTS
2958 (TREE_OPERAND (TREE_OPERAND (stmt, 0), 1))))
2959 || (TREE_CODE (stmt) == MODIFY_EXPR
2960 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (stmt, 1)))
2961 || TREE_CODE (stmt) == COND_EXPR
2962 || TREE_CODE (stmt) == SWITCH_EXPR));
2964 if (may_optimize_p)
2965 may_have_exposed_new_symbols
2966 |= eliminate_redundant_computations (stmt, ann);
2968 /* Record any additional equivalences created by this statement. */
2969 if (TREE_CODE (stmt) == MODIFY_EXPR)
2970 record_equivalences_from_stmt (stmt,
2971 may_optimize_p,
2972 ann);
2974 /* If STMT is a COND_EXPR and it was modified, then we may know
2975 where it goes. If that is the case, then mark the CFG as altered.
2977 This will cause us to later call remove_unreachable_blocks and
2978 cleanup_tree_cfg when it is safe to do so. It is not safe to
2979 clean things up here since removal of edges and such can trigger
2980 the removal of PHI nodes, which in turn can release SSA_NAMEs to
2981 the manager.
2983 That's all fine and good, except that once SSA_NAMEs are released
2984 to the manager, we must not call create_ssa_name until all references
2985 to released SSA_NAMEs have been eliminated.
2987 All references to the deleted SSA_NAMEs can not be eliminated until
2988 we remove unreachable blocks.
2990 We can not remove unreachable blocks until after we have completed
2991 any queued jump threading.
2993 We can not complete any queued jump threads until we have taken
2994 appropriate variables out of SSA form. Taking variables out of
2995 SSA form can call create_ssa_name and thus we lose.
2997 Ultimately I suspect we're going to need to change the interface
2998 into the SSA_NAME manager. */
3000 if (ann->modified)
3002 tree val = NULL;
3004 if (TREE_CODE (stmt) == COND_EXPR)
3005 val = COND_EXPR_COND (stmt);
3006 else if (TREE_CODE (stmt) == SWITCH_EXPR)
3007 val = SWITCH_COND (stmt);
3009 if (val && TREE_CODE (val) == INTEGER_CST && find_taken_edge (bb, val))
3010 cfg_altered = true;
3012 /* If we simplified a statement in such a way as to be shown that it
3013 cannot trap, update the eh information and the cfg to match. */
3014 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
3016 bitmap_set_bit (need_eh_cleanup, bb->index);
3017 if (dump_file && (dump_flags & TDF_DETAILS))
3018 fprintf (dump_file, " Flagged to clear EH edges.\n");
3022 if (may_have_exposed_new_symbols)
3023 VEC_safe_push (tree, heap, stmts_to_rescan, bsi_stmt (si));
3026 /* Replace the RHS of STMT with NEW_RHS. If RHS can be found in the
3027 available expression hashtable, then return the LHS from the hash
3028 table.
3030 If INSERT is true, then we also update the available expression
3031 hash table to account for the changes made to STMT. */
3033 static tree
3034 update_rhs_and_lookup_avail_expr (tree stmt, tree new_rhs, bool insert)
3036 tree cached_lhs = NULL;
3038 /* Remove the old entry from the hash table. */
3039 if (insert)
3041 struct expr_hash_elt element;
3043 initialize_hash_element (stmt, NULL, &element);
3044 htab_remove_elt_with_hash (avail_exprs, &element, element.hash);
3047 /* Now update the RHS of the assignment. */
3048 TREE_OPERAND (stmt, 1) = new_rhs;
3050 /* Now lookup the updated statement in the hash table. */
3051 cached_lhs = lookup_avail_expr (stmt, insert);
3053 /* We have now called lookup_avail_expr twice with two different
3054 versions of this same statement, once in optimize_stmt, once here.
3056 We know the call in optimize_stmt did not find an existing entry
3057 in the hash table, so a new entry was created. At the same time
3058 this statement was pushed onto the AVAIL_EXPRS_STACK vector.
3060 If this call failed to find an existing entry on the hash table,
3061 then the new version of this statement was entered into the
3062 hash table. And this statement was pushed onto BLOCK_AVAIL_EXPR
3063 for the second time. So there are two copies on BLOCK_AVAIL_EXPRs
3065 If this call succeeded, we still have one copy of this statement
3066 on the BLOCK_AVAIL_EXPRs vector.
3068 For both cases, we need to pop the most recent entry off the
3069 BLOCK_AVAIL_EXPRs vector. For the case where we never found this
3070 statement in the hash tables, that will leave precisely one
3071 copy of this statement on BLOCK_AVAIL_EXPRs. For the case where
3072 we found a copy of this statement in the second hash table lookup
3073 we want _no_ copies of this statement in BLOCK_AVAIL_EXPRs. */
3074 if (insert)
3075 VEC_pop (tree, avail_exprs_stack);
3077 /* And make sure we record the fact that we modified this
3078 statement. */
3079 mark_stmt_modified (stmt);
3081 return cached_lhs;
3084 /* Search for an existing instance of STMT in the AVAIL_EXPRS table. If
3085 found, return its LHS. Otherwise insert STMT in the table and return
3086 NULL_TREE.
3088 Also, when an expression is first inserted in the AVAIL_EXPRS table, it
3089 is also added to the stack pointed to by BLOCK_AVAIL_EXPRS_P, so that they
3090 can be removed when we finish processing this block and its children.
3092 NOTE: This function assumes that STMT is a MODIFY_EXPR node that
3093 contains no CALL_EXPR on its RHS and makes no volatile nor
3094 aliased references. */
3096 static tree
3097 lookup_avail_expr (tree stmt, bool insert)
3099 void **slot;
3100 tree lhs;
3101 tree temp;
3102 struct expr_hash_elt *element = xmalloc (sizeof (struct expr_hash_elt));
3104 lhs = TREE_CODE (stmt) == MODIFY_EXPR ? TREE_OPERAND (stmt, 0) : NULL;
3106 initialize_hash_element (stmt, lhs, element);
3108 /* Don't bother remembering constant assignments and copy operations.
3109 Constants and copy operations are handled by the constant/copy propagator
3110 in optimize_stmt. */
3111 if (TREE_CODE (element->rhs) == SSA_NAME
3112 || is_gimple_min_invariant (element->rhs))
3114 free (element);
3115 return NULL_TREE;
3118 /* If this is an equality test against zero, see if we have recorded a
3119 nonzero value for the variable in question. */
3120 if ((TREE_CODE (element->rhs) == EQ_EXPR
3121 || TREE_CODE (element->rhs) == NE_EXPR)
3122 && TREE_CODE (TREE_OPERAND (element->rhs, 0)) == SSA_NAME
3123 && integer_zerop (TREE_OPERAND (element->rhs, 1)))
3125 int indx = SSA_NAME_VERSION (TREE_OPERAND (element->rhs, 0));
3127 if (bitmap_bit_p (nonzero_vars, indx))
3129 tree t = element->rhs;
3130 free (element);
3131 return constant_boolean_node (TREE_CODE (t) != EQ_EXPR,
3132 TREE_TYPE (t));
3136 /* Finally try to find the expression in the main expression hash table. */
3137 slot = htab_find_slot_with_hash (avail_exprs, element, element->hash,
3138 (insert ? INSERT : NO_INSERT));
3139 if (slot == NULL)
3141 free (element);
3142 return NULL_TREE;
3145 if (*slot == NULL)
3147 *slot = (void *) element;
3148 VEC_safe_push (tree, heap, avail_exprs_stack,
3149 stmt ? stmt : element->rhs);
3150 return NULL_TREE;
3153 /* Extract the LHS of the assignment so that it can be used as the current
3154 definition of another variable. */
3155 lhs = ((struct expr_hash_elt *)*slot)->lhs;
3157 /* See if the LHS appears in the CONST_AND_COPIES table. If it does, then
3158 use the value from the const_and_copies table. */
3159 if (TREE_CODE (lhs) == SSA_NAME)
3161 temp = SSA_NAME_VALUE (lhs);
3162 if (temp && TREE_CODE (temp) != VALUE_HANDLE)
3163 lhs = temp;
3166 free (element);
3167 return lhs;
3170 /* Given a condition COND, record into HI_P, LO_P and INVERTED_P the
3171 range of values that result in the conditional having a true value.
3173 Return true if we are successful in extracting a range from COND and
3174 false if we are unsuccessful. */
3176 static bool
3177 extract_range_from_cond (tree cond, tree *hi_p, tree *lo_p, int *inverted_p)
3179 tree op1 = TREE_OPERAND (cond, 1);
3180 tree high, low, type;
3181 int inverted;
3183 type = TREE_TYPE (op1);
3185 /* Experiments have shown that it's rarely, if ever useful to
3186 record ranges for enumerations. Presumably this is due to
3187 the fact that they're rarely used directly. They are typically
3188 cast into an integer type and used that way. */
3189 if (TREE_CODE (type) != INTEGER_TYPE
3190 /* We don't know how to deal with types with variable bounds. */
3191 || TREE_CODE (TYPE_MIN_VALUE (type)) != INTEGER_CST
3192 || TREE_CODE (TYPE_MAX_VALUE (type)) != INTEGER_CST)
3193 return 0;
3195 switch (TREE_CODE (cond))
3197 case EQ_EXPR:
3198 high = low = op1;
3199 inverted = 0;
3200 break;
3202 case NE_EXPR:
3203 high = low = op1;
3204 inverted = 1;
3205 break;
3207 case GE_EXPR:
3208 low = op1;
3209 high = TYPE_MAX_VALUE (type);
3210 inverted = 0;
3211 break;
3213 case GT_EXPR:
3214 high = TYPE_MAX_VALUE (type);
3215 if (!tree_int_cst_lt (op1, high))
3216 return 0;
3217 low = int_const_binop (PLUS_EXPR, op1, integer_one_node, 1);
3218 inverted = 0;
3219 break;
3221 case LE_EXPR:
3222 high = op1;
3223 low = TYPE_MIN_VALUE (type);
3224 inverted = 0;
3225 break;
3227 case LT_EXPR:
3228 low = TYPE_MIN_VALUE (type);
3229 if (!tree_int_cst_lt (low, op1))
3230 return 0;
3231 high = int_const_binop (MINUS_EXPR, op1, integer_one_node, 1);
3232 inverted = 0;
3233 break;
3235 default:
3236 return 0;
3239 *hi_p = high;
3240 *lo_p = low;
3241 *inverted_p = inverted;
3242 return 1;
3245 /* Record a range created by COND for basic block BB. */
3247 static void
3248 record_range (tree cond, basic_block bb)
3250 enum tree_code code = TREE_CODE (cond);
3252 /* We explicitly ignore NE_EXPRs and all the unordered comparisons.
3253 They rarely allow for meaningful range optimizations and significantly
3254 complicate the implementation. */
3255 if ((code == LT_EXPR || code == LE_EXPR || code == GT_EXPR
3256 || code == GE_EXPR || code == EQ_EXPR)
3257 && TREE_CODE (TREE_TYPE (TREE_OPERAND (cond, 1))) == INTEGER_TYPE)
3259 struct vrp_hash_elt *vrp_hash_elt;
3260 struct vrp_element *element;
3261 VEC(vrp_element_p,heap) **vrp_records_p;
3262 void **slot;
3265 vrp_hash_elt = xmalloc (sizeof (struct vrp_hash_elt));
3266 vrp_hash_elt->var = TREE_OPERAND (cond, 0);
3267 vrp_hash_elt->records = NULL;
3268 slot = htab_find_slot (vrp_data, vrp_hash_elt, INSERT);
3270 if (*slot == NULL)
3271 *slot = (void *) vrp_hash_elt;
3272 else
3273 vrp_free (vrp_hash_elt);
3275 vrp_hash_elt = (struct vrp_hash_elt *) *slot;
3276 vrp_records_p = &vrp_hash_elt->records;
3278 element = ggc_alloc (sizeof (struct vrp_element));
3279 element->low = NULL;
3280 element->high = NULL;
3281 element->cond = cond;
3282 element->bb = bb;
3284 VEC_safe_push (vrp_element_p, heap, *vrp_records_p, element);
3285 VEC_safe_push (tree, heap, vrp_variables_stack, TREE_OPERAND (cond, 0));
3289 /* Hashing and equality functions for VRP_DATA.
3291 Since this hash table is addressed by SSA_NAMEs, we can hash on
3292 their version number and equality can be determined with a
3293 pointer comparison. */
3295 static hashval_t
3296 vrp_hash (const void *p)
3298 tree var = ((struct vrp_hash_elt *)p)->var;
3300 return SSA_NAME_VERSION (var);
3303 static int
3304 vrp_eq (const void *p1, const void *p2)
3306 tree var1 = ((struct vrp_hash_elt *)p1)->var;
3307 tree var2 = ((struct vrp_hash_elt *)p2)->var;
3309 return var1 == var2;
3312 /* Hashing and equality functions for AVAIL_EXPRS. The table stores
3313 MODIFY_EXPR statements. We compute a value number for expressions using
3314 the code of the expression and the SSA numbers of its operands. */
3316 static hashval_t
3317 avail_expr_hash (const void *p)
3319 tree stmt = ((struct expr_hash_elt *)p)->stmt;
3320 tree rhs = ((struct expr_hash_elt *)p)->rhs;
3321 tree vuse;
3322 ssa_op_iter iter;
3323 hashval_t val = 0;
3325 /* iterative_hash_expr knows how to deal with any expression and
3326 deals with commutative operators as well, so just use it instead
3327 of duplicating such complexities here. */
3328 val = iterative_hash_expr (rhs, val);
3330 /* If the hash table entry is not associated with a statement, then we
3331 can just hash the expression and not worry about virtual operands
3332 and such. */
3333 if (!stmt || !stmt_ann (stmt))
3334 return val;
3336 /* Add the SSA version numbers of every vuse operand. This is important
3337 because compound variables like arrays are not renamed in the
3338 operands. Rather, the rename is done on the virtual variable
3339 representing all the elements of the array. */
3340 FOR_EACH_SSA_TREE_OPERAND (vuse, stmt, iter, SSA_OP_VUSE)
3341 val = iterative_hash_expr (vuse, val);
3343 return val;
3346 static hashval_t
3347 real_avail_expr_hash (const void *p)
3349 return ((const struct expr_hash_elt *)p)->hash;
3352 static int
3353 avail_expr_eq (const void *p1, const void *p2)
3355 tree stmt1 = ((struct expr_hash_elt *)p1)->stmt;
3356 tree rhs1 = ((struct expr_hash_elt *)p1)->rhs;
3357 tree stmt2 = ((struct expr_hash_elt *)p2)->stmt;
3358 tree rhs2 = ((struct expr_hash_elt *)p2)->rhs;
3360 /* If they are the same physical expression, return true. */
3361 if (rhs1 == rhs2 && stmt1 == stmt2)
3362 return true;
3364 /* If their codes are not equal, then quit now. */
3365 if (TREE_CODE (rhs1) != TREE_CODE (rhs2))
3366 return false;
3368 /* In case of a collision, both RHS have to be identical and have the
3369 same VUSE operands. */
3370 if ((TREE_TYPE (rhs1) == TREE_TYPE (rhs2)
3371 || lang_hooks.types_compatible_p (TREE_TYPE (rhs1), TREE_TYPE (rhs2)))
3372 && operand_equal_p (rhs1, rhs2, OEP_PURE_SAME))
3374 bool ret = compare_ssa_operands_equal (stmt1, stmt2, SSA_OP_VUSE);
3375 gcc_assert (!ret || ((struct expr_hash_elt *)p1)->hash
3376 == ((struct expr_hash_elt *)p2)->hash);
3377 return ret;
3380 return false;