* dwarf2out.c, fold-const.c, ipa-type-escape.c,
[official-gcc.git] / gcc / tree-ssa-dom.c
blob1a59e9eb642baeb3f852d21ad31603b6438b9cd3
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 cfg_altered = cleanup_tree_cfg ();
484 if (rediscover_loops_after_threading)
486 /* Rerun basic loop analysis to discover any newly
487 created loops and update the set of exit edges. */
488 rediscover_loops_after_threading = false;
489 flow_loops_find (&loops_info);
490 mark_loop_exit_edges (&loops_info);
491 flow_loops_free (&loops_info);
493 /* Remove any forwarder blocks inserted by loop
494 header canonicalization. */
495 cleanup_tree_cfg ();
498 calculate_dominance_info (CDI_DOMINATORS);
500 update_ssa (TODO_update_ssa);
502 /* Reinitialize the various tables. */
503 bitmap_clear (nonzero_vars);
504 bitmap_clear (threaded_blocks);
505 htab_empty (avail_exprs);
506 htab_empty (vrp_data);
508 /* Finally, remove everything except invariants in SSA_NAME_VALUE.
510 This must be done before we iterate as we might have a
511 reference to an SSA_NAME which was removed by the call to
512 update_ssa.
514 Long term we will be able to let everything in SSA_NAME_VALUE
515 persist. However, for now, we know this is the safe thing to do. */
516 for (i = 0; i < num_ssa_names; i++)
518 tree name = ssa_name (i);
519 tree value;
521 if (!name)
522 continue;
524 value = SSA_NAME_VALUE (name);
525 if (value && !is_gimple_min_invariant (value))
526 SSA_NAME_VALUE (name) = NULL;
529 opt_stats.num_iterations++;
531 while (optimize > 1 && cfg_altered);
533 /* Debugging dumps. */
534 if (dump_file && (dump_flags & TDF_STATS))
535 dump_dominator_optimization_stats (dump_file);
537 /* We emptied the hash table earlier, now delete it completely. */
538 htab_delete (avail_exprs);
539 htab_delete (vrp_data);
541 /* It is not necessary to clear CURRDEFS, REDIRECTION_EDGES, VRP_DATA,
542 CONST_AND_COPIES, and NONZERO_VARS as they all get cleared at the bottom
543 of the do-while loop above. */
545 /* And finalize the dominator walker. */
546 fini_walk_dominator_tree (&walk_data);
548 /* Free nonzero_vars. */
549 BITMAP_FREE (nonzero_vars);
550 BITMAP_FREE (threaded_blocks);
551 BITMAP_FREE (need_eh_cleanup);
553 VEC_free (tree, heap, avail_exprs_stack);
554 VEC_free (tree, heap, const_and_copies_stack);
555 VEC_free (tree, heap, nonzero_vars_stack);
556 VEC_free (tree, heap, vrp_variables_stack);
557 VEC_free (tree, heap, stmts_to_rescan);
560 static bool
561 gate_dominator (void)
563 return flag_tree_dom != 0;
566 struct tree_opt_pass pass_dominator =
568 "dom", /* name */
569 gate_dominator, /* gate */
570 tree_ssa_dominator_optimize, /* execute */
571 NULL, /* sub */
572 NULL, /* next */
573 0, /* static_pass_number */
574 TV_TREE_SSA_DOMINATOR_OPTS, /* tv_id */
575 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
576 0, /* properties_provided */
577 0, /* properties_destroyed */
578 0, /* todo_flags_start */
579 TODO_dump_func
580 | TODO_update_ssa
581 | TODO_verify_ssa, /* todo_flags_finish */
582 0 /* letter */
586 /* We are exiting E->src, see if E->dest ends with a conditional
587 jump which has a known value when reached via E.
589 Special care is necessary if E is a back edge in the CFG as we
590 will have already recorded equivalences for E->dest into our
591 various tables, including the result of the conditional at
592 the end of E->dest. Threading opportunities are severely
593 limited in that case to avoid short-circuiting the loop
594 incorrectly.
596 Note it is quite common for the first block inside a loop to
597 end with a conditional which is either always true or always
598 false when reached via the loop backedge. Thus we do not want
599 to blindly disable threading across a loop backedge. */
601 static void
602 thread_across_edge (struct dom_walk_data *walk_data, edge e)
604 block_stmt_iterator bsi;
605 tree stmt = NULL;
606 tree phi;
608 /* If E->dest does not end with a conditional, then there is
609 nothing to do. */
610 bsi = bsi_last (e->dest);
611 if (bsi_end_p (bsi)
612 || ! bsi_stmt (bsi)
613 || (TREE_CODE (bsi_stmt (bsi)) != COND_EXPR
614 && TREE_CODE (bsi_stmt (bsi)) != GOTO_EXPR
615 && TREE_CODE (bsi_stmt (bsi)) != SWITCH_EXPR))
616 return;
618 /* The basic idea here is to use whatever knowledge we have
619 from our dominator walk to simplify statements in E->dest,
620 with the ultimate goal being to simplify the conditional
621 at the end of E->dest.
623 Note that we must undo any changes we make to the underlying
624 statements as the simplifications we are making are control
625 flow sensitive (ie, the simplifications are valid when we
626 traverse E, but may not be valid on other paths to E->dest. */
628 /* Each PHI creates a temporary equivalence, record them. Again
629 these are context sensitive equivalences and will be removed
630 by our caller. */
631 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
633 tree src = PHI_ARG_DEF_FROM_EDGE (phi, e);
634 tree dst = PHI_RESULT (phi);
636 /* If the desired argument is not the same as this PHI's result
637 and it is set by a PHI in E->dest, then we can not thread
638 through E->dest. */
639 if (src != dst
640 && TREE_CODE (src) == SSA_NAME
641 && TREE_CODE (SSA_NAME_DEF_STMT (src)) == PHI_NODE
642 && bb_for_stmt (SSA_NAME_DEF_STMT (src)) == e->dest)
643 return;
645 record_const_or_copy (dst, src);
648 /* Try to simplify each statement in E->dest, ultimately leading to
649 a simplification of the COND_EXPR at the end of E->dest.
651 We might consider marking just those statements which ultimately
652 feed the COND_EXPR. It's not clear if the overhead of bookkeeping
653 would be recovered by trying to simplify fewer statements.
655 If we are able to simplify a statement into the form
656 SSA_NAME = (SSA_NAME | gimple invariant), then we can record
657 a context sensitive equivalency which may help us simplify
658 later statements in E->dest.
660 Failure to simplify into the form above merely means that the
661 statement provides no equivalences to help simplify later
662 statements. This does not prevent threading through E->dest. */
663 for (bsi = bsi_start (e->dest); ! bsi_end_p (bsi); bsi_next (&bsi))
665 tree cached_lhs;
667 stmt = bsi_stmt (bsi);
669 /* Ignore empty statements and labels. */
670 if (IS_EMPTY_STMT (stmt) || TREE_CODE (stmt) == LABEL_EXPR)
671 continue;
673 /* Safely handle threading across loop backedges. This is
674 over conservative, but still allows us to capture the
675 majority of the cases where we can thread across a loop
676 backedge. */
677 if ((e->flags & EDGE_DFS_BACK) != 0
678 && TREE_CODE (stmt) != COND_EXPR
679 && TREE_CODE (stmt) != SWITCH_EXPR)
680 return;
682 /* If the statement has volatile operands, then we assume we
683 can not thread through this block. This is overly
684 conservative in some ways. */
685 if (TREE_CODE (stmt) == ASM_EXPR && ASM_VOLATILE_P (stmt))
686 return;
688 /* If this is not a MODIFY_EXPR which sets an SSA_NAME to a new
689 value, then do not try to simplify this statement as it will
690 not simplify in any way that is helpful for jump threading. */
691 if (TREE_CODE (stmt) != MODIFY_EXPR
692 || TREE_CODE (TREE_OPERAND (stmt, 0)) != SSA_NAME)
693 continue;
695 /* At this point we have a statement which assigns an RHS to an
696 SSA_VAR on the LHS. We want to try and simplify this statement
697 to expose more context sensitive equivalences which in turn may
698 allow us to simplify the condition at the end of the loop. */
699 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == SSA_NAME)
700 cached_lhs = TREE_OPERAND (stmt, 1);
701 else
703 /* Copy the operands. */
704 tree *copy;
705 ssa_op_iter iter;
706 use_operand_p use_p;
707 unsigned int num, i = 0;
709 num = NUM_SSA_OPERANDS (stmt, (SSA_OP_USE | SSA_OP_VUSE));
710 copy = xcalloc (num, sizeof (tree));
712 /* Make a copy of the uses & vuses into USES_COPY, then cprop into
713 the operands. */
714 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE | SSA_OP_VUSE)
716 tree tmp = NULL;
717 tree use = USE_FROM_PTR (use_p);
719 copy[i++] = use;
720 if (TREE_CODE (use) == SSA_NAME)
721 tmp = SSA_NAME_VALUE (use);
722 if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
723 SET_USE (use_p, tmp);
726 /* Try to fold/lookup the new expression. Inserting the
727 expression into the hash table is unlikely to help
728 simplify anything later, so just query the hashtable. */
729 cached_lhs = fold (TREE_OPERAND (stmt, 1));
730 if (TREE_CODE (cached_lhs) != SSA_NAME
731 && !is_gimple_min_invariant (cached_lhs))
732 cached_lhs = lookup_avail_expr (stmt, false);
735 /* Restore the statement's original uses/defs. */
736 i = 0;
737 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE | SSA_OP_VUSE)
738 SET_USE (use_p, copy[i++]);
740 free (copy);
743 /* Record the context sensitive equivalence if we were able
744 to simplify this statement. */
745 if (cached_lhs
746 && (TREE_CODE (cached_lhs) == SSA_NAME
747 || is_gimple_min_invariant (cached_lhs)))
748 record_const_or_copy (TREE_OPERAND (stmt, 0), cached_lhs);
751 /* If we stopped at a COND_EXPR or SWITCH_EXPR, see if we know which arm
752 will be taken. */
753 if (stmt
754 && (TREE_CODE (stmt) == COND_EXPR
755 || TREE_CODE (stmt) == GOTO_EXPR
756 || TREE_CODE (stmt) == SWITCH_EXPR))
758 tree cond, cached_lhs;
760 /* Now temporarily cprop the operands and try to find the resulting
761 expression in the hash tables. */
762 if (TREE_CODE (stmt) == COND_EXPR)
763 cond = COND_EXPR_COND (stmt);
764 else if (TREE_CODE (stmt) == GOTO_EXPR)
765 cond = GOTO_DESTINATION (stmt);
766 else
767 cond = SWITCH_COND (stmt);
769 if (COMPARISON_CLASS_P (cond))
771 tree dummy_cond, op0, op1;
772 enum tree_code cond_code;
774 op0 = TREE_OPERAND (cond, 0);
775 op1 = TREE_OPERAND (cond, 1);
776 cond_code = TREE_CODE (cond);
778 /* Get the current value of both operands. */
779 if (TREE_CODE (op0) == SSA_NAME)
781 tree tmp = SSA_NAME_VALUE (op0);
782 if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
783 op0 = tmp;
786 if (TREE_CODE (op1) == SSA_NAME)
788 tree tmp = SSA_NAME_VALUE (op1);
789 if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
790 op1 = tmp;
793 /* Stuff the operator and operands into our dummy conditional
794 expression, creating the dummy conditional if necessary. */
795 dummy_cond = walk_data->global_data;
796 if (! dummy_cond)
798 dummy_cond = build (cond_code, boolean_type_node, op0, op1);
799 dummy_cond = build (COND_EXPR, void_type_node,
800 dummy_cond, NULL, NULL);
801 walk_data->global_data = dummy_cond;
803 else
805 TREE_SET_CODE (COND_EXPR_COND (dummy_cond), cond_code);
806 TREE_OPERAND (COND_EXPR_COND (dummy_cond), 0) = op0;
807 TREE_OPERAND (COND_EXPR_COND (dummy_cond), 1) = op1;
810 /* If the conditional folds to an invariant, then we are done,
811 otherwise look it up in the hash tables. */
812 cached_lhs = local_fold (COND_EXPR_COND (dummy_cond));
813 if (! is_gimple_min_invariant (cached_lhs))
815 cached_lhs = lookup_avail_expr (dummy_cond, false);
816 if (!cached_lhs || ! is_gimple_min_invariant (cached_lhs))
817 cached_lhs = simplify_cond_and_lookup_avail_expr (dummy_cond,
818 NULL,
819 false);
822 /* We can have conditionals which just test the state of a
823 variable rather than use a relational operator. These are
824 simpler to handle. */
825 else if (TREE_CODE (cond) == SSA_NAME)
827 cached_lhs = cond;
828 cached_lhs = SSA_NAME_VALUE (cached_lhs);
829 if (cached_lhs && ! is_gimple_min_invariant (cached_lhs))
830 cached_lhs = NULL;
832 else
833 cached_lhs = lookup_avail_expr (stmt, false);
835 if (cached_lhs)
837 edge taken_edge = find_taken_edge (e->dest, cached_lhs);
838 basic_block dest = (taken_edge ? taken_edge->dest : NULL);
840 if (dest == e->dest)
841 return;
843 /* If we have a known destination for the conditional, then
844 we can perform this optimization, which saves at least one
845 conditional jump each time it applies since we get to
846 bypass the conditional at our original destination. */
847 if (dest)
849 struct edge_info *edge_info;
851 update_bb_profile_for_threading (e->dest, EDGE_FREQUENCY (e),
852 e->count, taken_edge);
853 if (e->aux)
854 edge_info = e->aux;
855 else
856 edge_info = allocate_edge_info (e);
857 edge_info->redirection_target = taken_edge;
858 bitmap_set_bit (threaded_blocks, e->dest->index);
865 /* Initialize local stacks for this optimizer and record equivalences
866 upon entry to BB. Equivalences can come from the edge traversed to
867 reach BB or they may come from PHI nodes at the start of BB. */
869 static void
870 dom_opt_initialize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
871 basic_block bb)
873 if (dump_file && (dump_flags & TDF_DETAILS))
874 fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
876 /* Push a marker on the stacks of local information so that we know how
877 far to unwind when we finalize this block. */
878 VEC_safe_push (tree, heap, avail_exprs_stack, NULL_TREE);
879 VEC_safe_push (tree, heap, const_and_copies_stack, NULL_TREE);
880 VEC_safe_push (tree, heap, nonzero_vars_stack, NULL_TREE);
881 VEC_safe_push (tree, heap, vrp_variables_stack, NULL_TREE);
883 record_equivalences_from_incoming_edge (bb);
885 /* PHI nodes can create equivalences too. */
886 record_equivalences_from_phis (bb);
889 /* Given an expression EXPR (a relational expression or a statement),
890 initialize the hash table element pointed to by ELEMENT. */
892 static void
893 initialize_hash_element (tree expr, tree lhs, struct expr_hash_elt *element)
895 /* Hash table elements may be based on conditional expressions or statements.
897 For the former case, we have no annotation and we want to hash the
898 conditional expression. In the latter case we have an annotation and
899 we want to record the expression the statement evaluates. */
900 if (COMPARISON_CLASS_P (expr) || TREE_CODE (expr) == TRUTH_NOT_EXPR)
902 element->stmt = NULL;
903 element->rhs = expr;
905 else if (TREE_CODE (expr) == COND_EXPR)
907 element->stmt = expr;
908 element->rhs = COND_EXPR_COND (expr);
910 else if (TREE_CODE (expr) == SWITCH_EXPR)
912 element->stmt = expr;
913 element->rhs = SWITCH_COND (expr);
915 else if (TREE_CODE (expr) == RETURN_EXPR && TREE_OPERAND (expr, 0))
917 element->stmt = expr;
918 element->rhs = TREE_OPERAND (TREE_OPERAND (expr, 0), 1);
920 else if (TREE_CODE (expr) == GOTO_EXPR)
922 element->stmt = expr;
923 element->rhs = GOTO_DESTINATION (expr);
925 else
927 element->stmt = expr;
928 element->rhs = TREE_OPERAND (expr, 1);
931 element->lhs = lhs;
932 element->hash = avail_expr_hash (element);
935 /* Remove all the expressions in LOCALS from TABLE, stopping when there are
936 LIMIT entries left in LOCALs. */
938 static void
939 remove_local_expressions_from_table (void)
941 /* Remove all the expressions made available in this block. */
942 while (VEC_length (tree, avail_exprs_stack) > 0)
944 struct expr_hash_elt element;
945 tree expr = VEC_pop (tree, avail_exprs_stack);
947 if (expr == NULL_TREE)
948 break;
950 initialize_hash_element (expr, NULL, &element);
951 htab_remove_elt_with_hash (avail_exprs, &element, element.hash);
955 /* Use the SSA_NAMES in LOCALS to restore TABLE to its original
956 state, stopping when there are LIMIT entries left in LOCALs. */
958 static void
959 restore_nonzero_vars_to_original_value (void)
961 while (VEC_length (tree, nonzero_vars_stack) > 0)
963 tree name = VEC_pop (tree, nonzero_vars_stack);
965 if (name == NULL)
966 break;
968 bitmap_clear_bit (nonzero_vars, SSA_NAME_VERSION (name));
972 /* Use the source/dest pairs in CONST_AND_COPIES_STACK to restore
973 CONST_AND_COPIES to its original state, stopping when we hit a
974 NULL marker. */
976 static void
977 restore_vars_to_original_value (void)
979 while (VEC_length (tree, const_and_copies_stack) > 0)
981 tree prev_value, dest;
983 dest = VEC_pop (tree, const_and_copies_stack);
985 if (dest == NULL)
986 break;
988 prev_value = VEC_pop (tree, const_and_copies_stack);
989 SSA_NAME_VALUE (dest) = prev_value;
993 /* We have finished processing the dominator children of BB, perform
994 any finalization actions in preparation for leaving this node in
995 the dominator tree. */
997 static void
998 dom_opt_finalize_block (struct dom_walk_data *walk_data, basic_block bb)
1000 tree last;
1002 /* If we are at a leaf node in the dominator tree, see if we can thread
1003 the edge from BB through its successor.
1005 Do this before we remove entries from our equivalence tables. */
1006 if (single_succ_p (bb)
1007 && (single_succ_edge (bb)->flags & EDGE_ABNORMAL) == 0
1008 && (get_immediate_dominator (CDI_DOMINATORS, single_succ (bb)) != bb
1009 || phi_nodes (single_succ (bb))))
1012 thread_across_edge (walk_data, single_succ_edge (bb));
1014 else if ((last = last_stmt (bb))
1015 && TREE_CODE (last) == COND_EXPR
1016 && (COMPARISON_CLASS_P (COND_EXPR_COND (last))
1017 || TREE_CODE (COND_EXPR_COND (last)) == SSA_NAME)
1018 && EDGE_COUNT (bb->succs) == 2
1019 && (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL) == 0
1020 && (EDGE_SUCC (bb, 1)->flags & EDGE_ABNORMAL) == 0)
1022 edge true_edge, false_edge;
1024 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1026 /* If the THEN arm is the end of a dominator tree or has PHI nodes,
1027 then try to thread through its edge. */
1028 if (get_immediate_dominator (CDI_DOMINATORS, true_edge->dest) != bb
1029 || phi_nodes (true_edge->dest))
1031 struct edge_info *edge_info;
1032 unsigned int i;
1034 /* Push a marker onto the available expression stack so that we
1035 unwind any expressions related to the TRUE arm before processing
1036 the false arm below. */
1037 VEC_safe_push (tree, heap, avail_exprs_stack, NULL_TREE);
1038 VEC_safe_push (tree, heap, const_and_copies_stack, NULL_TREE);
1040 edge_info = true_edge->aux;
1042 /* If we have info associated with this edge, record it into
1043 our equivalency tables. */
1044 if (edge_info)
1046 tree *cond_equivalences = edge_info->cond_equivalences;
1047 tree lhs = edge_info->lhs;
1048 tree rhs = edge_info->rhs;
1050 /* If we have a simple NAME = VALUE equivalency record it. */
1051 if (lhs && TREE_CODE (lhs) == SSA_NAME)
1052 record_const_or_copy (lhs, rhs);
1054 /* If we have 0 = COND or 1 = COND equivalences, record them
1055 into our expression hash tables. */
1056 if (cond_equivalences)
1057 for (i = 0; i < edge_info->max_cond_equivalences; i += 2)
1059 tree expr = cond_equivalences[i];
1060 tree value = cond_equivalences[i + 1];
1062 record_cond (expr, value);
1066 /* Now thread the edge. */
1067 thread_across_edge (walk_data, true_edge);
1069 /* And restore the various tables to their state before
1070 we threaded this edge. */
1071 remove_local_expressions_from_table ();
1072 restore_vars_to_original_value ();
1075 /* Similarly for the ELSE arm. */
1076 if (get_immediate_dominator (CDI_DOMINATORS, false_edge->dest) != bb
1077 || phi_nodes (false_edge->dest))
1079 struct edge_info *edge_info;
1080 unsigned int i;
1082 edge_info = false_edge->aux;
1084 /* If we have info associated with this edge, record it into
1085 our equivalency tables. */
1086 if (edge_info)
1088 tree *cond_equivalences = edge_info->cond_equivalences;
1089 tree lhs = edge_info->lhs;
1090 tree rhs = edge_info->rhs;
1092 /* If we have a simple NAME = VALUE equivalency record it. */
1093 if (lhs && TREE_CODE (lhs) == SSA_NAME)
1094 record_const_or_copy (lhs, rhs);
1096 /* If we have 0 = COND or 1 = COND equivalences, record them
1097 into our expression hash tables. */
1098 if (cond_equivalences)
1099 for (i = 0; i < edge_info->max_cond_equivalences; i += 2)
1101 tree expr = cond_equivalences[i];
1102 tree value = cond_equivalences[i + 1];
1104 record_cond (expr, value);
1108 thread_across_edge (walk_data, false_edge);
1110 /* No need to remove local expressions from our tables
1111 or restore vars to their original value as that will
1112 be done immediately below. */
1116 remove_local_expressions_from_table ();
1117 restore_nonzero_vars_to_original_value ();
1118 restore_vars_to_original_value ();
1120 /* Remove VRP records associated with this basic block. They are no
1121 longer valid.
1123 To be efficient, we note which variables have had their values
1124 constrained in this block. So walk over each variable in the
1125 VRP_VARIABLEs array. */
1126 while (VEC_length (tree, vrp_variables_stack) > 0)
1128 tree var = VEC_pop (tree, vrp_variables_stack);
1129 struct vrp_hash_elt vrp_hash_elt, *vrp_hash_elt_p;
1130 void **slot;
1132 /* Each variable has a stack of value range records. We want to
1133 invalidate those associated with our basic block. So we walk
1134 the array backwards popping off records associated with our
1135 block. Once we hit a record not associated with our block
1136 we are done. */
1137 VEC(vrp_element_p,heap) **var_vrp_records;
1139 if (var == NULL)
1140 break;
1142 vrp_hash_elt.var = var;
1143 vrp_hash_elt.records = NULL;
1145 slot = htab_find_slot (vrp_data, &vrp_hash_elt, NO_INSERT);
1147 vrp_hash_elt_p = (struct vrp_hash_elt *) *slot;
1148 var_vrp_records = &vrp_hash_elt_p->records;
1150 while (VEC_length (vrp_element_p, *var_vrp_records) > 0)
1152 struct vrp_element *element
1153 = VEC_last (vrp_element_p, *var_vrp_records);
1155 if (element->bb != bb)
1156 break;
1158 VEC_pop (vrp_element_p, *var_vrp_records);
1162 /* If we queued any statements to rescan in this block, then
1163 go ahead and rescan them now. */
1164 while (VEC_length (tree, stmts_to_rescan) > 0)
1166 tree stmt = VEC_last (tree, stmts_to_rescan);
1167 basic_block stmt_bb = bb_for_stmt (stmt);
1169 if (stmt_bb != bb)
1170 break;
1172 VEC_pop (tree, stmts_to_rescan);
1173 mark_new_vars_to_rename (stmt);
1177 /* PHI nodes can create equivalences too.
1179 Ignoring any alternatives which are the same as the result, if
1180 all the alternatives are equal, then the PHI node creates an
1181 equivalence.
1183 Additionally, if all the PHI alternatives are known to have a nonzero
1184 value, then the result of this PHI is known to have a nonzero value,
1185 even if we do not know its exact value. */
1187 static void
1188 record_equivalences_from_phis (basic_block bb)
1190 tree phi;
1192 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1194 tree lhs = PHI_RESULT (phi);
1195 tree rhs = NULL;
1196 int i;
1198 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1200 tree t = PHI_ARG_DEF (phi, i);
1202 /* Ignore alternatives which are the same as our LHS. Since
1203 LHS is a PHI_RESULT, it is known to be a SSA_NAME, so we
1204 can simply compare pointers. */
1205 if (lhs == t)
1206 continue;
1208 /* If we have not processed an alternative yet, then set
1209 RHS to this alternative. */
1210 if (rhs == NULL)
1211 rhs = t;
1212 /* If we have processed an alternative (stored in RHS), then
1213 see if it is equal to this one. If it isn't, then stop
1214 the search. */
1215 else if (! operand_equal_for_phi_arg_p (rhs, t))
1216 break;
1219 /* If we had no interesting alternatives, then all the RHS alternatives
1220 must have been the same as LHS. */
1221 if (!rhs)
1222 rhs = lhs;
1224 /* If we managed to iterate through each PHI alternative without
1225 breaking out of the loop, then we have a PHI which may create
1226 a useful equivalence. We do not need to record unwind data for
1227 this, since this is a true assignment and not an equivalence
1228 inferred from a comparison. All uses of this ssa name are dominated
1229 by this assignment, so unwinding just costs time and space. */
1230 if (i == PHI_NUM_ARGS (phi)
1231 && may_propagate_copy (lhs, rhs))
1232 SSA_NAME_VALUE (lhs) = rhs;
1234 /* Now see if we know anything about the nonzero property for the
1235 result of this PHI. */
1236 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1238 if (!PHI_ARG_NONZERO (phi, i))
1239 break;
1242 if (i == PHI_NUM_ARGS (phi))
1243 bitmap_set_bit (nonzero_vars, SSA_NAME_VERSION (PHI_RESULT (phi)));
1247 /* Ignoring loop backedges, if BB has precisely one incoming edge then
1248 return that edge. Otherwise return NULL. */
1249 static edge
1250 single_incoming_edge_ignoring_loop_edges (basic_block bb)
1252 edge retval = NULL;
1253 edge e;
1254 edge_iterator ei;
1256 FOR_EACH_EDGE (e, ei, bb->preds)
1258 /* A loop back edge can be identified by the destination of
1259 the edge dominating the source of the edge. */
1260 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
1261 continue;
1263 /* If we have already seen a non-loop edge, then we must have
1264 multiple incoming non-loop edges and thus we return NULL. */
1265 if (retval)
1266 return NULL;
1268 /* This is the first non-loop incoming edge we have found. Record
1269 it. */
1270 retval = e;
1273 return retval;
1276 /* Record any equivalences created by the incoming edge to BB. If BB
1277 has more than one incoming edge, then no equivalence is created. */
1279 static void
1280 record_equivalences_from_incoming_edge (basic_block bb)
1282 edge e;
1283 basic_block parent;
1284 struct edge_info *edge_info;
1286 /* If our parent block ended with a control statement, then we may be
1287 able to record some equivalences based on which outgoing edge from
1288 the parent was followed. */
1289 parent = get_immediate_dominator (CDI_DOMINATORS, bb);
1291 e = single_incoming_edge_ignoring_loop_edges (bb);
1293 /* If we had a single incoming edge from our parent block, then enter
1294 any data associated with the edge into our tables. */
1295 if (e && e->src == parent)
1297 unsigned int i;
1299 edge_info = e->aux;
1301 if (edge_info)
1303 tree lhs = edge_info->lhs;
1304 tree rhs = edge_info->rhs;
1305 tree *cond_equivalences = edge_info->cond_equivalences;
1307 if (lhs)
1308 record_equality (lhs, rhs);
1310 if (cond_equivalences)
1312 bool recorded_range = false;
1313 for (i = 0; i < edge_info->max_cond_equivalences; i += 2)
1315 tree expr = cond_equivalences[i];
1316 tree value = cond_equivalences[i + 1];
1318 record_cond (expr, value);
1320 /* For the first true equivalence, record range
1321 information. We only do this for the first
1322 true equivalence as it should dominate any
1323 later true equivalences. */
1324 if (! recorded_range
1325 && COMPARISON_CLASS_P (expr)
1326 && value == boolean_true_node
1327 && TREE_CONSTANT (TREE_OPERAND (expr, 1)))
1329 record_range (expr, bb);
1330 recorded_range = true;
1338 /* Dump SSA statistics on FILE. */
1340 void
1341 dump_dominator_optimization_stats (FILE *file)
1343 long n_exprs;
1345 fprintf (file, "Total number of statements: %6ld\n\n",
1346 opt_stats.num_stmts);
1347 fprintf (file, "Exprs considered for dominator optimizations: %6ld\n",
1348 opt_stats.num_exprs_considered);
1350 n_exprs = opt_stats.num_exprs_considered;
1351 if (n_exprs == 0)
1352 n_exprs = 1;
1354 fprintf (file, " Redundant expressions eliminated: %6ld (%.0f%%)\n",
1355 opt_stats.num_re, PERCENT (opt_stats.num_re,
1356 n_exprs));
1357 fprintf (file, " Constants propagated: %6ld\n",
1358 opt_stats.num_const_prop);
1359 fprintf (file, " Copies propagated: %6ld\n",
1360 opt_stats.num_copy_prop);
1362 fprintf (file, "\nTotal number of DOM iterations: %6ld\n",
1363 opt_stats.num_iterations);
1365 fprintf (file, "\nHash table statistics:\n");
1367 fprintf (file, " avail_exprs: ");
1368 htab_statistics (file, avail_exprs);
1372 /* Dump SSA statistics on stderr. */
1374 void
1375 debug_dominator_optimization_stats (void)
1377 dump_dominator_optimization_stats (stderr);
1381 /* Dump statistics for the hash table HTAB. */
1383 static void
1384 htab_statistics (FILE *file, htab_t htab)
1386 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
1387 (long) htab_size (htab),
1388 (long) htab_elements (htab),
1389 htab_collisions (htab));
1392 /* Record the fact that VAR has a nonzero value, though we may not know
1393 its exact value. Note that if VAR is already known to have a nonzero
1394 value, then we do nothing. */
1396 static void
1397 record_var_is_nonzero (tree var)
1399 int indx = SSA_NAME_VERSION (var);
1401 if (bitmap_bit_p (nonzero_vars, indx))
1402 return;
1404 /* Mark it in the global table. */
1405 bitmap_set_bit (nonzero_vars, indx);
1407 /* Record this SSA_NAME so that we can reset the global table
1408 when we leave this block. */
1409 VEC_safe_push (tree, heap, nonzero_vars_stack, var);
1412 /* Enter a statement into the true/false expression hash table indicating
1413 that the condition COND has the value VALUE. */
1415 static void
1416 record_cond (tree cond, tree value)
1418 struct expr_hash_elt *element = xmalloc (sizeof (struct expr_hash_elt));
1419 void **slot;
1421 initialize_hash_element (cond, value, element);
1423 slot = htab_find_slot_with_hash (avail_exprs, (void *)element,
1424 element->hash, INSERT);
1425 if (*slot == NULL)
1427 *slot = (void *) element;
1428 VEC_safe_push (tree, heap, avail_exprs_stack, cond);
1430 else
1431 free (element);
1434 /* Build a new conditional using NEW_CODE, OP0 and OP1 and store
1435 the new conditional into *p, then store a boolean_true_node
1436 into *(p + 1). */
1438 static void
1439 build_and_record_new_cond (enum tree_code new_code, tree op0, tree op1, tree *p)
1441 *p = build2 (new_code, boolean_type_node, op0, op1);
1442 p++;
1443 *p = boolean_true_node;
1446 /* Record that COND is true and INVERTED is false into the edge information
1447 structure. Also record that any conditions dominated by COND are true
1448 as well.
1450 For example, if a < b is true, then a <= b must also be true. */
1452 static void
1453 record_conditions (struct edge_info *edge_info, tree cond, tree inverted)
1455 tree op0, op1;
1457 if (!COMPARISON_CLASS_P (cond))
1458 return;
1460 op0 = TREE_OPERAND (cond, 0);
1461 op1 = TREE_OPERAND (cond, 1);
1463 switch (TREE_CODE (cond))
1465 case LT_EXPR:
1466 case GT_EXPR:
1467 edge_info->max_cond_equivalences = 12;
1468 edge_info->cond_equivalences = xmalloc (12 * sizeof (tree));
1469 build_and_record_new_cond ((TREE_CODE (cond) == LT_EXPR
1470 ? LE_EXPR : GE_EXPR),
1471 op0, op1, &edge_info->cond_equivalences[4]);
1472 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1473 &edge_info->cond_equivalences[6]);
1474 build_and_record_new_cond (NE_EXPR, op0, op1,
1475 &edge_info->cond_equivalences[8]);
1476 build_and_record_new_cond (LTGT_EXPR, op0, op1,
1477 &edge_info->cond_equivalences[10]);
1478 break;
1480 case GE_EXPR:
1481 case LE_EXPR:
1482 edge_info->max_cond_equivalences = 6;
1483 edge_info->cond_equivalences = xmalloc (6 * sizeof (tree));
1484 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1485 &edge_info->cond_equivalences[4]);
1486 break;
1488 case EQ_EXPR:
1489 edge_info->max_cond_equivalences = 10;
1490 edge_info->cond_equivalences = xmalloc (10 * sizeof (tree));
1491 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1492 &edge_info->cond_equivalences[4]);
1493 build_and_record_new_cond (LE_EXPR, op0, op1,
1494 &edge_info->cond_equivalences[6]);
1495 build_and_record_new_cond (GE_EXPR, op0, op1,
1496 &edge_info->cond_equivalences[8]);
1497 break;
1499 case UNORDERED_EXPR:
1500 edge_info->max_cond_equivalences = 16;
1501 edge_info->cond_equivalences = xmalloc (16 * sizeof (tree));
1502 build_and_record_new_cond (NE_EXPR, op0, op1,
1503 &edge_info->cond_equivalences[4]);
1504 build_and_record_new_cond (UNLE_EXPR, op0, op1,
1505 &edge_info->cond_equivalences[6]);
1506 build_and_record_new_cond (UNGE_EXPR, op0, op1,
1507 &edge_info->cond_equivalences[8]);
1508 build_and_record_new_cond (UNEQ_EXPR, op0, op1,
1509 &edge_info->cond_equivalences[10]);
1510 build_and_record_new_cond (UNLT_EXPR, op0, op1,
1511 &edge_info->cond_equivalences[12]);
1512 build_and_record_new_cond (UNGT_EXPR, op0, op1,
1513 &edge_info->cond_equivalences[14]);
1514 break;
1516 case UNLT_EXPR:
1517 case UNGT_EXPR:
1518 edge_info->max_cond_equivalences = 8;
1519 edge_info->cond_equivalences = xmalloc (8 * sizeof (tree));
1520 build_and_record_new_cond ((TREE_CODE (cond) == UNLT_EXPR
1521 ? UNLE_EXPR : UNGE_EXPR),
1522 op0, op1, &edge_info->cond_equivalences[4]);
1523 build_and_record_new_cond (NE_EXPR, op0, op1,
1524 &edge_info->cond_equivalences[6]);
1525 break;
1527 case UNEQ_EXPR:
1528 edge_info->max_cond_equivalences = 8;
1529 edge_info->cond_equivalences = xmalloc (8 * sizeof (tree));
1530 build_and_record_new_cond (UNLE_EXPR, op0, op1,
1531 &edge_info->cond_equivalences[4]);
1532 build_and_record_new_cond (UNGE_EXPR, op0, op1,
1533 &edge_info->cond_equivalences[6]);
1534 break;
1536 case LTGT_EXPR:
1537 edge_info->max_cond_equivalences = 8;
1538 edge_info->cond_equivalences = xmalloc (8 * sizeof (tree));
1539 build_and_record_new_cond (NE_EXPR, op0, op1,
1540 &edge_info->cond_equivalences[4]);
1541 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1542 &edge_info->cond_equivalences[6]);
1543 break;
1545 default:
1546 edge_info->max_cond_equivalences = 4;
1547 edge_info->cond_equivalences = xmalloc (4 * sizeof (tree));
1548 break;
1551 /* Now store the original true and false conditions into the first
1552 two slots. */
1553 edge_info->cond_equivalences[0] = cond;
1554 edge_info->cond_equivalences[1] = boolean_true_node;
1555 edge_info->cond_equivalences[2] = inverted;
1556 edge_info->cond_equivalences[3] = boolean_false_node;
1559 /* A helper function for record_const_or_copy and record_equality.
1560 Do the work of recording the value and undo info. */
1562 static void
1563 record_const_or_copy_1 (tree x, tree y, tree prev_x)
1565 SSA_NAME_VALUE (x) = y;
1567 VEC_reserve (tree, heap, const_and_copies_stack, 2);
1568 VEC_quick_push (tree, const_and_copies_stack, prev_x);
1569 VEC_quick_push (tree, const_and_copies_stack, x);
1573 /* Return the loop depth of the basic block of the defining statement of X.
1574 This number should not be treated as absolutely correct because the loop
1575 information may not be completely up-to-date when dom runs. However, it
1576 will be relatively correct, and as more passes are taught to keep loop info
1577 up to date, the result will become more and more accurate. */
1580 loop_depth_of_name (tree x)
1582 tree defstmt;
1583 basic_block defbb;
1585 /* If it's not an SSA_NAME, we have no clue where the definition is. */
1586 if (TREE_CODE (x) != SSA_NAME)
1587 return 0;
1589 /* Otherwise return the loop depth of the defining statement's bb.
1590 Note that there may not actually be a bb for this statement, if the
1591 ssa_name is live on entry. */
1592 defstmt = SSA_NAME_DEF_STMT (x);
1593 defbb = bb_for_stmt (defstmt);
1594 if (!defbb)
1595 return 0;
1597 return defbb->loop_depth;
1601 /* Record that X is equal to Y in const_and_copies. Record undo
1602 information in the block-local vector. */
1604 static void
1605 record_const_or_copy (tree x, tree y)
1607 tree prev_x = SSA_NAME_VALUE (x);
1609 if (TREE_CODE (y) == SSA_NAME)
1611 tree tmp = SSA_NAME_VALUE (y);
1612 if (tmp)
1613 y = tmp;
1616 record_const_or_copy_1 (x, y, prev_x);
1619 /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1620 This constrains the cases in which we may treat this as assignment. */
1622 static void
1623 record_equality (tree x, tree y)
1625 tree prev_x = NULL, prev_y = NULL;
1627 if (TREE_CODE (x) == SSA_NAME)
1628 prev_x = SSA_NAME_VALUE (x);
1629 if (TREE_CODE (y) == SSA_NAME)
1630 prev_y = SSA_NAME_VALUE (y);
1632 /* If one of the previous values is invariant, or invariant in more loops
1633 (by depth), then use that.
1634 Otherwise it doesn't matter which value we choose, just so
1635 long as we canonicalize on one value. */
1636 if (TREE_INVARIANT (y))
1638 else if (TREE_INVARIANT (x) || (loop_depth_of_name (x) <= loop_depth_of_name (y)))
1639 prev_x = x, x = y, y = prev_x, prev_x = prev_y;
1640 else if (prev_x && TREE_INVARIANT (prev_x))
1641 x = y, y = prev_x, prev_x = prev_y;
1642 else if (prev_y && TREE_CODE (prev_y) != VALUE_HANDLE)
1643 y = prev_y;
1645 /* After the swapping, we must have one SSA_NAME. */
1646 if (TREE_CODE (x) != SSA_NAME)
1647 return;
1649 /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1650 variable compared against zero. If we're honoring signed zeros,
1651 then we cannot record this value unless we know that the value is
1652 nonzero. */
1653 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (x)))
1654 && (TREE_CODE (y) != REAL_CST
1655 || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (y))))
1656 return;
1658 record_const_or_copy_1 (x, y, prev_x);
1661 /* Return true, if it is ok to do folding of an associative expression.
1662 EXP is the tree for the associative expression. */
1664 static inline bool
1665 unsafe_associative_fp_binop (tree exp)
1667 enum tree_code code = TREE_CODE (exp);
1668 return !(!flag_unsafe_math_optimizations
1669 && (code == MULT_EXPR || code == PLUS_EXPR
1670 || code == MINUS_EXPR)
1671 && FLOAT_TYPE_P (TREE_TYPE (exp)));
1674 /* Returns true when STMT is a simple iv increment. It detects the
1675 following situation:
1677 i_1 = phi (..., i_2)
1678 i_2 = i_1 +/- ... */
1680 static bool
1681 simple_iv_increment_p (tree stmt)
1683 tree lhs, rhs, preinc, phi;
1684 unsigned i;
1686 if (TREE_CODE (stmt) != MODIFY_EXPR)
1687 return false;
1689 lhs = TREE_OPERAND (stmt, 0);
1690 if (TREE_CODE (lhs) != SSA_NAME)
1691 return false;
1693 rhs = TREE_OPERAND (stmt, 1);
1695 if (TREE_CODE (rhs) != PLUS_EXPR
1696 && TREE_CODE (rhs) != MINUS_EXPR)
1697 return false;
1699 preinc = TREE_OPERAND (rhs, 0);
1700 if (TREE_CODE (preinc) != SSA_NAME)
1701 return false;
1703 phi = SSA_NAME_DEF_STMT (preinc);
1704 if (TREE_CODE (phi) != PHI_NODE)
1705 return false;
1707 for (i = 0; i < (unsigned) PHI_NUM_ARGS (phi); i++)
1708 if (PHI_ARG_DEF (phi, i) == lhs)
1709 return true;
1711 return false;
1714 /* STMT is a MODIFY_EXPR for which we were unable to find RHS in the
1715 hash tables. Try to simplify the RHS using whatever equivalences
1716 we may have recorded.
1718 If we are able to simplify the RHS, then lookup the simplified form in
1719 the hash table and return the result. Otherwise return NULL. */
1721 static tree
1722 simplify_rhs_and_lookup_avail_expr (tree stmt, int insert)
1724 tree rhs = TREE_OPERAND (stmt, 1);
1725 enum tree_code rhs_code = TREE_CODE (rhs);
1726 tree result = NULL;
1728 /* If we have lhs = ~x, look and see if we earlier had x = ~y.
1729 In which case we can change this statement to be lhs = y.
1730 Which can then be copy propagated.
1732 Similarly for negation. */
1733 if ((rhs_code == BIT_NOT_EXPR || rhs_code == NEGATE_EXPR)
1734 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
1736 /* Get the definition statement for our RHS. */
1737 tree rhs_def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs, 0));
1739 /* See if the RHS_DEF_STMT has the same form as our statement. */
1740 if (TREE_CODE (rhs_def_stmt) == MODIFY_EXPR
1741 && TREE_CODE (TREE_OPERAND (rhs_def_stmt, 1)) == rhs_code)
1743 tree rhs_def_operand;
1745 rhs_def_operand = TREE_OPERAND (TREE_OPERAND (rhs_def_stmt, 1), 0);
1747 /* Verify that RHS_DEF_OPERAND is a suitable SSA variable. */
1748 if (TREE_CODE (rhs_def_operand) == SSA_NAME
1749 && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs_def_operand))
1750 result = update_rhs_and_lookup_avail_expr (stmt,
1751 rhs_def_operand,
1752 insert);
1756 /* If we have z = (x OP C1), see if we earlier had x = y OP C2.
1757 If OP is associative, create and fold (y OP C2) OP C1 which
1758 should result in (y OP C3), use that as the RHS for the
1759 assignment. Add minus to this, as we handle it specially below. */
1760 if ((associative_tree_code (rhs_code) || rhs_code == MINUS_EXPR)
1761 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME
1762 && is_gimple_min_invariant (TREE_OPERAND (rhs, 1)))
1764 tree rhs_def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs, 0));
1766 /* If the statement defines an induction variable, do not propagate
1767 its value, so that we do not create overlapping life ranges. */
1768 if (simple_iv_increment_p (rhs_def_stmt))
1769 goto dont_fold_assoc;
1771 /* See if the RHS_DEF_STMT has the same form as our statement. */
1772 if (TREE_CODE (rhs_def_stmt) == MODIFY_EXPR)
1774 tree rhs_def_rhs = TREE_OPERAND (rhs_def_stmt, 1);
1775 enum tree_code rhs_def_code = TREE_CODE (rhs_def_rhs);
1777 if ((rhs_code == rhs_def_code && unsafe_associative_fp_binop (rhs))
1778 || (rhs_code == PLUS_EXPR && rhs_def_code == MINUS_EXPR)
1779 || (rhs_code == MINUS_EXPR && rhs_def_code == PLUS_EXPR))
1781 tree def_stmt_op0 = TREE_OPERAND (rhs_def_rhs, 0);
1782 tree def_stmt_op1 = TREE_OPERAND (rhs_def_rhs, 1);
1784 if (TREE_CODE (def_stmt_op0) == SSA_NAME
1785 && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def_stmt_op0)
1786 && is_gimple_min_invariant (def_stmt_op1))
1788 tree outer_const = TREE_OPERAND (rhs, 1);
1789 tree type = TREE_TYPE (TREE_OPERAND (stmt, 0));
1790 tree t;
1792 /* If we care about correct floating point results, then
1793 don't fold x + c1 - c2. Note that we need to take both
1794 the codes and the signs to figure this out. */
1795 if (FLOAT_TYPE_P (type)
1796 && !flag_unsafe_math_optimizations
1797 && (rhs_def_code == PLUS_EXPR
1798 || rhs_def_code == MINUS_EXPR))
1800 bool neg = false;
1802 neg ^= (rhs_code == MINUS_EXPR);
1803 neg ^= (rhs_def_code == MINUS_EXPR);
1804 neg ^= real_isneg (TREE_REAL_CST_PTR (outer_const));
1805 neg ^= real_isneg (TREE_REAL_CST_PTR (def_stmt_op1));
1807 if (neg)
1808 goto dont_fold_assoc;
1811 /* Ho hum. So fold will only operate on the outermost
1812 thingy that we give it, so we have to build the new
1813 expression in two pieces. This requires that we handle
1814 combinations of plus and minus. */
1815 if (rhs_def_code != rhs_code)
1817 if (rhs_def_code == MINUS_EXPR)
1818 t = build (MINUS_EXPR, type, outer_const, def_stmt_op1);
1819 else
1820 t = build (MINUS_EXPR, type, def_stmt_op1, outer_const);
1821 rhs_code = PLUS_EXPR;
1823 else if (rhs_def_code == MINUS_EXPR)
1824 t = build (PLUS_EXPR, type, def_stmt_op1, outer_const);
1825 else
1826 t = build (rhs_def_code, type, def_stmt_op1, outer_const);
1827 t = local_fold (t);
1828 t = build (rhs_code, type, def_stmt_op0, t);
1829 t = local_fold (t);
1831 /* If the result is a suitable looking gimple expression,
1832 then use it instead of the original for STMT. */
1833 if (TREE_CODE (t) == SSA_NAME
1834 || (UNARY_CLASS_P (t)
1835 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME)
1836 || ((BINARY_CLASS_P (t) || COMPARISON_CLASS_P (t))
1837 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
1838 && is_gimple_val (TREE_OPERAND (t, 1))))
1839 result = update_rhs_and_lookup_avail_expr (stmt, t, insert);
1843 dont_fold_assoc:;
1846 /* Optimize *"foo" into 'f'. This is done here rather than
1847 in fold to avoid problems with stuff like &*"foo". */
1848 if (TREE_CODE (rhs) == INDIRECT_REF || TREE_CODE (rhs) == ARRAY_REF)
1850 tree t = fold_read_from_constant_string (rhs);
1852 if (t)
1853 result = update_rhs_and_lookup_avail_expr (stmt, t, insert);
1856 return result;
1859 /* COND is a condition of the form:
1861 x == const or x != const
1863 Look back to x's defining statement and see if x is defined as
1865 x = (type) y;
1867 If const is unchanged if we convert it to type, then we can build
1868 the equivalent expression:
1871 y == const or y != const
1873 Which may allow further optimizations.
1875 Return the equivalent comparison or NULL if no such equivalent comparison
1876 was found. */
1878 static tree
1879 find_equivalent_equality_comparison (tree cond)
1881 tree op0 = TREE_OPERAND (cond, 0);
1882 tree op1 = TREE_OPERAND (cond, 1);
1883 tree def_stmt = SSA_NAME_DEF_STMT (op0);
1885 /* OP0 might have been a parameter, so first make sure it
1886 was defined by a MODIFY_EXPR. */
1887 if (def_stmt && TREE_CODE (def_stmt) == MODIFY_EXPR)
1889 tree def_rhs = TREE_OPERAND (def_stmt, 1);
1892 /* If either operand to the comparison is a pointer to
1893 a function, then we can not apply this optimization
1894 as some targets require function pointers to be
1895 canonicalized and in this case this optimization would
1896 eliminate a necessary canonicalization. */
1897 if ((POINTER_TYPE_P (TREE_TYPE (op0))
1898 && TREE_CODE (TREE_TYPE (TREE_TYPE (op0))) == FUNCTION_TYPE)
1899 || (POINTER_TYPE_P (TREE_TYPE (op1))
1900 && TREE_CODE (TREE_TYPE (TREE_TYPE (op1))) == FUNCTION_TYPE))
1901 return NULL;
1903 /* Now make sure the RHS of the MODIFY_EXPR is a typecast. */
1904 if ((TREE_CODE (def_rhs) == NOP_EXPR
1905 || TREE_CODE (def_rhs) == CONVERT_EXPR)
1906 && TREE_CODE (TREE_OPERAND (def_rhs, 0)) == SSA_NAME)
1908 tree def_rhs_inner = TREE_OPERAND (def_rhs, 0);
1909 tree def_rhs_inner_type = TREE_TYPE (def_rhs_inner);
1910 tree new;
1912 if (TYPE_PRECISION (def_rhs_inner_type)
1913 > TYPE_PRECISION (TREE_TYPE (def_rhs)))
1914 return NULL;
1916 /* If the inner type of the conversion is a pointer to
1917 a function, then we can not apply this optimization
1918 as some targets require function pointers to be
1919 canonicalized. This optimization would result in
1920 canonicalization of the pointer when it was not originally
1921 needed/intended. */
1922 if (POINTER_TYPE_P (def_rhs_inner_type)
1923 && TREE_CODE (TREE_TYPE (def_rhs_inner_type)) == FUNCTION_TYPE)
1924 return NULL;
1926 /* What we want to prove is that if we convert OP1 to
1927 the type of the object inside the NOP_EXPR that the
1928 result is still equivalent to SRC.
1930 If that is true, the build and return new equivalent
1931 condition which uses the source of the typecast and the
1932 new constant (which has only changed its type). */
1933 new = build1 (TREE_CODE (def_rhs), def_rhs_inner_type, op1);
1934 new = local_fold (new);
1935 if (is_gimple_val (new) && tree_int_cst_equal (new, op1))
1936 return build (TREE_CODE (cond), TREE_TYPE (cond),
1937 def_rhs_inner, new);
1940 return NULL;
1943 /* STMT is a COND_EXPR for which we could not trivially determine its
1944 result. This routine attempts to find equivalent forms of the
1945 condition which we may be able to optimize better. It also
1946 uses simple value range propagation to optimize conditionals. */
1948 static tree
1949 simplify_cond_and_lookup_avail_expr (tree stmt,
1950 stmt_ann_t ann,
1951 int insert)
1953 tree cond = COND_EXPR_COND (stmt);
1955 if (COMPARISON_CLASS_P (cond))
1957 tree op0 = TREE_OPERAND (cond, 0);
1958 tree op1 = TREE_OPERAND (cond, 1);
1960 if (TREE_CODE (op0) == SSA_NAME && is_gimple_min_invariant (op1))
1962 int limit;
1963 tree low, high, cond_low, cond_high;
1964 int lowequal, highequal, swapped, no_overlap, subset, cond_inverted;
1965 VEC(vrp_element_p,heap) **vrp_records;
1966 struct vrp_element *element;
1967 struct vrp_hash_elt vrp_hash_elt, *vrp_hash_elt_p;
1968 void **slot;
1970 /* First see if we have test of an SSA_NAME against a constant
1971 where the SSA_NAME is defined by an earlier typecast which
1972 is irrelevant when performing tests against the given
1973 constant. */
1974 if (TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
1976 tree new_cond = find_equivalent_equality_comparison (cond);
1978 if (new_cond)
1980 /* Update the statement to use the new equivalent
1981 condition. */
1982 COND_EXPR_COND (stmt) = new_cond;
1984 /* If this is not a real stmt, ann will be NULL and we
1985 avoid processing the operands. */
1986 if (ann)
1987 mark_stmt_modified (stmt);
1989 /* Lookup the condition and return its known value if it
1990 exists. */
1991 new_cond = lookup_avail_expr (stmt, insert);
1992 if (new_cond)
1993 return new_cond;
1995 /* The operands have changed, so update op0 and op1. */
1996 op0 = TREE_OPERAND (cond, 0);
1997 op1 = TREE_OPERAND (cond, 1);
2001 /* Consult the value range records for this variable (if they exist)
2002 to see if we can eliminate or simplify this conditional.
2004 Note two tests are necessary to determine no records exist.
2005 First we have to see if the virtual array exists, if it
2006 exists, then we have to check its active size.
2008 Also note the vast majority of conditionals are not testing
2009 a variable which has had its range constrained by an earlier
2010 conditional. So this filter avoids a lot of unnecessary work. */
2011 vrp_hash_elt.var = op0;
2012 vrp_hash_elt.records = NULL;
2013 slot = htab_find_slot (vrp_data, &vrp_hash_elt, NO_INSERT);
2014 if (slot == NULL)
2015 return NULL;
2017 vrp_hash_elt_p = (struct vrp_hash_elt *) *slot;
2018 vrp_records = &vrp_hash_elt_p->records;
2020 limit = VEC_length (vrp_element_p, *vrp_records);
2022 /* If we have no value range records for this variable, or we are
2023 unable to extract a range for this condition, then there is
2024 nothing to do. */
2025 if (limit == 0
2026 || ! extract_range_from_cond (cond, &cond_high,
2027 &cond_low, &cond_inverted))
2028 return NULL;
2030 /* We really want to avoid unnecessary computations of range
2031 info. So all ranges are computed lazily; this avoids a
2032 lot of unnecessary work. i.e., we record the conditional,
2033 but do not process how it constrains the variable's
2034 potential values until we know that processing the condition
2035 could be helpful.
2037 However, we do not want to have to walk a potentially long
2038 list of ranges, nor do we want to compute a variable's
2039 range more than once for a given path.
2041 Luckily, each time we encounter a conditional that can not
2042 be otherwise optimized we will end up here and we will
2043 compute the necessary range information for the variable
2044 used in this condition.
2046 Thus you can conclude that there will never be more than one
2047 conditional associated with a variable which has not been
2048 processed. So we never need to merge more than one new
2049 conditional into the current range.
2051 These properties also help us avoid unnecessary work. */
2052 element = VEC_last (vrp_element_p, *vrp_records);
2054 if (element->high && element->low)
2056 /* The last element has been processed, so there is no range
2057 merging to do, we can simply use the high/low values
2058 recorded in the last element. */
2059 low = element->low;
2060 high = element->high;
2062 else
2064 tree tmp_high, tmp_low;
2065 int dummy;
2067 /* The last element has not been processed. Process it now.
2068 record_range should ensure for cond inverted is not set.
2069 This call can only fail if cond is x < min or x > max,
2070 which fold should have optimized into false.
2071 If that doesn't happen, just pretend all values are
2072 in the range. */
2073 if (! extract_range_from_cond (element->cond, &tmp_high,
2074 &tmp_low, &dummy))
2075 gcc_unreachable ();
2076 else
2077 gcc_assert (dummy == 0);
2079 /* If this is the only element, then no merging is necessary,
2080 the high/low values from extract_range_from_cond are all
2081 we need. */
2082 if (limit == 1)
2084 low = tmp_low;
2085 high = tmp_high;
2087 else
2089 /* Get the high/low value from the previous element. */
2090 struct vrp_element *prev
2091 = VEC_index (vrp_element_p, *vrp_records, limit - 2);
2092 low = prev->low;
2093 high = prev->high;
2095 /* Merge in this element's range with the range from the
2096 previous element.
2098 The low value for the merged range is the maximum of
2099 the previous low value and the low value of this record.
2101 Similarly the high value for the merged range is the
2102 minimum of the previous high value and the high value of
2103 this record. */
2104 low = (low && tree_int_cst_compare (low, tmp_low) == 1
2105 ? low : tmp_low);
2106 high = (high && tree_int_cst_compare (high, tmp_high) == -1
2107 ? high : tmp_high);
2110 /* And record the computed range. */
2111 element->low = low;
2112 element->high = high;
2116 /* After we have constrained this variable's potential values,
2117 we try to determine the result of the given conditional.
2119 To simplify later tests, first determine if the current
2120 low value is the same low value as the conditional.
2121 Similarly for the current high value and the high value
2122 for the conditional. */
2123 lowequal = tree_int_cst_equal (low, cond_low);
2124 highequal = tree_int_cst_equal (high, cond_high);
2126 if (lowequal && highequal)
2127 return (cond_inverted ? boolean_false_node : boolean_true_node);
2129 /* To simplify the overlap/subset tests below we may want
2130 to swap the two ranges so that the larger of the two
2131 ranges occurs "first". */
2132 swapped = 0;
2133 if (tree_int_cst_compare (low, cond_low) == 1
2134 || (lowequal
2135 && tree_int_cst_compare (cond_high, high) == 1))
2137 tree temp;
2139 swapped = 1;
2140 temp = low;
2141 low = cond_low;
2142 cond_low = temp;
2143 temp = high;
2144 high = cond_high;
2145 cond_high = temp;
2148 /* Now determine if there is no overlap in the ranges
2149 or if the second range is a subset of the first range. */
2150 no_overlap = tree_int_cst_lt (high, cond_low);
2151 subset = tree_int_cst_compare (cond_high, high) != 1;
2153 /* If there was no overlap in the ranges, then this conditional
2154 always has a false value (unless we had to invert this
2155 conditional, in which case it always has a true value). */
2156 if (no_overlap)
2157 return (cond_inverted ? boolean_true_node : boolean_false_node);
2159 /* If the current range is a subset of the condition's range,
2160 then this conditional always has a true value (unless we
2161 had to invert this conditional, in which case it always
2162 has a true value). */
2163 if (subset && swapped)
2164 return (cond_inverted ? boolean_false_node : boolean_true_node);
2166 /* We were unable to determine the result of the conditional.
2167 However, we may be able to simplify the conditional. First
2168 merge the ranges in the same manner as range merging above. */
2169 low = tree_int_cst_compare (low, cond_low) == 1 ? low : cond_low;
2170 high = tree_int_cst_compare (high, cond_high) == -1 ? high : cond_high;
2172 /* If the range has converged to a single point, then turn this
2173 into an equality comparison. */
2174 if (TREE_CODE (cond) != EQ_EXPR
2175 && TREE_CODE (cond) != NE_EXPR
2176 && tree_int_cst_equal (low, high))
2178 TREE_SET_CODE (cond, EQ_EXPR);
2179 TREE_OPERAND (cond, 1) = high;
2183 return 0;
2186 /* STMT is a SWITCH_EXPR for which we could not trivially determine its
2187 result. This routine attempts to find equivalent forms of the
2188 condition which we may be able to optimize better. */
2190 static tree
2191 simplify_switch_and_lookup_avail_expr (tree stmt, int insert)
2193 tree cond = SWITCH_COND (stmt);
2194 tree def, to, ti;
2196 /* The optimization that we really care about is removing unnecessary
2197 casts. That will let us do much better in propagating the inferred
2198 constant at the switch target. */
2199 if (TREE_CODE (cond) == SSA_NAME)
2201 def = SSA_NAME_DEF_STMT (cond);
2202 if (TREE_CODE (def) == MODIFY_EXPR)
2204 def = TREE_OPERAND (def, 1);
2205 if (TREE_CODE (def) == NOP_EXPR)
2207 int need_precision;
2208 bool fail;
2210 def = TREE_OPERAND (def, 0);
2212 #ifdef ENABLE_CHECKING
2213 /* ??? Why was Jeff testing this? We are gimple... */
2214 gcc_assert (is_gimple_val (def));
2215 #endif
2217 to = TREE_TYPE (cond);
2218 ti = TREE_TYPE (def);
2220 /* If we have an extension that preserves value, then we
2221 can copy the source value into the switch. */
2223 need_precision = TYPE_PRECISION (ti);
2224 fail = false;
2225 if (TYPE_UNSIGNED (to) && !TYPE_UNSIGNED (ti))
2226 fail = true;
2227 else if (!TYPE_UNSIGNED (to) && TYPE_UNSIGNED (ti))
2228 need_precision += 1;
2229 if (TYPE_PRECISION (to) < need_precision)
2230 fail = true;
2232 if (!fail)
2234 SWITCH_COND (stmt) = def;
2235 mark_stmt_modified (stmt);
2237 return lookup_avail_expr (stmt, insert);
2243 return 0;
2247 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
2248 known value for that SSA_NAME (or NULL if no value is known).
2250 NONZERO_VARS is the set SSA_NAMES known to have a nonzero value,
2251 even if we don't know their precise value.
2253 Propagate values from CONST_AND_COPIES and NONZERO_VARS into the PHI
2254 nodes of the successors of BB. */
2256 static void
2257 cprop_into_successor_phis (basic_block bb, bitmap nonzero_vars)
2259 edge e;
2260 edge_iterator ei;
2262 FOR_EACH_EDGE (e, ei, bb->succs)
2264 tree phi;
2265 int indx;
2267 /* If this is an abnormal edge, then we do not want to copy propagate
2268 into the PHI alternative associated with this edge. */
2269 if (e->flags & EDGE_ABNORMAL)
2270 continue;
2272 phi = phi_nodes (e->dest);
2273 if (! phi)
2274 continue;
2276 indx = e->dest_idx;
2277 for ( ; phi; phi = PHI_CHAIN (phi))
2279 tree new;
2280 use_operand_p orig_p;
2281 tree orig;
2283 /* The alternative may be associated with a constant, so verify
2284 it is an SSA_NAME before doing anything with it. */
2285 orig_p = PHI_ARG_DEF_PTR (phi, indx);
2286 orig = USE_FROM_PTR (orig_p);
2287 if (TREE_CODE (orig) != SSA_NAME)
2288 continue;
2290 /* If the alternative is known to have a nonzero value, record
2291 that fact in the PHI node itself for future use. */
2292 if (bitmap_bit_p (nonzero_vars, SSA_NAME_VERSION (orig)))
2293 PHI_ARG_NONZERO (phi, indx) = true;
2295 /* If we have *ORIG_P in our constant/copy table, then replace
2296 ORIG_P with its value in our constant/copy table. */
2297 new = SSA_NAME_VALUE (orig);
2298 if (new
2299 && new != orig
2300 && (TREE_CODE (new) == SSA_NAME
2301 || is_gimple_min_invariant (new))
2302 && may_propagate_copy (orig, new))
2303 propagate_value (orig_p, new);
2308 /* We have finished optimizing BB, record any information implied by
2309 taking a specific outgoing edge from BB. */
2311 static void
2312 record_edge_info (basic_block bb)
2314 block_stmt_iterator bsi = bsi_last (bb);
2315 struct edge_info *edge_info;
2317 if (! bsi_end_p (bsi))
2319 tree stmt = bsi_stmt (bsi);
2321 if (stmt && TREE_CODE (stmt) == SWITCH_EXPR)
2323 tree cond = SWITCH_COND (stmt);
2325 if (TREE_CODE (cond) == SSA_NAME)
2327 tree labels = SWITCH_LABELS (stmt);
2328 int i, n_labels = TREE_VEC_LENGTH (labels);
2329 tree *info = xcalloc (last_basic_block, sizeof (tree));
2330 edge e;
2331 edge_iterator ei;
2333 for (i = 0; i < n_labels; i++)
2335 tree label = TREE_VEC_ELT (labels, i);
2336 basic_block target_bb = label_to_block (CASE_LABEL (label));
2338 if (CASE_HIGH (label)
2339 || !CASE_LOW (label)
2340 || info[target_bb->index])
2341 info[target_bb->index] = error_mark_node;
2342 else
2343 info[target_bb->index] = label;
2346 FOR_EACH_EDGE (e, ei, bb->succs)
2348 basic_block target_bb = e->dest;
2349 tree node = info[target_bb->index];
2351 if (node != NULL && node != error_mark_node)
2353 tree x = fold_convert (TREE_TYPE (cond), CASE_LOW (node));
2354 edge_info = allocate_edge_info (e);
2355 edge_info->lhs = cond;
2356 edge_info->rhs = x;
2359 free (info);
2363 /* A COND_EXPR may create equivalences too. */
2364 if (stmt && TREE_CODE (stmt) == COND_EXPR)
2366 tree cond = COND_EXPR_COND (stmt);
2367 edge true_edge;
2368 edge false_edge;
2370 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2372 /* If the conditional is a single variable 'X', record 'X = 1'
2373 for the true edge and 'X = 0' on the false edge. */
2374 if (SSA_VAR_P (cond))
2376 struct edge_info *edge_info;
2378 edge_info = allocate_edge_info (true_edge);
2379 edge_info->lhs = cond;
2380 edge_info->rhs = constant_boolean_node (1, TREE_TYPE (cond));
2382 edge_info = allocate_edge_info (false_edge);
2383 edge_info->lhs = cond;
2384 edge_info->rhs = constant_boolean_node (0, TREE_TYPE (cond));
2386 /* Equality tests may create one or two equivalences. */
2387 else if (COMPARISON_CLASS_P (cond))
2389 tree op0 = TREE_OPERAND (cond, 0);
2390 tree op1 = TREE_OPERAND (cond, 1);
2392 /* Special case comparing booleans against a constant as we
2393 know the value of OP0 on both arms of the branch. i.e., we
2394 can record an equivalence for OP0 rather than COND. */
2395 if ((TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
2396 && TREE_CODE (op0) == SSA_NAME
2397 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
2398 && is_gimple_min_invariant (op1))
2400 if (TREE_CODE (cond) == EQ_EXPR)
2402 edge_info = allocate_edge_info (true_edge);
2403 edge_info->lhs = op0;
2404 edge_info->rhs = (integer_zerop (op1)
2405 ? boolean_false_node
2406 : boolean_true_node);
2408 edge_info = allocate_edge_info (false_edge);
2409 edge_info->lhs = op0;
2410 edge_info->rhs = (integer_zerop (op1)
2411 ? boolean_true_node
2412 : boolean_false_node);
2414 else
2416 edge_info = allocate_edge_info (true_edge);
2417 edge_info->lhs = op0;
2418 edge_info->rhs = (integer_zerop (op1)
2419 ? boolean_true_node
2420 : boolean_false_node);
2422 edge_info = allocate_edge_info (false_edge);
2423 edge_info->lhs = op0;
2424 edge_info->rhs = (integer_zerop (op1)
2425 ? boolean_false_node
2426 : boolean_true_node);
2430 else if (is_gimple_min_invariant (op0)
2431 && (TREE_CODE (op1) == SSA_NAME
2432 || is_gimple_min_invariant (op1)))
2434 tree inverted = invert_truthvalue (cond);
2435 struct edge_info *edge_info;
2437 edge_info = allocate_edge_info (true_edge);
2438 record_conditions (edge_info, cond, inverted);
2440 if (TREE_CODE (cond) == EQ_EXPR)
2442 edge_info->lhs = op1;
2443 edge_info->rhs = op0;
2446 edge_info = allocate_edge_info (false_edge);
2447 record_conditions (edge_info, inverted, cond);
2449 if (TREE_CODE (cond) == NE_EXPR)
2451 edge_info->lhs = op1;
2452 edge_info->rhs = op0;
2456 else if (TREE_CODE (op0) == SSA_NAME
2457 && (is_gimple_min_invariant (op1)
2458 || TREE_CODE (op1) == SSA_NAME))
2460 tree inverted = invert_truthvalue (cond);
2461 struct edge_info *edge_info;
2463 edge_info = allocate_edge_info (true_edge);
2464 record_conditions (edge_info, cond, inverted);
2466 if (TREE_CODE (cond) == EQ_EXPR)
2468 edge_info->lhs = op0;
2469 edge_info->rhs = op1;
2472 edge_info = allocate_edge_info (false_edge);
2473 record_conditions (edge_info, inverted, cond);
2475 if (TREE_CODE (cond) == NE_EXPR)
2477 edge_info->lhs = op0;
2478 edge_info->rhs = op1;
2483 /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
2488 /* Propagate information from BB to its outgoing edges.
2490 This can include equivalency information implied by control statements
2491 at the end of BB and const/copy propagation into PHIs in BB's
2492 successor blocks. */
2494 static void
2495 propagate_to_outgoing_edges (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
2496 basic_block bb)
2498 record_edge_info (bb);
2499 cprop_into_successor_phis (bb, nonzero_vars);
2502 /* Search for redundant computations in STMT. If any are found, then
2503 replace them with the variable holding the result of the computation.
2505 If safe, record this expression into the available expression hash
2506 table. */
2508 static bool
2509 eliminate_redundant_computations (tree stmt, stmt_ann_t ann)
2511 tree *expr_p, def = NULL_TREE;
2512 bool insert = true;
2513 tree cached_lhs;
2514 bool retval = false;
2515 bool modify_expr_p = false;
2517 if (TREE_CODE (stmt) == MODIFY_EXPR)
2518 def = TREE_OPERAND (stmt, 0);
2520 /* Certain expressions on the RHS can be optimized away, but can not
2521 themselves be entered into the hash tables. */
2522 if (ann->makes_aliased_stores
2523 || ! def
2524 || TREE_CODE (def) != SSA_NAME
2525 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)
2526 || !ZERO_SSA_OPERANDS (stmt, SSA_OP_VMAYDEF)
2527 /* Do not record equivalences for increments of ivs. This would create
2528 overlapping live ranges for a very questionable gain. */
2529 || simple_iv_increment_p (stmt))
2530 insert = false;
2532 /* Check if the expression has been computed before. */
2533 cached_lhs = lookup_avail_expr (stmt, insert);
2535 /* If this is an assignment and the RHS was not in the hash table,
2536 then try to simplify the RHS and lookup the new RHS in the
2537 hash table. */
2538 if (! cached_lhs && TREE_CODE (stmt) == MODIFY_EXPR)
2539 cached_lhs = simplify_rhs_and_lookup_avail_expr (stmt, insert);
2540 /* Similarly if this is a COND_EXPR and we did not find its
2541 expression in the hash table, simplify the condition and
2542 try again. */
2543 else if (! cached_lhs && TREE_CODE (stmt) == COND_EXPR)
2544 cached_lhs = simplify_cond_and_lookup_avail_expr (stmt, ann, insert);
2545 /* Similarly for a SWITCH_EXPR. */
2546 else if (!cached_lhs && TREE_CODE (stmt) == SWITCH_EXPR)
2547 cached_lhs = simplify_switch_and_lookup_avail_expr (stmt, insert);
2549 opt_stats.num_exprs_considered++;
2551 /* Get a pointer to the expression we are trying to optimize. */
2552 if (TREE_CODE (stmt) == COND_EXPR)
2553 expr_p = &COND_EXPR_COND (stmt);
2554 else if (TREE_CODE (stmt) == SWITCH_EXPR)
2555 expr_p = &SWITCH_COND (stmt);
2556 else if (TREE_CODE (stmt) == RETURN_EXPR && TREE_OPERAND (stmt, 0))
2558 expr_p = &TREE_OPERAND (TREE_OPERAND (stmt, 0), 1);
2559 modify_expr_p = true;
2561 else
2563 expr_p = &TREE_OPERAND (stmt, 1);
2564 modify_expr_p = true;
2567 /* It is safe to ignore types here since we have already done
2568 type checking in the hashing and equality routines. In fact
2569 type checking here merely gets in the way of constant
2570 propagation. Also, make sure that it is safe to propagate
2571 CACHED_LHS into *EXPR_P. */
2572 if (cached_lhs
2573 && ((TREE_CODE (cached_lhs) != SSA_NAME
2574 && (modify_expr_p
2575 || tree_ssa_useless_type_conversion_1 (TREE_TYPE (*expr_p),
2576 TREE_TYPE (cached_lhs))))
2577 || may_propagate_copy (*expr_p, cached_lhs)))
2579 if (dump_file && (dump_flags & TDF_DETAILS))
2581 fprintf (dump_file, " Replaced redundant expr '");
2582 print_generic_expr (dump_file, *expr_p, dump_flags);
2583 fprintf (dump_file, "' with '");
2584 print_generic_expr (dump_file, cached_lhs, dump_flags);
2585 fprintf (dump_file, "'\n");
2588 opt_stats.num_re++;
2590 #if defined ENABLE_CHECKING
2591 gcc_assert (TREE_CODE (cached_lhs) == SSA_NAME
2592 || is_gimple_min_invariant (cached_lhs));
2593 #endif
2595 if (TREE_CODE (cached_lhs) == ADDR_EXPR
2596 || (POINTER_TYPE_P (TREE_TYPE (*expr_p))
2597 && is_gimple_min_invariant (cached_lhs)))
2598 retval = true;
2600 if (modify_expr_p
2601 && !tree_ssa_useless_type_conversion_1 (TREE_TYPE (*expr_p),
2602 TREE_TYPE (cached_lhs)))
2603 cached_lhs = fold_convert (TREE_TYPE (*expr_p), cached_lhs);
2605 propagate_tree_value (expr_p, cached_lhs);
2606 mark_stmt_modified (stmt);
2608 return retval;
2611 /* STMT, a MODIFY_EXPR, may create certain equivalences, in either
2612 the available expressions table or the const_and_copies table.
2613 Detect and record those equivalences. */
2615 static void
2616 record_equivalences_from_stmt (tree stmt,
2617 int may_optimize_p,
2618 stmt_ann_t ann)
2620 tree lhs = TREE_OPERAND (stmt, 0);
2621 enum tree_code lhs_code = TREE_CODE (lhs);
2622 int i;
2624 if (lhs_code == SSA_NAME)
2626 tree rhs = TREE_OPERAND (stmt, 1);
2628 /* Strip away any useless type conversions. */
2629 STRIP_USELESS_TYPE_CONVERSION (rhs);
2631 /* If the RHS of the assignment is a constant or another variable that
2632 may be propagated, register it in the CONST_AND_COPIES table. We
2633 do not need to record unwind data for this, since this is a true
2634 assignment and not an equivalence inferred from a comparison. All
2635 uses of this ssa name are dominated by this assignment, so unwinding
2636 just costs time and space. */
2637 if (may_optimize_p
2638 && (TREE_CODE (rhs) == SSA_NAME
2639 || is_gimple_min_invariant (rhs)))
2640 SSA_NAME_VALUE (lhs) = rhs;
2642 if (tree_expr_nonzero_p (rhs))
2643 record_var_is_nonzero (lhs);
2646 /* Look at both sides for pointer dereferences. If we find one, then
2647 the pointer must be nonnull and we can enter that equivalence into
2648 the hash tables. */
2649 if (flag_delete_null_pointer_checks)
2650 for (i = 0; i < 2; i++)
2652 tree t = TREE_OPERAND (stmt, i);
2654 /* Strip away any COMPONENT_REFs. */
2655 while (TREE_CODE (t) == COMPONENT_REF)
2656 t = TREE_OPERAND (t, 0);
2658 /* Now see if this is a pointer dereference. */
2659 if (INDIRECT_REF_P (t))
2661 tree op = TREE_OPERAND (t, 0);
2663 /* If the pointer is a SSA variable, then enter new
2664 equivalences into the hash table. */
2665 while (TREE_CODE (op) == SSA_NAME)
2667 tree def = SSA_NAME_DEF_STMT (op);
2669 record_var_is_nonzero (op);
2671 /* And walk up the USE-DEF chains noting other SSA_NAMEs
2672 which are known to have a nonzero value. */
2673 if (def
2674 && TREE_CODE (def) == MODIFY_EXPR
2675 && TREE_CODE (TREE_OPERAND (def, 1)) == NOP_EXPR)
2676 op = TREE_OPERAND (TREE_OPERAND (def, 1), 0);
2677 else
2678 break;
2683 /* A memory store, even an aliased store, creates a useful
2684 equivalence. By exchanging the LHS and RHS, creating suitable
2685 vops and recording the result in the available expression table,
2686 we may be able to expose more redundant loads. */
2687 if (!ann->has_volatile_ops
2688 && (TREE_CODE (TREE_OPERAND (stmt, 1)) == SSA_NAME
2689 || is_gimple_min_invariant (TREE_OPERAND (stmt, 1)))
2690 && !is_gimple_reg (lhs))
2692 tree rhs = TREE_OPERAND (stmt, 1);
2693 tree new;
2695 /* FIXME: If the LHS of the assignment is a bitfield and the RHS
2696 is a constant, we need to adjust the constant to fit into the
2697 type of the LHS. If the LHS is a bitfield and the RHS is not
2698 a constant, then we can not record any equivalences for this
2699 statement since we would need to represent the widening or
2700 narrowing of RHS. This fixes gcc.c-torture/execute/921016-1.c
2701 and should not be necessary if GCC represented bitfields
2702 properly. */
2703 if (lhs_code == COMPONENT_REF
2704 && DECL_BIT_FIELD (TREE_OPERAND (lhs, 1)))
2706 if (TREE_CONSTANT (rhs))
2707 rhs = widen_bitfield (rhs, TREE_OPERAND (lhs, 1), lhs);
2708 else
2709 rhs = NULL;
2711 /* If the value overflowed, then we can not use this equivalence. */
2712 if (rhs && ! is_gimple_min_invariant (rhs))
2713 rhs = NULL;
2716 if (rhs)
2718 /* Build a new statement with the RHS and LHS exchanged. */
2719 new = build (MODIFY_EXPR, TREE_TYPE (stmt), rhs, lhs);
2721 create_ssa_artficial_load_stmt (new, stmt);
2723 /* Finally enter the statement into the available expression
2724 table. */
2725 lookup_avail_expr (new, true);
2730 /* Replace *OP_P in STMT with any known equivalent value for *OP_P from
2731 CONST_AND_COPIES. */
2733 static bool
2734 cprop_operand (tree stmt, use_operand_p op_p)
2736 bool may_have_exposed_new_symbols = false;
2737 tree val;
2738 tree op = USE_FROM_PTR (op_p);
2740 /* If the operand has a known constant value or it is known to be a
2741 copy of some other variable, use the value or copy stored in
2742 CONST_AND_COPIES. */
2743 val = SSA_NAME_VALUE (op);
2744 if (val && val != op && TREE_CODE (val) != VALUE_HANDLE)
2746 tree op_type, val_type;
2748 /* Do not change the base variable in the virtual operand
2749 tables. That would make it impossible to reconstruct
2750 the renamed virtual operand if we later modify this
2751 statement. Also only allow the new value to be an SSA_NAME
2752 for propagation into virtual operands. */
2753 if (!is_gimple_reg (op)
2754 && (TREE_CODE (val) != SSA_NAME
2755 || is_gimple_reg (val)
2756 || get_virtual_var (val) != get_virtual_var (op)))
2757 return false;
2759 /* Do not replace hard register operands in asm statements. */
2760 if (TREE_CODE (stmt) == ASM_EXPR
2761 && !may_propagate_copy_into_asm (op))
2762 return false;
2764 /* Get the toplevel type of each operand. */
2765 op_type = TREE_TYPE (op);
2766 val_type = TREE_TYPE (val);
2768 /* While both types are pointers, get the type of the object
2769 pointed to. */
2770 while (POINTER_TYPE_P (op_type) && POINTER_TYPE_P (val_type))
2772 op_type = TREE_TYPE (op_type);
2773 val_type = TREE_TYPE (val_type);
2776 /* Make sure underlying types match before propagating a constant by
2777 converting the constant to the proper type. Note that convert may
2778 return a non-gimple expression, in which case we ignore this
2779 propagation opportunity. */
2780 if (TREE_CODE (val) != SSA_NAME)
2782 if (!lang_hooks.types_compatible_p (op_type, val_type))
2784 val = fold_convert (TREE_TYPE (op), val);
2785 if (!is_gimple_min_invariant (val))
2786 return false;
2790 /* Certain operands are not allowed to be copy propagated due
2791 to their interaction with exception handling and some GCC
2792 extensions. */
2793 else if (!may_propagate_copy (op, val))
2794 return false;
2796 /* Do not propagate copies if the propagated value is at a deeper loop
2797 depth than the propagatee. Otherwise, this may move loop variant
2798 variables outside of their loops and prevent coalescing
2799 opportunities. If the value was loop invariant, it will be hoisted
2800 by LICM and exposed for copy propagation. */
2801 if (loop_depth_of_name (val) > loop_depth_of_name (op))
2802 return false;
2804 /* Dump details. */
2805 if (dump_file && (dump_flags & TDF_DETAILS))
2807 fprintf (dump_file, " Replaced '");
2808 print_generic_expr (dump_file, op, dump_flags);
2809 fprintf (dump_file, "' with %s '",
2810 (TREE_CODE (val) != SSA_NAME ? "constant" : "variable"));
2811 print_generic_expr (dump_file, val, dump_flags);
2812 fprintf (dump_file, "'\n");
2815 /* If VAL is an ADDR_EXPR or a constant of pointer type, note
2816 that we may have exposed a new symbol for SSA renaming. */
2817 if (TREE_CODE (val) == ADDR_EXPR
2818 || (POINTER_TYPE_P (TREE_TYPE (op))
2819 && is_gimple_min_invariant (val)))
2820 may_have_exposed_new_symbols = true;
2822 if (TREE_CODE (val) != SSA_NAME)
2823 opt_stats.num_const_prop++;
2824 else
2825 opt_stats.num_copy_prop++;
2827 propagate_value (op_p, val);
2829 /* And note that we modified this statement. This is now
2830 safe, even if we changed virtual operands since we will
2831 rescan the statement and rewrite its operands again. */
2832 mark_stmt_modified (stmt);
2834 return may_have_exposed_new_symbols;
2837 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
2838 known value for that SSA_NAME (or NULL if no value is known).
2840 Propagate values from CONST_AND_COPIES into the uses, vuses and
2841 v_may_def_ops of STMT. */
2843 static bool
2844 cprop_into_stmt (tree stmt)
2846 bool may_have_exposed_new_symbols = false;
2847 use_operand_p op_p;
2848 ssa_op_iter iter;
2850 FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_ALL_USES)
2852 if (TREE_CODE (USE_FROM_PTR (op_p)) == SSA_NAME)
2853 may_have_exposed_new_symbols |= cprop_operand (stmt, op_p);
2856 return may_have_exposed_new_symbols;
2860 /* Optimize the statement pointed to by iterator SI.
2862 We try to perform some simplistic global redundancy elimination and
2863 constant propagation:
2865 1- To detect global redundancy, we keep track of expressions that have
2866 been computed in this block and its dominators. If we find that the
2867 same expression is computed more than once, we eliminate repeated
2868 computations by using the target of the first one.
2870 2- Constant values and copy assignments. This is used to do very
2871 simplistic constant and copy propagation. When a constant or copy
2872 assignment is found, we map the value on the RHS of the assignment to
2873 the variable in the LHS in the CONST_AND_COPIES table. */
2875 static void
2876 optimize_stmt (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
2877 basic_block bb, block_stmt_iterator si)
2879 stmt_ann_t ann;
2880 tree stmt, old_stmt;
2881 bool may_optimize_p;
2882 bool may_have_exposed_new_symbols = false;
2884 old_stmt = stmt = bsi_stmt (si);
2886 update_stmt_if_modified (stmt);
2887 ann = stmt_ann (stmt);
2888 opt_stats.num_stmts++;
2889 may_have_exposed_new_symbols = false;
2891 if (dump_file && (dump_flags & TDF_DETAILS))
2893 fprintf (dump_file, "Optimizing statement ");
2894 print_generic_stmt (dump_file, stmt, TDF_SLIM);
2897 /* Const/copy propagate into USES, VUSES and the RHS of V_MAY_DEFs. */
2898 may_have_exposed_new_symbols = cprop_into_stmt (stmt);
2900 /* If the statement has been modified with constant replacements,
2901 fold its RHS before checking for redundant computations. */
2902 if (ann->modified)
2904 tree rhs;
2906 /* Try to fold the statement making sure that STMT is kept
2907 up to date. */
2908 if (fold_stmt (bsi_stmt_ptr (si)))
2910 stmt = bsi_stmt (si);
2911 ann = stmt_ann (stmt);
2913 if (dump_file && (dump_flags & TDF_DETAILS))
2915 fprintf (dump_file, " Folded to: ");
2916 print_generic_stmt (dump_file, stmt, TDF_SLIM);
2920 rhs = get_rhs (stmt);
2921 if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
2922 recompute_tree_invarant_for_addr_expr (rhs);
2924 /* Constant/copy propagation above may change the set of
2925 virtual operands associated with this statement. Folding
2926 may remove the need for some virtual operands.
2928 Indicate we will need to rescan and rewrite the statement. */
2929 may_have_exposed_new_symbols = true;
2932 /* Check for redundant computations. Do this optimization only
2933 for assignments that have no volatile ops and conditionals. */
2934 may_optimize_p = (!ann->has_volatile_ops
2935 && ((TREE_CODE (stmt) == RETURN_EXPR
2936 && TREE_OPERAND (stmt, 0)
2937 && TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR
2938 && ! (TREE_SIDE_EFFECTS
2939 (TREE_OPERAND (TREE_OPERAND (stmt, 0), 1))))
2940 || (TREE_CODE (stmt) == MODIFY_EXPR
2941 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (stmt, 1)))
2942 || TREE_CODE (stmt) == COND_EXPR
2943 || TREE_CODE (stmt) == SWITCH_EXPR));
2945 if (may_optimize_p)
2946 may_have_exposed_new_symbols
2947 |= eliminate_redundant_computations (stmt, ann);
2949 /* Record any additional equivalences created by this statement. */
2950 if (TREE_CODE (stmt) == MODIFY_EXPR)
2951 record_equivalences_from_stmt (stmt,
2952 may_optimize_p,
2953 ann);
2955 /* If STMT is a COND_EXPR and it was modified, then we may know
2956 where it goes. If that is the case, then mark the CFG as altered.
2958 This will cause us to later call remove_unreachable_blocks and
2959 cleanup_tree_cfg when it is safe to do so. It is not safe to
2960 clean things up here since removal of edges and such can trigger
2961 the removal of PHI nodes, which in turn can release SSA_NAMEs to
2962 the manager.
2964 That's all fine and good, except that once SSA_NAMEs are released
2965 to the manager, we must not call create_ssa_name until all references
2966 to released SSA_NAMEs have been eliminated.
2968 All references to the deleted SSA_NAMEs can not be eliminated until
2969 we remove unreachable blocks.
2971 We can not remove unreachable blocks until after we have completed
2972 any queued jump threading.
2974 We can not complete any queued jump threads until we have taken
2975 appropriate variables out of SSA form. Taking variables out of
2976 SSA form can call create_ssa_name and thus we lose.
2978 Ultimately I suspect we're going to need to change the interface
2979 into the SSA_NAME manager. */
2981 if (ann->modified)
2983 tree val = NULL;
2985 if (TREE_CODE (stmt) == COND_EXPR)
2986 val = COND_EXPR_COND (stmt);
2987 else if (TREE_CODE (stmt) == SWITCH_EXPR)
2988 val = SWITCH_COND (stmt);
2990 if (val && TREE_CODE (val) == INTEGER_CST && find_taken_edge (bb, val))
2991 cfg_altered = true;
2993 /* If we simplified a statement in such a way as to be shown that it
2994 cannot trap, update the eh information and the cfg to match. */
2995 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
2997 bitmap_set_bit (need_eh_cleanup, bb->index);
2998 if (dump_file && (dump_flags & TDF_DETAILS))
2999 fprintf (dump_file, " Flagged to clear EH edges.\n");
3003 if (may_have_exposed_new_symbols)
3004 VEC_safe_push (tree, heap, stmts_to_rescan, bsi_stmt (si));
3007 /* Replace the RHS of STMT with NEW_RHS. If RHS can be found in the
3008 available expression hashtable, then return the LHS from the hash
3009 table.
3011 If INSERT is true, then we also update the available expression
3012 hash table to account for the changes made to STMT. */
3014 static tree
3015 update_rhs_and_lookup_avail_expr (tree stmt, tree new_rhs, bool insert)
3017 tree cached_lhs = NULL;
3019 /* Remove the old entry from the hash table. */
3020 if (insert)
3022 struct expr_hash_elt element;
3024 initialize_hash_element (stmt, NULL, &element);
3025 htab_remove_elt_with_hash (avail_exprs, &element, element.hash);
3028 /* Now update the RHS of the assignment. */
3029 TREE_OPERAND (stmt, 1) = new_rhs;
3031 /* Now lookup the updated statement in the hash table. */
3032 cached_lhs = lookup_avail_expr (stmt, insert);
3034 /* We have now called lookup_avail_expr twice with two different
3035 versions of this same statement, once in optimize_stmt, once here.
3037 We know the call in optimize_stmt did not find an existing entry
3038 in the hash table, so a new entry was created. At the same time
3039 this statement was pushed onto the AVAIL_EXPRS_STACK vector.
3041 If this call failed to find an existing entry on the hash table,
3042 then the new version of this statement was entered into the
3043 hash table. And this statement was pushed onto BLOCK_AVAIL_EXPR
3044 for the second time. So there are two copies on BLOCK_AVAIL_EXPRs
3046 If this call succeeded, we still have one copy of this statement
3047 on the BLOCK_AVAIL_EXPRs vector.
3049 For both cases, we need to pop the most recent entry off the
3050 BLOCK_AVAIL_EXPRs vector. For the case where we never found this
3051 statement in the hash tables, that will leave precisely one
3052 copy of this statement on BLOCK_AVAIL_EXPRs. For the case where
3053 we found a copy of this statement in the second hash table lookup
3054 we want _no_ copies of this statement in BLOCK_AVAIL_EXPRs. */
3055 if (insert)
3056 VEC_pop (tree, avail_exprs_stack);
3058 /* And make sure we record the fact that we modified this
3059 statement. */
3060 mark_stmt_modified (stmt);
3062 return cached_lhs;
3065 /* Search for an existing instance of STMT in the AVAIL_EXPRS table. If
3066 found, return its LHS. Otherwise insert STMT in the table and return
3067 NULL_TREE.
3069 Also, when an expression is first inserted in the AVAIL_EXPRS table, it
3070 is also added to the stack pointed to by BLOCK_AVAIL_EXPRS_P, so that they
3071 can be removed when we finish processing this block and its children.
3073 NOTE: This function assumes that STMT is a MODIFY_EXPR node that
3074 contains no CALL_EXPR on its RHS and makes no volatile nor
3075 aliased references. */
3077 static tree
3078 lookup_avail_expr (tree stmt, bool insert)
3080 void **slot;
3081 tree lhs;
3082 tree temp;
3083 struct expr_hash_elt *element = xmalloc (sizeof (struct expr_hash_elt));
3085 lhs = TREE_CODE (stmt) == MODIFY_EXPR ? TREE_OPERAND (stmt, 0) : NULL;
3087 initialize_hash_element (stmt, lhs, element);
3089 /* Don't bother remembering constant assignments and copy operations.
3090 Constants and copy operations are handled by the constant/copy propagator
3091 in optimize_stmt. */
3092 if (TREE_CODE (element->rhs) == SSA_NAME
3093 || is_gimple_min_invariant (element->rhs))
3095 free (element);
3096 return NULL_TREE;
3099 /* If this is an equality test against zero, see if we have recorded a
3100 nonzero value for the variable in question. */
3101 if ((TREE_CODE (element->rhs) == EQ_EXPR
3102 || TREE_CODE (element->rhs) == NE_EXPR)
3103 && TREE_CODE (TREE_OPERAND (element->rhs, 0)) == SSA_NAME
3104 && integer_zerop (TREE_OPERAND (element->rhs, 1)))
3106 int indx = SSA_NAME_VERSION (TREE_OPERAND (element->rhs, 0));
3108 if (bitmap_bit_p (nonzero_vars, indx))
3110 tree t = element->rhs;
3111 free (element);
3112 return constant_boolean_node (TREE_CODE (t) != EQ_EXPR,
3113 TREE_TYPE (t));
3117 /* Finally try to find the expression in the main expression hash table. */
3118 slot = htab_find_slot_with_hash (avail_exprs, element, element->hash,
3119 (insert ? INSERT : NO_INSERT));
3120 if (slot == NULL)
3122 free (element);
3123 return NULL_TREE;
3126 if (*slot == NULL)
3128 *slot = (void *) element;
3129 VEC_safe_push (tree, heap, avail_exprs_stack,
3130 stmt ? stmt : element->rhs);
3131 return NULL_TREE;
3134 /* Extract the LHS of the assignment so that it can be used as the current
3135 definition of another variable. */
3136 lhs = ((struct expr_hash_elt *)*slot)->lhs;
3138 /* See if the LHS appears in the CONST_AND_COPIES table. If it does, then
3139 use the value from the const_and_copies table. */
3140 if (TREE_CODE (lhs) == SSA_NAME)
3142 temp = SSA_NAME_VALUE (lhs);
3143 if (temp && TREE_CODE (temp) != VALUE_HANDLE)
3144 lhs = temp;
3147 free (element);
3148 return lhs;
3151 /* Given a condition COND, record into HI_P, LO_P and INVERTED_P the
3152 range of values that result in the conditional having a true value.
3154 Return true if we are successful in extracting a range from COND and
3155 false if we are unsuccessful. */
3157 static bool
3158 extract_range_from_cond (tree cond, tree *hi_p, tree *lo_p, int *inverted_p)
3160 tree op1 = TREE_OPERAND (cond, 1);
3161 tree high, low, type;
3162 int inverted;
3164 type = TREE_TYPE (op1);
3166 /* Experiments have shown that it's rarely, if ever useful to
3167 record ranges for enumerations. Presumably this is due to
3168 the fact that they're rarely used directly. They are typically
3169 cast into an integer type and used that way. */
3170 if (TREE_CODE (type) != INTEGER_TYPE
3171 /* We don't know how to deal with types with variable bounds. */
3172 || TREE_CODE (TYPE_MIN_VALUE (type)) != INTEGER_CST
3173 || TREE_CODE (TYPE_MAX_VALUE (type)) != INTEGER_CST)
3174 return 0;
3176 switch (TREE_CODE (cond))
3178 case EQ_EXPR:
3179 high = low = op1;
3180 inverted = 0;
3181 break;
3183 case NE_EXPR:
3184 high = low = op1;
3185 inverted = 1;
3186 break;
3188 case GE_EXPR:
3189 low = op1;
3190 high = TYPE_MAX_VALUE (type);
3191 inverted = 0;
3192 break;
3194 case GT_EXPR:
3195 high = TYPE_MAX_VALUE (type);
3196 if (!tree_int_cst_lt (op1, high))
3197 return 0;
3198 low = int_const_binop (PLUS_EXPR, op1, integer_one_node, 1);
3199 inverted = 0;
3200 break;
3202 case LE_EXPR:
3203 high = op1;
3204 low = TYPE_MIN_VALUE (type);
3205 inverted = 0;
3206 break;
3208 case LT_EXPR:
3209 low = TYPE_MIN_VALUE (type);
3210 if (!tree_int_cst_lt (low, op1))
3211 return 0;
3212 high = int_const_binop (MINUS_EXPR, op1, integer_one_node, 1);
3213 inverted = 0;
3214 break;
3216 default:
3217 return 0;
3220 *hi_p = high;
3221 *lo_p = low;
3222 *inverted_p = inverted;
3223 return 1;
3226 /* Record a range created by COND for basic block BB. */
3228 static void
3229 record_range (tree cond, basic_block bb)
3231 enum tree_code code = TREE_CODE (cond);
3233 /* We explicitly ignore NE_EXPRs and all the unordered comparisons.
3234 They rarely allow for meaningful range optimizations and significantly
3235 complicate the implementation. */
3236 if ((code == LT_EXPR || code == LE_EXPR || code == GT_EXPR
3237 || code == GE_EXPR || code == EQ_EXPR)
3238 && TREE_CODE (TREE_TYPE (TREE_OPERAND (cond, 1))) == INTEGER_TYPE)
3240 struct vrp_hash_elt *vrp_hash_elt;
3241 struct vrp_element *element;
3242 VEC(vrp_element_p,heap) **vrp_records_p;
3243 void **slot;
3246 vrp_hash_elt = xmalloc (sizeof (struct vrp_hash_elt));
3247 vrp_hash_elt->var = TREE_OPERAND (cond, 0);
3248 vrp_hash_elt->records = NULL;
3249 slot = htab_find_slot (vrp_data, vrp_hash_elt, INSERT);
3251 if (*slot == NULL)
3252 *slot = (void *) vrp_hash_elt;
3253 else
3254 vrp_free (vrp_hash_elt);
3256 vrp_hash_elt = (struct vrp_hash_elt *) *slot;
3257 vrp_records_p = &vrp_hash_elt->records;
3259 element = ggc_alloc (sizeof (struct vrp_element));
3260 element->low = NULL;
3261 element->high = NULL;
3262 element->cond = cond;
3263 element->bb = bb;
3265 VEC_safe_push (vrp_element_p, heap, *vrp_records_p, element);
3266 VEC_safe_push (tree, heap, vrp_variables_stack, TREE_OPERAND (cond, 0));
3270 /* Hashing and equality functions for VRP_DATA.
3272 Since this hash table is addressed by SSA_NAMEs, we can hash on
3273 their version number and equality can be determined with a
3274 pointer comparison. */
3276 static hashval_t
3277 vrp_hash (const void *p)
3279 tree var = ((struct vrp_hash_elt *)p)->var;
3281 return SSA_NAME_VERSION (var);
3284 static int
3285 vrp_eq (const void *p1, const void *p2)
3287 tree var1 = ((struct vrp_hash_elt *)p1)->var;
3288 tree var2 = ((struct vrp_hash_elt *)p2)->var;
3290 return var1 == var2;
3293 /* Hashing and equality functions for AVAIL_EXPRS. The table stores
3294 MODIFY_EXPR statements. We compute a value number for expressions using
3295 the code of the expression and the SSA numbers of its operands. */
3297 static hashval_t
3298 avail_expr_hash (const void *p)
3300 tree stmt = ((struct expr_hash_elt *)p)->stmt;
3301 tree rhs = ((struct expr_hash_elt *)p)->rhs;
3302 tree vuse;
3303 ssa_op_iter iter;
3304 hashval_t val = 0;
3306 /* iterative_hash_expr knows how to deal with any expression and
3307 deals with commutative operators as well, so just use it instead
3308 of duplicating such complexities here. */
3309 val = iterative_hash_expr (rhs, val);
3311 /* If the hash table entry is not associated with a statement, then we
3312 can just hash the expression and not worry about virtual operands
3313 and such. */
3314 if (!stmt || !stmt_ann (stmt))
3315 return val;
3317 /* Add the SSA version numbers of every vuse operand. This is important
3318 because compound variables like arrays are not renamed in the
3319 operands. Rather, the rename is done on the virtual variable
3320 representing all the elements of the array. */
3321 FOR_EACH_SSA_TREE_OPERAND (vuse, stmt, iter, SSA_OP_VUSE)
3322 val = iterative_hash_expr (vuse, val);
3324 return val;
3327 static hashval_t
3328 real_avail_expr_hash (const void *p)
3330 return ((const struct expr_hash_elt *)p)->hash;
3333 static int
3334 avail_expr_eq (const void *p1, const void *p2)
3336 tree stmt1 = ((struct expr_hash_elt *)p1)->stmt;
3337 tree rhs1 = ((struct expr_hash_elt *)p1)->rhs;
3338 tree stmt2 = ((struct expr_hash_elt *)p2)->stmt;
3339 tree rhs2 = ((struct expr_hash_elt *)p2)->rhs;
3341 /* If they are the same physical expression, return true. */
3342 if (rhs1 == rhs2 && stmt1 == stmt2)
3343 return true;
3345 /* If their codes are not equal, then quit now. */
3346 if (TREE_CODE (rhs1) != TREE_CODE (rhs2))
3347 return false;
3349 /* In case of a collision, both RHS have to be identical and have the
3350 same VUSE operands. */
3351 if ((TREE_TYPE (rhs1) == TREE_TYPE (rhs2)
3352 || lang_hooks.types_compatible_p (TREE_TYPE (rhs1), TREE_TYPE (rhs2)))
3353 && operand_equal_p (rhs1, rhs2, OEP_PURE_SAME))
3355 bool ret = compare_ssa_operands_equal (stmt1, stmt2, SSA_OP_VUSE);
3356 gcc_assert (!ret || ((struct expr_hash_elt *)p1)->hash
3357 == ((struct expr_hash_elt *)p2)->hash);
3358 return ret;
3361 return false;