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