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