Make more use of paradoxical_subreg_p
[official-gcc.git] / gcc / tree-ssa-dom.c
blob407a4ef97d2591287cf8f04a1f4b6ad773de4794
1 /* SSA Dominator optimizations for trees
2 Copyright (C) 2001-2017 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 "tree-inline.h"
36 #include "gimple-iterator.h"
37 #include "tree-cfg.h"
38 #include "tree-into-ssa.h"
39 #include "domwalk.h"
40 #include "tree-ssa-propagate.h"
41 #include "tree-ssa-threadupdate.h"
42 #include "params.h"
43 #include "tree-ssa-scopedtables.h"
44 #include "tree-ssa-threadedge.h"
45 #include "tree-ssa-dom.h"
46 #include "gimplify.h"
47 #include "tree-cfgcleanup.h"
48 #include "dbgcnt.h"
50 /* This file implements optimizations on the dominator tree. */
52 /* Structure for recording edge equivalences.
54 Computing and storing the edge equivalences instead of creating
55 them on-demand can save significant amounts of time, particularly
56 for pathological cases involving switch statements.
58 These structures live for a single iteration of the dominator
59 optimizer in the edge's AUX field. At the end of an iteration we
60 free each of these structures. */
62 struct edge_info
64 /* If this edge creates a simple equivalence, the LHS and RHS of
65 the equivalence will be stored here. */
66 tree lhs;
67 tree rhs;
69 /* Traversing an edge may also indicate one or more particular conditions
70 are true or false. */
71 vec<cond_equivalence> cond_equivalences;
74 /* Track whether or not we have changed the control flow graph. */
75 static bool cfg_altered;
77 /* Bitmap of blocks that have had EH statements cleaned. We should
78 remove their dead edges eventually. */
79 static bitmap need_eh_cleanup;
80 static vec<gimple *> need_noreturn_fixup;
82 /* Statistics for dominator optimizations. */
83 struct opt_stats_d
85 long num_stmts;
86 long num_exprs_considered;
87 long num_re;
88 long num_const_prop;
89 long num_copy_prop;
92 static struct opt_stats_d opt_stats;
94 /* Local functions. */
95 static edge optimize_stmt (basic_block, gimple_stmt_iterator,
96 class const_and_copies *,
97 class avail_exprs_stack *);
98 static void record_equality (tree, tree, class const_and_copies *);
99 static void record_equivalences_from_phis (basic_block);
100 static void record_equivalences_from_incoming_edge (basic_block,
101 class const_and_copies *,
102 class avail_exprs_stack *);
103 static void eliminate_redundant_computations (gimple_stmt_iterator *,
104 class const_and_copies *,
105 class avail_exprs_stack *);
106 static void record_equivalences_from_stmt (gimple *, int,
107 class avail_exprs_stack *);
108 static edge single_incoming_edge_ignoring_loop_edges (basic_block);
109 static void dump_dominator_optimization_stats (FILE *file,
110 hash_table<expr_elt_hasher> *);
113 /* Free the edge_info data attached to E, if it exists. */
115 void
116 free_dom_edge_info (edge e)
118 struct edge_info *edge_info = (struct edge_info *)e->aux;
120 if (edge_info)
122 edge_info->cond_equivalences.release ();
123 free (edge_info);
127 /* Allocate an EDGE_INFO for edge E and attach it to E.
128 Return the new EDGE_INFO structure. */
130 static struct edge_info *
131 allocate_edge_info (edge e)
133 struct edge_info *edge_info;
135 /* Free the old one, if it exists. */
136 free_dom_edge_info (e);
138 edge_info = XCNEW (struct edge_info);
140 e->aux = edge_info;
141 return edge_info;
144 /* Free all EDGE_INFO structures associated with edges in the CFG.
145 If a particular edge can be threaded, copy the redirection
146 target from the EDGE_INFO structure into the edge's AUX field
147 as required by code to update the CFG and SSA graph for
148 jump threading. */
150 static void
151 free_all_edge_infos (void)
153 basic_block bb;
154 edge_iterator ei;
155 edge e;
157 FOR_EACH_BB_FN (bb, cfun)
159 FOR_EACH_EDGE (e, ei, bb->preds)
161 free_dom_edge_info (e);
162 e->aux = NULL;
167 /* We have finished optimizing BB, record any information implied by
168 taking a specific outgoing edge from BB. */
170 static void
171 record_edge_info (basic_block bb)
173 gimple_stmt_iterator gsi = gsi_last_bb (bb);
174 struct edge_info *edge_info;
176 if (! gsi_end_p (gsi))
178 gimple *stmt = gsi_stmt (gsi);
179 location_t loc = gimple_location (stmt);
181 if (gimple_code (stmt) == GIMPLE_SWITCH)
183 gswitch *switch_stmt = as_a <gswitch *> (stmt);
184 tree index = gimple_switch_index (switch_stmt);
186 if (TREE_CODE (index) == SSA_NAME)
188 int i;
189 int n_labels = gimple_switch_num_labels (switch_stmt);
190 tree *info = XCNEWVEC (tree, last_basic_block_for_fn (cfun));
191 edge e;
192 edge_iterator ei;
194 for (i = 0; i < n_labels; i++)
196 tree label = gimple_switch_label (switch_stmt, i);
197 basic_block target_bb = label_to_block (CASE_LABEL (label));
198 if (CASE_HIGH (label)
199 || !CASE_LOW (label)
200 || info[target_bb->index])
201 info[target_bb->index] = error_mark_node;
202 else
203 info[target_bb->index] = label;
206 FOR_EACH_EDGE (e, ei, bb->succs)
208 basic_block target_bb = e->dest;
209 tree label = info[target_bb->index];
211 if (label != NULL && label != error_mark_node)
213 tree x = fold_convert_loc (loc, TREE_TYPE (index),
214 CASE_LOW (label));
215 edge_info = allocate_edge_info (e);
216 edge_info->lhs = index;
217 edge_info->rhs = x;
220 free (info);
224 /* A COND_EXPR may create equivalences too. */
225 if (gimple_code (stmt) == GIMPLE_COND)
227 edge true_edge;
228 edge false_edge;
230 tree op0 = gimple_cond_lhs (stmt);
231 tree op1 = gimple_cond_rhs (stmt);
232 enum tree_code code = gimple_cond_code (stmt);
234 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
236 /* Special case comparing booleans against a constant as we
237 know the value of OP0 on both arms of the branch. i.e., we
238 can record an equivalence for OP0 rather than COND.
240 However, don't do this if the constant isn't zero or one.
241 Such conditionals will get optimized more thoroughly during
242 the domwalk. */
243 if ((code == EQ_EXPR || code == NE_EXPR)
244 && TREE_CODE (op0) == SSA_NAME
245 && ssa_name_has_boolean_range (op0)
246 && is_gimple_min_invariant (op1)
247 && (integer_zerop (op1) || integer_onep (op1)))
249 tree true_val = constant_boolean_node (true, TREE_TYPE (op0));
250 tree false_val = constant_boolean_node (false, TREE_TYPE (op0));
252 if (code == EQ_EXPR)
254 edge_info = allocate_edge_info (true_edge);
255 edge_info->lhs = op0;
256 edge_info->rhs = (integer_zerop (op1) ? false_val : true_val);
258 edge_info = allocate_edge_info (false_edge);
259 edge_info->lhs = op0;
260 edge_info->rhs = (integer_zerop (op1) ? true_val : false_val);
262 else
264 edge_info = allocate_edge_info (true_edge);
265 edge_info->lhs = op0;
266 edge_info->rhs = (integer_zerop (op1) ? true_val : false_val);
268 edge_info = allocate_edge_info (false_edge);
269 edge_info->lhs = op0;
270 edge_info->rhs = (integer_zerop (op1) ? false_val : true_val);
273 else if (is_gimple_min_invariant (op0)
274 && (TREE_CODE (op1) == SSA_NAME
275 || is_gimple_min_invariant (op1)))
277 tree cond = build2 (code, boolean_type_node, op0, op1);
278 tree inverted = invert_truthvalue_loc (loc, cond);
279 bool can_infer_simple_equiv
280 = !(HONOR_SIGNED_ZEROS (op0)
281 && real_zerop (op0));
282 struct edge_info *edge_info;
284 edge_info = allocate_edge_info (true_edge);
285 record_conditions (&edge_info->cond_equivalences, cond, inverted);
287 if (can_infer_simple_equiv && code == EQ_EXPR)
289 edge_info->lhs = op1;
290 edge_info->rhs = op0;
293 edge_info = allocate_edge_info (false_edge);
294 record_conditions (&edge_info->cond_equivalences, inverted, cond);
296 if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
298 edge_info->lhs = op1;
299 edge_info->rhs = op0;
303 else if (TREE_CODE (op0) == SSA_NAME
304 && (TREE_CODE (op1) == SSA_NAME
305 || is_gimple_min_invariant (op1)))
307 tree cond = build2 (code, boolean_type_node, op0, op1);
308 tree inverted = invert_truthvalue_loc (loc, cond);
309 bool can_infer_simple_equiv
310 = !(HONOR_SIGNED_ZEROS (op1)
311 && (TREE_CODE (op1) == SSA_NAME || real_zerop (op1)));
312 struct edge_info *edge_info;
314 edge_info = allocate_edge_info (true_edge);
315 record_conditions (&edge_info->cond_equivalences, cond, inverted);
317 if (can_infer_simple_equiv && code == EQ_EXPR)
319 edge_info->lhs = op0;
320 edge_info->rhs = op1;
323 edge_info = allocate_edge_info (false_edge);
324 record_conditions (&edge_info->cond_equivalences, inverted, cond);
326 if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
328 edge_info->lhs = op0;
329 edge_info->rhs = op1;
334 /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
339 class dom_opt_dom_walker : public dom_walker
341 public:
342 dom_opt_dom_walker (cdi_direction direction,
343 class const_and_copies *const_and_copies,
344 class avail_exprs_stack *avail_exprs_stack)
345 : dom_walker (direction, true),
346 m_const_and_copies (const_and_copies),
347 m_avail_exprs_stack (avail_exprs_stack),
348 m_dummy_cond (NULL) {}
350 virtual edge before_dom_children (basic_block);
351 virtual void after_dom_children (basic_block);
353 private:
355 /* Unwindable equivalences, both const/copy and expression varieties. */
356 class const_and_copies *m_const_and_copies;
357 class avail_exprs_stack *m_avail_exprs_stack;
359 gcond *m_dummy_cond;
362 /* Jump threading, redundancy elimination and const/copy propagation.
364 This pass may expose new symbols that need to be renamed into SSA. For
365 every new symbol exposed, its corresponding bit will be set in
366 VARS_TO_RENAME. */
368 namespace {
370 const pass_data pass_data_dominator =
372 GIMPLE_PASS, /* type */
373 "dom", /* name */
374 OPTGROUP_NONE, /* optinfo_flags */
375 TV_TREE_SSA_DOMINATOR_OPTS, /* tv_id */
376 ( PROP_cfg | PROP_ssa ), /* properties_required */
377 0, /* properties_provided */
378 0, /* properties_destroyed */
379 0, /* todo_flags_start */
380 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
383 class pass_dominator : public gimple_opt_pass
385 public:
386 pass_dominator (gcc::context *ctxt)
387 : gimple_opt_pass (pass_data_dominator, ctxt),
388 may_peel_loop_headers_p (false)
391 /* opt_pass methods: */
392 opt_pass * clone () { return new pass_dominator (m_ctxt); }
393 void set_pass_param (unsigned int n, bool param)
395 gcc_assert (n == 0);
396 may_peel_loop_headers_p = param;
398 virtual bool gate (function *) { return flag_tree_dom != 0; }
399 virtual unsigned int execute (function *);
401 private:
402 /* This flag is used to prevent loops from being peeled repeatedly in jump
403 threading; it will be removed once we preserve loop structures throughout
404 the compilation -- we will be able to mark the affected loops directly in
405 jump threading, and avoid peeling them next time. */
406 bool may_peel_loop_headers_p;
407 }; // class pass_dominator
409 unsigned int
410 pass_dominator::execute (function *fun)
412 memset (&opt_stats, 0, sizeof (opt_stats));
414 /* Create our hash tables. */
415 hash_table<expr_elt_hasher> *avail_exprs
416 = new hash_table<expr_elt_hasher> (1024);
417 class avail_exprs_stack *avail_exprs_stack
418 = new class avail_exprs_stack (avail_exprs);
419 class const_and_copies *const_and_copies = new class const_and_copies ();
420 need_eh_cleanup = BITMAP_ALLOC (NULL);
421 need_noreturn_fixup.create (0);
423 calculate_dominance_info (CDI_DOMINATORS);
424 cfg_altered = false;
426 /* We need to know loop structures in order to avoid destroying them
427 in jump threading. Note that we still can e.g. thread through loop
428 headers to an exit edge, or through loop header to the loop body, assuming
429 that we update the loop info.
431 TODO: We don't need to set LOOPS_HAVE_PREHEADERS generally, but due
432 to several overly conservative bail-outs in jump threading, case
433 gcc.dg/tree-ssa/pr21417.c can't be threaded if loop preheader is
434 missing. We should improve jump threading in future then
435 LOOPS_HAVE_PREHEADERS won't be needed here. */
436 loop_optimizer_init (LOOPS_HAVE_PREHEADERS | LOOPS_HAVE_SIMPLE_LATCHES);
438 /* Initialize the value-handle array. */
439 threadedge_initialize_values ();
441 /* We need accurate information regarding back edges in the CFG
442 for jump threading; this may include back edges that are not part of
443 a single loop. */
444 mark_dfs_back_edges ();
446 /* We want to create the edge info structures before the dominator walk
447 so that they'll be in place for the jump threader, particularly when
448 threading through a join block.
450 The conditions will be lazily updated with global equivalences as
451 we reach them during the dominator walk. */
452 basic_block bb;
453 FOR_EACH_BB_FN (bb, fun)
454 record_edge_info (bb);
456 /* Recursively walk the dominator tree optimizing statements. */
457 dom_opt_dom_walker walker (CDI_DOMINATORS,
458 const_and_copies,
459 avail_exprs_stack);
460 walker.walk (fun->cfg->x_entry_block_ptr);
462 /* Look for blocks where we cleared EDGE_EXECUTABLE on an outgoing
463 edge. When found, remove jump threads which contain any outgoing
464 edge from the affected block. */
465 if (cfg_altered)
467 FOR_EACH_BB_FN (bb, fun)
469 edge_iterator ei;
470 edge e;
472 /* First see if there are any edges without EDGE_EXECUTABLE
473 set. */
474 bool found = false;
475 FOR_EACH_EDGE (e, ei, bb->succs)
477 if ((e->flags & EDGE_EXECUTABLE) == 0)
479 found = true;
480 break;
484 /* If there were any such edges found, then remove jump threads
485 containing any edge leaving BB. */
486 if (found)
487 FOR_EACH_EDGE (e, ei, bb->succs)
488 remove_jump_threads_including (e);
493 gimple_stmt_iterator gsi;
494 basic_block bb;
495 FOR_EACH_BB_FN (bb, fun)
497 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
498 update_stmt_if_modified (gsi_stmt (gsi));
502 /* If we exposed any new variables, go ahead and put them into
503 SSA form now, before we handle jump threading. This simplifies
504 interactions between rewriting of _DECL nodes into SSA form
505 and rewriting SSA_NAME nodes into SSA form after block
506 duplication and CFG manipulation. */
507 update_ssa (TODO_update_ssa);
509 free_all_edge_infos ();
511 /* Thread jumps, creating duplicate blocks as needed. */
512 cfg_altered |= thread_through_all_blocks (may_peel_loop_headers_p);
514 if (cfg_altered)
515 free_dominance_info (CDI_DOMINATORS);
517 /* Removal of statements may make some EH edges dead. Purge
518 such edges from the CFG as needed. */
519 if (!bitmap_empty_p (need_eh_cleanup))
521 unsigned i;
522 bitmap_iterator bi;
524 /* Jump threading may have created forwarder blocks from blocks
525 needing EH cleanup; the new successor of these blocks, which
526 has inherited from the original block, needs the cleanup.
527 Don't clear bits in the bitmap, as that can break the bitmap
528 iterator. */
529 EXECUTE_IF_SET_IN_BITMAP (need_eh_cleanup, 0, i, bi)
531 basic_block bb = BASIC_BLOCK_FOR_FN (fun, i);
532 if (bb == NULL)
533 continue;
534 while (single_succ_p (bb)
535 && (single_succ_edge (bb)->flags & EDGE_EH) == 0)
536 bb = single_succ (bb);
537 if (bb == EXIT_BLOCK_PTR_FOR_FN (fun))
538 continue;
539 if ((unsigned) bb->index != i)
540 bitmap_set_bit (need_eh_cleanup, bb->index);
543 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
544 bitmap_clear (need_eh_cleanup);
547 /* Fixup stmts that became noreturn calls. This may require splitting
548 blocks and thus isn't possible during the dominator walk or before
549 jump threading finished. Do this in reverse order so we don't
550 inadvertedly remove a stmt we want to fixup by visiting a dominating
551 now noreturn call first. */
552 while (!need_noreturn_fixup.is_empty ())
554 gimple *stmt = need_noreturn_fixup.pop ();
555 if (dump_file && dump_flags & TDF_DETAILS)
557 fprintf (dump_file, "Fixing up noreturn call ");
558 print_gimple_stmt (dump_file, stmt, 0);
559 fprintf (dump_file, "\n");
561 fixup_noreturn_call (stmt);
564 statistics_counter_event (fun, "Redundant expressions eliminated",
565 opt_stats.num_re);
566 statistics_counter_event (fun, "Constants propagated",
567 opt_stats.num_const_prop);
568 statistics_counter_event (fun, "Copies propagated",
569 opt_stats.num_copy_prop);
571 /* Debugging dumps. */
572 if (dump_file && (dump_flags & TDF_STATS))
573 dump_dominator_optimization_stats (dump_file, avail_exprs);
575 loop_optimizer_finalize ();
577 /* Delete our main hashtable. */
578 delete avail_exprs;
579 avail_exprs = NULL;
581 /* Free asserted bitmaps and stacks. */
582 BITMAP_FREE (need_eh_cleanup);
583 need_noreturn_fixup.release ();
584 delete avail_exprs_stack;
585 delete const_and_copies;
587 /* Free the value-handle array. */
588 threadedge_finalize_values ();
590 return 0;
593 } // anon namespace
595 gimple_opt_pass *
596 make_pass_dominator (gcc::context *ctxt)
598 return new pass_dominator (ctxt);
602 /* A trivial wrapper so that we can present the generic jump
603 threading code with a simple API for simplifying statements. */
604 static tree
605 simplify_stmt_for_jump_threading (gimple *stmt,
606 gimple *within_stmt ATTRIBUTE_UNUSED,
607 class avail_exprs_stack *avail_exprs_stack,
608 basic_block bb ATTRIBUTE_UNUSED)
610 return avail_exprs_stack->lookup_avail_expr (stmt, false, true);
613 /* Valueize hook for gimple_fold_stmt_to_constant_1. */
615 static tree
616 dom_valueize (tree t)
618 if (TREE_CODE (t) == SSA_NAME)
620 tree tem = SSA_NAME_VALUE (t);
621 if (tem)
622 return tem;
624 return t;
627 /* We have just found an equivalence for LHS on an edge E.
628 Look backwards to other uses of LHS and see if we can derive
629 additional equivalences that are valid on edge E. */
630 static void
631 back_propagate_equivalences (tree lhs, edge e,
632 class const_and_copies *const_and_copies)
634 use_operand_p use_p;
635 imm_use_iterator iter;
636 bitmap domby = NULL;
637 basic_block dest = e->dest;
639 /* Iterate over the uses of LHS to see if any dominate E->dest.
640 If so, they may create useful equivalences too.
642 ??? If the code gets re-organized to a worklist to catch more
643 indirect opportunities and it is made to handle PHIs then this
644 should only consider use_stmts in basic-blocks we have already visited. */
645 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
647 gimple *use_stmt = USE_STMT (use_p);
649 /* Often the use is in DEST, which we trivially know we can't use.
650 This is cheaper than the dominator set tests below. */
651 if (dest == gimple_bb (use_stmt))
652 continue;
654 /* Filter out statements that can never produce a useful
655 equivalence. */
656 tree lhs2 = gimple_get_lhs (use_stmt);
657 if (!lhs2 || TREE_CODE (lhs2) != SSA_NAME)
658 continue;
660 /* Profiling has shown the domination tests here can be fairly
661 expensive. We get significant improvements by building the
662 set of blocks that dominate BB. We can then just test
663 for set membership below.
665 We also initialize the set lazily since often the only uses
666 are going to be in the same block as DEST. */
667 if (!domby)
669 domby = BITMAP_ALLOC (NULL);
670 basic_block bb = get_immediate_dominator (CDI_DOMINATORS, dest);
671 while (bb)
673 bitmap_set_bit (domby, bb->index);
674 bb = get_immediate_dominator (CDI_DOMINATORS, bb);
678 /* This tests if USE_STMT does not dominate DEST. */
679 if (!bitmap_bit_p (domby, gimple_bb (use_stmt)->index))
680 continue;
682 /* At this point USE_STMT dominates DEST and may result in a
683 useful equivalence. Try to simplify its RHS to a constant
684 or SSA_NAME. */
685 tree res = gimple_fold_stmt_to_constant_1 (use_stmt, dom_valueize,
686 no_follow_ssa_edges);
687 if (res && (TREE_CODE (res) == SSA_NAME || is_gimple_min_invariant (res)))
688 record_equality (lhs2, res, const_and_copies);
691 if (domby)
692 BITMAP_FREE (domby);
695 /* Record NAME has the value zero and if NAME was set from a BIT_IOR_EXPR
696 recurse into both operands recording their values as zero too.
697 RECURSION_DEPTH controls how far back we recurse through the operands
698 of the BIT_IOR_EXPR. */
700 static void
701 derive_equivalences_from_bit_ior (tree name,
702 const_and_copies *const_and_copies,
703 int recursion_limit)
705 if (recursion_limit == 0)
706 return;
708 if (TREE_CODE (name) == SSA_NAME)
710 tree value = build_zero_cst (TREE_TYPE (name));
712 /* This records the equivalence for the toplevel object. */
713 record_equality (name, value, const_and_copies);
715 /* And we can recurse into each operand to potentially find more
716 equivalences. */
717 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
718 if (is_gimple_assign (def_stmt)
719 && gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR)
721 derive_equivalences_from_bit_ior (gimple_assign_rhs1 (def_stmt),
722 const_and_copies,
723 recursion_limit - 1);
724 derive_equivalences_from_bit_ior (gimple_assign_rhs2 (def_stmt),
725 const_and_copies,
726 recursion_limit - 1);
731 /* Record into CONST_AND_COPIES and AVAIL_EXPRS_STACK any equivalences implied
732 by traversing edge E (which are cached in E->aux).
734 Callers are responsible for managing the unwinding markers. */
735 void
736 record_temporary_equivalences (edge e,
737 class const_and_copies *const_and_copies,
738 class avail_exprs_stack *avail_exprs_stack)
740 int i;
741 struct edge_info *edge_info = (struct edge_info *) e->aux;
743 /* If we have info associated with this edge, record it into
744 our equivalence tables. */
745 if (edge_info)
747 cond_equivalence *eq;
748 /* If we have 0 = COND or 1 = COND equivalences, record them
749 into our expression hash tables. */
750 for (i = 0; edge_info->cond_equivalences.iterate (i, &eq); ++i)
752 avail_exprs_stack->record_cond (eq);
754 /* If the condition is testing that X == 0 is true or X != 0 is false
755 and X is set from a BIT_IOR_EXPR, then we can record equivalences
756 for the operands of the BIT_IOR_EXPR (and recurse on those). */
757 tree op0 = eq->cond.ops.binary.opnd0;
758 tree op1 = eq->cond.ops.binary.opnd1;
759 if (TREE_CODE (op0) == SSA_NAME && integer_zerop (op1))
761 enum tree_code code = eq->cond.ops.binary.op;
762 if ((code == EQ_EXPR && eq->value == boolean_true_node)
763 || (code == NE_EXPR && eq->value == boolean_false_node))
764 derive_equivalences_from_bit_ior (op0, const_and_copies, 4);
766 /* TODO: We could handle BIT_AND_EXPR in a similar fashion
767 recording that the operands have a nonzero value. */
769 /* TODO: We can handle more cases here, particularly when OP0 is
770 known to have a boolean range. */
774 tree lhs = edge_info->lhs;
775 if (!lhs || TREE_CODE (lhs) != SSA_NAME)
776 return;
778 /* Record the simple NAME = VALUE equivalence. */
779 tree rhs = edge_info->rhs;
781 /* If this is a SSA_NAME = SSA_NAME equivalence and one operand is
782 cheaper to compute than the other, then set up the equivalence
783 such that we replace the expensive one with the cheap one.
785 If they are the same cost to compute, then do not record anything. */
786 if (TREE_CODE (lhs) == SSA_NAME && TREE_CODE (rhs) == SSA_NAME)
788 gimple *rhs_def = SSA_NAME_DEF_STMT (rhs);
789 int rhs_cost = estimate_num_insns (rhs_def, &eni_size_weights);
791 gimple *lhs_def = SSA_NAME_DEF_STMT (lhs);
792 int lhs_cost = estimate_num_insns (lhs_def, &eni_size_weights);
794 if (rhs_cost > lhs_cost)
795 record_equality (rhs, lhs, const_and_copies);
796 else if (rhs_cost < lhs_cost)
797 record_equality (lhs, rhs, const_and_copies);
799 else
800 record_equality (lhs, rhs, const_and_copies);
802 /* If LHS is an SSA_NAME and RHS is a constant integer and LHS was
803 set via a widening type conversion, then we may be able to record
804 additional equivalences. */
805 if (TREE_CODE (rhs) == INTEGER_CST)
807 gimple *defstmt = SSA_NAME_DEF_STMT (lhs);
809 if (defstmt
810 && is_gimple_assign (defstmt)
811 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (defstmt)))
813 tree old_rhs = gimple_assign_rhs1 (defstmt);
815 /* If the conversion widens the original value and
816 the constant is in the range of the type of OLD_RHS,
817 then convert the constant and record the equivalence.
819 Note that int_fits_type_p does not check the precision
820 if the upper and lower bounds are OK. */
821 if (INTEGRAL_TYPE_P (TREE_TYPE (old_rhs))
822 && (TYPE_PRECISION (TREE_TYPE (lhs))
823 > TYPE_PRECISION (TREE_TYPE (old_rhs)))
824 && int_fits_type_p (rhs, TREE_TYPE (old_rhs)))
826 tree newval = fold_convert (TREE_TYPE (old_rhs), rhs);
827 record_equality (old_rhs, newval, const_and_copies);
832 /* Any equivalence found for LHS may result in additional
833 equivalences for other uses of LHS that we have already
834 processed. */
835 back_propagate_equivalences (lhs, e, const_and_copies);
839 /* PHI nodes can create equivalences too.
841 Ignoring any alternatives which are the same as the result, if
842 all the alternatives are equal, then the PHI node creates an
843 equivalence. */
845 static void
846 record_equivalences_from_phis (basic_block bb)
848 gphi_iterator gsi;
850 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
852 gphi *phi = gsi.phi ();
854 tree lhs = gimple_phi_result (phi);
855 tree rhs = NULL;
856 size_t i;
858 for (i = 0; i < gimple_phi_num_args (phi); i++)
860 tree t = gimple_phi_arg_def (phi, i);
862 /* Ignore alternatives which are the same as our LHS. Since
863 LHS is a PHI_RESULT, it is known to be a SSA_NAME, so we
864 can simply compare pointers. */
865 if (lhs == t)
866 continue;
868 /* If the associated edge is not marked as executable, then it
869 can be ignored. */
870 if ((gimple_phi_arg_edge (phi, i)->flags & EDGE_EXECUTABLE) == 0)
871 continue;
873 t = dom_valueize (t);
875 /* If we have not processed an alternative yet, then set
876 RHS to this alternative. */
877 if (rhs == NULL)
878 rhs = t;
879 /* If we have processed an alternative (stored in RHS), then
880 see if it is equal to this one. If it isn't, then stop
881 the search. */
882 else if (! operand_equal_for_phi_arg_p (rhs, t))
883 break;
886 /* If we had no interesting alternatives, then all the RHS alternatives
887 must have been the same as LHS. */
888 if (!rhs)
889 rhs = lhs;
891 /* If we managed to iterate through each PHI alternative without
892 breaking out of the loop, then we have a PHI which may create
893 a useful equivalence. We do not need to record unwind data for
894 this, since this is a true assignment and not an equivalence
895 inferred from a comparison. All uses of this ssa name are dominated
896 by this assignment, so unwinding just costs time and space. */
897 if (i == gimple_phi_num_args (phi)
898 && may_propagate_copy (lhs, rhs))
899 set_ssa_name_value (lhs, rhs);
903 /* Ignoring loop backedges, if BB has precisely one incoming edge then
904 return that edge. Otherwise return NULL. */
905 static edge
906 single_incoming_edge_ignoring_loop_edges (basic_block bb)
908 edge retval = NULL;
909 edge e;
910 edge_iterator ei;
912 FOR_EACH_EDGE (e, ei, bb->preds)
914 /* A loop back edge can be identified by the destination of
915 the edge dominating the source of the edge. */
916 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
917 continue;
919 /* We can safely ignore edges that are not executable. */
920 if ((e->flags & EDGE_EXECUTABLE) == 0)
921 continue;
923 /* If we have already seen a non-loop edge, then we must have
924 multiple incoming non-loop edges and thus we return NULL. */
925 if (retval)
926 return NULL;
928 /* This is the first non-loop incoming edge we have found. Record
929 it. */
930 retval = e;
933 return retval;
936 /* Record any equivalences created by the incoming edge to BB into
937 CONST_AND_COPIES and AVAIL_EXPRS_STACK. If BB has more than one
938 incoming edge, then no equivalence is created. */
940 static void
941 record_equivalences_from_incoming_edge (basic_block bb,
942 class const_and_copies *const_and_copies,
943 class avail_exprs_stack *avail_exprs_stack)
945 edge e;
946 basic_block parent;
948 /* If our parent block ended with a control statement, then we may be
949 able to record some equivalences based on which outgoing edge from
950 the parent was followed. */
951 parent = get_immediate_dominator (CDI_DOMINATORS, bb);
953 e = single_incoming_edge_ignoring_loop_edges (bb);
955 /* If we had a single incoming edge from our parent block, then enter
956 any data associated with the edge into our tables. */
957 if (e && e->src == parent)
958 record_temporary_equivalences (e, const_and_copies, avail_exprs_stack);
961 /* Dump statistics for the hash table HTAB. */
963 static void
964 htab_statistics (FILE *file, const hash_table<expr_elt_hasher> &htab)
966 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
967 (long) htab.size (),
968 (long) htab.elements (),
969 htab.collisions ());
972 /* Dump SSA statistics on FILE. */
974 static void
975 dump_dominator_optimization_stats (FILE *file,
976 hash_table<expr_elt_hasher> *avail_exprs)
978 fprintf (file, "Total number of statements: %6ld\n\n",
979 opt_stats.num_stmts);
980 fprintf (file, "Exprs considered for dominator optimizations: %6ld\n",
981 opt_stats.num_exprs_considered);
983 fprintf (file, "\nHash table statistics:\n");
985 fprintf (file, " avail_exprs: ");
986 htab_statistics (file, *avail_exprs);
990 /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
991 This constrains the cases in which we may treat this as assignment. */
993 static void
994 record_equality (tree x, tree y, class const_and_copies *const_and_copies)
996 tree prev_x = NULL, prev_y = NULL;
998 if (tree_swap_operands_p (x, y))
999 std::swap (x, y);
1001 /* Most of the time tree_swap_operands_p does what we want. But there
1002 are cases where we know one operand is better for copy propagation than
1003 the other. Given no other code cares about ordering of equality
1004 comparison operators for that purpose, we just handle the special cases
1005 here. */
1006 if (TREE_CODE (x) == SSA_NAME && TREE_CODE (y) == SSA_NAME)
1008 /* If one operand is a single use operand, then make it
1009 X. This will preserve its single use properly and if this
1010 conditional is eliminated, the computation of X can be
1011 eliminated as well. */
1012 if (has_single_use (y) && ! has_single_use (x))
1013 std::swap (x, y);
1015 if (TREE_CODE (x) == SSA_NAME)
1016 prev_x = SSA_NAME_VALUE (x);
1017 if (TREE_CODE (y) == SSA_NAME)
1018 prev_y = SSA_NAME_VALUE (y);
1020 /* If one of the previous values is invariant, or invariant in more loops
1021 (by depth), then use that.
1022 Otherwise it doesn't matter which value we choose, just so
1023 long as we canonicalize on one value. */
1024 if (is_gimple_min_invariant (y))
1026 else if (is_gimple_min_invariant (x))
1027 prev_x = x, x = y, y = prev_x, prev_x = prev_y;
1028 else if (prev_x && is_gimple_min_invariant (prev_x))
1029 x = y, y = prev_x, prev_x = prev_y;
1030 else if (prev_y)
1031 y = prev_y;
1033 /* After the swapping, we must have one SSA_NAME. */
1034 if (TREE_CODE (x) != SSA_NAME)
1035 return;
1037 /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1038 variable compared against zero. If we're honoring signed zeros,
1039 then we cannot record this value unless we know that the value is
1040 nonzero. */
1041 if (HONOR_SIGNED_ZEROS (x)
1042 && (TREE_CODE (y) != REAL_CST
1043 || real_equal (&dconst0, &TREE_REAL_CST (y))))
1044 return;
1046 const_and_copies->record_const_or_copy (x, y, prev_x);
1049 /* Returns true when STMT is a simple iv increment. It detects the
1050 following situation:
1052 i_1 = phi (..., i_2)
1053 i_2 = i_1 +/- ... */
1055 bool
1056 simple_iv_increment_p (gimple *stmt)
1058 enum tree_code code;
1059 tree lhs, preinc;
1060 gimple *phi;
1061 size_t i;
1063 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1064 return false;
1066 lhs = gimple_assign_lhs (stmt);
1067 if (TREE_CODE (lhs) != SSA_NAME)
1068 return false;
1070 code = gimple_assign_rhs_code (stmt);
1071 if (code != PLUS_EXPR
1072 && code != MINUS_EXPR
1073 && code != POINTER_PLUS_EXPR)
1074 return false;
1076 preinc = gimple_assign_rhs1 (stmt);
1077 if (TREE_CODE (preinc) != SSA_NAME)
1078 return false;
1080 phi = SSA_NAME_DEF_STMT (preinc);
1081 if (gimple_code (phi) != GIMPLE_PHI)
1082 return false;
1084 for (i = 0; i < gimple_phi_num_args (phi); i++)
1085 if (gimple_phi_arg_def (phi, i) == lhs)
1086 return true;
1088 return false;
1091 /* Propagate know values from SSA_NAME_VALUE into the PHI nodes of the
1092 successors of BB. */
1094 static void
1095 cprop_into_successor_phis (basic_block bb,
1096 class const_and_copies *const_and_copies)
1098 edge e;
1099 edge_iterator ei;
1101 FOR_EACH_EDGE (e, ei, bb->succs)
1103 int indx;
1104 gphi_iterator gsi;
1106 /* If this is an abnormal edge, then we do not want to copy propagate
1107 into the PHI alternative associated with this edge. */
1108 if (e->flags & EDGE_ABNORMAL)
1109 continue;
1111 gsi = gsi_start_phis (e->dest);
1112 if (gsi_end_p (gsi))
1113 continue;
1115 /* We may have an equivalence associated with this edge. While
1116 we can not propagate it into non-dominated blocks, we can
1117 propagate them into PHIs in non-dominated blocks. */
1119 /* Push the unwind marker so we can reset the const and copies
1120 table back to its original state after processing this edge. */
1121 const_and_copies->push_marker ();
1123 /* Extract and record any simple NAME = VALUE equivalences.
1125 Don't bother with [01] = COND equivalences, they're not useful
1126 here. */
1127 struct edge_info *edge_info = (struct edge_info *) e->aux;
1128 if (edge_info)
1130 tree lhs = edge_info->lhs;
1131 tree rhs = edge_info->rhs;
1133 if (lhs && TREE_CODE (lhs) == SSA_NAME)
1134 const_and_copies->record_const_or_copy (lhs, rhs);
1137 indx = e->dest_idx;
1138 for ( ; !gsi_end_p (gsi); gsi_next (&gsi))
1140 tree new_val;
1141 use_operand_p orig_p;
1142 tree orig_val;
1143 gphi *phi = gsi.phi ();
1145 /* The alternative may be associated with a constant, so verify
1146 it is an SSA_NAME before doing anything with it. */
1147 orig_p = gimple_phi_arg_imm_use_ptr (phi, indx);
1148 orig_val = get_use_from_ptr (orig_p);
1149 if (TREE_CODE (orig_val) != SSA_NAME)
1150 continue;
1152 /* If we have *ORIG_P in our constant/copy table, then replace
1153 ORIG_P with its value in our constant/copy table. */
1154 new_val = SSA_NAME_VALUE (orig_val);
1155 if (new_val
1156 && new_val != orig_val
1157 && may_propagate_copy (orig_val, new_val))
1158 propagate_value (orig_p, new_val);
1161 const_and_copies->pop_to_marker ();
1165 edge
1166 dom_opt_dom_walker::before_dom_children (basic_block bb)
1168 gimple_stmt_iterator gsi;
1170 if (dump_file && (dump_flags & TDF_DETAILS))
1171 fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
1173 /* Push a marker on the stacks of local information so that we know how
1174 far to unwind when we finalize this block. */
1175 m_avail_exprs_stack->push_marker ();
1176 m_const_and_copies->push_marker ();
1178 record_equivalences_from_incoming_edge (bb, m_const_and_copies,
1179 m_avail_exprs_stack);
1181 /* PHI nodes can create equivalences too. */
1182 record_equivalences_from_phis (bb);
1184 /* Create equivalences from redundant PHIs. PHIs are only truly
1185 redundant when they exist in the same block, so push another
1186 marker and unwind right afterwards. */
1187 m_avail_exprs_stack->push_marker ();
1188 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1189 eliminate_redundant_computations (&gsi, m_const_and_copies,
1190 m_avail_exprs_stack);
1191 m_avail_exprs_stack->pop_to_marker ();
1193 edge taken_edge = NULL;
1194 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1195 taken_edge
1196 = optimize_stmt (bb, gsi, m_const_and_copies, m_avail_exprs_stack);
1198 /* Now prepare to process dominated blocks. */
1199 record_edge_info (bb);
1200 cprop_into_successor_phis (bb, m_const_and_copies);
1201 if (taken_edge && !dbg_cnt (dom_unreachable_edges))
1202 return NULL;
1204 return taken_edge;
1207 /* We have finished processing the dominator children of BB, perform
1208 any finalization actions in preparation for leaving this node in
1209 the dominator tree. */
1211 void
1212 dom_opt_dom_walker::after_dom_children (basic_block bb)
1214 if (! m_dummy_cond)
1215 m_dummy_cond = gimple_build_cond (NE_EXPR, integer_zero_node,
1216 integer_zero_node, NULL, NULL);
1218 thread_outgoing_edges (bb, m_dummy_cond, m_const_and_copies,
1219 m_avail_exprs_stack,
1220 simplify_stmt_for_jump_threading);
1222 /* These remove expressions local to BB from the tables. */
1223 m_avail_exprs_stack->pop_to_marker ();
1224 m_const_and_copies->pop_to_marker ();
1227 /* Search for redundant computations in STMT. If any are found, then
1228 replace them with the variable holding the result of the computation.
1230 If safe, record this expression into AVAIL_EXPRS_STACK and
1231 CONST_AND_COPIES. */
1233 static void
1234 eliminate_redundant_computations (gimple_stmt_iterator* gsi,
1235 class const_and_copies *const_and_copies,
1236 class avail_exprs_stack *avail_exprs_stack)
1238 tree expr_type;
1239 tree cached_lhs;
1240 tree def;
1241 bool insert = true;
1242 bool assigns_var_p = false;
1244 gimple *stmt = gsi_stmt (*gsi);
1246 if (gimple_code (stmt) == GIMPLE_PHI)
1247 def = gimple_phi_result (stmt);
1248 else
1249 def = gimple_get_lhs (stmt);
1251 /* Certain expressions on the RHS can be optimized away, but can not
1252 themselves be entered into the hash tables. */
1253 if (! def
1254 || TREE_CODE (def) != SSA_NAME
1255 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)
1256 || gimple_vdef (stmt)
1257 /* Do not record equivalences for increments of ivs. This would create
1258 overlapping live ranges for a very questionable gain. */
1259 || simple_iv_increment_p (stmt))
1260 insert = false;
1262 /* Check if the expression has been computed before. */
1263 cached_lhs = avail_exprs_stack->lookup_avail_expr (stmt, insert, true);
1265 opt_stats.num_exprs_considered++;
1267 /* Get the type of the expression we are trying to optimize. */
1268 if (is_gimple_assign (stmt))
1270 expr_type = TREE_TYPE (gimple_assign_lhs (stmt));
1271 assigns_var_p = true;
1273 else if (gimple_code (stmt) == GIMPLE_COND)
1274 expr_type = boolean_type_node;
1275 else if (is_gimple_call (stmt))
1277 gcc_assert (gimple_call_lhs (stmt));
1278 expr_type = TREE_TYPE (gimple_call_lhs (stmt));
1279 assigns_var_p = true;
1281 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
1282 expr_type = TREE_TYPE (gimple_switch_index (swtch_stmt));
1283 else if (gimple_code (stmt) == GIMPLE_PHI)
1284 /* We can't propagate into a phi, so the logic below doesn't apply.
1285 Instead record an equivalence between the cached LHS and the
1286 PHI result of this statement, provided they are in the same block.
1287 This should be sufficient to kill the redundant phi. */
1289 if (def && cached_lhs)
1290 const_and_copies->record_const_or_copy (def, cached_lhs);
1291 return;
1293 else
1294 gcc_unreachable ();
1296 if (!cached_lhs)
1297 return;
1299 /* It is safe to ignore types here since we have already done
1300 type checking in the hashing and equality routines. In fact
1301 type checking here merely gets in the way of constant
1302 propagation. Also, make sure that it is safe to propagate
1303 CACHED_LHS into the expression in STMT. */
1304 if ((TREE_CODE (cached_lhs) != SSA_NAME
1305 && (assigns_var_p
1306 || useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs))))
1307 || may_propagate_copy_into_stmt (stmt, cached_lhs))
1309 gcc_checking_assert (TREE_CODE (cached_lhs) == SSA_NAME
1310 || is_gimple_min_invariant (cached_lhs));
1312 if (dump_file && (dump_flags & TDF_DETAILS))
1314 fprintf (dump_file, " Replaced redundant expr '");
1315 print_gimple_expr (dump_file, stmt, 0, dump_flags);
1316 fprintf (dump_file, "' with '");
1317 print_generic_expr (dump_file, cached_lhs, dump_flags);
1318 fprintf (dump_file, "'\n");
1321 opt_stats.num_re++;
1323 if (assigns_var_p
1324 && !useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs)))
1325 cached_lhs = fold_convert (expr_type, cached_lhs);
1327 propagate_tree_value_into_stmt (gsi, cached_lhs);
1329 /* Since it is always necessary to mark the result as modified,
1330 perhaps we should move this into propagate_tree_value_into_stmt
1331 itself. */
1332 gimple_set_modified (gsi_stmt (*gsi), true);
1336 /* STMT, a GIMPLE_ASSIGN, may create certain equivalences, in either
1337 the available expressions table or the const_and_copies table.
1338 Detect and record those equivalences into AVAIL_EXPRS_STACK.
1340 We handle only very simple copy equivalences here. The heavy
1341 lifing is done by eliminate_redundant_computations. */
1343 static void
1344 record_equivalences_from_stmt (gimple *stmt, int may_optimize_p,
1345 class avail_exprs_stack *avail_exprs_stack)
1347 tree lhs;
1348 enum tree_code lhs_code;
1350 gcc_assert (is_gimple_assign (stmt));
1352 lhs = gimple_assign_lhs (stmt);
1353 lhs_code = TREE_CODE (lhs);
1355 if (lhs_code == SSA_NAME
1356 && gimple_assign_single_p (stmt))
1358 tree rhs = gimple_assign_rhs1 (stmt);
1360 /* If the RHS of the assignment is a constant or another variable that
1361 may be propagated, register it in the CONST_AND_COPIES table. We
1362 do not need to record unwind data for this, since this is a true
1363 assignment and not an equivalence inferred from a comparison. All
1364 uses of this ssa name are dominated by this assignment, so unwinding
1365 just costs time and space. */
1366 if (may_optimize_p
1367 && (TREE_CODE (rhs) == SSA_NAME
1368 || is_gimple_min_invariant (rhs)))
1370 rhs = dom_valueize (rhs);
1372 if (dump_file && (dump_flags & TDF_DETAILS))
1374 fprintf (dump_file, "==== ASGN ");
1375 print_generic_expr (dump_file, lhs);
1376 fprintf (dump_file, " = ");
1377 print_generic_expr (dump_file, rhs);
1378 fprintf (dump_file, "\n");
1381 set_ssa_name_value (lhs, rhs);
1385 /* Make sure we can propagate &x + CST. */
1386 if (lhs_code == SSA_NAME
1387 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
1388 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR
1389 && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)
1391 tree op0 = gimple_assign_rhs1 (stmt);
1392 tree op1 = gimple_assign_rhs2 (stmt);
1393 tree new_rhs
1394 = build_fold_addr_expr (fold_build2 (MEM_REF,
1395 TREE_TYPE (TREE_TYPE (op0)),
1396 unshare_expr (op0),
1397 fold_convert (ptr_type_node,
1398 op1)));
1399 if (dump_file && (dump_flags & TDF_DETAILS))
1401 fprintf (dump_file, "==== ASGN ");
1402 print_generic_expr (dump_file, lhs);
1403 fprintf (dump_file, " = ");
1404 print_generic_expr (dump_file, new_rhs);
1405 fprintf (dump_file, "\n");
1408 set_ssa_name_value (lhs, new_rhs);
1411 /* A memory store, even an aliased store, creates a useful
1412 equivalence. By exchanging the LHS and RHS, creating suitable
1413 vops and recording the result in the available expression table,
1414 we may be able to expose more redundant loads. */
1415 if (!gimple_has_volatile_ops (stmt)
1416 && gimple_references_memory_p (stmt)
1417 && gimple_assign_single_p (stmt)
1418 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
1419 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
1420 && !is_gimple_reg (lhs))
1422 tree rhs = gimple_assign_rhs1 (stmt);
1423 gassign *new_stmt;
1425 /* Build a new statement with the RHS and LHS exchanged. */
1426 if (TREE_CODE (rhs) == SSA_NAME)
1428 /* NOTE tuples. The call to gimple_build_assign below replaced
1429 a call to build_gimple_modify_stmt, which did not set the
1430 SSA_NAME_DEF_STMT on the LHS of the assignment. Doing so
1431 may cause an SSA validation failure, as the LHS may be a
1432 default-initialized name and should have no definition. I'm
1433 a bit dubious of this, as the artificial statement that we
1434 generate here may in fact be ill-formed, but it is simply
1435 used as an internal device in this pass, and never becomes
1436 part of the CFG. */
1437 gimple *defstmt = SSA_NAME_DEF_STMT (rhs);
1438 new_stmt = gimple_build_assign (rhs, lhs);
1439 SSA_NAME_DEF_STMT (rhs) = defstmt;
1441 else
1442 new_stmt = gimple_build_assign (rhs, lhs);
1444 gimple_set_vuse (new_stmt, gimple_vdef (stmt));
1446 /* Finally enter the statement into the available expression
1447 table. */
1448 avail_exprs_stack->lookup_avail_expr (new_stmt, true, true);
1452 /* Replace *OP_P in STMT with any known equivalent value for *OP_P from
1453 CONST_AND_COPIES. */
1455 static void
1456 cprop_operand (gimple *stmt, use_operand_p op_p)
1458 tree val;
1459 tree op = USE_FROM_PTR (op_p);
1461 /* If the operand has a known constant value or it is known to be a
1462 copy of some other variable, use the value or copy stored in
1463 CONST_AND_COPIES. */
1464 val = SSA_NAME_VALUE (op);
1465 if (val && val != op)
1467 /* Do not replace hard register operands in asm statements. */
1468 if (gimple_code (stmt) == GIMPLE_ASM
1469 && !may_propagate_copy_into_asm (op))
1470 return;
1472 /* Certain operands are not allowed to be copy propagated due
1473 to their interaction with exception handling and some GCC
1474 extensions. */
1475 if (!may_propagate_copy (op, val))
1476 return;
1478 /* Do not propagate copies into BIVs.
1479 See PR23821 and PR62217 for how this can disturb IV and
1480 number of iteration analysis. */
1481 if (TREE_CODE (val) != INTEGER_CST)
1483 gimple *def = SSA_NAME_DEF_STMT (op);
1484 if (gimple_code (def) == GIMPLE_PHI
1485 && gimple_bb (def)->loop_father->header == gimple_bb (def))
1486 return;
1489 /* Dump details. */
1490 if (dump_file && (dump_flags & TDF_DETAILS))
1492 fprintf (dump_file, " Replaced '");
1493 print_generic_expr (dump_file, op, dump_flags);
1494 fprintf (dump_file, "' with %s '",
1495 (TREE_CODE (val) != SSA_NAME ? "constant" : "variable"));
1496 print_generic_expr (dump_file, val, dump_flags);
1497 fprintf (dump_file, "'\n");
1500 if (TREE_CODE (val) != SSA_NAME)
1501 opt_stats.num_const_prop++;
1502 else
1503 opt_stats.num_copy_prop++;
1505 propagate_value (op_p, val);
1507 /* And note that we modified this statement. This is now
1508 safe, even if we changed virtual operands since we will
1509 rescan the statement and rewrite its operands again. */
1510 gimple_set_modified (stmt, true);
1514 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
1515 known value for that SSA_NAME (or NULL if no value is known).
1517 Propagate values from CONST_AND_COPIES into the uses, vuses and
1518 vdef_ops of STMT. */
1520 static void
1521 cprop_into_stmt (gimple *stmt)
1523 use_operand_p op_p;
1524 ssa_op_iter iter;
1525 tree last_copy_propagated_op = NULL;
1527 FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_USE)
1529 tree old_op = USE_FROM_PTR (op_p);
1531 /* If we have A = B and B = A in the copy propagation tables
1532 (due to an equality comparison), avoid substituting B for A
1533 then A for B in the trivially discovered cases. This allows
1534 optimization of statements were A and B appear as input
1535 operands. */
1536 if (old_op != last_copy_propagated_op)
1538 cprop_operand (stmt, op_p);
1540 tree new_op = USE_FROM_PTR (op_p);
1541 if (new_op != old_op && TREE_CODE (new_op) == SSA_NAME)
1542 last_copy_propagated_op = new_op;
1547 /* Optimize the statement in block BB pointed to by iterator SI
1548 using equivalences from CONST_AND_COPIES and AVAIL_EXPRS_STACK.
1550 We try to perform some simplistic global redundancy elimination and
1551 constant propagation:
1553 1- To detect global redundancy, we keep track of expressions that have
1554 been computed in this block and its dominators. If we find that the
1555 same expression is computed more than once, we eliminate repeated
1556 computations by using the target of the first one.
1558 2- Constant values and copy assignments. This is used to do very
1559 simplistic constant and copy propagation. When a constant or copy
1560 assignment is found, we map the value on the RHS of the assignment to
1561 the variable in the LHS in the CONST_AND_COPIES table. */
1563 static edge
1564 optimize_stmt (basic_block bb, gimple_stmt_iterator si,
1565 class const_and_copies *const_and_copies,
1566 class avail_exprs_stack *avail_exprs_stack)
1568 gimple *stmt, *old_stmt;
1569 bool may_optimize_p;
1570 bool modified_p = false;
1571 bool was_noreturn;
1572 edge retval = NULL;
1574 old_stmt = stmt = gsi_stmt (si);
1575 was_noreturn = is_gimple_call (stmt) && gimple_call_noreturn_p (stmt);
1577 if (dump_file && (dump_flags & TDF_DETAILS))
1579 fprintf (dump_file, "Optimizing statement ");
1580 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1583 update_stmt_if_modified (stmt);
1584 opt_stats.num_stmts++;
1586 /* Const/copy propagate into USES, VUSES and the RHS of VDEFs. */
1587 cprop_into_stmt (stmt);
1589 /* If the statement has been modified with constant replacements,
1590 fold its RHS before checking for redundant computations. */
1591 if (gimple_modified_p (stmt))
1593 tree rhs = NULL;
1595 /* Try to fold the statement making sure that STMT is kept
1596 up to date. */
1597 if (fold_stmt (&si))
1599 stmt = gsi_stmt (si);
1600 gimple_set_modified (stmt, true);
1602 if (dump_file && (dump_flags & TDF_DETAILS))
1604 fprintf (dump_file, " Folded to: ");
1605 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1609 /* We only need to consider cases that can yield a gimple operand. */
1610 if (gimple_assign_single_p (stmt))
1611 rhs = gimple_assign_rhs1 (stmt);
1612 else if (gimple_code (stmt) == GIMPLE_GOTO)
1613 rhs = gimple_goto_dest (stmt);
1614 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
1615 /* This should never be an ADDR_EXPR. */
1616 rhs = gimple_switch_index (swtch_stmt);
1618 if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
1619 recompute_tree_invariant_for_addr_expr (rhs);
1621 /* Indicate that maybe_clean_or_replace_eh_stmt needs to be called,
1622 even if fold_stmt updated the stmt already and thus cleared
1623 gimple_modified_p flag on it. */
1624 modified_p = true;
1627 /* Check for redundant computations. Do this optimization only
1628 for assignments that have no volatile ops and conditionals. */
1629 may_optimize_p = (!gimple_has_side_effects (stmt)
1630 && (is_gimple_assign (stmt)
1631 || (is_gimple_call (stmt)
1632 && gimple_call_lhs (stmt) != NULL_TREE)
1633 || gimple_code (stmt) == GIMPLE_COND
1634 || gimple_code (stmt) == GIMPLE_SWITCH));
1636 if (may_optimize_p)
1638 if (gimple_code (stmt) == GIMPLE_CALL)
1640 /* Resolve __builtin_constant_p. If it hasn't been
1641 folded to integer_one_node by now, it's fairly
1642 certain that the value simply isn't constant. */
1643 tree callee = gimple_call_fndecl (stmt);
1644 if (callee
1645 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
1646 && DECL_FUNCTION_CODE (callee) == BUILT_IN_CONSTANT_P)
1648 propagate_tree_value_into_stmt (&si, integer_zero_node);
1649 stmt = gsi_stmt (si);
1653 if (gimple_code (stmt) == GIMPLE_COND)
1655 tree lhs = gimple_cond_lhs (stmt);
1656 tree rhs = gimple_cond_rhs (stmt);
1658 /* If the LHS has a range [0..1] and the RHS has a range ~[0..1],
1659 then this conditional is computable at compile time. We can just
1660 shove either 0 or 1 into the LHS, mark the statement as modified
1661 and all the right things will just happen below.
1663 Note this would apply to any case where LHS has a range
1664 narrower than its type implies and RHS is outside that
1665 narrower range. Future work. */
1666 if (TREE_CODE (lhs) == SSA_NAME
1667 && ssa_name_has_boolean_range (lhs)
1668 && TREE_CODE (rhs) == INTEGER_CST
1669 && ! (integer_zerop (rhs) || integer_onep (rhs)))
1671 gimple_cond_set_lhs (as_a <gcond *> (stmt),
1672 fold_convert (TREE_TYPE (lhs),
1673 integer_zero_node));
1674 gimple_set_modified (stmt, true);
1678 update_stmt_if_modified (stmt);
1679 eliminate_redundant_computations (&si, const_and_copies,
1680 avail_exprs_stack);
1681 stmt = gsi_stmt (si);
1683 /* Perform simple redundant store elimination. */
1684 if (gimple_assign_single_p (stmt)
1685 && TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
1687 tree lhs = gimple_assign_lhs (stmt);
1688 tree rhs = gimple_assign_rhs1 (stmt);
1689 tree cached_lhs;
1690 gassign *new_stmt;
1691 rhs = dom_valueize (rhs);
1692 /* Build a new statement with the RHS and LHS exchanged. */
1693 if (TREE_CODE (rhs) == SSA_NAME)
1695 gimple *defstmt = SSA_NAME_DEF_STMT (rhs);
1696 new_stmt = gimple_build_assign (rhs, lhs);
1697 SSA_NAME_DEF_STMT (rhs) = defstmt;
1699 else
1700 new_stmt = gimple_build_assign (rhs, lhs);
1701 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
1702 cached_lhs = avail_exprs_stack->lookup_avail_expr (new_stmt, false,
1703 false);
1704 if (cached_lhs
1705 && rhs == cached_lhs)
1707 basic_block bb = gimple_bb (stmt);
1708 unlink_stmt_vdef (stmt);
1709 if (gsi_remove (&si, true))
1711 bitmap_set_bit (need_eh_cleanup, bb->index);
1712 if (dump_file && (dump_flags & TDF_DETAILS))
1713 fprintf (dump_file, " Flagged to clear EH edges.\n");
1715 release_defs (stmt);
1716 return retval;
1721 /* Record any additional equivalences created by this statement. */
1722 if (is_gimple_assign (stmt))
1723 record_equivalences_from_stmt (stmt, may_optimize_p, avail_exprs_stack);
1725 /* If STMT is a COND_EXPR or SWITCH_EXPR and it was modified, then we may
1726 know where it goes. */
1727 if (gimple_modified_p (stmt) || modified_p)
1729 tree val = NULL;
1731 if (gimple_code (stmt) == GIMPLE_COND)
1732 val = fold_binary_loc (gimple_location (stmt),
1733 gimple_cond_code (stmt), boolean_type_node,
1734 gimple_cond_lhs (stmt),
1735 gimple_cond_rhs (stmt));
1736 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
1737 val = gimple_switch_index (swtch_stmt);
1739 if (val && TREE_CODE (val) == INTEGER_CST)
1741 retval = find_taken_edge (bb, val);
1742 if (retval)
1744 /* Fix the condition to be either true or false. */
1745 if (gimple_code (stmt) == GIMPLE_COND)
1747 if (integer_zerop (val))
1748 gimple_cond_make_false (as_a <gcond *> (stmt));
1749 else if (integer_onep (val))
1750 gimple_cond_make_true (as_a <gcond *> (stmt));
1751 else
1752 gcc_unreachable ();
1754 gimple_set_modified (stmt, true);
1757 /* Further simplifications may be possible. */
1758 cfg_altered = true;
1762 update_stmt_if_modified (stmt);
1764 /* If we simplified a statement in such a way as to be shown that it
1765 cannot trap, update the eh information and the cfg to match. */
1766 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
1768 bitmap_set_bit (need_eh_cleanup, bb->index);
1769 if (dump_file && (dump_flags & TDF_DETAILS))
1770 fprintf (dump_file, " Flagged to clear EH edges.\n");
1773 if (!was_noreturn
1774 && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
1775 need_noreturn_fixup.safe_push (stmt);
1777 return retval;