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