[31/77] Use scalar_int_mode for move2add
[official-gcc.git] / gcc / tree-ssa-dom.c
blobd91766e902e4846383ab7e843a13ba5adc64e969
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. */
61 class edge_info
63 public:
64 typedef std::pair <tree, tree> equiv_pair;
65 edge_info (edge);
66 ~edge_info ();
68 /* Record a simple LHS = RHS equivalence. This may trigger
69 calls to derive_equivalences. */
70 void record_simple_equiv (tree, tree);
72 /* If traversing this edge creates simple equivalences, we store
73 them as LHS/RHS pairs within this vector. */
74 vec<equiv_pair> simple_equivalences;
76 /* Traversing an edge may also indicate one or more particular conditions
77 are true or false. */
78 vec<cond_equivalence> cond_equivalences;
80 private:
81 /* Derive equivalences by walking the use-def chains. */
82 void derive_equivalences (tree, tree, int);
85 /* Track whether or not we have changed the control flow graph. */
86 static bool cfg_altered;
88 /* Bitmap of blocks that have had EH statements cleaned. We should
89 remove their dead edges eventually. */
90 static bitmap need_eh_cleanup;
91 static vec<gimple *> need_noreturn_fixup;
93 /* Statistics for dominator optimizations. */
94 struct opt_stats_d
96 long num_stmts;
97 long num_exprs_considered;
98 long num_re;
99 long num_const_prop;
100 long num_copy_prop;
103 static struct opt_stats_d opt_stats;
105 /* Local functions. */
106 static edge optimize_stmt (basic_block, gimple_stmt_iterator,
107 class const_and_copies *,
108 class avail_exprs_stack *);
109 static void record_equality (tree, tree, class const_and_copies *);
110 static void record_equivalences_from_phis (basic_block);
111 static void record_equivalences_from_incoming_edge (basic_block,
112 class const_and_copies *,
113 class avail_exprs_stack *);
114 static void eliminate_redundant_computations (gimple_stmt_iterator *,
115 class const_and_copies *,
116 class avail_exprs_stack *);
117 static void record_equivalences_from_stmt (gimple *, int,
118 class avail_exprs_stack *);
119 static edge single_incoming_edge_ignoring_loop_edges (basic_block);
120 static void dump_dominator_optimization_stats (FILE *file,
121 hash_table<expr_elt_hasher> *);
123 /* Constructor for EDGE_INFO. An EDGE_INFO instance is always
124 associated with an edge E. */
126 edge_info::edge_info (edge e)
128 /* Free the old one associated with E, if it exists and
129 associate our new object with E. */
130 free_dom_edge_info (e);
131 e->aux = this;
133 /* And initialize the embedded vectors. */
134 simple_equivalences = vNULL;
135 cond_equivalences = vNULL;
138 /* Destructor just needs to release the vectors. */
140 edge_info::~edge_info (void)
142 this->cond_equivalences.release ();
143 this->simple_equivalences.release ();
146 /* NAME is known to have the value VALUE, which must be a constant.
148 Walk through its use-def chain to see if there are other equivalences
149 we might be able to derive.
151 RECURSION_LIMIT controls how far back we recurse through the use-def
152 chains. */
154 void
155 edge_info::derive_equivalences (tree name, tree value, int recursion_limit)
157 if (TREE_CODE (name) != SSA_NAME || TREE_CODE (value) != INTEGER_CST)
158 return;
160 /* This records the equivalence for the toplevel object. Do
161 this before checking the recursion limit. */
162 simple_equivalences.safe_push (equiv_pair (name, value));
164 /* Limit how far up the use-def chains we are willing to walk. */
165 if (recursion_limit == 0)
166 return;
168 /* We can walk up the use-def chains to potentially find more
169 equivalences. */
170 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
171 if (is_gimple_assign (def_stmt))
173 /* We know the result of DEF_STMT was zero. See if that allows
174 us to deduce anything about the SSA_NAMEs used on the RHS. */
175 enum tree_code code = gimple_assign_rhs_code (def_stmt);
176 switch (code)
178 case BIT_IOR_EXPR:
179 if (integer_zerop (value))
181 tree rhs1 = gimple_assign_rhs1 (def_stmt);
182 tree rhs2 = gimple_assign_rhs2 (def_stmt);
184 value = build_zero_cst (TREE_TYPE (rhs1));
185 derive_equivalences (rhs1, value, recursion_limit - 1);
186 value = build_zero_cst (TREE_TYPE (rhs2));
187 derive_equivalences (rhs2, value, recursion_limit - 1);
189 break;
191 /* We know the result of DEF_STMT was one. See if that allows
192 us to deduce anything about the SSA_NAMEs used on the RHS. */
193 case BIT_AND_EXPR:
194 if (!integer_zerop (value))
196 tree rhs1 = gimple_assign_rhs1 (def_stmt);
197 tree rhs2 = gimple_assign_rhs2 (def_stmt);
199 /* If either operand has a boolean range, then we
200 know its value must be one, otherwise we just know it
201 is nonzero. The former is clearly useful, I haven't
202 seen cases where the latter is helpful yet. */
203 if (TREE_CODE (rhs1) == SSA_NAME)
205 if (ssa_name_has_boolean_range (rhs1))
207 value = build_one_cst (TREE_TYPE (rhs1));
208 derive_equivalences (rhs1, value, recursion_limit - 1);
211 if (TREE_CODE (rhs2) == SSA_NAME)
213 if (ssa_name_has_boolean_range (rhs2))
215 value = build_one_cst (TREE_TYPE (rhs2));
216 derive_equivalences (rhs2, value, recursion_limit - 1);
220 break;
222 /* If LHS is an SSA_NAME and RHS is a constant integer and LHS was
223 set via a widening type conversion, then we may be able to record
224 additional equivalences. */
225 case NOP_EXPR:
226 case CONVERT_EXPR:
228 tree rhs = gimple_assign_rhs1 (def_stmt);
229 tree rhs_type = TREE_TYPE (rhs);
230 if (INTEGRAL_TYPE_P (rhs_type)
231 && (TYPE_PRECISION (TREE_TYPE (name))
232 >= TYPE_PRECISION (rhs_type))
233 && int_fits_type_p (value, rhs_type))
234 derive_equivalences (rhs,
235 fold_convert (rhs_type, value),
236 recursion_limit - 1);
237 break;
240 /* We can invert the operation of these codes trivially if
241 one of the RHS operands is a constant to produce a known
242 value for the other RHS operand. */
243 case POINTER_PLUS_EXPR:
244 case PLUS_EXPR:
246 tree rhs1 = gimple_assign_rhs1 (def_stmt);
247 tree rhs2 = gimple_assign_rhs2 (def_stmt);
249 /* If either argument is a constant, then we can compute
250 a constant value for the nonconstant argument. */
251 if (TREE_CODE (rhs1) == INTEGER_CST
252 && TREE_CODE (rhs2) == SSA_NAME)
253 derive_equivalences (rhs2,
254 fold_binary (MINUS_EXPR, TREE_TYPE (rhs1),
255 value, rhs1),
256 recursion_limit - 1);
257 else if (TREE_CODE (rhs2) == INTEGER_CST
258 && TREE_CODE (rhs1) == SSA_NAME)
259 derive_equivalences (rhs1,
260 fold_binary (MINUS_EXPR, TREE_TYPE (rhs1),
261 value, rhs2),
262 recursion_limit - 1);
263 break;
266 /* If one of the operands is a constant, then we can compute
267 the value of the other operand. If both operands are
268 SSA_NAMEs, then they must be equal if the result is zero. */
269 case MINUS_EXPR:
271 tree rhs1 = gimple_assign_rhs1 (def_stmt);
272 tree rhs2 = gimple_assign_rhs2 (def_stmt);
274 /* If either argument is a constant, then we can compute
275 a constant value for the nonconstant argument. */
276 if (TREE_CODE (rhs1) == INTEGER_CST
277 && TREE_CODE (rhs2) == SSA_NAME)
278 derive_equivalences (rhs2,
279 fold_binary (MINUS_EXPR, TREE_TYPE (rhs1),
280 rhs1, value),
281 recursion_limit - 1);
282 else if (TREE_CODE (rhs2) == INTEGER_CST
283 && TREE_CODE (rhs1) == SSA_NAME)
284 derive_equivalences (rhs1,
285 fold_binary (PLUS_EXPR, TREE_TYPE (rhs1),
286 value, rhs2),
287 recursion_limit - 1);
288 else if (integer_zerop (value))
290 tree cond = build2 (EQ_EXPR, boolean_type_node,
291 gimple_assign_rhs1 (def_stmt),
292 gimple_assign_rhs2 (def_stmt));
293 tree inverted = invert_truthvalue (cond);
294 record_conditions (&this->cond_equivalences, cond, inverted);
296 break;
300 case EQ_EXPR:
301 case NE_EXPR:
303 if ((code == EQ_EXPR && integer_onep (value))
304 || (code == NE_EXPR && integer_zerop (value)))
306 tree rhs1 = gimple_assign_rhs1 (def_stmt);
307 tree rhs2 = gimple_assign_rhs2 (def_stmt);
309 /* If either argument is a constant, then record the
310 other argument as being the same as that constant.
312 If neither operand is a constant, then we have a
313 conditional name == name equivalence. */
314 if (TREE_CODE (rhs1) == INTEGER_CST)
315 derive_equivalences (rhs2, rhs1, recursion_limit - 1);
316 else if (TREE_CODE (rhs2) == INTEGER_CST)
317 derive_equivalences (rhs1, rhs2, recursion_limit - 1);
319 else
321 tree cond = build2 (code, boolean_type_node,
322 gimple_assign_rhs1 (def_stmt),
323 gimple_assign_rhs2 (def_stmt));
324 tree inverted = invert_truthvalue (cond);
325 if (integer_zerop (value))
326 std::swap (cond, inverted);
327 record_conditions (&this->cond_equivalences, cond, inverted);
329 break;
332 /* For BIT_NOT and NEGATE, we can just apply the operation to the
333 VALUE to get the new equivalence. It will always be a constant
334 so we can recurse. */
335 case BIT_NOT_EXPR:
336 case NEGATE_EXPR:
338 tree rhs = gimple_assign_rhs1 (def_stmt);
339 tree res = fold_build1 (code, TREE_TYPE (rhs), value);
340 derive_equivalences (rhs, res, recursion_limit - 1);
341 break;
344 default:
346 if (TREE_CODE_CLASS (code) == tcc_comparison)
348 tree cond = build2 (code, boolean_type_node,
349 gimple_assign_rhs1 (def_stmt),
350 gimple_assign_rhs2 (def_stmt));
351 tree inverted = invert_truthvalue (cond);
352 if (integer_zerop (value))
353 std::swap (cond, inverted);
354 record_conditions (&this->cond_equivalences, cond, inverted);
355 break;
357 break;
363 void
364 edge_info::record_simple_equiv (tree lhs, tree rhs)
366 /* If the RHS is a constant, then we may be able to derive
367 further equivalences. Else just record the name = name
368 equivalence. */
369 if (TREE_CODE (rhs) == INTEGER_CST)
370 derive_equivalences (lhs, rhs, 4);
371 else
372 simple_equivalences.safe_push (equiv_pair (lhs, rhs));
375 /* Free the edge_info data attached to E, if it exists. */
377 void
378 free_dom_edge_info (edge e)
380 class edge_info *edge_info = (struct edge_info *)e->aux;
382 if (edge_info)
383 delete edge_info;
386 /* Free all EDGE_INFO structures associated with edges in the CFG.
387 If a particular edge can be threaded, copy the redirection
388 target from the EDGE_INFO structure into the edge's AUX field
389 as required by code to update the CFG and SSA graph for
390 jump threading. */
392 static void
393 free_all_edge_infos (void)
395 basic_block bb;
396 edge_iterator ei;
397 edge e;
399 FOR_EACH_BB_FN (bb, cfun)
401 FOR_EACH_EDGE (e, ei, bb->preds)
403 free_dom_edge_info (e);
404 e->aux = NULL;
409 /* We have finished optimizing BB, record any information implied by
410 taking a specific outgoing edge from BB. */
412 static void
413 record_edge_info (basic_block bb)
415 gimple_stmt_iterator gsi = gsi_last_bb (bb);
416 class edge_info *edge_info;
418 if (! gsi_end_p (gsi))
420 gimple *stmt = gsi_stmt (gsi);
421 location_t loc = gimple_location (stmt);
423 if (gimple_code (stmt) == GIMPLE_SWITCH)
425 gswitch *switch_stmt = as_a <gswitch *> (stmt);
426 tree index = gimple_switch_index (switch_stmt);
428 if (TREE_CODE (index) == SSA_NAME)
430 int i;
431 int n_labels = gimple_switch_num_labels (switch_stmt);
432 tree *info = XCNEWVEC (tree, last_basic_block_for_fn (cfun));
433 edge e;
434 edge_iterator ei;
436 for (i = 0; i < n_labels; i++)
438 tree label = gimple_switch_label (switch_stmt, i);
439 basic_block target_bb = label_to_block (CASE_LABEL (label));
440 if (CASE_HIGH (label)
441 || !CASE_LOW (label)
442 || info[target_bb->index])
443 info[target_bb->index] = error_mark_node;
444 else
445 info[target_bb->index] = label;
448 FOR_EACH_EDGE (e, ei, bb->succs)
450 basic_block target_bb = e->dest;
451 tree label = info[target_bb->index];
453 if (label != NULL && label != error_mark_node)
455 tree x = fold_convert_loc (loc, TREE_TYPE (index),
456 CASE_LOW (label));
457 edge_info = new class edge_info (e);
458 edge_info->record_simple_equiv (index, x);
461 free (info);
465 /* A COND_EXPR may create equivalences too. */
466 if (gimple_code (stmt) == GIMPLE_COND)
468 edge true_edge;
469 edge false_edge;
471 tree op0 = gimple_cond_lhs (stmt);
472 tree op1 = gimple_cond_rhs (stmt);
473 enum tree_code code = gimple_cond_code (stmt);
475 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
477 /* Special case comparing booleans against a constant as we
478 know the value of OP0 on both arms of the branch. i.e., we
479 can record an equivalence for OP0 rather than COND.
481 However, don't do this if the constant isn't zero or one.
482 Such conditionals will get optimized more thoroughly during
483 the domwalk. */
484 if ((code == EQ_EXPR || code == NE_EXPR)
485 && TREE_CODE (op0) == SSA_NAME
486 && ssa_name_has_boolean_range (op0)
487 && is_gimple_min_invariant (op1)
488 && (integer_zerop (op1) || integer_onep (op1)))
490 tree true_val = constant_boolean_node (true, TREE_TYPE (op0));
491 tree false_val = constant_boolean_node (false, TREE_TYPE (op0));
493 if (code == EQ_EXPR)
495 edge_info = new class edge_info (true_edge);
496 edge_info->record_simple_equiv (op0,
497 (integer_zerop (op1)
498 ? false_val : true_val));
499 edge_info = new class edge_info (false_edge);
500 edge_info->record_simple_equiv (op0,
501 (integer_zerop (op1)
502 ? true_val : false_val));
504 else
506 edge_info = new class edge_info (true_edge);
507 edge_info->record_simple_equiv (op0,
508 (integer_zerop (op1)
509 ? true_val : false_val));
510 edge_info = new class edge_info (false_edge);
511 edge_info->record_simple_equiv (op0,
512 (integer_zerop (op1)
513 ? false_val : true_val));
516 /* This can show up in the IL as a result of copy propagation
517 it will eventually be canonicalized, but we have to cope
518 with this case within the pass. */
519 else if (is_gimple_min_invariant (op0)
520 && TREE_CODE (op1) == SSA_NAME)
522 tree cond = build2 (code, boolean_type_node, op0, op1);
523 tree inverted = invert_truthvalue_loc (loc, cond);
524 bool can_infer_simple_equiv
525 = !(HONOR_SIGNED_ZEROS (op0)
526 && real_zerop (op0));
527 struct edge_info *edge_info;
529 edge_info = new class edge_info (true_edge);
530 record_conditions (&edge_info->cond_equivalences, cond, inverted);
532 if (can_infer_simple_equiv && code == EQ_EXPR)
533 edge_info->record_simple_equiv (op1, op0);
535 edge_info = new class edge_info (false_edge);
536 record_conditions (&edge_info->cond_equivalences, inverted, cond);
538 if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
539 edge_info->record_simple_equiv (op1, op0);
542 else if (TREE_CODE (op0) == SSA_NAME
543 && (TREE_CODE (op1) == SSA_NAME
544 || is_gimple_min_invariant (op1)))
546 tree cond = build2 (code, boolean_type_node, op0, op1);
547 tree inverted = invert_truthvalue_loc (loc, cond);
548 bool can_infer_simple_equiv
549 = !(HONOR_SIGNED_ZEROS (op1)
550 && (TREE_CODE (op1) == SSA_NAME || real_zerop (op1)));
551 struct edge_info *edge_info;
553 edge_info = new class edge_info (true_edge);
554 record_conditions (&edge_info->cond_equivalences, cond, inverted);
556 if (can_infer_simple_equiv && code == EQ_EXPR)
557 edge_info->record_simple_equiv (op0, op1);
559 edge_info = new class edge_info (false_edge);
560 record_conditions (&edge_info->cond_equivalences, inverted, cond);
562 if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
563 edge_info->record_simple_equiv (op0, op1);
570 class dom_opt_dom_walker : public dom_walker
572 public:
573 dom_opt_dom_walker (cdi_direction direction,
574 class const_and_copies *const_and_copies,
575 class avail_exprs_stack *avail_exprs_stack)
576 : dom_walker (direction, true),
577 m_const_and_copies (const_and_copies),
578 m_avail_exprs_stack (avail_exprs_stack),
579 m_dummy_cond (NULL) {}
581 virtual edge before_dom_children (basic_block);
582 virtual void after_dom_children (basic_block);
584 private:
586 /* Unwindable equivalences, both const/copy and expression varieties. */
587 class const_and_copies *m_const_and_copies;
588 class avail_exprs_stack *m_avail_exprs_stack;
590 gcond *m_dummy_cond;
593 /* Jump threading, redundancy elimination and const/copy propagation.
595 This pass may expose new symbols that need to be renamed into SSA. For
596 every new symbol exposed, its corresponding bit will be set in
597 VARS_TO_RENAME. */
599 namespace {
601 const pass_data pass_data_dominator =
603 GIMPLE_PASS, /* type */
604 "dom", /* name */
605 OPTGROUP_NONE, /* optinfo_flags */
606 TV_TREE_SSA_DOMINATOR_OPTS, /* tv_id */
607 ( PROP_cfg | PROP_ssa ), /* properties_required */
608 0, /* properties_provided */
609 0, /* properties_destroyed */
610 0, /* todo_flags_start */
611 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
614 class pass_dominator : public gimple_opt_pass
616 public:
617 pass_dominator (gcc::context *ctxt)
618 : gimple_opt_pass (pass_data_dominator, ctxt),
619 may_peel_loop_headers_p (false)
622 /* opt_pass methods: */
623 opt_pass * clone () { return new pass_dominator (m_ctxt); }
624 void set_pass_param (unsigned int n, bool param)
626 gcc_assert (n == 0);
627 may_peel_loop_headers_p = param;
629 virtual bool gate (function *) { return flag_tree_dom != 0; }
630 virtual unsigned int execute (function *);
632 private:
633 /* This flag is used to prevent loops from being peeled repeatedly in jump
634 threading; it will be removed once we preserve loop structures throughout
635 the compilation -- we will be able to mark the affected loops directly in
636 jump threading, and avoid peeling them next time. */
637 bool may_peel_loop_headers_p;
638 }; // class pass_dominator
640 unsigned int
641 pass_dominator::execute (function *fun)
643 memset (&opt_stats, 0, sizeof (opt_stats));
645 /* Create our hash tables. */
646 hash_table<expr_elt_hasher> *avail_exprs
647 = new hash_table<expr_elt_hasher> (1024);
648 class avail_exprs_stack *avail_exprs_stack
649 = new class avail_exprs_stack (avail_exprs);
650 class const_and_copies *const_and_copies = new class const_and_copies ();
651 need_eh_cleanup = BITMAP_ALLOC (NULL);
652 need_noreturn_fixup.create (0);
654 calculate_dominance_info (CDI_DOMINATORS);
655 cfg_altered = false;
657 /* We need to know loop structures in order to avoid destroying them
658 in jump threading. Note that we still can e.g. thread through loop
659 headers to an exit edge, or through loop header to the loop body, assuming
660 that we update the loop info.
662 TODO: We don't need to set LOOPS_HAVE_PREHEADERS generally, but due
663 to several overly conservative bail-outs in jump threading, case
664 gcc.dg/tree-ssa/pr21417.c can't be threaded if loop preheader is
665 missing. We should improve jump threading in future then
666 LOOPS_HAVE_PREHEADERS won't be needed here. */
667 loop_optimizer_init (LOOPS_HAVE_PREHEADERS | LOOPS_HAVE_SIMPLE_LATCHES);
669 /* Initialize the value-handle array. */
670 threadedge_initialize_values ();
672 /* We need accurate information regarding back edges in the CFG
673 for jump threading; this may include back edges that are not part of
674 a single loop. */
675 mark_dfs_back_edges ();
677 /* We want to create the edge info structures before the dominator walk
678 so that they'll be in place for the jump threader, particularly when
679 threading through a join block.
681 The conditions will be lazily updated with global equivalences as
682 we reach them during the dominator walk. */
683 basic_block bb;
684 FOR_EACH_BB_FN (bb, fun)
685 record_edge_info (bb);
687 /* Recursively walk the dominator tree optimizing statements. */
688 dom_opt_dom_walker walker (CDI_DOMINATORS,
689 const_and_copies,
690 avail_exprs_stack);
691 walker.walk (fun->cfg->x_entry_block_ptr);
693 /* Look for blocks where we cleared EDGE_EXECUTABLE on an outgoing
694 edge. When found, remove jump threads which contain any outgoing
695 edge from the affected block. */
696 if (cfg_altered)
698 FOR_EACH_BB_FN (bb, fun)
700 edge_iterator ei;
701 edge e;
703 /* First see if there are any edges without EDGE_EXECUTABLE
704 set. */
705 bool found = false;
706 FOR_EACH_EDGE (e, ei, bb->succs)
708 if ((e->flags & EDGE_EXECUTABLE) == 0)
710 found = true;
711 break;
715 /* If there were any such edges found, then remove jump threads
716 containing any edge leaving BB. */
717 if (found)
718 FOR_EACH_EDGE (e, ei, bb->succs)
719 remove_jump_threads_including (e);
724 gimple_stmt_iterator gsi;
725 basic_block bb;
726 FOR_EACH_BB_FN (bb, fun)
728 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
729 update_stmt_if_modified (gsi_stmt (gsi));
733 /* If we exposed any new variables, go ahead and put them into
734 SSA form now, before we handle jump threading. This simplifies
735 interactions between rewriting of _DECL nodes into SSA form
736 and rewriting SSA_NAME nodes into SSA form after block
737 duplication and CFG manipulation. */
738 update_ssa (TODO_update_ssa);
740 free_all_edge_infos ();
742 /* Thread jumps, creating duplicate blocks as needed. */
743 cfg_altered |= thread_through_all_blocks (may_peel_loop_headers_p);
745 if (cfg_altered)
746 free_dominance_info (CDI_DOMINATORS);
748 /* Removal of statements may make some EH edges dead. Purge
749 such edges from the CFG as needed. */
750 if (!bitmap_empty_p (need_eh_cleanup))
752 unsigned i;
753 bitmap_iterator bi;
755 /* Jump threading may have created forwarder blocks from blocks
756 needing EH cleanup; the new successor of these blocks, which
757 has inherited from the original block, needs the cleanup.
758 Don't clear bits in the bitmap, as that can break the bitmap
759 iterator. */
760 EXECUTE_IF_SET_IN_BITMAP (need_eh_cleanup, 0, i, bi)
762 basic_block bb = BASIC_BLOCK_FOR_FN (fun, i);
763 if (bb == NULL)
764 continue;
765 while (single_succ_p (bb)
766 && (single_succ_edge (bb)->flags & EDGE_EH) == 0)
767 bb = single_succ (bb);
768 if (bb == EXIT_BLOCK_PTR_FOR_FN (fun))
769 continue;
770 if ((unsigned) bb->index != i)
771 bitmap_set_bit (need_eh_cleanup, bb->index);
774 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
775 bitmap_clear (need_eh_cleanup);
778 /* Fixup stmts that became noreturn calls. This may require splitting
779 blocks and thus isn't possible during the dominator walk or before
780 jump threading finished. Do this in reverse order so we don't
781 inadvertedly remove a stmt we want to fixup by visiting a dominating
782 now noreturn call first. */
783 while (!need_noreturn_fixup.is_empty ())
785 gimple *stmt = need_noreturn_fixup.pop ();
786 if (dump_file && dump_flags & TDF_DETAILS)
788 fprintf (dump_file, "Fixing up noreturn call ");
789 print_gimple_stmt (dump_file, stmt, 0);
790 fprintf (dump_file, "\n");
792 fixup_noreturn_call (stmt);
795 statistics_counter_event (fun, "Redundant expressions eliminated",
796 opt_stats.num_re);
797 statistics_counter_event (fun, "Constants propagated",
798 opt_stats.num_const_prop);
799 statistics_counter_event (fun, "Copies propagated",
800 opt_stats.num_copy_prop);
802 /* Debugging dumps. */
803 if (dump_file && (dump_flags & TDF_STATS))
804 dump_dominator_optimization_stats (dump_file, avail_exprs);
806 loop_optimizer_finalize ();
808 /* Delete our main hashtable. */
809 delete avail_exprs;
810 avail_exprs = NULL;
812 /* Free asserted bitmaps and stacks. */
813 BITMAP_FREE (need_eh_cleanup);
814 need_noreturn_fixup.release ();
815 delete avail_exprs_stack;
816 delete const_and_copies;
818 /* Free the value-handle array. */
819 threadedge_finalize_values ();
821 return 0;
824 } // anon namespace
826 gimple_opt_pass *
827 make_pass_dominator (gcc::context *ctxt)
829 return new pass_dominator (ctxt);
833 /* A trivial wrapper so that we can present the generic jump
834 threading code with a simple API for simplifying statements. */
835 static tree
836 simplify_stmt_for_jump_threading (gimple *stmt,
837 gimple *within_stmt ATTRIBUTE_UNUSED,
838 class avail_exprs_stack *avail_exprs_stack,
839 basic_block bb ATTRIBUTE_UNUSED)
841 return avail_exprs_stack->lookup_avail_expr (stmt, false, true);
844 /* Valueize hook for gimple_fold_stmt_to_constant_1. */
846 static tree
847 dom_valueize (tree t)
849 if (TREE_CODE (t) == SSA_NAME)
851 tree tem = SSA_NAME_VALUE (t);
852 if (tem)
853 return tem;
855 return t;
858 /* We have just found an equivalence for LHS on an edge E.
859 Look backwards to other uses of LHS and see if we can derive
860 additional equivalences that are valid on edge E. */
861 static void
862 back_propagate_equivalences (tree lhs, edge e,
863 class const_and_copies *const_and_copies)
865 use_operand_p use_p;
866 imm_use_iterator iter;
867 bitmap domby = NULL;
868 basic_block dest = e->dest;
870 /* Iterate over the uses of LHS to see if any dominate E->dest.
871 If so, they may create useful equivalences too.
873 ??? If the code gets re-organized to a worklist to catch more
874 indirect opportunities and it is made to handle PHIs then this
875 should only consider use_stmts in basic-blocks we have already visited. */
876 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
878 gimple *use_stmt = USE_STMT (use_p);
880 /* Often the use is in DEST, which we trivially know we can't use.
881 This is cheaper than the dominator set tests below. */
882 if (dest == gimple_bb (use_stmt))
883 continue;
885 /* Filter out statements that can never produce a useful
886 equivalence. */
887 tree lhs2 = gimple_get_lhs (use_stmt);
888 if (!lhs2 || TREE_CODE (lhs2) != SSA_NAME)
889 continue;
891 /* Profiling has shown the domination tests here can be fairly
892 expensive. We get significant improvements by building the
893 set of blocks that dominate BB. We can then just test
894 for set membership below.
896 We also initialize the set lazily since often the only uses
897 are going to be in the same block as DEST. */
898 if (!domby)
900 domby = BITMAP_ALLOC (NULL);
901 basic_block bb = get_immediate_dominator (CDI_DOMINATORS, dest);
902 while (bb)
904 bitmap_set_bit (domby, bb->index);
905 bb = get_immediate_dominator (CDI_DOMINATORS, bb);
909 /* This tests if USE_STMT does not dominate DEST. */
910 if (!bitmap_bit_p (domby, gimple_bb (use_stmt)->index))
911 continue;
913 /* At this point USE_STMT dominates DEST and may result in a
914 useful equivalence. Try to simplify its RHS to a constant
915 or SSA_NAME. */
916 tree res = gimple_fold_stmt_to_constant_1 (use_stmt, dom_valueize,
917 no_follow_ssa_edges);
918 if (res && (TREE_CODE (res) == SSA_NAME || is_gimple_min_invariant (res)))
919 record_equality (lhs2, res, const_and_copies);
922 if (domby)
923 BITMAP_FREE (domby);
926 /* Record into CONST_AND_COPIES and AVAIL_EXPRS_STACK any equivalences implied
927 by traversing edge E (which are cached in E->aux).
929 Callers are responsible for managing the unwinding markers. */
930 void
931 record_temporary_equivalences (edge e,
932 class const_and_copies *const_and_copies,
933 class avail_exprs_stack *avail_exprs_stack)
935 int i;
936 class edge_info *edge_info = (class edge_info *) e->aux;
938 /* If we have info associated with this edge, record it into
939 our equivalence tables. */
940 if (edge_info)
942 cond_equivalence *eq;
943 /* If we have 0 = COND or 1 = COND equivalences, record them
944 into our expression hash tables. */
945 for (i = 0; edge_info->cond_equivalences.iterate (i, &eq); ++i)
946 avail_exprs_stack->record_cond (eq);
948 edge_info::equiv_pair *seq;
949 for (i = 0; edge_info->simple_equivalences.iterate (i, &seq); ++i)
951 tree lhs = seq->first;
952 if (!lhs || TREE_CODE (lhs) != SSA_NAME)
953 continue;
955 /* Record the simple NAME = VALUE equivalence. */
956 tree rhs = seq->second;
958 /* If this is a SSA_NAME = SSA_NAME equivalence and one operand is
959 cheaper to compute than the other, then set up the equivalence
960 such that we replace the expensive one with the cheap one.
962 If they are the same cost to compute, then do not record
963 anything. */
964 if (TREE_CODE (lhs) == SSA_NAME && TREE_CODE (rhs) == SSA_NAME)
966 gimple *rhs_def = SSA_NAME_DEF_STMT (rhs);
967 int rhs_cost = estimate_num_insns (rhs_def, &eni_size_weights);
969 gimple *lhs_def = SSA_NAME_DEF_STMT (lhs);
970 int lhs_cost = estimate_num_insns (lhs_def, &eni_size_weights);
972 if (rhs_cost > lhs_cost)
973 record_equality (rhs, lhs, const_and_copies);
974 else if (rhs_cost < lhs_cost)
975 record_equality (lhs, rhs, const_and_copies);
977 else
978 record_equality (lhs, rhs, const_and_copies);
981 /* Any equivalence found for LHS may result in additional
982 equivalences for other uses of LHS that we have already
983 processed. */
984 back_propagate_equivalences (lhs, e, const_and_copies);
989 /* PHI nodes can create equivalences too.
991 Ignoring any alternatives which are the same as the result, if
992 all the alternatives are equal, then the PHI node creates an
993 equivalence. */
995 static void
996 record_equivalences_from_phis (basic_block bb)
998 gphi_iterator gsi;
1000 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1002 gphi *phi = gsi.phi ();
1004 tree lhs = gimple_phi_result (phi);
1005 tree rhs = NULL;
1006 size_t i;
1008 for (i = 0; i < gimple_phi_num_args (phi); i++)
1010 tree t = gimple_phi_arg_def (phi, i);
1012 /* Ignore alternatives which are the same as our LHS. Since
1013 LHS is a PHI_RESULT, it is known to be a SSA_NAME, so we
1014 can simply compare pointers. */
1015 if (lhs == t)
1016 continue;
1018 /* If the associated edge is not marked as executable, then it
1019 can be ignored. */
1020 if ((gimple_phi_arg_edge (phi, i)->flags & EDGE_EXECUTABLE) == 0)
1021 continue;
1023 t = dom_valueize (t);
1025 /* If we have not processed an alternative yet, then set
1026 RHS to this alternative. */
1027 if (rhs == NULL)
1028 rhs = t;
1029 /* If we have processed an alternative (stored in RHS), then
1030 see if it is equal to this one. If it isn't, then stop
1031 the search. */
1032 else if (! operand_equal_for_phi_arg_p (rhs, t))
1033 break;
1036 /* If we had no interesting alternatives, then all the RHS alternatives
1037 must have been the same as LHS. */
1038 if (!rhs)
1039 rhs = lhs;
1041 /* If we managed to iterate through each PHI alternative without
1042 breaking out of the loop, then we have a PHI which may create
1043 a useful equivalence. We do not need to record unwind data for
1044 this, since this is a true assignment and not an equivalence
1045 inferred from a comparison. All uses of this ssa name are dominated
1046 by this assignment, so unwinding just costs time and space. */
1047 if (i == gimple_phi_num_args (phi)
1048 && may_propagate_copy (lhs, rhs))
1049 set_ssa_name_value (lhs, rhs);
1053 /* Ignoring loop backedges, if BB has precisely one incoming edge then
1054 return that edge. Otherwise return NULL. */
1055 static edge
1056 single_incoming_edge_ignoring_loop_edges (basic_block bb)
1058 edge retval = NULL;
1059 edge e;
1060 edge_iterator ei;
1062 FOR_EACH_EDGE (e, ei, bb->preds)
1064 /* A loop back edge can be identified by the destination of
1065 the edge dominating the source of the edge. */
1066 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
1067 continue;
1069 /* We can safely ignore edges that are not executable. */
1070 if ((e->flags & EDGE_EXECUTABLE) == 0)
1071 continue;
1073 /* If we have already seen a non-loop edge, then we must have
1074 multiple incoming non-loop edges and thus we return NULL. */
1075 if (retval)
1076 return NULL;
1078 /* This is the first non-loop incoming edge we have found. Record
1079 it. */
1080 retval = e;
1083 return retval;
1086 /* Record any equivalences created by the incoming edge to BB into
1087 CONST_AND_COPIES and AVAIL_EXPRS_STACK. If BB has more than one
1088 incoming edge, then no equivalence is created. */
1090 static void
1091 record_equivalences_from_incoming_edge (basic_block bb,
1092 class const_and_copies *const_and_copies,
1093 class avail_exprs_stack *avail_exprs_stack)
1095 edge e;
1096 basic_block parent;
1098 /* If our parent block ended with a control statement, then we may be
1099 able to record some equivalences based on which outgoing edge from
1100 the parent was followed. */
1101 parent = get_immediate_dominator (CDI_DOMINATORS, bb);
1103 e = single_incoming_edge_ignoring_loop_edges (bb);
1105 /* If we had a single incoming edge from our parent block, then enter
1106 any data associated with the edge into our tables. */
1107 if (e && e->src == parent)
1108 record_temporary_equivalences (e, const_and_copies, avail_exprs_stack);
1111 /* Dump statistics for the hash table HTAB. */
1113 static void
1114 htab_statistics (FILE *file, const hash_table<expr_elt_hasher> &htab)
1116 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
1117 (long) htab.size (),
1118 (long) htab.elements (),
1119 htab.collisions ());
1122 /* Dump SSA statistics on FILE. */
1124 static void
1125 dump_dominator_optimization_stats (FILE *file,
1126 hash_table<expr_elt_hasher> *avail_exprs)
1128 fprintf (file, "Total number of statements: %6ld\n\n",
1129 opt_stats.num_stmts);
1130 fprintf (file, "Exprs considered for dominator optimizations: %6ld\n",
1131 opt_stats.num_exprs_considered);
1133 fprintf (file, "\nHash table statistics:\n");
1135 fprintf (file, " avail_exprs: ");
1136 htab_statistics (file, *avail_exprs);
1140 /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1141 This constrains the cases in which we may treat this as assignment. */
1143 static void
1144 record_equality (tree x, tree y, class const_and_copies *const_and_copies)
1146 tree prev_x = NULL, prev_y = NULL;
1148 if (tree_swap_operands_p (x, y))
1149 std::swap (x, y);
1151 /* Most of the time tree_swap_operands_p does what we want. But there
1152 are cases where we know one operand is better for copy propagation than
1153 the other. Given no other code cares about ordering of equality
1154 comparison operators for that purpose, we just handle the special cases
1155 here. */
1156 if (TREE_CODE (x) == SSA_NAME && TREE_CODE (y) == SSA_NAME)
1158 /* If one operand is a single use operand, then make it
1159 X. This will preserve its single use properly and if this
1160 conditional is eliminated, the computation of X can be
1161 eliminated as well. */
1162 if (has_single_use (y) && ! has_single_use (x))
1163 std::swap (x, y);
1165 if (TREE_CODE (x) == SSA_NAME)
1166 prev_x = SSA_NAME_VALUE (x);
1167 if (TREE_CODE (y) == SSA_NAME)
1168 prev_y = SSA_NAME_VALUE (y);
1170 /* If one of the previous values is invariant, or invariant in more loops
1171 (by depth), then use that.
1172 Otherwise it doesn't matter which value we choose, just so
1173 long as we canonicalize on one value. */
1174 if (is_gimple_min_invariant (y))
1176 else if (is_gimple_min_invariant (x))
1177 prev_x = x, x = y, y = prev_x, prev_x = prev_y;
1178 else if (prev_x && is_gimple_min_invariant (prev_x))
1179 x = y, y = prev_x, prev_x = prev_y;
1180 else if (prev_y)
1181 y = prev_y;
1183 /* After the swapping, we must have one SSA_NAME. */
1184 if (TREE_CODE (x) != SSA_NAME)
1185 return;
1187 /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1188 variable compared against zero. If we're honoring signed zeros,
1189 then we cannot record this value unless we know that the value is
1190 nonzero. */
1191 if (HONOR_SIGNED_ZEROS (x)
1192 && (TREE_CODE (y) != REAL_CST
1193 || real_equal (&dconst0, &TREE_REAL_CST (y))))
1194 return;
1196 const_and_copies->record_const_or_copy (x, y, prev_x);
1199 /* Returns true when STMT is a simple iv increment. It detects the
1200 following situation:
1202 i_1 = phi (..., i_2)
1203 i_2 = i_1 +/- ... */
1205 bool
1206 simple_iv_increment_p (gimple *stmt)
1208 enum tree_code code;
1209 tree lhs, preinc;
1210 gimple *phi;
1211 size_t i;
1213 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1214 return false;
1216 lhs = gimple_assign_lhs (stmt);
1217 if (TREE_CODE (lhs) != SSA_NAME)
1218 return false;
1220 code = gimple_assign_rhs_code (stmt);
1221 if (code != PLUS_EXPR
1222 && code != MINUS_EXPR
1223 && code != POINTER_PLUS_EXPR)
1224 return false;
1226 preinc = gimple_assign_rhs1 (stmt);
1227 if (TREE_CODE (preinc) != SSA_NAME)
1228 return false;
1230 phi = SSA_NAME_DEF_STMT (preinc);
1231 if (gimple_code (phi) != GIMPLE_PHI)
1232 return false;
1234 for (i = 0; i < gimple_phi_num_args (phi); i++)
1235 if (gimple_phi_arg_def (phi, i) == lhs)
1236 return true;
1238 return false;
1241 /* Propagate know values from SSA_NAME_VALUE into the PHI nodes of the
1242 successors of BB. */
1244 static void
1245 cprop_into_successor_phis (basic_block bb,
1246 class const_and_copies *const_and_copies)
1248 edge e;
1249 edge_iterator ei;
1251 FOR_EACH_EDGE (e, ei, bb->succs)
1253 int indx;
1254 gphi_iterator gsi;
1256 /* If this is an abnormal edge, then we do not want to copy propagate
1257 into the PHI alternative associated with this edge. */
1258 if (e->flags & EDGE_ABNORMAL)
1259 continue;
1261 gsi = gsi_start_phis (e->dest);
1262 if (gsi_end_p (gsi))
1263 continue;
1265 /* We may have an equivalence associated with this edge. While
1266 we can not propagate it into non-dominated blocks, we can
1267 propagate them into PHIs in non-dominated blocks. */
1269 /* Push the unwind marker so we can reset the const and copies
1270 table back to its original state after processing this edge. */
1271 const_and_copies->push_marker ();
1273 /* Extract and record any simple NAME = VALUE equivalences.
1275 Don't bother with [01] = COND equivalences, they're not useful
1276 here. */
1277 class edge_info *edge_info = (class edge_info *) e->aux;
1279 if (edge_info)
1281 edge_info::equiv_pair *seq;
1282 for (int i = 0; edge_info->simple_equivalences.iterate (i, &seq); ++i)
1284 tree lhs = seq->first;
1285 tree rhs = seq->second;
1287 if (lhs && TREE_CODE (lhs) == SSA_NAME)
1288 const_and_copies->record_const_or_copy (lhs, rhs);
1293 indx = e->dest_idx;
1294 for ( ; !gsi_end_p (gsi); gsi_next (&gsi))
1296 tree new_val;
1297 use_operand_p orig_p;
1298 tree orig_val;
1299 gphi *phi = gsi.phi ();
1301 /* The alternative may be associated with a constant, so verify
1302 it is an SSA_NAME before doing anything with it. */
1303 orig_p = gimple_phi_arg_imm_use_ptr (phi, indx);
1304 orig_val = get_use_from_ptr (orig_p);
1305 if (TREE_CODE (orig_val) != SSA_NAME)
1306 continue;
1308 /* If we have *ORIG_P in our constant/copy table, then replace
1309 ORIG_P with its value in our constant/copy table. */
1310 new_val = SSA_NAME_VALUE (orig_val);
1311 if (new_val
1312 && new_val != orig_val
1313 && may_propagate_copy (orig_val, new_val))
1314 propagate_value (orig_p, new_val);
1317 const_and_copies->pop_to_marker ();
1321 edge
1322 dom_opt_dom_walker::before_dom_children (basic_block bb)
1324 gimple_stmt_iterator gsi;
1326 if (dump_file && (dump_flags & TDF_DETAILS))
1327 fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
1329 /* Push a marker on the stacks of local information so that we know how
1330 far to unwind when we finalize this block. */
1331 m_avail_exprs_stack->push_marker ();
1332 m_const_and_copies->push_marker ();
1334 record_equivalences_from_incoming_edge (bb, m_const_and_copies,
1335 m_avail_exprs_stack);
1337 /* PHI nodes can create equivalences too. */
1338 record_equivalences_from_phis (bb);
1340 /* Create equivalences from redundant PHIs. PHIs are only truly
1341 redundant when they exist in the same block, so push another
1342 marker and unwind right afterwards. */
1343 m_avail_exprs_stack->push_marker ();
1344 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1345 eliminate_redundant_computations (&gsi, m_const_and_copies,
1346 m_avail_exprs_stack);
1347 m_avail_exprs_stack->pop_to_marker ();
1349 edge taken_edge = NULL;
1350 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1351 taken_edge
1352 = optimize_stmt (bb, gsi, m_const_and_copies, m_avail_exprs_stack);
1354 /* Now prepare to process dominated blocks. */
1355 record_edge_info (bb);
1356 cprop_into_successor_phis (bb, m_const_and_copies);
1357 if (taken_edge && !dbg_cnt (dom_unreachable_edges))
1358 return NULL;
1360 return taken_edge;
1363 /* We have finished processing the dominator children of BB, perform
1364 any finalization actions in preparation for leaving this node in
1365 the dominator tree. */
1367 void
1368 dom_opt_dom_walker::after_dom_children (basic_block bb)
1370 if (! m_dummy_cond)
1371 m_dummy_cond = gimple_build_cond (NE_EXPR, integer_zero_node,
1372 integer_zero_node, NULL, NULL);
1374 thread_outgoing_edges (bb, m_dummy_cond, m_const_and_copies,
1375 m_avail_exprs_stack,
1376 simplify_stmt_for_jump_threading);
1378 /* These remove expressions local to BB from the tables. */
1379 m_avail_exprs_stack->pop_to_marker ();
1380 m_const_and_copies->pop_to_marker ();
1383 /* Search for redundant computations in STMT. If any are found, then
1384 replace them with the variable holding the result of the computation.
1386 If safe, record this expression into AVAIL_EXPRS_STACK and
1387 CONST_AND_COPIES. */
1389 static void
1390 eliminate_redundant_computations (gimple_stmt_iterator* gsi,
1391 class const_and_copies *const_and_copies,
1392 class avail_exprs_stack *avail_exprs_stack)
1394 tree expr_type;
1395 tree cached_lhs;
1396 tree def;
1397 bool insert = true;
1398 bool assigns_var_p = false;
1400 gimple *stmt = gsi_stmt (*gsi);
1402 if (gimple_code (stmt) == GIMPLE_PHI)
1403 def = gimple_phi_result (stmt);
1404 else
1405 def = gimple_get_lhs (stmt);
1407 /* Certain expressions on the RHS can be optimized away, but can not
1408 themselves be entered into the hash tables. */
1409 if (! def
1410 || TREE_CODE (def) != SSA_NAME
1411 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)
1412 || gimple_vdef (stmt)
1413 /* Do not record equivalences for increments of ivs. This would create
1414 overlapping live ranges for a very questionable gain. */
1415 || simple_iv_increment_p (stmt))
1416 insert = false;
1418 /* Check if the expression has been computed before. */
1419 cached_lhs = avail_exprs_stack->lookup_avail_expr (stmt, insert, true);
1421 opt_stats.num_exprs_considered++;
1423 /* Get the type of the expression we are trying to optimize. */
1424 if (is_gimple_assign (stmt))
1426 expr_type = TREE_TYPE (gimple_assign_lhs (stmt));
1427 assigns_var_p = true;
1429 else if (gimple_code (stmt) == GIMPLE_COND)
1430 expr_type = boolean_type_node;
1431 else if (is_gimple_call (stmt))
1433 gcc_assert (gimple_call_lhs (stmt));
1434 expr_type = TREE_TYPE (gimple_call_lhs (stmt));
1435 assigns_var_p = true;
1437 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
1438 expr_type = TREE_TYPE (gimple_switch_index (swtch_stmt));
1439 else if (gimple_code (stmt) == GIMPLE_PHI)
1440 /* We can't propagate into a phi, so the logic below doesn't apply.
1441 Instead record an equivalence between the cached LHS and the
1442 PHI result of this statement, provided they are in the same block.
1443 This should be sufficient to kill the redundant phi. */
1445 if (def && cached_lhs)
1446 const_and_copies->record_const_or_copy (def, cached_lhs);
1447 return;
1449 else
1450 gcc_unreachable ();
1452 if (!cached_lhs)
1453 return;
1455 /* It is safe to ignore types here since we have already done
1456 type checking in the hashing and equality routines. In fact
1457 type checking here merely gets in the way of constant
1458 propagation. Also, make sure that it is safe to propagate
1459 CACHED_LHS into the expression in STMT. */
1460 if ((TREE_CODE (cached_lhs) != SSA_NAME
1461 && (assigns_var_p
1462 || useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs))))
1463 || may_propagate_copy_into_stmt (stmt, cached_lhs))
1465 gcc_checking_assert (TREE_CODE (cached_lhs) == SSA_NAME
1466 || is_gimple_min_invariant (cached_lhs));
1468 if (dump_file && (dump_flags & TDF_DETAILS))
1470 fprintf (dump_file, " Replaced redundant expr '");
1471 print_gimple_expr (dump_file, stmt, 0, dump_flags);
1472 fprintf (dump_file, "' with '");
1473 print_generic_expr (dump_file, cached_lhs, dump_flags);
1474 fprintf (dump_file, "'\n");
1477 opt_stats.num_re++;
1479 if (assigns_var_p
1480 && !useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs)))
1481 cached_lhs = fold_convert (expr_type, cached_lhs);
1483 propagate_tree_value_into_stmt (gsi, cached_lhs);
1485 /* Since it is always necessary to mark the result as modified,
1486 perhaps we should move this into propagate_tree_value_into_stmt
1487 itself. */
1488 gimple_set_modified (gsi_stmt (*gsi), true);
1492 /* STMT, a GIMPLE_ASSIGN, may create certain equivalences, in either
1493 the available expressions table or the const_and_copies table.
1494 Detect and record those equivalences into AVAIL_EXPRS_STACK.
1496 We handle only very simple copy equivalences here. The heavy
1497 lifing is done by eliminate_redundant_computations. */
1499 static void
1500 record_equivalences_from_stmt (gimple *stmt, int may_optimize_p,
1501 class avail_exprs_stack *avail_exprs_stack)
1503 tree lhs;
1504 enum tree_code lhs_code;
1506 gcc_assert (is_gimple_assign (stmt));
1508 lhs = gimple_assign_lhs (stmt);
1509 lhs_code = TREE_CODE (lhs);
1511 if (lhs_code == SSA_NAME
1512 && gimple_assign_single_p (stmt))
1514 tree rhs = gimple_assign_rhs1 (stmt);
1516 /* If the RHS of the assignment is a constant or another variable that
1517 may be propagated, register it in the CONST_AND_COPIES table. We
1518 do not need to record unwind data for this, since this is a true
1519 assignment and not an equivalence inferred from a comparison. All
1520 uses of this ssa name are dominated by this assignment, so unwinding
1521 just costs time and space. */
1522 if (may_optimize_p
1523 && (TREE_CODE (rhs) == SSA_NAME
1524 || is_gimple_min_invariant (rhs)))
1526 rhs = dom_valueize (rhs);
1528 if (dump_file && (dump_flags & TDF_DETAILS))
1530 fprintf (dump_file, "==== ASGN ");
1531 print_generic_expr (dump_file, lhs);
1532 fprintf (dump_file, " = ");
1533 print_generic_expr (dump_file, rhs);
1534 fprintf (dump_file, "\n");
1537 set_ssa_name_value (lhs, rhs);
1541 /* Make sure we can propagate &x + CST. */
1542 if (lhs_code == SSA_NAME
1543 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
1544 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR
1545 && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)
1547 tree op0 = gimple_assign_rhs1 (stmt);
1548 tree op1 = gimple_assign_rhs2 (stmt);
1549 tree new_rhs
1550 = build_fold_addr_expr (fold_build2 (MEM_REF,
1551 TREE_TYPE (TREE_TYPE (op0)),
1552 unshare_expr (op0),
1553 fold_convert (ptr_type_node,
1554 op1)));
1555 if (dump_file && (dump_flags & TDF_DETAILS))
1557 fprintf (dump_file, "==== ASGN ");
1558 print_generic_expr (dump_file, lhs);
1559 fprintf (dump_file, " = ");
1560 print_generic_expr (dump_file, new_rhs);
1561 fprintf (dump_file, "\n");
1564 set_ssa_name_value (lhs, new_rhs);
1567 /* A memory store, even an aliased store, creates a useful
1568 equivalence. By exchanging the LHS and RHS, creating suitable
1569 vops and recording the result in the available expression table,
1570 we may be able to expose more redundant loads. */
1571 if (!gimple_has_volatile_ops (stmt)
1572 && gimple_references_memory_p (stmt)
1573 && gimple_assign_single_p (stmt)
1574 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
1575 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
1576 && !is_gimple_reg (lhs))
1578 tree rhs = gimple_assign_rhs1 (stmt);
1579 gassign *new_stmt;
1581 /* Build a new statement with the RHS and LHS exchanged. */
1582 if (TREE_CODE (rhs) == SSA_NAME)
1584 /* NOTE tuples. The call to gimple_build_assign below replaced
1585 a call to build_gimple_modify_stmt, which did not set the
1586 SSA_NAME_DEF_STMT on the LHS of the assignment. Doing so
1587 may cause an SSA validation failure, as the LHS may be a
1588 default-initialized name and should have no definition. I'm
1589 a bit dubious of this, as the artificial statement that we
1590 generate here may in fact be ill-formed, but it is simply
1591 used as an internal device in this pass, and never becomes
1592 part of the CFG. */
1593 gimple *defstmt = SSA_NAME_DEF_STMT (rhs);
1594 new_stmt = gimple_build_assign (rhs, lhs);
1595 SSA_NAME_DEF_STMT (rhs) = defstmt;
1597 else
1598 new_stmt = gimple_build_assign (rhs, lhs);
1600 gimple_set_vuse (new_stmt, gimple_vdef (stmt));
1602 /* Finally enter the statement into the available expression
1603 table. */
1604 avail_exprs_stack->lookup_avail_expr (new_stmt, true, true);
1608 /* Replace *OP_P in STMT with any known equivalent value for *OP_P from
1609 CONST_AND_COPIES. */
1611 static void
1612 cprop_operand (gimple *stmt, use_operand_p op_p)
1614 tree val;
1615 tree op = USE_FROM_PTR (op_p);
1617 /* If the operand has a known constant value or it is known to be a
1618 copy of some other variable, use the value or copy stored in
1619 CONST_AND_COPIES. */
1620 val = SSA_NAME_VALUE (op);
1621 if (val && val != op)
1623 /* Do not replace hard register operands in asm statements. */
1624 if (gimple_code (stmt) == GIMPLE_ASM
1625 && !may_propagate_copy_into_asm (op))
1626 return;
1628 /* Certain operands are not allowed to be copy propagated due
1629 to their interaction with exception handling and some GCC
1630 extensions. */
1631 if (!may_propagate_copy (op, val))
1632 return;
1634 /* Do not propagate copies into BIVs.
1635 See PR23821 and PR62217 for how this can disturb IV and
1636 number of iteration analysis. */
1637 if (TREE_CODE (val) != INTEGER_CST)
1639 gimple *def = SSA_NAME_DEF_STMT (op);
1640 if (gimple_code (def) == GIMPLE_PHI
1641 && gimple_bb (def)->loop_father->header == gimple_bb (def))
1642 return;
1645 /* Dump details. */
1646 if (dump_file && (dump_flags & TDF_DETAILS))
1648 fprintf (dump_file, " Replaced '");
1649 print_generic_expr (dump_file, op, dump_flags);
1650 fprintf (dump_file, "' with %s '",
1651 (TREE_CODE (val) != SSA_NAME ? "constant" : "variable"));
1652 print_generic_expr (dump_file, val, dump_flags);
1653 fprintf (dump_file, "'\n");
1656 if (TREE_CODE (val) != SSA_NAME)
1657 opt_stats.num_const_prop++;
1658 else
1659 opt_stats.num_copy_prop++;
1661 propagate_value (op_p, val);
1663 /* And note that we modified this statement. This is now
1664 safe, even if we changed virtual operands since we will
1665 rescan the statement and rewrite its operands again. */
1666 gimple_set_modified (stmt, true);
1670 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
1671 known value for that SSA_NAME (or NULL if no value is known).
1673 Propagate values from CONST_AND_COPIES into the uses, vuses and
1674 vdef_ops of STMT. */
1676 static void
1677 cprop_into_stmt (gimple *stmt)
1679 use_operand_p op_p;
1680 ssa_op_iter iter;
1681 tree last_copy_propagated_op = NULL;
1683 FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_USE)
1685 tree old_op = USE_FROM_PTR (op_p);
1687 /* If we have A = B and B = A in the copy propagation tables
1688 (due to an equality comparison), avoid substituting B for A
1689 then A for B in the trivially discovered cases. This allows
1690 optimization of statements were A and B appear as input
1691 operands. */
1692 if (old_op != last_copy_propagated_op)
1694 cprop_operand (stmt, op_p);
1696 tree new_op = USE_FROM_PTR (op_p);
1697 if (new_op != old_op && TREE_CODE (new_op) == SSA_NAME)
1698 last_copy_propagated_op = new_op;
1703 /* Optimize the statement in block BB pointed to by iterator SI
1704 using equivalences from CONST_AND_COPIES and AVAIL_EXPRS_STACK.
1706 We try to perform some simplistic global redundancy elimination and
1707 constant propagation:
1709 1- To detect global redundancy, we keep track of expressions that have
1710 been computed in this block and its dominators. If we find that the
1711 same expression is computed more than once, we eliminate repeated
1712 computations by using the target of the first one.
1714 2- Constant values and copy assignments. This is used to do very
1715 simplistic constant and copy propagation. When a constant or copy
1716 assignment is found, we map the value on the RHS of the assignment to
1717 the variable in the LHS in the CONST_AND_COPIES table. */
1719 static edge
1720 optimize_stmt (basic_block bb, gimple_stmt_iterator si,
1721 class const_and_copies *const_and_copies,
1722 class avail_exprs_stack *avail_exprs_stack)
1724 gimple *stmt, *old_stmt;
1725 bool may_optimize_p;
1726 bool modified_p = false;
1727 bool was_noreturn;
1728 edge retval = NULL;
1730 old_stmt = stmt = gsi_stmt (si);
1731 was_noreturn = is_gimple_call (stmt) && gimple_call_noreturn_p (stmt);
1733 if (dump_file && (dump_flags & TDF_DETAILS))
1735 fprintf (dump_file, "Optimizing statement ");
1736 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1739 update_stmt_if_modified (stmt);
1740 opt_stats.num_stmts++;
1742 /* Const/copy propagate into USES, VUSES and the RHS of VDEFs. */
1743 cprop_into_stmt (stmt);
1745 /* If the statement has been modified with constant replacements,
1746 fold its RHS before checking for redundant computations. */
1747 if (gimple_modified_p (stmt))
1749 tree rhs = NULL;
1751 /* Try to fold the statement making sure that STMT is kept
1752 up to date. */
1753 if (fold_stmt (&si))
1755 stmt = gsi_stmt (si);
1756 gimple_set_modified (stmt, true);
1758 if (dump_file && (dump_flags & TDF_DETAILS))
1760 fprintf (dump_file, " Folded to: ");
1761 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1765 /* We only need to consider cases that can yield a gimple operand. */
1766 if (gimple_assign_single_p (stmt))
1767 rhs = gimple_assign_rhs1 (stmt);
1768 else if (gimple_code (stmt) == GIMPLE_GOTO)
1769 rhs = gimple_goto_dest (stmt);
1770 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
1771 /* This should never be an ADDR_EXPR. */
1772 rhs = gimple_switch_index (swtch_stmt);
1774 if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
1775 recompute_tree_invariant_for_addr_expr (rhs);
1777 /* Indicate that maybe_clean_or_replace_eh_stmt needs to be called,
1778 even if fold_stmt updated the stmt already and thus cleared
1779 gimple_modified_p flag on it. */
1780 modified_p = true;
1783 /* Check for redundant computations. Do this optimization only
1784 for assignments that have no volatile ops and conditionals. */
1785 may_optimize_p = (!gimple_has_side_effects (stmt)
1786 && (is_gimple_assign (stmt)
1787 || (is_gimple_call (stmt)
1788 && gimple_call_lhs (stmt) != NULL_TREE)
1789 || gimple_code (stmt) == GIMPLE_COND
1790 || gimple_code (stmt) == GIMPLE_SWITCH));
1792 if (may_optimize_p)
1794 if (gimple_code (stmt) == GIMPLE_CALL)
1796 /* Resolve __builtin_constant_p. If it hasn't been
1797 folded to integer_one_node by now, it's fairly
1798 certain that the value simply isn't constant. */
1799 tree callee = gimple_call_fndecl (stmt);
1800 if (callee
1801 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
1802 && DECL_FUNCTION_CODE (callee) == BUILT_IN_CONSTANT_P)
1804 propagate_tree_value_into_stmt (&si, integer_zero_node);
1805 stmt = gsi_stmt (si);
1809 if (gimple_code (stmt) == GIMPLE_COND)
1811 tree lhs = gimple_cond_lhs (stmt);
1812 tree rhs = gimple_cond_rhs (stmt);
1814 /* If the LHS has a range [0..1] and the RHS has a range ~[0..1],
1815 then this conditional is computable at compile time. We can just
1816 shove either 0 or 1 into the LHS, mark the statement as modified
1817 and all the right things will just happen below.
1819 Note this would apply to any case where LHS has a range
1820 narrower than its type implies and RHS is outside that
1821 narrower range. Future work. */
1822 if (TREE_CODE (lhs) == SSA_NAME
1823 && ssa_name_has_boolean_range (lhs)
1824 && TREE_CODE (rhs) == INTEGER_CST
1825 && ! (integer_zerop (rhs) || integer_onep (rhs)))
1827 gimple_cond_set_lhs (as_a <gcond *> (stmt),
1828 fold_convert (TREE_TYPE (lhs),
1829 integer_zero_node));
1830 gimple_set_modified (stmt, true);
1834 update_stmt_if_modified (stmt);
1835 eliminate_redundant_computations (&si, const_and_copies,
1836 avail_exprs_stack);
1837 stmt = gsi_stmt (si);
1839 /* Perform simple redundant store elimination. */
1840 if (gimple_assign_single_p (stmt)
1841 && TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
1843 tree lhs = gimple_assign_lhs (stmt);
1844 tree rhs = gimple_assign_rhs1 (stmt);
1845 tree cached_lhs;
1846 gassign *new_stmt;
1847 rhs = dom_valueize (rhs);
1848 /* Build a new statement with the RHS and LHS exchanged. */
1849 if (TREE_CODE (rhs) == SSA_NAME)
1851 gimple *defstmt = SSA_NAME_DEF_STMT (rhs);
1852 new_stmt = gimple_build_assign (rhs, lhs);
1853 SSA_NAME_DEF_STMT (rhs) = defstmt;
1855 else
1856 new_stmt = gimple_build_assign (rhs, lhs);
1857 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
1858 cached_lhs = avail_exprs_stack->lookup_avail_expr (new_stmt, false,
1859 false);
1860 if (cached_lhs && operand_equal_p (rhs, cached_lhs, 0))
1862 basic_block bb = gimple_bb (stmt);
1863 unlink_stmt_vdef (stmt);
1864 if (gsi_remove (&si, true))
1866 bitmap_set_bit (need_eh_cleanup, bb->index);
1867 if (dump_file && (dump_flags & TDF_DETAILS))
1868 fprintf (dump_file, " Flagged to clear EH edges.\n");
1870 release_defs (stmt);
1871 return retval;
1876 /* Record any additional equivalences created by this statement. */
1877 if (is_gimple_assign (stmt))
1878 record_equivalences_from_stmt (stmt, may_optimize_p, avail_exprs_stack);
1880 /* If STMT is a COND_EXPR or SWITCH_EXPR and it was modified, then we may
1881 know where it goes. */
1882 if (gimple_modified_p (stmt) || modified_p)
1884 tree val = NULL;
1886 if (gimple_code (stmt) == GIMPLE_COND)
1887 val = fold_binary_loc (gimple_location (stmt),
1888 gimple_cond_code (stmt), boolean_type_node,
1889 gimple_cond_lhs (stmt),
1890 gimple_cond_rhs (stmt));
1891 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
1892 val = gimple_switch_index (swtch_stmt);
1894 if (val && TREE_CODE (val) == INTEGER_CST)
1896 retval = find_taken_edge (bb, val);
1897 if (retval)
1899 /* Fix the condition to be either true or false. */
1900 if (gimple_code (stmt) == GIMPLE_COND)
1902 if (integer_zerop (val))
1903 gimple_cond_make_false (as_a <gcond *> (stmt));
1904 else if (integer_onep (val))
1905 gimple_cond_make_true (as_a <gcond *> (stmt));
1906 else
1907 gcc_unreachable ();
1909 gimple_set_modified (stmt, true);
1912 /* Further simplifications may be possible. */
1913 cfg_altered = true;
1917 update_stmt_if_modified (stmt);
1919 /* If we simplified a statement in such a way as to be shown that it
1920 cannot trap, update the eh information and the cfg to match. */
1921 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
1923 bitmap_set_bit (need_eh_cleanup, bb->index);
1924 if (dump_file && (dump_flags & TDF_DETAILS))
1925 fprintf (dump_file, " Flagged to clear EH edges.\n");
1928 if (!was_noreturn
1929 && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
1930 need_noreturn_fixup.safe_push (stmt);
1932 return retval;