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