* HACKING: Update instructions for classpath import.
[official-gcc.git] / gcc / tree-ssa-dom.c
blob1b78b6d5f3971b5875e90fcc8411ee4aaf2b9d57
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;
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;
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 simplify anything later, so just query the hashtable. */
733 cached_lhs = fold (TREE_OPERAND (stmt, 1));
734 if (TREE_CODE (cached_lhs) != SSA_NAME
735 && !is_gimple_min_invariant (cached_lhs))
736 cached_lhs = lookup_avail_expr (stmt, false);
739 /* Restore the statement's original uses/defs. */
740 i = 0;
741 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE | SSA_OP_VUSE)
742 SET_USE (use_p, copy[i++]);
744 free (copy);
747 /* Record the context sensitive equivalence if we were able
748 to simplify this statement. */
749 if (cached_lhs
750 && (TREE_CODE (cached_lhs) == SSA_NAME
751 || is_gimple_min_invariant (cached_lhs)))
752 record_const_or_copy (TREE_OPERAND (stmt, 0), cached_lhs);
755 /* If we stopped at a COND_EXPR or SWITCH_EXPR, see if we know which arm
756 will be taken. */
757 if (stmt
758 && (TREE_CODE (stmt) == COND_EXPR
759 || TREE_CODE (stmt) == GOTO_EXPR
760 || TREE_CODE (stmt) == SWITCH_EXPR))
762 tree cond, cached_lhs;
764 /* Now temporarily cprop the operands and try to find the resulting
765 expression in the hash tables. */
766 if (TREE_CODE (stmt) == COND_EXPR)
767 cond = COND_EXPR_COND (stmt);
768 else if (TREE_CODE (stmt) == GOTO_EXPR)
769 cond = GOTO_DESTINATION (stmt);
770 else
771 cond = SWITCH_COND (stmt);
773 if (COMPARISON_CLASS_P (cond))
775 tree dummy_cond, op0, op1;
776 enum tree_code cond_code;
778 op0 = TREE_OPERAND (cond, 0);
779 op1 = TREE_OPERAND (cond, 1);
780 cond_code = TREE_CODE (cond);
782 /* Get the current value of both operands. */
783 if (TREE_CODE (op0) == SSA_NAME)
785 tree tmp = SSA_NAME_VALUE (op0);
786 if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
787 op0 = tmp;
790 if (TREE_CODE (op1) == SSA_NAME)
792 tree tmp = SSA_NAME_VALUE (op1);
793 if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
794 op1 = tmp;
797 /* Stuff the operator and operands into our dummy conditional
798 expression, creating the dummy conditional if necessary. */
799 dummy_cond = walk_data->global_data;
800 if (! dummy_cond)
802 dummy_cond = build (cond_code, boolean_type_node, op0, op1);
803 dummy_cond = build (COND_EXPR, void_type_node,
804 dummy_cond, NULL, NULL);
805 walk_data->global_data = dummy_cond;
807 else
809 TREE_SET_CODE (COND_EXPR_COND (dummy_cond), cond_code);
810 TREE_OPERAND (COND_EXPR_COND (dummy_cond), 0) = op0;
811 TREE_OPERAND (COND_EXPR_COND (dummy_cond), 1) = op1;
814 /* If the conditional folds to an invariant, then we are done,
815 otherwise look it up in the hash tables. */
816 cached_lhs = local_fold (COND_EXPR_COND (dummy_cond));
817 if (! is_gimple_min_invariant (cached_lhs))
819 cached_lhs = lookup_avail_expr (dummy_cond, false);
820 if (!cached_lhs || ! is_gimple_min_invariant (cached_lhs))
821 cached_lhs = simplify_cond_and_lookup_avail_expr (dummy_cond,
822 NULL,
823 false);
826 /* We can have conditionals which just test the state of a
827 variable rather than use a relational operator. These are
828 simpler to handle. */
829 else if (TREE_CODE (cond) == SSA_NAME)
831 cached_lhs = cond;
832 cached_lhs = SSA_NAME_VALUE (cached_lhs);
833 if (cached_lhs && ! is_gimple_min_invariant (cached_lhs))
834 cached_lhs = NULL;
836 else
837 cached_lhs = lookup_avail_expr (stmt, false);
839 if (cached_lhs)
841 edge taken_edge = find_taken_edge (e->dest, cached_lhs);
842 basic_block dest = (taken_edge ? taken_edge->dest : NULL);
844 if (dest == e->dest)
845 return;
847 /* If we have a known destination for the conditional, then
848 we can perform this optimization, which saves at least one
849 conditional jump each time it applies since we get to
850 bypass the conditional at our original destination. */
851 if (dest)
853 struct edge_info *edge_info;
855 if (e->aux)
856 edge_info = e->aux;
857 else
858 edge_info = allocate_edge_info (e);
859 edge_info->redirection_target = taken_edge;
860 bitmap_set_bit (threaded_blocks, e->dest->index);
867 /* Initialize local stacks for this optimizer and record equivalences
868 upon entry to BB. Equivalences can come from the edge traversed to
869 reach BB or they may come from PHI nodes at the start of BB. */
871 static void
872 dom_opt_initialize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
873 basic_block bb)
875 if (dump_file && (dump_flags & TDF_DETAILS))
876 fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
878 /* Push a marker on the stacks of local information so that we know how
879 far to unwind when we finalize this block. */
880 VEC_safe_push (tree, heap, avail_exprs_stack, NULL_TREE);
881 VEC_safe_push (tree, heap, const_and_copies_stack, NULL_TREE);
882 VEC_safe_push (tree, heap, nonzero_vars_stack, NULL_TREE);
883 VEC_safe_push (tree, heap, vrp_variables_stack, NULL_TREE);
885 record_equivalences_from_incoming_edge (bb);
887 /* PHI nodes can create equivalences too. */
888 record_equivalences_from_phis (bb);
891 /* Given an expression EXPR (a relational expression or a statement),
892 initialize the hash table element pointed to by ELEMENT. */
894 static void
895 initialize_hash_element (tree expr, tree lhs, struct expr_hash_elt *element)
897 /* Hash table elements may be based on conditional expressions or statements.
899 For the former case, we have no annotation and we want to hash the
900 conditional expression. In the latter case we have an annotation and
901 we want to record the expression the statement evaluates. */
902 if (COMPARISON_CLASS_P (expr) || TREE_CODE (expr) == TRUTH_NOT_EXPR)
904 element->stmt = NULL;
905 element->rhs = expr;
907 else if (TREE_CODE (expr) == COND_EXPR)
909 element->stmt = expr;
910 element->rhs = COND_EXPR_COND (expr);
912 else if (TREE_CODE (expr) == SWITCH_EXPR)
914 element->stmt = expr;
915 element->rhs = SWITCH_COND (expr);
917 else if (TREE_CODE (expr) == RETURN_EXPR && TREE_OPERAND (expr, 0))
919 element->stmt = expr;
920 element->rhs = TREE_OPERAND (TREE_OPERAND (expr, 0), 1);
922 else if (TREE_CODE (expr) == GOTO_EXPR)
924 element->stmt = expr;
925 element->rhs = GOTO_DESTINATION (expr);
927 else
929 element->stmt = expr;
930 element->rhs = TREE_OPERAND (expr, 1);
933 element->lhs = lhs;
934 element->hash = avail_expr_hash (element);
937 /* Remove all the expressions in LOCALS from TABLE, stopping when there are
938 LIMIT entries left in LOCALs. */
940 static void
941 remove_local_expressions_from_table (void)
943 /* Remove all the expressions made available in this block. */
944 while (VEC_length (tree, avail_exprs_stack) > 0)
946 struct expr_hash_elt element;
947 tree expr = VEC_pop (tree, avail_exprs_stack);
949 if (expr == NULL_TREE)
950 break;
952 initialize_hash_element (expr, NULL, &element);
953 htab_remove_elt_with_hash (avail_exprs, &element, element.hash);
957 /* Use the SSA_NAMES in LOCALS to restore TABLE to its original
958 state, stopping when there are LIMIT entries left in LOCALs. */
960 static void
961 restore_nonzero_vars_to_original_value (void)
963 while (VEC_length (tree, nonzero_vars_stack) > 0)
965 tree name = VEC_pop (tree, nonzero_vars_stack);
967 if (name == NULL)
968 break;
970 bitmap_clear_bit (nonzero_vars, SSA_NAME_VERSION (name));
974 /* Use the source/dest pairs in CONST_AND_COPIES_STACK to restore
975 CONST_AND_COPIES to its original state, stopping when we hit a
976 NULL marker. */
978 static void
979 restore_vars_to_original_value (void)
981 while (VEC_length (tree, const_and_copies_stack) > 0)
983 tree prev_value, dest;
985 dest = VEC_pop (tree, const_and_copies_stack);
987 if (dest == NULL)
988 break;
990 prev_value = VEC_pop (tree, const_and_copies_stack);
991 SSA_NAME_VALUE (dest) = prev_value;
995 /* We have finished processing the dominator children of BB, perform
996 any finalization actions in preparation for leaving this node in
997 the dominator tree. */
999 static void
1000 dom_opt_finalize_block (struct dom_walk_data *walk_data, basic_block bb)
1002 tree last;
1004 /* If we are at a leaf node in the dominator tree, see if we can thread
1005 the edge from BB through its successor.
1007 Do this before we remove entries from our equivalence tables. */
1008 if (single_succ_p (bb)
1009 && (single_succ_edge (bb)->flags & EDGE_ABNORMAL) == 0
1010 && (get_immediate_dominator (CDI_DOMINATORS, single_succ (bb)) != bb
1011 || phi_nodes (single_succ (bb))))
1014 thread_across_edge (walk_data, single_succ_edge (bb));
1016 else if ((last = last_stmt (bb))
1017 && TREE_CODE (last) == COND_EXPR
1018 && (COMPARISON_CLASS_P (COND_EXPR_COND (last))
1019 || TREE_CODE (COND_EXPR_COND (last)) == SSA_NAME)
1020 && EDGE_COUNT (bb->succs) == 2
1021 && (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL) == 0
1022 && (EDGE_SUCC (bb, 1)->flags & EDGE_ABNORMAL) == 0)
1024 edge true_edge, false_edge;
1026 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1028 /* If the THEN arm is the end of a dominator tree or has PHI nodes,
1029 then try to thread through its edge. */
1030 if (get_immediate_dominator (CDI_DOMINATORS, true_edge->dest) != bb
1031 || phi_nodes (true_edge->dest))
1033 struct edge_info *edge_info;
1034 unsigned int i;
1036 /* Push a marker onto the available expression stack so that we
1037 unwind any expressions related to the TRUE arm before processing
1038 the false arm below. */
1039 VEC_safe_push (tree, heap, avail_exprs_stack, NULL_TREE);
1040 VEC_safe_push (tree, heap, const_and_copies_stack, NULL_TREE);
1042 edge_info = true_edge->aux;
1044 /* If we have info associated with this edge, record it into
1045 our equivalency tables. */
1046 if (edge_info)
1048 tree *cond_equivalences = edge_info->cond_equivalences;
1049 tree lhs = edge_info->lhs;
1050 tree rhs = edge_info->rhs;
1052 /* If we have a simple NAME = VALUE equivalency record it. */
1053 if (lhs && TREE_CODE (lhs) == SSA_NAME)
1054 record_const_or_copy (lhs, rhs);
1056 /* If we have 0 = COND or 1 = COND equivalences, record them
1057 into our expression hash tables. */
1058 if (cond_equivalences)
1059 for (i = 0; i < edge_info->max_cond_equivalences; i += 2)
1061 tree expr = cond_equivalences[i];
1062 tree value = cond_equivalences[i + 1];
1064 record_cond (expr, value);
1068 /* Now thread the edge. */
1069 thread_across_edge (walk_data, true_edge);
1071 /* And restore the various tables to their state before
1072 we threaded this edge. */
1073 remove_local_expressions_from_table ();
1074 restore_vars_to_original_value ();
1077 /* Similarly for the ELSE arm. */
1078 if (get_immediate_dominator (CDI_DOMINATORS, false_edge->dest) != bb
1079 || phi_nodes (false_edge->dest))
1081 struct edge_info *edge_info;
1082 unsigned int i;
1084 edge_info = false_edge->aux;
1086 /* If we have info associated with this edge, record it into
1087 our equivalency tables. */
1088 if (edge_info)
1090 tree *cond_equivalences = edge_info->cond_equivalences;
1091 tree lhs = edge_info->lhs;
1092 tree rhs = edge_info->rhs;
1094 /* If we have a simple NAME = VALUE equivalency record it. */
1095 if (lhs && TREE_CODE (lhs) == SSA_NAME)
1096 record_const_or_copy (lhs, rhs);
1098 /* If we have 0 = COND or 1 = COND equivalences, record them
1099 into our expression hash tables. */
1100 if (cond_equivalences)
1101 for (i = 0; i < edge_info->max_cond_equivalences; i += 2)
1103 tree expr = cond_equivalences[i];
1104 tree value = cond_equivalences[i + 1];
1106 record_cond (expr, value);
1110 thread_across_edge (walk_data, false_edge);
1112 /* No need to remove local expressions from our tables
1113 or restore vars to their original value as that will
1114 be done immediately below. */
1118 remove_local_expressions_from_table ();
1119 restore_nonzero_vars_to_original_value ();
1120 restore_vars_to_original_value ();
1122 /* Remove VRP records associated with this basic block. They are no
1123 longer valid.
1125 To be efficient, we note which variables have had their values
1126 constrained in this block. So walk over each variable in the
1127 VRP_VARIABLEs array. */
1128 while (VEC_length (tree, vrp_variables_stack) > 0)
1130 tree var = VEC_pop (tree, vrp_variables_stack);
1131 struct vrp_hash_elt vrp_hash_elt, *vrp_hash_elt_p;
1132 void **slot;
1134 /* Each variable has a stack of value range records. We want to
1135 invalidate those associated with our basic block. So we walk
1136 the array backwards popping off records associated with our
1137 block. Once we hit a record not associated with our block
1138 we are done. */
1139 VEC(vrp_element_p,heap) **var_vrp_records;
1141 if (var == NULL)
1142 break;
1144 vrp_hash_elt.var = var;
1145 vrp_hash_elt.records = NULL;
1147 slot = htab_find_slot (vrp_data, &vrp_hash_elt, NO_INSERT);
1149 vrp_hash_elt_p = (struct vrp_hash_elt *) *slot;
1150 var_vrp_records = &vrp_hash_elt_p->records;
1152 while (VEC_length (vrp_element_p, *var_vrp_records) > 0)
1154 struct vrp_element *element
1155 = VEC_last (vrp_element_p, *var_vrp_records);
1157 if (element->bb != bb)
1158 break;
1160 VEC_pop (vrp_element_p, *var_vrp_records);
1164 /* If we queued any statements to rescan in this block, then
1165 go ahead and rescan them now. */
1166 while (VEC_length (tree, stmts_to_rescan) > 0)
1168 tree stmt = VEC_last (tree, stmts_to_rescan);
1169 basic_block stmt_bb = bb_for_stmt (stmt);
1171 if (stmt_bb != bb)
1172 break;
1174 VEC_pop (tree, stmts_to_rescan);
1175 mark_new_vars_to_rename (stmt);
1179 /* PHI nodes can create equivalences too.
1181 Ignoring any alternatives which are the same as the result, if
1182 all the alternatives are equal, then the PHI node creates an
1183 equivalence.
1185 Additionally, if all the PHI alternatives are known to have a nonzero
1186 value, then the result of this PHI is known to have a nonzero value,
1187 even if we do not know its exact value. */
1189 static void
1190 record_equivalences_from_phis (basic_block bb)
1192 tree phi;
1194 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1196 tree lhs = PHI_RESULT (phi);
1197 tree rhs = NULL;
1198 int i;
1200 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1202 tree t = PHI_ARG_DEF (phi, i);
1204 /* Ignore alternatives which are the same as our LHS. Since
1205 LHS is a PHI_RESULT, it is known to be a SSA_NAME, so we
1206 can simply compare pointers. */
1207 if (lhs == t)
1208 continue;
1210 /* If we have not processed an alternative yet, then set
1211 RHS to this alternative. */
1212 if (rhs == NULL)
1213 rhs = t;
1214 /* If we have processed an alternative (stored in RHS), then
1215 see if it is equal to this one. If it isn't, then stop
1216 the search. */
1217 else if (! operand_equal_for_phi_arg_p (rhs, t))
1218 break;
1221 /* If we had no interesting alternatives, then all the RHS alternatives
1222 must have been the same as LHS. */
1223 if (!rhs)
1224 rhs = lhs;
1226 /* If we managed to iterate through each PHI alternative without
1227 breaking out of the loop, then we have a PHI which may create
1228 a useful equivalence. We do not need to record unwind data for
1229 this, since this is a true assignment and not an equivalence
1230 inferred from a comparison. All uses of this ssa name are dominated
1231 by this assignment, so unwinding just costs time and space. */
1232 if (i == PHI_NUM_ARGS (phi)
1233 && may_propagate_copy (lhs, rhs))
1234 SSA_NAME_VALUE (lhs) = rhs;
1236 /* Now see if we know anything about the nonzero property for the
1237 result of this PHI. */
1238 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1240 if (!PHI_ARG_NONZERO (phi, i))
1241 break;
1244 if (i == PHI_NUM_ARGS (phi))
1245 bitmap_set_bit (nonzero_vars, SSA_NAME_VERSION (PHI_RESULT (phi)));
1249 /* Ignoring loop backedges, if BB has precisely one incoming edge then
1250 return that edge. Otherwise return NULL. */
1251 static edge
1252 single_incoming_edge_ignoring_loop_edges (basic_block bb)
1254 edge retval = NULL;
1255 edge e;
1256 edge_iterator ei;
1258 FOR_EACH_EDGE (e, ei, bb->preds)
1260 /* A loop back edge can be identified by the destination of
1261 the edge dominating the source of the edge. */
1262 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
1263 continue;
1265 /* If we have already seen a non-loop edge, then we must have
1266 multiple incoming non-loop edges and thus we return NULL. */
1267 if (retval)
1268 return NULL;
1270 /* This is the first non-loop incoming edge we have found. Record
1271 it. */
1272 retval = e;
1275 return retval;
1278 /* Record any equivalences created by the incoming edge to BB. If BB
1279 has more than one incoming edge, then no equivalence is created. */
1281 static void
1282 record_equivalences_from_incoming_edge (basic_block bb)
1284 edge e;
1285 basic_block parent;
1286 struct edge_info *edge_info;
1288 /* If our parent block ended with a control statement, then we may be
1289 able to record some equivalences based on which outgoing edge from
1290 the parent was followed. */
1291 parent = get_immediate_dominator (CDI_DOMINATORS, bb);
1293 e = single_incoming_edge_ignoring_loop_edges (bb);
1295 /* If we had a single incoming edge from our parent block, then enter
1296 any data associated with the edge into our tables. */
1297 if (e && e->src == parent)
1299 unsigned int i;
1301 edge_info = e->aux;
1303 if (edge_info)
1305 tree lhs = edge_info->lhs;
1306 tree rhs = edge_info->rhs;
1307 tree *cond_equivalences = edge_info->cond_equivalences;
1309 if (lhs)
1310 record_equality (lhs, rhs);
1312 if (cond_equivalences)
1314 bool recorded_range = false;
1315 for (i = 0; i < edge_info->max_cond_equivalences; i += 2)
1317 tree expr = cond_equivalences[i];
1318 tree value = cond_equivalences[i + 1];
1320 record_cond (expr, value);
1322 /* For the first true equivalence, record range
1323 information. We only do this for the first
1324 true equivalence as it should dominate any
1325 later true equivalences. */
1326 if (! recorded_range
1327 && COMPARISON_CLASS_P (expr)
1328 && value == boolean_true_node
1329 && TREE_CONSTANT (TREE_OPERAND (expr, 1)))
1331 record_range (expr, bb);
1332 recorded_range = true;
1340 /* Dump SSA statistics on FILE. */
1342 void
1343 dump_dominator_optimization_stats (FILE *file)
1345 long n_exprs;
1347 fprintf (file, "Total number of statements: %6ld\n\n",
1348 opt_stats.num_stmts);
1349 fprintf (file, "Exprs considered for dominator optimizations: %6ld\n",
1350 opt_stats.num_exprs_considered);
1352 n_exprs = opt_stats.num_exprs_considered;
1353 if (n_exprs == 0)
1354 n_exprs = 1;
1356 fprintf (file, " Redundant expressions eliminated: %6ld (%.0f%%)\n",
1357 opt_stats.num_re, PERCENT (opt_stats.num_re,
1358 n_exprs));
1359 fprintf (file, " Constants propagated: %6ld\n",
1360 opt_stats.num_const_prop);
1361 fprintf (file, " Copies propagated: %6ld\n",
1362 opt_stats.num_copy_prop);
1364 fprintf (file, "\nTotal number of DOM iterations: %6ld\n",
1365 opt_stats.num_iterations);
1367 fprintf (file, "\nHash table statistics:\n");
1369 fprintf (file, " avail_exprs: ");
1370 htab_statistics (file, avail_exprs);
1374 /* Dump SSA statistics on stderr. */
1376 void
1377 debug_dominator_optimization_stats (void)
1379 dump_dominator_optimization_stats (stderr);
1383 /* Dump statistics for the hash table HTAB. */
1385 static void
1386 htab_statistics (FILE *file, htab_t htab)
1388 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
1389 (long) htab_size (htab),
1390 (long) htab_elements (htab),
1391 htab_collisions (htab));
1394 /* Record the fact that VAR has a nonzero value, though we may not know
1395 its exact value. Note that if VAR is already known to have a nonzero
1396 value, then we do nothing. */
1398 static void
1399 record_var_is_nonzero (tree var)
1401 int indx = SSA_NAME_VERSION (var);
1403 if (bitmap_bit_p (nonzero_vars, indx))
1404 return;
1406 /* Mark it in the global table. */
1407 bitmap_set_bit (nonzero_vars, indx);
1409 /* Record this SSA_NAME so that we can reset the global table
1410 when we leave this block. */
1411 VEC_safe_push (tree, heap, nonzero_vars_stack, var);
1414 /* Enter a statement into the true/false expression hash table indicating
1415 that the condition COND has the value VALUE. */
1417 static void
1418 record_cond (tree cond, tree value)
1420 struct expr_hash_elt *element = xmalloc (sizeof (struct expr_hash_elt));
1421 void **slot;
1423 initialize_hash_element (cond, value, element);
1425 slot = htab_find_slot_with_hash (avail_exprs, (void *)element,
1426 element->hash, INSERT);
1427 if (*slot == NULL)
1429 *slot = (void *) element;
1430 VEC_safe_push (tree, heap, avail_exprs_stack, cond);
1432 else
1433 free (element);
1436 /* Build a new conditional using NEW_CODE, OP0 and OP1 and store
1437 the new conditional into *p, then store a boolean_true_node
1438 into *(p + 1). */
1440 static void
1441 build_and_record_new_cond (enum tree_code new_code, tree op0, tree op1, tree *p)
1443 *p = build2 (new_code, boolean_type_node, op0, op1);
1444 p++;
1445 *p = boolean_true_node;
1448 /* Record that COND is true and INVERTED is false into the edge information
1449 structure. Also record that any conditions dominated by COND are true
1450 as well.
1452 For example, if a < b is true, then a <= b must also be true. */
1454 static void
1455 record_conditions (struct edge_info *edge_info, tree cond, tree inverted)
1457 tree op0, op1;
1459 if (!COMPARISON_CLASS_P (cond))
1460 return;
1462 op0 = TREE_OPERAND (cond, 0);
1463 op1 = TREE_OPERAND (cond, 1);
1465 switch (TREE_CODE (cond))
1467 case LT_EXPR:
1468 case GT_EXPR:
1469 edge_info->max_cond_equivalences = 12;
1470 edge_info->cond_equivalences = xmalloc (12 * sizeof (tree));
1471 build_and_record_new_cond ((TREE_CODE (cond) == LT_EXPR
1472 ? LE_EXPR : GE_EXPR),
1473 op0, op1, &edge_info->cond_equivalences[4]);
1474 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1475 &edge_info->cond_equivalences[6]);
1476 build_and_record_new_cond (NE_EXPR, op0, op1,
1477 &edge_info->cond_equivalences[8]);
1478 build_and_record_new_cond (LTGT_EXPR, op0, op1,
1479 &edge_info->cond_equivalences[10]);
1480 break;
1482 case GE_EXPR:
1483 case LE_EXPR:
1484 edge_info->max_cond_equivalences = 6;
1485 edge_info->cond_equivalences = xmalloc (6 * sizeof (tree));
1486 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1487 &edge_info->cond_equivalences[4]);
1488 break;
1490 case EQ_EXPR:
1491 edge_info->max_cond_equivalences = 10;
1492 edge_info->cond_equivalences = xmalloc (10 * sizeof (tree));
1493 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1494 &edge_info->cond_equivalences[4]);
1495 build_and_record_new_cond (LE_EXPR, op0, op1,
1496 &edge_info->cond_equivalences[6]);
1497 build_and_record_new_cond (GE_EXPR, op0, op1,
1498 &edge_info->cond_equivalences[8]);
1499 break;
1501 case UNORDERED_EXPR:
1502 edge_info->max_cond_equivalences = 16;
1503 edge_info->cond_equivalences = xmalloc (16 * sizeof (tree));
1504 build_and_record_new_cond (NE_EXPR, op0, op1,
1505 &edge_info->cond_equivalences[4]);
1506 build_and_record_new_cond (UNLE_EXPR, op0, op1,
1507 &edge_info->cond_equivalences[6]);
1508 build_and_record_new_cond (UNGE_EXPR, op0, op1,
1509 &edge_info->cond_equivalences[8]);
1510 build_and_record_new_cond (UNEQ_EXPR, op0, op1,
1511 &edge_info->cond_equivalences[10]);
1512 build_and_record_new_cond (UNLT_EXPR, op0, op1,
1513 &edge_info->cond_equivalences[12]);
1514 build_and_record_new_cond (UNGT_EXPR, op0, op1,
1515 &edge_info->cond_equivalences[14]);
1516 break;
1518 case UNLT_EXPR:
1519 case UNGT_EXPR:
1520 edge_info->max_cond_equivalences = 8;
1521 edge_info->cond_equivalences = xmalloc (8 * sizeof (tree));
1522 build_and_record_new_cond ((TREE_CODE (cond) == UNLT_EXPR
1523 ? UNLE_EXPR : UNGE_EXPR),
1524 op0, op1, &edge_info->cond_equivalences[4]);
1525 build_and_record_new_cond (NE_EXPR, op0, op1,
1526 &edge_info->cond_equivalences[6]);
1527 break;
1529 case UNEQ_EXPR:
1530 edge_info->max_cond_equivalences = 8;
1531 edge_info->cond_equivalences = xmalloc (8 * sizeof (tree));
1532 build_and_record_new_cond (UNLE_EXPR, op0, op1,
1533 &edge_info->cond_equivalences[4]);
1534 build_and_record_new_cond (UNGE_EXPR, op0, op1,
1535 &edge_info->cond_equivalences[6]);
1536 break;
1538 case LTGT_EXPR:
1539 edge_info->max_cond_equivalences = 8;
1540 edge_info->cond_equivalences = xmalloc (8 * sizeof (tree));
1541 build_and_record_new_cond (NE_EXPR, op0, op1,
1542 &edge_info->cond_equivalences[4]);
1543 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1544 &edge_info->cond_equivalences[6]);
1545 break;
1547 default:
1548 edge_info->max_cond_equivalences = 4;
1549 edge_info->cond_equivalences = xmalloc (4 * sizeof (tree));
1550 break;
1553 /* Now store the original true and false conditions into the first
1554 two slots. */
1555 edge_info->cond_equivalences[0] = cond;
1556 edge_info->cond_equivalences[1] = boolean_true_node;
1557 edge_info->cond_equivalences[2] = inverted;
1558 edge_info->cond_equivalences[3] = boolean_false_node;
1561 /* A helper function for record_const_or_copy and record_equality.
1562 Do the work of recording the value and undo info. */
1564 static void
1565 record_const_or_copy_1 (tree x, tree y, tree prev_x)
1567 SSA_NAME_VALUE (x) = y;
1569 VEC_reserve (tree, heap, const_and_copies_stack, 2);
1570 VEC_quick_push (tree, const_and_copies_stack, prev_x);
1571 VEC_quick_push (tree, const_and_copies_stack, x);
1575 /* Return the loop depth of the basic block of the defining statement of X.
1576 This number should not be treated as absolutely correct because the loop
1577 information may not be completely up-to-date when dom runs. However, it
1578 will be relatively correct, and as more passes are taught to keep loop info
1579 up to date, the result will become more and more accurate. */
1582 loop_depth_of_name (tree x)
1584 tree defstmt;
1585 basic_block defbb;
1587 /* If it's not an SSA_NAME, we have no clue where the definition is. */
1588 if (TREE_CODE (x) != SSA_NAME)
1589 return 0;
1591 /* Otherwise return the loop depth of the defining statement's bb.
1592 Note that there may not actually be a bb for this statement, if the
1593 ssa_name is live on entry. */
1594 defstmt = SSA_NAME_DEF_STMT (x);
1595 defbb = bb_for_stmt (defstmt);
1596 if (!defbb)
1597 return 0;
1599 return defbb->loop_depth;
1603 /* Record that X is equal to Y in const_and_copies. Record undo
1604 information in the block-local vector. */
1606 static void
1607 record_const_or_copy (tree x, tree y)
1609 tree prev_x = SSA_NAME_VALUE (x);
1611 if (TREE_CODE (y) == SSA_NAME)
1613 tree tmp = SSA_NAME_VALUE (y);
1614 if (tmp)
1615 y = tmp;
1618 record_const_or_copy_1 (x, y, prev_x);
1621 /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1622 This constrains the cases in which we may treat this as assignment. */
1624 static void
1625 record_equality (tree x, tree y)
1627 tree prev_x = NULL, prev_y = NULL;
1629 if (TREE_CODE (x) == SSA_NAME)
1630 prev_x = SSA_NAME_VALUE (x);
1631 if (TREE_CODE (y) == SSA_NAME)
1632 prev_y = SSA_NAME_VALUE (y);
1634 /* If one of the previous values is invariant, or invariant in more loops
1635 (by depth), then use that.
1636 Otherwise it doesn't matter which value we choose, just so
1637 long as we canonicalize on one value. */
1638 if (TREE_INVARIANT (y))
1640 else if (TREE_INVARIANT (x) || (loop_depth_of_name (x) <= loop_depth_of_name (y)))
1641 prev_x = x, x = y, y = prev_x, prev_x = prev_y;
1642 else if (prev_x && TREE_INVARIANT (prev_x))
1643 x = y, y = prev_x, prev_x = prev_y;
1644 else if (prev_y && TREE_CODE (prev_y) != VALUE_HANDLE)
1645 y = prev_y;
1647 /* After the swapping, we must have one SSA_NAME. */
1648 if (TREE_CODE (x) != SSA_NAME)
1649 return;
1651 /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1652 variable compared against zero. If we're honoring signed zeros,
1653 then we cannot record this value unless we know that the value is
1654 nonzero. */
1655 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (x)))
1656 && (TREE_CODE (y) != REAL_CST
1657 || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (y))))
1658 return;
1660 record_const_or_copy_1 (x, y, prev_x);
1663 /* Return true, if it is ok to do folding of an associative expression.
1664 EXP is the tree for the associative expression. */
1666 static inline bool
1667 unsafe_associative_fp_binop (tree exp)
1669 enum tree_code code = TREE_CODE (exp);
1670 return !(!flag_unsafe_math_optimizations
1671 && (code == MULT_EXPR || code == PLUS_EXPR
1672 || code == MINUS_EXPR)
1673 && FLOAT_TYPE_P (TREE_TYPE (exp)));
1676 /* Returns true when STMT is a simple iv increment. It detects the
1677 following situation:
1679 i_1 = phi (..., i_2)
1680 i_2 = i_1 +/- ... */
1682 static bool
1683 simple_iv_increment_p (tree stmt)
1685 tree lhs, rhs, preinc, phi;
1686 unsigned i;
1688 if (TREE_CODE (stmt) != MODIFY_EXPR)
1689 return false;
1691 lhs = TREE_OPERAND (stmt, 0);
1692 if (TREE_CODE (lhs) != SSA_NAME)
1693 return false;
1695 rhs = TREE_OPERAND (stmt, 1);
1697 if (TREE_CODE (rhs) != PLUS_EXPR
1698 && TREE_CODE (rhs) != MINUS_EXPR)
1699 return false;
1701 preinc = TREE_OPERAND (rhs, 0);
1702 if (TREE_CODE (preinc) != SSA_NAME)
1703 return false;
1705 phi = SSA_NAME_DEF_STMT (preinc);
1706 if (TREE_CODE (phi) != PHI_NODE)
1707 return false;
1709 for (i = 0; i < (unsigned) PHI_NUM_ARGS (phi); i++)
1710 if (PHI_ARG_DEF (phi, i) == lhs)
1711 return true;
1713 return false;
1716 /* STMT is a MODIFY_EXPR for which we were unable to find RHS in the
1717 hash tables. Try to simplify the RHS using whatever equivalences
1718 we may have recorded.
1720 If we are able to simplify the RHS, then lookup the simplified form in
1721 the hash table and return the result. Otherwise return NULL. */
1723 static tree
1724 simplify_rhs_and_lookup_avail_expr (tree stmt, int insert)
1726 tree rhs = TREE_OPERAND (stmt, 1);
1727 enum tree_code rhs_code = TREE_CODE (rhs);
1728 tree result = NULL;
1730 /* If we have lhs = ~x, look and see if we earlier had x = ~y.
1731 In which case we can change this statement to be lhs = y.
1732 Which can then be copy propagated.
1734 Similarly for negation. */
1735 if ((rhs_code == BIT_NOT_EXPR || rhs_code == NEGATE_EXPR)
1736 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
1738 /* Get the definition statement for our RHS. */
1739 tree rhs_def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs, 0));
1741 /* See if the RHS_DEF_STMT has the same form as our statement. */
1742 if (TREE_CODE (rhs_def_stmt) == MODIFY_EXPR
1743 && TREE_CODE (TREE_OPERAND (rhs_def_stmt, 1)) == rhs_code)
1745 tree rhs_def_operand;
1747 rhs_def_operand = TREE_OPERAND (TREE_OPERAND (rhs_def_stmt, 1), 0);
1749 /* Verify that RHS_DEF_OPERAND is a suitable SSA variable. */
1750 if (TREE_CODE (rhs_def_operand) == SSA_NAME
1751 && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs_def_operand))
1752 result = update_rhs_and_lookup_avail_expr (stmt,
1753 rhs_def_operand,
1754 insert);
1758 /* If we have z = (x OP C1), see if we earlier had x = y OP C2.
1759 If OP is associative, create and fold (y OP C2) OP C1 which
1760 should result in (y OP C3), use that as the RHS for the
1761 assignment. Add minus to this, as we handle it specially below. */
1762 if ((associative_tree_code (rhs_code) || rhs_code == MINUS_EXPR)
1763 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME
1764 && is_gimple_min_invariant (TREE_OPERAND (rhs, 1)))
1766 tree rhs_def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs, 0));
1768 /* If the statement defines an induction variable, do not propagate
1769 its value, so that we do not create overlapping life ranges. */
1770 if (simple_iv_increment_p (rhs_def_stmt))
1771 goto dont_fold_assoc;
1773 /* See if the RHS_DEF_STMT has the same form as our statement. */
1774 if (TREE_CODE (rhs_def_stmt) == MODIFY_EXPR)
1776 tree rhs_def_rhs = TREE_OPERAND (rhs_def_stmt, 1);
1777 enum tree_code rhs_def_code = TREE_CODE (rhs_def_rhs);
1779 if ((rhs_code == rhs_def_code && unsafe_associative_fp_binop (rhs))
1780 || (rhs_code == PLUS_EXPR && rhs_def_code == MINUS_EXPR)
1781 || (rhs_code == MINUS_EXPR && rhs_def_code == PLUS_EXPR))
1783 tree def_stmt_op0 = TREE_OPERAND (rhs_def_rhs, 0);
1784 tree def_stmt_op1 = TREE_OPERAND (rhs_def_rhs, 1);
1786 if (TREE_CODE (def_stmt_op0) == SSA_NAME
1787 && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def_stmt_op0)
1788 && is_gimple_min_invariant (def_stmt_op1))
1790 tree outer_const = TREE_OPERAND (rhs, 1);
1791 tree type = TREE_TYPE (TREE_OPERAND (stmt, 0));
1792 tree t;
1794 /* If we care about correct floating point results, then
1795 don't fold x + c1 - c2. Note that we need to take both
1796 the codes and the signs to figure this out. */
1797 if (FLOAT_TYPE_P (type)
1798 && !flag_unsafe_math_optimizations
1799 && (rhs_def_code == PLUS_EXPR
1800 || rhs_def_code == MINUS_EXPR))
1802 bool neg = false;
1804 neg ^= (rhs_code == MINUS_EXPR);
1805 neg ^= (rhs_def_code == MINUS_EXPR);
1806 neg ^= real_isneg (TREE_REAL_CST_PTR (outer_const));
1807 neg ^= real_isneg (TREE_REAL_CST_PTR (def_stmt_op1));
1809 if (neg)
1810 goto dont_fold_assoc;
1813 /* Ho hum. So fold will only operate on the outermost
1814 thingy that we give it, so we have to build the new
1815 expression in two pieces. This requires that we handle
1816 combinations of plus and minus. */
1817 if (rhs_def_code != rhs_code)
1819 if (rhs_def_code == MINUS_EXPR)
1820 t = build (MINUS_EXPR, type, outer_const, def_stmt_op1);
1821 else
1822 t = build (MINUS_EXPR, type, def_stmt_op1, outer_const);
1823 rhs_code = PLUS_EXPR;
1825 else if (rhs_def_code == MINUS_EXPR)
1826 t = build (PLUS_EXPR, type, def_stmt_op1, outer_const);
1827 else
1828 t = build (rhs_def_code, type, def_stmt_op1, outer_const);
1829 t = local_fold (t);
1830 t = build (rhs_code, type, def_stmt_op0, t);
1831 t = local_fold (t);
1833 /* If the result is a suitable looking gimple expression,
1834 then use it instead of the original for STMT. */
1835 if (TREE_CODE (t) == SSA_NAME
1836 || (UNARY_CLASS_P (t)
1837 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME)
1838 || ((BINARY_CLASS_P (t) || COMPARISON_CLASS_P (t))
1839 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
1840 && is_gimple_val (TREE_OPERAND (t, 1))))
1841 result = update_rhs_and_lookup_avail_expr (stmt, t, insert);
1845 dont_fold_assoc:;
1848 /* Optimize *"foo" into 'f'. This is done here rather than
1849 in fold to avoid problems with stuff like &*"foo". */
1850 if (TREE_CODE (rhs) == INDIRECT_REF || TREE_CODE (rhs) == ARRAY_REF)
1852 tree t = fold_read_from_constant_string (rhs);
1854 if (t)
1855 result = update_rhs_and_lookup_avail_expr (stmt, t, insert);
1858 return result;
1861 /* COND is a condition of the form:
1863 x == const or x != const
1865 Look back to x's defining statement and see if x is defined as
1867 x = (type) y;
1869 If const is unchanged if we convert it to type, then we can build
1870 the equivalent expression:
1873 y == const or y != const
1875 Which may allow further optimizations.
1877 Return the equivalent comparison or NULL if no such equivalent comparison
1878 was found. */
1880 static tree
1881 find_equivalent_equality_comparison (tree cond)
1883 tree op0 = TREE_OPERAND (cond, 0);
1884 tree op1 = TREE_OPERAND (cond, 1);
1885 tree def_stmt = SSA_NAME_DEF_STMT (op0);
1887 /* OP0 might have been a parameter, so first make sure it
1888 was defined by a MODIFY_EXPR. */
1889 if (def_stmt && TREE_CODE (def_stmt) == MODIFY_EXPR)
1891 tree def_rhs = TREE_OPERAND (def_stmt, 1);
1894 /* If either operand to the comparison is a pointer to
1895 a function, then we can not apply this optimization
1896 as some targets require function pointers to be
1897 canonicalized and in this case this optimization would
1898 eliminate a necessary canonicalization. */
1899 if ((POINTER_TYPE_P (TREE_TYPE (op0))
1900 && TREE_CODE (TREE_TYPE (TREE_TYPE (op0))) == FUNCTION_TYPE)
1901 || (POINTER_TYPE_P (TREE_TYPE (op1))
1902 && TREE_CODE (TREE_TYPE (TREE_TYPE (op1))) == FUNCTION_TYPE))
1903 return NULL;
1905 /* Now make sure the RHS of the MODIFY_EXPR is a typecast. */
1906 if ((TREE_CODE (def_rhs) == NOP_EXPR
1907 || TREE_CODE (def_rhs) == CONVERT_EXPR)
1908 && TREE_CODE (TREE_OPERAND (def_rhs, 0)) == SSA_NAME)
1910 tree def_rhs_inner = TREE_OPERAND (def_rhs, 0);
1911 tree def_rhs_inner_type = TREE_TYPE (def_rhs_inner);
1912 tree new;
1914 if (TYPE_PRECISION (def_rhs_inner_type)
1915 > TYPE_PRECISION (TREE_TYPE (def_rhs)))
1916 return NULL;
1918 /* If the inner type of the conversion is a pointer to
1919 a function, then we can not apply this optimization
1920 as some targets require function pointers to be
1921 canonicalized. This optimization would result in
1922 canonicalization of the pointer when it was not originally
1923 needed/intended. */
1924 if (POINTER_TYPE_P (def_rhs_inner_type)
1925 && TREE_CODE (TREE_TYPE (def_rhs_inner_type)) == FUNCTION_TYPE)
1926 return NULL;
1928 /* What we want to prove is that if we convert OP1 to
1929 the type of the object inside the NOP_EXPR that the
1930 result is still equivalent to SRC.
1932 If that is true, the build and return new equivalent
1933 condition which uses the source of the typecast and the
1934 new constant (which has only changed its type). */
1935 new = build1 (TREE_CODE (def_rhs), def_rhs_inner_type, op1);
1936 new = local_fold (new);
1937 if (is_gimple_val (new) && tree_int_cst_equal (new, op1))
1938 return build (TREE_CODE (cond), TREE_TYPE (cond),
1939 def_rhs_inner, new);
1942 return NULL;
1945 /* STMT is a COND_EXPR for which we could not trivially determine its
1946 result. This routine attempts to find equivalent forms of the
1947 condition which we may be able to optimize better. It also
1948 uses simple value range propagation to optimize conditionals. */
1950 static tree
1951 simplify_cond_and_lookup_avail_expr (tree stmt,
1952 stmt_ann_t ann,
1953 int insert)
1955 tree cond = COND_EXPR_COND (stmt);
1957 if (COMPARISON_CLASS_P (cond))
1959 tree op0 = TREE_OPERAND (cond, 0);
1960 tree op1 = TREE_OPERAND (cond, 1);
1962 if (TREE_CODE (op0) == SSA_NAME && is_gimple_min_invariant (op1))
1964 int limit;
1965 tree low, high, cond_low, cond_high;
1966 int lowequal, highequal, swapped, no_overlap, subset, cond_inverted;
1967 VEC(vrp_element_p,heap) **vrp_records;
1968 struct vrp_element *element;
1969 struct vrp_hash_elt vrp_hash_elt, *vrp_hash_elt_p;
1970 void **slot;
1972 /* First see if we have test of an SSA_NAME against a constant
1973 where the SSA_NAME is defined by an earlier typecast which
1974 is irrelevant when performing tests against the given
1975 constant. */
1976 if (TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
1978 tree new_cond = find_equivalent_equality_comparison (cond);
1980 if (new_cond)
1982 /* Update the statement to use the new equivalent
1983 condition. */
1984 COND_EXPR_COND (stmt) = new_cond;
1986 /* If this is not a real stmt, ann will be NULL and we
1987 avoid processing the operands. */
1988 if (ann)
1989 mark_stmt_modified (stmt);
1991 /* Lookup the condition and return its known value if it
1992 exists. */
1993 new_cond = lookup_avail_expr (stmt, insert);
1994 if (new_cond)
1995 return new_cond;
1997 /* The operands have changed, so update op0 and op1. */
1998 op0 = TREE_OPERAND (cond, 0);
1999 op1 = TREE_OPERAND (cond, 1);
2003 /* Consult the value range records for this variable (if they exist)
2004 to see if we can eliminate or simplify this conditional.
2006 Note two tests are necessary to determine no records exist.
2007 First we have to see if the virtual array exists, if it
2008 exists, then we have to check its active size.
2010 Also note the vast majority of conditionals are not testing
2011 a variable which has had its range constrained by an earlier
2012 conditional. So this filter avoids a lot of unnecessary work. */
2013 vrp_hash_elt.var = op0;
2014 vrp_hash_elt.records = NULL;
2015 slot = htab_find_slot (vrp_data, &vrp_hash_elt, NO_INSERT);
2016 if (slot == NULL)
2017 return NULL;
2019 vrp_hash_elt_p = (struct vrp_hash_elt *) *slot;
2020 vrp_records = &vrp_hash_elt_p->records;
2022 limit = VEC_length (vrp_element_p, *vrp_records);
2024 /* If we have no value range records for this variable, or we are
2025 unable to extract a range for this condition, then there is
2026 nothing to do. */
2027 if (limit == 0
2028 || ! extract_range_from_cond (cond, &cond_high,
2029 &cond_low, &cond_inverted))
2030 return NULL;
2032 /* We really want to avoid unnecessary computations of range
2033 info. So all ranges are computed lazily; this avoids a
2034 lot of unnecessary work. i.e., we record the conditional,
2035 but do not process how it constrains the variable's
2036 potential values until we know that processing the condition
2037 could be helpful.
2039 However, we do not want to have to walk a potentially long
2040 list of ranges, nor do we want to compute a variable's
2041 range more than once for a given path.
2043 Luckily, each time we encounter a conditional that can not
2044 be otherwise optimized we will end up here and we will
2045 compute the necessary range information for the variable
2046 used in this condition.
2048 Thus you can conclude that there will never be more than one
2049 conditional associated with a variable which has not been
2050 processed. So we never need to merge more than one new
2051 conditional into the current range.
2053 These properties also help us avoid unnecessary work. */
2054 element = VEC_last (vrp_element_p, *vrp_records);
2056 if (element->high && element->low)
2058 /* The last element has been processed, so there is no range
2059 merging to do, we can simply use the high/low values
2060 recorded in the last element. */
2061 low = element->low;
2062 high = element->high;
2064 else
2066 tree tmp_high, tmp_low;
2067 int dummy;
2069 /* The last element has not been processed. Process it now.
2070 record_range should ensure for cond inverted is not set.
2071 This call can only fail if cond is x < min or x > max,
2072 which fold should have optimized into false.
2073 If that doesn't happen, just pretend all values are
2074 in the range. */
2075 if (! extract_range_from_cond (element->cond, &tmp_high,
2076 &tmp_low, &dummy))
2077 gcc_unreachable ();
2078 else
2079 gcc_assert (dummy == 0);
2081 /* If this is the only element, then no merging is necessary,
2082 the high/low values from extract_range_from_cond are all
2083 we need. */
2084 if (limit == 1)
2086 low = tmp_low;
2087 high = tmp_high;
2089 else
2091 /* Get the high/low value from the previous element. */
2092 struct vrp_element *prev
2093 = VEC_index (vrp_element_p, *vrp_records, limit - 2);
2094 low = prev->low;
2095 high = prev->high;
2097 /* Merge in this element's range with the range from the
2098 previous element.
2100 The low value for the merged range is the maximum of
2101 the previous low value and the low value of this record.
2103 Similarly the high value for the merged range is the
2104 minimum of the previous high value and the high value of
2105 this record. */
2106 low = (low && tree_int_cst_compare (low, tmp_low) == 1
2107 ? low : tmp_low);
2108 high = (high && tree_int_cst_compare (high, tmp_high) == -1
2109 ? high : tmp_high);
2112 /* And record the computed range. */
2113 element->low = low;
2114 element->high = high;
2118 /* After we have constrained this variable's potential values,
2119 we try to determine the result of the given conditional.
2121 To simplify later tests, first determine if the current
2122 low value is the same low value as the conditional.
2123 Similarly for the current high value and the high value
2124 for the conditional. */
2125 lowequal = tree_int_cst_equal (low, cond_low);
2126 highequal = tree_int_cst_equal (high, cond_high);
2128 if (lowequal && highequal)
2129 return (cond_inverted ? boolean_false_node : boolean_true_node);
2131 /* To simplify the overlap/subset tests below we may want
2132 to swap the two ranges so that the larger of the two
2133 ranges occurs "first". */
2134 swapped = 0;
2135 if (tree_int_cst_compare (low, cond_low) == 1
2136 || (lowequal
2137 && tree_int_cst_compare (cond_high, high) == 1))
2139 tree temp;
2141 swapped = 1;
2142 temp = low;
2143 low = cond_low;
2144 cond_low = temp;
2145 temp = high;
2146 high = cond_high;
2147 cond_high = temp;
2150 /* Now determine if there is no overlap in the ranges
2151 or if the second range is a subset of the first range. */
2152 no_overlap = tree_int_cst_lt (high, cond_low);
2153 subset = tree_int_cst_compare (cond_high, high) != 1;
2155 /* If there was no overlap in the ranges, then this conditional
2156 always has a false value (unless we had to invert this
2157 conditional, in which case it always has a true value). */
2158 if (no_overlap)
2159 return (cond_inverted ? boolean_true_node : boolean_false_node);
2161 /* If the current range is a subset of the condition's range,
2162 then this conditional always has a true value (unless we
2163 had to invert this conditional, in which case it always
2164 has a true value). */
2165 if (subset && swapped)
2166 return (cond_inverted ? boolean_false_node : boolean_true_node);
2168 /* We were unable to determine the result of the conditional.
2169 However, we may be able to simplify the conditional. First
2170 merge the ranges in the same manner as range merging above. */
2171 low = tree_int_cst_compare (low, cond_low) == 1 ? low : cond_low;
2172 high = tree_int_cst_compare (high, cond_high) == -1 ? high : cond_high;
2174 /* If the range has converged to a single point, then turn this
2175 into an equality comparison. */
2176 if (TREE_CODE (cond) != EQ_EXPR
2177 && TREE_CODE (cond) != NE_EXPR
2178 && tree_int_cst_equal (low, high))
2180 TREE_SET_CODE (cond, EQ_EXPR);
2181 TREE_OPERAND (cond, 1) = high;
2185 return 0;
2188 /* STMT is a SWITCH_EXPR for which we could not trivially determine its
2189 result. This routine attempts to find equivalent forms of the
2190 condition which we may be able to optimize better. */
2192 static tree
2193 simplify_switch_and_lookup_avail_expr (tree stmt, int insert)
2195 tree cond = SWITCH_COND (stmt);
2196 tree def, to, ti;
2198 /* The optimization that we really care about is removing unnecessary
2199 casts. That will let us do much better in propagating the inferred
2200 constant at the switch target. */
2201 if (TREE_CODE (cond) == SSA_NAME)
2203 def = SSA_NAME_DEF_STMT (cond);
2204 if (TREE_CODE (def) == MODIFY_EXPR)
2206 def = TREE_OPERAND (def, 1);
2207 if (TREE_CODE (def) == NOP_EXPR)
2209 int need_precision;
2210 bool fail;
2212 def = TREE_OPERAND (def, 0);
2214 #ifdef ENABLE_CHECKING
2215 /* ??? Why was Jeff testing this? We are gimple... */
2216 gcc_assert (is_gimple_val (def));
2217 #endif
2219 to = TREE_TYPE (cond);
2220 ti = TREE_TYPE (def);
2222 /* If we have an extension that preserves value, then we
2223 can copy the source value into the switch. */
2225 need_precision = TYPE_PRECISION (ti);
2226 fail = false;
2227 if (TYPE_UNSIGNED (to) && !TYPE_UNSIGNED (ti))
2228 fail = true;
2229 else if (!TYPE_UNSIGNED (to) && TYPE_UNSIGNED (ti))
2230 need_precision += 1;
2231 if (TYPE_PRECISION (to) < need_precision)
2232 fail = true;
2234 if (!fail)
2236 SWITCH_COND (stmt) = def;
2237 mark_stmt_modified (stmt);
2239 return lookup_avail_expr (stmt, insert);
2245 return 0;
2249 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
2250 known value for that SSA_NAME (or NULL if no value is known).
2252 NONZERO_VARS is the set SSA_NAMES known to have a nonzero value,
2253 even if we don't know their precise value.
2255 Propagate values from CONST_AND_COPIES and NONZERO_VARS into the PHI
2256 nodes of the successors of BB. */
2258 static void
2259 cprop_into_successor_phis (basic_block bb, bitmap nonzero_vars)
2261 edge e;
2262 edge_iterator ei;
2264 FOR_EACH_EDGE (e, ei, bb->succs)
2266 tree phi;
2267 int indx;
2269 /* If this is an abnormal edge, then we do not want to copy propagate
2270 into the PHI alternative associated with this edge. */
2271 if (e->flags & EDGE_ABNORMAL)
2272 continue;
2274 phi = phi_nodes (e->dest);
2275 if (! phi)
2276 continue;
2278 indx = e->dest_idx;
2279 for ( ; phi; phi = PHI_CHAIN (phi))
2281 tree new;
2282 use_operand_p orig_p;
2283 tree orig;
2285 /* The alternative may be associated with a constant, so verify
2286 it is an SSA_NAME before doing anything with it. */
2287 orig_p = PHI_ARG_DEF_PTR (phi, indx);
2288 orig = USE_FROM_PTR (orig_p);
2289 if (TREE_CODE (orig) != SSA_NAME)
2290 continue;
2292 /* If the alternative is known to have a nonzero value, record
2293 that fact in the PHI node itself for future use. */
2294 if (bitmap_bit_p (nonzero_vars, SSA_NAME_VERSION (orig)))
2295 PHI_ARG_NONZERO (phi, indx) = true;
2297 /* If we have *ORIG_P in our constant/copy table, then replace
2298 ORIG_P with its value in our constant/copy table. */
2299 new = SSA_NAME_VALUE (orig);
2300 if (new
2301 && new != orig
2302 && (TREE_CODE (new) == SSA_NAME
2303 || is_gimple_min_invariant (new))
2304 && may_propagate_copy (orig, new))
2305 propagate_value (orig_p, new);
2310 /* We have finished optimizing BB, record any information implied by
2311 taking a specific outgoing edge from BB. */
2313 static void
2314 record_edge_info (basic_block bb)
2316 block_stmt_iterator bsi = bsi_last (bb);
2317 struct edge_info *edge_info;
2319 if (! bsi_end_p (bsi))
2321 tree stmt = bsi_stmt (bsi);
2323 if (stmt && TREE_CODE (stmt) == SWITCH_EXPR)
2325 tree cond = SWITCH_COND (stmt);
2327 if (TREE_CODE (cond) == SSA_NAME)
2329 tree labels = SWITCH_LABELS (stmt);
2330 int i, n_labels = TREE_VEC_LENGTH (labels);
2331 tree *info = xcalloc (last_basic_block, sizeof (tree));
2332 edge e;
2333 edge_iterator ei;
2335 for (i = 0; i < n_labels; i++)
2337 tree label = TREE_VEC_ELT (labels, i);
2338 basic_block target_bb = label_to_block (CASE_LABEL (label));
2340 if (CASE_HIGH (label)
2341 || !CASE_LOW (label)
2342 || info[target_bb->index])
2343 info[target_bb->index] = error_mark_node;
2344 else
2345 info[target_bb->index] = label;
2348 FOR_EACH_EDGE (e, ei, bb->succs)
2350 basic_block target_bb = e->dest;
2351 tree node = info[target_bb->index];
2353 if (node != NULL && node != error_mark_node)
2355 tree x = fold_convert (TREE_TYPE (cond), CASE_LOW (node));
2356 edge_info = allocate_edge_info (e);
2357 edge_info->lhs = cond;
2358 edge_info->rhs = x;
2361 free (info);
2365 /* A COND_EXPR may create equivalences too. */
2366 if (stmt && TREE_CODE (stmt) == COND_EXPR)
2368 tree cond = COND_EXPR_COND (stmt);
2369 edge true_edge;
2370 edge false_edge;
2372 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2374 /* If the conditional is a single variable 'X', record 'X = 1'
2375 for the true edge and 'X = 0' on the false edge. */
2376 if (SSA_VAR_P (cond))
2378 struct edge_info *edge_info;
2380 edge_info = allocate_edge_info (true_edge);
2381 edge_info->lhs = cond;
2382 edge_info->rhs = constant_boolean_node (1, TREE_TYPE (cond));
2384 edge_info = allocate_edge_info (false_edge);
2385 edge_info->lhs = cond;
2386 edge_info->rhs = constant_boolean_node (0, TREE_TYPE (cond));
2388 /* Equality tests may create one or two equivalences. */
2389 else if (COMPARISON_CLASS_P (cond))
2391 tree op0 = TREE_OPERAND (cond, 0);
2392 tree op1 = TREE_OPERAND (cond, 1);
2394 /* Special case comparing booleans against a constant as we
2395 know the value of OP0 on both arms of the branch. i.e., we
2396 can record an equivalence for OP0 rather than COND. */
2397 if ((TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
2398 && TREE_CODE (op0) == SSA_NAME
2399 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
2400 && is_gimple_min_invariant (op1))
2402 if (TREE_CODE (cond) == EQ_EXPR)
2404 edge_info = allocate_edge_info (true_edge);
2405 edge_info->lhs = op0;
2406 edge_info->rhs = (integer_zerop (op1)
2407 ? boolean_false_node
2408 : boolean_true_node);
2410 edge_info = allocate_edge_info (false_edge);
2411 edge_info->lhs = op0;
2412 edge_info->rhs = (integer_zerop (op1)
2413 ? boolean_true_node
2414 : boolean_false_node);
2416 else
2418 edge_info = allocate_edge_info (true_edge);
2419 edge_info->lhs = op0;
2420 edge_info->rhs = (integer_zerop (op1)
2421 ? boolean_true_node
2422 : boolean_false_node);
2424 edge_info = allocate_edge_info (false_edge);
2425 edge_info->lhs = op0;
2426 edge_info->rhs = (integer_zerop (op1)
2427 ? boolean_false_node
2428 : boolean_true_node);
2432 else if (is_gimple_min_invariant (op0)
2433 && (TREE_CODE (op1) == SSA_NAME
2434 || is_gimple_min_invariant (op1)))
2436 tree inverted = invert_truthvalue (cond);
2437 struct edge_info *edge_info;
2439 edge_info = allocate_edge_info (true_edge);
2440 record_conditions (edge_info, cond, inverted);
2442 if (TREE_CODE (cond) == EQ_EXPR)
2444 edge_info->lhs = op1;
2445 edge_info->rhs = op0;
2448 edge_info = allocate_edge_info (false_edge);
2449 record_conditions (edge_info, inverted, cond);
2451 if (TREE_CODE (cond) == NE_EXPR)
2453 edge_info->lhs = op1;
2454 edge_info->rhs = op0;
2458 else if (TREE_CODE (op0) == SSA_NAME
2459 && (is_gimple_min_invariant (op1)
2460 || TREE_CODE (op1) == SSA_NAME))
2462 tree inverted = invert_truthvalue (cond);
2463 struct edge_info *edge_info;
2465 edge_info = allocate_edge_info (true_edge);
2466 record_conditions (edge_info, cond, inverted);
2468 if (TREE_CODE (cond) == EQ_EXPR)
2470 edge_info->lhs = op0;
2471 edge_info->rhs = op1;
2474 edge_info = allocate_edge_info (false_edge);
2475 record_conditions (edge_info, inverted, cond);
2477 if (TREE_CODE (cond) == NE_EXPR)
2479 edge_info->lhs = op0;
2480 edge_info->rhs = op1;
2485 /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
2490 /* Propagate information from BB to its outgoing edges.
2492 This can include equivalency information implied by control statements
2493 at the end of BB and const/copy propagation into PHIs in BB's
2494 successor blocks. */
2496 static void
2497 propagate_to_outgoing_edges (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
2498 basic_block bb)
2500 record_edge_info (bb);
2501 cprop_into_successor_phis (bb, nonzero_vars);
2504 /* Search for redundant computations in STMT. If any are found, then
2505 replace them with the variable holding the result of the computation.
2507 If safe, record this expression into the available expression hash
2508 table. */
2510 static bool
2511 eliminate_redundant_computations (tree stmt, stmt_ann_t ann)
2513 tree *expr_p, def = NULL_TREE;
2514 bool insert = true;
2515 tree cached_lhs;
2516 bool retval = false;
2517 bool modify_expr_p = false;
2519 if (TREE_CODE (stmt) == MODIFY_EXPR)
2520 def = TREE_OPERAND (stmt, 0);
2522 /* Certain expressions on the RHS can be optimized away, but can not
2523 themselves be entered into the hash tables. */
2524 if (ann->makes_aliased_stores
2525 || ! def
2526 || TREE_CODE (def) != SSA_NAME
2527 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)
2528 || !ZERO_SSA_OPERANDS (stmt, SSA_OP_VMAYDEF)
2529 /* Do not record equivalences for increments of ivs. This would create
2530 overlapping live ranges for a very questionable gain. */
2531 || simple_iv_increment_p (stmt))
2532 insert = false;
2534 /* Check if the expression has been computed before. */
2535 cached_lhs = lookup_avail_expr (stmt, insert);
2537 /* If this is an assignment and the RHS was not in the hash table,
2538 then try to simplify the RHS and lookup the new RHS in the
2539 hash table. */
2540 if (! cached_lhs && TREE_CODE (stmt) == MODIFY_EXPR)
2541 cached_lhs = simplify_rhs_and_lookup_avail_expr (stmt, insert);
2542 /* Similarly if this is a COND_EXPR and we did not find its
2543 expression in the hash table, simplify the condition and
2544 try again. */
2545 else if (! cached_lhs && TREE_CODE (stmt) == COND_EXPR)
2546 cached_lhs = simplify_cond_and_lookup_avail_expr (stmt, ann, insert);
2547 /* Similarly for a SWITCH_EXPR. */
2548 else if (!cached_lhs && TREE_CODE (stmt) == SWITCH_EXPR)
2549 cached_lhs = simplify_switch_and_lookup_avail_expr (stmt, insert);
2551 opt_stats.num_exprs_considered++;
2553 /* Get a pointer to the expression we are trying to optimize. */
2554 if (TREE_CODE (stmt) == COND_EXPR)
2555 expr_p = &COND_EXPR_COND (stmt);
2556 else if (TREE_CODE (stmt) == SWITCH_EXPR)
2557 expr_p = &SWITCH_COND (stmt);
2558 else if (TREE_CODE (stmt) == RETURN_EXPR && TREE_OPERAND (stmt, 0))
2560 expr_p = &TREE_OPERAND (TREE_OPERAND (stmt, 0), 1);
2561 modify_expr_p = true;
2563 else
2565 expr_p = &TREE_OPERAND (stmt, 1);
2566 modify_expr_p = true;
2569 /* It is safe to ignore types here since we have already done
2570 type checking in the hashing and equality routines. In fact
2571 type checking here merely gets in the way of constant
2572 propagation. Also, make sure that it is safe to propagate
2573 CACHED_LHS into *EXPR_P. */
2574 if (cached_lhs
2575 && ((TREE_CODE (cached_lhs) != SSA_NAME
2576 && (modify_expr_p
2577 || tree_ssa_useless_type_conversion_1 (TREE_TYPE (*expr_p),
2578 TREE_TYPE (cached_lhs))))
2579 || may_propagate_copy (*expr_p, cached_lhs)))
2581 if (dump_file && (dump_flags & TDF_DETAILS))
2583 fprintf (dump_file, " Replaced redundant expr '");
2584 print_generic_expr (dump_file, *expr_p, dump_flags);
2585 fprintf (dump_file, "' with '");
2586 print_generic_expr (dump_file, cached_lhs, dump_flags);
2587 fprintf (dump_file, "'\n");
2590 opt_stats.num_re++;
2592 #if defined ENABLE_CHECKING
2593 gcc_assert (TREE_CODE (cached_lhs) == SSA_NAME
2594 || is_gimple_min_invariant (cached_lhs));
2595 #endif
2597 if (TREE_CODE (cached_lhs) == ADDR_EXPR
2598 || (POINTER_TYPE_P (TREE_TYPE (*expr_p))
2599 && is_gimple_min_invariant (cached_lhs)))
2600 retval = true;
2602 if (modify_expr_p
2603 && !tree_ssa_useless_type_conversion_1 (TREE_TYPE (*expr_p),
2604 TREE_TYPE (cached_lhs)))
2605 cached_lhs = fold_convert (TREE_TYPE (*expr_p), cached_lhs);
2607 propagate_tree_value (expr_p, cached_lhs);
2608 mark_stmt_modified (stmt);
2610 return retval;
2613 /* STMT, a MODIFY_EXPR, may create certain equivalences, in either
2614 the available expressions table or the const_and_copies table.
2615 Detect and record those equivalences. */
2617 static void
2618 record_equivalences_from_stmt (tree stmt,
2619 int may_optimize_p,
2620 stmt_ann_t ann)
2622 tree lhs = TREE_OPERAND (stmt, 0);
2623 enum tree_code lhs_code = TREE_CODE (lhs);
2624 int i;
2626 if (lhs_code == SSA_NAME)
2628 tree rhs = TREE_OPERAND (stmt, 1);
2630 /* Strip away any useless type conversions. */
2631 STRIP_USELESS_TYPE_CONVERSION (rhs);
2633 /* If the RHS of the assignment is a constant or another variable that
2634 may be propagated, register it in the CONST_AND_COPIES table. We
2635 do not need to record unwind data for this, since this is a true
2636 assignment and not an equivalence inferred from a comparison. All
2637 uses of this ssa name are dominated by this assignment, so unwinding
2638 just costs time and space. */
2639 if (may_optimize_p
2640 && (TREE_CODE (rhs) == SSA_NAME
2641 || is_gimple_min_invariant (rhs)))
2642 SSA_NAME_VALUE (lhs) = rhs;
2644 if (tree_expr_nonzero_p (rhs))
2645 record_var_is_nonzero (lhs);
2648 /* Look at both sides for pointer dereferences. If we find one, then
2649 the pointer must be nonnull and we can enter that equivalence into
2650 the hash tables. */
2651 if (flag_delete_null_pointer_checks)
2652 for (i = 0; i < 2; i++)
2654 tree t = TREE_OPERAND (stmt, i);
2656 /* Strip away any COMPONENT_REFs. */
2657 while (TREE_CODE (t) == COMPONENT_REF)
2658 t = TREE_OPERAND (t, 0);
2660 /* Now see if this is a pointer dereference. */
2661 if (INDIRECT_REF_P (t))
2663 tree op = TREE_OPERAND (t, 0);
2665 /* If the pointer is a SSA variable, then enter new
2666 equivalences into the hash table. */
2667 while (TREE_CODE (op) == SSA_NAME)
2669 tree def = SSA_NAME_DEF_STMT (op);
2671 record_var_is_nonzero (op);
2673 /* And walk up the USE-DEF chains noting other SSA_NAMEs
2674 which are known to have a nonzero value. */
2675 if (def
2676 && TREE_CODE (def) == MODIFY_EXPR
2677 && TREE_CODE (TREE_OPERAND (def, 1)) == NOP_EXPR)
2678 op = TREE_OPERAND (TREE_OPERAND (def, 1), 0);
2679 else
2680 break;
2685 /* A memory store, even an aliased store, creates a useful
2686 equivalence. By exchanging the LHS and RHS, creating suitable
2687 vops and recording the result in the available expression table,
2688 we may be able to expose more redundant loads. */
2689 if (!ann->has_volatile_ops
2690 && (TREE_CODE (TREE_OPERAND (stmt, 1)) == SSA_NAME
2691 || is_gimple_min_invariant (TREE_OPERAND (stmt, 1)))
2692 && !is_gimple_reg (lhs))
2694 tree rhs = TREE_OPERAND (stmt, 1);
2695 tree new;
2697 /* FIXME: If the LHS of the assignment is a bitfield and the RHS
2698 is a constant, we need to adjust the constant to fit into the
2699 type of the LHS. If the LHS is a bitfield and the RHS is not
2700 a constant, then we can not record any equivalences for this
2701 statement since we would need to represent the widening or
2702 narrowing of RHS. This fixes gcc.c-torture/execute/921016-1.c
2703 and should not be necessary if GCC represented bitfields
2704 properly. */
2705 if (lhs_code == COMPONENT_REF
2706 && DECL_BIT_FIELD (TREE_OPERAND (lhs, 1)))
2708 if (TREE_CONSTANT (rhs))
2709 rhs = widen_bitfield (rhs, TREE_OPERAND (lhs, 1), lhs);
2710 else
2711 rhs = NULL;
2713 /* If the value overflowed, then we can not use this equivalence. */
2714 if (rhs && ! is_gimple_min_invariant (rhs))
2715 rhs = NULL;
2718 if (rhs)
2720 /* Build a new statement with the RHS and LHS exchanged. */
2721 new = build (MODIFY_EXPR, TREE_TYPE (stmt), rhs, lhs);
2723 create_ssa_artficial_load_stmt (new, stmt);
2725 /* Finally enter the statement into the available expression
2726 table. */
2727 lookup_avail_expr (new, true);
2732 /* Replace *OP_P in STMT with any known equivalent value for *OP_P from
2733 CONST_AND_COPIES. */
2735 static bool
2736 cprop_operand (tree stmt, use_operand_p op_p)
2738 bool may_have_exposed_new_symbols = false;
2739 tree val;
2740 tree op = USE_FROM_PTR (op_p);
2742 /* If the operand has a known constant value or it is known to be a
2743 copy of some other variable, use the value or copy stored in
2744 CONST_AND_COPIES. */
2745 val = SSA_NAME_VALUE (op);
2746 if (val && val != op && TREE_CODE (val) != VALUE_HANDLE)
2748 tree op_type, val_type;
2750 /* Do not change the base variable in the virtual operand
2751 tables. That would make it impossible to reconstruct
2752 the renamed virtual operand if we later modify this
2753 statement. Also only allow the new value to be an SSA_NAME
2754 for propagation into virtual operands. */
2755 if (!is_gimple_reg (op)
2756 && (TREE_CODE (val) != SSA_NAME
2757 || is_gimple_reg (val)
2758 || get_virtual_var (val) != get_virtual_var (op)))
2759 return false;
2761 /* Do not replace hard register operands in asm statements. */
2762 if (TREE_CODE (stmt) == ASM_EXPR
2763 && !may_propagate_copy_into_asm (op))
2764 return false;
2766 /* Get the toplevel type of each operand. */
2767 op_type = TREE_TYPE (op);
2768 val_type = TREE_TYPE (val);
2770 /* While both types are pointers, get the type of the object
2771 pointed to. */
2772 while (POINTER_TYPE_P (op_type) && POINTER_TYPE_P (val_type))
2774 op_type = TREE_TYPE (op_type);
2775 val_type = TREE_TYPE (val_type);
2778 /* Make sure underlying types match before propagating a constant by
2779 converting the constant to the proper type. Note that convert may
2780 return a non-gimple expression, in which case we ignore this
2781 propagation opportunity. */
2782 if (TREE_CODE (val) != SSA_NAME)
2784 if (!lang_hooks.types_compatible_p (op_type, val_type))
2786 val = fold_convert (TREE_TYPE (op), val);
2787 if (!is_gimple_min_invariant (val))
2788 return false;
2792 /* Certain operands are not allowed to be copy propagated due
2793 to their interaction with exception handling and some GCC
2794 extensions. */
2795 else if (!may_propagate_copy (op, val))
2796 return false;
2798 /* Do not propagate copies if the propagated value is at a deeper loop
2799 depth than the propagatee. Otherwise, this may move loop variant
2800 variables outside of their loops and prevent coalescing
2801 opportunities. If the value was loop invariant, it will be hoisted
2802 by LICM and exposed for copy propagation. */
2803 if (loop_depth_of_name (val) > loop_depth_of_name (op))
2804 return false;
2806 /* Dump details. */
2807 if (dump_file && (dump_flags & TDF_DETAILS))
2809 fprintf (dump_file, " Replaced '");
2810 print_generic_expr (dump_file, op, dump_flags);
2811 fprintf (dump_file, "' with %s '",
2812 (TREE_CODE (val) != SSA_NAME ? "constant" : "variable"));
2813 print_generic_expr (dump_file, val, dump_flags);
2814 fprintf (dump_file, "'\n");
2817 /* If VAL is an ADDR_EXPR or a constant of pointer type, note
2818 that we may have exposed a new symbol for SSA renaming. */
2819 if (TREE_CODE (val) == ADDR_EXPR
2820 || (POINTER_TYPE_P (TREE_TYPE (op))
2821 && is_gimple_min_invariant (val)))
2822 may_have_exposed_new_symbols = true;
2824 if (TREE_CODE (val) != SSA_NAME)
2825 opt_stats.num_const_prop++;
2826 else
2827 opt_stats.num_copy_prop++;
2829 propagate_value (op_p, val);
2831 /* And note that we modified this statement. This is now
2832 safe, even if we changed virtual operands since we will
2833 rescan the statement and rewrite its operands again. */
2834 mark_stmt_modified (stmt);
2836 return may_have_exposed_new_symbols;
2839 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
2840 known value for that SSA_NAME (or NULL if no value is known).
2842 Propagate values from CONST_AND_COPIES into the uses, vuses and
2843 v_may_def_ops of STMT. */
2845 static bool
2846 cprop_into_stmt (tree stmt)
2848 bool may_have_exposed_new_symbols = false;
2849 use_operand_p op_p;
2850 ssa_op_iter iter;
2852 FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_ALL_USES)
2854 if (TREE_CODE (USE_FROM_PTR (op_p)) == SSA_NAME)
2855 may_have_exposed_new_symbols |= cprop_operand (stmt, op_p);
2858 return may_have_exposed_new_symbols;
2862 /* Optimize the statement pointed to by iterator SI.
2864 We try to perform some simplistic global redundancy elimination and
2865 constant propagation:
2867 1- To detect global redundancy, we keep track of expressions that have
2868 been computed in this block and its dominators. If we find that the
2869 same expression is computed more than once, we eliminate repeated
2870 computations by using the target of the first one.
2872 2- Constant values and copy assignments. This is used to do very
2873 simplistic constant and copy propagation. When a constant or copy
2874 assignment is found, we map the value on the RHS of the assignment to
2875 the variable in the LHS in the CONST_AND_COPIES table. */
2877 static void
2878 optimize_stmt (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
2879 basic_block bb, block_stmt_iterator si)
2881 stmt_ann_t ann;
2882 tree stmt, old_stmt;
2883 bool may_optimize_p;
2884 bool may_have_exposed_new_symbols = false;
2886 old_stmt = stmt = bsi_stmt (si);
2888 update_stmt_if_modified (stmt);
2889 ann = stmt_ann (stmt);
2890 opt_stats.num_stmts++;
2891 may_have_exposed_new_symbols = false;
2893 if (dump_file && (dump_flags & TDF_DETAILS))
2895 fprintf (dump_file, "Optimizing statement ");
2896 print_generic_stmt (dump_file, stmt, TDF_SLIM);
2899 /* Const/copy propagate into USES, VUSES and the RHS of V_MAY_DEFs. */
2900 may_have_exposed_new_symbols = cprop_into_stmt (stmt);
2902 /* If the statement has been modified with constant replacements,
2903 fold its RHS before checking for redundant computations. */
2904 if (ann->modified)
2906 tree rhs;
2908 /* Try to fold the statement making sure that STMT is kept
2909 up to date. */
2910 if (fold_stmt (bsi_stmt_ptr (si)))
2912 stmt = bsi_stmt (si);
2913 ann = stmt_ann (stmt);
2915 if (dump_file && (dump_flags & TDF_DETAILS))
2917 fprintf (dump_file, " Folded to: ");
2918 print_generic_stmt (dump_file, stmt, TDF_SLIM);
2922 rhs = get_rhs (stmt);
2923 if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
2924 recompute_tree_invarant_for_addr_expr (rhs);
2926 /* Constant/copy propagation above may change the set of
2927 virtual operands associated with this statement. Folding
2928 may remove the need for some virtual operands.
2930 Indicate we will need to rescan and rewrite the statement. */
2931 may_have_exposed_new_symbols = true;
2934 /* Check for redundant computations. Do this optimization only
2935 for assignments that have no volatile ops and conditionals. */
2936 may_optimize_p = (!ann->has_volatile_ops
2937 && ((TREE_CODE (stmt) == RETURN_EXPR
2938 && TREE_OPERAND (stmt, 0)
2939 && TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR
2940 && ! (TREE_SIDE_EFFECTS
2941 (TREE_OPERAND (TREE_OPERAND (stmt, 0), 1))))
2942 || (TREE_CODE (stmt) == MODIFY_EXPR
2943 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (stmt, 1)))
2944 || TREE_CODE (stmt) == COND_EXPR
2945 || TREE_CODE (stmt) == SWITCH_EXPR));
2947 if (may_optimize_p)
2948 may_have_exposed_new_symbols
2949 |= eliminate_redundant_computations (stmt, ann);
2951 /* Record any additional equivalences created by this statement. */
2952 if (TREE_CODE (stmt) == MODIFY_EXPR)
2953 record_equivalences_from_stmt (stmt,
2954 may_optimize_p,
2955 ann);
2957 /* If STMT is a COND_EXPR and it was modified, then we may know
2958 where it goes. If that is the case, then mark the CFG as altered.
2960 This will cause us to later call remove_unreachable_blocks and
2961 cleanup_tree_cfg when it is safe to do so. It is not safe to
2962 clean things up here since removal of edges and such can trigger
2963 the removal of PHI nodes, which in turn can release SSA_NAMEs to
2964 the manager.
2966 That's all fine and good, except that once SSA_NAMEs are released
2967 to the manager, we must not call create_ssa_name until all references
2968 to released SSA_NAMEs have been eliminated.
2970 All references to the deleted SSA_NAMEs can not be eliminated until
2971 we remove unreachable blocks.
2973 We can not remove unreachable blocks until after we have completed
2974 any queued jump threading.
2976 We can not complete any queued jump threads until we have taken
2977 appropriate variables out of SSA form. Taking variables out of
2978 SSA form can call create_ssa_name and thus we lose.
2980 Ultimately I suspect we're going to need to change the interface
2981 into the SSA_NAME manager. */
2983 if (ann->modified)
2985 tree val = NULL;
2987 if (TREE_CODE (stmt) == COND_EXPR)
2988 val = COND_EXPR_COND (stmt);
2989 else if (TREE_CODE (stmt) == SWITCH_EXPR)
2990 val = SWITCH_COND (stmt);
2992 if (val && TREE_CODE (val) == INTEGER_CST && find_taken_edge (bb, val))
2993 cfg_altered = true;
2995 /* If we simplified a statement in such a way as to be shown that it
2996 cannot trap, update the eh information and the cfg to match. */
2997 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
2999 bitmap_set_bit (need_eh_cleanup, bb->index);
3000 if (dump_file && (dump_flags & TDF_DETAILS))
3001 fprintf (dump_file, " Flagged to clear EH edges.\n");
3005 if (may_have_exposed_new_symbols)
3006 VEC_safe_push (tree, heap, stmts_to_rescan, bsi_stmt (si));
3009 /* Replace the RHS of STMT with NEW_RHS. If RHS can be found in the
3010 available expression hashtable, then return the LHS from the hash
3011 table.
3013 If INSERT is true, then we also update the available expression
3014 hash table to account for the changes made to STMT. */
3016 static tree
3017 update_rhs_and_lookup_avail_expr (tree stmt, tree new_rhs, bool insert)
3019 tree cached_lhs = NULL;
3021 /* Remove the old entry from the hash table. */
3022 if (insert)
3024 struct expr_hash_elt element;
3026 initialize_hash_element (stmt, NULL, &element);
3027 htab_remove_elt_with_hash (avail_exprs, &element, element.hash);
3030 /* Now update the RHS of the assignment. */
3031 TREE_OPERAND (stmt, 1) = new_rhs;
3033 /* Now lookup the updated statement in the hash table. */
3034 cached_lhs = lookup_avail_expr (stmt, insert);
3036 /* We have now called lookup_avail_expr twice with two different
3037 versions of this same statement, once in optimize_stmt, once here.
3039 We know the call in optimize_stmt did not find an existing entry
3040 in the hash table, so a new entry was created. At the same time
3041 this statement was pushed onto the AVAIL_EXPRS_STACK vector.
3043 If this call failed to find an existing entry on the hash table,
3044 then the new version of this statement was entered into the
3045 hash table. And this statement was pushed onto BLOCK_AVAIL_EXPR
3046 for the second time. So there are two copies on BLOCK_AVAIL_EXPRs
3048 If this call succeeded, we still have one copy of this statement
3049 on the BLOCK_AVAIL_EXPRs vector.
3051 For both cases, we need to pop the most recent entry off the
3052 BLOCK_AVAIL_EXPRs vector. For the case where we never found this
3053 statement in the hash tables, that will leave precisely one
3054 copy of this statement on BLOCK_AVAIL_EXPRs. For the case where
3055 we found a copy of this statement in the second hash table lookup
3056 we want _no_ copies of this statement in BLOCK_AVAIL_EXPRs. */
3057 if (insert)
3058 VEC_pop (tree, avail_exprs_stack);
3060 /* And make sure we record the fact that we modified this
3061 statement. */
3062 mark_stmt_modified (stmt);
3064 return cached_lhs;
3067 /* Search for an existing instance of STMT in the AVAIL_EXPRS table. If
3068 found, return its LHS. Otherwise insert STMT in the table and return
3069 NULL_TREE.
3071 Also, when an expression is first inserted in the AVAIL_EXPRS table, it
3072 is also added to the stack pointed to by BLOCK_AVAIL_EXPRS_P, so that they
3073 can be removed when we finish processing this block and its children.
3075 NOTE: This function assumes that STMT is a MODIFY_EXPR node that
3076 contains no CALL_EXPR on its RHS and makes no volatile nor
3077 aliased references. */
3079 static tree
3080 lookup_avail_expr (tree stmt, bool insert)
3082 void **slot;
3083 tree lhs;
3084 tree temp;
3085 struct expr_hash_elt *element = xmalloc (sizeof (struct expr_hash_elt));
3087 lhs = TREE_CODE (stmt) == MODIFY_EXPR ? TREE_OPERAND (stmt, 0) : NULL;
3089 initialize_hash_element (stmt, lhs, element);
3091 /* Don't bother remembering constant assignments and copy operations.
3092 Constants and copy operations are handled by the constant/copy propagator
3093 in optimize_stmt. */
3094 if (TREE_CODE (element->rhs) == SSA_NAME
3095 || is_gimple_min_invariant (element->rhs))
3097 free (element);
3098 return NULL_TREE;
3101 /* If this is an equality test against zero, see if we have recorded a
3102 nonzero value for the variable in question. */
3103 if ((TREE_CODE (element->rhs) == EQ_EXPR
3104 || TREE_CODE (element->rhs) == NE_EXPR)
3105 && TREE_CODE (TREE_OPERAND (element->rhs, 0)) == SSA_NAME
3106 && integer_zerop (TREE_OPERAND (element->rhs, 1)))
3108 int indx = SSA_NAME_VERSION (TREE_OPERAND (element->rhs, 0));
3110 if (bitmap_bit_p (nonzero_vars, indx))
3112 tree t = element->rhs;
3113 free (element);
3114 return constant_boolean_node (TREE_CODE (t) != EQ_EXPR,
3115 TREE_TYPE (t));
3119 /* Finally try to find the expression in the main expression hash table. */
3120 slot = htab_find_slot_with_hash (avail_exprs, element, element->hash,
3121 (insert ? INSERT : NO_INSERT));
3122 if (slot == NULL)
3124 free (element);
3125 return NULL_TREE;
3128 if (*slot == NULL)
3130 *slot = (void *) element;
3131 VEC_safe_push (tree, heap, avail_exprs_stack,
3132 stmt ? stmt : element->rhs);
3133 return NULL_TREE;
3136 /* Extract the LHS of the assignment so that it can be used as the current
3137 definition of another variable. */
3138 lhs = ((struct expr_hash_elt *)*slot)->lhs;
3140 /* See if the LHS appears in the CONST_AND_COPIES table. If it does, then
3141 use the value from the const_and_copies table. */
3142 if (TREE_CODE (lhs) == SSA_NAME)
3144 temp = SSA_NAME_VALUE (lhs);
3145 if (temp && TREE_CODE (temp) != VALUE_HANDLE)
3146 lhs = temp;
3149 free (element);
3150 return lhs;
3153 /* Given a condition COND, record into HI_P, LO_P and INVERTED_P the
3154 range of values that result in the conditional having a true value.
3156 Return true if we are successful in extracting a range from COND and
3157 false if we are unsuccessful. */
3159 static bool
3160 extract_range_from_cond (tree cond, tree *hi_p, tree *lo_p, int *inverted_p)
3162 tree op1 = TREE_OPERAND (cond, 1);
3163 tree high, low, type;
3164 int inverted;
3166 type = TREE_TYPE (op1);
3168 /* Experiments have shown that it's rarely, if ever useful to
3169 record ranges for enumerations. Presumably this is due to
3170 the fact that they're rarely used directly. They are typically
3171 cast into an integer type and used that way. */
3172 if (TREE_CODE (type) != INTEGER_TYPE
3173 /* We don't know how to deal with types with variable bounds. */
3174 || TREE_CODE (TYPE_MIN_VALUE (type)) != INTEGER_CST
3175 || TREE_CODE (TYPE_MAX_VALUE (type)) != INTEGER_CST)
3176 return 0;
3178 switch (TREE_CODE (cond))
3180 case EQ_EXPR:
3181 high = low = op1;
3182 inverted = 0;
3183 break;
3185 case NE_EXPR:
3186 high = low = op1;
3187 inverted = 1;
3188 break;
3190 case GE_EXPR:
3191 low = op1;
3192 high = TYPE_MAX_VALUE (type);
3193 inverted = 0;
3194 break;
3196 case GT_EXPR:
3197 high = TYPE_MAX_VALUE (type);
3198 if (!tree_int_cst_lt (op1, high))
3199 return 0;
3200 low = int_const_binop (PLUS_EXPR, op1, integer_one_node, 1);
3201 inverted = 0;
3202 break;
3204 case LE_EXPR:
3205 high = op1;
3206 low = TYPE_MIN_VALUE (type);
3207 inverted = 0;
3208 break;
3210 case LT_EXPR:
3211 low = TYPE_MIN_VALUE (type);
3212 if (!tree_int_cst_lt (low, op1))
3213 return 0;
3214 high = int_const_binop (MINUS_EXPR, op1, integer_one_node, 1);
3215 inverted = 0;
3216 break;
3218 default:
3219 return 0;
3222 *hi_p = high;
3223 *lo_p = low;
3224 *inverted_p = inverted;
3225 return 1;
3228 /* Record a range created by COND for basic block BB. */
3230 static void
3231 record_range (tree cond, basic_block bb)
3233 enum tree_code code = TREE_CODE (cond);
3235 /* We explicitly ignore NE_EXPRs and all the unordered comparisons.
3236 They rarely allow for meaningful range optimizations and significantly
3237 complicate the implementation. */
3238 if ((code == LT_EXPR || code == LE_EXPR || code == GT_EXPR
3239 || code == GE_EXPR || code == EQ_EXPR)
3240 && TREE_CODE (TREE_TYPE (TREE_OPERAND (cond, 1))) == INTEGER_TYPE)
3242 struct vrp_hash_elt *vrp_hash_elt;
3243 struct vrp_element *element;
3244 VEC(vrp_element_p,heap) **vrp_records_p;
3245 void **slot;
3248 vrp_hash_elt = xmalloc (sizeof (struct vrp_hash_elt));
3249 vrp_hash_elt->var = TREE_OPERAND (cond, 0);
3250 vrp_hash_elt->records = NULL;
3251 slot = htab_find_slot (vrp_data, vrp_hash_elt, INSERT);
3253 if (*slot == NULL)
3254 *slot = (void *) vrp_hash_elt;
3255 else
3256 vrp_free (vrp_hash_elt);
3258 vrp_hash_elt = (struct vrp_hash_elt *) *slot;
3259 vrp_records_p = &vrp_hash_elt->records;
3261 element = ggc_alloc (sizeof (struct vrp_element));
3262 element->low = NULL;
3263 element->high = NULL;
3264 element->cond = cond;
3265 element->bb = bb;
3267 VEC_safe_push (vrp_element_p, heap, *vrp_records_p, element);
3268 VEC_safe_push (tree, heap, vrp_variables_stack, TREE_OPERAND (cond, 0));
3272 /* Hashing and equality functions for VRP_DATA.
3274 Since this hash table is addressed by SSA_NAMEs, we can hash on
3275 their version number and equality can be determined with a
3276 pointer comparison. */
3278 static hashval_t
3279 vrp_hash (const void *p)
3281 tree var = ((struct vrp_hash_elt *)p)->var;
3283 return SSA_NAME_VERSION (var);
3286 static int
3287 vrp_eq (const void *p1, const void *p2)
3289 tree var1 = ((struct vrp_hash_elt *)p1)->var;
3290 tree var2 = ((struct vrp_hash_elt *)p2)->var;
3292 return var1 == var2;
3295 /* Hashing and equality functions for AVAIL_EXPRS. The table stores
3296 MODIFY_EXPR statements. We compute a value number for expressions using
3297 the code of the expression and the SSA numbers of its operands. */
3299 static hashval_t
3300 avail_expr_hash (const void *p)
3302 tree stmt = ((struct expr_hash_elt *)p)->stmt;
3303 tree rhs = ((struct expr_hash_elt *)p)->rhs;
3304 tree vuse;
3305 ssa_op_iter iter;
3306 hashval_t val = 0;
3308 /* iterative_hash_expr knows how to deal with any expression and
3309 deals with commutative operators as well, so just use it instead
3310 of duplicating such complexities here. */
3311 val = iterative_hash_expr (rhs, val);
3313 /* If the hash table entry is not associated with a statement, then we
3314 can just hash the expression and not worry about virtual operands
3315 and such. */
3316 if (!stmt || !stmt_ann (stmt))
3317 return val;
3319 /* Add the SSA version numbers of every vuse operand. This is important
3320 because compound variables like arrays are not renamed in the
3321 operands. Rather, the rename is done on the virtual variable
3322 representing all the elements of the array. */
3323 FOR_EACH_SSA_TREE_OPERAND (vuse, stmt, iter, SSA_OP_VUSE)
3324 val = iterative_hash_expr (vuse, val);
3326 return val;
3329 static hashval_t
3330 real_avail_expr_hash (const void *p)
3332 return ((const struct expr_hash_elt *)p)->hash;
3335 static int
3336 avail_expr_eq (const void *p1, const void *p2)
3338 tree stmt1 = ((struct expr_hash_elt *)p1)->stmt;
3339 tree rhs1 = ((struct expr_hash_elt *)p1)->rhs;
3340 tree stmt2 = ((struct expr_hash_elt *)p2)->stmt;
3341 tree rhs2 = ((struct expr_hash_elt *)p2)->rhs;
3343 /* If they are the same physical expression, return true. */
3344 if (rhs1 == rhs2 && stmt1 == stmt2)
3345 return true;
3347 /* If their codes are not equal, then quit now. */
3348 if (TREE_CODE (rhs1) != TREE_CODE (rhs2))
3349 return false;
3351 /* In case of a collision, both RHS have to be identical and have the
3352 same VUSE operands. */
3353 if ((TREE_TYPE (rhs1) == TREE_TYPE (rhs2)
3354 || lang_hooks.types_compatible_p (TREE_TYPE (rhs1), TREE_TYPE (rhs2)))
3355 && operand_equal_p (rhs1, rhs2, OEP_PURE_SAME))
3357 bool ret = compare_ssa_operands_equal (stmt1, stmt2, SSA_OP_VUSE);
3358 gcc_assert (!ret || ((struct expr_hash_elt *)p1)->hash
3359 == ((struct expr_hash_elt *)p2)->hash);
3360 return ret;
3363 return false;