[RTL-ifcvt] PR rtl-optimization/68506: Fix emitting order of insns in IF-THEN-JOIN...
[official-gcc.git] / gcc / tree-ssa-dom.c
blobaeb726c9bdbb6883e535694580a3865205cc20cb
1 /* SSA Dominator optimizations for trees
2 Copyright (C) 2001-2015 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "tree-pass.h"
28 #include "ssa.h"
29 #include "gimple-pretty-print.h"
30 #include "fold-const.h"
31 #include "cfganal.h"
32 #include "cfgloop.h"
33 #include "gimple-fold.h"
34 #include "tree-eh.h"
35 #include "gimple-iterator.h"
36 #include "tree-cfg.h"
37 #include "tree-into-ssa.h"
38 #include "domwalk.h"
39 #include "tree-ssa-propagate.h"
40 #include "tree-ssa-threadupdate.h"
41 #include "params.h"
42 #include "tree-ssa-scopedtables.h"
43 #include "tree-ssa-threadedge.h"
44 #include "tree-ssa-dom.h"
45 #include "gimplify.h"
46 #include "tree-cfgcleanup.h"
48 /* This file implements optimizations on the dominator tree. */
50 /* Structure for recording known values of a conditional expression
51 at the exits from its block. */
53 struct cond_equivalence
55 struct hashable_expr cond;
56 tree value;
59 /* Structure for recording edge equivalences.
61 Computing and storing the edge equivalences instead of creating
62 them on-demand can save significant amounts of time, particularly
63 for pathological cases involving switch statements.
65 These structures live for a single iteration of the dominator
66 optimizer in the edge's AUX field. At the end of an iteration we
67 free each of these structures. */
69 struct edge_info
71 /* If this edge creates a simple equivalence, the LHS and RHS of
72 the equivalence will be stored here. */
73 tree lhs;
74 tree rhs;
76 /* Traversing an edge may also indicate one or more particular conditions
77 are true or false. */
78 vec<cond_equivalence> cond_equivalences;
81 /* Track whether or not we have changed the control flow graph. */
82 static bool cfg_altered;
84 /* Bitmap of blocks that have had EH statements cleaned. We should
85 remove their dead edges eventually. */
86 static bitmap need_eh_cleanup;
87 static vec<gimple *> need_noreturn_fixup;
89 /* Statistics for dominator optimizations. */
90 struct opt_stats_d
92 long num_stmts;
93 long num_exprs_considered;
94 long num_re;
95 long num_const_prop;
96 long num_copy_prop;
99 static struct opt_stats_d opt_stats;
101 /* Local functions. */
102 static void optimize_stmt (basic_block, gimple_stmt_iterator,
103 class const_and_copies *,
104 class avail_exprs_stack *);
105 static tree lookup_avail_expr (gimple *, bool, class avail_exprs_stack *);
106 static void record_cond (cond_equivalence *, class avail_exprs_stack *);
107 static void record_equality (tree, tree, class const_and_copies *);
108 static void record_equivalences_from_phis (basic_block);
109 static void record_equivalences_from_incoming_edge (basic_block,
110 class const_and_copies *,
111 class avail_exprs_stack *);
112 static void eliminate_redundant_computations (gimple_stmt_iterator *,
113 class const_and_copies *,
114 class avail_exprs_stack *);
115 static void record_equivalences_from_stmt (gimple *, int,
116 class avail_exprs_stack *);
117 static edge single_incoming_edge_ignoring_loop_edges (basic_block);
118 static void dump_dominator_optimization_stats (FILE *file,
119 hash_table<expr_elt_hasher> *);
122 /* Free the edge_info data attached to E, if it exists. */
124 void
125 free_dom_edge_info (edge e)
127 struct edge_info *edge_info = (struct edge_info *)e->aux;
129 if (edge_info)
131 edge_info->cond_equivalences.release ();
132 free (edge_info);
136 /* Allocate an EDGE_INFO for edge E and attach it to E.
137 Return the new EDGE_INFO structure. */
139 static struct edge_info *
140 allocate_edge_info (edge e)
142 struct edge_info *edge_info;
144 /* Free the old one, if it exists. */
145 free_dom_edge_info (e);
147 edge_info = XCNEW (struct edge_info);
149 e->aux = edge_info;
150 return edge_info;
153 /* Free all EDGE_INFO structures associated with edges in the CFG.
154 If a particular edge can be threaded, copy the redirection
155 target from the EDGE_INFO structure into the edge's AUX field
156 as required by code to update the CFG and SSA graph for
157 jump threading. */
159 static void
160 free_all_edge_infos (void)
162 basic_block bb;
163 edge_iterator ei;
164 edge e;
166 FOR_EACH_BB_FN (bb, cfun)
168 FOR_EACH_EDGE (e, ei, bb->preds)
170 free_dom_edge_info (e);
171 e->aux = NULL;
176 /* Build a cond_equivalence record indicating that the comparison
177 CODE holds between operands OP0 and OP1 and push it to **P. */
179 static void
180 build_and_record_new_cond (enum tree_code code,
181 tree op0, tree op1,
182 vec<cond_equivalence> *p,
183 bool val = true)
185 cond_equivalence c;
186 struct hashable_expr *cond = &c.cond;
188 gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
190 cond->type = boolean_type_node;
191 cond->kind = EXPR_BINARY;
192 cond->ops.binary.op = code;
193 cond->ops.binary.opnd0 = op0;
194 cond->ops.binary.opnd1 = op1;
196 c.value = val ? boolean_true_node : boolean_false_node;
197 p->safe_push (c);
200 /* Record that COND is true and INVERTED is false into the edge information
201 structure. Also record that any conditions dominated by COND are true
202 as well.
204 For example, if a < b is true, then a <= b must also be true. */
206 static void
207 record_conditions (struct edge_info *edge_info, tree cond, tree inverted)
209 tree op0, op1;
210 cond_equivalence c;
212 if (!COMPARISON_CLASS_P (cond))
213 return;
215 op0 = TREE_OPERAND (cond, 0);
216 op1 = TREE_OPERAND (cond, 1);
218 switch (TREE_CODE (cond))
220 case LT_EXPR:
221 case GT_EXPR:
222 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
224 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
225 &edge_info->cond_equivalences);
226 build_and_record_new_cond (LTGT_EXPR, op0, op1,
227 &edge_info->cond_equivalences);
230 build_and_record_new_cond ((TREE_CODE (cond) == LT_EXPR
231 ? LE_EXPR : GE_EXPR),
232 op0, op1, &edge_info->cond_equivalences);
233 build_and_record_new_cond (NE_EXPR, op0, op1,
234 &edge_info->cond_equivalences);
235 build_and_record_new_cond (EQ_EXPR, op0, op1,
236 &edge_info->cond_equivalences, false);
237 break;
239 case GE_EXPR:
240 case LE_EXPR:
241 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
243 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
244 &edge_info->cond_equivalences);
246 break;
248 case EQ_EXPR:
249 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
251 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
252 &edge_info->cond_equivalences);
254 build_and_record_new_cond (LE_EXPR, op0, op1,
255 &edge_info->cond_equivalences);
256 build_and_record_new_cond (GE_EXPR, op0, op1,
257 &edge_info->cond_equivalences);
258 break;
260 case UNORDERED_EXPR:
261 build_and_record_new_cond (NE_EXPR, op0, op1,
262 &edge_info->cond_equivalences);
263 build_and_record_new_cond (UNLE_EXPR, op0, op1,
264 &edge_info->cond_equivalences);
265 build_and_record_new_cond (UNGE_EXPR, op0, op1,
266 &edge_info->cond_equivalences);
267 build_and_record_new_cond (UNEQ_EXPR, op0, op1,
268 &edge_info->cond_equivalences);
269 build_and_record_new_cond (UNLT_EXPR, op0, op1,
270 &edge_info->cond_equivalences);
271 build_and_record_new_cond (UNGT_EXPR, op0, op1,
272 &edge_info->cond_equivalences);
273 break;
275 case UNLT_EXPR:
276 case UNGT_EXPR:
277 build_and_record_new_cond ((TREE_CODE (cond) == UNLT_EXPR
278 ? UNLE_EXPR : UNGE_EXPR),
279 op0, op1, &edge_info->cond_equivalences);
280 build_and_record_new_cond (NE_EXPR, op0, op1,
281 &edge_info->cond_equivalences);
282 break;
284 case UNEQ_EXPR:
285 build_and_record_new_cond (UNLE_EXPR, op0, op1,
286 &edge_info->cond_equivalences);
287 build_and_record_new_cond (UNGE_EXPR, op0, op1,
288 &edge_info->cond_equivalences);
289 break;
291 case LTGT_EXPR:
292 build_and_record_new_cond (NE_EXPR, op0, op1,
293 &edge_info->cond_equivalences);
294 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
295 &edge_info->cond_equivalences);
296 break;
298 default:
299 break;
302 /* Now store the original true and false conditions into the first
303 two slots. */
304 initialize_expr_from_cond (cond, &c.cond);
305 c.value = boolean_true_node;
306 edge_info->cond_equivalences.safe_push (c);
308 /* It is possible for INVERTED to be the negation of a comparison,
309 and not a valid RHS or GIMPLE_COND condition. This happens because
310 invert_truthvalue may return such an expression when asked to invert
311 a floating-point comparison. These comparisons are not assumed to
312 obey the trichotomy law. */
313 initialize_expr_from_cond (inverted, &c.cond);
314 c.value = boolean_false_node;
315 edge_info->cond_equivalences.safe_push (c);
318 /* We have finished optimizing BB, record any information implied by
319 taking a specific outgoing edge from BB. */
321 static void
322 record_edge_info (basic_block bb)
324 gimple_stmt_iterator gsi = gsi_last_bb (bb);
325 struct edge_info *edge_info;
327 if (! gsi_end_p (gsi))
329 gimple *stmt = gsi_stmt (gsi);
330 location_t loc = gimple_location (stmt);
332 if (gimple_code (stmt) == GIMPLE_SWITCH)
334 gswitch *switch_stmt = as_a <gswitch *> (stmt);
335 tree index = gimple_switch_index (switch_stmt);
337 if (TREE_CODE (index) == SSA_NAME)
339 int i;
340 int n_labels = gimple_switch_num_labels (switch_stmt);
341 tree *info = XCNEWVEC (tree, last_basic_block_for_fn (cfun));
342 edge e;
343 edge_iterator ei;
345 for (i = 0; i < n_labels; i++)
347 tree label = gimple_switch_label (switch_stmt, i);
348 basic_block target_bb = label_to_block (CASE_LABEL (label));
349 if (CASE_HIGH (label)
350 || !CASE_LOW (label)
351 || info[target_bb->index])
352 info[target_bb->index] = error_mark_node;
353 else
354 info[target_bb->index] = label;
357 FOR_EACH_EDGE (e, ei, bb->succs)
359 basic_block target_bb = e->dest;
360 tree label = info[target_bb->index];
362 if (label != NULL && label != error_mark_node)
364 tree x = fold_convert_loc (loc, TREE_TYPE (index),
365 CASE_LOW (label));
366 edge_info = allocate_edge_info (e);
367 edge_info->lhs = index;
368 edge_info->rhs = x;
371 free (info);
375 /* A COND_EXPR may create equivalences too. */
376 if (gimple_code (stmt) == GIMPLE_COND)
378 edge true_edge;
379 edge false_edge;
381 tree op0 = gimple_cond_lhs (stmt);
382 tree op1 = gimple_cond_rhs (stmt);
383 enum tree_code code = gimple_cond_code (stmt);
385 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
387 /* Special case comparing booleans against a constant as we
388 know the value of OP0 on both arms of the branch. i.e., we
389 can record an equivalence for OP0 rather than COND. */
390 if ((code == EQ_EXPR || code == NE_EXPR)
391 && TREE_CODE (op0) == SSA_NAME
392 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
393 && is_gimple_min_invariant (op1))
395 if (code == EQ_EXPR)
397 edge_info = allocate_edge_info (true_edge);
398 edge_info->lhs = op0;
399 edge_info->rhs = (integer_zerop (op1)
400 ? boolean_false_node
401 : boolean_true_node);
403 edge_info = allocate_edge_info (false_edge);
404 edge_info->lhs = op0;
405 edge_info->rhs = (integer_zerop (op1)
406 ? boolean_true_node
407 : boolean_false_node);
409 else
411 edge_info = allocate_edge_info (true_edge);
412 edge_info->lhs = op0;
413 edge_info->rhs = (integer_zerop (op1)
414 ? boolean_true_node
415 : boolean_false_node);
417 edge_info = allocate_edge_info (false_edge);
418 edge_info->lhs = op0;
419 edge_info->rhs = (integer_zerop (op1)
420 ? boolean_false_node
421 : boolean_true_node);
424 else if (is_gimple_min_invariant (op0)
425 && (TREE_CODE (op1) == SSA_NAME
426 || is_gimple_min_invariant (op1)))
428 tree cond = build2 (code, boolean_type_node, op0, op1);
429 tree inverted = invert_truthvalue_loc (loc, cond);
430 bool can_infer_simple_equiv
431 = !(HONOR_SIGNED_ZEROS (op0)
432 && real_zerop (op0));
433 struct edge_info *edge_info;
435 edge_info = allocate_edge_info (true_edge);
436 record_conditions (edge_info, cond, inverted);
438 if (can_infer_simple_equiv && code == EQ_EXPR)
440 edge_info->lhs = op1;
441 edge_info->rhs = op0;
444 edge_info = allocate_edge_info (false_edge);
445 record_conditions (edge_info, inverted, cond);
447 if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
449 edge_info->lhs = op1;
450 edge_info->rhs = op0;
454 else if (TREE_CODE (op0) == SSA_NAME
455 && (TREE_CODE (op1) == SSA_NAME
456 || is_gimple_min_invariant (op1)))
458 tree cond = build2 (code, boolean_type_node, op0, op1);
459 tree inverted = invert_truthvalue_loc (loc, cond);
460 bool can_infer_simple_equiv
461 = !(HONOR_SIGNED_ZEROS (op1)
462 && (TREE_CODE (op1) == SSA_NAME || real_zerop (op1)));
463 struct edge_info *edge_info;
465 edge_info = allocate_edge_info (true_edge);
466 record_conditions (edge_info, cond, inverted);
468 if (can_infer_simple_equiv && code == EQ_EXPR)
470 edge_info->lhs = op0;
471 edge_info->rhs = op1;
474 edge_info = allocate_edge_info (false_edge);
475 record_conditions (edge_info, inverted, cond);
477 if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
479 edge_info->lhs = op0;
480 edge_info->rhs = op1;
485 /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
490 class dom_opt_dom_walker : public dom_walker
492 public:
493 dom_opt_dom_walker (cdi_direction direction,
494 class const_and_copies *const_and_copies,
495 class avail_exprs_stack *avail_exprs_stack)
496 : dom_walker (direction),
497 m_const_and_copies (const_and_copies),
498 m_avail_exprs_stack (avail_exprs_stack),
499 m_dummy_cond (NULL) {}
501 virtual void before_dom_children (basic_block);
502 virtual void after_dom_children (basic_block);
504 private:
505 void thread_across_edge (edge);
507 /* Unwindable equivalences, both const/copy and expression varieties. */
508 class const_and_copies *m_const_and_copies;
509 class avail_exprs_stack *m_avail_exprs_stack;
511 gcond *m_dummy_cond;
514 /* Jump threading, redundancy elimination and const/copy propagation.
516 This pass may expose new symbols that need to be renamed into SSA. For
517 every new symbol exposed, its corresponding bit will be set in
518 VARS_TO_RENAME. */
520 namespace {
522 const pass_data pass_data_dominator =
524 GIMPLE_PASS, /* type */
525 "dom", /* name */
526 OPTGROUP_NONE, /* optinfo_flags */
527 TV_TREE_SSA_DOMINATOR_OPTS, /* tv_id */
528 ( PROP_cfg | PROP_ssa ), /* properties_required */
529 0, /* properties_provided */
530 0, /* properties_destroyed */
531 0, /* todo_flags_start */
532 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
535 class pass_dominator : public gimple_opt_pass
537 public:
538 pass_dominator (gcc::context *ctxt)
539 : gimple_opt_pass (pass_data_dominator, ctxt),
540 may_peel_loop_headers_p (false)
543 /* opt_pass methods: */
544 opt_pass * clone () { return new pass_dominator (m_ctxt); }
545 void set_pass_param (unsigned int n, bool param)
547 gcc_assert (n == 0);
548 may_peel_loop_headers_p = param;
550 virtual bool gate (function *) { return flag_tree_dom != 0; }
551 virtual unsigned int execute (function *);
553 private:
554 /* This flag is used to prevent loops from being peeled repeatedly in jump
555 threading; it will be removed once we preserve loop structures throughout
556 the compilation -- we will be able to mark the affected loops directly in
557 jump threading, and avoid peeling them next time. */
558 bool may_peel_loop_headers_p;
559 }; // class pass_dominator
561 unsigned int
562 pass_dominator::execute (function *fun)
564 memset (&opt_stats, 0, sizeof (opt_stats));
566 /* Create our hash tables. */
567 hash_table<expr_elt_hasher> *avail_exprs
568 = new hash_table<expr_elt_hasher> (1024);
569 class avail_exprs_stack *avail_exprs_stack
570 = new class avail_exprs_stack (avail_exprs);
571 class const_and_copies *const_and_copies = new class const_and_copies ();
572 need_eh_cleanup = BITMAP_ALLOC (NULL);
573 need_noreturn_fixup.create (0);
575 calculate_dominance_info (CDI_DOMINATORS);
576 cfg_altered = false;
578 /* We need to know loop structures in order to avoid destroying them
579 in jump threading. Note that we still can e.g. thread through loop
580 headers to an exit edge, or through loop header to the loop body, assuming
581 that we update the loop info.
583 TODO: We don't need to set LOOPS_HAVE_PREHEADERS generally, but due
584 to several overly conservative bail-outs in jump threading, case
585 gcc.dg/tree-ssa/pr21417.c can't be threaded if loop preheader is
586 missing. We should improve jump threading in future then
587 LOOPS_HAVE_PREHEADERS won't be needed here. */
588 loop_optimizer_init (LOOPS_HAVE_PREHEADERS | LOOPS_HAVE_SIMPLE_LATCHES);
590 /* Initialize the value-handle array. */
591 threadedge_initialize_values ();
593 /* We need accurate information regarding back edges in the CFG
594 for jump threading; this may include back edges that are not part of
595 a single loop. */
596 mark_dfs_back_edges ();
598 /* We want to create the edge info structures before the dominator walk
599 so that they'll be in place for the jump threader, particularly when
600 threading through a join block.
602 The conditions will be lazily updated with global equivalences as
603 we reach them during the dominator walk. */
604 basic_block bb;
605 FOR_EACH_BB_FN (bb, fun)
606 record_edge_info (bb);
608 /* Recursively walk the dominator tree optimizing statements. */
609 dom_opt_dom_walker walker (CDI_DOMINATORS,
610 const_and_copies,
611 avail_exprs_stack);
612 walker.walk (fun->cfg->x_entry_block_ptr);
615 gimple_stmt_iterator gsi;
616 basic_block bb;
617 FOR_EACH_BB_FN (bb, fun)
619 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
620 update_stmt_if_modified (gsi_stmt (gsi));
624 /* If we exposed any new variables, go ahead and put them into
625 SSA form now, before we handle jump threading. This simplifies
626 interactions between rewriting of _DECL nodes into SSA form
627 and rewriting SSA_NAME nodes into SSA form after block
628 duplication and CFG manipulation. */
629 update_ssa (TODO_update_ssa);
631 free_all_edge_infos ();
633 /* Thread jumps, creating duplicate blocks as needed. */
634 cfg_altered |= thread_through_all_blocks (may_peel_loop_headers_p);
636 if (cfg_altered)
637 free_dominance_info (CDI_DOMINATORS);
639 /* Removal of statements may make some EH edges dead. Purge
640 such edges from the CFG as needed. */
641 if (!bitmap_empty_p (need_eh_cleanup))
643 unsigned i;
644 bitmap_iterator bi;
646 /* Jump threading may have created forwarder blocks from blocks
647 needing EH cleanup; the new successor of these blocks, which
648 has inherited from the original block, needs the cleanup.
649 Don't clear bits in the bitmap, as that can break the bitmap
650 iterator. */
651 EXECUTE_IF_SET_IN_BITMAP (need_eh_cleanup, 0, i, bi)
653 basic_block bb = BASIC_BLOCK_FOR_FN (fun, i);
654 if (bb == NULL)
655 continue;
656 while (single_succ_p (bb)
657 && (single_succ_edge (bb)->flags & EDGE_EH) == 0)
658 bb = single_succ (bb);
659 if (bb == EXIT_BLOCK_PTR_FOR_FN (fun))
660 continue;
661 if ((unsigned) bb->index != i)
662 bitmap_set_bit (need_eh_cleanup, bb->index);
665 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
666 bitmap_clear (need_eh_cleanup);
669 /* Fixup stmts that became noreturn calls. This may require splitting
670 blocks and thus isn't possible during the dominator walk or before
671 jump threading finished. Do this in reverse order so we don't
672 inadvertedly remove a stmt we want to fixup by visiting a dominating
673 now noreturn call first. */
674 while (!need_noreturn_fixup.is_empty ())
676 gimple *stmt = need_noreturn_fixup.pop ();
677 if (dump_file && dump_flags & TDF_DETAILS)
679 fprintf (dump_file, "Fixing up noreturn call ");
680 print_gimple_stmt (dump_file, stmt, 0, 0);
681 fprintf (dump_file, "\n");
683 fixup_noreturn_call (stmt);
686 statistics_counter_event (fun, "Redundant expressions eliminated",
687 opt_stats.num_re);
688 statistics_counter_event (fun, "Constants propagated",
689 opt_stats.num_const_prop);
690 statistics_counter_event (fun, "Copies propagated",
691 opt_stats.num_copy_prop);
693 /* Debugging dumps. */
694 if (dump_file && (dump_flags & TDF_STATS))
695 dump_dominator_optimization_stats (dump_file, avail_exprs);
697 loop_optimizer_finalize ();
699 /* Delete our main hashtable. */
700 delete avail_exprs;
701 avail_exprs = NULL;
703 /* Free asserted bitmaps and stacks. */
704 BITMAP_FREE (need_eh_cleanup);
705 need_noreturn_fixup.release ();
706 delete avail_exprs_stack;
707 delete const_and_copies;
709 /* Free the value-handle array. */
710 threadedge_finalize_values ();
712 return 0;
715 } // anon namespace
717 gimple_opt_pass *
718 make_pass_dominator (gcc::context *ctxt)
720 return new pass_dominator (ctxt);
724 /* Given a conditional statement CONDSTMT, convert the
725 condition to a canonical form. */
727 static void
728 canonicalize_comparison (gcond *condstmt)
730 tree op0;
731 tree op1;
732 enum tree_code code;
734 gcc_assert (gimple_code (condstmt) == GIMPLE_COND);
736 op0 = gimple_cond_lhs (condstmt);
737 op1 = gimple_cond_rhs (condstmt);
739 code = gimple_cond_code (condstmt);
741 /* If it would be profitable to swap the operands, then do so to
742 canonicalize the statement, enabling better optimization.
744 By placing canonicalization of such expressions here we
745 transparently keep statements in canonical form, even
746 when the statement is modified. */
747 if (tree_swap_operands_p (op0, op1, false))
749 /* For relationals we need to swap the operands
750 and change the code. */
751 if (code == LT_EXPR
752 || code == GT_EXPR
753 || code == LE_EXPR
754 || code == GE_EXPR)
756 code = swap_tree_comparison (code);
758 gimple_cond_set_code (condstmt, code);
759 gimple_cond_set_lhs (condstmt, op1);
760 gimple_cond_set_rhs (condstmt, op0);
762 update_stmt (condstmt);
767 /* A trivial wrapper so that we can present the generic jump
768 threading code with a simple API for simplifying statements. */
769 static tree
770 simplify_stmt_for_jump_threading (gimple *stmt,
771 gimple *within_stmt ATTRIBUTE_UNUSED,
772 class avail_exprs_stack *avail_exprs_stack)
774 return lookup_avail_expr (stmt, false, avail_exprs_stack);
777 /* Valueize hook for gimple_fold_stmt_to_constant_1. */
779 static tree
780 dom_valueize (tree t)
782 if (TREE_CODE (t) == SSA_NAME)
784 tree tem = SSA_NAME_VALUE (t);
785 if (tem)
786 return tem;
788 return t;
791 /* Record into CONST_AND_COPIES and AVAIL_EXPRS_STACK any equivalences implied
792 by traversing edge E (which are cached in E->aux).
794 Callers are responsible for managing the unwinding markers. */
795 void
796 record_temporary_equivalences (edge e,
797 class const_and_copies *const_and_copies,
798 class avail_exprs_stack *avail_exprs_stack)
800 int i;
801 struct edge_info *edge_info = (struct edge_info *) e->aux;
803 /* If we have info associated with this edge, record it into
804 our equivalence tables. */
805 if (edge_info)
807 cond_equivalence *eq;
808 tree lhs = edge_info->lhs;
809 tree rhs = edge_info->rhs;
811 /* If we have a simple NAME = VALUE equivalence, record it. */
812 if (lhs)
813 record_equality (lhs, rhs, const_and_copies);
815 /* If LHS is an SSA_NAME and RHS is a constant integer and LHS was
816 set via a widening type conversion, then we may be able to record
817 additional equivalences. */
818 if (lhs
819 && TREE_CODE (lhs) == SSA_NAME
820 && TREE_CODE (rhs) == INTEGER_CST)
822 gimple *defstmt = SSA_NAME_DEF_STMT (lhs);
824 if (defstmt
825 && is_gimple_assign (defstmt)
826 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (defstmt)))
828 tree old_rhs = gimple_assign_rhs1 (defstmt);
830 /* If the conversion widens the original value and
831 the constant is in the range of the type of OLD_RHS,
832 then convert the constant and record the equivalence.
834 Note that int_fits_type_p does not check the precision
835 if the upper and lower bounds are OK. */
836 if (INTEGRAL_TYPE_P (TREE_TYPE (old_rhs))
837 && (TYPE_PRECISION (TREE_TYPE (lhs))
838 > TYPE_PRECISION (TREE_TYPE (old_rhs)))
839 && int_fits_type_p (rhs, TREE_TYPE (old_rhs)))
841 tree newval = fold_convert (TREE_TYPE (old_rhs), rhs);
842 record_equality (old_rhs, newval, const_and_copies);
847 /* If LHS is an SSA_NAME with a new equivalency then try if
848 stmts with uses of that LHS that dominate the edge destination
849 simplify and allow further equivalences to be recorded. */
850 if (lhs && TREE_CODE (lhs) == SSA_NAME)
852 use_operand_p use_p;
853 imm_use_iterator iter;
854 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
856 gimple *use_stmt = USE_STMT (use_p);
858 /* Only bother to record more equivalences for lhs that
859 can be directly used by e->dest.
860 ??? If the code gets re-organized to a worklist to
861 catch more indirect opportunities and it is made to
862 handle PHIs then this should only consider use_stmts
863 in basic-blocks we have already visited. */
864 if (e->dest == gimple_bb (use_stmt)
865 || !dominated_by_p (CDI_DOMINATORS,
866 e->dest, gimple_bb (use_stmt)))
867 continue;
868 tree lhs2 = gimple_get_lhs (use_stmt);
869 if (lhs2 && TREE_CODE (lhs2) == SSA_NAME)
871 tree res
872 = gimple_fold_stmt_to_constant_1 (use_stmt, dom_valueize,
873 no_follow_ssa_edges);
874 if (res
875 && (TREE_CODE (res) == SSA_NAME
876 || is_gimple_min_invariant (res)))
877 record_equality (lhs2, res, const_and_copies);
882 /* If we have 0 = COND or 1 = COND equivalences, record them
883 into our expression hash tables. */
884 for (i = 0; edge_info->cond_equivalences.iterate (i, &eq); ++i)
885 record_cond (eq, avail_exprs_stack);
889 /* Wrapper for common code to attempt to thread an edge. For example,
890 it handles lazily building the dummy condition and the bookkeeping
891 when jump threading is successful. */
893 void
894 dom_opt_dom_walker::thread_across_edge (edge e)
896 if (! m_dummy_cond)
897 m_dummy_cond =
898 gimple_build_cond (NE_EXPR,
899 integer_zero_node, integer_zero_node,
900 NULL, NULL);
902 /* Push a marker on both stacks so we can unwind the tables back to their
903 current state. */
904 m_avail_exprs_stack->push_marker ();
905 m_const_and_copies->push_marker ();
907 /* Traversing E may result in equivalences we can utilize. */
908 record_temporary_equivalences (e, m_const_and_copies, m_avail_exprs_stack);
910 /* With all the edge equivalences in the tables, go ahead and attempt
911 to thread through E->dest. */
912 ::thread_across_edge (m_dummy_cond, e, false,
913 m_const_and_copies, m_avail_exprs_stack,
914 simplify_stmt_for_jump_threading);
916 /* And restore the various tables to their state before
917 we threaded this edge.
919 XXX The code in tree-ssa-threadedge.c will restore the state of
920 the const_and_copies table. We we just have to restore the expression
921 table. */
922 m_avail_exprs_stack->pop_to_marker ();
925 /* PHI nodes can create equivalences too.
927 Ignoring any alternatives which are the same as the result, if
928 all the alternatives are equal, then the PHI node creates an
929 equivalence. */
931 static void
932 record_equivalences_from_phis (basic_block bb)
934 gphi_iterator gsi;
936 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
938 gphi *phi = gsi.phi ();
940 tree lhs = gimple_phi_result (phi);
941 tree rhs = NULL;
942 size_t i;
944 for (i = 0; i < gimple_phi_num_args (phi); i++)
946 tree t = gimple_phi_arg_def (phi, i);
948 /* Ignore alternatives which are the same as our LHS. Since
949 LHS is a PHI_RESULT, it is known to be a SSA_NAME, so we
950 can simply compare pointers. */
951 if (lhs == t)
952 continue;
954 t = dom_valueize (t);
956 /* If we have not processed an alternative yet, then set
957 RHS to this alternative. */
958 if (rhs == NULL)
959 rhs = t;
960 /* If we have processed an alternative (stored in RHS), then
961 see if it is equal to this one. If it isn't, then stop
962 the search. */
963 else if (! operand_equal_for_phi_arg_p (rhs, t))
964 break;
967 /* If we had no interesting alternatives, then all the RHS alternatives
968 must have been the same as LHS. */
969 if (!rhs)
970 rhs = lhs;
972 /* If we managed to iterate through each PHI alternative without
973 breaking out of the loop, then we have a PHI which may create
974 a useful equivalence. We do not need to record unwind data for
975 this, since this is a true assignment and not an equivalence
976 inferred from a comparison. All uses of this ssa name are dominated
977 by this assignment, so unwinding just costs time and space. */
978 if (i == gimple_phi_num_args (phi)
979 && may_propagate_copy (lhs, rhs))
980 set_ssa_name_value (lhs, rhs);
984 /* Ignoring loop backedges, if BB has precisely one incoming edge then
985 return that edge. Otherwise return NULL. */
986 static edge
987 single_incoming_edge_ignoring_loop_edges (basic_block bb)
989 edge retval = NULL;
990 edge e;
991 edge_iterator ei;
993 FOR_EACH_EDGE (e, ei, bb->preds)
995 /* A loop back edge can be identified by the destination of
996 the edge dominating the source of the edge. */
997 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
998 continue;
1000 /* If we have already seen a non-loop edge, then we must have
1001 multiple incoming non-loop edges and thus we return NULL. */
1002 if (retval)
1003 return NULL;
1005 /* This is the first non-loop incoming edge we have found. Record
1006 it. */
1007 retval = e;
1010 return retval;
1013 /* Record any equivalences created by the incoming edge to BB into
1014 CONST_AND_COPIES and AVAIL_EXPRS_STACK. If BB has more than one
1015 incoming edge, then no equivalence is created. */
1017 static void
1018 record_equivalences_from_incoming_edge (basic_block bb,
1019 class const_and_copies *const_and_copies,
1020 class avail_exprs_stack *avail_exprs_stack)
1022 edge e;
1023 basic_block parent;
1025 /* If our parent block ended with a control statement, then we may be
1026 able to record some equivalences based on which outgoing edge from
1027 the parent was followed. */
1028 parent = get_immediate_dominator (CDI_DOMINATORS, bb);
1030 e = single_incoming_edge_ignoring_loop_edges (bb);
1032 /* If we had a single incoming edge from our parent block, then enter
1033 any data associated with the edge into our tables. */
1034 if (e && e->src == parent)
1035 record_temporary_equivalences (e, const_and_copies, avail_exprs_stack);
1038 /* Dump statistics for the hash table HTAB. */
1040 static void
1041 htab_statistics (FILE *file, const hash_table<expr_elt_hasher> &htab)
1043 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
1044 (long) htab.size (),
1045 (long) htab.elements (),
1046 htab.collisions ());
1049 /* Dump SSA statistics on FILE. */
1051 static void
1052 dump_dominator_optimization_stats (FILE *file,
1053 hash_table<expr_elt_hasher> *avail_exprs)
1055 fprintf (file, "Total number of statements: %6ld\n\n",
1056 opt_stats.num_stmts);
1057 fprintf (file, "Exprs considered for dominator optimizations: %6ld\n",
1058 opt_stats.num_exprs_considered);
1060 fprintf (file, "\nHash table statistics:\n");
1062 fprintf (file, " avail_exprs: ");
1063 htab_statistics (file, *avail_exprs);
1067 /* Enter condition equivalence P into AVAIL_EXPRS_HASH.
1069 This indicates that a conditional expression has a known
1070 boolean value. */
1072 static void
1073 record_cond (cond_equivalence *p,
1074 class avail_exprs_stack *avail_exprs_stack)
1076 class expr_hash_elt *element = new expr_hash_elt (&p->cond, p->value);
1077 expr_hash_elt **slot;
1079 hash_table<expr_elt_hasher> *avail_exprs = avail_exprs_stack->avail_exprs ();
1080 slot = avail_exprs->find_slot_with_hash (element, element->hash (), INSERT);
1081 if (*slot == NULL)
1083 *slot = element;
1084 avail_exprs_stack->record_expr (element, NULL, '1');
1086 else
1087 delete element;
1090 /* Return the loop depth of the basic block of the defining statement of X.
1091 This number should not be treated as absolutely correct because the loop
1092 information may not be completely up-to-date when dom runs. However, it
1093 will be relatively correct, and as more passes are taught to keep loop info
1094 up to date, the result will become more and more accurate. */
1096 static int
1097 loop_depth_of_name (tree x)
1099 gimple *defstmt;
1100 basic_block defbb;
1102 /* If it's not an SSA_NAME, we have no clue where the definition is. */
1103 if (TREE_CODE (x) != SSA_NAME)
1104 return 0;
1106 /* Otherwise return the loop depth of the defining statement's bb.
1107 Note that there may not actually be a bb for this statement, if the
1108 ssa_name is live on entry. */
1109 defstmt = SSA_NAME_DEF_STMT (x);
1110 defbb = gimple_bb (defstmt);
1111 if (!defbb)
1112 return 0;
1114 return bb_loop_depth (defbb);
1117 /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1118 This constrains the cases in which we may treat this as assignment. */
1120 static void
1121 record_equality (tree x, tree y, class const_and_copies *const_and_copies)
1123 tree prev_x = NULL, prev_y = NULL;
1125 if (tree_swap_operands_p (x, y, false))
1126 std::swap (x, y);
1128 /* Most of the time tree_swap_operands_p does what we want. But there
1129 are cases where we know one operand is better for copy propagation than
1130 the other. Given no other code cares about ordering of equality
1131 comparison operators for that purpose, we just handle the special cases
1132 here. */
1133 if (TREE_CODE (x) == SSA_NAME && TREE_CODE (y) == SSA_NAME)
1135 /* If one operand is a single use operand, then make it
1136 X. This will preserve its single use properly and if this
1137 conditional is eliminated, the computation of X can be
1138 eliminated as well. */
1139 if (has_single_use (y) && ! has_single_use (x))
1140 std::swap (x, y);
1142 if (TREE_CODE (x) == SSA_NAME)
1143 prev_x = SSA_NAME_VALUE (x);
1144 if (TREE_CODE (y) == SSA_NAME)
1145 prev_y = SSA_NAME_VALUE (y);
1147 /* If one of the previous values is invariant, or invariant in more loops
1148 (by depth), then use that.
1149 Otherwise it doesn't matter which value we choose, just so
1150 long as we canonicalize on one value. */
1151 if (is_gimple_min_invariant (y))
1153 else if (is_gimple_min_invariant (x)
1154 /* ??? When threading over backedges the following is important
1155 for correctness. See PR61757. */
1156 || (loop_depth_of_name (x) < loop_depth_of_name (y)))
1157 prev_x = x, x = y, y = prev_x, prev_x = prev_y;
1158 else if (prev_x && is_gimple_min_invariant (prev_x))
1159 x = y, y = prev_x, prev_x = prev_y;
1160 else if (prev_y)
1161 y = prev_y;
1163 /* After the swapping, we must have one SSA_NAME. */
1164 if (TREE_CODE (x) != SSA_NAME)
1165 return;
1167 /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1168 variable compared against zero. If we're honoring signed zeros,
1169 then we cannot record this value unless we know that the value is
1170 nonzero. */
1171 if (HONOR_SIGNED_ZEROS (x)
1172 && (TREE_CODE (y) != REAL_CST
1173 || real_equal (&dconst0, &TREE_REAL_CST (y))))
1174 return;
1176 const_and_copies->record_const_or_copy (x, y, prev_x);
1179 /* Returns true when STMT is a simple iv increment. It detects the
1180 following situation:
1182 i_1 = phi (..., i_2)
1183 i_2 = i_1 +/- ... */
1185 bool
1186 simple_iv_increment_p (gimple *stmt)
1188 enum tree_code code;
1189 tree lhs, preinc;
1190 gimple *phi;
1191 size_t i;
1193 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1194 return false;
1196 lhs = gimple_assign_lhs (stmt);
1197 if (TREE_CODE (lhs) != SSA_NAME)
1198 return false;
1200 code = gimple_assign_rhs_code (stmt);
1201 if (code != PLUS_EXPR
1202 && code != MINUS_EXPR
1203 && code != POINTER_PLUS_EXPR)
1204 return false;
1206 preinc = gimple_assign_rhs1 (stmt);
1207 if (TREE_CODE (preinc) != SSA_NAME)
1208 return false;
1210 phi = SSA_NAME_DEF_STMT (preinc);
1211 if (gimple_code (phi) != GIMPLE_PHI)
1212 return false;
1214 for (i = 0; i < gimple_phi_num_args (phi); i++)
1215 if (gimple_phi_arg_def (phi, i) == lhs)
1216 return true;
1218 return false;
1221 /* Propagate know values from SSA_NAME_VALUE into the PHI nodes of the
1222 successors of BB. */
1224 static void
1225 cprop_into_successor_phis (basic_block bb,
1226 class const_and_copies *const_and_copies)
1228 edge e;
1229 edge_iterator ei;
1231 FOR_EACH_EDGE (e, ei, bb->succs)
1233 int indx;
1234 gphi_iterator gsi;
1236 /* If this is an abnormal edge, then we do not want to copy propagate
1237 into the PHI alternative associated with this edge. */
1238 if (e->flags & EDGE_ABNORMAL)
1239 continue;
1241 gsi = gsi_start_phis (e->dest);
1242 if (gsi_end_p (gsi))
1243 continue;
1245 /* We may have an equivalence associated with this edge. While
1246 we can not propagate it into non-dominated blocks, we can
1247 propagate them into PHIs in non-dominated blocks. */
1249 /* Push the unwind marker so we can reset the const and copies
1250 table back to its original state after processing this edge. */
1251 const_and_copies->push_marker ();
1253 /* Extract and record any simple NAME = VALUE equivalences.
1255 Don't bother with [01] = COND equivalences, they're not useful
1256 here. */
1257 struct edge_info *edge_info = (struct edge_info *) e->aux;
1258 if (edge_info)
1260 tree lhs = edge_info->lhs;
1261 tree rhs = edge_info->rhs;
1263 if (lhs && TREE_CODE (lhs) == SSA_NAME)
1264 const_and_copies->record_const_or_copy (lhs, rhs);
1267 indx = e->dest_idx;
1268 for ( ; !gsi_end_p (gsi); gsi_next (&gsi))
1270 tree new_val;
1271 use_operand_p orig_p;
1272 tree orig_val;
1273 gphi *phi = gsi.phi ();
1275 /* The alternative may be associated with a constant, so verify
1276 it is an SSA_NAME before doing anything with it. */
1277 orig_p = gimple_phi_arg_imm_use_ptr (phi, indx);
1278 orig_val = get_use_from_ptr (orig_p);
1279 if (TREE_CODE (orig_val) != SSA_NAME)
1280 continue;
1282 /* If we have *ORIG_P in our constant/copy table, then replace
1283 ORIG_P with its value in our constant/copy table. */
1284 new_val = SSA_NAME_VALUE (orig_val);
1285 if (new_val
1286 && new_val != orig_val
1287 && (TREE_CODE (new_val) == SSA_NAME
1288 || is_gimple_min_invariant (new_val))
1289 && may_propagate_copy (orig_val, new_val))
1290 propagate_value (orig_p, new_val);
1293 const_and_copies->pop_to_marker ();
1297 void
1298 dom_opt_dom_walker::before_dom_children (basic_block bb)
1300 gimple_stmt_iterator gsi;
1302 if (dump_file && (dump_flags & TDF_DETAILS))
1303 fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
1305 /* Push a marker on the stacks of local information so that we know how
1306 far to unwind when we finalize this block. */
1307 m_avail_exprs_stack->push_marker ();
1308 m_const_and_copies->push_marker ();
1310 record_equivalences_from_incoming_edge (bb, m_const_and_copies,
1311 m_avail_exprs_stack);
1313 /* PHI nodes can create equivalences too. */
1314 record_equivalences_from_phis (bb);
1316 /* Create equivalences from redundant PHIs. PHIs are only truly
1317 redundant when they exist in the same block, so push another
1318 marker and unwind right afterwards. */
1319 m_avail_exprs_stack->push_marker ();
1320 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1321 eliminate_redundant_computations (&gsi, m_const_and_copies,
1322 m_avail_exprs_stack);
1323 m_avail_exprs_stack->pop_to_marker ();
1325 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1326 optimize_stmt (bb, gsi, m_const_and_copies, m_avail_exprs_stack);
1328 /* Now prepare to process dominated blocks. */
1329 record_edge_info (bb);
1330 cprop_into_successor_phis (bb, m_const_and_copies);
1333 /* We have finished processing the dominator children of BB, perform
1334 any finalization actions in preparation for leaving this node in
1335 the dominator tree. */
1337 void
1338 dom_opt_dom_walker::after_dom_children (basic_block bb)
1340 gimple *last;
1342 /* If we have an outgoing edge to a block with multiple incoming and
1343 outgoing edges, then we may be able to thread the edge, i.e., we
1344 may be able to statically determine which of the outgoing edges
1345 will be traversed when the incoming edge from BB is traversed. */
1346 if (single_succ_p (bb)
1347 && (single_succ_edge (bb)->flags & EDGE_ABNORMAL) == 0
1348 && potentially_threadable_block (single_succ (bb)))
1350 thread_across_edge (single_succ_edge (bb));
1352 else if ((last = last_stmt (bb))
1353 && gimple_code (last) == GIMPLE_COND
1354 && EDGE_COUNT (bb->succs) == 2
1355 && (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL) == 0
1356 && (EDGE_SUCC (bb, 1)->flags & EDGE_ABNORMAL) == 0)
1358 edge true_edge, false_edge;
1360 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1362 /* Only try to thread the edge if it reaches a target block with
1363 more than one predecessor and more than one successor. */
1364 if (potentially_threadable_block (true_edge->dest))
1365 thread_across_edge (true_edge);
1367 /* Similarly for the ELSE arm. */
1368 if (potentially_threadable_block (false_edge->dest))
1369 thread_across_edge (false_edge);
1373 /* These remove expressions local to BB from the tables. */
1374 m_avail_exprs_stack->pop_to_marker ();
1375 m_const_and_copies->pop_to_marker ();
1378 /* Search for redundant computations in STMT. If any are found, then
1379 replace them with the variable holding the result of the computation.
1381 If safe, record this expression into AVAIL_EXPRS_STACK and
1382 CONST_AND_COPIES. */
1384 static void
1385 eliminate_redundant_computations (gimple_stmt_iterator* gsi,
1386 class const_and_copies *const_and_copies,
1387 class avail_exprs_stack *avail_exprs_stack)
1389 tree expr_type;
1390 tree cached_lhs;
1391 tree def;
1392 bool insert = true;
1393 bool assigns_var_p = false;
1395 gimple *stmt = gsi_stmt (*gsi);
1397 if (gimple_code (stmt) == GIMPLE_PHI)
1398 def = gimple_phi_result (stmt);
1399 else
1400 def = gimple_get_lhs (stmt);
1402 /* Certain expressions on the RHS can be optimized away, but can not
1403 themselves be entered into the hash tables. */
1404 if (! def
1405 || TREE_CODE (def) != SSA_NAME
1406 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)
1407 || gimple_vdef (stmt)
1408 /* Do not record equivalences for increments of ivs. This would create
1409 overlapping live ranges for a very questionable gain. */
1410 || simple_iv_increment_p (stmt))
1411 insert = false;
1413 /* Check if the expression has been computed before. */
1414 cached_lhs = lookup_avail_expr (stmt, insert, avail_exprs_stack);
1416 opt_stats.num_exprs_considered++;
1418 /* Get the type of the expression we are trying to optimize. */
1419 if (is_gimple_assign (stmt))
1421 expr_type = TREE_TYPE (gimple_assign_lhs (stmt));
1422 assigns_var_p = true;
1424 else if (gimple_code (stmt) == GIMPLE_COND)
1425 expr_type = boolean_type_node;
1426 else if (is_gimple_call (stmt))
1428 gcc_assert (gimple_call_lhs (stmt));
1429 expr_type = TREE_TYPE (gimple_call_lhs (stmt));
1430 assigns_var_p = true;
1432 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
1433 expr_type = TREE_TYPE (gimple_switch_index (swtch_stmt));
1434 else if (gimple_code (stmt) == GIMPLE_PHI)
1435 /* We can't propagate into a phi, so the logic below doesn't apply.
1436 Instead record an equivalence between the cached LHS and the
1437 PHI result of this statement, provided they are in the same block.
1438 This should be sufficient to kill the redundant phi. */
1440 if (def && cached_lhs)
1441 const_and_copies->record_const_or_copy (def, cached_lhs);
1442 return;
1444 else
1445 gcc_unreachable ();
1447 if (!cached_lhs)
1448 return;
1450 /* It is safe to ignore types here since we have already done
1451 type checking in the hashing and equality routines. In fact
1452 type checking here merely gets in the way of constant
1453 propagation. Also, make sure that it is safe to propagate
1454 CACHED_LHS into the expression in STMT. */
1455 if ((TREE_CODE (cached_lhs) != SSA_NAME
1456 && (assigns_var_p
1457 || useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs))))
1458 || may_propagate_copy_into_stmt (stmt, cached_lhs))
1460 gcc_checking_assert (TREE_CODE (cached_lhs) == SSA_NAME
1461 || is_gimple_min_invariant (cached_lhs));
1463 if (dump_file && (dump_flags & TDF_DETAILS))
1465 fprintf (dump_file, " Replaced redundant expr '");
1466 print_gimple_expr (dump_file, stmt, 0, dump_flags);
1467 fprintf (dump_file, "' with '");
1468 print_generic_expr (dump_file, cached_lhs, dump_flags);
1469 fprintf (dump_file, "'\n");
1472 opt_stats.num_re++;
1474 if (assigns_var_p
1475 && !useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs)))
1476 cached_lhs = fold_convert (expr_type, cached_lhs);
1478 propagate_tree_value_into_stmt (gsi, cached_lhs);
1480 /* Since it is always necessary to mark the result as modified,
1481 perhaps we should move this into propagate_tree_value_into_stmt
1482 itself. */
1483 gimple_set_modified (gsi_stmt (*gsi), true);
1487 /* STMT, a GIMPLE_ASSIGN, may create certain equivalences, in either
1488 the available expressions table or the const_and_copies table.
1489 Detect and record those equivalences into AVAIL_EXPRS_STACK.
1491 We handle only very simple copy equivalences here. The heavy
1492 lifing is done by eliminate_redundant_computations. */
1494 static void
1495 record_equivalences_from_stmt (gimple *stmt, int may_optimize_p,
1496 class avail_exprs_stack *avail_exprs_stack)
1498 tree lhs;
1499 enum tree_code lhs_code;
1501 gcc_assert (is_gimple_assign (stmt));
1503 lhs = gimple_assign_lhs (stmt);
1504 lhs_code = TREE_CODE (lhs);
1506 if (lhs_code == SSA_NAME
1507 && gimple_assign_single_p (stmt))
1509 tree rhs = gimple_assign_rhs1 (stmt);
1511 /* If the RHS of the assignment is a constant or another variable that
1512 may be propagated, register it in the CONST_AND_COPIES table. We
1513 do not need to record unwind data for this, since this is a true
1514 assignment and not an equivalence inferred from a comparison. All
1515 uses of this ssa name are dominated by this assignment, so unwinding
1516 just costs time and space. */
1517 if (may_optimize_p
1518 && (TREE_CODE (rhs) == SSA_NAME
1519 || is_gimple_min_invariant (rhs)))
1521 rhs = dom_valueize (rhs);
1523 if (dump_file && (dump_flags & TDF_DETAILS))
1525 fprintf (dump_file, "==== ASGN ");
1526 print_generic_expr (dump_file, lhs, 0);
1527 fprintf (dump_file, " = ");
1528 print_generic_expr (dump_file, rhs, 0);
1529 fprintf (dump_file, "\n");
1532 set_ssa_name_value (lhs, rhs);
1536 /* Make sure we can propagate &x + CST. */
1537 if (lhs_code == SSA_NAME
1538 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
1539 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR
1540 && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)
1542 tree op0 = gimple_assign_rhs1 (stmt);
1543 tree op1 = gimple_assign_rhs2 (stmt);
1544 tree new_rhs
1545 = build_fold_addr_expr (fold_build2 (MEM_REF,
1546 TREE_TYPE (TREE_TYPE (op0)),
1547 unshare_expr (op0),
1548 fold_convert (ptr_type_node,
1549 op1)));
1550 if (dump_file && (dump_flags & TDF_DETAILS))
1552 fprintf (dump_file, "==== ASGN ");
1553 print_generic_expr (dump_file, lhs, 0);
1554 fprintf (dump_file, " = ");
1555 print_generic_expr (dump_file, new_rhs, 0);
1556 fprintf (dump_file, "\n");
1559 set_ssa_name_value (lhs, new_rhs);
1562 /* A memory store, even an aliased store, creates a useful
1563 equivalence. By exchanging the LHS and RHS, creating suitable
1564 vops and recording the result in the available expression table,
1565 we may be able to expose more redundant loads. */
1566 if (!gimple_has_volatile_ops (stmt)
1567 && gimple_references_memory_p (stmt)
1568 && gimple_assign_single_p (stmt)
1569 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
1570 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
1571 && !is_gimple_reg (lhs))
1573 tree rhs = gimple_assign_rhs1 (stmt);
1574 gassign *new_stmt;
1576 /* Build a new statement with the RHS and LHS exchanged. */
1577 if (TREE_CODE (rhs) == SSA_NAME)
1579 /* NOTE tuples. The call to gimple_build_assign below replaced
1580 a call to build_gimple_modify_stmt, which did not set the
1581 SSA_NAME_DEF_STMT on the LHS of the assignment. Doing so
1582 may cause an SSA validation failure, as the LHS may be a
1583 default-initialized name and should have no definition. I'm
1584 a bit dubious of this, as the artificial statement that we
1585 generate here may in fact be ill-formed, but it is simply
1586 used as an internal device in this pass, and never becomes
1587 part of the CFG. */
1588 gimple *defstmt = SSA_NAME_DEF_STMT (rhs);
1589 new_stmt = gimple_build_assign (rhs, lhs);
1590 SSA_NAME_DEF_STMT (rhs) = defstmt;
1592 else
1593 new_stmt = gimple_build_assign (rhs, lhs);
1595 gimple_set_vuse (new_stmt, gimple_vdef (stmt));
1597 /* Finally enter the statement into the available expression
1598 table. */
1599 lookup_avail_expr (new_stmt, true, avail_exprs_stack);
1603 /* Replace *OP_P in STMT with any known equivalent value for *OP_P from
1604 CONST_AND_COPIES. */
1606 static void
1607 cprop_operand (gimple *stmt, use_operand_p op_p)
1609 tree val;
1610 tree op = USE_FROM_PTR (op_p);
1612 /* If the operand has a known constant value or it is known to be a
1613 copy of some other variable, use the value or copy stored in
1614 CONST_AND_COPIES. */
1615 val = SSA_NAME_VALUE (op);
1616 if (val && val != op)
1618 /* Do not replace hard register operands in asm statements. */
1619 if (gimple_code (stmt) == GIMPLE_ASM
1620 && !may_propagate_copy_into_asm (op))
1621 return;
1623 /* Certain operands are not allowed to be copy propagated due
1624 to their interaction with exception handling and some GCC
1625 extensions. */
1626 if (!may_propagate_copy (op, val))
1627 return;
1629 /* Do not propagate copies into BIVs.
1630 See PR23821 and PR62217 for how this can disturb IV and
1631 number of iteration analysis. */
1632 if (TREE_CODE (val) != INTEGER_CST)
1634 gimple *def = SSA_NAME_DEF_STMT (op);
1635 if (gimple_code (def) == GIMPLE_PHI
1636 && gimple_bb (def)->loop_father->header == gimple_bb (def))
1637 return;
1640 /* Dump details. */
1641 if (dump_file && (dump_flags & TDF_DETAILS))
1643 fprintf (dump_file, " Replaced '");
1644 print_generic_expr (dump_file, op, dump_flags);
1645 fprintf (dump_file, "' with %s '",
1646 (TREE_CODE (val) != SSA_NAME ? "constant" : "variable"));
1647 print_generic_expr (dump_file, val, dump_flags);
1648 fprintf (dump_file, "'\n");
1651 if (TREE_CODE (val) != SSA_NAME)
1652 opt_stats.num_const_prop++;
1653 else
1654 opt_stats.num_copy_prop++;
1656 propagate_value (op_p, val);
1658 /* And note that we modified this statement. This is now
1659 safe, even if we changed virtual operands since we will
1660 rescan the statement and rewrite its operands again. */
1661 gimple_set_modified (stmt, true);
1665 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
1666 known value for that SSA_NAME (or NULL if no value is known).
1668 Propagate values from CONST_AND_COPIES into the uses, vuses and
1669 vdef_ops of STMT. */
1671 static void
1672 cprop_into_stmt (gimple *stmt)
1674 use_operand_p op_p;
1675 ssa_op_iter iter;
1677 FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_USE)
1678 cprop_operand (stmt, op_p);
1681 /* Optimize the statement in block BB pointed to by iterator SI
1682 using equivalences from CONST_AND_COPIES and AVAIL_EXPRS_STACK.
1684 We try to perform some simplistic global redundancy elimination and
1685 constant propagation:
1687 1- To detect global redundancy, we keep track of expressions that have
1688 been computed in this block and its dominators. If we find that the
1689 same expression is computed more than once, we eliminate repeated
1690 computations by using the target of the first one.
1692 2- Constant values and copy assignments. This is used to do very
1693 simplistic constant and copy propagation. When a constant or copy
1694 assignment is found, we map the value on the RHS of the assignment to
1695 the variable in the LHS in the CONST_AND_COPIES table. */
1697 static void
1698 optimize_stmt (basic_block bb, gimple_stmt_iterator si,
1699 class const_and_copies *const_and_copies,
1700 class avail_exprs_stack *avail_exprs_stack)
1702 gimple *stmt, *old_stmt;
1703 bool may_optimize_p;
1704 bool modified_p = false;
1705 bool was_noreturn;
1707 old_stmt = stmt = gsi_stmt (si);
1708 was_noreturn = is_gimple_call (stmt) && gimple_call_noreturn_p (stmt);
1710 if (dump_file && (dump_flags & TDF_DETAILS))
1712 fprintf (dump_file, "Optimizing statement ");
1713 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1716 if (gimple_code (stmt) == GIMPLE_COND)
1717 canonicalize_comparison (as_a <gcond *> (stmt));
1719 update_stmt_if_modified (stmt);
1720 opt_stats.num_stmts++;
1722 /* Const/copy propagate into USES, VUSES and the RHS of VDEFs. */
1723 cprop_into_stmt (stmt);
1725 /* If the statement has been modified with constant replacements,
1726 fold its RHS before checking for redundant computations. */
1727 if (gimple_modified_p (stmt))
1729 tree rhs = NULL;
1731 /* Try to fold the statement making sure that STMT is kept
1732 up to date. */
1733 if (fold_stmt (&si))
1735 stmt = gsi_stmt (si);
1736 gimple_set_modified (stmt, true);
1738 if (dump_file && (dump_flags & TDF_DETAILS))
1740 fprintf (dump_file, " Folded to: ");
1741 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1745 /* We only need to consider cases that can yield a gimple operand. */
1746 if (gimple_assign_single_p (stmt))
1747 rhs = gimple_assign_rhs1 (stmt);
1748 else if (gimple_code (stmt) == GIMPLE_GOTO)
1749 rhs = gimple_goto_dest (stmt);
1750 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
1751 /* This should never be an ADDR_EXPR. */
1752 rhs = gimple_switch_index (swtch_stmt);
1754 if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
1755 recompute_tree_invariant_for_addr_expr (rhs);
1757 /* Indicate that maybe_clean_or_replace_eh_stmt needs to be called,
1758 even if fold_stmt updated the stmt already and thus cleared
1759 gimple_modified_p flag on it. */
1760 modified_p = true;
1763 /* Check for redundant computations. Do this optimization only
1764 for assignments that have no volatile ops and conditionals. */
1765 may_optimize_p = (!gimple_has_side_effects (stmt)
1766 && (is_gimple_assign (stmt)
1767 || (is_gimple_call (stmt)
1768 && gimple_call_lhs (stmt) != NULL_TREE)
1769 || gimple_code (stmt) == GIMPLE_COND
1770 || gimple_code (stmt) == GIMPLE_SWITCH));
1772 if (may_optimize_p)
1774 if (gimple_code (stmt) == GIMPLE_CALL)
1776 /* Resolve __builtin_constant_p. If it hasn't been
1777 folded to integer_one_node by now, it's fairly
1778 certain that the value simply isn't constant. */
1779 tree callee = gimple_call_fndecl (stmt);
1780 if (callee
1781 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
1782 && DECL_FUNCTION_CODE (callee) == BUILT_IN_CONSTANT_P)
1784 propagate_tree_value_into_stmt (&si, integer_zero_node);
1785 stmt = gsi_stmt (si);
1789 update_stmt_if_modified (stmt);
1790 eliminate_redundant_computations (&si, const_and_copies,
1791 avail_exprs_stack);
1792 stmt = gsi_stmt (si);
1794 /* Perform simple redundant store elimination. */
1795 if (gimple_assign_single_p (stmt)
1796 && TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
1798 tree lhs = gimple_assign_lhs (stmt);
1799 tree rhs = gimple_assign_rhs1 (stmt);
1800 tree cached_lhs;
1801 gassign *new_stmt;
1802 rhs = dom_valueize (rhs);
1803 /* Build a new statement with the RHS and LHS exchanged. */
1804 if (TREE_CODE (rhs) == SSA_NAME)
1806 gimple *defstmt = SSA_NAME_DEF_STMT (rhs);
1807 new_stmt = gimple_build_assign (rhs, lhs);
1808 SSA_NAME_DEF_STMT (rhs) = defstmt;
1810 else
1811 new_stmt = gimple_build_assign (rhs, lhs);
1812 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
1813 cached_lhs = lookup_avail_expr (new_stmt, false, avail_exprs_stack);
1814 if (cached_lhs
1815 && rhs == cached_lhs)
1817 basic_block bb = gimple_bb (stmt);
1818 unlink_stmt_vdef (stmt);
1819 if (gsi_remove (&si, true))
1821 bitmap_set_bit (need_eh_cleanup, bb->index);
1822 if (dump_file && (dump_flags & TDF_DETAILS))
1823 fprintf (dump_file, " Flagged to clear EH edges.\n");
1825 release_defs (stmt);
1826 return;
1831 /* Record any additional equivalences created by this statement. */
1832 if (is_gimple_assign (stmt))
1833 record_equivalences_from_stmt (stmt, may_optimize_p, avail_exprs_stack);
1835 /* If STMT is a COND_EXPR or SWITCH_EXPR and it was modified, then we may
1836 know where it goes. */
1837 if (gimple_modified_p (stmt) || modified_p)
1839 tree val = NULL;
1841 update_stmt_if_modified (stmt);
1843 if (gimple_code (stmt) == GIMPLE_COND)
1844 val = fold_binary_loc (gimple_location (stmt),
1845 gimple_cond_code (stmt), boolean_type_node,
1846 gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
1847 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
1848 val = gimple_switch_index (swtch_stmt);
1850 if (val && TREE_CODE (val) == INTEGER_CST)
1852 edge taken_edge = find_taken_edge (bb, val);
1853 if (taken_edge)
1856 /* We need to remove any queued jump threads that
1857 reference outgoing edges from this block. */
1858 edge_iterator ei;
1859 edge e;
1860 FOR_EACH_EDGE (e, ei, bb->succs)
1861 remove_jump_threads_including (e);
1863 /* Now clean up the control statement at the end of
1864 BB and remove unexecutable edges. */
1865 remove_ctrl_stmt_and_useless_edges (bb, taken_edge->dest);
1867 /* Fixup the flags on the single remaining edge. */
1868 taken_edge->flags
1869 &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE | EDGE_ABNORMAL);
1870 taken_edge->flags |= EDGE_FALLTHRU;
1872 /* Further simplifications may be possible. */
1873 cfg_altered = true;
1877 /* If we simplified a statement in such a way as to be shown that it
1878 cannot trap, update the eh information and the cfg to match. */
1879 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
1881 bitmap_set_bit (need_eh_cleanup, bb->index);
1882 if (dump_file && (dump_flags & TDF_DETAILS))
1883 fprintf (dump_file, " Flagged to clear EH edges.\n");
1886 if (!was_noreturn
1887 && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
1888 need_noreturn_fixup.safe_push (stmt);
1892 /* Helper for walk_non_aliased_vuses. Determine if we arrived at
1893 the desired memory state. */
1895 static void *
1896 vuse_eq (ao_ref *, tree vuse1, unsigned int cnt, void *data)
1898 tree vuse2 = (tree) data;
1899 if (vuse1 == vuse2)
1900 return data;
1902 /* This bounds the stmt walks we perform on reference lookups
1903 to O(1) instead of O(N) where N is the number of dominating
1904 stores leading to a candidate. We re-use the SCCVN param
1905 for this as it is basically the same complexity. */
1906 if (cnt > (unsigned) PARAM_VALUE (PARAM_SCCVN_MAX_ALIAS_QUERIES_PER_ACCESS))
1907 return (void *)-1;
1909 return NULL;
1912 /* Search for an existing instance of STMT in the AVAIL_EXPRS_STACK table.
1913 If found, return its LHS. Otherwise insert STMT in the table and
1914 return NULL_TREE.
1916 Also, when an expression is first inserted in the table, it is also
1917 is also added to AVAIL_EXPRS_STACK, so that it can be removed when
1918 we finish processing this block and its children. */
1920 static tree
1921 lookup_avail_expr (gimple *stmt, bool insert,
1922 class avail_exprs_stack *avail_exprs_stack)
1924 expr_hash_elt **slot;
1925 tree lhs;
1927 /* Get LHS of phi, assignment, or call; else NULL_TREE. */
1928 if (gimple_code (stmt) == GIMPLE_PHI)
1929 lhs = gimple_phi_result (stmt);
1930 else
1931 lhs = gimple_get_lhs (stmt);
1933 class expr_hash_elt element (stmt, lhs);
1935 if (dump_file && (dump_flags & TDF_DETAILS))
1937 fprintf (dump_file, "LKUP ");
1938 element.print (dump_file);
1941 /* Don't bother remembering constant assignments and copy operations.
1942 Constants and copy operations are handled by the constant/copy propagator
1943 in optimize_stmt. */
1944 if (element.expr()->kind == EXPR_SINGLE
1945 && (TREE_CODE (element.expr()->ops.single.rhs) == SSA_NAME
1946 || is_gimple_min_invariant (element.expr()->ops.single.rhs)))
1947 return NULL_TREE;
1949 /* Finally try to find the expression in the main expression hash table. */
1950 hash_table<expr_elt_hasher> *avail_exprs = avail_exprs_stack->avail_exprs ();
1951 slot = avail_exprs->find_slot (&element, (insert ? INSERT : NO_INSERT));
1952 if (slot == NULL)
1954 return NULL_TREE;
1956 else if (*slot == NULL)
1958 class expr_hash_elt *element2 = new expr_hash_elt (element);
1959 *slot = element2;
1961 avail_exprs_stack->record_expr (element2, NULL, '2');
1962 return NULL_TREE;
1965 /* If we found a redundant memory operation do an alias walk to
1966 check if we can re-use it. */
1967 if (gimple_vuse (stmt) != (*slot)->vop ())
1969 tree vuse1 = (*slot)->vop ();
1970 tree vuse2 = gimple_vuse (stmt);
1971 /* If we have a load of a register and a candidate in the
1972 hash with vuse1 then try to reach its stmt by walking
1973 up the virtual use-def chain using walk_non_aliased_vuses.
1974 But don't do this when removing expressions from the hash. */
1975 ao_ref ref;
1976 if (!(vuse1 && vuse2
1977 && gimple_assign_single_p (stmt)
1978 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME
1979 && (ao_ref_init (&ref, gimple_assign_rhs1 (stmt)), true)
1980 && walk_non_aliased_vuses (&ref, vuse2,
1981 vuse_eq, NULL, NULL, vuse1) != NULL))
1983 if (insert)
1985 class expr_hash_elt *element2 = new expr_hash_elt (element);
1987 /* Insert the expr into the hash by replacing the current
1988 entry and recording the value to restore in the
1989 avail_exprs_stack. */
1990 avail_exprs_stack->record_expr (element2, *slot, '2');
1991 *slot = element2;
1993 return NULL_TREE;
1997 /* Extract the LHS of the assignment so that it can be used as the current
1998 definition of another variable. */
1999 lhs = (*slot)->lhs ();
2001 lhs = dom_valueize (lhs);
2003 if (dump_file && (dump_flags & TDF_DETAILS))
2005 fprintf (dump_file, "FIND: ");
2006 print_generic_expr (dump_file, lhs, 0);
2007 fprintf (dump_file, "\n");
2010 return lhs;