tree-cfg.c (verify_expr): Replace TREE_OPERAND with COND_EXPR_COND.
[gcc.git] / gcc / tree-ssa-dom.c
1 /* SSA Dominator optimizations for trees
2 Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
4
5 This file is part of GCC.
6
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.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "rtl.h"
29 #include "tm_p.h"
30 #include "ggc.h"
31 #include "basic-block.h"
32 #include "output.h"
33 #include "errors.h"
34 #include "expr.h"
35 #include "function.h"
36 #include "diagnostic.h"
37 #include "timevar.h"
38 #include "tree-dump.h"
39 #include "tree-flow.h"
40 #include "domwalk.h"
41 #include "real.h"
42 #include "tree-pass.h"
43 #include "tree-ssa-propagate.h"
44 #include "langhooks.h"
45
46 /* This file implements optimizations on the dominator tree. */
47
48
49 /* Structure for recording edge equivalences as well as any pending
50 edge redirections during the dominator optimizer.
51
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.
55
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. */
62
63 struct edge_info
64 {
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;
69
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;
76
77 /* If we can thread this edge this field records the new target. */
78 edge redirection_target;
79 };
80
81
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;
90
91 /* Stack of available expressions in AVAIL_EXPRs. Each block pushes any
92 expressions it enters into the hash table along with a marker entry
93 (null). When we finish processing the block, we pop off entries and
94 remove the expressions from the global hash table until we hit the
95 marker. */
96 static varray_type avail_exprs_stack;
97
98 /* Stack of trees used to restore the global currdefs to its original
99 state after completing optimization of a block and its dominator children.
100
101 An SSA_NAME indicates that the current definition of the underlying
102 variable should be set to the given SSA_NAME.
103
104 A _DECL node indicates that the underlying variable has no current
105 definition.
106
107 A NULL node is used to mark the last node associated with the
108 current block. */
109 varray_type block_defs_stack;
110
111 /* Stack of statements we need to rescan during finalization for newly
112 exposed variables.
113
114 Statement rescanning must occur after the current block's available
115 expressions are removed from AVAIL_EXPRS. Else we may change the
116 hash code for an expression and be unable to find/remove it from
117 AVAIL_EXPRS. */
118 varray_type stmts_to_rescan;
119
120 /* Structure for entries in the expression hash table.
121
122 This requires more memory for the hash table entries, but allows us
123 to avoid creating silly tree nodes and annotations for conditionals,
124 eliminates 2 global hash tables and two block local varrays.
125
126 It also allows us to reduce the number of hash table lookups we
127 have to perform in lookup_avail_expr and finally it allows us to
128 significantly reduce the number of calls into the hashing routine
129 itself. */
130
131 struct expr_hash_elt
132 {
133 /* The value (lhs) of this expression. */
134 tree lhs;
135
136 /* The expression (rhs) we want to record. */
137 tree rhs;
138
139 /* The annotation if this element corresponds to a statement. */
140 stmt_ann_t ann;
141
142 /* The hash value for RHS/ann. */
143 hashval_t hash;
144 };
145
146 /* Stack of dest,src pairs that need to be restored during finalization.
147
148 A NULL entry is used to mark the end of pairs which need to be
149 restored during finalization of this block. */
150 static varray_type const_and_copies_stack;
151
152 /* Bitmap of SSA_NAMEs known to have a nonzero value, even if we do not
153 know their exact value. */
154 static bitmap nonzero_vars;
155
156 /* Stack of SSA_NAMEs which need their NONZERO_VARS property cleared
157 when the current block is finalized.
158
159 A NULL entry is used to mark the end of names needing their
160 entry in NONZERO_VARS cleared during finalization of this block. */
161 static varray_type nonzero_vars_stack;
162
163 /* Track whether or not we have changed the control flow graph. */
164 static bool cfg_altered;
165
166 /* Bitmap of blocks that have had EH statements cleaned. We should
167 remove their dead edges eventually. */
168 static bitmap need_eh_cleanup;
169
170 /* Statistics for dominator optimizations. */
171 struct opt_stats_d
172 {
173 long num_stmts;
174 long num_exprs_considered;
175 long num_re;
176 };
177
178 static struct opt_stats_d opt_stats;
179
180 /* Value range propagation record. Each time we encounter a conditional
181 of the form SSA_NAME COND CONST we create a new vrp_element to record
182 how the condition affects the possible values SSA_NAME may have.
183
184 Each record contains the condition tested (COND), and the the range of
185 values the variable may legitimately have if COND is true. Note the
186 range of values may be a smaller range than COND specifies if we have
187 recorded other ranges for this variable. Each record also contains the
188 block in which the range was recorded for invalidation purposes.
189
190 Note that the current known range is computed lazily. This allows us
191 to avoid the overhead of computing ranges which are never queried.
192
193 When we encounter a conditional, we look for records which constrain
194 the SSA_NAME used in the condition. In some cases those records allow
195 us to determine the condition's result at compile time. In other cases
196 they may allow us to simplify the condition.
197
198 We also use value ranges to do things like transform signed div/mod
199 operations into unsigned div/mod or to simplify ABS_EXPRs.
200
201 Simple experiments have shown these optimizations to not be all that
202 useful on switch statements (much to my surprise). So switch statement
203 optimizations are not performed.
204
205 Note carefully we do not propagate information through each statement
206 in the block. i.e., if we know variable X has a value defined of
207 [0, 25] and we encounter Y = X + 1, we do not track a value range
208 for Y (which would be [1, 26] if we cared). Similarly we do not
209 constrain values as we encounter narrowing typecasts, etc. */
210
211 struct vrp_element
212 {
213 /* The highest and lowest values the variable in COND may contain when
214 COND is true. Note this may not necessarily be the same values
215 tested by COND if the same variable was used in earlier conditionals.
216
217 Note this is computed lazily and thus can be NULL indicating that
218 the values have not been computed yet. */
219 tree low;
220 tree high;
221
222 /* The actual conditional we recorded. This is needed since we compute
223 ranges lazily. */
224 tree cond;
225
226 /* The basic block where this record was created. We use this to determine
227 when to remove records. */
228 basic_block bb;
229 };
230
231 /* A hash table holding value range records (VRP_ELEMENTs) for a given
232 SSA_NAME. We used to use a varray indexed by SSA_NAME_VERSION, but
233 that gets awful wasteful, particularly since the density objects
234 with useful information is very low. */
235 static htab_t vrp_data;
236
237 /* An entry in the VRP_DATA hash table. We record the variable and a
238 varray of VRP_ELEMENT records associated with that variable. */
239
240 struct vrp_hash_elt
241 {
242 tree var;
243 varray_type records;
244 };
245
246 /* Array of variables which have their values constrained by operations
247 in this basic block. We use this during finalization to know
248 which variables need their VRP data updated. */
249
250 /* Stack of SSA_NAMEs which had their values constrainted by operations
251 in this basic block. During finalization of this block we use this
252 list to determine which variables need their VRP data updated.
253
254 A NULL entry marks the end of the SSA_NAMEs associated with this block. */
255 static varray_type vrp_variables_stack;
256
257 struct eq_expr_value
258 {
259 tree src;
260 tree dst;
261 };
262
263 /* Local functions. */
264 static void optimize_stmt (struct dom_walk_data *,
265 basic_block bb,
266 block_stmt_iterator);
267 static tree lookup_avail_expr (tree, bool);
268 static hashval_t vrp_hash (const void *);
269 static int vrp_eq (const void *, const void *);
270 static hashval_t avail_expr_hash (const void *);
271 static hashval_t real_avail_expr_hash (const void *);
272 static int avail_expr_eq (const void *, const void *);
273 static void htab_statistics (FILE *, htab_t);
274 static void record_cond (tree, tree);
275 static void record_const_or_copy (tree, tree);
276 static void record_equality (tree, tree);
277 static tree update_rhs_and_lookup_avail_expr (tree, tree, bool);
278 static tree simplify_rhs_and_lookup_avail_expr (struct dom_walk_data *,
279 tree, int);
280 static tree simplify_cond_and_lookup_avail_expr (tree, stmt_ann_t, int);
281 static tree simplify_switch_and_lookup_avail_expr (tree, int);
282 static tree find_equivalent_equality_comparison (tree);
283 static void record_range (tree, basic_block);
284 static bool extract_range_from_cond (tree, tree *, tree *, int *);
285 static void record_equivalences_from_phis (basic_block);
286 static void record_equivalences_from_incoming_edge (basic_block);
287 static bool eliminate_redundant_computations (struct dom_walk_data *,
288 tree, stmt_ann_t);
289 static void record_equivalences_from_stmt (tree, int, stmt_ann_t);
290 static void thread_across_edge (struct dom_walk_data *, edge);
291 static void dom_opt_finalize_block (struct dom_walk_data *, basic_block);
292 static void dom_opt_initialize_block (struct dom_walk_data *, basic_block);
293 static void propagate_to_outgoing_edges (struct dom_walk_data *, basic_block);
294 static void remove_local_expressions_from_table (void);
295 static void restore_vars_to_original_value (void);
296 static void restore_currdefs_to_original_value (void);
297 static void register_definitions_for_stmt (tree);
298 static edge single_incoming_edge_ignoring_loop_edges (basic_block);
299 static void restore_nonzero_vars_to_original_value (void);
300 static inline bool unsafe_associative_fp_binop (tree);
301
302 /* Local version of fold that doesn't introduce cruft. */
303
304 static tree
305 local_fold (tree t)
306 {
307 t = fold (t);
308
309 /* Strip away useless type conversions. Both the NON_LVALUE_EXPR that
310 may have been added by fold, and "useless" type conversions that might
311 now be apparent due to propagation. */
312 STRIP_USELESS_TYPE_CONVERSION (t);
313
314 return t;
315 }
316
317 /* Allocate an EDGE_INFO for edge E and attach it to E.
318 Return the new EDGE_INFO structure. */
319
320 static struct edge_info *
321 allocate_edge_info (edge e)
322 {
323 struct edge_info *edge_info;
324
325 edge_info = xcalloc (1, sizeof (struct edge_info));
326
327 e->aux = edge_info;
328 return edge_info;
329 }
330
331 /* Free all EDGE_INFO structures associated with edges in the CFG.
332 If a particular edge can be threaded, copy the redirection
333 target from the EDGE_INFO structure into the edge's AUX field
334 as required by code to update the CFG and SSA graph for
335 jump threading. */
336
337 static void
338 free_all_edge_infos (void)
339 {
340 basic_block bb;
341 edge_iterator ei;
342 edge e;
343
344 FOR_EACH_BB (bb)
345 {
346 FOR_EACH_EDGE (e, ei, bb->preds)
347 {
348 struct edge_info *edge_info = e->aux;
349
350 if (edge_info)
351 {
352 e->aux = edge_info->redirection_target;
353 if (edge_info->cond_equivalences)
354 free (edge_info->cond_equivalences);
355 free (edge_info);
356 }
357 }
358 }
359 }
360
361 /* Jump threading, redundancy elimination and const/copy propagation.
362
363 This pass may expose new symbols that need to be renamed into SSA. For
364 every new symbol exposed, its corresponding bit will be set in
365 VARS_TO_RENAME. */
366
367 static void
368 tree_ssa_dominator_optimize (void)
369 {
370 struct dom_walk_data walk_data;
371 unsigned int i;
372
373 memset (&opt_stats, 0, sizeof (opt_stats));
374
375 for (i = 0; i < num_referenced_vars; i++)
376 var_ann (referenced_var (i))->current_def = NULL;
377
378 /* Mark loop edges so we avoid threading across loop boundaries.
379 This may result in transforming natural loop into irreducible
380 region. */
381 mark_dfs_back_edges ();
382
383 /* Create our hash tables. */
384 avail_exprs = htab_create (1024, real_avail_expr_hash, avail_expr_eq, free);
385 vrp_data = htab_create (ceil_log2 (num_ssa_names), vrp_hash, vrp_eq, free);
386 VARRAY_TREE_INIT (avail_exprs_stack, 20, "Available expression stack");
387 VARRAY_TREE_INIT (block_defs_stack, 20, "Block DEFS stack");
388 VARRAY_TREE_INIT (const_and_copies_stack, 20, "Block const_and_copies stack");
389 VARRAY_TREE_INIT (nonzero_vars_stack, 20, "Block nonzero_vars stack");
390 VARRAY_TREE_INIT (vrp_variables_stack, 20, "Block vrp_variables stack");
391 VARRAY_TREE_INIT (stmts_to_rescan, 20, "Statements to rescan");
392 nonzero_vars = BITMAP_XMALLOC ();
393 need_eh_cleanup = BITMAP_XMALLOC ();
394
395 /* Setup callbacks for the generic dominator tree walker. */
396 walk_data.walk_stmts_backward = false;
397 walk_data.dom_direction = CDI_DOMINATORS;
398 walk_data.initialize_block_local_data = NULL;
399 walk_data.before_dom_children_before_stmts = dom_opt_initialize_block;
400 walk_data.before_dom_children_walk_stmts = optimize_stmt;
401 walk_data.before_dom_children_after_stmts = propagate_to_outgoing_edges;
402 walk_data.after_dom_children_before_stmts = NULL;
403 walk_data.after_dom_children_walk_stmts = NULL;
404 walk_data.after_dom_children_after_stmts = dom_opt_finalize_block;
405 /* Right now we only attach a dummy COND_EXPR to the global data pointer.
406 When we attach more stuff we'll need to fill this out with a real
407 structure. */
408 walk_data.global_data = NULL;
409 walk_data.block_local_data_size = 0;
410
411 /* Now initialize the dominator walker. */
412 init_walk_dominator_tree (&walk_data);
413
414 calculate_dominance_info (CDI_DOMINATORS);
415
416 /* If we prove certain blocks are unreachable, then we want to
417 repeat the dominator optimization process as PHI nodes may
418 have turned into copies which allows better propagation of
419 values. So we repeat until we do not identify any new unreachable
420 blocks. */
421 do
422 {
423 /* Optimize the dominator tree. */
424 cfg_altered = false;
425
426 /* Recursively walk the dominator tree optimizing statements. */
427 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
428
429 /* If we exposed any new variables, go ahead and put them into
430 SSA form now, before we handle jump threading. This simplifies
431 interactions between rewriting of _DECL nodes into SSA form
432 and rewriting SSA_NAME nodes into SSA form after block
433 duplication and CFG manipulation. */
434 if (!bitmap_empty_p (vars_to_rename))
435 {
436 rewrite_into_ssa (false);
437 bitmap_clear (vars_to_rename);
438 }
439
440 free_all_edge_infos ();
441
442 /* Thread jumps, creating duplicate blocks as needed. */
443 cfg_altered = thread_through_all_blocks ();
444
445 /* Removal of statements may make some EH edges dead. Purge
446 such edges from the CFG as needed. */
447 if (!bitmap_empty_p (need_eh_cleanup))
448 {
449 cfg_altered |= tree_purge_all_dead_eh_edges (need_eh_cleanup);
450 bitmap_zero (need_eh_cleanup);
451 }
452
453 free_dominance_info (CDI_DOMINATORS);
454 cfg_altered = cleanup_tree_cfg ();
455 calculate_dominance_info (CDI_DOMINATORS);
456
457 rewrite_ssa_into_ssa ();
458
459 /* Reinitialize the various tables. */
460 bitmap_clear (nonzero_vars);
461 htab_empty (avail_exprs);
462 htab_empty (vrp_data);
463
464 for (i = 0; i < num_referenced_vars; i++)
465 var_ann (referenced_var (i))->current_def = NULL;
466 }
467 while (cfg_altered);
468
469 /* Debugging dumps. */
470 if (dump_file && (dump_flags & TDF_STATS))
471 dump_dominator_optimization_stats (dump_file);
472
473 /* We emptied the hash table earlier, now delete it completely. */
474 htab_delete (avail_exprs);
475 htab_delete (vrp_data);
476
477 /* It is not necessary to clear CURRDEFS, REDIRECTION_EDGES, VRP_DATA,
478 CONST_AND_COPIES, and NONZERO_VARS as they all get cleared at the bottom
479 of the do-while loop above. */
480
481 /* And finalize the dominator walker. */
482 fini_walk_dominator_tree (&walk_data);
483
484 /* Free nonzero_vars. */
485 BITMAP_XFREE (nonzero_vars);
486 BITMAP_XFREE (need_eh_cleanup);
487
488 /* Finally, remove everything except invariants in SSA_NAME_VALUE.
489
490 Long term we will be able to let everything in SSA_NAME_VALUE
491 persist. However, for now, we know this is the safe thing to
492 do. */
493 for (i = 0; i < num_ssa_names; i++)
494 {
495 tree name = ssa_name (i);
496 tree value;
497
498 if (!name)
499 continue;
500
501 value = SSA_NAME_VALUE (name);
502 if (value && !is_gimple_min_invariant (value))
503 SSA_NAME_VALUE (name) = NULL;
504 }
505 }
506
507 static bool
508 gate_dominator (void)
509 {
510 return flag_tree_dom != 0;
511 }
512
513 struct tree_opt_pass pass_dominator =
514 {
515 "dom", /* name */
516 gate_dominator, /* gate */
517 tree_ssa_dominator_optimize, /* execute */
518 NULL, /* sub */
519 NULL, /* next */
520 0, /* static_pass_number */
521 TV_TREE_SSA_DOMINATOR_OPTS, /* tv_id */
522 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
523 0, /* properties_provided */
524 0, /* properties_destroyed */
525 0, /* todo_flags_start */
526 TODO_dump_func | TODO_rename_vars
527 | TODO_verify_ssa, /* todo_flags_finish */
528 0 /* letter */
529 };
530
531
532 /* We are exiting BB, see if the target block begins with a conditional
533 jump which has a known value when reached via BB. */
534
535 static void
536 thread_across_edge (struct dom_walk_data *walk_data, edge e)
537 {
538 block_stmt_iterator bsi;
539 tree stmt = NULL;
540 tree phi;
541
542 /* Each PHI creates a temporary equivalence, record them. */
543 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
544 {
545 tree src = PHI_ARG_DEF_FROM_EDGE (phi, e);
546 tree dst = PHI_RESULT (phi);
547 record_const_or_copy (dst, src);
548 register_new_def (dst, &block_defs_stack);
549 }
550
551 for (bsi = bsi_start (e->dest); ! bsi_end_p (bsi); bsi_next (&bsi))
552 {
553 tree lhs, cached_lhs;
554
555 stmt = bsi_stmt (bsi);
556
557 /* Ignore empty statements and labels. */
558 if (IS_EMPTY_STMT (stmt) || TREE_CODE (stmt) == LABEL_EXPR)
559 continue;
560
561 /* If this is not a MODIFY_EXPR which sets an SSA_NAME to a new
562 value, then stop our search here. Ideally when we stop a
563 search we stop on a COND_EXPR or SWITCH_EXPR. */
564 if (TREE_CODE (stmt) != MODIFY_EXPR
565 || TREE_CODE (TREE_OPERAND (stmt, 0)) != SSA_NAME)
566 break;
567
568 /* At this point we have a statement which assigns an RHS to an
569 SSA_VAR on the LHS. We want to prove that the RHS is already
570 available and that its value is held in the current definition
571 of the LHS -- meaning that this assignment is a NOP when
572 reached via edge E. */
573 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == SSA_NAME)
574 cached_lhs = TREE_OPERAND (stmt, 1);
575 else
576 cached_lhs = lookup_avail_expr (stmt, false);
577
578 lhs = TREE_OPERAND (stmt, 0);
579
580 /* This can happen if we thread around to the start of a loop. */
581 if (lhs == cached_lhs)
582 break;
583
584 /* If we did not find RHS in the hash table, then try again after
585 temporarily const/copy propagating the operands. */
586 if (!cached_lhs)
587 {
588 /* Copy the operands. */
589 stmt_ann_t ann = stmt_ann (stmt);
590 use_optype uses = USE_OPS (ann);
591 vuse_optype vuses = VUSE_OPS (ann);
592 tree *uses_copy = xcalloc (NUM_USES (uses), sizeof (tree));
593 tree *vuses_copy = xcalloc (NUM_VUSES (vuses), sizeof (tree));
594 unsigned int i;
595
596 /* Make a copy of the uses into USES_COPY, then cprop into
597 the use operands. */
598 for (i = 0; i < NUM_USES (uses); i++)
599 {
600 tree tmp = NULL;
601
602 uses_copy[i] = USE_OP (uses, i);
603 if (TREE_CODE (USE_OP (uses, i)) == SSA_NAME)
604 tmp = SSA_NAME_VALUE (USE_OP (uses, i));
605 if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
606 SET_USE_OP (uses, i, tmp);
607 }
608
609 /* Similarly for virtual uses. */
610 for (i = 0; i < NUM_VUSES (vuses); i++)
611 {
612 tree tmp = NULL;
613
614 vuses_copy[i] = VUSE_OP (vuses, i);
615 if (TREE_CODE (VUSE_OP (vuses, i)) == SSA_NAME)
616 tmp = SSA_NAME_VALUE (VUSE_OP (vuses, i));
617 if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
618 SET_VUSE_OP (vuses, i, tmp);
619 }
620
621 /* Try to lookup the new expression. */
622 cached_lhs = lookup_avail_expr (stmt, false);
623
624 /* Restore the statement's original uses/defs. */
625 for (i = 0; i < NUM_USES (uses); i++)
626 SET_USE_OP (uses, i, uses_copy[i]);
627
628 for (i = 0; i < NUM_VUSES (vuses); i++)
629 SET_VUSE_OP (vuses, i, vuses_copy[i]);
630
631 free (uses_copy);
632 free (vuses_copy);
633
634 /* If we still did not find the expression in the hash table,
635 then we can not ignore this statement. */
636 if (! cached_lhs)
637 break;
638 }
639
640 /* If the expression in the hash table was not assigned to an
641 SSA_NAME, then we can not ignore this statement. */
642 if (TREE_CODE (cached_lhs) != SSA_NAME)
643 break;
644
645 /* If we have different underlying variables, then we can not
646 ignore this statement. */
647 if (SSA_NAME_VAR (cached_lhs) != SSA_NAME_VAR (lhs))
648 break;
649
650 /* If CACHED_LHS does not represent the current value of the undering
651 variable in CACHED_LHS/LHS, then we can not ignore this statement. */
652 if (var_ann (SSA_NAME_VAR (lhs))->current_def != cached_lhs)
653 break;
654
655 /* If we got here, then we can ignore this statement and continue
656 walking through the statements in the block looking for a threadable
657 COND_EXPR.
658
659 We want to record an equivalence lhs = cache_lhs so that if
660 the result of this statement is used later we can copy propagate
661 suitably. */
662 record_const_or_copy (lhs, cached_lhs);
663 register_new_def (lhs, &block_defs_stack);
664 }
665
666 /* If we stopped at a COND_EXPR or SWITCH_EXPR, then see if we know which
667 arm will be taken. */
668 if (stmt
669 && (TREE_CODE (stmt) == COND_EXPR
670 || TREE_CODE (stmt) == SWITCH_EXPR))
671 {
672 tree cond, cached_lhs;
673 edge e1;
674 edge_iterator ei;
675
676 /* Do not forward entry edges into the loop. In the case loop
677 has multiple entry edges we may end up in constructing irreducible
678 region.
679 ??? We may consider forwarding the edges in the case all incoming
680 edges forward to the same destination block. */
681 if (!e->flags & EDGE_DFS_BACK)
682 {
683 FOR_EACH_EDGE (e1, ei, e->dest->preds)
684 if (e1->flags & EDGE_DFS_BACK)
685 break;
686 if (e1)
687 return;
688 }
689
690 /* Now temporarily cprop the operands and try to find the resulting
691 expression in the hash tables. */
692 if (TREE_CODE (stmt) == COND_EXPR)
693 cond = COND_EXPR_COND (stmt);
694 else
695 cond = SWITCH_COND (stmt);
696
697 if (COMPARISON_CLASS_P (cond))
698 {
699 tree dummy_cond, op0, op1;
700 enum tree_code cond_code;
701
702 op0 = TREE_OPERAND (cond, 0);
703 op1 = TREE_OPERAND (cond, 1);
704 cond_code = TREE_CODE (cond);
705
706 /* Get the current value of both operands. */
707 if (TREE_CODE (op0) == SSA_NAME)
708 {
709 tree tmp = SSA_NAME_VALUE (op0);
710 if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
711 op0 = tmp;
712 }
713
714 if (TREE_CODE (op1) == SSA_NAME)
715 {
716 tree tmp = SSA_NAME_VALUE (op1);
717 if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
718 op1 = tmp;
719 }
720
721 /* Stuff the operator and operands into our dummy conditional
722 expression, creating the dummy conditional if necessary. */
723 dummy_cond = walk_data->global_data;
724 if (! dummy_cond)
725 {
726 dummy_cond = build (cond_code, boolean_type_node, op0, op1);
727 dummy_cond = build (COND_EXPR, void_type_node,
728 dummy_cond, NULL, NULL);
729 walk_data->global_data = dummy_cond;
730 }
731 else
732 {
733 TREE_SET_CODE (COND_EXPR_COND (dummy_cond), cond_code);
734 TREE_OPERAND (COND_EXPR_COND (dummy_cond), 0) = op0;
735 TREE_OPERAND (COND_EXPR_COND (dummy_cond), 1) = op1;
736 }
737
738 /* If the conditional folds to an invariant, then we are done,
739 otherwise look it up in the hash tables. */
740 cached_lhs = local_fold (COND_EXPR_COND (dummy_cond));
741 if (! is_gimple_min_invariant (cached_lhs))
742 cached_lhs = lookup_avail_expr (dummy_cond, false);
743 if (!cached_lhs || ! is_gimple_min_invariant (cached_lhs))
744 {
745 cached_lhs = simplify_cond_and_lookup_avail_expr (dummy_cond,
746 NULL,
747 false);
748 }
749 }
750 /* We can have conditionals which just test the state of a
751 variable rather than use a relational operator. These are
752 simpler to handle. */
753 else if (TREE_CODE (cond) == SSA_NAME)
754 {
755 cached_lhs = cond;
756 cached_lhs = SSA_NAME_VALUE (cached_lhs);
757 if (cached_lhs && ! is_gimple_min_invariant (cached_lhs))
758 cached_lhs = 0;
759 }
760 else
761 cached_lhs = lookup_avail_expr (stmt, false);
762
763 if (cached_lhs)
764 {
765 edge taken_edge = find_taken_edge (e->dest, cached_lhs);
766 basic_block dest = (taken_edge ? taken_edge->dest : NULL);
767
768 if (dest == e->dest)
769 return;
770
771 /* If we have a known destination for the conditional, then
772 we can perform this optimization, which saves at least one
773 conditional jump each time it applies since we get to
774 bypass the conditional at our original destination. */
775 if (dest)
776 {
777 struct edge_info *edge_info;
778
779 update_bb_profile_for_threading (e->dest, EDGE_FREQUENCY (e),
780 e->count, taken_edge);
781 if (e->aux)
782 edge_info = e->aux;
783 else
784 edge_info = allocate_edge_info (e);
785 edge_info->redirection_target = taken_edge;
786 bb_ann (e->dest)->incoming_edge_threaded = true;
787 }
788 }
789 }
790 }
791
792
793 /* Initialize local stacks for this optimizer and record equivalences
794 upon entry to BB. Equivalences can come from the edge traversed to
795 reach BB or they may come from PHI nodes at the start of BB. */
796
797 static void
798 dom_opt_initialize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
799 basic_block bb)
800 {
801 if (dump_file && (dump_flags & TDF_DETAILS))
802 fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
803
804 /* Push a marker on the stacks of local information so that we know how
805 far to unwind when we finalize this block. */
806 VARRAY_PUSH_TREE (avail_exprs_stack, NULL_TREE);
807 VARRAY_PUSH_TREE (block_defs_stack, NULL_TREE);
808 VARRAY_PUSH_TREE (const_and_copies_stack, NULL_TREE);
809 VARRAY_PUSH_TREE (nonzero_vars_stack, NULL_TREE);
810 VARRAY_PUSH_TREE (vrp_variables_stack, NULL_TREE);
811
812 record_equivalences_from_incoming_edge (bb);
813
814 /* PHI nodes can create equivalences too. */
815 record_equivalences_from_phis (bb);
816 }
817
818 /* Given an expression EXPR (a relational expression or a statement),
819 initialize the hash table element pointed by by ELEMENT. */
820
821 static void
822 initialize_hash_element (tree expr, tree lhs, struct expr_hash_elt *element)
823 {
824 /* Hash table elements may be based on conditional expressions or statements.
825
826 For the former case, we have no annotation and we want to hash the
827 conditional expression. In the latter case we have an annotation and
828 we want to record the expression the statement evaluates. */
829 if (COMPARISON_CLASS_P (expr) || TREE_CODE (expr) == TRUTH_NOT_EXPR)
830 {
831 element->ann = NULL;
832 element->rhs = expr;
833 }
834 else if (TREE_CODE (expr) == COND_EXPR)
835 {
836 element->ann = stmt_ann (expr);
837 element->rhs = COND_EXPR_COND (expr);
838 }
839 else if (TREE_CODE (expr) == SWITCH_EXPR)
840 {
841 element->ann = stmt_ann (expr);
842 element->rhs = SWITCH_COND (expr);
843 }
844 else if (TREE_CODE (expr) == RETURN_EXPR && TREE_OPERAND (expr, 0))
845 {
846 element->ann = stmt_ann (expr);
847 element->rhs = TREE_OPERAND (TREE_OPERAND (expr, 0), 1);
848 }
849 else
850 {
851 element->ann = stmt_ann (expr);
852 element->rhs = TREE_OPERAND (expr, 1);
853 }
854
855 element->lhs = lhs;
856 element->hash = avail_expr_hash (element);
857 }
858
859 /* Remove all the expressions in LOCALS from TABLE, stopping when there are
860 LIMIT entries left in LOCALs. */
861
862 static void
863 remove_local_expressions_from_table (void)
864 {
865 /* Remove all the expressions made available in this block. */
866 while (VARRAY_ACTIVE_SIZE (avail_exprs_stack) > 0)
867 {
868 struct expr_hash_elt element;
869 tree expr = VARRAY_TOP_TREE (avail_exprs_stack);
870 VARRAY_POP (avail_exprs_stack);
871
872 if (expr == NULL_TREE)
873 break;
874
875 initialize_hash_element (expr, NULL, &element);
876 htab_remove_elt_with_hash (avail_exprs, &element, element.hash);
877 }
878 }
879
880 /* Use the SSA_NAMES in LOCALS to restore TABLE to its original
881 state, stopping when there are LIMIT entries left in LOCALs. */
882
883 static void
884 restore_nonzero_vars_to_original_value (void)
885 {
886 while (VARRAY_ACTIVE_SIZE (nonzero_vars_stack) > 0)
887 {
888 tree name = VARRAY_TOP_TREE (nonzero_vars_stack);
889 VARRAY_POP (nonzero_vars_stack);
890
891 if (name == NULL)
892 break;
893
894 bitmap_clear_bit (nonzero_vars, SSA_NAME_VERSION (name));
895 }
896 }
897
898 /* Use the source/dest pairs in CONST_AND_COPIES_STACK to restore
899 CONST_AND_COPIES to its original state, stopping when we hit a
900 NULL marker. */
901
902 static void
903 restore_vars_to_original_value (void)
904 {
905 while (VARRAY_ACTIVE_SIZE (const_and_copies_stack) > 0)
906 {
907 tree prev_value, dest;
908
909 dest = VARRAY_TOP_TREE (const_and_copies_stack);
910 VARRAY_POP (const_and_copies_stack);
911
912 if (dest == NULL)
913 break;
914
915 prev_value = VARRAY_TOP_TREE (const_and_copies_stack);
916 VARRAY_POP (const_and_copies_stack);
917
918 SSA_NAME_VALUE (dest) = prev_value;
919 }
920 }
921
922 /* Similar to restore_vars_to_original_value, except that it restores
923 CURRDEFS to its original value. */
924 static void
925 restore_currdefs_to_original_value (void)
926 {
927 /* Restore CURRDEFS to its original state. */
928 while (VARRAY_ACTIVE_SIZE (block_defs_stack) > 0)
929 {
930 tree tmp = VARRAY_TOP_TREE (block_defs_stack);
931 tree saved_def, var;
932
933 VARRAY_POP (block_defs_stack);
934
935 if (tmp == NULL_TREE)
936 break;
937
938 /* If we recorded an SSA_NAME, then make the SSA_NAME the current
939 definition of its underlying variable. If we recorded anything
940 else, it must have been an _DECL node and its current reaching
941 definition must have been NULL. */
942 if (TREE_CODE (tmp) == SSA_NAME)
943 {
944 saved_def = tmp;
945 var = SSA_NAME_VAR (saved_def);
946 }
947 else
948 {
949 saved_def = NULL;
950 var = tmp;
951 }
952
953 var_ann (var)->current_def = saved_def;
954 }
955 }
956
957 /* We have finished processing the dominator children of BB, perform
958 any finalization actions in preparation for leaving this node in
959 the dominator tree. */
960
961 static void
962 dom_opt_finalize_block (struct dom_walk_data *walk_data, basic_block bb)
963 {
964 tree last;
965
966 /* If we are at a leaf node in the dominator graph, see if we can thread
967 the edge from BB through its successor.
968
969 Do this before we remove entries from our equivalence tables. */
970 if (EDGE_COUNT (bb->succs) == 1
971 && (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL) == 0
972 && (get_immediate_dominator (CDI_DOMINATORS, EDGE_SUCC (bb, 0)->dest) != bb
973 || phi_nodes (EDGE_SUCC (bb, 0)->dest)))
974
975 {
976 thread_across_edge (walk_data, EDGE_SUCC (bb, 0));
977 }
978 else if ((last = last_stmt (bb))
979 && TREE_CODE (last) == COND_EXPR
980 && (COMPARISON_CLASS_P (COND_EXPR_COND (last))
981 || TREE_CODE (COND_EXPR_COND (last)) == SSA_NAME)
982 && EDGE_COUNT (bb->succs) == 2
983 && (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL) == 0
984 && (EDGE_SUCC (bb, 1)->flags & EDGE_ABNORMAL) == 0)
985 {
986 edge true_edge, false_edge;
987
988 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
989
990 /* If the THEN arm is the end of a dominator tree or has PHI nodes,
991 then try to thread through its edge. */
992 if (get_immediate_dominator (CDI_DOMINATORS, true_edge->dest) != bb
993 || phi_nodes (true_edge->dest))
994 {
995 struct edge_info *edge_info;
996 unsigned int i;
997
998 /* Push a marker onto the available expression stack so that we
999 unwind any expressions related to the TRUE arm before processing
1000 the false arm below. */
1001 VARRAY_PUSH_TREE (avail_exprs_stack, NULL_TREE);
1002 VARRAY_PUSH_TREE (block_defs_stack, NULL_TREE);
1003 VARRAY_PUSH_TREE (const_and_copies_stack, NULL_TREE);
1004
1005 edge_info = true_edge->aux;
1006
1007 /* If we have info associated with this edge, record it into
1008 our equivalency tables. */
1009 if (edge_info)
1010 {
1011 tree *cond_equivalences = edge_info->cond_equivalences;
1012 tree lhs = edge_info->lhs;
1013 tree rhs = edge_info->rhs;
1014
1015 /* If we have a simple NAME = VALUE equivalency record it.
1016 Until the jump threading selection code improves, only
1017 do this if both the name and value are SSA_NAMEs with
1018 the same underlying variable to avoid missing threading
1019 opportunities. */
1020 if (lhs
1021 && TREE_CODE (COND_EXPR_COND (last)) == SSA_NAME
1022 && TREE_CODE (edge_info->rhs) == SSA_NAME
1023 && SSA_NAME_VAR (lhs) == SSA_NAME_VAR (rhs))
1024 record_const_or_copy (lhs, rhs);
1025
1026 /* If we have 0 = COND or 1 = COND equivalences, record them
1027 into our expression hash tables. */
1028 if (cond_equivalences)
1029 for (i = 0; i < edge_info->max_cond_equivalences; i += 2)
1030 {
1031 tree expr = cond_equivalences[i];
1032 tree value = cond_equivalences[i + 1];
1033
1034 record_cond (expr, value);
1035 }
1036 }
1037
1038 /* Now thread the edge. */
1039 thread_across_edge (walk_data, true_edge);
1040
1041 /* And restore the various tables to their state before
1042 we threaded this edge. */
1043 remove_local_expressions_from_table ();
1044 restore_vars_to_original_value ();
1045 restore_currdefs_to_original_value ();
1046 }
1047
1048 /* Similarly for the ELSE arm. */
1049 if (get_immediate_dominator (CDI_DOMINATORS, false_edge->dest) != bb
1050 || phi_nodes (false_edge->dest))
1051 {
1052 struct edge_info *edge_info;
1053 unsigned int i;
1054
1055 edge_info = false_edge->aux;
1056
1057 /* If we have info associated with this edge, record it into
1058 our equivalency tables. */
1059 if (edge_info)
1060 {
1061 tree *cond_equivalences = edge_info->cond_equivalences;
1062 tree lhs = edge_info->lhs;
1063 tree rhs = edge_info->rhs;
1064
1065 /* If we have a simple NAME = VALUE equivalency record it.
1066 Until the jump threading selection code improves, only
1067 do this if both the name and value are SSA_NAMEs with
1068 the same underlying variable to avoid missing threading
1069 opportunities. */
1070 if (lhs
1071 && TREE_CODE (COND_EXPR_COND (last)) == SSA_NAME)
1072 record_const_or_copy (lhs, rhs);
1073
1074 /* If we have 0 = COND or 1 = COND equivalences, record them
1075 into our expression hash tables. */
1076 if (cond_equivalences)
1077 for (i = 0; i < edge_info->max_cond_equivalences; i += 2)
1078 {
1079 tree expr = cond_equivalences[i];
1080 tree value = cond_equivalences[i + 1];
1081
1082 record_cond (expr, value);
1083 }
1084 }
1085
1086 thread_across_edge (walk_data, false_edge);
1087
1088 /* No need to remove local expressions from our tables
1089 or restore vars to their original value as that will
1090 be done immediately below. */
1091 }
1092 }
1093
1094 remove_local_expressions_from_table ();
1095 restore_nonzero_vars_to_original_value ();
1096 restore_vars_to_original_value ();
1097 restore_currdefs_to_original_value ();
1098
1099 /* Remove VRP records associated with this basic block. They are no
1100 longer valid.
1101
1102 To be efficient, we note which variables have had their values
1103 constrained in this block. So walk over each variable in the
1104 VRP_VARIABLEs array. */
1105 while (VARRAY_ACTIVE_SIZE (vrp_variables_stack) > 0)
1106 {
1107 tree var = VARRAY_TOP_TREE (vrp_variables_stack);
1108 struct vrp_hash_elt vrp_hash_elt, *vrp_hash_elt_p;
1109 void **slot;
1110
1111 /* Each variable has a stack of value range records. We want to
1112 invalidate those associated with our basic block. So we walk
1113 the array backwards popping off records associated with our
1114 block. Once we hit a record not associated with our block
1115 we are done. */
1116 varray_type var_vrp_records;
1117
1118 VARRAY_POP (vrp_variables_stack);
1119
1120 if (var == NULL)
1121 break;
1122
1123 vrp_hash_elt.var = var;
1124 vrp_hash_elt.records = NULL;
1125
1126 slot = htab_find_slot (vrp_data, &vrp_hash_elt, NO_INSERT);
1127
1128 vrp_hash_elt_p = (struct vrp_hash_elt *) *slot;
1129 var_vrp_records = vrp_hash_elt_p->records;
1130
1131 while (VARRAY_ACTIVE_SIZE (var_vrp_records) > 0)
1132 {
1133 struct vrp_element *element
1134 = (struct vrp_element *)VARRAY_TOP_GENERIC_PTR (var_vrp_records);
1135
1136 if (element->bb != bb)
1137 break;
1138
1139 VARRAY_POP (var_vrp_records);
1140 }
1141 }
1142
1143 /* If we queued any statements to rescan in this block, then
1144 go ahead and rescan them now. */
1145 while (VARRAY_ACTIVE_SIZE (stmts_to_rescan) > 0)
1146 {
1147 tree stmt = VARRAY_TOP_TREE (stmts_to_rescan);
1148 basic_block stmt_bb = bb_for_stmt (stmt);
1149
1150 if (stmt_bb != bb)
1151 break;
1152
1153 VARRAY_POP (stmts_to_rescan);
1154 mark_new_vars_to_rename (stmt, vars_to_rename);
1155 }
1156 }
1157
1158 /* PHI nodes can create equivalences too.
1159
1160 Ignoring any alternatives which are the same as the result, if
1161 all the alternatives are equal, then the PHI node creates an
1162 equivalence.
1163
1164 Additionally, if all the PHI alternatives are known to have a nonzero
1165 value, then the result of this PHI is known to have a nonzero value,
1166 even if we do not know its exact value. */
1167
1168 static void
1169 record_equivalences_from_phis (basic_block bb)
1170 {
1171 tree phi;
1172
1173 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1174 {
1175 tree lhs = PHI_RESULT (phi);
1176 tree rhs = NULL;
1177 int i;
1178
1179 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1180 {
1181 tree t = PHI_ARG_DEF (phi, i);
1182
1183 if (TREE_CODE (t) == SSA_NAME || is_gimple_min_invariant (t))
1184 {
1185 /* Ignore alternatives which are the same as our LHS. */
1186 if (operand_equal_p (lhs, t, 0))
1187 continue;
1188
1189 /* If we have not processed an alternative yet, then set
1190 RHS to this alternative. */
1191 if (rhs == NULL)
1192 rhs = t;
1193 /* If we have processed an alternative (stored in RHS), then
1194 see if it is equal to this one. If it isn't, then stop
1195 the search. */
1196 else if (! operand_equal_p (rhs, t, 0))
1197 break;
1198 }
1199 else
1200 break;
1201 }
1202
1203 /* If we had no interesting alternatives, then all the RHS alternatives
1204 must have been the same as LHS. */
1205 if (!rhs)
1206 rhs = lhs;
1207
1208 /* If we managed to iterate through each PHI alternative without
1209 breaking out of the loop, then we have a PHI which may create
1210 a useful equivalence. We do not need to record unwind data for
1211 this, since this is a true assignment and not an equivalence
1212 inferred from a comparison. All uses of this ssa name are dominated
1213 by this assignment, so unwinding just costs time and space. */
1214 if (i == PHI_NUM_ARGS (phi)
1215 && may_propagate_copy (lhs, rhs))
1216 SSA_NAME_VALUE (lhs) = rhs;
1217
1218 /* Now see if we know anything about the nonzero property for the
1219 result of this PHI. */
1220 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1221 {
1222 if (!PHI_ARG_NONZERO (phi, i))
1223 break;
1224 }
1225
1226 if (i == PHI_NUM_ARGS (phi))
1227 bitmap_set_bit (nonzero_vars, SSA_NAME_VERSION (PHI_RESULT (phi)));
1228
1229 register_new_def (lhs, &block_defs_stack);
1230 }
1231 }
1232
1233 /* Ignoring loop backedges, if BB has precisely one incoming edge then
1234 return that edge. Otherwise return NULL. */
1235 static edge
1236 single_incoming_edge_ignoring_loop_edges (basic_block bb)
1237 {
1238 edge retval = NULL;
1239 edge e;
1240 edge_iterator ei;
1241
1242 FOR_EACH_EDGE (e, ei, bb->preds)
1243 {
1244 /* A loop back edge can be identified by the destination of
1245 the edge dominating the source of the edge. */
1246 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
1247 continue;
1248
1249 /* If we have already seen a non-loop edge, then we must have
1250 multiple incoming non-loop edges and thus we return NULL. */
1251 if (retval)
1252 return NULL;
1253
1254 /* This is the first non-loop incoming edge we have found. Record
1255 it. */
1256 retval = e;
1257 }
1258
1259 return retval;
1260 }
1261
1262 /* Record any equivalences created by the incoming edge to BB. If BB
1263 has more than one incoming edge, then no equivalence is created. */
1264
1265 static void
1266 record_equivalences_from_incoming_edge (basic_block bb)
1267 {
1268 edge e;
1269 basic_block parent;
1270 struct edge_info *edge_info;
1271
1272 /* If our parent block ended with a control statment, then we may be
1273 able to record some equivalences based on which outgoing edge from
1274 the parent was followed. */
1275 parent = get_immediate_dominator (CDI_DOMINATORS, bb);
1276
1277 e = single_incoming_edge_ignoring_loop_edges (bb);
1278
1279 /* If we had a single incoming edge from our parent block, then enter
1280 any data associated with the edge into our tables. */
1281 if (e && e->src == parent)
1282 {
1283 unsigned int i;
1284
1285 edge_info = e->aux;
1286
1287 if (edge_info)
1288 {
1289 tree lhs = edge_info->lhs;
1290 tree rhs = edge_info->rhs;
1291 tree *cond_equivalences = edge_info->cond_equivalences;
1292
1293 if (lhs)
1294 record_equality (lhs, rhs);
1295
1296 if (cond_equivalences)
1297 {
1298 bool recorded_range = false;
1299 for (i = 0; i < edge_info->max_cond_equivalences; i += 2)
1300 {
1301 tree expr = cond_equivalences[i];
1302 tree value = cond_equivalences[i + 1];
1303
1304 record_cond (expr, value);
1305
1306 /* For the first true equivalence, record range
1307 information. We only do this for the first
1308 true equivalence as it should dominate any
1309 later true equivalences. */
1310 if (! recorded_range
1311 && COMPARISON_CLASS_P (expr)
1312 && value == boolean_true_node
1313 && TREE_CONSTANT (TREE_OPERAND (expr, 1)))
1314 {
1315 record_range (expr, bb);
1316 recorded_range = true;
1317 }
1318 }
1319 }
1320 }
1321 }
1322 }
1323
1324 /* Dump SSA statistics on FILE. */
1325
1326 void
1327 dump_dominator_optimization_stats (FILE *file)
1328 {
1329 long n_exprs;
1330
1331 fprintf (file, "Total number of statements: %6ld\n\n",
1332 opt_stats.num_stmts);
1333 fprintf (file, "Exprs considered for dominator optimizations: %6ld\n",
1334 opt_stats.num_exprs_considered);
1335
1336 n_exprs = opt_stats.num_exprs_considered;
1337 if (n_exprs == 0)
1338 n_exprs = 1;
1339
1340 fprintf (file, " Redundant expressions eliminated: %6ld (%.0f%%)\n",
1341 opt_stats.num_re, PERCENT (opt_stats.num_re,
1342 n_exprs));
1343
1344 fprintf (file, "\nHash table statistics:\n");
1345
1346 fprintf (file, " avail_exprs: ");
1347 htab_statistics (file, avail_exprs);
1348 }
1349
1350
1351 /* Dump SSA statistics on stderr. */
1352
1353 void
1354 debug_dominator_optimization_stats (void)
1355 {
1356 dump_dominator_optimization_stats (stderr);
1357 }
1358
1359
1360 /* Dump statistics for the hash table HTAB. */
1361
1362 static void
1363 htab_statistics (FILE *file, htab_t htab)
1364 {
1365 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
1366 (long) htab_size (htab),
1367 (long) htab_elements (htab),
1368 htab_collisions (htab));
1369 }
1370
1371 /* Record the fact that VAR has a nonzero value, though we may not know
1372 its exact value. Note that if VAR is already known to have a nonzero
1373 value, then we do nothing. */
1374
1375 static void
1376 record_var_is_nonzero (tree var)
1377 {
1378 int indx = SSA_NAME_VERSION (var);
1379
1380 if (bitmap_bit_p (nonzero_vars, indx))
1381 return;
1382
1383 /* Mark it in the global table. */
1384 bitmap_set_bit (nonzero_vars, indx);
1385
1386 /* Record this SSA_NAME so that we can reset the global table
1387 when we leave this block. */
1388 VARRAY_PUSH_TREE (nonzero_vars_stack, var);
1389 }
1390
1391 /* Enter a statement into the true/false expression hash table indicating
1392 that the condition COND has the value VALUE. */
1393
1394 static void
1395 record_cond (tree cond, tree value)
1396 {
1397 struct expr_hash_elt *element = xmalloc (sizeof (struct expr_hash_elt));
1398 void **slot;
1399
1400 initialize_hash_element (cond, value, element);
1401
1402 slot = htab_find_slot_with_hash (avail_exprs, (void *)element,
1403 element->hash, true);
1404 if (*slot == NULL)
1405 {
1406 *slot = (void *) element;
1407 VARRAY_PUSH_TREE (avail_exprs_stack, cond);
1408 }
1409 else
1410 free (element);
1411 }
1412
1413 /* Build a new conditional using NEW_CODE, OP0 and OP1 and store
1414 the new conditional into *p, then store a boolean_true_node
1415 into the the *(p + 1). */
1416
1417 static void
1418 build_and_record_new_cond (enum tree_code new_code, tree op0, tree op1, tree *p)
1419 {
1420 *p = build2 (new_code, boolean_type_node, op0, op1);
1421 p++;
1422 *p = boolean_true_node;
1423 }
1424
1425 /* Record that COND is true and INVERTED is false into the edge information
1426 structure. Also record that any conditions dominated by COND are true
1427 as well.
1428
1429 For example, if a < b is true, then a <= b must also be true. */
1430
1431 static void
1432 record_conditions (struct edge_info *edge_info, tree cond, tree inverted)
1433 {
1434 tree op0, op1;
1435
1436 if (!COMPARISON_CLASS_P (cond))
1437 return;
1438
1439 op0 = TREE_OPERAND (cond, 0);
1440 op1 = TREE_OPERAND (cond, 1);
1441
1442 switch (TREE_CODE (cond))
1443 {
1444 case LT_EXPR:
1445 case GT_EXPR:
1446 edge_info->max_cond_equivalences = 12;
1447 edge_info->cond_equivalences = xmalloc (12 * sizeof (tree));
1448 build_and_record_new_cond ((TREE_CODE (cond) == LT_EXPR
1449 ? LE_EXPR : GE_EXPR),
1450 op0, op1, &edge_info->cond_equivalences[4]);
1451 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1452 &edge_info->cond_equivalences[6]);
1453 build_and_record_new_cond (NE_EXPR, op0, op1,
1454 &edge_info->cond_equivalences[8]);
1455 build_and_record_new_cond (LTGT_EXPR, op0, op1,
1456 &edge_info->cond_equivalences[10]);
1457 break;
1458
1459 case GE_EXPR:
1460 case LE_EXPR:
1461 edge_info->max_cond_equivalences = 6;
1462 edge_info->cond_equivalences = xmalloc (6 * sizeof (tree));
1463 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1464 &edge_info->cond_equivalences[4]);
1465 break;
1466
1467 case EQ_EXPR:
1468 edge_info->max_cond_equivalences = 10;
1469 edge_info->cond_equivalences = xmalloc (10 * sizeof (tree));
1470 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1471 &edge_info->cond_equivalences[4]);
1472 build_and_record_new_cond (LE_EXPR, op0, op1,
1473 &edge_info->cond_equivalences[6]);
1474 build_and_record_new_cond (GE_EXPR, op0, op1,
1475 &edge_info->cond_equivalences[8]);
1476 break;
1477
1478 case UNORDERED_EXPR:
1479 edge_info->max_cond_equivalences = 16;
1480 edge_info->cond_equivalences = xmalloc (16 * sizeof (tree));
1481 build_and_record_new_cond (NE_EXPR, op0, op1,
1482 &edge_info->cond_equivalences[4]);
1483 build_and_record_new_cond (UNLE_EXPR, op0, op1,
1484 &edge_info->cond_equivalences[6]);
1485 build_and_record_new_cond (UNGE_EXPR, op0, op1,
1486 &edge_info->cond_equivalences[8]);
1487 build_and_record_new_cond (UNEQ_EXPR, op0, op1,
1488 &edge_info->cond_equivalences[10]);
1489 build_and_record_new_cond (UNLT_EXPR, op0, op1,
1490 &edge_info->cond_equivalences[12]);
1491 build_and_record_new_cond (UNGT_EXPR, op0, op1,
1492 &edge_info->cond_equivalences[14]);
1493 break;
1494
1495 case UNLT_EXPR:
1496 case UNGT_EXPR:
1497 edge_info->max_cond_equivalences = 8;
1498 edge_info->cond_equivalences = xmalloc (8 * sizeof (tree));
1499 build_and_record_new_cond ((TREE_CODE (cond) == UNLT_EXPR
1500 ? UNLE_EXPR : UNGE_EXPR),
1501 op0, op1, &edge_info->cond_equivalences[4]);
1502 build_and_record_new_cond (NE_EXPR, op0, op1,
1503 &edge_info->cond_equivalences[6]);
1504 break;
1505
1506 case UNEQ_EXPR:
1507 edge_info->max_cond_equivalences = 8;
1508 edge_info->cond_equivalences = xmalloc (8 * sizeof (tree));
1509 build_and_record_new_cond (UNLE_EXPR, op0, op1,
1510 &edge_info->cond_equivalences[4]);
1511 build_and_record_new_cond (UNGE_EXPR, op0, op1,
1512 &edge_info->cond_equivalences[6]);
1513 break;
1514
1515 case LTGT_EXPR:
1516 edge_info->max_cond_equivalences = 8;
1517 edge_info->cond_equivalences = xmalloc (8 * sizeof (tree));
1518 build_and_record_new_cond (NE_EXPR, op0, op1,
1519 &edge_info->cond_equivalences[4]);
1520 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
1521 &edge_info->cond_equivalences[6]);
1522 break;
1523
1524 default:
1525 edge_info->max_cond_equivalences = 4;
1526 edge_info->cond_equivalences = xmalloc (4 * sizeof (tree));
1527 break;
1528 }
1529
1530 /* Now store the original true and false conditions into the first
1531 two slots. */
1532 edge_info->cond_equivalences[0] = cond;
1533 edge_info->cond_equivalences[1] = boolean_true_node;
1534 edge_info->cond_equivalences[2] = inverted;
1535 edge_info->cond_equivalences[3] = boolean_false_node;
1536 }
1537
1538 /* A helper function for record_const_or_copy and record_equality.
1539 Do the work of recording the value and undo info. */
1540
1541 static void
1542 record_const_or_copy_1 (tree x, tree y, tree prev_x)
1543 {
1544 SSA_NAME_VALUE (x) = y;
1545
1546 VARRAY_PUSH_TREE (const_and_copies_stack, prev_x);
1547 VARRAY_PUSH_TREE (const_and_copies_stack, x);
1548 }
1549
1550
1551 /* Return the loop depth of the basic block of the defining statement of X.
1552 This number should not be treated as absolutely correct because the loop
1553 information may not be completely up-to-date when dom runs. However, it
1554 will be relatively correct, and as more passes are taught to keep loop info
1555 up to date, the result will become more and more accurate. */
1556
1557 static int
1558 loop_depth_of_name (tree x)
1559 {
1560 tree defstmt;
1561 basic_block defbb;
1562
1563 /* If it's not an SSA_NAME, we have no clue where the definition is. */
1564 if (TREE_CODE (x) != SSA_NAME)
1565 return 0;
1566
1567 /* Otherwise return the loop depth of the defining statement's bb.
1568 Note that there may not actually be a bb for this statement, if the
1569 ssa_name is live on entry. */
1570 defstmt = SSA_NAME_DEF_STMT (x);
1571 defbb = bb_for_stmt (defstmt);
1572 if (!defbb)
1573 return 0;
1574
1575 return defbb->loop_depth;
1576 }
1577
1578
1579 /* Record that X is equal to Y in const_and_copies. Record undo
1580 information in the block-local varray. */
1581
1582 static void
1583 record_const_or_copy (tree x, tree y)
1584 {
1585 tree prev_x = SSA_NAME_VALUE (x);
1586
1587 if (TREE_CODE (y) == SSA_NAME)
1588 {
1589 tree tmp = SSA_NAME_VALUE (y);
1590 if (tmp)
1591 y = tmp;
1592 }
1593
1594 record_const_or_copy_1 (x, y, prev_x);
1595 }
1596
1597 /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1598 This constrains the cases in which we may treat this as assignment. */
1599
1600 static void
1601 record_equality (tree x, tree y)
1602 {
1603 tree prev_x = NULL, prev_y = NULL;
1604
1605 if (TREE_CODE (x) == SSA_NAME)
1606 prev_x = SSA_NAME_VALUE (x);
1607 if (TREE_CODE (y) == SSA_NAME)
1608 prev_y = SSA_NAME_VALUE (y);
1609
1610 /* If one of the previous values is invariant, or invariant in more loops
1611 (by depth), then use that.
1612 Otherwise it doesn't matter which value we choose, just so
1613 long as we canonicalize on one value. */
1614 if (TREE_INVARIANT (y))
1615 ;
1616 else if (TREE_INVARIANT (x) || (loop_depth_of_name (x) <= loop_depth_of_name (y)))
1617 prev_x = x, x = y, y = prev_x, prev_x = prev_y;
1618 else if (prev_x && TREE_INVARIANT (prev_x))
1619 x = y, y = prev_x, prev_x = prev_y;
1620 else if (prev_y && TREE_CODE (prev_y) != VALUE_HANDLE)
1621 y = prev_y;
1622
1623 /* After the swapping, we must have one SSA_NAME. */
1624 if (TREE_CODE (x) != SSA_NAME)
1625 return;
1626
1627 /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1628 variable compared against zero. If we're honoring signed zeros,
1629 then we cannot record this value unless we know that the value is
1630 nonzero. */
1631 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (x)))
1632 && (TREE_CODE (y) != REAL_CST
1633 || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (y))))
1634 return;
1635
1636 record_const_or_copy_1 (x, y, prev_x);
1637 }
1638
1639 /* Return true, if it is ok to do folding of an associative expression.
1640 EXP is the tree for the associative expression. */
1641
1642 static inline bool
1643 unsafe_associative_fp_binop (tree exp)
1644 {
1645 enum tree_code code = TREE_CODE (exp);
1646 return !(!flag_unsafe_math_optimizations
1647 && (code == MULT_EXPR || code == PLUS_EXPR
1648 || code == MINUS_EXPR)
1649 && FLOAT_TYPE_P (TREE_TYPE (exp)));
1650 }
1651
1652 /* STMT is a MODIFY_EXPR for which we were unable to find RHS in the
1653 hash tables. Try to simplify the RHS using whatever equivalences
1654 we may have recorded.
1655
1656 If we are able to simplify the RHS, then lookup the simplified form in
1657 the hash table and return the result. Otherwise return NULL. */
1658
1659 static tree
1660 simplify_rhs_and_lookup_avail_expr (struct dom_walk_data *walk_data,
1661 tree stmt, int insert)
1662 {
1663 tree rhs = TREE_OPERAND (stmt, 1);
1664 enum tree_code rhs_code = TREE_CODE (rhs);
1665 tree result = NULL;
1666
1667 /* If we have lhs = ~x, look and see if we earlier had x = ~y.
1668 In which case we can change this statement to be lhs = y.
1669 Which can then be copy propagated.
1670
1671 Similarly for negation. */
1672 if ((rhs_code == BIT_NOT_EXPR || rhs_code == NEGATE_EXPR)
1673 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
1674 {
1675 /* Get the definition statement for our RHS. */
1676 tree rhs_def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs, 0));
1677
1678 /* See if the RHS_DEF_STMT has the same form as our statement. */
1679 if (TREE_CODE (rhs_def_stmt) == MODIFY_EXPR
1680 && TREE_CODE (TREE_OPERAND (rhs_def_stmt, 1)) == rhs_code)
1681 {
1682 tree rhs_def_operand;
1683
1684 rhs_def_operand = TREE_OPERAND (TREE_OPERAND (rhs_def_stmt, 1), 0);
1685
1686 /* Verify that RHS_DEF_OPERAND is a suitable SSA variable. */
1687 if (TREE_CODE (rhs_def_operand) == SSA_NAME
1688 && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs_def_operand))
1689 result = update_rhs_and_lookup_avail_expr (stmt,
1690 rhs_def_operand,
1691 insert);
1692 }
1693 }
1694
1695 /* If we have z = (x OP C1), see if we earlier had x = y OP C2.
1696 If OP is associative, create and fold (y OP C2) OP C1 which
1697 should result in (y OP C3), use that as the RHS for the
1698 assignment. Add minus to this, as we handle it specially below. */
1699 if ((associative_tree_code (rhs_code) || rhs_code == MINUS_EXPR)
1700 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME
1701 && is_gimple_min_invariant (TREE_OPERAND (rhs, 1)))
1702 {
1703 tree rhs_def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs, 0));
1704
1705 /* See if the RHS_DEF_STMT has the same form as our statement. */
1706 if (TREE_CODE (rhs_def_stmt) == MODIFY_EXPR)
1707 {
1708 tree rhs_def_rhs = TREE_OPERAND (rhs_def_stmt, 1);
1709 enum tree_code rhs_def_code = TREE_CODE (rhs_def_rhs);
1710
1711 if ((rhs_code == rhs_def_code && unsafe_associative_fp_binop (rhs))
1712 || (rhs_code == PLUS_EXPR && rhs_def_code == MINUS_EXPR)
1713 || (rhs_code == MINUS_EXPR && rhs_def_code == PLUS_EXPR))
1714 {
1715 tree def_stmt_op0 = TREE_OPERAND (rhs_def_rhs, 0);
1716 tree def_stmt_op1 = TREE_OPERAND (rhs_def_rhs, 1);
1717
1718 if (TREE_CODE (def_stmt_op0) == SSA_NAME
1719 && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def_stmt_op0)
1720 && is_gimple_min_invariant (def_stmt_op1))
1721 {
1722 tree outer_const = TREE_OPERAND (rhs, 1);
1723 tree type = TREE_TYPE (TREE_OPERAND (stmt, 0));
1724 tree t;
1725
1726 /* If we care about correct floating point results, then
1727 don't fold x + c1 - c2. Note that we need to take both
1728 the codes and the signs to figure this out. */
1729 if (FLOAT_TYPE_P (type)
1730 && !flag_unsafe_math_optimizations
1731 && (rhs_def_code == PLUS_EXPR
1732 || rhs_def_code == MINUS_EXPR))
1733 {
1734 bool neg = false;
1735
1736 neg ^= (rhs_code == MINUS_EXPR);
1737 neg ^= (rhs_def_code == MINUS_EXPR);
1738 neg ^= real_isneg (TREE_REAL_CST_PTR (outer_const));
1739 neg ^= real_isneg (TREE_REAL_CST_PTR (def_stmt_op1));
1740
1741 if (neg)
1742 goto dont_fold_assoc;
1743 }
1744
1745 /* Ho hum. So fold will only operate on the outermost
1746 thingy that we give it, so we have to build the new
1747 expression in two pieces. This requires that we handle
1748 combinations of plus and minus. */
1749 if (rhs_def_code != rhs_code)
1750 {
1751 if (rhs_def_code == MINUS_EXPR)
1752 t = build (MINUS_EXPR, type, outer_const, def_stmt_op1);
1753 else
1754 t = build (MINUS_EXPR, type, def_stmt_op1, outer_const);
1755 rhs_code = PLUS_EXPR;
1756 }
1757 else if (rhs_def_code == MINUS_EXPR)
1758 t = build (PLUS_EXPR, type, def_stmt_op1, outer_const);
1759 else
1760 t = build (rhs_def_code, type, def_stmt_op1, outer_const);
1761 t = local_fold (t);
1762 t = build (rhs_code, type, def_stmt_op0, t);
1763 t = local_fold (t);
1764
1765 /* If the result is a suitable looking gimple expression,
1766 then use it instead of the original for STMT. */
1767 if (TREE_CODE (t) == SSA_NAME
1768 || (UNARY_CLASS_P (t)
1769 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME)
1770 || ((BINARY_CLASS_P (t) || COMPARISON_CLASS_P (t))
1771 && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
1772 && is_gimple_val (TREE_OPERAND (t, 1))))
1773 result = update_rhs_and_lookup_avail_expr (stmt, t, insert);
1774 }
1775 }
1776 }
1777 dont_fold_assoc:;
1778 }
1779
1780 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
1781 and BIT_AND_EXPR respectively if the first operand is greater
1782 than zero and the second operand is an exact power of two. */
1783 if ((rhs_code == TRUNC_DIV_EXPR || rhs_code == TRUNC_MOD_EXPR)
1784 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0)))
1785 && integer_pow2p (TREE_OPERAND (rhs, 1)))
1786 {
1787 tree val;
1788 tree op = TREE_OPERAND (rhs, 0);
1789
1790 if (TYPE_UNSIGNED (TREE_TYPE (op)))
1791 {
1792 val = integer_one_node;
1793 }
1794 else
1795 {
1796 tree dummy_cond = walk_data->global_data;
1797
1798 if (! dummy_cond)
1799 {
1800 dummy_cond = build (GT_EXPR, boolean_type_node,
1801 op, integer_zero_node);
1802 dummy_cond = build (COND_EXPR, void_type_node,
1803 dummy_cond, NULL, NULL);
1804 walk_data->global_data = dummy_cond;
1805 }
1806 else
1807 {
1808 TREE_SET_CODE (COND_EXPR_COND (dummy_cond), GT_EXPR);
1809 TREE_OPERAND (COND_EXPR_COND (dummy_cond), 0) = op;
1810 TREE_OPERAND (COND_EXPR_COND (dummy_cond), 1)
1811 = integer_zero_node;
1812 }
1813 val = simplify_cond_and_lookup_avail_expr (dummy_cond, NULL, false);
1814 }
1815
1816 if (val && integer_onep (val))
1817 {
1818 tree t;
1819 tree op0 = TREE_OPERAND (rhs, 0);
1820 tree op1 = TREE_OPERAND (rhs, 1);
1821
1822 if (rhs_code == TRUNC_DIV_EXPR)
1823 t = build (RSHIFT_EXPR, TREE_TYPE (op0), op0,
1824 build_int_cst (NULL_TREE, tree_log2 (op1)));
1825 else
1826 t = build (BIT_AND_EXPR, TREE_TYPE (op0), op0,
1827 local_fold (build (MINUS_EXPR, TREE_TYPE (op1),
1828 op1, integer_one_node)));
1829
1830 result = update_rhs_and_lookup_avail_expr (stmt, t, insert);
1831 }
1832 }
1833
1834 /* Transform ABS (X) into X or -X as appropriate. */
1835 if (rhs_code == ABS_EXPR
1836 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0))))
1837 {
1838 tree val;
1839 tree op = TREE_OPERAND (rhs, 0);
1840 tree type = TREE_TYPE (op);
1841
1842 if (TYPE_UNSIGNED (type))
1843 {
1844 val = integer_zero_node;
1845 }
1846 else
1847 {
1848 tree dummy_cond = walk_data->global_data;
1849
1850 if (! dummy_cond)
1851 {
1852 dummy_cond = build (LE_EXPR, boolean_type_node,
1853 op, integer_zero_node);
1854 dummy_cond = build (COND_EXPR, void_type_node,
1855 dummy_cond, NULL, NULL);
1856 walk_data->global_data = dummy_cond;
1857 }
1858 else
1859 {
1860 TREE_SET_CODE (COND_EXPR_COND (dummy_cond), LE_EXPR);
1861 TREE_OPERAND (COND_EXPR_COND (dummy_cond), 0) = op;
1862 TREE_OPERAND (COND_EXPR_COND (dummy_cond), 1)
1863 = build_int_cst (type, 0);
1864 }
1865 val = simplify_cond_and_lookup_avail_expr (dummy_cond, NULL, false);
1866
1867 if (!val)
1868 {
1869 TREE_SET_CODE (COND_EXPR_COND (dummy_cond), GE_EXPR);
1870 TREE_OPERAND (COND_EXPR_COND (dummy_cond), 0) = op;
1871 TREE_OPERAND (COND_EXPR_COND (dummy_cond), 1)
1872 = build_int_cst (type, 0);
1873
1874 val = simplify_cond_and_lookup_avail_expr (dummy_cond,
1875 NULL, false);
1876
1877 if (val)
1878 {
1879 if (integer_zerop (val))
1880 val = integer_one_node;
1881 else if (integer_onep (val))
1882 val = integer_zero_node;
1883 }
1884 }
1885 }
1886
1887 if (val
1888 && (integer_onep (val) || integer_zerop (val)))
1889 {
1890 tree t;
1891
1892 if (integer_onep (val))
1893 t = build1 (NEGATE_EXPR, TREE_TYPE (op), op);
1894 else
1895 t = op;
1896
1897 result = update_rhs_and_lookup_avail_expr (stmt, t, insert);
1898 }
1899 }
1900
1901 /* Optimize *"foo" into 'f'. This is done here rather than
1902 in fold to avoid problems with stuff like &*"foo". */
1903 if (TREE_CODE (rhs) == INDIRECT_REF || TREE_CODE (rhs) == ARRAY_REF)
1904 {
1905 tree t = fold_read_from_constant_string (rhs);
1906
1907 if (t)
1908 result = update_rhs_and_lookup_avail_expr (stmt, t, insert);
1909 }
1910
1911 return result;
1912 }
1913
1914 /* COND is a condition of the form:
1915
1916 x == const or x != const
1917
1918 Look back to x's defining statement and see if x is defined as
1919
1920 x = (type) y;
1921
1922 If const is unchanged if we convert it to type, then we can build
1923 the equivalent expression:
1924
1925
1926 y == const or y != const
1927
1928 Which may allow further optimizations.
1929
1930 Return the equivalent comparison or NULL if no such equivalent comparison
1931 was found. */
1932
1933 static tree
1934 find_equivalent_equality_comparison (tree cond)
1935 {
1936 tree op0 = TREE_OPERAND (cond, 0);
1937 tree op1 = TREE_OPERAND (cond, 1);
1938 tree def_stmt = SSA_NAME_DEF_STMT (op0);
1939
1940 /* OP0 might have been a parameter, so first make sure it
1941 was defined by a MODIFY_EXPR. */
1942 if (def_stmt && TREE_CODE (def_stmt) == MODIFY_EXPR)
1943 {
1944 tree def_rhs = TREE_OPERAND (def_stmt, 1);
1945
1946 /* Now make sure the RHS of the MODIFY_EXPR is a typecast. */
1947 if ((TREE_CODE (def_rhs) == NOP_EXPR
1948 || TREE_CODE (def_rhs) == CONVERT_EXPR)
1949 && TREE_CODE (TREE_OPERAND (def_rhs, 0)) == SSA_NAME)
1950 {
1951 tree def_rhs_inner = TREE_OPERAND (def_rhs, 0);
1952 tree def_rhs_inner_type = TREE_TYPE (def_rhs_inner);
1953 tree new;
1954
1955 if (TYPE_PRECISION (def_rhs_inner_type)
1956 > TYPE_PRECISION (TREE_TYPE (def_rhs)))
1957 return NULL;
1958
1959 /* What we want to prove is that if we convert OP1 to
1960 the type of the object inside the NOP_EXPR that the
1961 result is still equivalent to SRC.
1962
1963 If that is true, the build and return new equivalent
1964 condition which uses the source of the typecast and the
1965 new constant (which has only changed its type). */
1966 new = build1 (TREE_CODE (def_rhs), def_rhs_inner_type, op1);
1967 new = local_fold (new);
1968 if (is_gimple_val (new) && tree_int_cst_equal (new, op1))
1969 return build (TREE_CODE (cond), TREE_TYPE (cond),
1970 def_rhs_inner, new);
1971 }
1972 }
1973 return NULL;
1974 }
1975
1976 /* STMT is a COND_EXPR for which we could not trivially determine its
1977 result. This routine attempts to find equivalent forms of the
1978 condition which we may be able to optimize better. It also
1979 uses simple value range propagation to optimize conditionals. */
1980
1981 static tree
1982 simplify_cond_and_lookup_avail_expr (tree stmt,
1983 stmt_ann_t ann,
1984 int insert)
1985 {
1986 tree cond = COND_EXPR_COND (stmt);
1987
1988 if (COMPARISON_CLASS_P (cond))
1989 {
1990 tree op0 = TREE_OPERAND (cond, 0);
1991 tree op1 = TREE_OPERAND (cond, 1);
1992
1993 if (TREE_CODE (op0) == SSA_NAME && is_gimple_min_invariant (op1))
1994 {
1995 int limit;
1996 tree low, high, cond_low, cond_high;
1997 int lowequal, highequal, swapped, no_overlap, subset, cond_inverted;
1998 varray_type vrp_records;
1999 struct vrp_element *element;
2000 struct vrp_hash_elt vrp_hash_elt, *vrp_hash_elt_p;
2001 void **slot;
2002
2003 /* First see if we have test of an SSA_NAME against a constant
2004 where the SSA_NAME is defined by an earlier typecast which
2005 is irrelevant when performing tests against the given
2006 constant. */
2007 if (TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
2008 {
2009 tree new_cond = find_equivalent_equality_comparison (cond);
2010
2011 if (new_cond)
2012 {
2013 /* Update the statement to use the new equivalent
2014 condition. */
2015 COND_EXPR_COND (stmt) = new_cond;
2016
2017 /* If this is not a real stmt, ann will be NULL and we
2018 avoid processing the operands. */
2019 if (ann)
2020 modify_stmt (stmt);
2021
2022 /* Lookup the condition and return its known value if it
2023 exists. */
2024 new_cond = lookup_avail_expr (stmt, insert);
2025 if (new_cond)
2026 return new_cond;
2027
2028 /* The operands have changed, so update op0 and op1. */
2029 op0 = TREE_OPERAND (cond, 0);
2030 op1 = TREE_OPERAND (cond, 1);
2031 }
2032 }
2033
2034 /* Consult the value range records for this variable (if they exist)
2035 to see if we can eliminate or simplify this conditional.
2036
2037 Note two tests are necessary to determine no records exist.
2038 First we have to see if the virtual array exists, if it
2039 exists, then we have to check its active size.
2040
2041 Also note the vast majority of conditionals are not testing
2042 a variable which has had its range constrained by an earlier
2043 conditional. So this filter avoids a lot of unnecessary work. */
2044 vrp_hash_elt.var = op0;
2045 vrp_hash_elt.records = NULL;
2046 slot = htab_find_slot (vrp_data, &vrp_hash_elt, NO_INSERT);
2047 if (slot == NULL)
2048 return NULL;
2049
2050 vrp_hash_elt_p = (struct vrp_hash_elt *) *slot;
2051 vrp_records = vrp_hash_elt_p->records;
2052 if (vrp_records == NULL)
2053 return NULL;
2054
2055 limit = VARRAY_ACTIVE_SIZE (vrp_records);
2056
2057 /* If we have no value range records for this variable, or we are
2058 unable to extract a range for this condition, then there is
2059 nothing to do. */
2060 if (limit == 0
2061 || ! extract_range_from_cond (cond, &cond_high,
2062 &cond_low, &cond_inverted))
2063 return NULL;
2064
2065 /* We really want to avoid unnecessary computations of range
2066 info. So all ranges are computed lazily; this avoids a
2067 lot of unnecessary work. i.e., we record the conditional,
2068 but do not process how it constrains the variable's
2069 potential values until we know that processing the condition
2070 could be helpful.
2071
2072 However, we do not want to have to walk a potentially long
2073 list of ranges, nor do we want to compute a variable's
2074 range more than once for a given path.
2075
2076 Luckily, each time we encounter a conditional that can not
2077 be otherwise optimized we will end up here and we will
2078 compute the necessary range information for the variable
2079 used in this condition.
2080
2081 Thus you can conclude that there will never be more than one
2082 conditional associated with a variable which has not been
2083 processed. So we never need to merge more than one new
2084 conditional into the current range.
2085
2086 These properties also help us avoid unnecessary work. */
2087 element
2088 = (struct vrp_element *)VARRAY_GENERIC_PTR (vrp_records, limit - 1);
2089
2090 if (element->high && element->low)
2091 {
2092 /* The last element has been processed, so there is no range
2093 merging to do, we can simply use the high/low values
2094 recorded in the last element. */
2095 low = element->low;
2096 high = element->high;
2097 }
2098 else
2099 {
2100 tree tmp_high, tmp_low;
2101 int dummy;
2102
2103 /* The last element has not been processed. Process it now. */
2104 extract_range_from_cond (element->cond, &tmp_high,
2105 &tmp_low, &dummy);
2106
2107 /* If this is the only element, then no merging is necessary,
2108 the high/low values from extract_range_from_cond are all
2109 we need. */
2110 if (limit == 1)
2111 {
2112 low = tmp_low;
2113 high = tmp_high;
2114 }
2115 else
2116 {
2117 /* Get the high/low value from the previous element. */
2118 struct vrp_element *prev
2119 = (struct vrp_element *)VARRAY_GENERIC_PTR (vrp_records,
2120 limit - 2);
2121 low = prev->low;
2122 high = prev->high;
2123
2124 /* Merge in this element's range with the range from the
2125 previous element.
2126
2127 The low value for the merged range is the maximum of
2128 the previous low value and the low value of this record.
2129
2130 Similarly the high value for the merged range is the
2131 minimum of the previous high value and the high value of
2132 this record. */
2133 low = (tree_int_cst_compare (low, tmp_low) == 1
2134 ? low : tmp_low);
2135 high = (tree_int_cst_compare (high, tmp_high) == -1
2136 ? high : tmp_high);
2137 }
2138
2139 /* And record the computed range. */
2140 element->low = low;
2141 element->high = high;
2142
2143 }
2144
2145 /* After we have constrained this variable's potential values,
2146 we try to determine the result of the given conditional.
2147
2148 To simplify later tests, first determine if the current
2149 low value is the same low value as the conditional.
2150 Similarly for the current high value and the high value
2151 for the conditional. */
2152 lowequal = tree_int_cst_equal (low, cond_low);
2153 highequal = tree_int_cst_equal (high, cond_high);
2154
2155 if (lowequal && highequal)
2156 return (cond_inverted ? boolean_false_node : boolean_true_node);
2157
2158 /* To simplify the overlap/subset tests below we may want
2159 to swap the two ranges so that the larger of the two
2160 ranges occurs "first". */
2161 swapped = 0;
2162 if (tree_int_cst_compare (low, cond_low) == 1
2163 || (lowequal
2164 && tree_int_cst_compare (cond_high, high) == 1))
2165 {
2166 tree temp;
2167
2168 swapped = 1;
2169 temp = low;
2170 low = cond_low;
2171 cond_low = temp;
2172 temp = high;
2173 high = cond_high;
2174 cond_high = temp;
2175 }
2176
2177 /* Now determine if there is no overlap in the ranges
2178 or if the second range is a subset of the first range. */
2179 no_overlap = tree_int_cst_lt (high, cond_low);
2180 subset = tree_int_cst_compare (cond_high, high) != 1;
2181
2182 /* If there was no overlap in the ranges, then this conditional
2183 always has a false value (unless we had to invert this
2184 conditional, in which case it always has a true value). */
2185 if (no_overlap)
2186 return (cond_inverted ? boolean_true_node : boolean_false_node);
2187
2188 /* If the current range is a subset of the condition's range,
2189 then this conditional always has a true value (unless we
2190 had to invert this conditional, in which case it always
2191 has a true value). */
2192 if (subset && swapped)
2193 return (cond_inverted ? boolean_false_node : boolean_true_node);
2194
2195 /* We were unable to determine the result of the conditional.
2196 However, we may be able to simplify the conditional. First
2197 merge the ranges in the same manner as range merging above. */
2198 low = tree_int_cst_compare (low, cond_low) == 1 ? low : cond_low;
2199 high = tree_int_cst_compare (high, cond_high) == -1 ? high : cond_high;
2200
2201 /* If the range has converged to a single point, then turn this
2202 into an equality comparison. */
2203 if (TREE_CODE (cond) != EQ_EXPR
2204 && TREE_CODE (cond) != NE_EXPR
2205 && tree_int_cst_equal (low, high))
2206 {
2207 TREE_SET_CODE (cond, EQ_EXPR);
2208 TREE_OPERAND (cond, 1) = high;
2209 }
2210 }
2211 }
2212 return 0;
2213 }
2214
2215 /* STMT is a SWITCH_EXPR for which we could not trivially determine its
2216 result. This routine attempts to find equivalent forms of the
2217 condition which we may be able to optimize better. */
2218
2219 static tree
2220 simplify_switch_and_lookup_avail_expr (tree stmt, int insert)
2221 {
2222 tree cond = SWITCH_COND (stmt);
2223 tree def, to, ti;
2224
2225 /* The optimization that we really care about is removing unnecessary
2226 casts. That will let us do much better in propagating the inferred
2227 constant at the switch target. */
2228 if (TREE_CODE (cond) == SSA_NAME)
2229 {
2230 def = SSA_NAME_DEF_STMT (cond);
2231 if (TREE_CODE (def) == MODIFY_EXPR)
2232 {
2233 def = TREE_OPERAND (def, 1);
2234 if (TREE_CODE (def) == NOP_EXPR)
2235 {
2236 int need_precision;
2237 bool fail;
2238
2239 def = TREE_OPERAND (def, 0);
2240
2241 #ifdef ENABLE_CHECKING
2242 /* ??? Why was Jeff testing this? We are gimple... */
2243 gcc_assert (is_gimple_val (def));
2244 #endif
2245
2246 to = TREE_TYPE (cond);
2247 ti = TREE_TYPE (def);
2248
2249 /* If we have an extension that preserves value, then we
2250 can copy the source value into the switch. */
2251
2252 need_precision = TYPE_PRECISION (ti);
2253 fail = false;
2254 if (TYPE_UNSIGNED (to) && !TYPE_UNSIGNED (ti))
2255 fail = true;
2256 else if (!TYPE_UNSIGNED (to) && TYPE_UNSIGNED (ti))
2257 need_precision += 1;
2258 if (TYPE_PRECISION (to) < need_precision)
2259 fail = true;
2260
2261 if (!fail)
2262 {
2263 SWITCH_COND (stmt) = def;
2264 modify_stmt (stmt);
2265
2266 return lookup_avail_expr (stmt, insert);
2267 }
2268 }
2269 }
2270 }
2271
2272 return 0;
2273 }
2274
2275
2276 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
2277 known value for that SSA_NAME (or NULL if no value is known).
2278
2279 NONZERO_VARS is the set SSA_NAMES known to have a nonzero value,
2280 even if we don't know their precise value.
2281
2282 Propagate values from CONST_AND_COPIES and NONZERO_VARS into the PHI
2283 nodes of the successors of BB. */
2284
2285 static void
2286 cprop_into_successor_phis (basic_block bb, bitmap nonzero_vars)
2287 {
2288 edge e;
2289 edge_iterator ei;
2290
2291 /* This can get rather expensive if the implementation is naive in
2292 how it finds the phi alternative associated with a particular edge. */
2293 FOR_EACH_EDGE (e, ei, bb->succs)
2294 {
2295 tree phi;
2296 int phi_num_args;
2297 int hint;
2298
2299 /* If this is an abnormal edge, then we do not want to copy propagate
2300 into the PHI alternative associated with this edge. */
2301 if (e->flags & EDGE_ABNORMAL)
2302 continue;
2303
2304 phi = phi_nodes (e->dest);
2305 if (! phi)
2306 continue;
2307
2308 /* There is no guarantee that for any two PHI nodes in a block that
2309 the phi alternative associated with a particular edge will be
2310 at the same index in the phi alternative array.
2311
2312 However, it is very likely they will be the same. So we keep
2313 track of the index of the alternative where we found the edge in
2314 the previous phi node and check that index first in the next
2315 phi node. If that hint fails, then we actually search all
2316 the entries. */
2317 phi_num_args = PHI_NUM_ARGS (phi);
2318 hint = phi_num_args;
2319 for ( ; phi; phi = PHI_CHAIN (phi))
2320 {
2321 int i;
2322 tree new;
2323 use_operand_p orig_p;
2324 tree orig;
2325
2326 /* If the hint is valid (!= phi_num_args), see if it points
2327 us to the desired phi alternative. */
2328 if (hint != phi_num_args && PHI_ARG_EDGE (phi, hint) == e)
2329 ;
2330 else
2331 {
2332 /* The hint was either invalid or did not point to the
2333 correct phi alternative. Search all the alternatives
2334 for the correct one. Update the hint. */
2335 for (i = 0; i < phi_num_args; i++)
2336 if (PHI_ARG_EDGE (phi, i) == e)
2337 break;
2338 hint = i;
2339 }
2340
2341 /* If we did not find the proper alternative, then something is
2342 horribly wrong. */
2343 gcc_assert (hint != phi_num_args);
2344
2345 /* The alternative may be associated with a constant, so verify
2346 it is an SSA_NAME before doing anything with it. */
2347 orig_p = PHI_ARG_DEF_PTR (phi, hint);
2348 orig = USE_FROM_PTR (orig_p);
2349 if (TREE_CODE (orig) != SSA_NAME)
2350 continue;
2351
2352 /* If the alternative is known to have a nonzero value, record
2353 that fact in the PHI node itself for future use. */
2354 if (bitmap_bit_p (nonzero_vars, SSA_NAME_VERSION (orig)))
2355 PHI_ARG_NONZERO (phi, hint) = true;
2356
2357 /* If we have *ORIG_P in our constant/copy table, then replace
2358 ORIG_P with its value in our constant/copy table. */
2359 new = SSA_NAME_VALUE (orig);
2360 if (new
2361 && (TREE_CODE (new) == SSA_NAME
2362 || is_gimple_min_invariant (new))
2363 && may_propagate_copy (orig, new))
2364 {
2365 propagate_value (orig_p, new);
2366 }
2367 }
2368 }
2369 }
2370
2371 /* We have finished optimizing BB, record any information implied by
2372 taking a specific outgoing edge from BB. */
2373
2374 static void
2375 record_edge_info (basic_block bb)
2376 {
2377 block_stmt_iterator bsi = bsi_last (bb);
2378 struct edge_info *edge_info;
2379
2380 if (! bsi_end_p (bsi))
2381 {
2382 tree stmt = bsi_stmt (bsi);
2383
2384 if (stmt && TREE_CODE (stmt) == SWITCH_EXPR)
2385 {
2386 tree cond = SWITCH_COND (stmt);
2387
2388 if (TREE_CODE (cond) == SSA_NAME)
2389 {
2390 tree labels = SWITCH_LABELS (stmt);
2391 int i, n_labels = TREE_VEC_LENGTH (labels);
2392 tree *info = xcalloc (n_basic_blocks, sizeof (tree));
2393 edge e;
2394 edge_iterator ei;
2395
2396 for (i = 0; i < n_labels; i++)
2397 {
2398 tree label = TREE_VEC_ELT (labels, i);
2399 basic_block target_bb = label_to_block (CASE_LABEL (label));
2400
2401 if (CASE_HIGH (label)
2402 || !CASE_LOW (label)
2403 || info[target_bb->index])
2404 info[target_bb->index] = error_mark_node;
2405 else
2406 info[target_bb->index] = label;
2407 }
2408
2409 FOR_EACH_EDGE (e, ei, bb->succs)
2410 {
2411 basic_block target_bb = e->dest;
2412 tree node = info[target_bb->index];
2413
2414 if (node != NULL && node != error_mark_node)
2415 {
2416 tree x = fold_convert (TREE_TYPE (cond), CASE_LOW (node));
2417 edge_info = allocate_edge_info (e);
2418 edge_info->lhs = cond;
2419 edge_info->rhs = x;
2420 }
2421 }
2422 free (info);
2423 }
2424 }
2425
2426 /* A COND_EXPR may create equivalences too. */
2427 if (stmt && TREE_CODE (stmt) == COND_EXPR)
2428 {
2429 tree cond = COND_EXPR_COND (stmt);
2430 edge true_edge;
2431 edge false_edge;
2432
2433 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2434
2435 /* If the conditional is a single variable 'X', record 'X = 1'
2436 for the true edge and 'X = 0' on the false edge. */
2437 if (SSA_VAR_P (cond))
2438 {
2439 struct edge_info *edge_info;
2440
2441 edge_info = allocate_edge_info (true_edge);
2442 edge_info->lhs = cond;
2443 edge_info->rhs = constant_boolean_node (1, TREE_TYPE (cond));
2444
2445 edge_info = allocate_edge_info (false_edge);
2446 edge_info->lhs = cond;
2447 edge_info->rhs = constant_boolean_node (0, TREE_TYPE (cond));
2448 }
2449 /* Equality tests may create one or two equivalences. */
2450 else if (COMPARISON_CLASS_P (cond))
2451 {
2452 tree op0 = TREE_OPERAND (cond, 0);
2453 tree op1 = TREE_OPERAND (cond, 1);
2454
2455 /* Special case comparing booleans against a constant as we
2456 know the value of OP0 on both arms of the branch. i.e., we
2457 can record an equivalence for OP0 rather than COND. */
2458 if ((TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
2459 && TREE_CODE (op0) == SSA_NAME
2460 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
2461 && is_gimple_min_invariant (op1))
2462 {
2463 if (TREE_CODE (cond) == EQ_EXPR)
2464 {
2465 edge_info = allocate_edge_info (true_edge);
2466 edge_info->lhs = op0;
2467 edge_info->rhs = (integer_zerop (op1)
2468 ? boolean_false_node
2469 : boolean_true_node);
2470
2471 edge_info = allocate_edge_info (false_edge);
2472 edge_info->lhs = op0;
2473 edge_info->rhs = (integer_zerop (op1)
2474 ? boolean_true_node
2475 : boolean_false_node);
2476 }
2477 else
2478 {
2479 edge_info = allocate_edge_info (true_edge);
2480 edge_info->lhs = op0;
2481 edge_info->rhs = (integer_zerop (op1)
2482 ? boolean_true_node
2483 : boolean_false_node);
2484
2485 edge_info = allocate_edge_info (false_edge);
2486 edge_info->lhs = op0;
2487 edge_info->rhs = (integer_zerop (op1)
2488 ? boolean_false_node
2489 : boolean_true_node);
2490 }
2491 }
2492
2493 if (is_gimple_min_invariant (op0)
2494 && (TREE_CODE (op1) == SSA_NAME
2495 || is_gimple_min_invariant (op1)))
2496 {
2497 tree inverted = invert_truthvalue (cond);
2498 struct edge_info *edge_info;
2499
2500 edge_info = allocate_edge_info (true_edge);
2501 record_conditions (edge_info, cond, inverted);
2502
2503 if (TREE_CODE (cond) == EQ_EXPR)
2504 {
2505 edge_info->lhs = op1;
2506 edge_info->rhs = op0;
2507 }
2508
2509 edge_info = allocate_edge_info (false_edge);
2510 record_conditions (edge_info, inverted, cond);
2511
2512 if (TREE_CODE (cond) == NE_EXPR)
2513 {
2514 edge_info->lhs = op1;
2515 edge_info->rhs = op0;
2516 }
2517 }
2518
2519 if (TREE_CODE (op0) == SSA_NAME
2520 && (is_gimple_min_invariant (op1)
2521 || TREE_CODE (op1) == SSA_NAME))
2522 {
2523 tree inverted = invert_truthvalue (cond);
2524 struct edge_info *edge_info;
2525
2526 edge_info = allocate_edge_info (true_edge);
2527 record_conditions (edge_info, cond, inverted);
2528
2529 if (TREE_CODE (cond) == EQ_EXPR)
2530 {
2531 edge_info->lhs = op0;
2532 edge_info->rhs = op1;
2533 }
2534
2535 edge_info = allocate_edge_info (false_edge);
2536 record_conditions (edge_info, inverted, cond);
2537
2538 if (TREE_CODE (cond) == NE_EXPR)
2539 {
2540 edge_info->lhs = op0;
2541 edge_info->rhs = op1;
2542 }
2543 }
2544 }
2545
2546 /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
2547 }
2548 }
2549 }
2550
2551 /* Propagate information from BB to its outgoing edges.
2552
2553 This can include equivalency information implied by control statements
2554 at the end of BB and const/copy propagation into PHIs in BB's
2555 successor blocks. */
2556
2557 static void
2558 propagate_to_outgoing_edges (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
2559 basic_block bb)
2560 {
2561
2562 record_edge_info (bb);
2563 cprop_into_successor_phis (bb, nonzero_vars);
2564 }
2565
2566 /* Search for redundant computations in STMT. If any are found, then
2567 replace them with the variable holding the result of the computation.
2568
2569 If safe, record this expression into the available expression hash
2570 table. */
2571
2572 static bool
2573 eliminate_redundant_computations (struct dom_walk_data *walk_data,
2574 tree stmt, stmt_ann_t ann)
2575 {
2576 v_may_def_optype v_may_defs = V_MAY_DEF_OPS (ann);
2577 tree *expr_p, def = NULL_TREE;
2578 bool insert = true;
2579 tree cached_lhs;
2580 bool retval = false;
2581
2582 if (TREE_CODE (stmt) == MODIFY_EXPR)
2583 def = TREE_OPERAND (stmt, 0);
2584
2585 /* Certain expressions on the RHS can be optimized away, but can not
2586 themselves be entered into the hash tables. */
2587 if (ann->makes_aliased_stores
2588 || ! def
2589 || TREE_CODE (def) != SSA_NAME
2590 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)
2591 || NUM_V_MAY_DEFS (v_may_defs) != 0)
2592 insert = false;
2593
2594 /* Check if the expression has been computed before. */
2595 cached_lhs = lookup_avail_expr (stmt, insert);
2596
2597 /* If this is an assignment and the RHS was not in the hash table,
2598 then try to simplify the RHS and lookup the new RHS in the
2599 hash table. */
2600 if (! cached_lhs && TREE_CODE (stmt) == MODIFY_EXPR)
2601 cached_lhs = simplify_rhs_and_lookup_avail_expr (walk_data, stmt, insert);
2602 /* Similarly if this is a COND_EXPR and we did not find its
2603 expression in the hash table, simplify the condition and
2604 try again. */
2605 else if (! cached_lhs && TREE_CODE (stmt) == COND_EXPR)
2606 cached_lhs = simplify_cond_and_lookup_avail_expr (stmt, ann, insert);
2607 /* Similarly for a SWITCH_EXPR. */
2608 else if (!cached_lhs && TREE_CODE (stmt) == SWITCH_EXPR)
2609 cached_lhs = simplify_switch_and_lookup_avail_expr (stmt, insert);
2610
2611 opt_stats.num_exprs_considered++;
2612
2613 /* Get a pointer to the expression we are trying to optimize. */
2614 if (TREE_CODE (stmt) == COND_EXPR)
2615 expr_p = &COND_EXPR_COND (stmt);
2616 else if (TREE_CODE (stmt) == SWITCH_EXPR)
2617 expr_p = &SWITCH_COND (stmt);
2618 else if (TREE_CODE (stmt) == RETURN_EXPR && TREE_OPERAND (stmt, 0))
2619 expr_p = &TREE_OPERAND (TREE_OPERAND (stmt, 0), 1);
2620 else
2621 expr_p = &TREE_OPERAND (stmt, 1);
2622
2623 /* It is safe to ignore types here since we have already done
2624 type checking in the hashing and equality routines. In fact
2625 type checking here merely gets in the way of constant
2626 propagation. Also, make sure that it is safe to propagate
2627 CACHED_LHS into *EXPR_P. */
2628 if (cached_lhs
2629 && (TREE_CODE (cached_lhs) != SSA_NAME
2630 || may_propagate_copy (*expr_p, cached_lhs)))
2631 {
2632 if (dump_file && (dump_flags & TDF_DETAILS))
2633 {
2634 fprintf (dump_file, " Replaced redundant expr '");
2635 print_generic_expr (dump_file, *expr_p, dump_flags);
2636 fprintf (dump_file, "' with '");
2637 print_generic_expr (dump_file, cached_lhs, dump_flags);
2638 fprintf (dump_file, "'\n");
2639 }
2640
2641 opt_stats.num_re++;
2642
2643 #if defined ENABLE_CHECKING
2644 gcc_assert (TREE_CODE (cached_lhs) == SSA_NAME
2645 || is_gimple_min_invariant (cached_lhs));
2646 #endif
2647
2648 if (TREE_CODE (cached_lhs) == ADDR_EXPR
2649 || (POINTER_TYPE_P (TREE_TYPE (*expr_p))
2650 && is_gimple_min_invariant (cached_lhs)))
2651 retval = true;
2652
2653 propagate_tree_value (expr_p, cached_lhs);
2654 modify_stmt (stmt);
2655 }
2656 return retval;
2657 }
2658
2659 /* STMT, a MODIFY_EXPR, may create certain equivalences, in either
2660 the available expressions table or the const_and_copies table.
2661 Detect and record those equivalences. */
2662
2663 static void
2664 record_equivalences_from_stmt (tree stmt,
2665 int may_optimize_p,
2666 stmt_ann_t ann)
2667 {
2668 tree lhs = TREE_OPERAND (stmt, 0);
2669 enum tree_code lhs_code = TREE_CODE (lhs);
2670 int i;
2671
2672 if (lhs_code == SSA_NAME)
2673 {
2674 tree rhs = TREE_OPERAND (stmt, 1);
2675
2676 /* Strip away any useless type conversions. */
2677 STRIP_USELESS_TYPE_CONVERSION (rhs);
2678
2679 /* If the RHS of the assignment is a constant or another variable that
2680 may be propagated, register it in the CONST_AND_COPIES table. We
2681 do not need to record unwind data for this, since this is a true
2682 assignment and not an equivalence inferred from a comparison. All
2683 uses of this ssa name are dominated by this assignment, so unwinding
2684 just costs time and space. */
2685 if (may_optimize_p
2686 && (TREE_CODE (rhs) == SSA_NAME
2687 || is_gimple_min_invariant (rhs)))
2688 SSA_NAME_VALUE (lhs) = rhs;
2689
2690 /* alloca never returns zero and the address of a non-weak symbol
2691 is never zero. NOP_EXPRs and CONVERT_EXPRs can be completely
2692 stripped as they do not affect this equivalence. */
2693 while (TREE_CODE (rhs) == NOP_EXPR
2694 || TREE_CODE (rhs) == CONVERT_EXPR)
2695 rhs = TREE_OPERAND (rhs, 0);
2696
2697 if (alloca_call_p (rhs)
2698 || (TREE_CODE (rhs) == ADDR_EXPR
2699 && DECL_P (TREE_OPERAND (rhs, 0))
2700 && ! DECL_WEAK (TREE_OPERAND (rhs, 0))))
2701 record_var_is_nonzero (lhs);
2702
2703 /* IOR of any value with a nonzero value will result in a nonzero
2704 value. Even if we do not know the exact result recording that
2705 the result is nonzero is worth the effort. */
2706 if (TREE_CODE (rhs) == BIT_IOR_EXPR
2707 && integer_nonzerop (TREE_OPERAND (rhs, 1)))
2708 record_var_is_nonzero (lhs);
2709 }
2710
2711 /* Look at both sides for pointer dereferences. If we find one, then
2712 the pointer must be nonnull and we can enter that equivalence into
2713 the hash tables. */
2714 if (flag_delete_null_pointer_checks)
2715 for (i = 0; i < 2; i++)
2716 {
2717 tree t = TREE_OPERAND (stmt, i);
2718
2719 /* Strip away any COMPONENT_REFs. */
2720 while (TREE_CODE (t) == COMPONENT_REF)
2721 t = TREE_OPERAND (t, 0);
2722
2723 /* Now see if this is a pointer dereference. */
2724 if (INDIRECT_REF_P (t))
2725 {
2726 tree op = TREE_OPERAND (t, 0);
2727
2728 /* If the pointer is a SSA variable, then enter new
2729 equivalences into the hash table. */
2730 while (TREE_CODE (op) == SSA_NAME)
2731 {
2732 tree def = SSA_NAME_DEF_STMT (op);
2733
2734 record_var_is_nonzero (op);
2735
2736 /* And walk up the USE-DEF chains noting other SSA_NAMEs
2737 which are known to have a nonzero value. */
2738 if (def
2739 && TREE_CODE (def) == MODIFY_EXPR
2740 && TREE_CODE (TREE_OPERAND (def, 1)) == NOP_EXPR)
2741 op = TREE_OPERAND (TREE_OPERAND (def, 1), 0);
2742 else
2743 break;
2744 }
2745 }
2746 }
2747
2748 /* A memory store, even an aliased store, creates a useful
2749 equivalence. By exchanging the LHS and RHS, creating suitable
2750 vops and recording the result in the available expression table,
2751 we may be able to expose more redundant loads. */
2752 if (!ann->has_volatile_ops
2753 && (TREE_CODE (TREE_OPERAND (stmt, 1)) == SSA_NAME
2754 || is_gimple_min_invariant (TREE_OPERAND (stmt, 1)))
2755 && !is_gimple_reg (lhs))
2756 {
2757 tree rhs = TREE_OPERAND (stmt, 1);
2758 tree new;
2759
2760 /* FIXME: If the LHS of the assignment is a bitfield and the RHS
2761 is a constant, we need to adjust the constant to fit into the
2762 type of the LHS. If the LHS is a bitfield and the RHS is not
2763 a constant, then we can not record any equivalences for this
2764 statement since we would need to represent the widening or
2765 narrowing of RHS. This fixes gcc.c-torture/execute/921016-1.c
2766 and should not be necessary if GCC represented bitfields
2767 properly. */
2768 if (lhs_code == COMPONENT_REF
2769 && DECL_BIT_FIELD (TREE_OPERAND (lhs, 1)))
2770 {
2771 if (TREE_CONSTANT (rhs))
2772 rhs = widen_bitfield (rhs, TREE_OPERAND (lhs, 1), lhs);
2773 else
2774 rhs = NULL;
2775
2776 /* If the value overflowed, then we can not use this equivalence. */
2777 if (rhs && ! is_gimple_min_invariant (rhs))
2778 rhs = NULL;
2779 }
2780
2781 if (rhs)
2782 {
2783 /* Build a new statement with the RHS and LHS exchanged. */
2784 new = build (MODIFY_EXPR, TREE_TYPE (stmt), rhs, lhs);
2785
2786 create_ssa_artficial_load_stmt (&(ann->operands), new);
2787
2788 /* Finally enter the statement into the available expression
2789 table. */
2790 lookup_avail_expr (new, true);
2791 }
2792 }
2793 }
2794
2795 /* Replace *OP_P in STMT with any known equivalent value for *OP_P from
2796 CONST_AND_COPIES. */
2797
2798 static bool
2799 cprop_operand (tree stmt, use_operand_p op_p)
2800 {
2801 bool may_have_exposed_new_symbols = false;
2802 tree val;
2803 tree op = USE_FROM_PTR (op_p);
2804
2805 /* If the operand has a known constant value or it is known to be a
2806 copy of some other variable, use the value or copy stored in
2807 CONST_AND_COPIES. */
2808 val = SSA_NAME_VALUE (op);
2809 if (val && TREE_CODE (val) != VALUE_HANDLE)
2810 {
2811 tree op_type, val_type;
2812
2813 /* Do not change the base variable in the virtual operand
2814 tables. That would make it impossible to reconstruct
2815 the renamed virtual operand if we later modify this
2816 statement. Also only allow the new value to be an SSA_NAME
2817 for propagation into virtual operands. */
2818 if (!is_gimple_reg (op)
2819 && (get_virtual_var (val) != get_virtual_var (op)
2820 || TREE_CODE (val) != SSA_NAME))
2821 return false;
2822
2823 /* Do not replace hard register operands in asm statements. */
2824 if (TREE_CODE (stmt) == ASM_EXPR
2825 && !may_propagate_copy_into_asm (op))
2826 return false;
2827
2828 /* Get the toplevel type of each operand. */
2829 op_type = TREE_TYPE (op);
2830 val_type = TREE_TYPE (val);
2831
2832 /* While both types are pointers, get the type of the object
2833 pointed to. */
2834 while (POINTER_TYPE_P (op_type) && POINTER_TYPE_P (val_type))
2835 {
2836 op_type = TREE_TYPE (op_type);
2837 val_type = TREE_TYPE (val_type);
2838 }
2839
2840 /* Make sure underlying types match before propagating a constant by
2841 converting the constant to the proper type. Note that convert may
2842 return a non-gimple expression, in which case we ignore this
2843 propagation opportunity. */
2844 if (TREE_CODE (val) != SSA_NAME)
2845 {
2846 if (!lang_hooks.types_compatible_p (op_type, val_type))
2847 {
2848 val = fold_convert (TREE_TYPE (op), val);
2849 if (!is_gimple_min_invariant (val))
2850 return false;
2851 }
2852 }
2853
2854 /* Certain operands are not allowed to be copy propagated due
2855 to their interaction with exception handling and some GCC
2856 extensions. */
2857 else if (!may_propagate_copy (op, val))
2858 return false;
2859
2860 /* Dump details. */
2861 if (dump_file && (dump_flags & TDF_DETAILS))
2862 {
2863 fprintf (dump_file, " Replaced '");
2864 print_generic_expr (dump_file, op, dump_flags);
2865 fprintf (dump_file, "' with %s '",
2866 (TREE_CODE (val) != SSA_NAME ? "constant" : "variable"));
2867 print_generic_expr (dump_file, val, dump_flags);
2868 fprintf (dump_file, "'\n");
2869 }
2870
2871 /* If VAL is an ADDR_EXPR or a constant of pointer type, note
2872 that we may have exposed a new symbol for SSA renaming. */
2873 if (TREE_CODE (val) == ADDR_EXPR
2874 || (POINTER_TYPE_P (TREE_TYPE (op))
2875 && is_gimple_min_invariant (val)))
2876 may_have_exposed_new_symbols = true;
2877
2878 propagate_value (op_p, val);
2879
2880 /* And note that we modified this statement. This is now
2881 safe, even if we changed virtual operands since we will
2882 rescan the statement and rewrite its operands again. */
2883 modify_stmt (stmt);
2884 }
2885 return may_have_exposed_new_symbols;
2886 }
2887
2888 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
2889 known value for that SSA_NAME (or NULL if no value is known).
2890
2891 Propagate values from CONST_AND_COPIES into the uses, vuses and
2892 v_may_def_ops of STMT. */
2893
2894 static bool
2895 cprop_into_stmt (tree stmt)
2896 {
2897 bool may_have_exposed_new_symbols = false;
2898 use_operand_p op_p;
2899 ssa_op_iter iter;
2900 tree rhs;
2901
2902 FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_ALL_USES)
2903 {
2904 if (TREE_CODE (USE_FROM_PTR (op_p)) == SSA_NAME)
2905 may_have_exposed_new_symbols |= cprop_operand (stmt, op_p);
2906 }
2907
2908 if (may_have_exposed_new_symbols)
2909 {
2910 rhs = get_rhs (stmt);
2911 if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
2912 recompute_tree_invarant_for_addr_expr (rhs);
2913 }
2914
2915 return may_have_exposed_new_symbols;
2916 }
2917
2918
2919 /* Optimize the statement pointed by iterator SI.
2920
2921 We try to perform some simplistic global redundancy elimination and
2922 constant propagation:
2923
2924 1- To detect global redundancy, we keep track of expressions that have
2925 been computed in this block and its dominators. If we find that the
2926 same expression is computed more than once, we eliminate repeated
2927 computations by using the target of the first one.
2928
2929 2- Constant values and copy assignments. This is used to do very
2930 simplistic constant and copy propagation. When a constant or copy
2931 assignment is found, we map the value on the RHS of the assignment to
2932 the variable in the LHS in the CONST_AND_COPIES table. */
2933
2934 static void
2935 optimize_stmt (struct dom_walk_data *walk_data, basic_block bb,
2936 block_stmt_iterator si)
2937 {
2938 stmt_ann_t ann;
2939 tree stmt;
2940 bool may_optimize_p;
2941 bool may_have_exposed_new_symbols = false;
2942
2943 stmt = bsi_stmt (si);
2944
2945 get_stmt_operands (stmt);
2946 ann = stmt_ann (stmt);
2947 opt_stats.num_stmts++;
2948 may_have_exposed_new_symbols = false;
2949
2950 if (dump_file && (dump_flags & TDF_DETAILS))
2951 {
2952 fprintf (dump_file, "Optimizing statement ");
2953 print_generic_stmt (dump_file, stmt, TDF_SLIM);
2954 }
2955
2956 /* Const/copy propagate into USES, VUSES and the RHS of V_MAY_DEFs. */
2957 may_have_exposed_new_symbols = cprop_into_stmt (stmt);
2958
2959 /* If the statement has been modified with constant replacements,
2960 fold its RHS before checking for redundant computations. */
2961 if (ann->modified)
2962 {
2963 /* Try to fold the statement making sure that STMT is kept
2964 up to date. */
2965 if (fold_stmt (bsi_stmt_ptr (si)))
2966 {
2967 stmt = bsi_stmt (si);
2968 ann = stmt_ann (stmt);
2969
2970 if (dump_file && (dump_flags & TDF_DETAILS))
2971 {
2972 fprintf (dump_file, " Folded to: ");
2973 print_generic_stmt (dump_file, stmt, TDF_SLIM);
2974 }
2975 }
2976
2977 /* Constant/copy propagation above may change the set of
2978 virtual operands associated with this statement. Folding
2979 may remove the need for some virtual operands.
2980
2981 Indicate we will need to rescan and rewrite the statement. */
2982 may_have_exposed_new_symbols = true;
2983 }
2984
2985 /* Check for redundant computations. Do this optimization only
2986 for assignments that have no volatile ops and conditionals. */
2987 may_optimize_p = (!ann->has_volatile_ops
2988 && ((TREE_CODE (stmt) == RETURN_EXPR
2989 && TREE_OPERAND (stmt, 0)
2990 && TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR
2991 && ! (TREE_SIDE_EFFECTS
2992 (TREE_OPERAND (TREE_OPERAND (stmt, 0), 1))))
2993 || (TREE_CODE (stmt) == MODIFY_EXPR
2994 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (stmt, 1)))
2995 || TREE_CODE (stmt) == COND_EXPR
2996 || TREE_CODE (stmt) == SWITCH_EXPR));
2997
2998 if (may_optimize_p)
2999 may_have_exposed_new_symbols
3000 |= eliminate_redundant_computations (walk_data, stmt, ann);
3001
3002 /* Record any additional equivalences created by this statement. */
3003 if (TREE_CODE (stmt) == MODIFY_EXPR)
3004 record_equivalences_from_stmt (stmt,
3005 may_optimize_p,
3006 ann);
3007
3008 register_definitions_for_stmt (stmt);
3009
3010 /* If STMT is a COND_EXPR and it was modified, then we may know
3011 where it goes. If that is the case, then mark the CFG as altered.
3012
3013 This will cause us to later call remove_unreachable_blocks and
3014 cleanup_tree_cfg when it is safe to do so. It is not safe to
3015 clean things up here since removal of edges and such can trigger
3016 the removal of PHI nodes, which in turn can release SSA_NAMEs to
3017 the manager.
3018
3019 That's all fine and good, except that once SSA_NAMEs are released
3020 to the manager, we must not call create_ssa_name until all references
3021 to released SSA_NAMEs have been eliminated.
3022
3023 All references to the deleted SSA_NAMEs can not be eliminated until
3024 we remove unreachable blocks.
3025
3026 We can not remove unreachable blocks until after we have completed
3027 any queued jump threading.
3028
3029 We can not complete any queued jump threads until we have taken
3030 appropriate variables out of SSA form. Taking variables out of
3031 SSA form can call create_ssa_name and thus we lose.
3032
3033 Ultimately I suspect we're going to need to change the interface
3034 into the SSA_NAME manager. */
3035
3036 if (ann->modified)
3037 {
3038 tree val = NULL;
3039
3040 if (TREE_CODE (stmt) == COND_EXPR)
3041 val = COND_EXPR_COND (stmt);
3042 else if (TREE_CODE (stmt) == SWITCH_EXPR)
3043 val = SWITCH_COND (stmt);
3044
3045 if (val && TREE_CODE (val) == INTEGER_CST && find_taken_edge (bb, val))
3046 cfg_altered = true;
3047
3048 /* If we simplified a statement in such a way as to be shown that it
3049 cannot trap, update the eh information and the cfg to match. */
3050 if (maybe_clean_eh_stmt (stmt))
3051 {
3052 bitmap_set_bit (need_eh_cleanup, bb->index);
3053 if (dump_file && (dump_flags & TDF_DETAILS))
3054 fprintf (dump_file, " Flagged to clear EH edges.\n");
3055 }
3056 }
3057
3058 if (may_have_exposed_new_symbols)
3059 VARRAY_PUSH_TREE (stmts_to_rescan, bsi_stmt (si));
3060 }
3061
3062 /* Replace the RHS of STMT with NEW_RHS. If RHS can be found in the
3063 available expression hashtable, then return the LHS from the hash
3064 table.
3065
3066 If INSERT is true, then we also update the available expression
3067 hash table to account for the changes made to STMT. */
3068
3069 static tree
3070 update_rhs_and_lookup_avail_expr (tree stmt, tree new_rhs, bool insert)
3071 {
3072 tree cached_lhs = NULL;
3073
3074 /* Remove the old entry from the hash table. */
3075 if (insert)
3076 {
3077 struct expr_hash_elt element;
3078
3079 initialize_hash_element (stmt, NULL, &element);
3080 htab_remove_elt_with_hash (avail_exprs, &element, element.hash);
3081 }
3082
3083 /* Now update the RHS of the assignment. */
3084 TREE_OPERAND (stmt, 1) = new_rhs;
3085
3086 /* Now lookup the updated statement in the hash table. */
3087 cached_lhs = lookup_avail_expr (stmt, insert);
3088
3089 /* We have now called lookup_avail_expr twice with two different
3090 versions of this same statement, once in optimize_stmt, once here.
3091
3092 We know the call in optimize_stmt did not find an existing entry
3093 in the hash table, so a new entry was created. At the same time
3094 this statement was pushed onto the BLOCK_AVAIL_EXPRS varray.
3095
3096 If this call failed to find an existing entry on the hash table,
3097 then the new version of this statement was entered into the
3098 hash table. And this statement was pushed onto BLOCK_AVAIL_EXPR
3099 for the second time. So there are two copies on BLOCK_AVAIL_EXPRs
3100
3101 If this call succeeded, we still have one copy of this statement
3102 on the BLOCK_AVAIL_EXPRs varray.
3103
3104 For both cases, we need to pop the most recent entry off the
3105 BLOCK_AVAIL_EXPRs varray. For the case where we never found this
3106 statement in the hash tables, that will leave precisely one
3107 copy of this statement on BLOCK_AVAIL_EXPRs. For the case where
3108 we found a copy of this statement in the second hash table lookup
3109 we want _no_ copies of this statement in BLOCK_AVAIL_EXPRs. */
3110 if (insert)
3111 VARRAY_POP (avail_exprs_stack);
3112
3113 /* And make sure we record the fact that we modified this
3114 statement. */
3115 modify_stmt (stmt);
3116
3117 return cached_lhs;
3118 }
3119
3120 /* Search for an existing instance of STMT in the AVAIL_EXPRS table. If
3121 found, return its LHS. Otherwise insert STMT in the table and return
3122 NULL_TREE.
3123
3124 Also, when an expression is first inserted in the AVAIL_EXPRS table, it
3125 is also added to the stack pointed by BLOCK_AVAIL_EXPRS_P, so that they
3126 can be removed when we finish processing this block and its children.
3127
3128 NOTE: This function assumes that STMT is a MODIFY_EXPR node that
3129 contains no CALL_EXPR on its RHS and makes no volatile nor
3130 aliased references. */
3131
3132 static tree
3133 lookup_avail_expr (tree stmt, bool insert)
3134 {
3135 void **slot;
3136 tree lhs;
3137 tree temp;
3138 struct expr_hash_elt *element = xcalloc (sizeof (struct expr_hash_elt), 1);
3139
3140 lhs = TREE_CODE (stmt) == MODIFY_EXPR ? TREE_OPERAND (stmt, 0) : NULL;
3141
3142 initialize_hash_element (stmt, lhs, element);
3143
3144 /* Don't bother remembering constant assignments and copy operations.
3145 Constants and copy operations are handled by the constant/copy propagator
3146 in optimize_stmt. */
3147 if (TREE_CODE (element->rhs) == SSA_NAME
3148 || is_gimple_min_invariant (element->rhs))
3149 {
3150 free (element);
3151 return NULL_TREE;
3152 }
3153
3154 /* If this is an equality test against zero, see if we have recorded a
3155 nonzero value for the variable in question. */
3156 if ((TREE_CODE (element->rhs) == EQ_EXPR
3157 || TREE_CODE (element->rhs) == NE_EXPR)
3158 && TREE_CODE (TREE_OPERAND (element->rhs, 0)) == SSA_NAME
3159 && integer_zerop (TREE_OPERAND (element->rhs, 1)))
3160 {
3161 int indx = SSA_NAME_VERSION (TREE_OPERAND (element->rhs, 0));
3162
3163 if (bitmap_bit_p (nonzero_vars, indx))
3164 {
3165 tree t = element->rhs;
3166 free (element);
3167
3168 if (TREE_CODE (t) == EQ_EXPR)
3169 return boolean_false_node;
3170 else
3171 return boolean_true_node;
3172 }
3173 }
3174
3175 /* Finally try to find the expression in the main expression hash table. */
3176 slot = htab_find_slot_with_hash (avail_exprs, element, element->hash,
3177 (insert ? INSERT : NO_INSERT));
3178 if (slot == NULL)
3179 {
3180 free (element);
3181 return NULL_TREE;
3182 }
3183
3184 if (*slot == NULL)
3185 {
3186 *slot = (void *) element;
3187 VARRAY_PUSH_TREE (avail_exprs_stack, stmt ? stmt : element->rhs);
3188 return NULL_TREE;
3189 }
3190
3191 /* Extract the LHS of the assignment so that it can be used as the current
3192 definition of another variable. */
3193 lhs = ((struct expr_hash_elt *)*slot)->lhs;
3194
3195 /* See if the LHS appears in the CONST_AND_COPIES table. If it does, then
3196 use the value from the const_and_copies table. */
3197 if (TREE_CODE (lhs) == SSA_NAME)
3198 {
3199 temp = SSA_NAME_VALUE (lhs);
3200 if (temp && TREE_CODE (temp) != VALUE_HANDLE)
3201 lhs = temp;
3202 }
3203
3204 free (element);
3205 return lhs;
3206 }
3207
3208 /* Given a condition COND, record into HI_P, LO_P and INVERTED_P the
3209 range of values that result in the conditional having a true value.
3210
3211 Return true if we are successful in extracting a range from COND and
3212 false if we are unsuccessful. */
3213
3214 static bool
3215 extract_range_from_cond (tree cond, tree *hi_p, tree *lo_p, int *inverted_p)
3216 {
3217 tree op1 = TREE_OPERAND (cond, 1);
3218 tree high, low, type;
3219 int inverted;
3220
3221 /* Experiments have shown that it's rarely, if ever useful to
3222 record ranges for enumerations. Presumably this is due to
3223 the fact that they're rarely used directly. They are typically
3224 cast into an integer type and used that way. */
3225 if (TREE_CODE (TREE_TYPE (op1)) != INTEGER_TYPE)
3226 return 0;
3227
3228 type = TREE_TYPE (op1);
3229
3230 switch (TREE_CODE (cond))
3231 {
3232 case EQ_EXPR:
3233 high = low = op1;
3234 inverted = 0;
3235 break;
3236
3237 case NE_EXPR:
3238 high = low = op1;
3239 inverted = 1;
3240 break;
3241
3242 case GE_EXPR:
3243 low = op1;
3244 high = TYPE_MAX_VALUE (type);
3245 inverted = 0;
3246 break;
3247
3248 case GT_EXPR:
3249 low = int_const_binop (PLUS_EXPR, op1, integer_one_node, 1);
3250 high = TYPE_MAX_VALUE (type);
3251 inverted = 0;
3252 break;
3253
3254 case LE_EXPR:
3255 high = op1;
3256 low = TYPE_MIN_VALUE (type);
3257 inverted = 0;
3258 break;
3259
3260 case LT_EXPR:
3261 high = int_const_binop (MINUS_EXPR, op1, integer_one_node, 1);
3262 low = TYPE_MIN_VALUE (type);
3263 inverted = 0;
3264 break;
3265
3266 default:
3267 return 0;
3268 }
3269
3270 *hi_p = high;
3271 *lo_p = low;
3272 *inverted_p = inverted;
3273 return 1;
3274 }
3275
3276 /* Record a range created by COND for basic block BB. */
3277
3278 static void
3279 record_range (tree cond, basic_block bb)
3280 {
3281 enum tree_code code = TREE_CODE (cond);
3282
3283 /* We explicitly ignore NE_EXPRs and all the unordered comparisons.
3284 They rarely allow for meaningful range optimizations and significantly
3285 complicate the implementation. */
3286 if ((code == LT_EXPR || code == LE_EXPR || code == GT_EXPR
3287 || code == GE_EXPR || code == EQ_EXPR)
3288 && TREE_CODE (TREE_TYPE (TREE_OPERAND (cond, 1))) == INTEGER_TYPE)
3289 {
3290 struct vrp_hash_elt *vrp_hash_elt;
3291 struct vrp_element *element;
3292 varray_type *vrp_records_p;
3293 void **slot;
3294
3295
3296 vrp_hash_elt = xmalloc (sizeof (struct vrp_hash_elt));
3297 vrp_hash_elt->var = TREE_OPERAND (cond, 0);
3298 vrp_hash_elt->records = NULL;
3299 slot = htab_find_slot (vrp_data, vrp_hash_elt, INSERT);
3300
3301 if (*slot == NULL)
3302 *slot = (void *) vrp_hash_elt;
3303 else
3304 free (vrp_hash_elt);
3305
3306 vrp_hash_elt = (struct vrp_hash_elt *) *slot;
3307 vrp_records_p = &vrp_hash_elt->records;
3308
3309 element = ggc_alloc (sizeof (struct vrp_element));
3310 element->low = NULL;
3311 element->high = NULL;
3312 element->cond = cond;
3313 element->bb = bb;
3314
3315 if (*vrp_records_p == NULL)
3316 VARRAY_GENERIC_PTR_INIT (*vrp_records_p, 2, "vrp records");
3317
3318 VARRAY_PUSH_GENERIC_PTR (*vrp_records_p, element);
3319 VARRAY_PUSH_TREE (vrp_variables_stack, TREE_OPERAND (cond, 0));
3320 }
3321 }
3322
3323 /* Hashing and equality functions for VRP_DATA.
3324
3325 Since this hash table is addressed by SSA_NAMEs, we can hash on
3326 their version number and equality can be determined with a
3327 pointer comparison. */
3328
3329 static hashval_t
3330 vrp_hash (const void *p)
3331 {
3332 tree var = ((struct vrp_hash_elt *)p)->var;
3333
3334 return SSA_NAME_VERSION (var);
3335 }
3336
3337 static int
3338 vrp_eq (const void *p1, const void *p2)
3339 {
3340 tree var1 = ((struct vrp_hash_elt *)p1)->var;
3341 tree var2 = ((struct vrp_hash_elt *)p2)->var;
3342
3343 return var1 == var2;
3344 }
3345
3346 /* Hashing and equality functions for AVAIL_EXPRS. The table stores
3347 MODIFY_EXPR statements. We compute a value number for expressions using
3348 the code of the expression and the SSA numbers of its operands. */
3349
3350 static hashval_t
3351 avail_expr_hash (const void *p)
3352 {
3353 stmt_ann_t ann = ((struct expr_hash_elt *)p)->ann;
3354 tree rhs = ((struct expr_hash_elt *)p)->rhs;
3355 hashval_t val = 0;
3356 size_t i;
3357 vuse_optype vuses;
3358
3359 /* iterative_hash_expr knows how to deal with any expression and
3360 deals with commutative operators as well, so just use it instead
3361 of duplicating such complexities here. */
3362 val = iterative_hash_expr (rhs, val);
3363
3364 /* If the hash table entry is not associated with a statement, then we
3365 can just hash the expression and not worry about virtual operands
3366 and such. */
3367 if (!ann)
3368 return val;
3369
3370 /* Add the SSA version numbers of every vuse operand. This is important
3371 because compound variables like arrays are not renamed in the
3372 operands. Rather, the rename is done on the virtual variable
3373 representing all the elements of the array. */
3374 vuses = VUSE_OPS (ann);
3375 for (i = 0; i < NUM_VUSES (vuses); i++)
3376 val = iterative_hash_expr (VUSE_OP (vuses, i), val);
3377
3378 return val;
3379 }
3380
3381 static hashval_t
3382 real_avail_expr_hash (const void *p)
3383 {
3384 return ((const struct expr_hash_elt *)p)->hash;
3385 }
3386
3387 static int
3388 avail_expr_eq (const void *p1, const void *p2)
3389 {
3390 stmt_ann_t ann1 = ((struct expr_hash_elt *)p1)->ann;
3391 tree rhs1 = ((struct expr_hash_elt *)p1)->rhs;
3392 stmt_ann_t ann2 = ((struct expr_hash_elt *)p2)->ann;
3393 tree rhs2 = ((struct expr_hash_elt *)p2)->rhs;
3394
3395 /* If they are the same physical expression, return true. */
3396 if (rhs1 == rhs2 && ann1 == ann2)
3397 return true;
3398
3399 /* If their codes are not equal, then quit now. */
3400 if (TREE_CODE (rhs1) != TREE_CODE (rhs2))
3401 return false;
3402
3403 /* In case of a collision, both RHS have to be identical and have the
3404 same VUSE operands. */
3405 if ((TREE_TYPE (rhs1) == TREE_TYPE (rhs2)
3406 || lang_hooks.types_compatible_p (TREE_TYPE (rhs1), TREE_TYPE (rhs2)))
3407 && operand_equal_p (rhs1, rhs2, OEP_PURE_SAME))
3408 {
3409 vuse_optype ops1 = NULL;
3410 vuse_optype ops2 = NULL;
3411 size_t num_ops1 = 0;
3412 size_t num_ops2 = 0;
3413 size_t i;
3414
3415 if (ann1)
3416 {
3417 ops1 = VUSE_OPS (ann1);
3418 num_ops1 = NUM_VUSES (ops1);
3419 }
3420
3421 if (ann2)
3422 {
3423 ops2 = VUSE_OPS (ann2);
3424 num_ops2 = NUM_VUSES (ops2);
3425 }
3426
3427 /* If the number of virtual uses is different, then we consider
3428 them not equal. */
3429 if (num_ops1 != num_ops2)
3430 return false;
3431
3432 for (i = 0; i < num_ops1; i++)
3433 if (VUSE_OP (ops1, i) != VUSE_OP (ops2, i))
3434 return false;
3435
3436 gcc_assert (((struct expr_hash_elt *)p1)->hash
3437 == ((struct expr_hash_elt *)p2)->hash);
3438 return true;
3439 }
3440
3441 return false;
3442 }
3443
3444 /* Given STMT and a pointer to the block local definitions BLOCK_DEFS_P,
3445 register register all objects set by this statement into BLOCK_DEFS_P
3446 and CURRDEFS. */
3447
3448 static void
3449 register_definitions_for_stmt (tree stmt)
3450 {
3451 tree def;
3452 ssa_op_iter iter;
3453
3454 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
3455 {
3456
3457 /* FIXME: We shouldn't be registering new defs if the variable
3458 doesn't need to be renamed. */
3459 register_new_def (def, &block_defs_stack);
3460 }
3461 }
3462