Clean up some minor white space issues in trans-decl.c and trans-expr.c
[official-gcc.git] / gcc / tree-ssa-dom.c
blob829863794a31ee1d462dcfa4c196bc49dc5dc866
1 /* SSA Dominator optimizations for trees
2 Copyright (C) 2001-2016 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"
47 #include "dbgcnt.h"
49 /* This file implements optimizations on the dominator tree. */
51 /* Structure for recording known values of a conditional expression
52 at the exits from its block. */
54 struct cond_equivalence
56 struct hashable_expr cond;
57 tree value;
60 /* Structure for recording edge equivalences.
62 Computing and storing the edge equivalences instead of creating
63 them on-demand can save significant amounts of time, particularly
64 for pathological cases involving switch statements.
66 These structures live for a single iteration of the dominator
67 optimizer in the edge's AUX field. At the end of an iteration we
68 free each of these structures. */
70 struct edge_info
72 /* If this edge creates a simple equivalence, the LHS and RHS of
73 the equivalence will be stored here. */
74 tree lhs;
75 tree rhs;
77 /* Traversing an edge may also indicate one or more particular conditions
78 are true or false. */
79 vec<cond_equivalence> cond_equivalences;
82 /* Track whether or not we have changed the control flow graph. */
83 static bool cfg_altered;
85 /* Bitmap of blocks that have had EH statements cleaned. We should
86 remove their dead edges eventually. */
87 static bitmap need_eh_cleanup;
88 static vec<gimple *> need_noreturn_fixup;
90 /* Statistics for dominator optimizations. */
91 struct opt_stats_d
93 long num_stmts;
94 long num_exprs_considered;
95 long num_re;
96 long num_const_prop;
97 long num_copy_prop;
100 static struct opt_stats_d opt_stats;
102 /* Local functions. */
103 static edge optimize_stmt (basic_block, gimple_stmt_iterator,
104 class const_and_copies *,
105 class avail_exprs_stack *);
106 static tree lookup_avail_expr (gimple *, bool, class avail_exprs_stack *);
107 static void record_cond (cond_equivalence *, class avail_exprs_stack *);
108 static void record_equality (tree, tree, class const_and_copies *);
109 static void record_equivalences_from_phis (basic_block);
110 static void record_equivalences_from_incoming_edge (basic_block,
111 class const_and_copies *,
112 class avail_exprs_stack *);
113 static void eliminate_redundant_computations (gimple_stmt_iterator *,
114 class const_and_copies *,
115 class avail_exprs_stack *);
116 static void record_equivalences_from_stmt (gimple *, int,
117 class avail_exprs_stack *);
118 static edge single_incoming_edge_ignoring_loop_edges (basic_block);
119 static void dump_dominator_optimization_stats (FILE *file,
120 hash_table<expr_elt_hasher> *);
123 /* Free the edge_info data attached to E, if it exists. */
125 void
126 free_dom_edge_info (edge e)
128 struct edge_info *edge_info = (struct edge_info *)e->aux;
130 if (edge_info)
132 edge_info->cond_equivalences.release ();
133 free (edge_info);
137 /* Allocate an EDGE_INFO for edge E and attach it to E.
138 Return the new EDGE_INFO structure. */
140 static struct edge_info *
141 allocate_edge_info (edge e)
143 struct edge_info *edge_info;
145 /* Free the old one, if it exists. */
146 free_dom_edge_info (e);
148 edge_info = XCNEW (struct edge_info);
150 e->aux = edge_info;
151 return edge_info;
154 /* Free all EDGE_INFO structures associated with edges in the CFG.
155 If a particular edge can be threaded, copy the redirection
156 target from the EDGE_INFO structure into the edge's AUX field
157 as required by code to update the CFG and SSA graph for
158 jump threading. */
160 static void
161 free_all_edge_infos (void)
163 basic_block bb;
164 edge_iterator ei;
165 edge e;
167 FOR_EACH_BB_FN (bb, cfun)
169 FOR_EACH_EDGE (e, ei, bb->preds)
171 free_dom_edge_info (e);
172 e->aux = NULL;
177 /* Build a cond_equivalence record indicating that the comparison
178 CODE holds between operands OP0 and OP1 and push it to **P. */
180 static void
181 build_and_record_new_cond (enum tree_code code,
182 tree op0, tree op1,
183 vec<cond_equivalence> *p,
184 bool val = true)
186 cond_equivalence c;
187 struct hashable_expr *cond = &c.cond;
189 gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
191 cond->type = boolean_type_node;
192 cond->kind = EXPR_BINARY;
193 cond->ops.binary.op = code;
194 cond->ops.binary.opnd0 = op0;
195 cond->ops.binary.opnd1 = op1;
197 c.value = val ? boolean_true_node : boolean_false_node;
198 p->safe_push (c);
201 /* Record that COND is true and INVERTED is false into the edge information
202 structure. Also record that any conditions dominated by COND are true
203 as well.
205 For example, if a < b is true, then a <= b must also be true. */
207 static void
208 record_conditions (struct edge_info *edge_info, tree cond, tree inverted)
210 tree op0, op1;
211 cond_equivalence c;
213 if (!COMPARISON_CLASS_P (cond))
214 return;
216 op0 = TREE_OPERAND (cond, 0);
217 op1 = TREE_OPERAND (cond, 1);
219 switch (TREE_CODE (cond))
221 case LT_EXPR:
222 case GT_EXPR:
223 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
225 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
226 &edge_info->cond_equivalences);
227 build_and_record_new_cond (LTGT_EXPR, op0, op1,
228 &edge_info->cond_equivalences);
231 build_and_record_new_cond ((TREE_CODE (cond) == LT_EXPR
232 ? LE_EXPR : GE_EXPR),
233 op0, op1, &edge_info->cond_equivalences);
234 build_and_record_new_cond (NE_EXPR, op0, op1,
235 &edge_info->cond_equivalences);
236 build_and_record_new_cond (EQ_EXPR, op0, op1,
237 &edge_info->cond_equivalences, false);
238 break;
240 case GE_EXPR:
241 case LE_EXPR:
242 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
244 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
245 &edge_info->cond_equivalences);
247 break;
249 case EQ_EXPR:
250 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
252 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
253 &edge_info->cond_equivalences);
255 build_and_record_new_cond (LE_EXPR, op0, op1,
256 &edge_info->cond_equivalences);
257 build_and_record_new_cond (GE_EXPR, op0, op1,
258 &edge_info->cond_equivalences);
259 break;
261 case UNORDERED_EXPR:
262 build_and_record_new_cond (NE_EXPR, op0, op1,
263 &edge_info->cond_equivalences);
264 build_and_record_new_cond (UNLE_EXPR, op0, op1,
265 &edge_info->cond_equivalences);
266 build_and_record_new_cond (UNGE_EXPR, op0, op1,
267 &edge_info->cond_equivalences);
268 build_and_record_new_cond (UNEQ_EXPR, op0, op1,
269 &edge_info->cond_equivalences);
270 build_and_record_new_cond (UNLT_EXPR, op0, op1,
271 &edge_info->cond_equivalences);
272 build_and_record_new_cond (UNGT_EXPR, op0, op1,
273 &edge_info->cond_equivalences);
274 break;
276 case UNLT_EXPR:
277 case UNGT_EXPR:
278 build_and_record_new_cond ((TREE_CODE (cond) == UNLT_EXPR
279 ? UNLE_EXPR : UNGE_EXPR),
280 op0, op1, &edge_info->cond_equivalences);
281 build_and_record_new_cond (NE_EXPR, op0, op1,
282 &edge_info->cond_equivalences);
283 break;
285 case UNEQ_EXPR:
286 build_and_record_new_cond (UNLE_EXPR, op0, op1,
287 &edge_info->cond_equivalences);
288 build_and_record_new_cond (UNGE_EXPR, op0, op1,
289 &edge_info->cond_equivalences);
290 break;
292 case LTGT_EXPR:
293 build_and_record_new_cond (NE_EXPR, op0, op1,
294 &edge_info->cond_equivalences);
295 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
296 &edge_info->cond_equivalences);
297 break;
299 default:
300 break;
303 /* Now store the original true and false conditions into the first
304 two slots. */
305 initialize_expr_from_cond (cond, &c.cond);
306 c.value = boolean_true_node;
307 edge_info->cond_equivalences.safe_push (c);
309 /* It is possible for INVERTED to be the negation of a comparison,
310 and not a valid RHS or GIMPLE_COND condition. This happens because
311 invert_truthvalue may return such an expression when asked to invert
312 a floating-point comparison. These comparisons are not assumed to
313 obey the trichotomy law. */
314 initialize_expr_from_cond (inverted, &c.cond);
315 c.value = boolean_false_node;
316 edge_info->cond_equivalences.safe_push (c);
319 /* We have finished optimizing BB, record any information implied by
320 taking a specific outgoing edge from BB. */
322 static void
323 record_edge_info (basic_block bb)
325 gimple_stmt_iterator gsi = gsi_last_bb (bb);
326 struct edge_info *edge_info;
328 if (! gsi_end_p (gsi))
330 gimple *stmt = gsi_stmt (gsi);
331 location_t loc = gimple_location (stmt);
333 if (gimple_code (stmt) == GIMPLE_SWITCH)
335 gswitch *switch_stmt = as_a <gswitch *> (stmt);
336 tree index = gimple_switch_index (switch_stmt);
338 if (TREE_CODE (index) == SSA_NAME)
340 int i;
341 int n_labels = gimple_switch_num_labels (switch_stmt);
342 tree *info = XCNEWVEC (tree, last_basic_block_for_fn (cfun));
343 edge e;
344 edge_iterator ei;
346 for (i = 0; i < n_labels; i++)
348 tree label = gimple_switch_label (switch_stmt, i);
349 basic_block target_bb = label_to_block (CASE_LABEL (label));
350 if (CASE_HIGH (label)
351 || !CASE_LOW (label)
352 || info[target_bb->index])
353 info[target_bb->index] = error_mark_node;
354 else
355 info[target_bb->index] = label;
358 FOR_EACH_EDGE (e, ei, bb->succs)
360 basic_block target_bb = e->dest;
361 tree label = info[target_bb->index];
363 if (label != NULL && label != error_mark_node)
365 tree x = fold_convert_loc (loc, TREE_TYPE (index),
366 CASE_LOW (label));
367 edge_info = allocate_edge_info (e);
368 edge_info->lhs = index;
369 edge_info->rhs = x;
372 free (info);
376 /* A COND_EXPR may create equivalences too. */
377 if (gimple_code (stmt) == GIMPLE_COND)
379 edge true_edge;
380 edge false_edge;
382 tree op0 = gimple_cond_lhs (stmt);
383 tree op1 = gimple_cond_rhs (stmt);
384 enum tree_code code = gimple_cond_code (stmt);
386 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
388 /* Special case comparing booleans against a constant as we
389 know the value of OP0 on both arms of the branch. i.e., we
390 can record an equivalence for OP0 rather than COND. */
391 if ((code == EQ_EXPR || code == NE_EXPR)
392 && TREE_CODE (op0) == SSA_NAME
393 && ssa_name_has_boolean_range (op0)
394 && is_gimple_min_invariant (op1))
396 tree true_val = constant_boolean_node (true, TREE_TYPE (op0));
397 tree false_val = constant_boolean_node (false, TREE_TYPE (op0));
399 if (code == EQ_EXPR)
401 edge_info = allocate_edge_info (true_edge);
402 edge_info->lhs = op0;
403 edge_info->rhs = (integer_zerop (op1) ? false_val : true_val);
405 edge_info = allocate_edge_info (false_edge);
406 edge_info->lhs = op0;
407 edge_info->rhs = (integer_zerop (op1) ? true_val : false_val);
409 else
411 edge_info = allocate_edge_info (true_edge);
412 edge_info->lhs = op0;
413 edge_info->rhs = (integer_zerop (op1) ? true_val : false_val);
415 edge_info = allocate_edge_info (false_edge);
416 edge_info->lhs = op0;
417 edge_info->rhs = (integer_zerop (op1) ? false_val : true_val);
420 else if (is_gimple_min_invariant (op0)
421 && (TREE_CODE (op1) == SSA_NAME
422 || is_gimple_min_invariant (op1)))
424 tree cond = build2 (code, boolean_type_node, op0, op1);
425 tree inverted = invert_truthvalue_loc (loc, cond);
426 bool can_infer_simple_equiv
427 = !(HONOR_SIGNED_ZEROS (op0)
428 && real_zerop (op0));
429 struct edge_info *edge_info;
431 edge_info = allocate_edge_info (true_edge);
432 record_conditions (edge_info, cond, inverted);
434 if (can_infer_simple_equiv && code == EQ_EXPR)
436 edge_info->lhs = op1;
437 edge_info->rhs = op0;
440 edge_info = allocate_edge_info (false_edge);
441 record_conditions (edge_info, inverted, cond);
443 if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
445 edge_info->lhs = op1;
446 edge_info->rhs = op0;
450 else if (TREE_CODE (op0) == SSA_NAME
451 && (TREE_CODE (op1) == SSA_NAME
452 || is_gimple_min_invariant (op1)))
454 tree cond = build2 (code, boolean_type_node, op0, op1);
455 tree inverted = invert_truthvalue_loc (loc, cond);
456 bool can_infer_simple_equiv
457 = !(HONOR_SIGNED_ZEROS (op1)
458 && (TREE_CODE (op1) == SSA_NAME || real_zerop (op1)));
459 struct edge_info *edge_info;
461 edge_info = allocate_edge_info (true_edge);
462 record_conditions (edge_info, cond, inverted);
464 if (can_infer_simple_equiv && code == EQ_EXPR)
466 edge_info->lhs = op0;
467 edge_info->rhs = op1;
470 edge_info = allocate_edge_info (false_edge);
471 record_conditions (edge_info, inverted, cond);
473 if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
475 edge_info->lhs = op0;
476 edge_info->rhs = op1;
481 /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
486 class dom_opt_dom_walker : public dom_walker
488 public:
489 dom_opt_dom_walker (cdi_direction direction,
490 class const_and_copies *const_and_copies,
491 class avail_exprs_stack *avail_exprs_stack)
492 : dom_walker (direction, true),
493 m_const_and_copies (const_and_copies),
494 m_avail_exprs_stack (avail_exprs_stack),
495 m_dummy_cond (NULL) {}
497 virtual edge before_dom_children (basic_block);
498 virtual void after_dom_children (basic_block);
500 private:
501 void thread_across_edge (edge);
503 /* Unwindable equivalences, both const/copy and expression varieties. */
504 class const_and_copies *m_const_and_copies;
505 class avail_exprs_stack *m_avail_exprs_stack;
507 gcond *m_dummy_cond;
510 /* Jump threading, redundancy elimination and const/copy propagation.
512 This pass may expose new symbols that need to be renamed into SSA. For
513 every new symbol exposed, its corresponding bit will be set in
514 VARS_TO_RENAME. */
516 namespace {
518 const pass_data pass_data_dominator =
520 GIMPLE_PASS, /* type */
521 "dom", /* name */
522 OPTGROUP_NONE, /* optinfo_flags */
523 TV_TREE_SSA_DOMINATOR_OPTS, /* tv_id */
524 ( PROP_cfg | PROP_ssa ), /* properties_required */
525 0, /* properties_provided */
526 0, /* properties_destroyed */
527 0, /* todo_flags_start */
528 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
531 class pass_dominator : public gimple_opt_pass
533 public:
534 pass_dominator (gcc::context *ctxt)
535 : gimple_opt_pass (pass_data_dominator, ctxt),
536 may_peel_loop_headers_p (false)
539 /* opt_pass methods: */
540 opt_pass * clone () { return new pass_dominator (m_ctxt); }
541 void set_pass_param (unsigned int n, bool param)
543 gcc_assert (n == 0);
544 may_peel_loop_headers_p = param;
546 virtual bool gate (function *) { return flag_tree_dom != 0; }
547 virtual unsigned int execute (function *);
549 private:
550 /* This flag is used to prevent loops from being peeled repeatedly in jump
551 threading; it will be removed once we preserve loop structures throughout
552 the compilation -- we will be able to mark the affected loops directly in
553 jump threading, and avoid peeling them next time. */
554 bool may_peel_loop_headers_p;
555 }; // class pass_dominator
557 unsigned int
558 pass_dominator::execute (function *fun)
560 memset (&opt_stats, 0, sizeof (opt_stats));
562 /* Create our hash tables. */
563 hash_table<expr_elt_hasher> *avail_exprs
564 = new hash_table<expr_elt_hasher> (1024);
565 class avail_exprs_stack *avail_exprs_stack
566 = new class avail_exprs_stack (avail_exprs);
567 class const_and_copies *const_and_copies = new class const_and_copies ();
568 need_eh_cleanup = BITMAP_ALLOC (NULL);
569 need_noreturn_fixup.create (0);
571 calculate_dominance_info (CDI_DOMINATORS);
572 cfg_altered = false;
574 /* We need to know loop structures in order to avoid destroying them
575 in jump threading. Note that we still can e.g. thread through loop
576 headers to an exit edge, or through loop header to the loop body, assuming
577 that we update the loop info.
579 TODO: We don't need to set LOOPS_HAVE_PREHEADERS generally, but due
580 to several overly conservative bail-outs in jump threading, case
581 gcc.dg/tree-ssa/pr21417.c can't be threaded if loop preheader is
582 missing. We should improve jump threading in future then
583 LOOPS_HAVE_PREHEADERS won't be needed here. */
584 loop_optimizer_init (LOOPS_HAVE_PREHEADERS | LOOPS_HAVE_SIMPLE_LATCHES);
586 /* Initialize the value-handle array. */
587 threadedge_initialize_values ();
589 /* We need accurate information regarding back edges in the CFG
590 for jump threading; this may include back edges that are not part of
591 a single loop. */
592 mark_dfs_back_edges ();
594 /* We want to create the edge info structures before the dominator walk
595 so that they'll be in place for the jump threader, particularly when
596 threading through a join block.
598 The conditions will be lazily updated with global equivalences as
599 we reach them during the dominator walk. */
600 basic_block bb;
601 FOR_EACH_BB_FN (bb, fun)
602 record_edge_info (bb);
604 /* Recursively walk the dominator tree optimizing statements. */
605 dom_opt_dom_walker walker (CDI_DOMINATORS,
606 const_and_copies,
607 avail_exprs_stack);
608 walker.walk (fun->cfg->x_entry_block_ptr);
610 /* Look for blocks where we cleared EDGE_EXECUTABLE on an outgoing
611 edge. When found, remove jump threads which contain any outgoing
612 edge from the affected block. */
613 if (cfg_altered)
615 FOR_EACH_BB_FN (bb, fun)
617 edge_iterator ei;
618 edge e;
620 /* First see if there are any edges without EDGE_EXECUTABLE
621 set. */
622 bool found = false;
623 FOR_EACH_EDGE (e, ei, bb->succs)
625 if ((e->flags & EDGE_EXECUTABLE) == 0)
627 found = true;
628 break;
632 /* If there were any such edges found, then remove jump threads
633 containing any edge leaving BB. */
634 if (found)
635 FOR_EACH_EDGE (e, ei, bb->succs)
636 remove_jump_threads_including (e);
641 gimple_stmt_iterator gsi;
642 basic_block bb;
643 FOR_EACH_BB_FN (bb, fun)
645 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
646 update_stmt_if_modified (gsi_stmt (gsi));
650 /* If we exposed any new variables, go ahead and put them into
651 SSA form now, before we handle jump threading. This simplifies
652 interactions between rewriting of _DECL nodes into SSA form
653 and rewriting SSA_NAME nodes into SSA form after block
654 duplication and CFG manipulation. */
655 update_ssa (TODO_update_ssa);
657 free_all_edge_infos ();
659 /* Thread jumps, creating duplicate blocks as needed. */
660 cfg_altered |= thread_through_all_blocks (may_peel_loop_headers_p);
662 if (cfg_altered)
663 free_dominance_info (CDI_DOMINATORS);
665 /* Removal of statements may make some EH edges dead. Purge
666 such edges from the CFG as needed. */
667 if (!bitmap_empty_p (need_eh_cleanup))
669 unsigned i;
670 bitmap_iterator bi;
672 /* Jump threading may have created forwarder blocks from blocks
673 needing EH cleanup; the new successor of these blocks, which
674 has inherited from the original block, needs the cleanup.
675 Don't clear bits in the bitmap, as that can break the bitmap
676 iterator. */
677 EXECUTE_IF_SET_IN_BITMAP (need_eh_cleanup, 0, i, bi)
679 basic_block bb = BASIC_BLOCK_FOR_FN (fun, i);
680 if (bb == NULL)
681 continue;
682 while (single_succ_p (bb)
683 && (single_succ_edge (bb)->flags & EDGE_EH) == 0)
684 bb = single_succ (bb);
685 if (bb == EXIT_BLOCK_PTR_FOR_FN (fun))
686 continue;
687 if ((unsigned) bb->index != i)
688 bitmap_set_bit (need_eh_cleanup, bb->index);
691 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
692 bitmap_clear (need_eh_cleanup);
695 /* Fixup stmts that became noreturn calls. This may require splitting
696 blocks and thus isn't possible during the dominator walk or before
697 jump threading finished. Do this in reverse order so we don't
698 inadvertedly remove a stmt we want to fixup by visiting a dominating
699 now noreturn call first. */
700 while (!need_noreturn_fixup.is_empty ())
702 gimple *stmt = need_noreturn_fixup.pop ();
703 if (dump_file && dump_flags & TDF_DETAILS)
705 fprintf (dump_file, "Fixing up noreturn call ");
706 print_gimple_stmt (dump_file, stmt, 0, 0);
707 fprintf (dump_file, "\n");
709 fixup_noreturn_call (stmt);
712 statistics_counter_event (fun, "Redundant expressions eliminated",
713 opt_stats.num_re);
714 statistics_counter_event (fun, "Constants propagated",
715 opt_stats.num_const_prop);
716 statistics_counter_event (fun, "Copies propagated",
717 opt_stats.num_copy_prop);
719 /* Debugging dumps. */
720 if (dump_file && (dump_flags & TDF_STATS))
721 dump_dominator_optimization_stats (dump_file, avail_exprs);
723 loop_optimizer_finalize ();
725 /* Delete our main hashtable. */
726 delete avail_exprs;
727 avail_exprs = NULL;
729 /* Free asserted bitmaps and stacks. */
730 BITMAP_FREE (need_eh_cleanup);
731 need_noreturn_fixup.release ();
732 delete avail_exprs_stack;
733 delete const_and_copies;
735 /* Free the value-handle array. */
736 threadedge_finalize_values ();
738 return 0;
741 } // anon namespace
743 gimple_opt_pass *
744 make_pass_dominator (gcc::context *ctxt)
746 return new pass_dominator (ctxt);
750 /* Given a conditional statement CONDSTMT, convert the
751 condition to a canonical form. */
753 static void
754 canonicalize_comparison (gcond *condstmt)
756 tree op0;
757 tree op1;
758 enum tree_code code;
760 gcc_assert (gimple_code (condstmt) == GIMPLE_COND);
762 op0 = gimple_cond_lhs (condstmt);
763 op1 = gimple_cond_rhs (condstmt);
765 code = gimple_cond_code (condstmt);
767 /* If it would be profitable to swap the operands, then do so to
768 canonicalize the statement, enabling better optimization.
770 By placing canonicalization of such expressions here we
771 transparently keep statements in canonical form, even
772 when the statement is modified. */
773 if (tree_swap_operands_p (op0, op1, false))
775 /* For relationals we need to swap the operands
776 and change the code. */
777 if (code == LT_EXPR
778 || code == GT_EXPR
779 || code == LE_EXPR
780 || code == GE_EXPR)
782 code = swap_tree_comparison (code);
784 gimple_cond_set_code (condstmt, code);
785 gimple_cond_set_lhs (condstmt, op1);
786 gimple_cond_set_rhs (condstmt, op0);
788 update_stmt (condstmt);
793 /* A trivial wrapper so that we can present the generic jump
794 threading code with a simple API for simplifying statements. */
795 static tree
796 simplify_stmt_for_jump_threading (gimple *stmt,
797 gimple *within_stmt ATTRIBUTE_UNUSED,
798 class avail_exprs_stack *avail_exprs_stack)
800 return lookup_avail_expr (stmt, false, avail_exprs_stack);
803 /* Valueize hook for gimple_fold_stmt_to_constant_1. */
805 static tree
806 dom_valueize (tree t)
808 if (TREE_CODE (t) == SSA_NAME)
810 tree tem = SSA_NAME_VALUE (t);
811 if (tem)
812 return tem;
814 return t;
817 /* Record into CONST_AND_COPIES and AVAIL_EXPRS_STACK any equivalences implied
818 by traversing edge E (which are cached in E->aux).
820 Callers are responsible for managing the unwinding markers. */
821 void
822 record_temporary_equivalences (edge e,
823 class const_and_copies *const_and_copies,
824 class avail_exprs_stack *avail_exprs_stack)
826 int i;
827 struct edge_info *edge_info = (struct edge_info *) e->aux;
829 /* If we have info associated with this edge, record it into
830 our equivalence tables. */
831 if (edge_info)
833 cond_equivalence *eq;
834 tree lhs = edge_info->lhs;
835 tree rhs = edge_info->rhs;
837 /* If we have a simple NAME = VALUE equivalence, record it. */
838 if (lhs)
839 record_equality (lhs, rhs, const_and_copies);
841 /* If LHS is an SSA_NAME and RHS is a constant integer and LHS was
842 set via a widening type conversion, then we may be able to record
843 additional equivalences. */
844 if (lhs
845 && TREE_CODE (lhs) == SSA_NAME
846 && TREE_CODE (rhs) == INTEGER_CST)
848 gimple *defstmt = SSA_NAME_DEF_STMT (lhs);
850 if (defstmt
851 && is_gimple_assign (defstmt)
852 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (defstmt)))
854 tree old_rhs = gimple_assign_rhs1 (defstmt);
856 /* If the conversion widens the original value and
857 the constant is in the range of the type of OLD_RHS,
858 then convert the constant and record the equivalence.
860 Note that int_fits_type_p does not check the precision
861 if the upper and lower bounds are OK. */
862 if (INTEGRAL_TYPE_P (TREE_TYPE (old_rhs))
863 && (TYPE_PRECISION (TREE_TYPE (lhs))
864 > TYPE_PRECISION (TREE_TYPE (old_rhs)))
865 && int_fits_type_p (rhs, TREE_TYPE (old_rhs)))
867 tree newval = fold_convert (TREE_TYPE (old_rhs), rhs);
868 record_equality (old_rhs, newval, const_and_copies);
873 /* If LHS is an SSA_NAME with a new equivalency then try if
874 stmts with uses of that LHS that dominate the edge destination
875 simplify and allow further equivalences to be recorded. */
876 if (lhs && TREE_CODE (lhs) == SSA_NAME)
878 use_operand_p use_p;
879 imm_use_iterator iter;
880 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
882 gimple *use_stmt = USE_STMT (use_p);
884 /* Only bother to record more equivalences for lhs that
885 can be directly used by e->dest.
886 ??? If the code gets re-organized to a worklist to
887 catch more indirect opportunities and it is made to
888 handle PHIs then this should only consider use_stmts
889 in basic-blocks we have already visited. */
890 if (e->dest == gimple_bb (use_stmt)
891 || !dominated_by_p (CDI_DOMINATORS,
892 e->dest, gimple_bb (use_stmt)))
893 continue;
894 tree lhs2 = gimple_get_lhs (use_stmt);
895 if (lhs2 && TREE_CODE (lhs2) == SSA_NAME)
897 tree res
898 = gimple_fold_stmt_to_constant_1 (use_stmt, dom_valueize,
899 no_follow_ssa_edges);
900 if (res
901 && (TREE_CODE (res) == SSA_NAME
902 || is_gimple_min_invariant (res)))
903 record_equality (lhs2, res, const_and_copies);
908 /* If we have 0 = COND or 1 = COND equivalences, record them
909 into our expression hash tables. */
910 for (i = 0; edge_info->cond_equivalences.iterate (i, &eq); ++i)
911 record_cond (eq, avail_exprs_stack);
915 /* Wrapper for common code to attempt to thread an edge. For example,
916 it handles lazily building the dummy condition and the bookkeeping
917 when jump threading is successful. */
919 void
920 dom_opt_dom_walker::thread_across_edge (edge e)
922 if (! m_dummy_cond)
923 m_dummy_cond =
924 gimple_build_cond (NE_EXPR,
925 integer_zero_node, integer_zero_node,
926 NULL, NULL);
928 /* Push a marker on both stacks so we can unwind the tables back to their
929 current state. */
930 m_avail_exprs_stack->push_marker ();
931 m_const_and_copies->push_marker ();
933 /* Traversing E may result in equivalences we can utilize. */
934 record_temporary_equivalences (e, m_const_and_copies, m_avail_exprs_stack);
936 /* With all the edge equivalences in the tables, go ahead and attempt
937 to thread through E->dest. */
938 ::thread_across_edge (m_dummy_cond, e, false,
939 m_const_and_copies, m_avail_exprs_stack,
940 simplify_stmt_for_jump_threading);
942 /* And restore the various tables to their state before
943 we threaded this edge.
945 XXX The code in tree-ssa-threadedge.c will restore the state of
946 the const_and_copies table. We we just have to restore the expression
947 table. */
948 m_avail_exprs_stack->pop_to_marker ();
951 /* PHI nodes can create equivalences too.
953 Ignoring any alternatives which are the same as the result, if
954 all the alternatives are equal, then the PHI node creates an
955 equivalence. */
957 static void
958 record_equivalences_from_phis (basic_block bb)
960 gphi_iterator gsi;
962 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
964 gphi *phi = gsi.phi ();
966 tree lhs = gimple_phi_result (phi);
967 tree rhs = NULL;
968 size_t i;
970 for (i = 0; i < gimple_phi_num_args (phi); i++)
972 tree t = gimple_phi_arg_def (phi, i);
974 /* Ignore alternatives which are the same as our LHS. Since
975 LHS is a PHI_RESULT, it is known to be a SSA_NAME, so we
976 can simply compare pointers. */
977 if (lhs == t)
978 continue;
980 /* If the associated edge is not marked as executable, then it
981 can be ignored. */
982 if ((gimple_phi_arg_edge (phi, i)->flags & EDGE_EXECUTABLE) == 0)
983 continue;
985 t = dom_valueize (t);
987 /* If we have not processed an alternative yet, then set
988 RHS to this alternative. */
989 if (rhs == NULL)
990 rhs = t;
991 /* If we have processed an alternative (stored in RHS), then
992 see if it is equal to this one. If it isn't, then stop
993 the search. */
994 else if (! operand_equal_for_phi_arg_p (rhs, t))
995 break;
998 /* If we had no interesting alternatives, then all the RHS alternatives
999 must have been the same as LHS. */
1000 if (!rhs)
1001 rhs = lhs;
1003 /* If we managed to iterate through each PHI alternative without
1004 breaking out of the loop, then we have a PHI which may create
1005 a useful equivalence. We do not need to record unwind data for
1006 this, since this is a true assignment and not an equivalence
1007 inferred from a comparison. All uses of this ssa name are dominated
1008 by this assignment, so unwinding just costs time and space. */
1009 if (i == gimple_phi_num_args (phi)
1010 && may_propagate_copy (lhs, rhs))
1011 set_ssa_name_value (lhs, rhs);
1015 /* Ignoring loop backedges, if BB has precisely one incoming edge then
1016 return that edge. Otherwise return NULL. */
1017 static edge
1018 single_incoming_edge_ignoring_loop_edges (basic_block bb)
1020 edge retval = NULL;
1021 edge e;
1022 edge_iterator ei;
1024 FOR_EACH_EDGE (e, ei, bb->preds)
1026 /* A loop back edge can be identified by the destination of
1027 the edge dominating the source of the edge. */
1028 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
1029 continue;
1031 /* We can safely ignore edges that are not executable. */
1032 if ((e->flags & EDGE_EXECUTABLE) == 0)
1033 continue;
1035 /* If we have already seen a non-loop edge, then we must have
1036 multiple incoming non-loop edges and thus we return NULL. */
1037 if (retval)
1038 return NULL;
1040 /* This is the first non-loop incoming edge we have found. Record
1041 it. */
1042 retval = e;
1045 return retval;
1048 /* Record any equivalences created by the incoming edge to BB into
1049 CONST_AND_COPIES and AVAIL_EXPRS_STACK. If BB has more than one
1050 incoming edge, then no equivalence is created. */
1052 static void
1053 record_equivalences_from_incoming_edge (basic_block bb,
1054 class const_and_copies *const_and_copies,
1055 class avail_exprs_stack *avail_exprs_stack)
1057 edge e;
1058 basic_block parent;
1060 /* If our parent block ended with a control statement, then we may be
1061 able to record some equivalences based on which outgoing edge from
1062 the parent was followed. */
1063 parent = get_immediate_dominator (CDI_DOMINATORS, bb);
1065 e = single_incoming_edge_ignoring_loop_edges (bb);
1067 /* If we had a single incoming edge from our parent block, then enter
1068 any data associated with the edge into our tables. */
1069 if (e && e->src == parent)
1070 record_temporary_equivalences (e, const_and_copies, avail_exprs_stack);
1073 /* Dump statistics for the hash table HTAB. */
1075 static void
1076 htab_statistics (FILE *file, const hash_table<expr_elt_hasher> &htab)
1078 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
1079 (long) htab.size (),
1080 (long) htab.elements (),
1081 htab.collisions ());
1084 /* Dump SSA statistics on FILE. */
1086 static void
1087 dump_dominator_optimization_stats (FILE *file,
1088 hash_table<expr_elt_hasher> *avail_exprs)
1090 fprintf (file, "Total number of statements: %6ld\n\n",
1091 opt_stats.num_stmts);
1092 fprintf (file, "Exprs considered for dominator optimizations: %6ld\n",
1093 opt_stats.num_exprs_considered);
1095 fprintf (file, "\nHash table statistics:\n");
1097 fprintf (file, " avail_exprs: ");
1098 htab_statistics (file, *avail_exprs);
1102 /* Enter condition equivalence P into AVAIL_EXPRS_HASH.
1104 This indicates that a conditional expression has a known
1105 boolean value. */
1107 static void
1108 record_cond (cond_equivalence *p,
1109 class avail_exprs_stack *avail_exprs_stack)
1111 class expr_hash_elt *element = new expr_hash_elt (&p->cond, p->value);
1112 expr_hash_elt **slot;
1114 hash_table<expr_elt_hasher> *avail_exprs = avail_exprs_stack->avail_exprs ();
1115 slot = avail_exprs->find_slot_with_hash (element, element->hash (), INSERT);
1116 if (*slot == NULL)
1118 *slot = element;
1119 avail_exprs_stack->record_expr (element, NULL, '1');
1121 else
1122 delete element;
1125 /* Return the loop depth of the basic block of the defining statement of X.
1126 This number should not be treated as absolutely correct because the loop
1127 information may not be completely up-to-date when dom runs. However, it
1128 will be relatively correct, and as more passes are taught to keep loop info
1129 up to date, the result will become more and more accurate. */
1131 static int
1132 loop_depth_of_name (tree x)
1134 gimple *defstmt;
1135 basic_block defbb;
1137 /* If it's not an SSA_NAME, we have no clue where the definition is. */
1138 if (TREE_CODE (x) != SSA_NAME)
1139 return 0;
1141 /* Otherwise return the loop depth of the defining statement's bb.
1142 Note that there may not actually be a bb for this statement, if the
1143 ssa_name is live on entry. */
1144 defstmt = SSA_NAME_DEF_STMT (x);
1145 defbb = gimple_bb (defstmt);
1146 if (!defbb)
1147 return 0;
1149 return bb_loop_depth (defbb);
1152 /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1153 This constrains the cases in which we may treat this as assignment. */
1155 static void
1156 record_equality (tree x, tree y, class const_and_copies *const_and_copies)
1158 tree prev_x = NULL, prev_y = NULL;
1160 if (tree_swap_operands_p (x, y, false))
1161 std::swap (x, y);
1163 /* Most of the time tree_swap_operands_p does what we want. But there
1164 are cases where we know one operand is better for copy propagation than
1165 the other. Given no other code cares about ordering of equality
1166 comparison operators for that purpose, we just handle the special cases
1167 here. */
1168 if (TREE_CODE (x) == SSA_NAME && TREE_CODE (y) == SSA_NAME)
1170 /* If one operand is a single use operand, then make it
1171 X. This will preserve its single use properly and if this
1172 conditional is eliminated, the computation of X can be
1173 eliminated as well. */
1174 if (has_single_use (y) && ! has_single_use (x))
1175 std::swap (x, y);
1177 if (TREE_CODE (x) == SSA_NAME)
1178 prev_x = SSA_NAME_VALUE (x);
1179 if (TREE_CODE (y) == SSA_NAME)
1180 prev_y = SSA_NAME_VALUE (y);
1182 /* If one of the previous values is invariant, or invariant in more loops
1183 (by depth), then use that.
1184 Otherwise it doesn't matter which value we choose, just so
1185 long as we canonicalize on one value. */
1186 if (is_gimple_min_invariant (y))
1188 else if (is_gimple_min_invariant (x)
1189 /* ??? When threading over backedges the following is important
1190 for correctness. See PR61757. */
1191 || (loop_depth_of_name (x) < loop_depth_of_name (y)))
1192 prev_x = x, x = y, y = prev_x, prev_x = prev_y;
1193 else if (prev_x && is_gimple_min_invariant (prev_x))
1194 x = y, y = prev_x, prev_x = prev_y;
1195 else if (prev_y)
1196 y = prev_y;
1198 /* After the swapping, we must have one SSA_NAME. */
1199 if (TREE_CODE (x) != SSA_NAME)
1200 return;
1202 /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1203 variable compared against zero. If we're honoring signed zeros,
1204 then we cannot record this value unless we know that the value is
1205 nonzero. */
1206 if (HONOR_SIGNED_ZEROS (x)
1207 && (TREE_CODE (y) != REAL_CST
1208 || real_equal (&dconst0, &TREE_REAL_CST (y))))
1209 return;
1211 const_and_copies->record_const_or_copy (x, y, prev_x);
1214 /* Returns true when STMT is a simple iv increment. It detects the
1215 following situation:
1217 i_1 = phi (..., i_2)
1218 i_2 = i_1 +/- ... */
1220 bool
1221 simple_iv_increment_p (gimple *stmt)
1223 enum tree_code code;
1224 tree lhs, preinc;
1225 gimple *phi;
1226 size_t i;
1228 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1229 return false;
1231 lhs = gimple_assign_lhs (stmt);
1232 if (TREE_CODE (lhs) != SSA_NAME)
1233 return false;
1235 code = gimple_assign_rhs_code (stmt);
1236 if (code != PLUS_EXPR
1237 && code != MINUS_EXPR
1238 && code != POINTER_PLUS_EXPR)
1239 return false;
1241 preinc = gimple_assign_rhs1 (stmt);
1242 if (TREE_CODE (preinc) != SSA_NAME)
1243 return false;
1245 phi = SSA_NAME_DEF_STMT (preinc);
1246 if (gimple_code (phi) != GIMPLE_PHI)
1247 return false;
1249 for (i = 0; i < gimple_phi_num_args (phi); i++)
1250 if (gimple_phi_arg_def (phi, i) == lhs)
1251 return true;
1253 return false;
1256 /* Propagate know values from SSA_NAME_VALUE into the PHI nodes of the
1257 successors of BB. */
1259 static void
1260 cprop_into_successor_phis (basic_block bb,
1261 class const_and_copies *const_and_copies)
1263 edge e;
1264 edge_iterator ei;
1266 FOR_EACH_EDGE (e, ei, bb->succs)
1268 int indx;
1269 gphi_iterator gsi;
1271 /* If this is an abnormal edge, then we do not want to copy propagate
1272 into the PHI alternative associated with this edge. */
1273 if (e->flags & EDGE_ABNORMAL)
1274 continue;
1276 gsi = gsi_start_phis (e->dest);
1277 if (gsi_end_p (gsi))
1278 continue;
1280 /* We may have an equivalence associated with this edge. While
1281 we can not propagate it into non-dominated blocks, we can
1282 propagate them into PHIs in non-dominated blocks. */
1284 /* Push the unwind marker so we can reset the const and copies
1285 table back to its original state after processing this edge. */
1286 const_and_copies->push_marker ();
1288 /* Extract and record any simple NAME = VALUE equivalences.
1290 Don't bother with [01] = COND equivalences, they're not useful
1291 here. */
1292 struct edge_info *edge_info = (struct edge_info *) e->aux;
1293 if (edge_info)
1295 tree lhs = edge_info->lhs;
1296 tree rhs = edge_info->rhs;
1298 if (lhs && TREE_CODE (lhs) == SSA_NAME)
1299 const_and_copies->record_const_or_copy (lhs, rhs);
1302 indx = e->dest_idx;
1303 for ( ; !gsi_end_p (gsi); gsi_next (&gsi))
1305 tree new_val;
1306 use_operand_p orig_p;
1307 tree orig_val;
1308 gphi *phi = gsi.phi ();
1310 /* The alternative may be associated with a constant, so verify
1311 it is an SSA_NAME before doing anything with it. */
1312 orig_p = gimple_phi_arg_imm_use_ptr (phi, indx);
1313 orig_val = get_use_from_ptr (orig_p);
1314 if (TREE_CODE (orig_val) != SSA_NAME)
1315 continue;
1317 /* If we have *ORIG_P in our constant/copy table, then replace
1318 ORIG_P with its value in our constant/copy table. */
1319 new_val = SSA_NAME_VALUE (orig_val);
1320 if (new_val
1321 && new_val != orig_val
1322 && (TREE_CODE (new_val) == SSA_NAME
1323 || is_gimple_min_invariant (new_val))
1324 && may_propagate_copy (orig_val, new_val))
1325 propagate_value (orig_p, new_val);
1328 const_and_copies->pop_to_marker ();
1332 edge
1333 dom_opt_dom_walker::before_dom_children (basic_block bb)
1335 gimple_stmt_iterator gsi;
1337 if (dump_file && (dump_flags & TDF_DETAILS))
1338 fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
1340 /* Push a marker on the stacks of local information so that we know how
1341 far to unwind when we finalize this block. */
1342 m_avail_exprs_stack->push_marker ();
1343 m_const_and_copies->push_marker ();
1345 record_equivalences_from_incoming_edge (bb, m_const_and_copies,
1346 m_avail_exprs_stack);
1348 /* PHI nodes can create equivalences too. */
1349 record_equivalences_from_phis (bb);
1351 /* Create equivalences from redundant PHIs. PHIs are only truly
1352 redundant when they exist in the same block, so push another
1353 marker and unwind right afterwards. */
1354 m_avail_exprs_stack->push_marker ();
1355 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1356 eliminate_redundant_computations (&gsi, m_const_and_copies,
1357 m_avail_exprs_stack);
1358 m_avail_exprs_stack->pop_to_marker ();
1360 edge taken_edge = NULL;
1361 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1362 taken_edge
1363 = optimize_stmt (bb, gsi, m_const_and_copies, m_avail_exprs_stack);
1365 /* Now prepare to process dominated blocks. */
1366 record_edge_info (bb);
1367 cprop_into_successor_phis (bb, m_const_and_copies);
1368 if (taken_edge && !dbg_cnt (dom_unreachable_edges))
1369 return NULL;
1371 return taken_edge;
1374 /* We have finished processing the dominator children of BB, perform
1375 any finalization actions in preparation for leaving this node in
1376 the dominator tree. */
1378 void
1379 dom_opt_dom_walker::after_dom_children (basic_block bb)
1381 gimple *last;
1383 /* If we have an outgoing edge to a block with multiple incoming and
1384 outgoing edges, then we may be able to thread the edge, i.e., we
1385 may be able to statically determine which of the outgoing edges
1386 will be traversed when the incoming edge from BB is traversed. */
1387 if (single_succ_p (bb)
1388 && (single_succ_edge (bb)->flags & EDGE_ABNORMAL) == 0
1389 && potentially_threadable_block (single_succ (bb)))
1391 thread_across_edge (single_succ_edge (bb));
1393 else if ((last = last_stmt (bb))
1394 && gimple_code (last) == GIMPLE_COND
1395 && EDGE_COUNT (bb->succs) == 2
1396 && (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL) == 0
1397 && (EDGE_SUCC (bb, 1)->flags & EDGE_ABNORMAL) == 0)
1399 edge true_edge, false_edge;
1401 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1403 /* Only try to thread the edge if it reaches a target block with
1404 more than one predecessor and more than one successor. */
1405 if (potentially_threadable_block (true_edge->dest))
1406 thread_across_edge (true_edge);
1408 /* Similarly for the ELSE arm. */
1409 if (potentially_threadable_block (false_edge->dest))
1410 thread_across_edge (false_edge);
1414 /* These remove expressions local to BB from the tables. */
1415 m_avail_exprs_stack->pop_to_marker ();
1416 m_const_and_copies->pop_to_marker ();
1419 /* Search for redundant computations in STMT. If any are found, then
1420 replace them with the variable holding the result of the computation.
1422 If safe, record this expression into AVAIL_EXPRS_STACK and
1423 CONST_AND_COPIES. */
1425 static void
1426 eliminate_redundant_computations (gimple_stmt_iterator* gsi,
1427 class const_and_copies *const_and_copies,
1428 class avail_exprs_stack *avail_exprs_stack)
1430 tree expr_type;
1431 tree cached_lhs;
1432 tree def;
1433 bool insert = true;
1434 bool assigns_var_p = false;
1436 gimple *stmt = gsi_stmt (*gsi);
1438 if (gimple_code (stmt) == GIMPLE_PHI)
1439 def = gimple_phi_result (stmt);
1440 else
1441 def = gimple_get_lhs (stmt);
1443 /* Certain expressions on the RHS can be optimized away, but can not
1444 themselves be entered into the hash tables. */
1445 if (! def
1446 || TREE_CODE (def) != SSA_NAME
1447 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)
1448 || gimple_vdef (stmt)
1449 /* Do not record equivalences for increments of ivs. This would create
1450 overlapping live ranges for a very questionable gain. */
1451 || simple_iv_increment_p (stmt))
1452 insert = false;
1454 /* Check if the expression has been computed before. */
1455 cached_lhs = lookup_avail_expr (stmt, insert, avail_exprs_stack);
1457 opt_stats.num_exprs_considered++;
1459 /* Get the type of the expression we are trying to optimize. */
1460 if (is_gimple_assign (stmt))
1462 expr_type = TREE_TYPE (gimple_assign_lhs (stmt));
1463 assigns_var_p = true;
1465 else if (gimple_code (stmt) == GIMPLE_COND)
1466 expr_type = boolean_type_node;
1467 else if (is_gimple_call (stmt))
1469 gcc_assert (gimple_call_lhs (stmt));
1470 expr_type = TREE_TYPE (gimple_call_lhs (stmt));
1471 assigns_var_p = true;
1473 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
1474 expr_type = TREE_TYPE (gimple_switch_index (swtch_stmt));
1475 else if (gimple_code (stmt) == GIMPLE_PHI)
1476 /* We can't propagate into a phi, so the logic below doesn't apply.
1477 Instead record an equivalence between the cached LHS and the
1478 PHI result of this statement, provided they are in the same block.
1479 This should be sufficient to kill the redundant phi. */
1481 if (def && cached_lhs)
1482 const_and_copies->record_const_or_copy (def, cached_lhs);
1483 return;
1485 else
1486 gcc_unreachable ();
1488 if (!cached_lhs)
1489 return;
1491 /* It is safe to ignore types here since we have already done
1492 type checking in the hashing and equality routines. In fact
1493 type checking here merely gets in the way of constant
1494 propagation. Also, make sure that it is safe to propagate
1495 CACHED_LHS into the expression in STMT. */
1496 if ((TREE_CODE (cached_lhs) != SSA_NAME
1497 && (assigns_var_p
1498 || useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs))))
1499 || may_propagate_copy_into_stmt (stmt, cached_lhs))
1501 gcc_checking_assert (TREE_CODE (cached_lhs) == SSA_NAME
1502 || is_gimple_min_invariant (cached_lhs));
1504 if (dump_file && (dump_flags & TDF_DETAILS))
1506 fprintf (dump_file, " Replaced redundant expr '");
1507 print_gimple_expr (dump_file, stmt, 0, dump_flags);
1508 fprintf (dump_file, "' with '");
1509 print_generic_expr (dump_file, cached_lhs, dump_flags);
1510 fprintf (dump_file, "'\n");
1513 opt_stats.num_re++;
1515 if (assigns_var_p
1516 && !useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs)))
1517 cached_lhs = fold_convert (expr_type, cached_lhs);
1519 propagate_tree_value_into_stmt (gsi, cached_lhs);
1521 /* Since it is always necessary to mark the result as modified,
1522 perhaps we should move this into propagate_tree_value_into_stmt
1523 itself. */
1524 gimple_set_modified (gsi_stmt (*gsi), true);
1528 /* STMT, a GIMPLE_ASSIGN, may create certain equivalences, in either
1529 the available expressions table or the const_and_copies table.
1530 Detect and record those equivalences into AVAIL_EXPRS_STACK.
1532 We handle only very simple copy equivalences here. The heavy
1533 lifing is done by eliminate_redundant_computations. */
1535 static void
1536 record_equivalences_from_stmt (gimple *stmt, int may_optimize_p,
1537 class avail_exprs_stack *avail_exprs_stack)
1539 tree lhs;
1540 enum tree_code lhs_code;
1542 gcc_assert (is_gimple_assign (stmt));
1544 lhs = gimple_assign_lhs (stmt);
1545 lhs_code = TREE_CODE (lhs);
1547 if (lhs_code == SSA_NAME
1548 && gimple_assign_single_p (stmt))
1550 tree rhs = gimple_assign_rhs1 (stmt);
1552 /* If the RHS of the assignment is a constant or another variable that
1553 may be propagated, register it in the CONST_AND_COPIES table. We
1554 do not need to record unwind data for this, since this is a true
1555 assignment and not an equivalence inferred from a comparison. All
1556 uses of this ssa name are dominated by this assignment, so unwinding
1557 just costs time and space. */
1558 if (may_optimize_p
1559 && (TREE_CODE (rhs) == SSA_NAME
1560 || is_gimple_min_invariant (rhs)))
1562 rhs = dom_valueize (rhs);
1564 if (dump_file && (dump_flags & TDF_DETAILS))
1566 fprintf (dump_file, "==== ASGN ");
1567 print_generic_expr (dump_file, lhs, 0);
1568 fprintf (dump_file, " = ");
1569 print_generic_expr (dump_file, rhs, 0);
1570 fprintf (dump_file, "\n");
1573 set_ssa_name_value (lhs, rhs);
1577 /* Make sure we can propagate &x + CST. */
1578 if (lhs_code == SSA_NAME
1579 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
1580 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR
1581 && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)
1583 tree op0 = gimple_assign_rhs1 (stmt);
1584 tree op1 = gimple_assign_rhs2 (stmt);
1585 tree new_rhs
1586 = build_fold_addr_expr (fold_build2 (MEM_REF,
1587 TREE_TYPE (TREE_TYPE (op0)),
1588 unshare_expr (op0),
1589 fold_convert (ptr_type_node,
1590 op1)));
1591 if (dump_file && (dump_flags & TDF_DETAILS))
1593 fprintf (dump_file, "==== ASGN ");
1594 print_generic_expr (dump_file, lhs, 0);
1595 fprintf (dump_file, " = ");
1596 print_generic_expr (dump_file, new_rhs, 0);
1597 fprintf (dump_file, "\n");
1600 set_ssa_name_value (lhs, new_rhs);
1603 /* A memory store, even an aliased store, creates a useful
1604 equivalence. By exchanging the LHS and RHS, creating suitable
1605 vops and recording the result in the available expression table,
1606 we may be able to expose more redundant loads. */
1607 if (!gimple_has_volatile_ops (stmt)
1608 && gimple_references_memory_p (stmt)
1609 && gimple_assign_single_p (stmt)
1610 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
1611 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
1612 && !is_gimple_reg (lhs))
1614 tree rhs = gimple_assign_rhs1 (stmt);
1615 gassign *new_stmt;
1617 /* Build a new statement with the RHS and LHS exchanged. */
1618 if (TREE_CODE (rhs) == SSA_NAME)
1620 /* NOTE tuples. The call to gimple_build_assign below replaced
1621 a call to build_gimple_modify_stmt, which did not set the
1622 SSA_NAME_DEF_STMT on the LHS of the assignment. Doing so
1623 may cause an SSA validation failure, as the LHS may be a
1624 default-initialized name and should have no definition. I'm
1625 a bit dubious of this, as the artificial statement that we
1626 generate here may in fact be ill-formed, but it is simply
1627 used as an internal device in this pass, and never becomes
1628 part of the CFG. */
1629 gimple *defstmt = SSA_NAME_DEF_STMT (rhs);
1630 new_stmt = gimple_build_assign (rhs, lhs);
1631 SSA_NAME_DEF_STMT (rhs) = defstmt;
1633 else
1634 new_stmt = gimple_build_assign (rhs, lhs);
1636 gimple_set_vuse (new_stmt, gimple_vdef (stmt));
1638 /* Finally enter the statement into the available expression
1639 table. */
1640 lookup_avail_expr (new_stmt, true, avail_exprs_stack);
1644 /* Replace *OP_P in STMT with any known equivalent value for *OP_P from
1645 CONST_AND_COPIES. */
1647 static void
1648 cprop_operand (gimple *stmt, use_operand_p op_p)
1650 tree val;
1651 tree op = USE_FROM_PTR (op_p);
1653 /* If the operand has a known constant value or it is known to be a
1654 copy of some other variable, use the value or copy stored in
1655 CONST_AND_COPIES. */
1656 val = SSA_NAME_VALUE (op);
1657 if (val && val != op)
1659 /* Do not replace hard register operands in asm statements. */
1660 if (gimple_code (stmt) == GIMPLE_ASM
1661 && !may_propagate_copy_into_asm (op))
1662 return;
1664 /* Certain operands are not allowed to be copy propagated due
1665 to their interaction with exception handling and some GCC
1666 extensions. */
1667 if (!may_propagate_copy (op, val))
1668 return;
1670 /* Do not propagate copies into BIVs.
1671 See PR23821 and PR62217 for how this can disturb IV and
1672 number of iteration analysis. */
1673 if (TREE_CODE (val) != INTEGER_CST)
1675 gimple *def = SSA_NAME_DEF_STMT (op);
1676 if (gimple_code (def) == GIMPLE_PHI
1677 && gimple_bb (def)->loop_father->header == gimple_bb (def))
1678 return;
1681 /* Dump details. */
1682 if (dump_file && (dump_flags & TDF_DETAILS))
1684 fprintf (dump_file, " Replaced '");
1685 print_generic_expr (dump_file, op, dump_flags);
1686 fprintf (dump_file, "' with %s '",
1687 (TREE_CODE (val) != SSA_NAME ? "constant" : "variable"));
1688 print_generic_expr (dump_file, val, dump_flags);
1689 fprintf (dump_file, "'\n");
1692 if (TREE_CODE (val) != SSA_NAME)
1693 opt_stats.num_const_prop++;
1694 else
1695 opt_stats.num_copy_prop++;
1697 propagate_value (op_p, val);
1699 /* And note that we modified this statement. This is now
1700 safe, even if we changed virtual operands since we will
1701 rescan the statement and rewrite its operands again. */
1702 gimple_set_modified (stmt, true);
1706 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
1707 known value for that SSA_NAME (or NULL if no value is known).
1709 Propagate values from CONST_AND_COPIES into the uses, vuses and
1710 vdef_ops of STMT. */
1712 static void
1713 cprop_into_stmt (gimple *stmt)
1715 use_operand_p op_p;
1716 ssa_op_iter iter;
1718 FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_USE)
1719 cprop_operand (stmt, op_p);
1722 /* Optimize the statement in block BB pointed to by iterator SI
1723 using equivalences from CONST_AND_COPIES and AVAIL_EXPRS_STACK.
1725 We try to perform some simplistic global redundancy elimination and
1726 constant propagation:
1728 1- To detect global redundancy, we keep track of expressions that have
1729 been computed in this block and its dominators. If we find that the
1730 same expression is computed more than once, we eliminate repeated
1731 computations by using the target of the first one.
1733 2- Constant values and copy assignments. This is used to do very
1734 simplistic constant and copy propagation. When a constant or copy
1735 assignment is found, we map the value on the RHS of the assignment to
1736 the variable in the LHS in the CONST_AND_COPIES table. */
1738 static edge
1739 optimize_stmt (basic_block bb, gimple_stmt_iterator si,
1740 class const_and_copies *const_and_copies,
1741 class avail_exprs_stack *avail_exprs_stack)
1743 gimple *stmt, *old_stmt;
1744 bool may_optimize_p;
1745 bool modified_p = false;
1746 bool was_noreturn;
1747 edge retval = NULL;
1749 old_stmt = stmt = gsi_stmt (si);
1750 was_noreturn = is_gimple_call (stmt) && gimple_call_noreturn_p (stmt);
1752 if (dump_file && (dump_flags & TDF_DETAILS))
1754 fprintf (dump_file, "Optimizing statement ");
1755 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1758 if (gimple_code (stmt) == GIMPLE_COND)
1759 canonicalize_comparison (as_a <gcond *> (stmt));
1761 update_stmt_if_modified (stmt);
1762 opt_stats.num_stmts++;
1764 /* Const/copy propagate into USES, VUSES and the RHS of VDEFs. */
1765 cprop_into_stmt (stmt);
1767 /* If the statement has been modified with constant replacements,
1768 fold its RHS before checking for redundant computations. */
1769 if (gimple_modified_p (stmt))
1771 tree rhs = NULL;
1773 /* Try to fold the statement making sure that STMT is kept
1774 up to date. */
1775 if (fold_stmt (&si))
1777 stmt = gsi_stmt (si);
1778 gimple_set_modified (stmt, true);
1780 if (dump_file && (dump_flags & TDF_DETAILS))
1782 fprintf (dump_file, " Folded to: ");
1783 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1787 /* We only need to consider cases that can yield a gimple operand. */
1788 if (gimple_assign_single_p (stmt))
1789 rhs = gimple_assign_rhs1 (stmt);
1790 else if (gimple_code (stmt) == GIMPLE_GOTO)
1791 rhs = gimple_goto_dest (stmt);
1792 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
1793 /* This should never be an ADDR_EXPR. */
1794 rhs = gimple_switch_index (swtch_stmt);
1796 if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
1797 recompute_tree_invariant_for_addr_expr (rhs);
1799 /* Indicate that maybe_clean_or_replace_eh_stmt needs to be called,
1800 even if fold_stmt updated the stmt already and thus cleared
1801 gimple_modified_p flag on it. */
1802 modified_p = true;
1805 /* Check for redundant computations. Do this optimization only
1806 for assignments that have no volatile ops and conditionals. */
1807 may_optimize_p = (!gimple_has_side_effects (stmt)
1808 && (is_gimple_assign (stmt)
1809 || (is_gimple_call (stmt)
1810 && gimple_call_lhs (stmt) != NULL_TREE)
1811 || gimple_code (stmt) == GIMPLE_COND
1812 || gimple_code (stmt) == GIMPLE_SWITCH));
1814 if (may_optimize_p)
1816 if (gimple_code (stmt) == GIMPLE_CALL)
1818 /* Resolve __builtin_constant_p. If it hasn't been
1819 folded to integer_one_node by now, it's fairly
1820 certain that the value simply isn't constant. */
1821 tree callee = gimple_call_fndecl (stmt);
1822 if (callee
1823 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
1824 && DECL_FUNCTION_CODE (callee) == BUILT_IN_CONSTANT_P)
1826 propagate_tree_value_into_stmt (&si, integer_zero_node);
1827 stmt = gsi_stmt (si);
1831 update_stmt_if_modified (stmt);
1832 eliminate_redundant_computations (&si, const_and_copies,
1833 avail_exprs_stack);
1834 stmt = gsi_stmt (si);
1836 /* Perform simple redundant store elimination. */
1837 if (gimple_assign_single_p (stmt)
1838 && TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
1840 tree lhs = gimple_assign_lhs (stmt);
1841 tree rhs = gimple_assign_rhs1 (stmt);
1842 tree cached_lhs;
1843 gassign *new_stmt;
1844 rhs = dom_valueize (rhs);
1845 /* Build a new statement with the RHS and LHS exchanged. */
1846 if (TREE_CODE (rhs) == SSA_NAME)
1848 gimple *defstmt = SSA_NAME_DEF_STMT (rhs);
1849 new_stmt = gimple_build_assign (rhs, lhs);
1850 SSA_NAME_DEF_STMT (rhs) = defstmt;
1852 else
1853 new_stmt = gimple_build_assign (rhs, lhs);
1854 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
1855 cached_lhs = lookup_avail_expr (new_stmt, false, avail_exprs_stack);
1856 if (cached_lhs
1857 && rhs == cached_lhs)
1859 basic_block bb = gimple_bb (stmt);
1860 unlink_stmt_vdef (stmt);
1861 if (gsi_remove (&si, true))
1863 bitmap_set_bit (need_eh_cleanup, bb->index);
1864 if (dump_file && (dump_flags & TDF_DETAILS))
1865 fprintf (dump_file, " Flagged to clear EH edges.\n");
1867 release_defs (stmt);
1868 return retval;
1873 /* Record any additional equivalences created by this statement. */
1874 if (is_gimple_assign (stmt))
1875 record_equivalences_from_stmt (stmt, may_optimize_p, avail_exprs_stack);
1877 /* If STMT is a COND_EXPR or SWITCH_EXPR and it was modified, then we may
1878 know where it goes. */
1879 if (gimple_modified_p (stmt) || modified_p)
1881 tree val = NULL;
1883 update_stmt_if_modified (stmt);
1885 if (gimple_code (stmt) == GIMPLE_COND)
1886 val = fold_binary_loc (gimple_location (stmt),
1887 gimple_cond_code (stmt), boolean_type_node,
1888 gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
1889 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
1890 val = gimple_switch_index (swtch_stmt);
1892 if (val && TREE_CODE (val) == INTEGER_CST)
1894 retval = find_taken_edge (bb, val);
1895 if (retval)
1897 /* Fix the condition to be either true or false. */
1898 if (gimple_code (stmt) == GIMPLE_COND)
1900 if (integer_zerop (val))
1901 gimple_cond_make_false (as_a <gcond *> (stmt));
1902 else if (integer_onep (val))
1903 gimple_cond_make_true (as_a <gcond *> (stmt));
1904 else
1905 gcc_unreachable ();
1908 /* Further simplifications may be possible. */
1909 cfg_altered = true;
1913 /* If we simplified a statement in such a way as to be shown that it
1914 cannot trap, update the eh information and the cfg to match. */
1915 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
1917 bitmap_set_bit (need_eh_cleanup, bb->index);
1918 if (dump_file && (dump_flags & TDF_DETAILS))
1919 fprintf (dump_file, " Flagged to clear EH edges.\n");
1922 if (!was_noreturn
1923 && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
1924 need_noreturn_fixup.safe_push (stmt);
1926 return retval;
1929 /* Helper for walk_non_aliased_vuses. Determine if we arrived at
1930 the desired memory state. */
1932 static void *
1933 vuse_eq (ao_ref *, tree vuse1, unsigned int cnt, void *data)
1935 tree vuse2 = (tree) data;
1936 if (vuse1 == vuse2)
1937 return data;
1939 /* This bounds the stmt walks we perform on reference lookups
1940 to O(1) instead of O(N) where N is the number of dominating
1941 stores leading to a candidate. We re-use the SCCVN param
1942 for this as it is basically the same complexity. */
1943 if (cnt > (unsigned) PARAM_VALUE (PARAM_SCCVN_MAX_ALIAS_QUERIES_PER_ACCESS))
1944 return (void *)-1;
1946 return NULL;
1949 /* Search for an existing instance of STMT in the AVAIL_EXPRS_STACK table.
1950 If found, return its LHS. Otherwise insert STMT in the table and
1951 return NULL_TREE.
1953 Also, when an expression is first inserted in the table, it is also
1954 is also added to AVAIL_EXPRS_STACK, so that it can be removed when
1955 we finish processing this block and its children. */
1957 static tree
1958 lookup_avail_expr (gimple *stmt, bool insert,
1959 class avail_exprs_stack *avail_exprs_stack)
1961 expr_hash_elt **slot;
1962 tree lhs;
1964 /* Get LHS of phi, assignment, or call; else NULL_TREE. */
1965 if (gimple_code (stmt) == GIMPLE_PHI)
1966 lhs = gimple_phi_result (stmt);
1967 else
1968 lhs = gimple_get_lhs (stmt);
1970 class expr_hash_elt element (stmt, lhs);
1972 if (dump_file && (dump_flags & TDF_DETAILS))
1974 fprintf (dump_file, "LKUP ");
1975 element.print (dump_file);
1978 /* Don't bother remembering constant assignments and copy operations.
1979 Constants and copy operations are handled by the constant/copy propagator
1980 in optimize_stmt. */
1981 if (element.expr()->kind == EXPR_SINGLE
1982 && (TREE_CODE (element.expr()->ops.single.rhs) == SSA_NAME
1983 || is_gimple_min_invariant (element.expr()->ops.single.rhs)))
1984 return NULL_TREE;
1986 /* Finally try to find the expression in the main expression hash table. */
1987 hash_table<expr_elt_hasher> *avail_exprs = avail_exprs_stack->avail_exprs ();
1988 slot = avail_exprs->find_slot (&element, (insert ? INSERT : NO_INSERT));
1989 if (slot == NULL)
1991 return NULL_TREE;
1993 else if (*slot == NULL)
1995 class expr_hash_elt *element2 = new expr_hash_elt (element);
1996 *slot = element2;
1998 avail_exprs_stack->record_expr (element2, NULL, '2');
1999 return NULL_TREE;
2002 /* If we found a redundant memory operation do an alias walk to
2003 check if we can re-use it. */
2004 if (gimple_vuse (stmt) != (*slot)->vop ())
2006 tree vuse1 = (*slot)->vop ();
2007 tree vuse2 = gimple_vuse (stmt);
2008 /* If we have a load of a register and a candidate in the
2009 hash with vuse1 then try to reach its stmt by walking
2010 up the virtual use-def chain using walk_non_aliased_vuses.
2011 But don't do this when removing expressions from the hash. */
2012 ao_ref ref;
2013 if (!(vuse1 && vuse2
2014 && gimple_assign_single_p (stmt)
2015 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME
2016 && (ao_ref_init (&ref, gimple_assign_rhs1 (stmt)), true)
2017 && walk_non_aliased_vuses (&ref, vuse2,
2018 vuse_eq, NULL, NULL, vuse1) != NULL))
2020 if (insert)
2022 class expr_hash_elt *element2 = new expr_hash_elt (element);
2024 /* Insert the expr into the hash by replacing the current
2025 entry and recording the value to restore in the
2026 avail_exprs_stack. */
2027 avail_exprs_stack->record_expr (element2, *slot, '2');
2028 *slot = element2;
2030 return NULL_TREE;
2034 /* Extract the LHS of the assignment so that it can be used as the current
2035 definition of another variable. */
2036 lhs = (*slot)->lhs ();
2038 lhs = dom_valueize (lhs);
2040 if (dump_file && (dump_flags & TDF_DETAILS))
2042 fprintf (dump_file, "FIND: ");
2043 print_generic_expr (dump_file, lhs, 0);
2044 fprintf (dump_file, "\n");
2047 return lhs;