]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-ssa-dom.c
genattrtab.c (write_header): Include hash-set.h...
[thirdparty/gcc.git] / gcc / tree-ssa-dom.c
CommitLineData
6de9cd9a 1/* SSA Dominator optimizations for trees
5624e564 2 Copyright (C) 2001-2015 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
9dcd6f09 9the Free Software Foundation; either version 3, or (at your option)
6de9cd9a
DN
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
9dcd6f09
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
6de9cd9a
DN
20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
4a8fb1a1 24#include "hash-table.h"
6de9cd9a 25#include "tm.h"
40e23961
MC
26#include "hash-set.h"
27#include "machmode.h"
28#include "vec.h"
29#include "double-int.h"
30#include "input.h"
31#include "alias.h"
32#include "symtab.h"
33#include "wide-int.h"
34#include "inchash.h"
35#include "real.h"
6de9cd9a 36#include "tree.h"
40e23961 37#include "fold-const.h"
d8a2d370 38#include "stor-layout.h"
6de9cd9a 39#include "flags.h"
6de9cd9a 40#include "tm_p.h"
60393bbc 41#include "predict.h"
83685514
AM
42#include "hard-reg-set.h"
43#include "input.h"
6de9cd9a 44#include "function.h"
60393bbc
AM
45#include "dominance.h"
46#include "cfg.h"
47#include "cfganal.h"
48#include "basic-block.h"
49#include "cfgloop.h"
50#include "inchash.h"
cf835838 51#include "gimple-pretty-print.h"
2fb9a547
AM
52#include "tree-ssa-alias.h"
53#include "internal-fn.h"
54#include "gimple-fold.h"
55#include "tree-eh.h"
56#include "gimple-expr.h"
57#include "is-a.h"
442b4905 58#include "gimple.h"
5be5c238 59#include "gimple-iterator.h"
442b4905
AM
60#include "gimple-ssa.h"
61#include "tree-cfg.h"
62#include "tree-phinodes.h"
63#include "ssa-iterators.h"
d8a2d370 64#include "stringpool.h"
442b4905
AM
65#include "tree-ssanames.h"
66#include "tree-into-ssa.h"
6de9cd9a 67#include "domwalk.h"
6de9cd9a 68#include "tree-pass.h"
c7f90219 69#include "tree-ssa-propagate.h"
5254eac4 70#include "tree-ssa-threadupdate.h"
6de9cd9a 71#include "langhooks.h"
43f31be5 72#include "params.h"
4484a35a
AM
73#include "tree-ssa-threadedge.h"
74#include "tree-ssa-dom.h"
2bc10537 75#include "inchash.h"
b00734df 76#include "gimplify.h"
6de9cd9a
DN
77
78/* This file implements optimizations on the dominator tree. */
79
726a989a
RB
80/* Representation of a "naked" right-hand-side expression, to be used
81 in recording available expressions in the expression hash table. */
82
83enum expr_kind
84{
85 EXPR_SINGLE,
86 EXPR_UNARY,
87 EXPR_BINARY,
0354c0c7 88 EXPR_TERNARY,
13a3e5b6
BS
89 EXPR_CALL,
90 EXPR_PHI
726a989a
RB
91};
92
93struct hashable_expr
94{
95 tree type;
96 enum expr_kind kind;
97 union {
98 struct { tree rhs; } single;
99 struct { enum tree_code op; tree opnd; } unary;
0354c0c7
BS
100 struct { enum tree_code op; tree opnd0, opnd1; } binary;
101 struct { enum tree_code op; tree opnd0, opnd1, opnd2; } ternary;
538dd0b7 102 struct { gcall *fn_from; bool pure; size_t nargs; tree *args; } call;
13a3e5b6 103 struct { size_t nargs; tree *args; } phi;
726a989a
RB
104 } ops;
105};
106
107/* Structure for recording known values of a conditional expression
108 at the exits from its block. */
109
fd4a760e 110typedef struct cond_equivalence_s
726a989a
RB
111{
112 struct hashable_expr cond;
113 tree value;
fd4a760e
RG
114} cond_equivalence;
115
efea75f9
JL
116
117/* Structure for recording edge equivalences as well as any pending
118 edge redirections during the dominator optimizer.
119
120 Computing and storing the edge equivalences instead of creating
121 them on-demand can save significant amounts of time, particularly
b8698a0f 122 for pathological cases involving switch statements.
efea75f9
JL
123
124 These structures live for a single iteration of the dominator
125 optimizer in the edge's AUX field. At the end of an iteration we
126 free each of these structures and update the AUX field to point
127 to any requested redirection target (the code for updating the
128 CFG and SSA graph for edge redirection expects redirection edge
129 targets to be in the AUX field for each edge. */
130
131struct edge_info
132{
133 /* If this edge creates a simple equivalence, the LHS and RHS of
134 the equivalence will be stored here. */
135 tree lhs;
136 tree rhs;
137
138 /* Traversing an edge may also indicate one or more particular conditions
fd4a760e 139 are true or false. */
9771b263 140 vec<cond_equivalence> cond_equivalences;
efea75f9
JL
141};
142
48732f23
JL
143/* Stack of available expressions in AVAIL_EXPRs. Each block pushes any
144 expressions it enters into the hash table along with a marker entry
b3a27618 145 (null). When we finish processing the block, we pop off entries and
48732f23
JL
146 remove the expressions from the global hash table until we hit the
147 marker. */
726a989a 148typedef struct expr_hash_elt * expr_hash_elt_t;
726a989a 149
b00734df 150static vec<std::pair<expr_hash_elt_t, expr_hash_elt_t> > avail_exprs_stack;
48732f23 151
726a989a 152/* Structure for entries in the expression hash table. */
56b043c8 153
6de9cd9a
DN
154struct expr_hash_elt
155{
156 /* The value (lhs) of this expression. */
157 tree lhs;
158
159 /* The expression (rhs) we want to record. */
726a989a 160 struct hashable_expr expr;
6de9cd9a 161
b00734df
RB
162 /* The virtual operand associated with the nearest dominating stmt
163 loading from or storing to expr. */
164 tree vop;
6de9cd9a 165
726a989a 166 /* The hash value for RHS. */
6de9cd9a 167 hashval_t hash;
726a989a
RB
168
169 /* A unique stamp, typically the address of the hash
170 element itself, used in removing entries from the table. */
171 struct expr_hash_elt *stamp;
6de9cd9a
DN
172};
173
4a8fb1a1
LC
174/* Hashtable helpers. */
175
176static bool hashable_expr_equal_p (const struct hashable_expr *,
177 const struct hashable_expr *);
178static void free_expr_hash_elt (void *);
179
180struct expr_elt_hasher
181{
84baa4b9
TS
182 typedef expr_hash_elt *value_type;
183 typedef expr_hash_elt *compare_type;
184 typedef int store_values_directly;
185 static inline hashval_t hash (const value_type &);
186 static inline bool equal (const value_type &, const compare_type &);
187 static inline void remove (value_type &);
4a8fb1a1
LC
188};
189
190inline hashval_t
84baa4b9 191expr_elt_hasher::hash (const value_type &p)
4a8fb1a1
LC
192{
193 return p->hash;
194}
195
196inline bool
84baa4b9 197expr_elt_hasher::equal (const value_type &p1, const compare_type &p2)
4a8fb1a1 198{
4a8fb1a1
LC
199 const struct hashable_expr *expr1 = &p1->expr;
200 const struct expr_hash_elt *stamp1 = p1->stamp;
4a8fb1a1
LC
201 const struct hashable_expr *expr2 = &p2->expr;
202 const struct expr_hash_elt *stamp2 = p2->stamp;
203
204 /* This case should apply only when removing entries from the table. */
205 if (stamp1 == stamp2)
206 return true;
207
b00734df 208 if (p1->hash != p2->hash)
4a8fb1a1
LC
209 return false;
210
211 /* In case of a collision, both RHS have to be identical and have the
212 same VUSE operands. */
213 if (hashable_expr_equal_p (expr1, expr2)
214 && types_compatible_p (expr1->type, expr2->type))
b00734df 215 return true;
4a8fb1a1
LC
216
217 return false;
218}
219
220/* Delete an expr_hash_elt and reclaim its storage. */
221
222inline void
84baa4b9 223expr_elt_hasher::remove (value_type &element)
4a8fb1a1
LC
224{
225 free_expr_hash_elt (element);
226}
227
228/* Hash table with expressions made available during the renaming process.
229 When an assignment of the form X_i = EXPR is found, the statement is
230 stored in this table. If the same expression EXPR is later found on the
231 RHS of another statement, it is replaced with X_i (thus performing
232 global redundancy elimination). Similarly as we pass through conditionals
233 we record the conditional itself as having either a true or false value
234 in this table. */
c203e8a7 235static hash_table<expr_elt_hasher> *avail_exprs;
4a8fb1a1 236
b5fefcf6
JL
237/* Stack of dest,src pairs that need to be restored during finalization.
238
239 A NULL entry is used to mark the end of pairs which need to be
240 restored during finalization of this block. */
9771b263 241static vec<tree> const_and_copies_stack;
b5fefcf6 242
6de9cd9a
DN
243/* Track whether or not we have changed the control flow graph. */
244static bool cfg_altered;
245
1eaba2f2 246/* Bitmap of blocks that have had EH statements cleaned. We should
f6fe65dc 247 remove their dead edges eventually. */
1eaba2f2
RH
248static bitmap need_eh_cleanup;
249
6de9cd9a
DN
250/* Statistics for dominator optimizations. */
251struct opt_stats_d
252{
253 long num_stmts;
254 long num_exprs_considered;
255 long num_re;
0bca51f0
DN
256 long num_const_prop;
257 long num_copy_prop;
6de9cd9a
DN
258};
259
23530866
JL
260static struct opt_stats_d opt_stats;
261
6de9cd9a 262/* Local functions. */
ccf5c864 263static void optimize_stmt (basic_block, gimple_stmt_iterator);
726a989a 264static tree lookup_avail_expr (gimple, bool);
6de9cd9a 265static hashval_t avail_expr_hash (const void *);
c203e8a7
TS
266static void htab_statistics (FILE *,
267 const hash_table<expr_elt_hasher> &);
fd4a760e 268static void record_cond (cond_equivalence *);
b5fefcf6
JL
269static void record_const_or_copy (tree, tree);
270static void record_equality (tree, tree);
efea75f9
JL
271static void record_equivalences_from_phis (basic_block);
272static void record_equivalences_from_incoming_edge (basic_block);
87c93592 273static void eliminate_redundant_computations (gimple_stmt_iterator *);
726a989a 274static void record_equivalences_from_stmt (gimple, int);
48732f23 275static void remove_local_expressions_from_table (void);
b5fefcf6 276static void restore_vars_to_original_value (void);
28c008bb 277static edge single_incoming_edge_ignoring_loop_edges (basic_block);
6de9cd9a 278
0bca51f0 279
726a989a
RB
280/* Given a statement STMT, initialize the hash table element pointed to
281 by ELEMENT. */
282
283static void
284initialize_hash_element (gimple stmt, tree lhs,
285 struct expr_hash_elt *element)
286{
287 enum gimple_code code = gimple_code (stmt);
288 struct hashable_expr *expr = &element->expr;
289
290 if (code == GIMPLE_ASSIGN)
291 {
292 enum tree_code subcode = gimple_assign_rhs_code (stmt);
293
726a989a
RB
294 switch (get_gimple_rhs_class (subcode))
295 {
296 case GIMPLE_SINGLE_RHS:
0354c0c7 297 expr->kind = EXPR_SINGLE;
e2c9010a 298 expr->type = TREE_TYPE (gimple_assign_rhs1 (stmt));
0354c0c7
BS
299 expr->ops.single.rhs = gimple_assign_rhs1 (stmt);
300 break;
726a989a 301 case GIMPLE_UNARY_RHS:
0354c0c7 302 expr->kind = EXPR_UNARY;
726a989a 303 expr->type = TREE_TYPE (gimple_assign_lhs (stmt));
d822570f 304 if (CONVERT_EXPR_CODE_P (subcode))
625a9766 305 subcode = NOP_EXPR;
0354c0c7
BS
306 expr->ops.unary.op = subcode;
307 expr->ops.unary.opnd = gimple_assign_rhs1 (stmt);
308 break;
726a989a 309 case GIMPLE_BINARY_RHS:
0354c0c7 310 expr->kind = EXPR_BINARY;
726a989a 311 expr->type = TREE_TYPE (gimple_assign_lhs (stmt));
0354c0c7
BS
312 expr->ops.binary.op = subcode;
313 expr->ops.binary.opnd0 = gimple_assign_rhs1 (stmt);
314 expr->ops.binary.opnd1 = gimple_assign_rhs2 (stmt);
315 break;
316 case GIMPLE_TERNARY_RHS:
317 expr->kind = EXPR_TERNARY;
318 expr->type = TREE_TYPE (gimple_assign_lhs (stmt));
319 expr->ops.ternary.op = subcode;
320 expr->ops.ternary.opnd0 = gimple_assign_rhs1 (stmt);
321 expr->ops.ternary.opnd1 = gimple_assign_rhs2 (stmt);
322 expr->ops.ternary.opnd2 = gimple_assign_rhs3 (stmt);
323 break;
726a989a
RB
324 default:
325 gcc_unreachable ();
326 }
327 }
328 else if (code == GIMPLE_COND)
329 {
330 expr->type = boolean_type_node;
331 expr->kind = EXPR_BINARY;
332 expr->ops.binary.op = gimple_cond_code (stmt);
333 expr->ops.binary.opnd0 = gimple_cond_lhs (stmt);
334 expr->ops.binary.opnd1 = gimple_cond_rhs (stmt);
335 }
538dd0b7 336 else if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
726a989a 337 {
538dd0b7 338 size_t nargs = gimple_call_num_args (call_stmt);
726a989a
RB
339 size_t i;
340
538dd0b7 341 gcc_assert (gimple_call_lhs (call_stmt));
726a989a 342
538dd0b7 343 expr->type = TREE_TYPE (gimple_call_lhs (call_stmt));
726a989a 344 expr->kind = EXPR_CALL;
538dd0b7 345 expr->ops.call.fn_from = call_stmt;
726a989a 346
538dd0b7 347 if (gimple_call_flags (call_stmt) & (ECF_CONST | ECF_PURE))
726a989a 348 expr->ops.call.pure = true;
b8698a0f 349 else
726a989a
RB
350 expr->ops.call.pure = false;
351
352 expr->ops.call.nargs = nargs;
13a3e5b6 353 expr->ops.call.args = XCNEWVEC (tree, nargs);
726a989a 354 for (i = 0; i < nargs; i++)
538dd0b7 355 expr->ops.call.args[i] = gimple_call_arg (call_stmt, i);
726a989a 356 }
538dd0b7 357 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
726a989a 358 {
538dd0b7 359 expr->type = TREE_TYPE (gimple_switch_index (swtch_stmt));
726a989a 360 expr->kind = EXPR_SINGLE;
538dd0b7 361 expr->ops.single.rhs = gimple_switch_index (swtch_stmt);
726a989a
RB
362 }
363 else if (code == GIMPLE_GOTO)
364 {
365 expr->type = TREE_TYPE (gimple_goto_dest (stmt));
366 expr->kind = EXPR_SINGLE;
367 expr->ops.single.rhs = gimple_goto_dest (stmt);
368 }
13a3e5b6
BS
369 else if (code == GIMPLE_PHI)
370 {
371 size_t nargs = gimple_phi_num_args (stmt);
372 size_t i;
373
374 expr->type = TREE_TYPE (gimple_phi_result (stmt));
375 expr->kind = EXPR_PHI;
376 expr->ops.phi.nargs = nargs;
377 expr->ops.phi.args = XCNEWVEC (tree, nargs);
378
379 for (i = 0; i < nargs; i++)
380 expr->ops.phi.args[i] = gimple_phi_arg_def (stmt, i);
381 }
726a989a
RB
382 else
383 gcc_unreachable ();
384
385 element->lhs = lhs;
b00734df 386 element->vop = gimple_vuse (stmt);
726a989a
RB
387 element->hash = avail_expr_hash (element);
388 element->stamp = element;
389}
390
391/* Given a conditional expression COND as a tree, initialize
392 a hashable_expr expression EXPR. The conditional must be a
393 comparison or logical negation. A constant or a variable is
394 not permitted. */
395
396static void
397initialize_expr_from_cond (tree cond, struct hashable_expr *expr)
398{
399 expr->type = boolean_type_node;
b8698a0f 400
726a989a
RB
401 if (COMPARISON_CLASS_P (cond))
402 {
403 expr->kind = EXPR_BINARY;
404 expr->ops.binary.op = TREE_CODE (cond);
405 expr->ops.binary.opnd0 = TREE_OPERAND (cond, 0);
406 expr->ops.binary.opnd1 = TREE_OPERAND (cond, 1);
407 }
408 else if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
409 {
410 expr->kind = EXPR_UNARY;
411 expr->ops.unary.op = TRUTH_NOT_EXPR;
412 expr->ops.unary.opnd = TREE_OPERAND (cond, 0);
413 }
414 else
415 gcc_unreachable ();
416}
417
418/* Given a hashable_expr expression EXPR and an LHS,
419 initialize the hash table element pointed to by ELEMENT. */
420
421static void
422initialize_hash_element_from_expr (struct hashable_expr *expr,
423 tree lhs,
424 struct expr_hash_elt *element)
425{
426 element->expr = *expr;
427 element->lhs = lhs;
b00734df 428 element->vop = NULL_TREE;
726a989a
RB
429 element->hash = avail_expr_hash (element);
430 element->stamp = element;
431}
432
433/* Compare two hashable_expr structures for equivalence.
434 They are considered equivalent when the the expressions
435 they denote must necessarily be equal. The logic is intended
436 to follow that of operand_equal_p in fold-const.c */
437
438static bool
439hashable_expr_equal_p (const struct hashable_expr *expr0,
440 const struct hashable_expr *expr1)
441{
442 tree type0 = expr0->type;
443 tree type1 = expr1->type;
444
445 /* If either type is NULL, there is nothing to check. */
446 if ((type0 == NULL_TREE) ^ (type1 == NULL_TREE))
447 return false;
448
449 /* If both types don't have the same signedness, precision, and mode,
450 then we can't consider them equal. */
451 if (type0 != type1
452 && (TREE_CODE (type0) == ERROR_MARK
453 || TREE_CODE (type1) == ERROR_MARK
454 || TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)
455 || TYPE_PRECISION (type0) != TYPE_PRECISION (type1)
456 || TYPE_MODE (type0) != TYPE_MODE (type1)))
457 return false;
458
459 if (expr0->kind != expr1->kind)
460 return false;
461
462 switch (expr0->kind)
463 {
464 case EXPR_SINGLE:
465 return operand_equal_p (expr0->ops.single.rhs,
466 expr1->ops.single.rhs, 0);
467
468 case EXPR_UNARY:
469 if (expr0->ops.unary.op != expr1->ops.unary.op)
470 return false;
471
1a87cf0c 472 if ((CONVERT_EXPR_CODE_P (expr0->ops.unary.op)
726a989a
RB
473 || expr0->ops.unary.op == NON_LVALUE_EXPR)
474 && TYPE_UNSIGNED (expr0->type) != TYPE_UNSIGNED (expr1->type))
475 return false;
476
477 return operand_equal_p (expr0->ops.unary.opnd,
478 expr1->ops.unary.opnd, 0);
479
480 case EXPR_BINARY:
0354c0c7
BS
481 if (expr0->ops.binary.op != expr1->ops.binary.op)
482 return false;
483
484 if (operand_equal_p (expr0->ops.binary.opnd0,
485 expr1->ops.binary.opnd0, 0)
486 && operand_equal_p (expr0->ops.binary.opnd1,
487 expr1->ops.binary.opnd1, 0))
488 return true;
489
490 /* For commutative ops, allow the other order. */
491 return (commutative_tree_code (expr0->ops.binary.op)
492 && operand_equal_p (expr0->ops.binary.opnd0,
493 expr1->ops.binary.opnd1, 0)
494 && operand_equal_p (expr0->ops.binary.opnd1,
495 expr1->ops.binary.opnd0, 0));
496
497 case EXPR_TERNARY:
498 if (expr0->ops.ternary.op != expr1->ops.ternary.op
499 || !operand_equal_p (expr0->ops.ternary.opnd2,
500 expr1->ops.ternary.opnd2, 0))
501 return false;
502
503 if (operand_equal_p (expr0->ops.ternary.opnd0,
504 expr1->ops.ternary.opnd0, 0)
505 && operand_equal_p (expr0->ops.ternary.opnd1,
506 expr1->ops.ternary.opnd1, 0))
507 return true;
508
509 /* For commutative ops, allow the other order. */
510 return (commutative_ternary_tree_code (expr0->ops.ternary.op)
511 && operand_equal_p (expr0->ops.ternary.opnd0,
512 expr1->ops.ternary.opnd1, 0)
513 && operand_equal_p (expr0->ops.ternary.opnd1,
514 expr1->ops.ternary.opnd0, 0));
726a989a
RB
515
516 case EXPR_CALL:
517 {
518 size_t i;
519
520 /* If the calls are to different functions, then they
521 clearly cannot be equal. */
25583c4f
RS
522 if (!gimple_call_same_target_p (expr0->ops.call.fn_from,
523 expr1->ops.call.fn_from))
726a989a
RB
524 return false;
525
526 if (! expr0->ops.call.pure)
527 return false;
528
529 if (expr0->ops.call.nargs != expr1->ops.call.nargs)
530 return false;
531
532 for (i = 0; i < expr0->ops.call.nargs; i++)
533 if (! operand_equal_p (expr0->ops.call.args[i],
534 expr1->ops.call.args[i], 0))
535 return false;
536
7eab31ed
EB
537 if (stmt_could_throw_p (expr0->ops.call.fn_from))
538 {
539 int lp0 = lookup_stmt_eh_lp (expr0->ops.call.fn_from);
540 int lp1 = lookup_stmt_eh_lp (expr1->ops.call.fn_from);
541 if ((lp0 > 0 || lp1 > 0) && lp0 != lp1)
542 return false;
543 }
544
726a989a
RB
545 return true;
546 }
b8698a0f 547
13a3e5b6
BS
548 case EXPR_PHI:
549 {
550 size_t i;
551
552 if (expr0->ops.phi.nargs != expr1->ops.phi.nargs)
553 return false;
554
555 for (i = 0; i < expr0->ops.phi.nargs; i++)
556 if (! operand_equal_p (expr0->ops.phi.args[i],
557 expr1->ops.phi.args[i], 0))
558 return false;
559
560 return true;
561 }
562
726a989a
RB
563 default:
564 gcc_unreachable ();
565 }
566}
567
862d0b35 568/* Generate a hash value for a pair of expressions. This can be used
2bc10537 569 iteratively by passing a previous result in HSTATE.
862d0b35
DN
570
571 The same hash value is always returned for a given pair of expressions,
572 regardless of the order in which they are presented. This is useful in
573 hashing the operands of commutative functions. */
574
2bc10537 575namespace inchash
862d0b35 576{
862d0b35 577
2bc10537
AK
578static void
579add_expr_commutative (const_tree t1, const_tree t2, hash &hstate)
580{
581 hash one, two;
862d0b35 582
2bc10537
AK
583 inchash::add_expr (t1, one);
584 inchash::add_expr (t2, two);
585 hstate.add_commutative (one, two);
862d0b35
DN
586}
587
726a989a
RB
588/* Compute a hash value for a hashable_expr value EXPR and a
589 previously accumulated hash value VAL. If two hashable_expr
590 values compare equal with hashable_expr_equal_p, they must
591 hash to the same value, given an identical value of VAL.
2bc10537 592 The logic is intended to follow inchash::add_expr in tree.c. */
726a989a 593
2bc10537
AK
594static void
595add_hashable_expr (const struct hashable_expr *expr, hash &hstate)
726a989a
RB
596{
597 switch (expr->kind)
598 {
599 case EXPR_SINGLE:
2bc10537 600 inchash::add_expr (expr->ops.single.rhs, hstate);
726a989a
RB
601 break;
602
603 case EXPR_UNARY:
2bc10537 604 hstate.add_object (expr->ops.unary.op);
726a989a
RB
605
606 /* Make sure to include signedness in the hash computation.
607 Don't hash the type, that can lead to having nodes which
608 compare equal according to operand_equal_p, but which
609 have different hash codes. */
1a87cf0c 610 if (CONVERT_EXPR_CODE_P (expr->ops.unary.op)
726a989a 611 || expr->ops.unary.op == NON_LVALUE_EXPR)
2bc10537 612 hstate.add_int (TYPE_UNSIGNED (expr->type));
726a989a 613
2bc10537 614 inchash::add_expr (expr->ops.unary.opnd, hstate);
726a989a
RB
615 break;
616
617 case EXPR_BINARY:
2bc10537 618 hstate.add_object (expr->ops.binary.op);
726a989a 619 if (commutative_tree_code (expr->ops.binary.op))
2bc10537
AK
620 inchash::add_expr_commutative (expr->ops.binary.opnd0,
621 expr->ops.binary.opnd1, hstate);
726a989a
RB
622 else
623 {
2bc10537
AK
624 inchash::add_expr (expr->ops.binary.opnd0, hstate);
625 inchash::add_expr (expr->ops.binary.opnd1, hstate);
726a989a
RB
626 }
627 break;
628
0354c0c7 629 case EXPR_TERNARY:
2bc10537 630 hstate.add_object (expr->ops.ternary.op);
0354c0c7 631 if (commutative_ternary_tree_code (expr->ops.ternary.op))
2bc10537
AK
632 inchash::add_expr_commutative (expr->ops.ternary.opnd0,
633 expr->ops.ternary.opnd1, hstate);
0354c0c7
BS
634 else
635 {
2bc10537
AK
636 inchash::add_expr (expr->ops.ternary.opnd0, hstate);
637 inchash::add_expr (expr->ops.ternary.opnd1, hstate);
0354c0c7 638 }
2bc10537 639 inchash::add_expr (expr->ops.ternary.opnd2, hstate);
0354c0c7
BS
640 break;
641
726a989a
RB
642 case EXPR_CALL:
643 {
644 size_t i;
645 enum tree_code code = CALL_EXPR;
538dd0b7 646 gcall *fn_from;
726a989a 647
2bc10537 648 hstate.add_object (code);
25583c4f
RS
649 fn_from = expr->ops.call.fn_from;
650 if (gimple_call_internal_p (fn_from))
2bc10537 651 hstate.merge_hash ((hashval_t) gimple_call_internal_fn (fn_from));
25583c4f 652 else
2bc10537 653 inchash::add_expr (gimple_call_fn (fn_from), hstate);
726a989a 654 for (i = 0; i < expr->ops.call.nargs; i++)
2bc10537 655 inchash::add_expr (expr->ops.call.args[i], hstate);
726a989a
RB
656 }
657 break;
b8698a0f 658
13a3e5b6
BS
659 case EXPR_PHI:
660 {
661 size_t i;
662
663 for (i = 0; i < expr->ops.phi.nargs; i++)
2bc10537 664 inchash::add_expr (expr->ops.phi.args[i], hstate);
13a3e5b6
BS
665 }
666 break;
667
726a989a
RB
668 default:
669 gcc_unreachable ();
670 }
2bc10537 671}
726a989a 672
726a989a
RB
673}
674
675/* Print a diagnostic dump of an expression hash table entry. */
676
677static void
678print_expr_hash_elt (FILE * stream, const struct expr_hash_elt *element)
679{
b00734df 680 fprintf (stream, "STMT ");
726a989a
RB
681
682 if (element->lhs)
683 {
684 print_generic_expr (stream, element->lhs, 0);
685 fprintf (stream, " = ");
686 }
b8698a0f 687
726a989a
RB
688 switch (element->expr.kind)
689 {
690 case EXPR_SINGLE:
691 print_generic_expr (stream, element->expr.ops.single.rhs, 0);
692 break;
693
694 case EXPR_UNARY:
5806f481 695 fprintf (stream, "%s ", get_tree_code_name (element->expr.ops.unary.op));
726a989a
RB
696 print_generic_expr (stream, element->expr.ops.unary.opnd, 0);
697 break;
698
699 case EXPR_BINARY:
700 print_generic_expr (stream, element->expr.ops.binary.opnd0, 0);
5806f481 701 fprintf (stream, " %s ", get_tree_code_name (element->expr.ops.binary.op));
726a989a
RB
702 print_generic_expr (stream, element->expr.ops.binary.opnd1, 0);
703 break;
704
0354c0c7 705 case EXPR_TERNARY:
5806f481 706 fprintf (stream, " %s <", get_tree_code_name (element->expr.ops.ternary.op));
0354c0c7
BS
707 print_generic_expr (stream, element->expr.ops.ternary.opnd0, 0);
708 fputs (", ", stream);
709 print_generic_expr (stream, element->expr.ops.ternary.opnd1, 0);
710 fputs (", ", stream);
711 print_generic_expr (stream, element->expr.ops.ternary.opnd2, 0);
712 fputs (">", stream);
713 break;
714
726a989a
RB
715 case EXPR_CALL:
716 {
717 size_t i;
718 size_t nargs = element->expr.ops.call.nargs;
538dd0b7 719 gcall *fn_from;
25583c4f
RS
720
721 fn_from = element->expr.ops.call.fn_from;
722 if (gimple_call_internal_p (fn_from))
723 fputs (internal_fn_name (gimple_call_internal_fn (fn_from)),
724 stream);
725 else
726 print_generic_expr (stream, gimple_call_fn (fn_from), 0);
726a989a
RB
727 fprintf (stream, " (");
728 for (i = 0; i < nargs; i++)
729 {
730 print_generic_expr (stream, element->expr.ops.call.args[i], 0);
731 if (i + 1 < nargs)
732 fprintf (stream, ", ");
733 }
734 fprintf (stream, ")");
735 }
736 break;
13a3e5b6
BS
737
738 case EXPR_PHI:
739 {
740 size_t i;
741 size_t nargs = element->expr.ops.phi.nargs;
742
743 fprintf (stream, "PHI <");
744 for (i = 0; i < nargs; i++)
745 {
746 print_generic_expr (stream, element->expr.ops.phi.args[i], 0);
747 if (i + 1 < nargs)
748 fprintf (stream, ", ");
749 }
750 fprintf (stream, ">");
751 }
752 break;
726a989a 753 }
726a989a 754
b00734df 755 if (element->vop)
726a989a 756 {
b00734df
RB
757 fprintf (stream, " with ");
758 print_generic_expr (stream, element->vop, 0);
726a989a 759 }
b00734df
RB
760
761 fprintf (stream, "\n");
726a989a
RB
762}
763
b6db991c 764/* Delete variable sized pieces of the expr_hash_elt ELEMENT. */
726a989a
RB
765
766static void
b6db991c 767free_expr_hash_elt_contents (struct expr_hash_elt *element)
726a989a 768{
726a989a
RB
769 if (element->expr.kind == EXPR_CALL)
770 free (element->expr.ops.call.args);
b6db991c 771 else if (element->expr.kind == EXPR_PHI)
13a3e5b6 772 free (element->expr.ops.phi.args);
b6db991c
RG
773}
774
775/* Delete an expr_hash_elt and reclaim its storage. */
13a3e5b6 776
b6db991c
RG
777static void
778free_expr_hash_elt (void *elt)
779{
780 struct expr_hash_elt *element = ((struct expr_hash_elt *)elt);
781 free_expr_hash_elt_contents (element);
726a989a
RB
782 free (element);
783}
784
efea75f9
JL
785/* Allocate an EDGE_INFO for edge E and attach it to E.
786 Return the new EDGE_INFO structure. */
787
788static struct edge_info *
789allocate_edge_info (edge e)
790{
791 struct edge_info *edge_info;
792
e1111e8e 793 edge_info = XCNEW (struct edge_info);
efea75f9
JL
794
795 e->aux = edge_info;
796 return edge_info;
797}
798
799/* Free all EDGE_INFO structures associated with edges in the CFG.
cbb1cada 800 If a particular edge can be threaded, copy the redirection
efea75f9
JL
801 target from the EDGE_INFO structure into the edge's AUX field
802 as required by code to update the CFG and SSA graph for
803 jump threading. */
804
805static void
806free_all_edge_infos (void)
807{
808 basic_block bb;
809 edge_iterator ei;
810 edge e;
811
11cd3bed 812 FOR_EACH_BB_FN (bb, cfun)
efea75f9
JL
813 {
814 FOR_EACH_EDGE (e, ei, bb->preds)
815 {
e1111e8e 816 struct edge_info *edge_info = (struct edge_info *) e->aux;
efea75f9
JL
817
818 if (edge_info)
819 {
9771b263 820 edge_info->cond_equivalences.release ();
efea75f9 821 free (edge_info);
8702a557 822 e->aux = NULL;
efea75f9
JL
823 }
824 }
825 }
826}
827
4d9192b5
TS
828class dom_opt_dom_walker : public dom_walker
829{
830public:
831 dom_opt_dom_walker (cdi_direction direction)
65d3284b 832 : dom_walker (direction), m_dummy_cond (NULL) {}
4d9192b5
TS
833
834 virtual void before_dom_children (basic_block);
835 virtual void after_dom_children (basic_block);
836
837private:
838 void thread_across_edge (edge);
839
538dd0b7 840 gcond *m_dummy_cond;
4d9192b5
TS
841};
842
b8698a0f 843/* Jump threading, redundancy elimination and const/copy propagation.
6de9cd9a 844
6de9cd9a
DN
845 This pass may expose new symbols that need to be renamed into SSA. For
846 every new symbol exposed, its corresponding bit will be set in
ff2ad0f7 847 VARS_TO_RENAME. */
6de9cd9a 848
be55bfe6
TS
849namespace {
850
851const pass_data pass_data_dominator =
852{
853 GIMPLE_PASS, /* type */
854 "dom", /* name */
855 OPTGROUP_NONE, /* optinfo_flags */
be55bfe6
TS
856 TV_TREE_SSA_DOMINATOR_OPTS, /* tv_id */
857 ( PROP_cfg | PROP_ssa ), /* properties_required */
858 0, /* properties_provided */
859 0, /* properties_destroyed */
860 0, /* todo_flags_start */
3bea341f 861 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
be55bfe6
TS
862};
863
864class pass_dominator : public gimple_opt_pass
865{
866public:
867 pass_dominator (gcc::context *ctxt)
868 : gimple_opt_pass (pass_data_dominator, ctxt)
869 {}
870
871 /* opt_pass methods: */
872 opt_pass * clone () { return new pass_dominator (m_ctxt); }
873 virtual bool gate (function *) { return flag_tree_dom != 0; }
874 virtual unsigned int execute (function *);
875
876}; // class pass_dominator
877
878unsigned int
879pass_dominator::execute (function *fun)
6de9cd9a 880{
fded8de7
DN
881 memset (&opt_stats, 0, sizeof (opt_stats));
882
6de9cd9a 883 /* Create our hash tables. */
c203e8a7 884 avail_exprs = new hash_table<expr_elt_hasher> (1024);
9771b263
DN
885 avail_exprs_stack.create (20);
886 const_and_copies_stack.create (20);
8bdbfff5 887 need_eh_cleanup = BITMAP_ALLOC (NULL);
6de9cd9a 888
6de9cd9a 889 calculate_dominance_info (CDI_DOMINATORS);
8d9d6561 890 cfg_altered = false;
6de9cd9a 891
b02b9b53
ZD
892 /* We need to know loop structures in order to avoid destroying them
893 in jump threading. Note that we still can e.g. thread through loop
894 headers to an exit edge, or through loop header to the loop body, assuming
a3afdbb8
BC
895 that we update the loop info.
896
897 TODO: We don't need to set LOOPS_HAVE_PREHEADERS generally, but due
898 to several overly conservative bail-outs in jump threading, case
899 gcc.dg/tree-ssa/pr21417.c can't be threaded if loop preheader is
900 missing. We should improve jump threading in future then
901 LOOPS_HAVE_PREHEADERS won't be needed here. */
902 loop_optimizer_init (LOOPS_HAVE_PREHEADERS | LOOPS_HAVE_SIMPLE_LATCHES);
d38ffc55 903
448ee662
RG
904 /* Initialize the value-handle array. */
905 threadedge_initialize_values ();
906
2090d6a0 907 /* We need accurate information regarding back edges in the CFG
fa10beec 908 for jump threading; this may include back edges that are not part of
b02b9b53 909 a single loop. */
2090d6a0 910 mark_dfs_back_edges ();
b8698a0f 911
2090d6a0 912 /* Recursively walk the dominator tree optimizing statements. */
be55bfe6 913 dom_opt_dom_walker (CDI_DOMINATORS).walk (fun->cfg->x_entry_block_ptr);
6de9cd9a 914
2090d6a0 915 {
726a989a 916 gimple_stmt_iterator gsi;
2090d6a0 917 basic_block bb;
be55bfe6 918 FOR_EACH_BB_FN (bb, fun)
a881aaa7
RG
919 {
920 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
726a989a 921 update_stmt_if_modified (gsi_stmt (gsi));
f430bae8 922 }
2090d6a0 923 }
a3b609df 924
2090d6a0
JL
925 /* If we exposed any new variables, go ahead and put them into
926 SSA form now, before we handle jump threading. This simplifies
927 interactions between rewriting of _DECL nodes into SSA form
928 and rewriting SSA_NAME nodes into SSA form after block
929 duplication and CFG manipulation. */
930 update_ssa (TODO_update_ssa);
d38ffc55 931
2090d6a0 932 free_all_edge_infos ();
d38ffc55 933
2090d6a0 934 /* Thread jumps, creating duplicate blocks as needed. */
b02b9b53 935 cfg_altered |= thread_through_all_blocks (first_pass_instance);
6de9cd9a 936
8d9d6561
EB
937 if (cfg_altered)
938 free_dominance_info (CDI_DOMINATORS);
939
2090d6a0
JL
940 /* Removal of statements may make some EH edges dead. Purge
941 such edges from the CFG as needed. */
942 if (!bitmap_empty_p (need_eh_cleanup))
943 {
45a7844f
EB
944 unsigned i;
945 bitmap_iterator bi;
946
947 /* Jump threading may have created forwarder blocks from blocks
948 needing EH cleanup; the new successor of these blocks, which
1ce296cf
JJ
949 has inherited from the original block, needs the cleanup.
950 Don't clear bits in the bitmap, as that can break the bitmap
951 iterator. */
45a7844f
EB
952 EXECUTE_IF_SET_IN_BITMAP (need_eh_cleanup, 0, i, bi)
953 {
be55bfe6 954 basic_block bb = BASIC_BLOCK_FOR_FN (fun, i);
1ce296cf
JJ
955 if (bb == NULL)
956 continue;
957 while (single_succ_p (bb)
958 && (single_succ_edge (bb)->flags & EDGE_EH) == 0)
959 bb = single_succ (bb);
be55bfe6 960 if (bb == EXIT_BLOCK_PTR_FOR_FN (fun))
1ce296cf
JJ
961 continue;
962 if ((unsigned) bb->index != i)
963 bitmap_set_bit (need_eh_cleanup, bb->index);
45a7844f
EB
964 }
965
726a989a 966 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
f61e445a 967 bitmap_clear (need_eh_cleanup);
2090d6a0 968 }
6de9cd9a 969
be55bfe6 970 statistics_counter_event (fun, "Redundant expressions eliminated",
01902653 971 opt_stats.num_re);
be55bfe6 972 statistics_counter_event (fun, "Constants propagated",
01902653 973 opt_stats.num_const_prop);
be55bfe6 974 statistics_counter_event (fun, "Copies propagated",
01902653
RG
975 opt_stats.num_copy_prop);
976
6de9cd9a
DN
977 /* Debugging dumps. */
978 if (dump_file && (dump_flags & TDF_STATS))
979 dump_dominator_optimization_stats (dump_file);
980
b02b9b53
ZD
981 loop_optimizer_finalize ();
982
2090d6a0 983 /* Delete our main hashtable. */
c203e8a7
TS
984 delete avail_exprs;
985 avail_exprs = NULL;
6de9cd9a 986
b16caf72 987 /* Free asserted bitmaps and stacks. */
8bdbfff5 988 BITMAP_FREE (need_eh_cleanup);
b8698a0f 989
9771b263
DN
990 avail_exprs_stack.release ();
991 const_and_copies_stack.release ();
b8698a0f 992
448ee662
RG
993 /* Free the value-handle array. */
994 threadedge_finalize_values ();
448ee662 995
c2924966 996 return 0;
6de9cd9a
DN
997}
998
27a4cd48
DM
999} // anon namespace
1000
1001gimple_opt_pass *
1002make_pass_dominator (gcc::context *ctxt)
1003{
1004 return new pass_dominator (ctxt);
1005}
1006
6de9cd9a 1007
726a989a
RB
1008/* Given a conditional statement CONDSTMT, convert the
1009 condition to a canonical form. */
0e0ed594
JL
1010
1011static void
538dd0b7 1012canonicalize_comparison (gcond *condstmt)
0e0ed594 1013{
0e0ed594
JL
1014 tree op0;
1015 tree op1;
726a989a 1016 enum tree_code code;
0e0ed594 1017
726a989a 1018 gcc_assert (gimple_code (condstmt) == GIMPLE_COND);
0e0ed594 1019
726a989a
RB
1020 op0 = gimple_cond_lhs (condstmt);
1021 op1 = gimple_cond_rhs (condstmt);
1022
1023 code = gimple_cond_code (condstmt);
0e0ed594
JL
1024
1025 /* If it would be profitable to swap the operands, then do so to
1026 canonicalize the statement, enabling better optimization.
1027
1028 By placing canonicalization of such expressions here we
1029 transparently keep statements in canonical form, even
1030 when the statement is modified. */
1031 if (tree_swap_operands_p (op0, op1, false))
1032 {
1033 /* For relationals we need to swap the operands
1034 and change the code. */
1035 if (code == LT_EXPR
1036 || code == GT_EXPR
1037 || code == LE_EXPR
1038 || code == GE_EXPR)
1039 {
726a989a
RB
1040 code = swap_tree_comparison (code);
1041
1042 gimple_cond_set_code (condstmt, code);
1043 gimple_cond_set_lhs (condstmt, op1);
1044 gimple_cond_set_rhs (condstmt, op0);
1045
1046 update_stmt (condstmt);
0e0ed594
JL
1047 }
1048 }
1049}
6de9cd9a 1050
6de9cd9a
DN
1051/* Initialize local stacks for this optimizer and record equivalences
1052 upon entry to BB. Equivalences can come from the edge traversed to
1053 reach BB or they may come from PHI nodes at the start of BB. */
1054
6de9cd9a
DN
1055/* Remove all the expressions in LOCALS from TABLE, stopping when there are
1056 LIMIT entries left in LOCALs. */
1057
1058static void
48732f23 1059remove_local_expressions_from_table (void)
6de9cd9a 1060{
6de9cd9a 1061 /* Remove all the expressions made available in this block. */
9771b263 1062 while (avail_exprs_stack.length () > 0)
6de9cd9a 1063 {
b00734df
RB
1064 std::pair<expr_hash_elt_t, expr_hash_elt_t> victim
1065 = avail_exprs_stack.pop ();
4a8fb1a1 1066 expr_hash_elt **slot;
48732f23 1067
b00734df 1068 if (victim.first == NULL)
48732f23 1069 break;
6de9cd9a 1070
726a989a
RB
1071 /* This must precede the actual removal from the hash table,
1072 as ELEMENT and the table entry may share a call argument
1073 vector which will be freed during removal. */
1074 if (dump_file && (dump_flags & TDF_DETAILS))
1075 {
1076 fprintf (dump_file, "<<<< ");
b00734df 1077 print_expr_hash_elt (dump_file, victim.first);
726a989a
RB
1078 }
1079
b00734df
RB
1080 slot = avail_exprs->find_slot (victim.first, NO_INSERT);
1081 gcc_assert (slot && *slot == victim.first);
1082 if (victim.second != NULL)
1083 {
1084 free_expr_hash_elt (*slot);
1085 *slot = victim.second;
1086 }
1087 else
1088 avail_exprs->clear_slot (slot);
6de9cd9a
DN
1089 }
1090}
1091
b5fefcf6
JL
1092/* Use the source/dest pairs in CONST_AND_COPIES_STACK to restore
1093 CONST_AND_COPIES to its original state, stopping when we hit a
1094 NULL marker. */
6de9cd9a
DN
1095
1096static void
b5fefcf6 1097restore_vars_to_original_value (void)
6de9cd9a 1098{
9771b263 1099 while (const_and_copies_stack.length () > 0)
6de9cd9a
DN
1100 {
1101 tree prev_value, dest;
1102
9771b263 1103 dest = const_and_copies_stack.pop ();
6de9cd9a 1104
b5fefcf6
JL
1105 if (dest == NULL)
1106 break;
1107
726a989a
RB
1108 if (dump_file && (dump_flags & TDF_DETAILS))
1109 {
1110 fprintf (dump_file, "<<<< COPY ");
1111 print_generic_expr (dump_file, dest, 0);
1112 fprintf (dump_file, " = ");
1113 print_generic_expr (dump_file, SSA_NAME_VALUE (dest), 0);
1114 fprintf (dump_file, "\n");
1115 }
1116
9771b263 1117 prev_value = const_and_copies_stack.pop ();
448ee662 1118 set_ssa_name_value (dest, prev_value);
6de9cd9a
DN
1119 }
1120}
1121
2090d6a0
JL
1122/* A trivial wrapper so that we can present the generic jump
1123 threading code with a simple API for simplifying statements. */
1124static tree
726a989a
RB
1125simplify_stmt_for_jump_threading (gimple stmt,
1126 gimple within_stmt ATTRIBUTE_UNUSED)
2090d6a0
JL
1127{
1128 return lookup_avail_expr (stmt, false);
1129}
1130
925f3871
JL
1131/* Record into the equivalence tables any equivalences implied by
1132 traversing edge E (which are cached in E->aux).
1133
1134 Callers are responsible for managing the unwinding markers. */
83ae86f5
JL
1135static void
1136record_temporary_equivalences (edge e)
1137{
1138 int i;
1139 struct edge_info *edge_info = (struct edge_info *) e->aux;
1140
1141 /* If we have info associated with this edge, record it into
1142 our equivalence tables. */
1143 if (edge_info)
1144 {
1145 cond_equivalence *eq;
1146 tree lhs = edge_info->lhs;
1147 tree rhs = edge_info->rhs;
1148
1149 /* If we have a simple NAME = VALUE equivalence, record it. */
1150 if (lhs && TREE_CODE (lhs) == SSA_NAME)
1151 record_const_or_copy (lhs, rhs);
1152
1153 /* If we have 0 = COND or 1 = COND equivalences, record them
1154 into our expression hash tables. */
1155 for (i = 0; edge_info->cond_equivalences.iterate (i, &eq); ++i)
1156 record_cond (eq);
1157 }
1158}
1159
2090d6a0
JL
1160/* Wrapper for common code to attempt to thread an edge. For example,
1161 it handles lazily building the dummy condition and the bookkeeping
1162 when jump threading is successful. */
1163
4d9192b5
TS
1164void
1165dom_opt_dom_walker::thread_across_edge (edge e)
2090d6a0 1166{
65d3284b
RS
1167 if (! m_dummy_cond)
1168 m_dummy_cond =
726a989a
RB
1169 gimple_build_cond (NE_EXPR,
1170 integer_zero_node, integer_zero_node,
1171 NULL, NULL);
2090d6a0 1172
83ae86f5
JL
1173 /* Push a marker on both stacks so we can unwind the tables back to their
1174 current state. */
b00734df
RB
1175 avail_exprs_stack.safe_push
1176 (std::pair<expr_hash_elt_t, expr_hash_elt_t> (NULL, NULL));
83ae86f5
JL
1177 const_and_copies_stack.safe_push (NULL_TREE);
1178
1179 /* Traversing E may result in equivalences we can utilize. */
1180 record_temporary_equivalences (e);
1181
1182 /* With all the edge equivalences in the tables, go ahead and attempt
1183 to thread through E->dest. */
65d3284b 1184 ::thread_across_edge (m_dummy_cond, e, false,
4d9192b5
TS
1185 &const_and_copies_stack,
1186 simplify_stmt_for_jump_threading);
83ae86f5
JL
1187
1188 /* And restore the various tables to their state before
1189 we threaded this edge.
1190
1191 XXX The code in tree-ssa-threadedge.c will restore the state of
1192 the const_and_copies table. We we just have to restore the expression
1193 table. */
1194 remove_local_expressions_from_table ();
2090d6a0
JL
1195}
1196
6de9cd9a
DN
1197/* PHI nodes can create equivalences too.
1198
1199 Ignoring any alternatives which are the same as the result, if
1200 all the alternatives are equal, then the PHI node creates an
b16caf72 1201 equivalence. */
dd747311 1202
6de9cd9a 1203static void
efea75f9 1204record_equivalences_from_phis (basic_block bb)
6de9cd9a 1205{
538dd0b7 1206 gphi_iterator gsi;
b8698a0f 1207
726a989a 1208 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6de9cd9a 1209 {
538dd0b7 1210 gphi *phi = gsi.phi ();
726a989a
RB
1211
1212 tree lhs = gimple_phi_result (phi);
6de9cd9a 1213 tree rhs = NULL;
726a989a 1214 size_t i;
6de9cd9a 1215
726a989a 1216 for (i = 0; i < gimple_phi_num_args (phi); i++)
6de9cd9a 1217 {
726a989a 1218 tree t = gimple_phi_arg_def (phi, i);
6de9cd9a 1219
6e38fea3
KH
1220 /* Ignore alternatives which are the same as our LHS. Since
1221 LHS is a PHI_RESULT, it is known to be a SSA_NAME, so we
1222 can simply compare pointers. */
073b8140 1223 if (lhs == t)
a18428f3
KH
1224 continue;
1225
1226 /* If we have not processed an alternative yet, then set
1227 RHS to this alternative. */
1228 if (rhs == NULL)
1229 rhs = t;
1230 /* If we have processed an alternative (stored in RHS), then
1231 see if it is equal to this one. If it isn't, then stop
1232 the search. */
1233 else if (! operand_equal_for_phi_arg_p (rhs, t))
6de9cd9a
DN
1234 break;
1235 }
1236
1237 /* If we had no interesting alternatives, then all the RHS alternatives
1238 must have been the same as LHS. */
1239 if (!rhs)
1240 rhs = lhs;
1241
1242 /* If we managed to iterate through each PHI alternative without
1243 breaking out of the loop, then we have a PHI which may create
1244 a useful equivalence. We do not need to record unwind data for
1245 this, since this is a true assignment and not an equivalence
1ea7e6ad 1246 inferred from a comparison. All uses of this ssa name are dominated
6de9cd9a 1247 by this assignment, so unwinding just costs time and space. */
73c7d6bc 1248 if (i == gimple_phi_num_args (phi)
a59d8e8e 1249 && may_propagate_copy (lhs, rhs))
448ee662 1250 set_ssa_name_value (lhs, rhs);
6de9cd9a
DN
1251 }
1252}
1253
28c008bb
JL
1254/* Ignoring loop backedges, if BB has precisely one incoming edge then
1255 return that edge. Otherwise return NULL. */
1256static edge
1257single_incoming_edge_ignoring_loop_edges (basic_block bb)
1258{
1259 edge retval = NULL;
1260 edge e;
628f6a4e 1261 edge_iterator ei;
28c008bb 1262
628f6a4e 1263 FOR_EACH_EDGE (e, ei, bb->preds)
28c008bb
JL
1264 {
1265 /* A loop back edge can be identified by the destination of
1266 the edge dominating the source of the edge. */
1267 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
1268 continue;
1269
1270 /* If we have already seen a non-loop edge, then we must have
1271 multiple incoming non-loop edges and thus we return NULL. */
1272 if (retval)
1273 return NULL;
1274
1275 /* This is the first non-loop incoming edge we have found. Record
1276 it. */
1277 retval = e;
1278 }
1279
1280 return retval;
1281}
1282
6de9cd9a
DN
1283/* Record any equivalences created by the incoming edge to BB. If BB
1284 has more than one incoming edge, then no equivalence is created. */
1285
1286static void
efea75f9 1287record_equivalences_from_incoming_edge (basic_block bb)
6de9cd9a 1288{
efea75f9 1289 edge e;
6de9cd9a 1290 basic_block parent;
efea75f9 1291 struct edge_info *edge_info;
6de9cd9a 1292
35fd3193 1293 /* If our parent block ended with a control statement, then we may be
6de9cd9a
DN
1294 able to record some equivalences based on which outgoing edge from
1295 the parent was followed. */
1296 parent = get_immediate_dominator (CDI_DOMINATORS, bb);
6de9cd9a 1297
efea75f9 1298 e = single_incoming_edge_ignoring_loop_edges (bb);
6de9cd9a 1299
efea75f9
JL
1300 /* If we had a single incoming edge from our parent block, then enter
1301 any data associated with the edge into our tables. */
1302 if (e && e->src == parent)
6de9cd9a 1303 {
efea75f9 1304 unsigned int i;
6de9cd9a 1305
e1111e8e 1306 edge_info = (struct edge_info *) e->aux;
6de9cd9a 1307
efea75f9 1308 if (edge_info)
6de9cd9a 1309 {
efea75f9
JL
1310 tree lhs = edge_info->lhs;
1311 tree rhs = edge_info->rhs;
fd4a760e 1312 cond_equivalence *eq;
efea75f9
JL
1313
1314 if (lhs)
1315 record_equality (lhs, rhs);
1316
39e45653
JL
1317 /* If LHS is an SSA_NAME and RHS is a constant integer and LHS was
1318 set via a widening type conversion, then we may be able to record
4f1f78b9
JL
1319 additional equivalences. */
1320 if (lhs
1321 && TREE_CODE (lhs) == SSA_NAME
39e45653
JL
1322 && is_gimple_constant (rhs)
1323 && TREE_CODE (rhs) == INTEGER_CST)
4f1f78b9
JL
1324 {
1325 gimple defstmt = SSA_NAME_DEF_STMT (lhs);
1326
1327 if (defstmt
1328 && is_gimple_assign (defstmt)
1329 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (defstmt)))
1330 {
1331 tree old_rhs = gimple_assign_rhs1 (defstmt);
39e45653 1332
be672e08
JL
1333 /* If the conversion widens the original value and
1334 the constant is in the range of the type of OLD_RHS,
8d34e421 1335 then convert the constant and record the equivalence.
be672e08
JL
1336
1337 Note that int_fits_type_p does not check the precision
1338 if the upper and lower bounds are OK. */
dd884525 1339 if (INTEGRAL_TYPE_P (TREE_TYPE (old_rhs))
be672e08
JL
1340 && (TYPE_PRECISION (TREE_TYPE (lhs))
1341 > TYPE_PRECISION (TREE_TYPE (old_rhs)))
dd884525 1342 && int_fits_type_p (rhs, TREE_TYPE (old_rhs)))
39e45653
JL
1343 {
1344 tree newval = fold_convert (TREE_TYPE (old_rhs), rhs);
1345 record_equality (old_rhs, newval);
1346 }
4f1f78b9
JL
1347 }
1348 }
1349
9771b263 1350 for (i = 0; edge_info->cond_equivalences.iterate (i, &eq); ++i)
fd4a760e 1351 record_cond (eq);
6de9cd9a
DN
1352 }
1353 }
6de9cd9a
DN
1354}
1355
1356/* Dump SSA statistics on FILE. */
1357
1358void
1359dump_dominator_optimization_stats (FILE *file)
1360{
6de9cd9a
DN
1361 fprintf (file, "Total number of statements: %6ld\n\n",
1362 opt_stats.num_stmts);
1363 fprintf (file, "Exprs considered for dominator optimizations: %6ld\n",
1364 opt_stats.num_exprs_considered);
1365
6de9cd9a
DN
1366 fprintf (file, "\nHash table statistics:\n");
1367
1368 fprintf (file, " avail_exprs: ");
c203e8a7 1369 htab_statistics (file, *avail_exprs);
6de9cd9a
DN
1370}
1371
1372
1373/* Dump SSA statistics on stderr. */
1374
24e47c76 1375DEBUG_FUNCTION void
6de9cd9a
DN
1376debug_dominator_optimization_stats (void)
1377{
1378 dump_dominator_optimization_stats (stderr);
1379}
1380
1381
1382/* Dump statistics for the hash table HTAB. */
1383
1384static void
c203e8a7 1385htab_statistics (FILE *file, const hash_table<expr_elt_hasher> &htab)
6de9cd9a
DN
1386{
1387 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
4a8fb1a1
LC
1388 (long) htab.size (),
1389 (long) htab.elements (),
1390 htab.collisions ());
6de9cd9a
DN
1391}
1392
726a989a
RB
1393
1394/* Enter condition equivalence into the expression hash table.
1395 This indicates that a conditional expression has a known
1396 boolean value. */
6de9cd9a
DN
1397
1398static void
fd4a760e 1399record_cond (cond_equivalence *p)
6de9cd9a 1400{
e1111e8e 1401 struct expr_hash_elt *element = XCNEW (struct expr_hash_elt);
4a8fb1a1 1402 expr_hash_elt **slot;
6de9cd9a 1403
726a989a 1404 initialize_hash_element_from_expr (&p->cond, p->value, element);
6de9cd9a 1405
c203e8a7 1406 slot = avail_exprs->find_slot_with_hash (element, element->hash, INSERT);
6de9cd9a
DN
1407 if (*slot == NULL)
1408 {
4a8fb1a1 1409 *slot = element;
726a989a
RB
1410
1411 if (dump_file && (dump_flags & TDF_DETAILS))
1412 {
1413 fprintf (dump_file, "1>>> ");
1414 print_expr_hash_elt (dump_file, element);
1415 }
1416
b00734df
RB
1417 avail_exprs_stack.safe_push
1418 (std::pair<expr_hash_elt_t, expr_hash_elt_t> (element, NULL));
6de9cd9a
DN
1419 }
1420 else
b6db991c 1421 free_expr_hash_elt (element);
6de9cd9a
DN
1422}
1423
726a989a 1424/* Build a cond_equivalence record indicating that the comparison
fd4a760e 1425 CODE holds between operands OP0 and OP1 and push it to **P. */
b8698a0f 1426
efea75f9 1427static void
726a989a
RB
1428build_and_record_new_cond (enum tree_code code,
1429 tree op0, tree op1,
9771b263 1430 vec<cond_equivalence> *p)
efea75f9 1431{
fd4a760e
RG
1432 cond_equivalence c;
1433 struct hashable_expr *cond = &c.cond;
726a989a
RB
1434
1435 gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
1436
1437 cond->type = boolean_type_node;
1438 cond->kind = EXPR_BINARY;
1439 cond->ops.binary.op = code;
1440 cond->ops.binary.opnd0 = op0;
1441 cond->ops.binary.opnd1 = op1;
1442
fd4a760e 1443 c.value = boolean_true_node;
9771b263 1444 p->safe_push (c);
efea75f9
JL
1445}
1446
1447/* Record that COND is true and INVERTED is false into the edge information
1448 structure. Also record that any conditions dominated by COND are true
1449 as well.
d2d8936f
JL
1450
1451 For example, if a < b is true, then a <= b must also be true. */
1452
1453static void
efea75f9 1454record_conditions (struct edge_info *edge_info, tree cond, tree inverted)
d2d8936f 1455{
efea75f9 1456 tree op0, op1;
fd4a760e 1457 cond_equivalence c;
efea75f9
JL
1458
1459 if (!COMPARISON_CLASS_P (cond))
1460 return;
1461
1462 op0 = TREE_OPERAND (cond, 0);
1463 op1 = TREE_OPERAND (cond, 1);
1464
d2d8936f
JL
1465 switch (TREE_CODE (cond))
1466 {
1467 case LT_EXPR:
d2d8936f 1468 case GT_EXPR:
14b41b5f
RS
1469 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
1470 {
14b41b5f 1471 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
fd4a760e 1472 &edge_info->cond_equivalences);
14b41b5f 1473 build_and_record_new_cond (LTGT_EXPR, op0, op1,
fd4a760e 1474 &edge_info->cond_equivalences);
14b41b5f
RS
1475 }
1476
efea75f9
JL
1477 build_and_record_new_cond ((TREE_CODE (cond) == LT_EXPR
1478 ? LE_EXPR : GE_EXPR),
fd4a760e 1479 op0, op1, &edge_info->cond_equivalences);
efea75f9 1480 build_and_record_new_cond (NE_EXPR, op0, op1,
fd4a760e 1481 &edge_info->cond_equivalences);
d2d8936f
JL
1482 break;
1483
1484 case GE_EXPR:
1485 case LE_EXPR:
14b41b5f
RS
1486 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
1487 {
14b41b5f 1488 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
fd4a760e 1489 &edge_info->cond_equivalences);
14b41b5f 1490 }
d2d8936f
JL
1491 break;
1492
1493 case EQ_EXPR:
14b41b5f
RS
1494 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
1495 {
14b41b5f 1496 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
fd4a760e 1497 &edge_info->cond_equivalences);
14b41b5f 1498 }
efea75f9 1499 build_and_record_new_cond (LE_EXPR, op0, op1,
fd4a760e 1500 &edge_info->cond_equivalences);
efea75f9 1501 build_and_record_new_cond (GE_EXPR, op0, op1,
fd4a760e 1502 &edge_info->cond_equivalences);
d2d8936f
JL
1503 break;
1504
1505 case UNORDERED_EXPR:
efea75f9 1506 build_and_record_new_cond (NE_EXPR, op0, op1,
fd4a760e 1507 &edge_info->cond_equivalences);
efea75f9 1508 build_and_record_new_cond (UNLE_EXPR, op0, op1,
fd4a760e 1509 &edge_info->cond_equivalences);
efea75f9 1510 build_and_record_new_cond (UNGE_EXPR, op0, op1,
fd4a760e 1511 &edge_info->cond_equivalences);
efea75f9 1512 build_and_record_new_cond (UNEQ_EXPR, op0, op1,
fd4a760e 1513 &edge_info->cond_equivalences);
efea75f9 1514 build_and_record_new_cond (UNLT_EXPR, op0, op1,
fd4a760e 1515 &edge_info->cond_equivalences);
efea75f9 1516 build_and_record_new_cond (UNGT_EXPR, op0, op1,
fd4a760e 1517 &edge_info->cond_equivalences);
d2d8936f
JL
1518 break;
1519
1520 case UNLT_EXPR:
d2d8936f 1521 case UNGT_EXPR:
efea75f9
JL
1522 build_and_record_new_cond ((TREE_CODE (cond) == UNLT_EXPR
1523 ? UNLE_EXPR : UNGE_EXPR),
fd4a760e 1524 op0, op1, &edge_info->cond_equivalences);
efea75f9 1525 build_and_record_new_cond (NE_EXPR, op0, op1,
fd4a760e 1526 &edge_info->cond_equivalences);
d2d8936f
JL
1527 break;
1528
1529 case UNEQ_EXPR:
efea75f9 1530 build_and_record_new_cond (UNLE_EXPR, op0, op1,
fd4a760e 1531 &edge_info->cond_equivalences);
efea75f9 1532 build_and_record_new_cond (UNGE_EXPR, op0, op1,
fd4a760e 1533 &edge_info->cond_equivalences);
d2d8936f
JL
1534 break;
1535
1536 case LTGT_EXPR:
efea75f9 1537 build_and_record_new_cond (NE_EXPR, op0, op1,
fd4a760e 1538 &edge_info->cond_equivalences);
efea75f9 1539 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
fd4a760e 1540 &edge_info->cond_equivalences);
efea75f9 1541 break;
d2d8936f
JL
1542
1543 default:
1544 break;
1545 }
efea75f9
JL
1546
1547 /* Now store the original true and false conditions into the first
1548 two slots. */
fd4a760e
RG
1549 initialize_expr_from_cond (cond, &c.cond);
1550 c.value = boolean_true_node;
9771b263 1551 edge_info->cond_equivalences.safe_push (c);
726a989a
RB
1552
1553 /* It is possible for INVERTED to be the negation of a comparison,
1554 and not a valid RHS or GIMPLE_COND condition. This happens because
1555 invert_truthvalue may return such an expression when asked to invert
1556 a floating-point comparison. These comparisons are not assumed to
1557 obey the trichotomy law. */
fd4a760e
RG
1558 initialize_expr_from_cond (inverted, &c.cond);
1559 c.value = boolean_false_node;
9771b263 1560 edge_info->cond_equivalences.safe_push (c);
d2d8936f
JL
1561}
1562
6de9cd9a
DN
1563/* A helper function for record_const_or_copy and record_equality.
1564 Do the work of recording the value and undo info. */
1565
1566static void
b5fefcf6 1567record_const_or_copy_1 (tree x, tree y, tree prev_x)
6de9cd9a 1568{
448ee662 1569 set_ssa_name_value (x, y);
6de9cd9a 1570
726a989a
RB
1571 if (dump_file && (dump_flags & TDF_DETAILS))
1572 {
1573 fprintf (dump_file, "0>>> COPY ");
1574 print_generic_expr (dump_file, x, 0);
1575 fprintf (dump_file, " = ");
1576 print_generic_expr (dump_file, y, 0);
1577 fprintf (dump_file, "\n");
1578 }
1579
9771b263
DN
1580 const_and_copies_stack.reserve (2);
1581 const_and_copies_stack.quick_push (prev_x);
1582 const_and_copies_stack.quick_push (x);
6de9cd9a
DN
1583}
1584
1585/* Record that X is equal to Y in const_and_copies. Record undo
ceb7eb8f 1586 information in the block-local vector. */
6de9cd9a
DN
1587
1588static void
b5fefcf6 1589record_const_or_copy (tree x, tree y)
6de9cd9a 1590{
3aecd08b 1591 tree prev_x = SSA_NAME_VALUE (x);
6de9cd9a 1592
726a989a
RB
1593 gcc_assert (TREE_CODE (x) == SSA_NAME);
1594
6de9cd9a
DN
1595 if (TREE_CODE (y) == SSA_NAME)
1596 {
3aecd08b 1597 tree tmp = SSA_NAME_VALUE (y);
6de9cd9a
DN
1598 if (tmp)
1599 y = tmp;
1600 }
1601
b5fefcf6 1602 record_const_or_copy_1 (x, y, prev_x);
6de9cd9a
DN
1603}
1604
406bfdd3
RB
1605/* Return the loop depth of the basic block of the defining statement of X.
1606 This number should not be treated as absolutely correct because the loop
1607 information may not be completely up-to-date when dom runs. However, it
1608 will be relatively correct, and as more passes are taught to keep loop info
1609 up to date, the result will become more and more accurate. */
1610
1611static int
1612loop_depth_of_name (tree x)
1613{
1614 gimple defstmt;
1615 basic_block defbb;
1616
1617 /* If it's not an SSA_NAME, we have no clue where the definition is. */
1618 if (TREE_CODE (x) != SSA_NAME)
1619 return 0;
1620
1621 /* Otherwise return the loop depth of the defining statement's bb.
1622 Note that there may not actually be a bb for this statement, if the
1623 ssa_name is live on entry. */
1624 defstmt = SSA_NAME_DEF_STMT (x);
1625 defbb = gimple_bb (defstmt);
1626 if (!defbb)
1627 return 0;
1628
1629 return bb_loop_depth (defbb);
1630}
1631
6de9cd9a
DN
1632/* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1633 This constrains the cases in which we may treat this as assignment. */
1634
1635static void
b5fefcf6 1636record_equality (tree x, tree y)
6de9cd9a
DN
1637{
1638 tree prev_x = NULL, prev_y = NULL;
1639
1640 if (TREE_CODE (x) == SSA_NAME)
3aecd08b 1641 prev_x = SSA_NAME_VALUE (x);
6de9cd9a 1642 if (TREE_CODE (y) == SSA_NAME)
3aecd08b 1643 prev_y = SSA_NAME_VALUE (y);
6de9cd9a 1644
84dd478f
DB
1645 /* If one of the previous values is invariant, or invariant in more loops
1646 (by depth), then use that.
6de9cd9a
DN
1647 Otherwise it doesn't matter which value we choose, just so
1648 long as we canonicalize on one value. */
ad6003f2 1649 if (is_gimple_min_invariant (y))
6de9cd9a 1650 ;
406bfdd3
RB
1651 else if (is_gimple_min_invariant (x)
1652 /* ??? When threading over backedges the following is important
1653 for correctness. See PR61757. */
1654 || (loop_depth_of_name (x) <= loop_depth_of_name (y)))
6de9cd9a 1655 prev_x = x, x = y, y = prev_x, prev_x = prev_y;
ad6003f2 1656 else if (prev_x && is_gimple_min_invariant (prev_x))
6de9cd9a 1657 x = y, y = prev_x, prev_x = prev_y;
c9145754 1658 else if (prev_y)
6de9cd9a
DN
1659 y = prev_y;
1660
1661 /* After the swapping, we must have one SSA_NAME. */
1662 if (TREE_CODE (x) != SSA_NAME)
1663 return;
1664
1665 /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1666 variable compared against zero. If we're honoring signed zeros,
1667 then we cannot record this value unless we know that the value is
1ea7e6ad 1668 nonzero. */
3d3dbadd 1669 if (HONOR_SIGNED_ZEROS (x)
6de9cd9a
DN
1670 && (TREE_CODE (y) != REAL_CST
1671 || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (y))))
1672 return;
1673
b5fefcf6 1674 record_const_or_copy_1 (x, y, prev_x);
6de9cd9a
DN
1675}
1676
f67e783f
ZD
1677/* Returns true when STMT is a simple iv increment. It detects the
1678 following situation:
b8698a0f 1679
f67e783f
ZD
1680 i_1 = phi (..., i_2)
1681 i_2 = i_1 +/- ... */
1682
601f64e2 1683bool
726a989a 1684simple_iv_increment_p (gimple stmt)
f67e783f 1685{
601f64e2 1686 enum tree_code code;
726a989a
RB
1687 tree lhs, preinc;
1688 gimple phi;
1689 size_t i;
f67e783f 1690
726a989a 1691 if (gimple_code (stmt) != GIMPLE_ASSIGN)
f67e783f
ZD
1692 return false;
1693
726a989a 1694 lhs = gimple_assign_lhs (stmt);
f67e783f
ZD
1695 if (TREE_CODE (lhs) != SSA_NAME)
1696 return false;
1697
601f64e2
RG
1698 code = gimple_assign_rhs_code (stmt);
1699 if (code != PLUS_EXPR
1700 && code != MINUS_EXPR
1701 && code != POINTER_PLUS_EXPR)
f67e783f
ZD
1702 return false;
1703
726a989a 1704 preinc = gimple_assign_rhs1 (stmt);
f67e783f
ZD
1705 if (TREE_CODE (preinc) != SSA_NAME)
1706 return false;
1707
1708 phi = SSA_NAME_DEF_STMT (preinc);
726a989a 1709 if (gimple_code (phi) != GIMPLE_PHI)
f67e783f
ZD
1710 return false;
1711
726a989a
RB
1712 for (i = 0; i < gimple_phi_num_args (phi); i++)
1713 if (gimple_phi_arg_def (phi, i) == lhs)
f67e783f
ZD
1714 return true;
1715
1716 return false;
1717}
1718
ff2ad0f7 1719/* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
b8698a0f 1720 known value for that SSA_NAME (or NULL if no value is known).
ff2ad0f7 1721
b16caf72
JL
1722 Propagate values from CONST_AND_COPIES into the PHI nodes of the
1723 successors of BB. */
ff2ad0f7
DN
1724
1725static void
b16caf72 1726cprop_into_successor_phis (basic_block bb)
ff2ad0f7
DN
1727{
1728 edge e;
628f6a4e 1729 edge_iterator ei;
ff2ad0f7 1730
628f6a4e 1731 FOR_EACH_EDGE (e, ei, bb->succs)
ff2ad0f7 1732 {
0492baf2 1733 int indx;
538dd0b7 1734 gphi_iterator gsi;
ff2ad0f7
DN
1735
1736 /* If this is an abnormal edge, then we do not want to copy propagate
1737 into the PHI alternative associated with this edge. */
1738 if (e->flags & EDGE_ABNORMAL)
1739 continue;
1740
726a989a
RB
1741 gsi = gsi_start_phis (e->dest);
1742 if (gsi_end_p (gsi))
ff2ad0f7
DN
1743 continue;
1744
8d34e421
JL
1745 /* We may have an equivalence associated with this edge. While
1746 we can not propagate it into non-dominated blocks, we can
1747 propagate them into PHIs in non-dominated blocks. */
1748
1749 /* Push the unwind marker so we can reset the const and copies
1750 table back to its original state after processing this edge. */
1751 const_and_copies_stack.safe_push (NULL_TREE);
1752
1753 /* Extract and record any simple NAME = VALUE equivalences.
1754
1755 Don't bother with [01] = COND equivalences, they're not useful
1756 here. */
1757 struct edge_info *edge_info = (struct edge_info *) e->aux;
1758 if (edge_info)
1759 {
1760 tree lhs = edge_info->lhs;
1761 tree rhs = edge_info->rhs;
1762
1763 if (lhs && TREE_CODE (lhs) == SSA_NAME)
1764 record_const_or_copy (lhs, rhs);
1765 }
1766
0492baf2 1767 indx = e->dest_idx;
726a989a 1768 for ( ; !gsi_end_p (gsi); gsi_next (&gsi))
ff2ad0f7 1769 {
c22940cd 1770 tree new_val;
ff2ad0f7 1771 use_operand_p orig_p;
c22940cd 1772 tree orig_val;
538dd0b7 1773 gphi *phi = gsi.phi ();
ff2ad0f7 1774
ff2ad0f7
DN
1775 /* The alternative may be associated with a constant, so verify
1776 it is an SSA_NAME before doing anything with it. */
726a989a
RB
1777 orig_p = gimple_phi_arg_imm_use_ptr (phi, indx);
1778 orig_val = get_use_from_ptr (orig_p);
c22940cd 1779 if (TREE_CODE (orig_val) != SSA_NAME)
ff2ad0f7
DN
1780 continue;
1781
ff2ad0f7
DN
1782 /* If we have *ORIG_P in our constant/copy table, then replace
1783 ORIG_P with its value in our constant/copy table. */
c22940cd
TN
1784 new_val = SSA_NAME_VALUE (orig_val);
1785 if (new_val
1786 && new_val != orig_val
1787 && (TREE_CODE (new_val) == SSA_NAME
1788 || is_gimple_min_invariant (new_val))
1789 && may_propagate_copy (orig_val, new_val))
1790 propagate_value (orig_p, new_val);
ff2ad0f7 1791 }
8d34e421
JL
1792
1793 restore_vars_to_original_value ();
ff2ad0f7
DN
1794 }
1795}
1796
efea75f9
JL
1797/* We have finished optimizing BB, record any information implied by
1798 taking a specific outgoing edge from BB. */
1799
1800static void
1801record_edge_info (basic_block bb)
1802{
726a989a 1803 gimple_stmt_iterator gsi = gsi_last_bb (bb);
efea75f9
JL
1804 struct edge_info *edge_info;
1805
726a989a 1806 if (! gsi_end_p (gsi))
efea75f9 1807 {
726a989a 1808 gimple stmt = gsi_stmt (gsi);
db3927fb 1809 location_t loc = gimple_location (stmt);
efea75f9 1810
726a989a 1811 if (gimple_code (stmt) == GIMPLE_SWITCH)
efea75f9 1812 {
538dd0b7
DM
1813 gswitch *switch_stmt = as_a <gswitch *> (stmt);
1814 tree index = gimple_switch_index (switch_stmt);
efea75f9 1815
726a989a 1816 if (TREE_CODE (index) == SSA_NAME)
efea75f9 1817 {
726a989a 1818 int i;
538dd0b7 1819 int n_labels = gimple_switch_num_labels (switch_stmt);
8b1c6fd7 1820 tree *info = XCNEWVEC (tree, last_basic_block_for_fn (cfun));
efea75f9
JL
1821 edge e;
1822 edge_iterator ei;
1823
1824 for (i = 0; i < n_labels; i++)
1825 {
538dd0b7 1826 tree label = gimple_switch_label (switch_stmt, i);
efea75f9 1827 basic_block target_bb = label_to_block (CASE_LABEL (label));
efea75f9
JL
1828 if (CASE_HIGH (label)
1829 || !CASE_LOW (label)
1830 || info[target_bb->index])
1831 info[target_bb->index] = error_mark_node;
1832 else
1833 info[target_bb->index] = label;
1834 }
1835
1836 FOR_EACH_EDGE (e, ei, bb->succs)
1837 {
1838 basic_block target_bb = e->dest;
726a989a 1839 tree label = info[target_bb->index];
ff2ad0f7 1840
726a989a 1841 if (label != NULL && label != error_mark_node)
efea75f9 1842 {
db3927fb
AH
1843 tree x = fold_convert_loc (loc, TREE_TYPE (index),
1844 CASE_LOW (label));
efea75f9 1845 edge_info = allocate_edge_info (e);
726a989a 1846 edge_info->lhs = index;
efea75f9
JL
1847 edge_info->rhs = x;
1848 }
1849 }
1850 free (info);
1851 }
1852 }
1853
1854 /* A COND_EXPR may create equivalences too. */
726a989a 1855 if (gimple_code (stmt) == GIMPLE_COND)
efea75f9 1856 {
efea75f9
JL
1857 edge true_edge;
1858 edge false_edge;
1859
726a989a
RB
1860 tree op0 = gimple_cond_lhs (stmt);
1861 tree op1 = gimple_cond_rhs (stmt);
1862 enum tree_code code = gimple_cond_code (stmt);
efea75f9 1863
726a989a 1864 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
efea75f9 1865
726a989a
RB
1866 /* Special case comparing booleans against a constant as we
1867 know the value of OP0 on both arms of the branch. i.e., we
1868 can record an equivalence for OP0 rather than COND. */
1869 if ((code == EQ_EXPR || code == NE_EXPR)
1870 && TREE_CODE (op0) == SSA_NAME
1871 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
1872 && is_gimple_min_invariant (op1))
1873 {
1874 if (code == EQ_EXPR)
1875 {
1876 edge_info = allocate_edge_info (true_edge);
1877 edge_info->lhs = op0;
1878 edge_info->rhs = (integer_zerop (op1)
1879 ? boolean_false_node
1880 : boolean_true_node);
1881
1882 edge_info = allocate_edge_info (false_edge);
1883 edge_info->lhs = op0;
1884 edge_info->rhs = (integer_zerop (op1)
1885 ? boolean_true_node
1886 : boolean_false_node);
1887 }
1888 else
1889 {
1890 edge_info = allocate_edge_info (true_edge);
1891 edge_info->lhs = op0;
1892 edge_info->rhs = (integer_zerop (op1)
1893 ? boolean_true_node
1894 : boolean_false_node);
1895
1896 edge_info = allocate_edge_info (false_edge);
1897 edge_info->lhs = op0;
1898 edge_info->rhs = (integer_zerop (op1)
1899 ? boolean_false_node
1900 : boolean_true_node);
1901 }
1902 }
1903 else if (is_gimple_min_invariant (op0)
1904 && (TREE_CODE (op1) == SSA_NAME
1905 || is_gimple_min_invariant (op1)))
1906 {
1907 tree cond = build2 (code, boolean_type_node, op0, op1);
db3927fb 1908 tree inverted = invert_truthvalue_loc (loc, cond);
4db183a2 1909 bool can_infer_simple_equiv
3d3dbadd 1910 = !(HONOR_SIGNED_ZEROS (op0)
4db183a2 1911 && real_zerop (op0));
726a989a
RB
1912 struct edge_info *edge_info;
1913
1914 edge_info = allocate_edge_info (true_edge);
1915 record_conditions (edge_info, cond, inverted);
1916
4db183a2 1917 if (can_infer_simple_equiv && code == EQ_EXPR)
726a989a
RB
1918 {
1919 edge_info->lhs = op1;
1920 edge_info->rhs = op0;
1921 }
1922
1923 edge_info = allocate_edge_info (false_edge);
1924 record_conditions (edge_info, inverted, cond);
1925
4db183a2 1926 if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
726a989a
RB
1927 {
1928 edge_info->lhs = op1;
1929 edge_info->rhs = op0;
1930 }
1931 }
1932
1933 else if (TREE_CODE (op0) == SSA_NAME
4db183a2
EB
1934 && (TREE_CODE (op1) == SSA_NAME
1935 || is_gimple_min_invariant (op1)))
726a989a
RB
1936 {
1937 tree cond = build2 (code, boolean_type_node, op0, op1);
db3927fb 1938 tree inverted = invert_truthvalue_loc (loc, cond);
4db183a2 1939 bool can_infer_simple_equiv
3d3dbadd 1940 = !(HONOR_SIGNED_ZEROS (op1)
4db183a2 1941 && (TREE_CODE (op1) == SSA_NAME || real_zerop (op1)));
726a989a
RB
1942 struct edge_info *edge_info;
1943
1944 edge_info = allocate_edge_info (true_edge);
1945 record_conditions (edge_info, cond, inverted);
1946
4db183a2 1947 if (can_infer_simple_equiv && code == EQ_EXPR)
726a989a
RB
1948 {
1949 edge_info->lhs = op0;
1950 edge_info->rhs = op1;
1951 }
1952
1953 edge_info = allocate_edge_info (false_edge);
1954 record_conditions (edge_info, inverted, cond);
1955
4db183a2 1956 if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
726a989a
RB
1957 {
1958 edge_info->lhs = op0;
1959 edge_info->rhs = op1;
1960 }
1961 }
1962 }
1963
1964 /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
efea75f9
JL
1965 }
1966}
1967
4d9192b5
TS
1968void
1969dom_opt_dom_walker::before_dom_children (basic_block bb)
6de9cd9a 1970{
ccf5c864
PB
1971 gimple_stmt_iterator gsi;
1972
1973 if (dump_file && (dump_flags & TDF_DETAILS))
1974 fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
1975
1976 /* Push a marker on the stacks of local information so that we know how
1977 far to unwind when we finalize this block. */
b00734df
RB
1978 avail_exprs_stack.safe_push
1979 (std::pair<expr_hash_elt_t, expr_hash_elt_t> (NULL, NULL));
9771b263 1980 const_and_copies_stack.safe_push (NULL_TREE);
ccf5c864
PB
1981
1982 record_equivalences_from_incoming_edge (bb);
1983
1984 /* PHI nodes can create equivalences too. */
1985 record_equivalences_from_phis (bb);
1986
13a3e5b6
BS
1987 /* Create equivalences from redundant PHIs. PHIs are only truly
1988 redundant when they exist in the same block, so push another
1989 marker and unwind right afterwards. */
b00734df
RB
1990 avail_exprs_stack.safe_push
1991 (std::pair<expr_hash_elt_t, expr_hash_elt_t> (NULL, NULL));
13a3e5b6
BS
1992 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1993 eliminate_redundant_computations (&gsi);
1994 remove_local_expressions_from_table ();
1995
ccf5c864
PB
1996 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1997 optimize_stmt (bb, gsi);
1998
1999 /* Now prepare to process dominated blocks. */
efea75f9 2000 record_edge_info (bb);
b16caf72 2001 cprop_into_successor_phis (bb);
6de9cd9a
DN
2002}
2003
ccf5c864
PB
2004/* We have finished processing the dominator children of BB, perform
2005 any finalization actions in preparation for leaving this node in
2006 the dominator tree. */
2007
4d9192b5
TS
2008void
2009dom_opt_dom_walker::after_dom_children (basic_block bb)
ccf5c864
PB
2010{
2011 gimple last;
2012
2013 /* If we have an outgoing edge to a block with multiple incoming and
2014 outgoing edges, then we may be able to thread the edge, i.e., we
2015 may be able to statically determine which of the outgoing edges
2016 will be traversed when the incoming edge from BB is traversed. */
2017 if (single_succ_p (bb)
2018 && (single_succ_edge (bb)->flags & EDGE_ABNORMAL) == 0
2019 && potentially_threadable_block (single_succ (bb)))
2020 {
4d9192b5 2021 thread_across_edge (single_succ_edge (bb));
ccf5c864
PB
2022 }
2023 else if ((last = last_stmt (bb))
2024 && gimple_code (last) == GIMPLE_COND
2025 && EDGE_COUNT (bb->succs) == 2
2026 && (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL) == 0
2027 && (EDGE_SUCC (bb, 1)->flags & EDGE_ABNORMAL) == 0)
2028 {
2029 edge true_edge, false_edge;
2030
2031 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2032
2033 /* Only try to thread the edge if it reaches a target block with
2034 more than one predecessor and more than one successor. */
2035 if (potentially_threadable_block (true_edge->dest))
83ae86f5 2036 thread_across_edge (true_edge);
ccf5c864
PB
2037
2038 /* Similarly for the ELSE arm. */
2039 if (potentially_threadable_block (false_edge->dest))
83ae86f5 2040 thread_across_edge (false_edge);
ccf5c864 2041
ccf5c864
PB
2042 }
2043
83ae86f5 2044 /* These remove expressions local to BB from the tables. */
ccf5c864
PB
2045 remove_local_expressions_from_table ();
2046 restore_vars_to_original_value ();
ccf5c864
PB
2047}
2048
6de9cd9a
DN
2049/* Search for redundant computations in STMT. If any are found, then
2050 replace them with the variable holding the result of the computation.
2051
2052 If safe, record this expression into the available expression hash
2053 table. */
2054
87c93592 2055static void
726a989a 2056eliminate_redundant_computations (gimple_stmt_iterator* gsi)
6de9cd9a 2057{
726a989a 2058 tree expr_type;
6de9cd9a 2059 tree cached_lhs;
13a3e5b6 2060 tree def;
726a989a 2061 bool insert = true;
726a989a 2062 bool assigns_var_p = false;
6de9cd9a 2063
726a989a
RB
2064 gimple stmt = gsi_stmt (*gsi);
2065
13a3e5b6
BS
2066 if (gimple_code (stmt) == GIMPLE_PHI)
2067 def = gimple_phi_result (stmt);
2068 else
2069 def = gimple_get_lhs (stmt);
6de9cd9a
DN
2070
2071 /* Certain expressions on the RHS can be optimized away, but can not
471854f8 2072 themselves be entered into the hash tables. */
ff88c5aa 2073 if (! def
6de9cd9a
DN
2074 || TREE_CODE (def) != SSA_NAME
2075 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)
5006671f 2076 || gimple_vdef (stmt)
f67e783f
ZD
2077 /* Do not record equivalences for increments of ivs. This would create
2078 overlapping live ranges for a very questionable gain. */
2079 || simple_iv_increment_p (stmt))
6de9cd9a
DN
2080 insert = false;
2081
2082 /* Check if the expression has been computed before. */
48732f23 2083 cached_lhs = lookup_avail_expr (stmt, insert);
6de9cd9a 2084
6de9cd9a
DN
2085 opt_stats.num_exprs_considered++;
2086
726a989a
RB
2087 /* Get the type of the expression we are trying to optimize. */
2088 if (is_gimple_assign (stmt))
019b02f1 2089 {
726a989a
RB
2090 expr_type = TREE_TYPE (gimple_assign_lhs (stmt));
2091 assigns_var_p = true;
019b02f1 2092 }
726a989a
RB
2093 else if (gimple_code (stmt) == GIMPLE_COND)
2094 expr_type = boolean_type_node;
2095 else if (is_gimple_call (stmt))
019b02f1 2096 {
726a989a
RB
2097 gcc_assert (gimple_call_lhs (stmt));
2098 expr_type = TREE_TYPE (gimple_call_lhs (stmt));
2099 assigns_var_p = true;
019b02f1 2100 }
538dd0b7
DM
2101 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
2102 expr_type = TREE_TYPE (gimple_switch_index (swtch_stmt));
13a3e5b6
BS
2103 else if (gimple_code (stmt) == GIMPLE_PHI)
2104 /* We can't propagate into a phi, so the logic below doesn't apply.
2105 Instead record an equivalence between the cached LHS and the
2106 PHI result of this statement, provided they are in the same block.
2107 This should be sufficient to kill the redundant phi. */
2108 {
2109 if (def && cached_lhs)
2110 record_const_or_copy (def, cached_lhs);
2111 return;
2112 }
726a989a
RB
2113 else
2114 gcc_unreachable ();
2115
2116 if (!cached_lhs)
87c93592 2117 return;
6de9cd9a
DN
2118
2119 /* It is safe to ignore types here since we have already done
2120 type checking in the hashing and equality routines. In fact
2121 type checking here merely gets in the way of constant
2122 propagation. Also, make sure that it is safe to propagate
726a989a
RB
2123 CACHED_LHS into the expression in STMT. */
2124 if ((TREE_CODE (cached_lhs) != SSA_NAME
2125 && (assigns_var_p
2126 || useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs))))
2127 || may_propagate_copy_into_stmt (stmt, cached_lhs))
2128 {
77a74ed7
NF
2129 gcc_checking_assert (TREE_CODE (cached_lhs) == SSA_NAME
2130 || is_gimple_min_invariant (cached_lhs));
726a989a 2131
6de9cd9a
DN
2132 if (dump_file && (dump_flags & TDF_DETAILS))
2133 {
2134 fprintf (dump_file, " Replaced redundant expr '");
726a989a 2135 print_gimple_expr (dump_file, stmt, 0, dump_flags);
6de9cd9a
DN
2136 fprintf (dump_file, "' with '");
2137 print_generic_expr (dump_file, cached_lhs, dump_flags);
726a989a 2138 fprintf (dump_file, "'\n");
6de9cd9a
DN
2139 }
2140
2141 opt_stats.num_re++;
b8698a0f 2142
726a989a
RB
2143 if (assigns_var_p
2144 && !useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs)))
2145 cached_lhs = fold_convert (expr_type, cached_lhs);
6de9cd9a 2146
726a989a
RB
2147 propagate_tree_value_into_stmt (gsi, cached_lhs);
2148
2149 /* Since it is always necessary to mark the result as modified,
2150 perhaps we should move this into propagate_tree_value_into_stmt
2151 itself. */
2152 gimple_set_modified (gsi_stmt (*gsi), true);
2153 }
6de9cd9a
DN
2154}
2155
726a989a 2156/* STMT, a GIMPLE_ASSIGN, may create certain equivalences, in either
6de9cd9a
DN
2157 the available expressions table or the const_and_copies table.
2158 Detect and record those equivalences. */
726a989a
RB
2159/* We handle only very simple copy equivalences here. The heavy
2160 lifing is done by eliminate_redundant_computations. */
6de9cd9a
DN
2161
2162static void
726a989a 2163record_equivalences_from_stmt (gimple stmt, int may_optimize_p)
6de9cd9a 2164{
726a989a
RB
2165 tree lhs;
2166 enum tree_code lhs_code;
6de9cd9a 2167
726a989a
RB
2168 gcc_assert (is_gimple_assign (stmt));
2169
2170 lhs = gimple_assign_lhs (stmt);
2171 lhs_code = TREE_CODE (lhs);
6de9cd9a 2172
726a989a 2173 if (lhs_code == SSA_NAME
7e673273 2174 && gimple_assign_single_p (stmt))
726a989a
RB
2175 {
2176 tree rhs = gimple_assign_rhs1 (stmt);
b8698a0f 2177
6de9cd9a
DN
2178 /* If the RHS of the assignment is a constant or another variable that
2179 may be propagated, register it in the CONST_AND_COPIES table. We
2180 do not need to record unwind data for this, since this is a true
1ea7e6ad 2181 assignment and not an equivalence inferred from a comparison. All
6de9cd9a
DN
2182 uses of this ssa name are dominated by this assignment, so unwinding
2183 just costs time and space. */
2184 if (may_optimize_p
2185 && (TREE_CODE (rhs) == SSA_NAME
2186 || is_gimple_min_invariant (rhs)))
726a989a
RB
2187 {
2188 if (dump_file && (dump_flags & TDF_DETAILS))
2189 {
2190 fprintf (dump_file, "==== ASGN ");
2191 print_generic_expr (dump_file, lhs, 0);
2192 fprintf (dump_file, " = ");
2193 print_generic_expr (dump_file, rhs, 0);
2194 fprintf (dump_file, "\n");
2195 }
2196
448ee662 2197 set_ssa_name_value (lhs, rhs);
726a989a 2198 }
6de9cd9a
DN
2199 }
2200
b00734df
RB
2201 /* Make sure we can propagate &x + CST. */
2202 if (lhs_code == SSA_NAME
2203 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
2204 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR
2205 && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)
2206 {
2207 tree op0 = gimple_assign_rhs1 (stmt);
2208 tree op1 = gimple_assign_rhs2 (stmt);
2209 tree new_rhs
2210 = build_fold_addr_expr (fold_build2 (MEM_REF,
2211 TREE_TYPE (TREE_TYPE (op0)),
2212 unshare_expr (op0),
2213 fold_convert (ptr_type_node,
2214 op1)));
2215 if (dump_file && (dump_flags & TDF_DETAILS))
2216 {
2217 fprintf (dump_file, "==== ASGN ");
2218 print_generic_expr (dump_file, lhs, 0);
2219 fprintf (dump_file, " = ");
2220 print_generic_expr (dump_file, new_rhs, 0);
2221 fprintf (dump_file, "\n");
2222 }
2223
2224 set_ssa_name_value (lhs, new_rhs);
2225 }
2226
6de9cd9a
DN
2227 /* A memory store, even an aliased store, creates a useful
2228 equivalence. By exchanging the LHS and RHS, creating suitable
2229 vops and recording the result in the available expression table,
2230 we may be able to expose more redundant loads. */
726a989a
RB
2231 if (!gimple_has_volatile_ops (stmt)
2232 && gimple_references_memory_p (stmt)
2233 && gimple_assign_single_p (stmt)
2234 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
2235 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
6de9cd9a
DN
2236 && !is_gimple_reg (lhs))
2237 {
726a989a 2238 tree rhs = gimple_assign_rhs1 (stmt);
538dd0b7 2239 gassign *new_stmt;
6de9cd9a 2240
cf3135aa 2241 /* Build a new statement with the RHS and LHS exchanged. */
726a989a
RB
2242 if (TREE_CODE (rhs) == SSA_NAME)
2243 {
2244 /* NOTE tuples. The call to gimple_build_assign below replaced
2245 a call to build_gimple_modify_stmt, which did not set the
2246 SSA_NAME_DEF_STMT on the LHS of the assignment. Doing so
2247 may cause an SSA validation failure, as the LHS may be a
2248 default-initialized name and should have no definition. I'm
2249 a bit dubious of this, as the artificial statement that we
2250 generate here may in fact be ill-formed, but it is simply
2251 used as an internal device in this pass, and never becomes
2252 part of the CFG. */
2253 gimple defstmt = SSA_NAME_DEF_STMT (rhs);
2254 new_stmt = gimple_build_assign (rhs, lhs);
2255 SSA_NAME_DEF_STMT (rhs) = defstmt;
2256 }
2257 else
2258 new_stmt = gimple_build_assign (rhs, lhs);
2259
5006671f 2260 gimple_set_vuse (new_stmt, gimple_vdef (stmt));
6de9cd9a 2261
cf3135aa
RG
2262 /* Finally enter the statement into the available expression
2263 table. */
2264 lookup_avail_expr (new_stmt, true);
6de9cd9a
DN
2265 }
2266}
2267
ff2ad0f7
DN
2268/* Replace *OP_P in STMT with any known equivalent value for *OP_P from
2269 CONST_AND_COPIES. */
2270
87c93592 2271static void
726a989a 2272cprop_operand (gimple stmt, use_operand_p op_p)
ff2ad0f7 2273{
ff2ad0f7
DN
2274 tree val;
2275 tree op = USE_FROM_PTR (op_p);
2276
2277 /* If the operand has a known constant value or it is known to be a
2278 copy of some other variable, use the value or copy stored in
2279 CONST_AND_COPIES. */
3aecd08b 2280 val = SSA_NAME_VALUE (op);
c9145754 2281 if (val && val != op)
ff2ad0f7 2282 {
aa24864c 2283 /* Do not replace hard register operands in asm statements. */
726a989a 2284 if (gimple_code (stmt) == GIMPLE_ASM
aa24864c 2285 && !may_propagate_copy_into_asm (op))
87c93592 2286 return;
aa24864c 2287
ff2ad0f7
DN
2288 /* Certain operands are not allowed to be copy propagated due
2289 to their interaction with exception handling and some GCC
2290 extensions. */
66e8b99c 2291 if (!may_propagate_copy (op, val))
87c93592 2292 return;
66e8b99c 2293
e9d85fa6
RG
2294 /* Do not propagate copies into simple IV increment statements.
2295 See PR23821 for how this can disturb IV analysis. */
2296 if (TREE_CODE (val) != INTEGER_CST
2297 && simple_iv_increment_p (stmt))
2298 return;
2299
ff2ad0f7
DN
2300 /* Dump details. */
2301 if (dump_file && (dump_flags & TDF_DETAILS))
2302 {
2303 fprintf (dump_file, " Replaced '");
2304 print_generic_expr (dump_file, op, dump_flags);
2305 fprintf (dump_file, "' with %s '",
2306 (TREE_CODE (val) != SSA_NAME ? "constant" : "variable"));
2307 print_generic_expr (dump_file, val, dump_flags);
2308 fprintf (dump_file, "'\n");
2309 }
2310
0bca51f0
DN
2311 if (TREE_CODE (val) != SSA_NAME)
2312 opt_stats.num_const_prop++;
2313 else
2314 opt_stats.num_copy_prop++;
2315
ff2ad0f7
DN
2316 propagate_value (op_p, val);
2317
2318 /* And note that we modified this statement. This is now
2319 safe, even if we changed virtual operands since we will
2320 rescan the statement and rewrite its operands again. */
726a989a 2321 gimple_set_modified (stmt, true);
ff2ad0f7 2322 }
ff2ad0f7
DN
2323}
2324
2325/* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
b8698a0f 2326 known value for that SSA_NAME (or NULL if no value is known).
ff2ad0f7
DN
2327
2328 Propagate values from CONST_AND_COPIES into the uses, vuses and
cfaab3a9 2329 vdef_ops of STMT. */
ff2ad0f7 2330
87c93592 2331static void
726a989a 2332cprop_into_stmt (gimple stmt)
ff2ad0f7 2333{
4c124b4c
AM
2334 use_operand_p op_p;
2335 ssa_op_iter iter;
ff2ad0f7 2336
2a86de57
RB
2337 FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_USE)
2338 cprop_operand (stmt, op_p);
ff2ad0f7
DN
2339}
2340
206048bd 2341/* Optimize the statement pointed to by iterator SI.
b8698a0f 2342
6de9cd9a
DN
2343 We try to perform some simplistic global redundancy elimination and
2344 constant propagation:
2345
2346 1- To detect global redundancy, we keep track of expressions that have
2347 been computed in this block and its dominators. If we find that the
2348 same expression is computed more than once, we eliminate repeated
2349 computations by using the target of the first one.
2350
2351 2- Constant values and copy assignments. This is used to do very
2352 simplistic constant and copy propagation. When a constant or copy
2353 assignment is found, we map the value on the RHS of the assignment to
2354 the variable in the LHS in the CONST_AND_COPIES table. */
2355
2356static void
ccf5c864 2357optimize_stmt (basic_block bb, gimple_stmt_iterator si)
6de9cd9a 2358{
726a989a 2359 gimple stmt, old_stmt;
6de9cd9a 2360 bool may_optimize_p;
c5cac099 2361 bool modified_p = false;
6de9cd9a 2362
726a989a 2363 old_stmt = stmt = gsi_stmt (si);
b8698a0f 2364
6de9cd9a
DN
2365 if (dump_file && (dump_flags & TDF_DETAILS))
2366 {
2367 fprintf (dump_file, "Optimizing statement ");
726a989a 2368 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
6de9cd9a
DN
2369 }
2370
2a86de57 2371 if (gimple_code (stmt) == GIMPLE_COND)
538dd0b7 2372 canonicalize_comparison (as_a <gcond *> (stmt));
2a86de57
RB
2373
2374 update_stmt_if_modified (stmt);
2375 opt_stats.num_stmts++;
2376
cfaab3a9 2377 /* Const/copy propagate into USES, VUSES and the RHS of VDEFs. */
87c93592 2378 cprop_into_stmt (stmt);
6de9cd9a
DN
2379
2380 /* If the statement has been modified with constant replacements,
2381 fold its RHS before checking for redundant computations. */
726a989a 2382 if (gimple_modified_p (stmt))
6de9cd9a 2383 {
726a989a 2384 tree rhs = NULL;
6cedb4ac 2385
6de9cd9a
DN
2386 /* Try to fold the statement making sure that STMT is kept
2387 up to date. */
726a989a 2388 if (fold_stmt (&si))
6de9cd9a 2389 {
726a989a 2390 stmt = gsi_stmt (si);
076ba157 2391 gimple_set_modified (stmt, true);
6de9cd9a
DN
2392
2393 if (dump_file && (dump_flags & TDF_DETAILS))
2394 {
2395 fprintf (dump_file, " Folded to: ");
726a989a 2396 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
6de9cd9a
DN
2397 }
2398 }
2399
726a989a
RB
2400 /* We only need to consider cases that can yield a gimple operand. */
2401 if (gimple_assign_single_p (stmt))
2402 rhs = gimple_assign_rhs1 (stmt);
2403 else if (gimple_code (stmt) == GIMPLE_GOTO)
2404 rhs = gimple_goto_dest (stmt);
538dd0b7 2405 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
726a989a 2406 /* This should never be an ADDR_EXPR. */
538dd0b7 2407 rhs = gimple_switch_index (swtch_stmt);
726a989a 2408
6cedb4ac 2409 if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
726a989a 2410 recompute_tree_invariant_for_addr_expr (rhs);
6cedb4ac 2411
c5cac099
JJ
2412 /* Indicate that maybe_clean_or_replace_eh_stmt needs to be called,
2413 even if fold_stmt updated the stmt already and thus cleared
2414 gimple_modified_p flag on it. */
2415 modified_p = true;
6de9cd9a
DN
2416 }
2417
2418 /* Check for redundant computations. Do this optimization only
2419 for assignments that have no volatile ops and conditionals. */
d829c408
RG
2420 may_optimize_p = (!gimple_has_side_effects (stmt)
2421 && (is_gimple_assign (stmt)
726a989a 2422 || (is_gimple_call (stmt)
d829c408 2423 && gimple_call_lhs (stmt) != NULL_TREE)
726a989a
RB
2424 || gimple_code (stmt) == GIMPLE_COND
2425 || gimple_code (stmt) == GIMPLE_SWITCH));
6de9cd9a
DN
2426
2427 if (may_optimize_p)
726a989a 2428 {
44e10129
MM
2429 if (gimple_code (stmt) == GIMPLE_CALL)
2430 {
2431 /* Resolve __builtin_constant_p. If it hasn't been
2432 folded to integer_one_node by now, it's fairly
2433 certain that the value simply isn't constant. */
2434 tree callee = gimple_call_fndecl (stmt);
2435 if (callee
2436 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
2437 && DECL_FUNCTION_CODE (callee) == BUILT_IN_CONSTANT_P)
2438 {
2439 propagate_tree_value_into_stmt (&si, integer_zero_node);
2440 stmt = gsi_stmt (si);
2441 }
2442 }
aabf6a03
RG
2443
2444 update_stmt_if_modified (stmt);
2445 eliminate_redundant_computations (&si);
2446 stmt = gsi_stmt (si);
565b8886
RG
2447
2448 /* Perform simple redundant store elimination. */
2449 if (gimple_assign_single_p (stmt)
2450 && TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
2451 {
2452 tree lhs = gimple_assign_lhs (stmt);
2453 tree rhs = gimple_assign_rhs1 (stmt);
2454 tree cached_lhs;
538dd0b7 2455 gassign *new_stmt;
565b8886
RG
2456 if (TREE_CODE (rhs) == SSA_NAME)
2457 {
2458 tree tem = SSA_NAME_VALUE (rhs);
2459 if (tem)
2460 rhs = tem;
2461 }
2462 /* Build a new statement with the RHS and LHS exchanged. */
2463 if (TREE_CODE (rhs) == SSA_NAME)
2464 {
2465 gimple defstmt = SSA_NAME_DEF_STMT (rhs);
2466 new_stmt = gimple_build_assign (rhs, lhs);
2467 SSA_NAME_DEF_STMT (rhs) = defstmt;
2468 }
2469 else
2470 new_stmt = gimple_build_assign (rhs, lhs);
2471 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
2472 cached_lhs = lookup_avail_expr (new_stmt, false);
2473 if (cached_lhs
2474 && rhs == cached_lhs)
2475 {
2476 basic_block bb = gimple_bb (stmt);
565b8886 2477 unlink_stmt_vdef (stmt);
b5b3ec3e 2478 if (gsi_remove (&si, true))
565b8886
RG
2479 {
2480 bitmap_set_bit (need_eh_cleanup, bb->index);
2481 if (dump_file && (dump_flags & TDF_DETAILS))
2482 fprintf (dump_file, " Flagged to clear EH edges.\n");
2483 }
3d3f2249 2484 release_defs (stmt);
565b8886
RG
2485 return;
2486 }
2487 }
726a989a 2488 }
6de9cd9a
DN
2489
2490 /* Record any additional equivalences created by this statement. */
726a989a
RB
2491 if (is_gimple_assign (stmt))
2492 record_equivalences_from_stmt (stmt, may_optimize_p);
6de9cd9a 2493
6de9cd9a
DN
2494 /* If STMT is a COND_EXPR and it was modified, then we may know
2495 where it goes. If that is the case, then mark the CFG as altered.
2496
2497 This will cause us to later call remove_unreachable_blocks and
b8698a0f 2498 cleanup_tree_cfg when it is safe to do so. It is not safe to
6de9cd9a
DN
2499 clean things up here since removal of edges and such can trigger
2500 the removal of PHI nodes, which in turn can release SSA_NAMEs to
2501 the manager.
2502
2503 That's all fine and good, except that once SSA_NAMEs are released
2504 to the manager, we must not call create_ssa_name until all references
2505 to released SSA_NAMEs have been eliminated.
2506
2507 All references to the deleted SSA_NAMEs can not be eliminated until
2508 we remove unreachable blocks.
2509
2510 We can not remove unreachable blocks until after we have completed
2511 any queued jump threading.
2512
2513 We can not complete any queued jump threads until we have taken
2514 appropriate variables out of SSA form. Taking variables out of
2515 SSA form can call create_ssa_name and thus we lose.
2516
2517 Ultimately I suspect we're going to need to change the interface
2518 into the SSA_NAME manager. */
c5cac099 2519 if (gimple_modified_p (stmt) || modified_p)
6de9cd9a
DN
2520 {
2521 tree val = NULL;
b8698a0f 2522
aabf6a03 2523 update_stmt_if_modified (stmt);
6de9cd9a 2524
726a989a 2525 if (gimple_code (stmt) == GIMPLE_COND)
db3927fb
AH
2526 val = fold_binary_loc (gimple_location (stmt),
2527 gimple_cond_code (stmt), boolean_type_node,
726a989a 2528 gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
538dd0b7
DM
2529 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
2530 val = gimple_switch_index (swtch_stmt);
6de9cd9a 2531
1eaba2f2 2532 if (val && TREE_CODE (val) == INTEGER_CST && find_taken_edge (bb, val))
6de9cd9a 2533 cfg_altered = true;
1eaba2f2
RH
2534
2535 /* If we simplified a statement in such a way as to be shown that it
2536 cannot trap, update the eh information and the cfg to match. */
af47810a 2537 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
1eaba2f2
RH
2538 {
2539 bitmap_set_bit (need_eh_cleanup, bb->index);
2540 if (dump_file && (dump_flags & TDF_DETAILS))
2541 fprintf (dump_file, " Flagged to clear EH edges.\n");
2542 }
6de9cd9a 2543 }
6de9cd9a
DN
2544}
2545
b00734df
RB
2546/* Helper for walk_non_aliased_vuses. Determine if we arrived at
2547 the desired memory state. */
2548
2549static void *
2550vuse_eq (ao_ref *, tree vuse1, unsigned int cnt, void *data)
2551{
2552 tree vuse2 = (tree) data;
2553 if (vuse1 == vuse2)
2554 return data;
2555
2556 /* This bounds the stmt walks we perform on reference lookups
2557 to O(1) instead of O(N) where N is the number of dominating
2558 stores leading to a candidate. We re-use the SCCVN param
2559 for this as it is basically the same complexity. */
2560 if (cnt > (unsigned) PARAM_VALUE (PARAM_SCCVN_MAX_ALIAS_QUERIES_PER_ACCESS))
2561 return (void *)-1;
2562
2563 return NULL;
2564}
2565
726a989a
RB
2566/* Search for an existing instance of STMT in the AVAIL_EXPRS table.
2567 If found, return its LHS. Otherwise insert STMT in the table and
2568 return NULL_TREE.
6de9cd9a 2569
726a989a
RB
2570 Also, when an expression is first inserted in the table, it is also
2571 is also added to AVAIL_EXPRS_STACK, so that it can be removed when
2572 we finish processing this block and its children. */
6de9cd9a
DN
2573
2574static tree
726a989a 2575lookup_avail_expr (gimple stmt, bool insert)
6de9cd9a 2576{
4a8fb1a1 2577 expr_hash_elt **slot;
6de9cd9a
DN
2578 tree lhs;
2579 tree temp;
a7d04a53 2580 struct expr_hash_elt element;
6de9cd9a 2581
13a3e5b6
BS
2582 /* Get LHS of phi, assignment, or call; else NULL_TREE. */
2583 if (gimple_code (stmt) == GIMPLE_PHI)
2584 lhs = gimple_phi_result (stmt);
2585 else
2586 lhs = gimple_get_lhs (stmt);
6de9cd9a 2587
a7d04a53 2588 initialize_hash_element (stmt, lhs, &element);
6de9cd9a 2589
726a989a
RB
2590 if (dump_file && (dump_flags & TDF_DETAILS))
2591 {
2592 fprintf (dump_file, "LKUP ");
a7d04a53 2593 print_expr_hash_elt (dump_file, &element);
726a989a
RB
2594 }
2595
6de9cd9a
DN
2596 /* Don't bother remembering constant assignments and copy operations.
2597 Constants and copy operations are handled by the constant/copy propagator
2598 in optimize_stmt. */
a7d04a53
RG
2599 if (element.expr.kind == EXPR_SINGLE
2600 && (TREE_CODE (element.expr.ops.single.rhs) == SSA_NAME
2601 || is_gimple_min_invariant (element.expr.ops.single.rhs)))
2602 return NULL_TREE;
6de9cd9a 2603
6de9cd9a 2604 /* Finally try to find the expression in the main expression hash table. */
c203e8a7 2605 slot = avail_exprs->find_slot (&element, (insert ? INSERT : NO_INSERT));
6de9cd9a 2606 if (slot == NULL)
b6db991c
RG
2607 {
2608 free_expr_hash_elt_contents (&element);
2609 return NULL_TREE;
2610 }
2611 else if (*slot == NULL)
6de9cd9a 2612 {
a7d04a53
RG
2613 struct expr_hash_elt *element2 = XNEW (struct expr_hash_elt);
2614 *element2 = element;
2615 element2->stamp = element2;
4a8fb1a1 2616 *slot = element2;
726a989a
RB
2617
2618 if (dump_file && (dump_flags & TDF_DETAILS))
2619 {
2620 fprintf (dump_file, "2>>> ");
a7d04a53 2621 print_expr_hash_elt (dump_file, element2);
726a989a
RB
2622 }
2623
b00734df
RB
2624 avail_exprs_stack.safe_push
2625 (std::pair<expr_hash_elt_t, expr_hash_elt_t> (element2, NULL));
6de9cd9a
DN
2626 return NULL_TREE;
2627 }
b00734df
RB
2628
2629 /* If we found a redundant memory operation do an alias walk to
2630 check if we can re-use it. */
2631 if (gimple_vuse (stmt) != (*slot)->vop)
2632 {
2633 tree vuse1 = (*slot)->vop;
2634 tree vuse2 = gimple_vuse (stmt);
2635 /* If we have a load of a register and a candidate in the
2636 hash with vuse1 then try to reach its stmt by walking
2637 up the virtual use-def chain using walk_non_aliased_vuses.
2638 But don't do this when removing expressions from the hash. */
2639 ao_ref ref;
2640 if (!(vuse1 && vuse2
2641 && gimple_assign_single_p (stmt)
2642 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME
2643 && (ao_ref_init (&ref, gimple_assign_rhs1 (stmt)), true)
2644 && walk_non_aliased_vuses (&ref, vuse2,
92a5094e 2645 vuse_eq, NULL, NULL, vuse1) != NULL))
b00734df
RB
2646 {
2647 struct expr_hash_elt *element2 = XNEW (struct expr_hash_elt);
2648 *element2 = element;
2649 element2->stamp = element2;
2650
2651 /* Insert the expr into the hash by replacing the current
2652 entry and recording the value to restore in the
2653 aval_exprs_stack. */
2654 avail_exprs_stack.safe_push (std::make_pair (element2, *slot));
2655 *slot = element2;
2656 if (dump_file && (dump_flags & TDF_DETAILS))
2657 {
2658 fprintf (dump_file, "2>>> ");
2659 print_expr_hash_elt (dump_file, *slot);
2660 }
2661 return NULL_TREE;
2662 }
2663 }
2664
2665 free_expr_hash_elt_contents (&element);
6de9cd9a
DN
2666
2667 /* Extract the LHS of the assignment so that it can be used as the current
2668 definition of another variable. */
b00734df 2669 lhs = (*slot)->lhs;
6de9cd9a
DN
2670
2671 /* See if the LHS appears in the CONST_AND_COPIES table. If it does, then
2672 use the value from the const_and_copies table. */
2673 if (TREE_CODE (lhs) == SSA_NAME)
2674 {
3aecd08b 2675 temp = SSA_NAME_VALUE (lhs);
c9145754 2676 if (temp)
6de9cd9a
DN
2677 lhs = temp;
2678 }
2679
726a989a
RB
2680 if (dump_file && (dump_flags & TDF_DETAILS))
2681 {
2682 fprintf (dump_file, "FIND: ");
2683 print_generic_expr (dump_file, lhs, 0);
2684 fprintf (dump_file, "\n");
2685 }
2686
6de9cd9a
DN
2687 return lhs;
2688}
2689
726a989a
RB
2690/* Hashing and equality functions for AVAIL_EXPRS. We compute a value number
2691 for expressions using the code of the expression and the SSA numbers of
2692 its operands. */
6de9cd9a
DN
2693
2694static hashval_t
2695avail_expr_hash (const void *p)
2696{
726a989a 2697 const struct hashable_expr *expr = &((const struct expr_hash_elt *)p)->expr;
2bc10537 2698 inchash::hash hstate;
6de9cd9a 2699
2bc10537 2700 inchash::add_hashable_expr (expr, hstate);
6de9cd9a 2701
2bc10537 2702 return hstate.end ();
6de9cd9a
DN
2703}
2704
e67c25c7
JL
2705/* PHI-ONLY copy and constant propagation. This pass is meant to clean
2706 up degenerate PHIs created by or exposed by jump threading. */
2707
726a989a 2708/* Given a statement STMT, which is either a PHI node or an assignment,
e67c25c7
JL
2709 remove it from the IL. */
2710
2711static void
726a989a 2712remove_stmt_or_phi (gimple stmt)
e67c25c7 2713{
726a989a
RB
2714 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
2715
2716 if (gimple_code (stmt) == GIMPLE_PHI)
2717 remove_phi_node (&gsi, true);
e67c25c7
JL
2718 else
2719 {
726a989a
RB
2720 gsi_remove (&gsi, true);
2721 release_defs (stmt);
e67c25c7
JL
2722 }
2723}
2724
726a989a 2725/* Given a statement STMT, which is either a PHI node or an assignment,
e67c25c7 2726 return the "rhs" of the node, in the case of a non-degenerate
726a989a 2727 phi, NULL is returned. */
e67c25c7
JL
2728
2729static tree
726a989a 2730get_rhs_or_phi_arg (gimple stmt)
e67c25c7 2731{
726a989a 2732 if (gimple_code (stmt) == GIMPLE_PHI)
538dd0b7 2733 return degenerate_phi_result (as_a <gphi *> (stmt));
726a989a
RB
2734 else if (gimple_assign_single_p (stmt))
2735 return gimple_assign_rhs1 (stmt);
2736 else
2737 gcc_unreachable ();
e67c25c7
JL
2738}
2739
2740
726a989a 2741/* Given a statement STMT, which is either a PHI node or an assignment,
e67c25c7
JL
2742 return the "lhs" of the node. */
2743
2744static tree
726a989a 2745get_lhs_or_phi_result (gimple stmt)
e67c25c7 2746{
726a989a
RB
2747 if (gimple_code (stmt) == GIMPLE_PHI)
2748 return gimple_phi_result (stmt);
2749 else if (is_gimple_assign (stmt))
2750 return gimple_assign_lhs (stmt);
2751 else
2752 gcc_unreachable ();
e67c25c7
JL
2753}
2754
2755/* Propagate RHS into all uses of LHS (when possible).
2756
2757 RHS and LHS are derived from STMT, which is passed in solely so
2758 that we can remove it if propagation is successful.
2759
2760 When propagating into a PHI node or into a statement which turns
2761 into a trivial copy or constant initialization, set the
2762 appropriate bit in INTERESTING_NAMEs so that we will visit those
2763 nodes as well in an effort to pick up secondary optimization
2764 opportunities. */
2765
b8698a0f 2766static void
726a989a 2767propagate_rhs_into_lhs (gimple stmt, tree lhs, tree rhs, bitmap interesting_names)
e67c25c7 2768{
a59d8e8e 2769 /* First verify that propagation is valid. */
406bfdd3 2770 if (may_propagate_copy (lhs, rhs))
e67c25c7
JL
2771 {
2772 use_operand_p use_p;
2773 imm_use_iterator iter;
726a989a 2774 gimple use_stmt;
e67c25c7
JL
2775 bool all = true;
2776
2777 /* Dump details. */
2778 if (dump_file && (dump_flags & TDF_DETAILS))
2779 {
2780 fprintf (dump_file, " Replacing '");
2781 print_generic_expr (dump_file, lhs, dump_flags);
2782 fprintf (dump_file, "' with %s '",
2783 (TREE_CODE (rhs) != SSA_NAME ? "constant" : "variable"));
2784 print_generic_expr (dump_file, rhs, dump_flags);
2785 fprintf (dump_file, "'\n");
2786 }
2787
b8698a0f 2788 /* Walk over every use of LHS and try to replace the use with RHS.
e67c25c7
JL
2789 At this point the only reason why such a propagation would not
2790 be successful would be if the use occurs in an ASM_EXPR. */
6c00f606 2791 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
e67c25c7 2792 {
b5b8b0ac
AO
2793 /* Leave debug stmts alone. If we succeed in propagating
2794 all non-debug uses, we'll drop the DEF, and propagation
2795 into debug stmts will occur then. */
2796 if (gimple_debug_bind_p (use_stmt))
2797 continue;
b8698a0f 2798
e67c25c7 2799 /* It's not always safe to propagate into an ASM_EXPR. */
726a989a
RB
2800 if (gimple_code (use_stmt) == GIMPLE_ASM
2801 && ! may_propagate_copy_into_asm (lhs))
e67c25c7
JL
2802 {
2803 all = false;
2804 continue;
2805 }
2806
720151ca
RG
2807 /* It's not ok to propagate into the definition stmt of RHS.
2808 <bb 9>:
2809 # prephitmp.12_36 = PHI <g_67.1_6(9)>
2810 g_67.1_6 = prephitmp.12_36;
2811 goto <bb 9>;
2812 While this is strictly all dead code we do not want to
2813 deal with this here. */
2814 if (TREE_CODE (rhs) == SSA_NAME
2815 && SSA_NAME_DEF_STMT (rhs) == use_stmt)
2816 {
2817 all = false;
2818 continue;
2819 }
2820
e67c25c7
JL
2821 /* Dump details. */
2822 if (dump_file && (dump_flags & TDF_DETAILS))
2823 {
2824 fprintf (dump_file, " Original statement:");
726a989a 2825 print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
e67c25c7
JL
2826 }
2827
cbc75e62 2828 /* Propagate the RHS into this use of the LHS. */
6c00f606
AM
2829 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
2830 propagate_value (use_p, rhs);
cbc75e62
JL
2831
2832 /* Special cases to avoid useless calls into the folding
2833 routines, operand scanning, etc.
2834
6b4a85ad 2835 Propagation into a PHI may cause the PHI to become
cbc75e62 2836 a degenerate, so mark the PHI as interesting. No other
6b4a85ad
RG
2837 actions are necessary. */
2838 if (gimple_code (use_stmt) == GIMPLE_PHI)
cbc75e62 2839 {
6b4a85ad
RG
2840 tree result;
2841
cbc75e62
JL
2842 /* Dump details. */
2843 if (dump_file && (dump_flags & TDF_DETAILS))
2844 {
2845 fprintf (dump_file, " Updated statement:");
726a989a 2846 print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
cbc75e62
JL
2847 }
2848
6b4a85ad
RG
2849 result = get_lhs_or_phi_result (use_stmt);
2850 bitmap_set_bit (interesting_names, SSA_NAME_VERSION (result));
cbc75e62
JL
2851 continue;
2852 }
2853
b8698a0f 2854 /* From this point onward we are propagating into a
cbc75e62
JL
2855 real statement. Folding may (or may not) be possible,
2856 we may expose new operands, expose dead EH edges,
2857 etc. */
726a989a
RB
2858 /* NOTE tuples. In the tuples world, fold_stmt_inplace
2859 cannot fold a call that simplifies to a constant,
2860 because the GIMPLE_CALL must be replaced by a
2861 GIMPLE_ASSIGN, and there is no way to effect such a
2862 transformation in-place. We might want to consider
2863 using the more general fold_stmt here. */
59401b92
RG
2864 {
2865 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
2866 fold_stmt_inplace (&gsi);
2867 }
3ae194cd
JL
2868
2869 /* Sometimes propagation can expose new operands to the
cff4e50d
PB
2870 renamer. */
2871 update_stmt (use_stmt);
e67c25c7
JL
2872
2873 /* Dump details. */
2874 if (dump_file && (dump_flags & TDF_DETAILS))
2875 {
2876 fprintf (dump_file, " Updated statement:");
726a989a 2877 print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
e67c25c7
JL
2878 }
2879
e67c25c7
JL
2880 /* If we replaced a variable index with a constant, then
2881 we would need to update the invariant flag for ADDR_EXPRs. */
726a989a
RB
2882 if (gimple_assign_single_p (use_stmt)
2883 && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ADDR_EXPR)
07beea0d 2884 recompute_tree_invariant_for_addr_expr
726a989a 2885 (gimple_assign_rhs1 (use_stmt));
e67c25c7
JL
2886
2887 /* If we cleaned up EH information from the statement,
72922229 2888 mark its containing block as needing EH cleanups. */
e67c25c7 2889 if (maybe_clean_or_replace_eh_stmt (use_stmt, use_stmt))
72922229 2890 {
726a989a 2891 bitmap_set_bit (need_eh_cleanup, gimple_bb (use_stmt)->index);
72922229
JL
2892 if (dump_file && (dump_flags & TDF_DETAILS))
2893 fprintf (dump_file, " Flagged to clear EH edges.\n");
2894 }
e67c25c7 2895
cbc75e62
JL
2896 /* Propagation may expose new trivial copy/constant propagation
2897 opportunities. */
726a989a
RB
2898 if (gimple_assign_single_p (use_stmt)
2899 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
2900 && (TREE_CODE (gimple_assign_rhs1 (use_stmt)) == SSA_NAME
2901 || is_gimple_min_invariant (gimple_assign_rhs1 (use_stmt))))
2902 {
e67c25c7
JL
2903 tree result = get_lhs_or_phi_result (use_stmt);
2904 bitmap_set_bit (interesting_names, SSA_NAME_VERSION (result));
2905 }
2906
2907 /* Propagation into these nodes may make certain edges in
2908 the CFG unexecutable. We want to identify them as PHI nodes
2909 at the destination of those unexecutable edges may become
2910 degenerates. */
726a989a
RB
2911 else if (gimple_code (use_stmt) == GIMPLE_COND
2912 || gimple_code (use_stmt) == GIMPLE_SWITCH
2913 || gimple_code (use_stmt) == GIMPLE_GOTO)
2914 {
e67c25c7
JL
2915 tree val;
2916
726a989a 2917 if (gimple_code (use_stmt) == GIMPLE_COND)
db3927fb
AH
2918 val = fold_binary_loc (gimple_location (use_stmt),
2919 gimple_cond_code (use_stmt),
726a989a
RB
2920 boolean_type_node,
2921 gimple_cond_lhs (use_stmt),
2922 gimple_cond_rhs (use_stmt));
2923 else if (gimple_code (use_stmt) == GIMPLE_SWITCH)
538dd0b7 2924 val = gimple_switch_index (as_a <gswitch *> (use_stmt));
e67c25c7 2925 else
726a989a 2926 val = gimple_goto_dest (use_stmt);
e67c25c7 2927
726a989a 2928 if (val && is_gimple_min_invariant (val))
e67c25c7 2929 {
726a989a 2930 basic_block bb = gimple_bb (use_stmt);
e67c25c7
JL
2931 edge te = find_taken_edge (bb, val);
2932 edge_iterator ei;
2933 edge e;
538dd0b7
DM
2934 gimple_stmt_iterator gsi;
2935 gphi_iterator psi;
e67c25c7
JL
2936
2937 /* Remove all outgoing edges except TE. */
2938 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei));)
2939 {
2940 if (e != te)
2941 {
e67c25c7
JL
2942 /* Mark all the PHI nodes at the destination of
2943 the unexecutable edge as interesting. */
726a989a
RB
2944 for (psi = gsi_start_phis (e->dest);
2945 !gsi_end_p (psi);
2946 gsi_next (&psi))
2947 {
538dd0b7 2948 gphi *phi = psi.phi ();
726a989a
RB
2949
2950 tree result = gimple_phi_result (phi);
e67c25c7
JL
2951 int version = SSA_NAME_VERSION (result);
2952
2953 bitmap_set_bit (interesting_names, version);
2954 }
2955
2956 te->probability += e->probability;
2957
2958 te->count += e->count;
2959 remove_edge (e);
8d9d6561 2960 cfg_altered = true;
e67c25c7
JL
2961 }
2962 else
2963 ei_next (&ei);
2964 }
2965
726a989a
RB
2966 gsi = gsi_last_bb (gimple_bb (use_stmt));
2967 gsi_remove (&gsi, true);
e67c25c7
JL
2968
2969 /* And fixup the flags on the single remaining edge. */
2970 te->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
2971 te->flags &= ~EDGE_ABNORMAL;
2972 te->flags |= EDGE_FALLTHRU;
2973 if (te->probability > REG_BR_PROB_BASE)
2974 te->probability = REG_BR_PROB_BASE;
2975 }
2976 }
2977 }
2978
b8698a0f 2979 /* Ensure there is nothing else to do. */
a5f84464 2980 gcc_assert (!all || has_zero_uses (lhs));
243cc836 2981
e67c25c7
JL
2982 /* If we were able to propagate away all uses of LHS, then
2983 we can remove STMT. */
2984 if (all)
2985 remove_stmt_or_phi (stmt);
2986 }
2987}
2988
726a989a 2989/* STMT is either a PHI node (potentially a degenerate PHI node) or
e67c25c7
JL
2990 a statement that is a trivial copy or constant initialization.
2991
2992 Attempt to eliminate T by propagating its RHS into all uses of
2993 its LHS. This may in turn set new bits in INTERESTING_NAMES
2994 for nodes we want to revisit later.
2995
2996 All exit paths should clear INTERESTING_NAMES for the result
726a989a 2997 of STMT. */
e67c25c7
JL
2998
2999static void
726a989a 3000eliminate_const_or_copy (gimple stmt, bitmap interesting_names)
e67c25c7 3001{
726a989a 3002 tree lhs = get_lhs_or_phi_result (stmt);
e67c25c7
JL
3003 tree rhs;
3004 int version = SSA_NAME_VERSION (lhs);
3005
3006 /* If the LHS of this statement or PHI has no uses, then we can
3007 just eliminate it. This can occur if, for example, the PHI
3008 was created by block duplication due to threading and its only
3009 use was in the conditional at the end of the block which was
3010 deleted. */
3011 if (has_zero_uses (lhs))
3012 {
3013 bitmap_clear_bit (interesting_names, version);
726a989a 3014 remove_stmt_or_phi (stmt);
e67c25c7
JL
3015 return;
3016 }
3017
3018 /* Get the RHS of the assignment or PHI node if the PHI is a
3019 degenerate. */
726a989a 3020 rhs = get_rhs_or_phi_arg (stmt);
e67c25c7
JL
3021 if (!rhs)
3022 {
3023 bitmap_clear_bit (interesting_names, version);
3024 return;
3025 }
3026
bd388c2a
RB
3027 if (!virtual_operand_p (lhs))
3028 propagate_rhs_into_lhs (stmt, lhs, rhs, interesting_names);
3029 else
3030 {
3031 gimple use_stmt;
3032 imm_use_iterator iter;
3033 use_operand_p use_p;
3034 /* For virtual operands we have to propagate into all uses as
3035 otherwise we will create overlapping life-ranges. */
3036 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
3037 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
3038 SET_USE (use_p, rhs);
3039 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3040 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs) = 1;
3041 remove_stmt_or_phi (stmt);
3042 }
e67c25c7 3043
726a989a 3044 /* Note that STMT may well have been deleted by now, so do
e67c25c7
JL
3045 not access it, instead use the saved version # to clear
3046 T's entry in the worklist. */
3047 bitmap_clear_bit (interesting_names, version);
3048}
3049
3050/* The first phase in degenerate PHI elimination.
3051
3052 Eliminate the degenerate PHIs in BB, then recurse on the
3053 dominator children of BB. */
3054
3055static void
3056eliminate_degenerate_phis_1 (basic_block bb, bitmap interesting_names)
3057{
538dd0b7 3058 gphi_iterator gsi;
e67c25c7
JL
3059 basic_block son;
3060
726a989a 3061 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
e67c25c7 3062 {
538dd0b7 3063 gphi *phi = gsi.phi ();
726a989a 3064
e67c25c7
JL
3065 eliminate_const_or_copy (phi, interesting_names);
3066 }
3067
3068 /* Recurse into the dominator children of BB. */
3069 for (son = first_dom_son (CDI_DOMINATORS, bb);
3070 son;
3071 son = next_dom_son (CDI_DOMINATORS, son))
3072 eliminate_degenerate_phis_1 (son, interesting_names);
3073}
3074
3075
3076/* A very simple pass to eliminate degenerate PHI nodes from the
3077 IL. This is meant to be fast enough to be able to be run several
3078 times in the optimization pipeline.
3079
3080 Certain optimizations, particularly those which duplicate blocks
3081 or remove edges from the CFG can create or expose PHIs which are
3082 trivial copies or constant initializations.
3083
3084 While we could pick up these optimizations in DOM or with the
3085 combination of copy-prop and CCP, those solutions are far too
3086 heavy-weight for our needs.
3087
3088 This implementation has two phases so that we can efficiently
3089 eliminate the first order degenerate PHIs and second order
3090 degenerate PHIs.
3091
3092 The first phase performs a dominator walk to identify and eliminate
3093 the vast majority of the degenerate PHIs. When a degenerate PHI
3094 is identified and eliminated any affected statements or PHIs
3095 are put on a worklist.
3096
3097 The second phase eliminates degenerate PHIs and trivial copies
3098 or constant initializations using the worklist. This is how we
3099 pick up the secondary optimization opportunities with minimal
3100 cost. */
3101
be55bfe6
TS
3102namespace {
3103
3104const pass_data pass_data_phi_only_cprop =
3105{
3106 GIMPLE_PASS, /* type */
3107 "phicprop", /* name */
3108 OPTGROUP_NONE, /* optinfo_flags */
be55bfe6
TS
3109 TV_TREE_PHI_CPROP, /* tv_id */
3110 ( PROP_cfg | PROP_ssa ), /* properties_required */
3111 0, /* properties_provided */
3112 0, /* properties_destroyed */
3113 0, /* todo_flags_start */
3bea341f 3114 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
be55bfe6
TS
3115};
3116
3117class pass_phi_only_cprop : public gimple_opt_pass
3118{
3119public:
3120 pass_phi_only_cprop (gcc::context *ctxt)
3121 : gimple_opt_pass (pass_data_phi_only_cprop, ctxt)
3122 {}
3123
3124 /* opt_pass methods: */
3125 opt_pass * clone () { return new pass_phi_only_cprop (m_ctxt); }
3126 virtual bool gate (function *) { return flag_tree_dom != 0; }
3127 virtual unsigned int execute (function *);
3128
3129}; // class pass_phi_only_cprop
3130
3131unsigned int
3132pass_phi_only_cprop::execute (function *fun)
e67c25c7
JL
3133{
3134 bitmap interesting_names;
1f70491b 3135 bitmap interesting_names1;
e67c25c7 3136
72922229
JL
3137 /* Bitmap of blocks which need EH information updated. We can not
3138 update it on-the-fly as doing so invalidates the dominator tree. */
3139 need_eh_cleanup = BITMAP_ALLOC (NULL);
3140
e67c25c7
JL
3141 /* INTERESTING_NAMES is effectively our worklist, indexed by
3142 SSA_NAME_VERSION.
3143
3144 A set bit indicates that the statement or PHI node which
3145 defines the SSA_NAME should be (re)examined to determine if
66a4ad37 3146 it has become a degenerate PHI or trivial const/copy propagation
b8698a0f 3147 opportunity.
e67c25c7
JL
3148
3149 Experiments have show we generally get better compilation
3150 time behavior with bitmaps rather than sbitmaps. */
3151 interesting_names = BITMAP_ALLOC (NULL);
1f70491b 3152 interesting_names1 = BITMAP_ALLOC (NULL);
e67c25c7 3153
8d9d6561
EB
3154 calculate_dominance_info (CDI_DOMINATORS);
3155 cfg_altered = false;
3156
917f1b7e 3157 /* First phase. Eliminate degenerate PHIs via a dominator
e67c25c7
JL
3158 walk of the CFG.
3159
3160 Experiments have indicated that we generally get better
3161 compile-time behavior by visiting blocks in the first
3162 phase in dominator order. Presumably this is because walking
3163 in dominator order leaves fewer PHIs for later examination
3164 by the worklist phase. */
be55bfe6 3165 eliminate_degenerate_phis_1 (ENTRY_BLOCK_PTR_FOR_FN (fun),
fefa31b5 3166 interesting_names);
e67c25c7 3167
917f1b7e 3168 /* Second phase. Eliminate second order degenerate PHIs as well
e67c25c7
JL
3169 as trivial copies or constant initializations identified by
3170 the first phase or this phase. Basically we keep iterating
3171 until our set of INTERESTING_NAMEs is empty. */
3172 while (!bitmap_empty_p (interesting_names))
3173 {
3174 unsigned int i;
3175 bitmap_iterator bi;
3176
1f70491b
AP
3177 /* EXECUTE_IF_SET_IN_BITMAP does not like its bitmap
3178 changed during the loop. Copy it to another bitmap and
3179 use that. */
3180 bitmap_copy (interesting_names1, interesting_names);
3181
3182 EXECUTE_IF_SET_IN_BITMAP (interesting_names1, 0, i, bi)
e67c25c7
JL
3183 {
3184 tree name = ssa_name (i);
3185
3186 /* Ignore SSA_NAMEs that have been released because
3187 their defining statement was deleted (unreachable). */
3188 if (name)
3189 eliminate_const_or_copy (SSA_NAME_DEF_STMT (ssa_name (i)),
3190 interesting_names);
3191 }
3192 }
72922229 3193
8d9d6561 3194 if (cfg_altered)
83ba4d6f
RB
3195 {
3196 free_dominance_info (CDI_DOMINATORS);
3197 /* If we changed the CFG schedule loops for fixup by cfgcleanup. */
726338f4 3198 loops_state_set (LOOPS_NEED_FIXUP);
83ba4d6f 3199 }
8d9d6561 3200
72922229
JL
3201 /* Propagation of const and copies may make some EH edges dead. Purge
3202 such edges from the CFG as needed. */
3203 if (!bitmap_empty_p (need_eh_cleanup))
3204 {
726a989a 3205 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
72922229
JL
3206 BITMAP_FREE (need_eh_cleanup);
3207 }
e67c25c7
JL
3208
3209 BITMAP_FREE (interesting_names);
1f70491b 3210 BITMAP_FREE (interesting_names1);
e67c25c7
JL
3211 return 0;
3212}
3213
27a4cd48
DM
3214} // anon namespace
3215
3216gimple_opt_pass *
3217make_pass_phi_only_cprop (gcc::context *ctxt)
3218{
3219 return new pass_phi_only_cprop (ctxt);
3220}