[ARM] PR target/78253 Call weak function instead of strong when called through pointer.
[official-gcc.git] / gcc / tree-ssa-dom.c
blob2ec3f97e6d4a132762d558d55e171a4b44730e3a
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 "gimple-iterator.h"
36 #include "tree-cfg.h"
37 #include "tree-into-ssa.h"
38 #include "domwalk.h"
39 #include "tree-ssa-propagate.h"
40 #include "tree-ssa-threadupdate.h"
41 #include "params.h"
42 #include "tree-ssa-scopedtables.h"
43 #include "tree-ssa-threadedge.h"
44 #include "tree-ssa-dom.h"
45 #include "gimplify.h"
46 #include "tree-cfgcleanup.h"
47 #include "dbgcnt.h"
49 /* This file implements optimizations on the dominator tree. */
51 /* Structure for recording known values of a conditional expression
52 at the exits from its block. */
54 struct cond_equivalence
56 struct hashable_expr cond;
57 tree value;
60 /* Structure for recording edge equivalences.
62 Computing and storing the edge equivalences instead of creating
63 them on-demand can save significant amounts of time, particularly
64 for pathological cases involving switch statements.
66 These structures live for a single iteration of the dominator
67 optimizer in the edge's AUX field. At the end of an iteration we
68 free each of these structures. */
70 struct edge_info
72 /* If this edge creates a simple equivalence, the LHS and RHS of
73 the equivalence will be stored here. */
74 tree lhs;
75 tree rhs;
77 /* Traversing an edge may also indicate one or more particular conditions
78 are true or false. */
79 vec<cond_equivalence> cond_equivalences;
82 /* Track whether or not we have changed the control flow graph. */
83 static bool cfg_altered;
85 /* Bitmap of blocks that have had EH statements cleaned. We should
86 remove their dead edges eventually. */
87 static bitmap need_eh_cleanup;
88 static vec<gimple *> need_noreturn_fixup;
90 /* Statistics for dominator optimizations. */
91 struct opt_stats_d
93 long num_stmts;
94 long num_exprs_considered;
95 long num_re;
96 long num_const_prop;
97 long num_copy_prop;
100 static struct opt_stats_d opt_stats;
102 /* Local functions. */
103 static edge optimize_stmt (basic_block, gimple_stmt_iterator,
104 class const_and_copies *,
105 class avail_exprs_stack *);
106 static tree lookup_avail_expr (gimple *, bool, class avail_exprs_stack *,
107 bool = true);
108 static void record_cond (cond_equivalence *, 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> *);
124 /* Free the edge_info data attached to E, if it exists. */
126 void
127 free_dom_edge_info (edge e)
129 struct edge_info *edge_info = (struct edge_info *)e->aux;
131 if (edge_info)
133 edge_info->cond_equivalences.release ();
134 free (edge_info);
138 /* Allocate an EDGE_INFO for edge E and attach it to E.
139 Return the new EDGE_INFO structure. */
141 static struct edge_info *
142 allocate_edge_info (edge e)
144 struct edge_info *edge_info;
146 /* Free the old one, if it exists. */
147 free_dom_edge_info (e);
149 edge_info = XCNEW (struct edge_info);
151 e->aux = edge_info;
152 return edge_info;
155 /* Free all EDGE_INFO structures associated with edges in the CFG.
156 If a particular edge can be threaded, copy the redirection
157 target from the EDGE_INFO structure into the edge's AUX field
158 as required by code to update the CFG and SSA graph for
159 jump threading. */
161 static void
162 free_all_edge_infos (void)
164 basic_block bb;
165 edge_iterator ei;
166 edge e;
168 FOR_EACH_BB_FN (bb, cfun)
170 FOR_EACH_EDGE (e, ei, bb->preds)
172 free_dom_edge_info (e);
173 e->aux = NULL;
178 /* Build a cond_equivalence record indicating that the comparison
179 CODE holds between operands OP0 and OP1 and push it to **P. */
181 static void
182 build_and_record_new_cond (enum tree_code code,
183 tree op0, tree op1,
184 vec<cond_equivalence> *p,
185 bool val = true)
187 cond_equivalence c;
188 struct hashable_expr *cond = &c.cond;
190 gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
192 cond->type = boolean_type_node;
193 cond->kind = EXPR_BINARY;
194 cond->ops.binary.op = code;
195 cond->ops.binary.opnd0 = op0;
196 cond->ops.binary.opnd1 = op1;
198 c.value = val ? boolean_true_node : boolean_false_node;
199 p->safe_push (c);
202 /* Record that COND is true and INVERTED is false into the edge information
203 structure. Also record that any conditions dominated by COND are true
204 as well.
206 For example, if a < b is true, then a <= b must also be true. */
208 static void
209 record_conditions (struct edge_info *edge_info, tree cond, tree inverted)
211 tree op0, op1;
212 cond_equivalence c;
214 if (!COMPARISON_CLASS_P (cond))
215 return;
217 op0 = TREE_OPERAND (cond, 0);
218 op1 = TREE_OPERAND (cond, 1);
220 switch (TREE_CODE (cond))
222 case LT_EXPR:
223 case GT_EXPR:
224 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
226 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
227 &edge_info->cond_equivalences);
228 build_and_record_new_cond (LTGT_EXPR, op0, op1,
229 &edge_info->cond_equivalences);
232 build_and_record_new_cond ((TREE_CODE (cond) == LT_EXPR
233 ? LE_EXPR : GE_EXPR),
234 op0, op1, &edge_info->cond_equivalences);
235 build_and_record_new_cond (NE_EXPR, op0, op1,
236 &edge_info->cond_equivalences);
237 build_and_record_new_cond (EQ_EXPR, op0, op1,
238 &edge_info->cond_equivalences, false);
239 break;
241 case GE_EXPR:
242 case LE_EXPR:
243 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
245 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
246 &edge_info->cond_equivalences);
248 break;
250 case EQ_EXPR:
251 if (FLOAT_TYPE_P (TREE_TYPE (op0)))
253 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
254 &edge_info->cond_equivalences);
256 build_and_record_new_cond (LE_EXPR, op0, op1,
257 &edge_info->cond_equivalences);
258 build_and_record_new_cond (GE_EXPR, op0, op1,
259 &edge_info->cond_equivalences);
260 break;
262 case UNORDERED_EXPR:
263 build_and_record_new_cond (NE_EXPR, op0, op1,
264 &edge_info->cond_equivalences);
265 build_and_record_new_cond (UNLE_EXPR, op0, op1,
266 &edge_info->cond_equivalences);
267 build_and_record_new_cond (UNGE_EXPR, op0, op1,
268 &edge_info->cond_equivalences);
269 build_and_record_new_cond (UNEQ_EXPR, op0, op1,
270 &edge_info->cond_equivalences);
271 build_and_record_new_cond (UNLT_EXPR, op0, op1,
272 &edge_info->cond_equivalences);
273 build_and_record_new_cond (UNGT_EXPR, op0, op1,
274 &edge_info->cond_equivalences);
275 break;
277 case UNLT_EXPR:
278 case UNGT_EXPR:
279 build_and_record_new_cond ((TREE_CODE (cond) == UNLT_EXPR
280 ? UNLE_EXPR : UNGE_EXPR),
281 op0, op1, &edge_info->cond_equivalences);
282 build_and_record_new_cond (NE_EXPR, op0, op1,
283 &edge_info->cond_equivalences);
284 break;
286 case UNEQ_EXPR:
287 build_and_record_new_cond (UNLE_EXPR, op0, op1,
288 &edge_info->cond_equivalences);
289 build_and_record_new_cond (UNGE_EXPR, op0, op1,
290 &edge_info->cond_equivalences);
291 break;
293 case LTGT_EXPR:
294 build_and_record_new_cond (NE_EXPR, op0, op1,
295 &edge_info->cond_equivalences);
296 build_and_record_new_cond (ORDERED_EXPR, op0, op1,
297 &edge_info->cond_equivalences);
298 break;
300 default:
301 break;
304 /* Now store the original true and false conditions into the first
305 two slots. */
306 initialize_expr_from_cond (cond, &c.cond);
307 c.value = boolean_true_node;
308 edge_info->cond_equivalences.safe_push (c);
310 /* It is possible for INVERTED to be the negation of a comparison,
311 and not a valid RHS or GIMPLE_COND condition. This happens because
312 invert_truthvalue may return such an expression when asked to invert
313 a floating-point comparison. These comparisons are not assumed to
314 obey the trichotomy law. */
315 initialize_expr_from_cond (inverted, &c.cond);
316 c.value = boolean_false_node;
317 edge_info->cond_equivalences.safe_push (c);
320 /* We have finished optimizing BB, record any information implied by
321 taking a specific outgoing edge from BB. */
323 static void
324 record_edge_info (basic_block bb)
326 gimple_stmt_iterator gsi = gsi_last_bb (bb);
327 struct edge_info *edge_info;
329 if (! gsi_end_p (gsi))
331 gimple *stmt = gsi_stmt (gsi);
332 location_t loc = gimple_location (stmt);
334 if (gimple_code (stmt) == GIMPLE_SWITCH)
336 gswitch *switch_stmt = as_a <gswitch *> (stmt);
337 tree index = gimple_switch_index (switch_stmt);
339 if (TREE_CODE (index) == SSA_NAME)
341 int i;
342 int n_labels = gimple_switch_num_labels (switch_stmt);
343 tree *info = XCNEWVEC (tree, last_basic_block_for_fn (cfun));
344 edge e;
345 edge_iterator ei;
347 for (i = 0; i < n_labels; i++)
349 tree label = gimple_switch_label (switch_stmt, i);
350 basic_block target_bb = label_to_block (CASE_LABEL (label));
351 if (CASE_HIGH (label)
352 || !CASE_LOW (label)
353 || info[target_bb->index])
354 info[target_bb->index] = error_mark_node;
355 else
356 info[target_bb->index] = label;
359 FOR_EACH_EDGE (e, ei, bb->succs)
361 basic_block target_bb = e->dest;
362 tree label = info[target_bb->index];
364 if (label != NULL && label != error_mark_node)
366 tree x = fold_convert_loc (loc, TREE_TYPE (index),
367 CASE_LOW (label));
368 edge_info = allocate_edge_info (e);
369 edge_info->lhs = index;
370 edge_info->rhs = x;
373 free (info);
377 /* A COND_EXPR may create equivalences too. */
378 if (gimple_code (stmt) == GIMPLE_COND)
380 edge true_edge;
381 edge false_edge;
383 tree op0 = gimple_cond_lhs (stmt);
384 tree op1 = gimple_cond_rhs (stmt);
385 enum tree_code code = gimple_cond_code (stmt);
387 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
389 /* Special case comparing booleans against a constant as we
390 know the value of OP0 on both arms of the branch. i.e., we
391 can record an equivalence for OP0 rather than COND.
393 However, don't do this if the constant isn't zero or one.
394 Such conditionals will get optimized more thoroughly during
395 the domwalk. */
396 if ((code == EQ_EXPR || code == NE_EXPR)
397 && TREE_CODE (op0) == SSA_NAME
398 && ssa_name_has_boolean_range (op0)
399 && is_gimple_min_invariant (op1)
400 && (integer_zerop (op1) || integer_onep (op1)))
402 tree true_val = constant_boolean_node (true, TREE_TYPE (op0));
403 tree false_val = constant_boolean_node (false, TREE_TYPE (op0));
405 if (code == EQ_EXPR)
407 edge_info = allocate_edge_info (true_edge);
408 edge_info->lhs = op0;
409 edge_info->rhs = (integer_zerop (op1) ? false_val : true_val);
411 edge_info = allocate_edge_info (false_edge);
412 edge_info->lhs = op0;
413 edge_info->rhs = (integer_zerop (op1) ? true_val : false_val);
415 else
417 edge_info = allocate_edge_info (true_edge);
418 edge_info->lhs = op0;
419 edge_info->rhs = (integer_zerop (op1) ? true_val : false_val);
421 edge_info = allocate_edge_info (false_edge);
422 edge_info->lhs = op0;
423 edge_info->rhs = (integer_zerop (op1) ? false_val : true_val);
426 else if (is_gimple_min_invariant (op0)
427 && (TREE_CODE (op1) == SSA_NAME
428 || is_gimple_min_invariant (op1)))
430 tree cond = build2 (code, boolean_type_node, op0, op1);
431 tree inverted = invert_truthvalue_loc (loc, cond);
432 bool can_infer_simple_equiv
433 = !(HONOR_SIGNED_ZEROS (op0)
434 && real_zerop (op0));
435 struct edge_info *edge_info;
437 edge_info = allocate_edge_info (true_edge);
438 record_conditions (edge_info, cond, inverted);
440 if (can_infer_simple_equiv && code == EQ_EXPR)
442 edge_info->lhs = op1;
443 edge_info->rhs = op0;
446 edge_info = allocate_edge_info (false_edge);
447 record_conditions (edge_info, inverted, cond);
449 if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
451 edge_info->lhs = op1;
452 edge_info->rhs = op0;
456 else if (TREE_CODE (op0) == SSA_NAME
457 && (TREE_CODE (op1) == SSA_NAME
458 || is_gimple_min_invariant (op1)))
460 tree cond = build2 (code, boolean_type_node, op0, op1);
461 tree inverted = invert_truthvalue_loc (loc, cond);
462 bool can_infer_simple_equiv
463 = !(HONOR_SIGNED_ZEROS (op1)
464 && (TREE_CODE (op1) == SSA_NAME || real_zerop (op1)));
465 struct edge_info *edge_info;
467 edge_info = allocate_edge_info (true_edge);
468 record_conditions (edge_info, cond, inverted);
470 if (can_infer_simple_equiv && code == EQ_EXPR)
472 edge_info->lhs = op0;
473 edge_info->rhs = op1;
476 edge_info = allocate_edge_info (false_edge);
477 record_conditions (edge_info, inverted, cond);
479 if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
481 edge_info->lhs = op0;
482 edge_info->rhs = op1;
487 /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
492 class dom_opt_dom_walker : public dom_walker
494 public:
495 dom_opt_dom_walker (cdi_direction direction,
496 class const_and_copies *const_and_copies,
497 class avail_exprs_stack *avail_exprs_stack)
498 : dom_walker (direction, true),
499 m_const_and_copies (const_and_copies),
500 m_avail_exprs_stack (avail_exprs_stack),
501 m_dummy_cond (NULL) {}
503 virtual edge before_dom_children (basic_block);
504 virtual void after_dom_children (basic_block);
506 private:
507 void thread_across_edge (edge);
509 /* Unwindable equivalences, both const/copy and expression varieties. */
510 class const_and_copies *m_const_and_copies;
511 class avail_exprs_stack *m_avail_exprs_stack;
513 gcond *m_dummy_cond;
516 /* Jump threading, redundancy elimination and const/copy propagation.
518 This pass may expose new symbols that need to be renamed into SSA. For
519 every new symbol exposed, its corresponding bit will be set in
520 VARS_TO_RENAME. */
522 namespace {
524 const pass_data pass_data_dominator =
526 GIMPLE_PASS, /* type */
527 "dom", /* name */
528 OPTGROUP_NONE, /* optinfo_flags */
529 TV_TREE_SSA_DOMINATOR_OPTS, /* tv_id */
530 ( PROP_cfg | PROP_ssa ), /* properties_required */
531 0, /* properties_provided */
532 0, /* properties_destroyed */
533 0, /* todo_flags_start */
534 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
537 class pass_dominator : public gimple_opt_pass
539 public:
540 pass_dominator (gcc::context *ctxt)
541 : gimple_opt_pass (pass_data_dominator, ctxt),
542 may_peel_loop_headers_p (false)
545 /* opt_pass methods: */
546 opt_pass * clone () { return new pass_dominator (m_ctxt); }
547 void set_pass_param (unsigned int n, bool param)
549 gcc_assert (n == 0);
550 may_peel_loop_headers_p = param;
552 virtual bool gate (function *) { return flag_tree_dom != 0; }
553 virtual unsigned int execute (function *);
555 private:
556 /* This flag is used to prevent loops from being peeled repeatedly in jump
557 threading; it will be removed once we preserve loop structures throughout
558 the compilation -- we will be able to mark the affected loops directly in
559 jump threading, and avoid peeling them next time. */
560 bool may_peel_loop_headers_p;
561 }; // class pass_dominator
563 unsigned int
564 pass_dominator::execute (function *fun)
566 memset (&opt_stats, 0, sizeof (opt_stats));
568 /* Create our hash tables. */
569 hash_table<expr_elt_hasher> *avail_exprs
570 = new hash_table<expr_elt_hasher> (1024);
571 class avail_exprs_stack *avail_exprs_stack
572 = new class avail_exprs_stack (avail_exprs);
573 class const_and_copies *const_and_copies = new class const_and_copies ();
574 need_eh_cleanup = BITMAP_ALLOC (NULL);
575 need_noreturn_fixup.create (0);
577 calculate_dominance_info (CDI_DOMINATORS);
578 cfg_altered = false;
580 /* We need to know loop structures in order to avoid destroying them
581 in jump threading. Note that we still can e.g. thread through loop
582 headers to an exit edge, or through loop header to the loop body, assuming
583 that we update the loop info.
585 TODO: We don't need to set LOOPS_HAVE_PREHEADERS generally, but due
586 to several overly conservative bail-outs in jump threading, case
587 gcc.dg/tree-ssa/pr21417.c can't be threaded if loop preheader is
588 missing. We should improve jump threading in future then
589 LOOPS_HAVE_PREHEADERS won't be needed here. */
590 loop_optimizer_init (LOOPS_HAVE_PREHEADERS | LOOPS_HAVE_SIMPLE_LATCHES);
592 /* Initialize the value-handle array. */
593 threadedge_initialize_values ();
595 /* We need accurate information regarding back edges in the CFG
596 for jump threading; this may include back edges that are not part of
597 a single loop. */
598 mark_dfs_back_edges ();
600 /* We want to create the edge info structures before the dominator walk
601 so that they'll be in place for the jump threader, particularly when
602 threading through a join block.
604 The conditions will be lazily updated with global equivalences as
605 we reach them during the dominator walk. */
606 basic_block bb;
607 FOR_EACH_BB_FN (bb, fun)
608 record_edge_info (bb);
610 /* Recursively walk the dominator tree optimizing statements. */
611 dom_opt_dom_walker walker (CDI_DOMINATORS,
612 const_and_copies,
613 avail_exprs_stack);
614 walker.walk (fun->cfg->x_entry_block_ptr);
616 /* Look for blocks where we cleared EDGE_EXECUTABLE on an outgoing
617 edge. When found, remove jump threads which contain any outgoing
618 edge from the affected block. */
619 if (cfg_altered)
621 FOR_EACH_BB_FN (bb, fun)
623 edge_iterator ei;
624 edge e;
626 /* First see if there are any edges without EDGE_EXECUTABLE
627 set. */
628 bool found = false;
629 FOR_EACH_EDGE (e, ei, bb->succs)
631 if ((e->flags & EDGE_EXECUTABLE) == 0)
633 found = true;
634 break;
638 /* If there were any such edges found, then remove jump threads
639 containing any edge leaving BB. */
640 if (found)
641 FOR_EACH_EDGE (e, ei, bb->succs)
642 remove_jump_threads_including (e);
647 gimple_stmt_iterator gsi;
648 basic_block bb;
649 FOR_EACH_BB_FN (bb, fun)
651 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
652 update_stmt_if_modified (gsi_stmt (gsi));
656 /* If we exposed any new variables, go ahead and put them into
657 SSA form now, before we handle jump threading. This simplifies
658 interactions between rewriting of _DECL nodes into SSA form
659 and rewriting SSA_NAME nodes into SSA form after block
660 duplication and CFG manipulation. */
661 update_ssa (TODO_update_ssa);
663 free_all_edge_infos ();
665 /* Thread jumps, creating duplicate blocks as needed. */
666 cfg_altered |= thread_through_all_blocks (may_peel_loop_headers_p);
668 if (cfg_altered)
669 free_dominance_info (CDI_DOMINATORS);
671 /* Removal of statements may make some EH edges dead. Purge
672 such edges from the CFG as needed. */
673 if (!bitmap_empty_p (need_eh_cleanup))
675 unsigned i;
676 bitmap_iterator bi;
678 /* Jump threading may have created forwarder blocks from blocks
679 needing EH cleanup; the new successor of these blocks, which
680 has inherited from the original block, needs the cleanup.
681 Don't clear bits in the bitmap, as that can break the bitmap
682 iterator. */
683 EXECUTE_IF_SET_IN_BITMAP (need_eh_cleanup, 0, i, bi)
685 basic_block bb = BASIC_BLOCK_FOR_FN (fun, i);
686 if (bb == NULL)
687 continue;
688 while (single_succ_p (bb)
689 && (single_succ_edge (bb)->flags & EDGE_EH) == 0)
690 bb = single_succ (bb);
691 if (bb == EXIT_BLOCK_PTR_FOR_FN (fun))
692 continue;
693 if ((unsigned) bb->index != i)
694 bitmap_set_bit (need_eh_cleanup, bb->index);
697 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
698 bitmap_clear (need_eh_cleanup);
701 /* Fixup stmts that became noreturn calls. This may require splitting
702 blocks and thus isn't possible during the dominator walk or before
703 jump threading finished. Do this in reverse order so we don't
704 inadvertedly remove a stmt we want to fixup by visiting a dominating
705 now noreturn call first. */
706 while (!need_noreturn_fixup.is_empty ())
708 gimple *stmt = need_noreturn_fixup.pop ();
709 if (dump_file && dump_flags & TDF_DETAILS)
711 fprintf (dump_file, "Fixing up noreturn call ");
712 print_gimple_stmt (dump_file, stmt, 0, 0);
713 fprintf (dump_file, "\n");
715 fixup_noreturn_call (stmt);
718 statistics_counter_event (fun, "Redundant expressions eliminated",
719 opt_stats.num_re);
720 statistics_counter_event (fun, "Constants propagated",
721 opt_stats.num_const_prop);
722 statistics_counter_event (fun, "Copies propagated",
723 opt_stats.num_copy_prop);
725 /* Debugging dumps. */
726 if (dump_file && (dump_flags & TDF_STATS))
727 dump_dominator_optimization_stats (dump_file, avail_exprs);
729 loop_optimizer_finalize ();
731 /* Delete our main hashtable. */
732 delete avail_exprs;
733 avail_exprs = NULL;
735 /* Free asserted bitmaps and stacks. */
736 BITMAP_FREE (need_eh_cleanup);
737 need_noreturn_fixup.release ();
738 delete avail_exprs_stack;
739 delete const_and_copies;
741 /* Free the value-handle array. */
742 threadedge_finalize_values ();
744 return 0;
747 } // anon namespace
749 gimple_opt_pass *
750 make_pass_dominator (gcc::context *ctxt)
752 return new pass_dominator (ctxt);
756 /* A trivial wrapper so that we can present the generic jump
757 threading code with a simple API for simplifying statements. */
758 static tree
759 simplify_stmt_for_jump_threading (gimple *stmt,
760 gimple *within_stmt ATTRIBUTE_UNUSED,
761 class avail_exprs_stack *avail_exprs_stack)
763 return lookup_avail_expr (stmt, false, avail_exprs_stack);
766 /* Valueize hook for gimple_fold_stmt_to_constant_1. */
768 static tree
769 dom_valueize (tree t)
771 if (TREE_CODE (t) == SSA_NAME)
773 tree tem = SSA_NAME_VALUE (t);
774 if (tem)
775 return tem;
777 return t;
780 /* We have just found an equivalence for LHS on an edge E.
781 Look backwards to other uses of LHS and see if we can derive
782 additional equivalences that are valid on edge E. */
783 static void
784 back_propagate_equivalences (tree lhs, edge e,
785 class const_and_copies *const_and_copies)
787 use_operand_p use_p;
788 imm_use_iterator iter;
789 bitmap domby = NULL;
790 basic_block dest = e->dest;
792 /* Iterate over the uses of LHS to see if any dominate E->dest.
793 If so, they may create useful equivalences too.
795 ??? If the code gets re-organized to a worklist to catch more
796 indirect opportunities and it is made to handle PHIs then this
797 should only consider use_stmts in basic-blocks we have already visited. */
798 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
800 gimple *use_stmt = USE_STMT (use_p);
802 /* Often the use is in DEST, which we trivially know we can't use.
803 This is cheaper than the dominator set tests below. */
804 if (dest == gimple_bb (use_stmt))
805 continue;
807 /* Filter out statements that can never produce a useful
808 equivalence. */
809 tree lhs2 = gimple_get_lhs (use_stmt);
810 if (!lhs2 || TREE_CODE (lhs2) != SSA_NAME)
811 continue;
813 /* Profiling has shown the domination tests here can be fairly
814 expensive. We get significant improvements by building the
815 set of blocks that dominate BB. We can then just test
816 for set membership below.
818 We also initialize the set lazily since often the only uses
819 are going to be in the same block as DEST. */
820 if (!domby)
822 domby = BITMAP_ALLOC (NULL);
823 basic_block bb = get_immediate_dominator (CDI_DOMINATORS, dest);
824 while (bb)
826 bitmap_set_bit (domby, bb->index);
827 bb = get_immediate_dominator (CDI_DOMINATORS, bb);
831 /* This tests if USE_STMT does not dominate DEST. */
832 if (!bitmap_bit_p (domby, gimple_bb (use_stmt)->index))
833 continue;
835 /* At this point USE_STMT dominates DEST and may result in a
836 useful equivalence. Try to simplify its RHS to a constant
837 or SSA_NAME. */
838 tree res = gimple_fold_stmt_to_constant_1 (use_stmt, dom_valueize,
839 no_follow_ssa_edges);
840 if (res && (TREE_CODE (res) == SSA_NAME || is_gimple_min_invariant (res)))
841 record_equality (lhs2, res, const_and_copies);
844 if (domby)
845 BITMAP_FREE (domby);
848 /* Record into CONST_AND_COPIES and AVAIL_EXPRS_STACK any equivalences implied
849 by traversing edge E (which are cached in E->aux).
851 Callers are responsible for managing the unwinding markers. */
852 void
853 record_temporary_equivalences (edge e,
854 class const_and_copies *const_and_copies,
855 class avail_exprs_stack *avail_exprs_stack)
857 int i;
858 struct edge_info *edge_info = (struct edge_info *) e->aux;
860 /* If we have info associated with this edge, record it into
861 our equivalence tables. */
862 if (edge_info)
864 cond_equivalence *eq;
865 /* If we have 0 = COND or 1 = COND equivalences, record them
866 into our expression hash tables. */
867 for (i = 0; edge_info->cond_equivalences.iterate (i, &eq); ++i)
868 record_cond (eq, avail_exprs_stack);
870 tree lhs = edge_info->lhs;
871 if (!lhs || TREE_CODE (lhs) != SSA_NAME)
872 return;
874 /* Record the simple NAME = VALUE equivalence. */
875 tree rhs = edge_info->rhs;
876 record_equality (lhs, rhs, const_and_copies);
878 /* We already recorded that LHS = RHS, with canonicalization,
879 value chain following, etc.
881 We also want to record RHS = LHS, but without any canonicalization
882 or value chain following. */
883 if (TREE_CODE (rhs) == SSA_NAME)
884 const_and_copies->record_const_or_copy_raw (rhs, lhs,
885 SSA_NAME_VALUE (rhs));
887 /* If LHS is an SSA_NAME and RHS is a constant integer and LHS was
888 set via a widening type conversion, then we may be able to record
889 additional equivalences. */
890 if (TREE_CODE (rhs) == INTEGER_CST)
892 gimple *defstmt = SSA_NAME_DEF_STMT (lhs);
894 if (defstmt
895 && is_gimple_assign (defstmt)
896 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (defstmt)))
898 tree old_rhs = gimple_assign_rhs1 (defstmt);
900 /* If the conversion widens the original value and
901 the constant is in the range of the type of OLD_RHS,
902 then convert the constant and record the equivalence.
904 Note that int_fits_type_p does not check the precision
905 if the upper and lower bounds are OK. */
906 if (INTEGRAL_TYPE_P (TREE_TYPE (old_rhs))
907 && (TYPE_PRECISION (TREE_TYPE (lhs))
908 > TYPE_PRECISION (TREE_TYPE (old_rhs)))
909 && int_fits_type_p (rhs, TREE_TYPE (old_rhs)))
911 tree newval = fold_convert (TREE_TYPE (old_rhs), rhs);
912 record_equality (old_rhs, newval, const_and_copies);
917 /* Any equivalence found for LHS may result in additional
918 equivalences for other uses of LHS that we have already
919 processed. */
920 back_propagate_equivalences (lhs, e, const_and_copies);
924 /* Wrapper for common code to attempt to thread an edge. For example,
925 it handles lazily building the dummy condition and the bookkeeping
926 when jump threading is successful. */
928 void
929 dom_opt_dom_walker::thread_across_edge (edge e)
931 if (! m_dummy_cond)
932 m_dummy_cond =
933 gimple_build_cond (NE_EXPR,
934 integer_zero_node, integer_zero_node,
935 NULL, NULL);
937 /* Push a marker on both stacks so we can unwind the tables back to their
938 current state. */
939 m_avail_exprs_stack->push_marker ();
940 m_const_and_copies->push_marker ();
942 /* With all the edge equivalences in the tables, go ahead and attempt
943 to thread through E->dest. */
944 ::thread_across_edge (m_dummy_cond, e, false,
945 m_const_and_copies, m_avail_exprs_stack,
946 simplify_stmt_for_jump_threading);
948 /* And restore the various tables to their state before
949 we threaded this edge.
951 XXX The code in tree-ssa-threadedge.c will restore the state of
952 the const_and_copies table. We we just have to restore the expression
953 table. */
954 m_avail_exprs_stack->pop_to_marker ();
957 /* PHI nodes can create equivalences too.
959 Ignoring any alternatives which are the same as the result, if
960 all the alternatives are equal, then the PHI node creates an
961 equivalence. */
963 static void
964 record_equivalences_from_phis (basic_block bb)
966 gphi_iterator gsi;
968 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
970 gphi *phi = gsi.phi ();
972 tree lhs = gimple_phi_result (phi);
973 tree rhs = NULL;
974 size_t i;
976 for (i = 0; i < gimple_phi_num_args (phi); i++)
978 tree t = gimple_phi_arg_def (phi, i);
980 /* Ignore alternatives which are the same as our LHS. Since
981 LHS is a PHI_RESULT, it is known to be a SSA_NAME, so we
982 can simply compare pointers. */
983 if (lhs == t)
984 continue;
986 /* If the associated edge is not marked as executable, then it
987 can be ignored. */
988 if ((gimple_phi_arg_edge (phi, i)->flags & EDGE_EXECUTABLE) == 0)
989 continue;
991 t = dom_valueize (t);
993 /* If we have not processed an alternative yet, then set
994 RHS to this alternative. */
995 if (rhs == NULL)
996 rhs = t;
997 /* If we have processed an alternative (stored in RHS), then
998 see if it is equal to this one. If it isn't, then stop
999 the search. */
1000 else if (! operand_equal_for_phi_arg_p (rhs, t))
1001 break;
1004 /* If we had no interesting alternatives, then all the RHS alternatives
1005 must have been the same as LHS. */
1006 if (!rhs)
1007 rhs = lhs;
1009 /* If we managed to iterate through each PHI alternative without
1010 breaking out of the loop, then we have a PHI which may create
1011 a useful equivalence. We do not need to record unwind data for
1012 this, since this is a true assignment and not an equivalence
1013 inferred from a comparison. All uses of this ssa name are dominated
1014 by this assignment, so unwinding just costs time and space. */
1015 if (i == gimple_phi_num_args (phi)
1016 && may_propagate_copy (lhs, rhs))
1017 set_ssa_name_value (lhs, rhs);
1021 /* Ignoring loop backedges, if BB has precisely one incoming edge then
1022 return that edge. Otherwise return NULL. */
1023 static edge
1024 single_incoming_edge_ignoring_loop_edges (basic_block bb)
1026 edge retval = NULL;
1027 edge e;
1028 edge_iterator ei;
1030 FOR_EACH_EDGE (e, ei, bb->preds)
1032 /* A loop back edge can be identified by the destination of
1033 the edge dominating the source of the edge. */
1034 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
1035 continue;
1037 /* We can safely ignore edges that are not executable. */
1038 if ((e->flags & EDGE_EXECUTABLE) == 0)
1039 continue;
1041 /* If we have already seen a non-loop edge, then we must have
1042 multiple incoming non-loop edges and thus we return NULL. */
1043 if (retval)
1044 return NULL;
1046 /* This is the first non-loop incoming edge we have found. Record
1047 it. */
1048 retval = e;
1051 return retval;
1054 /* Record any equivalences created by the incoming edge to BB into
1055 CONST_AND_COPIES and AVAIL_EXPRS_STACK. If BB has more than one
1056 incoming edge, then no equivalence is created. */
1058 static void
1059 record_equivalences_from_incoming_edge (basic_block bb,
1060 class const_and_copies *const_and_copies,
1061 class avail_exprs_stack *avail_exprs_stack)
1063 edge e;
1064 basic_block parent;
1066 /* If our parent block ended with a control statement, then we may be
1067 able to record some equivalences based on which outgoing edge from
1068 the parent was followed. */
1069 parent = get_immediate_dominator (CDI_DOMINATORS, bb);
1071 e = single_incoming_edge_ignoring_loop_edges (bb);
1073 /* If we had a single incoming edge from our parent block, then enter
1074 any data associated with the edge into our tables. */
1075 if (e && e->src == parent)
1076 record_temporary_equivalences (e, const_and_copies, avail_exprs_stack);
1079 /* Dump statistics for the hash table HTAB. */
1081 static void
1082 htab_statistics (FILE *file, const hash_table<expr_elt_hasher> &htab)
1084 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
1085 (long) htab.size (),
1086 (long) htab.elements (),
1087 htab.collisions ());
1090 /* Dump SSA statistics on FILE. */
1092 static void
1093 dump_dominator_optimization_stats (FILE *file,
1094 hash_table<expr_elt_hasher> *avail_exprs)
1096 fprintf (file, "Total number of statements: %6ld\n\n",
1097 opt_stats.num_stmts);
1098 fprintf (file, "Exprs considered for dominator optimizations: %6ld\n",
1099 opt_stats.num_exprs_considered);
1101 fprintf (file, "\nHash table statistics:\n");
1103 fprintf (file, " avail_exprs: ");
1104 htab_statistics (file, *avail_exprs);
1108 /* Enter condition equivalence P into AVAIL_EXPRS_HASH.
1110 This indicates that a conditional expression has a known
1111 boolean value. */
1113 static void
1114 record_cond (cond_equivalence *p,
1115 class avail_exprs_stack *avail_exprs_stack)
1117 class expr_hash_elt *element = new expr_hash_elt (&p->cond, p->value);
1118 expr_hash_elt **slot;
1120 hash_table<expr_elt_hasher> *avail_exprs = avail_exprs_stack->avail_exprs ();
1121 slot = avail_exprs->find_slot_with_hash (element, element->hash (), INSERT);
1122 if (*slot == NULL)
1124 *slot = element;
1125 avail_exprs_stack->record_expr (element, NULL, '1');
1127 else
1128 delete element;
1131 /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1132 This constrains the cases in which we may treat this as assignment. */
1134 static void
1135 record_equality (tree x, tree y, class const_and_copies *const_and_copies)
1137 tree prev_x = NULL, prev_y = NULL;
1139 if (tree_swap_operands_p (x, y))
1140 std::swap (x, y);
1142 /* Most of the time tree_swap_operands_p does what we want. But there
1143 are cases where we know one operand is better for copy propagation than
1144 the other. Given no other code cares about ordering of equality
1145 comparison operators for that purpose, we just handle the special cases
1146 here. */
1147 if (TREE_CODE (x) == SSA_NAME && TREE_CODE (y) == SSA_NAME)
1149 /* If one operand is a single use operand, then make it
1150 X. This will preserve its single use properly and if this
1151 conditional is eliminated, the computation of X can be
1152 eliminated as well. */
1153 if (has_single_use (y) && ! has_single_use (x))
1154 std::swap (x, y);
1156 if (TREE_CODE (x) == SSA_NAME)
1157 prev_x = SSA_NAME_VALUE (x);
1158 if (TREE_CODE (y) == SSA_NAME)
1159 prev_y = SSA_NAME_VALUE (y);
1161 /* If one of the previous values is invariant, or invariant in more loops
1162 (by depth), then use that.
1163 Otherwise it doesn't matter which value we choose, just so
1164 long as we canonicalize on one value. */
1165 if (is_gimple_min_invariant (y))
1167 else if (is_gimple_min_invariant (x))
1168 prev_x = x, x = y, y = prev_x, prev_x = prev_y;
1169 else if (prev_x && is_gimple_min_invariant (prev_x))
1170 x = y, y = prev_x, prev_x = prev_y;
1171 else if (prev_y)
1172 y = prev_y;
1174 /* After the swapping, we must have one SSA_NAME. */
1175 if (TREE_CODE (x) != SSA_NAME)
1176 return;
1178 /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1179 variable compared against zero. If we're honoring signed zeros,
1180 then we cannot record this value unless we know that the value is
1181 nonzero. */
1182 if (HONOR_SIGNED_ZEROS (x)
1183 && (TREE_CODE (y) != REAL_CST
1184 || real_equal (&dconst0, &TREE_REAL_CST (y))))
1185 return;
1187 const_and_copies->record_const_or_copy (x, y, prev_x);
1190 /* Returns true when STMT is a simple iv increment. It detects the
1191 following situation:
1193 i_1 = phi (..., i_2)
1194 i_2 = i_1 +/- ... */
1196 bool
1197 simple_iv_increment_p (gimple *stmt)
1199 enum tree_code code;
1200 tree lhs, preinc;
1201 gimple *phi;
1202 size_t i;
1204 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1205 return false;
1207 lhs = gimple_assign_lhs (stmt);
1208 if (TREE_CODE (lhs) != SSA_NAME)
1209 return false;
1211 code = gimple_assign_rhs_code (stmt);
1212 if (code != PLUS_EXPR
1213 && code != MINUS_EXPR
1214 && code != POINTER_PLUS_EXPR)
1215 return false;
1217 preinc = gimple_assign_rhs1 (stmt);
1218 if (TREE_CODE (preinc) != SSA_NAME)
1219 return false;
1221 phi = SSA_NAME_DEF_STMT (preinc);
1222 if (gimple_code (phi) != GIMPLE_PHI)
1223 return false;
1225 for (i = 0; i < gimple_phi_num_args (phi); i++)
1226 if (gimple_phi_arg_def (phi, i) == lhs)
1227 return true;
1229 return false;
1232 /* Propagate know values from SSA_NAME_VALUE into the PHI nodes of the
1233 successors of BB. */
1235 static void
1236 cprop_into_successor_phis (basic_block bb,
1237 class const_and_copies *const_and_copies)
1239 edge e;
1240 edge_iterator ei;
1242 FOR_EACH_EDGE (e, ei, bb->succs)
1244 int indx;
1245 gphi_iterator gsi;
1247 /* If this is an abnormal edge, then we do not want to copy propagate
1248 into the PHI alternative associated with this edge. */
1249 if (e->flags & EDGE_ABNORMAL)
1250 continue;
1252 gsi = gsi_start_phis (e->dest);
1253 if (gsi_end_p (gsi))
1254 continue;
1256 /* We may have an equivalence associated with this edge. While
1257 we can not propagate it into non-dominated blocks, we can
1258 propagate them into PHIs in non-dominated blocks. */
1260 /* Push the unwind marker so we can reset the const and copies
1261 table back to its original state after processing this edge. */
1262 const_and_copies->push_marker ();
1264 /* Extract and record any simple NAME = VALUE equivalences.
1266 Don't bother with [01] = COND equivalences, they're not useful
1267 here. */
1268 struct edge_info *edge_info = (struct edge_info *) e->aux;
1269 if (edge_info)
1271 tree lhs = edge_info->lhs;
1272 tree rhs = edge_info->rhs;
1274 if (lhs && TREE_CODE (lhs) == SSA_NAME)
1275 const_and_copies->record_const_or_copy (lhs, rhs);
1278 indx = e->dest_idx;
1279 for ( ; !gsi_end_p (gsi); gsi_next (&gsi))
1281 tree new_val;
1282 use_operand_p orig_p;
1283 tree orig_val;
1284 gphi *phi = gsi.phi ();
1286 /* The alternative may be associated with a constant, so verify
1287 it is an SSA_NAME before doing anything with it. */
1288 orig_p = gimple_phi_arg_imm_use_ptr (phi, indx);
1289 orig_val = get_use_from_ptr (orig_p);
1290 if (TREE_CODE (orig_val) != SSA_NAME)
1291 continue;
1293 /* If we have *ORIG_P in our constant/copy table, then replace
1294 ORIG_P with its value in our constant/copy table. */
1295 new_val = SSA_NAME_VALUE (orig_val);
1296 if (new_val
1297 && new_val != orig_val
1298 && may_propagate_copy (orig_val, new_val))
1299 propagate_value (orig_p, new_val);
1302 const_and_copies->pop_to_marker ();
1306 edge
1307 dom_opt_dom_walker::before_dom_children (basic_block bb)
1309 gimple_stmt_iterator gsi;
1311 if (dump_file && (dump_flags & TDF_DETAILS))
1312 fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
1314 /* Push a marker on the stacks of local information so that we know how
1315 far to unwind when we finalize this block. */
1316 m_avail_exprs_stack->push_marker ();
1317 m_const_and_copies->push_marker ();
1319 record_equivalences_from_incoming_edge (bb, m_const_and_copies,
1320 m_avail_exprs_stack);
1322 /* PHI nodes can create equivalences too. */
1323 record_equivalences_from_phis (bb);
1325 /* Create equivalences from redundant PHIs. PHIs are only truly
1326 redundant when they exist in the same block, so push another
1327 marker and unwind right afterwards. */
1328 m_avail_exprs_stack->push_marker ();
1329 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1330 eliminate_redundant_computations (&gsi, m_const_and_copies,
1331 m_avail_exprs_stack);
1332 m_avail_exprs_stack->pop_to_marker ();
1334 edge taken_edge = NULL;
1335 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1336 taken_edge
1337 = optimize_stmt (bb, gsi, m_const_and_copies, m_avail_exprs_stack);
1339 /* Now prepare to process dominated blocks. */
1340 record_edge_info (bb);
1341 cprop_into_successor_phis (bb, m_const_and_copies);
1342 if (taken_edge && !dbg_cnt (dom_unreachable_edges))
1343 return NULL;
1345 return taken_edge;
1348 /* We have finished processing the dominator children of BB, perform
1349 any finalization actions in preparation for leaving this node in
1350 the dominator tree. */
1352 void
1353 dom_opt_dom_walker::after_dom_children (basic_block bb)
1355 gimple *last;
1357 /* If we have an outgoing edge to a block with multiple incoming and
1358 outgoing edges, then we may be able to thread the edge, i.e., we
1359 may be able to statically determine which of the outgoing edges
1360 will be traversed when the incoming edge from BB is traversed. */
1361 if (single_succ_p (bb)
1362 && (single_succ_edge (bb)->flags & EDGE_ABNORMAL) == 0
1363 && potentially_threadable_block (single_succ (bb)))
1365 thread_across_edge (single_succ_edge (bb));
1367 else if ((last = last_stmt (bb))
1368 && gimple_code (last) == GIMPLE_COND
1369 && EDGE_COUNT (bb->succs) == 2
1370 && (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL) == 0
1371 && (EDGE_SUCC (bb, 1)->flags & EDGE_ABNORMAL) == 0)
1373 edge true_edge, false_edge;
1375 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1377 /* Only try to thread the edge if it reaches a target block with
1378 more than one predecessor and more than one successor. */
1379 if (potentially_threadable_block (true_edge->dest))
1380 thread_across_edge (true_edge);
1382 /* Similarly for the ELSE arm. */
1383 if (potentially_threadable_block (false_edge->dest))
1384 thread_across_edge (false_edge);
1388 /* These remove expressions local to BB from the tables. */
1389 m_avail_exprs_stack->pop_to_marker ();
1390 m_const_and_copies->pop_to_marker ();
1393 /* Search for redundant computations in STMT. If any are found, then
1394 replace them with the variable holding the result of the computation.
1396 If safe, record this expression into AVAIL_EXPRS_STACK and
1397 CONST_AND_COPIES. */
1399 static void
1400 eliminate_redundant_computations (gimple_stmt_iterator* gsi,
1401 class const_and_copies *const_and_copies,
1402 class avail_exprs_stack *avail_exprs_stack)
1404 tree expr_type;
1405 tree cached_lhs;
1406 tree def;
1407 bool insert = true;
1408 bool assigns_var_p = false;
1410 gimple *stmt = gsi_stmt (*gsi);
1412 if (gimple_code (stmt) == GIMPLE_PHI)
1413 def = gimple_phi_result (stmt);
1414 else
1415 def = gimple_get_lhs (stmt);
1417 /* Certain expressions on the RHS can be optimized away, but can not
1418 themselves be entered into the hash tables. */
1419 if (! def
1420 || TREE_CODE (def) != SSA_NAME
1421 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)
1422 || gimple_vdef (stmt)
1423 /* Do not record equivalences for increments of ivs. This would create
1424 overlapping live ranges for a very questionable gain. */
1425 || simple_iv_increment_p (stmt))
1426 insert = false;
1428 /* Check if the expression has been computed before. */
1429 cached_lhs = lookup_avail_expr (stmt, insert, avail_exprs_stack);
1431 opt_stats.num_exprs_considered++;
1433 /* Get the type of the expression we are trying to optimize. */
1434 if (is_gimple_assign (stmt))
1436 expr_type = TREE_TYPE (gimple_assign_lhs (stmt));
1437 assigns_var_p = true;
1439 else if (gimple_code (stmt) == GIMPLE_COND)
1440 expr_type = boolean_type_node;
1441 else if (is_gimple_call (stmt))
1443 gcc_assert (gimple_call_lhs (stmt));
1444 expr_type = TREE_TYPE (gimple_call_lhs (stmt));
1445 assigns_var_p = true;
1447 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
1448 expr_type = TREE_TYPE (gimple_switch_index (swtch_stmt));
1449 else if (gimple_code (stmt) == GIMPLE_PHI)
1450 /* We can't propagate into a phi, so the logic below doesn't apply.
1451 Instead record an equivalence between the cached LHS and the
1452 PHI result of this statement, provided they are in the same block.
1453 This should be sufficient to kill the redundant phi. */
1455 if (def && cached_lhs)
1456 const_and_copies->record_const_or_copy (def, cached_lhs);
1457 return;
1459 else
1460 gcc_unreachable ();
1462 if (!cached_lhs)
1463 return;
1465 /* It is safe to ignore types here since we have already done
1466 type checking in the hashing and equality routines. In fact
1467 type checking here merely gets in the way of constant
1468 propagation. Also, make sure that it is safe to propagate
1469 CACHED_LHS into the expression in STMT. */
1470 if ((TREE_CODE (cached_lhs) != SSA_NAME
1471 && (assigns_var_p
1472 || useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs))))
1473 || may_propagate_copy_into_stmt (stmt, cached_lhs))
1475 gcc_checking_assert (TREE_CODE (cached_lhs) == SSA_NAME
1476 || is_gimple_min_invariant (cached_lhs));
1478 if (dump_file && (dump_flags & TDF_DETAILS))
1480 fprintf (dump_file, " Replaced redundant expr '");
1481 print_gimple_expr (dump_file, stmt, 0, dump_flags);
1482 fprintf (dump_file, "' with '");
1483 print_generic_expr (dump_file, cached_lhs, dump_flags);
1484 fprintf (dump_file, "'\n");
1487 opt_stats.num_re++;
1489 if (assigns_var_p
1490 && !useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs)))
1491 cached_lhs = fold_convert (expr_type, cached_lhs);
1493 propagate_tree_value_into_stmt (gsi, cached_lhs);
1495 /* Since it is always necessary to mark the result as modified,
1496 perhaps we should move this into propagate_tree_value_into_stmt
1497 itself. */
1498 gimple_set_modified (gsi_stmt (*gsi), true);
1502 /* STMT, a GIMPLE_ASSIGN, may create certain equivalences, in either
1503 the available expressions table or the const_and_copies table.
1504 Detect and record those equivalences into AVAIL_EXPRS_STACK.
1506 We handle only very simple copy equivalences here. The heavy
1507 lifing is done by eliminate_redundant_computations. */
1509 static void
1510 record_equivalences_from_stmt (gimple *stmt, int may_optimize_p,
1511 class avail_exprs_stack *avail_exprs_stack)
1513 tree lhs;
1514 enum tree_code lhs_code;
1516 gcc_assert (is_gimple_assign (stmt));
1518 lhs = gimple_assign_lhs (stmt);
1519 lhs_code = TREE_CODE (lhs);
1521 if (lhs_code == SSA_NAME
1522 && gimple_assign_single_p (stmt))
1524 tree rhs = gimple_assign_rhs1 (stmt);
1526 /* If the RHS of the assignment is a constant or another variable that
1527 may be propagated, register it in the CONST_AND_COPIES table. We
1528 do not need to record unwind data for this, since this is a true
1529 assignment and not an equivalence inferred from a comparison. All
1530 uses of this ssa name are dominated by this assignment, so unwinding
1531 just costs time and space. */
1532 if (may_optimize_p
1533 && (TREE_CODE (rhs) == SSA_NAME
1534 || is_gimple_min_invariant (rhs)))
1536 rhs = dom_valueize (rhs);
1538 if (dump_file && (dump_flags & TDF_DETAILS))
1540 fprintf (dump_file, "==== ASGN ");
1541 print_generic_expr (dump_file, lhs, 0);
1542 fprintf (dump_file, " = ");
1543 print_generic_expr (dump_file, rhs, 0);
1544 fprintf (dump_file, "\n");
1547 set_ssa_name_value (lhs, rhs);
1551 /* Make sure we can propagate &x + CST. */
1552 if (lhs_code == SSA_NAME
1553 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
1554 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR
1555 && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)
1557 tree op0 = gimple_assign_rhs1 (stmt);
1558 tree op1 = gimple_assign_rhs2 (stmt);
1559 tree new_rhs
1560 = build_fold_addr_expr (fold_build2 (MEM_REF,
1561 TREE_TYPE (TREE_TYPE (op0)),
1562 unshare_expr (op0),
1563 fold_convert (ptr_type_node,
1564 op1)));
1565 if (dump_file && (dump_flags & TDF_DETAILS))
1567 fprintf (dump_file, "==== ASGN ");
1568 print_generic_expr (dump_file, lhs, 0);
1569 fprintf (dump_file, " = ");
1570 print_generic_expr (dump_file, new_rhs, 0);
1571 fprintf (dump_file, "\n");
1574 set_ssa_name_value (lhs, new_rhs);
1577 /* A memory store, even an aliased store, creates a useful
1578 equivalence. By exchanging the LHS and RHS, creating suitable
1579 vops and recording the result in the available expression table,
1580 we may be able to expose more redundant loads. */
1581 if (!gimple_has_volatile_ops (stmt)
1582 && gimple_references_memory_p (stmt)
1583 && gimple_assign_single_p (stmt)
1584 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
1585 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
1586 && !is_gimple_reg (lhs))
1588 tree rhs = gimple_assign_rhs1 (stmt);
1589 gassign *new_stmt;
1591 /* Build a new statement with the RHS and LHS exchanged. */
1592 if (TREE_CODE (rhs) == SSA_NAME)
1594 /* NOTE tuples. The call to gimple_build_assign below replaced
1595 a call to build_gimple_modify_stmt, which did not set the
1596 SSA_NAME_DEF_STMT on the LHS of the assignment. Doing so
1597 may cause an SSA validation failure, as the LHS may be a
1598 default-initialized name and should have no definition. I'm
1599 a bit dubious of this, as the artificial statement that we
1600 generate here may in fact be ill-formed, but it is simply
1601 used as an internal device in this pass, and never becomes
1602 part of the CFG. */
1603 gimple *defstmt = SSA_NAME_DEF_STMT (rhs);
1604 new_stmt = gimple_build_assign (rhs, lhs);
1605 SSA_NAME_DEF_STMT (rhs) = defstmt;
1607 else
1608 new_stmt = gimple_build_assign (rhs, lhs);
1610 gimple_set_vuse (new_stmt, gimple_vdef (stmt));
1612 /* Finally enter the statement into the available expression
1613 table. */
1614 lookup_avail_expr (new_stmt, true, avail_exprs_stack);
1618 /* Replace *OP_P in STMT with any known equivalent value for *OP_P from
1619 CONST_AND_COPIES. */
1621 static void
1622 cprop_operand (gimple *stmt, use_operand_p op_p)
1624 tree val;
1625 tree op = USE_FROM_PTR (op_p);
1627 /* If the operand has a known constant value or it is known to be a
1628 copy of some other variable, use the value or copy stored in
1629 CONST_AND_COPIES. */
1630 val = SSA_NAME_VALUE (op);
1631 if (val && val != op)
1633 /* Do not replace hard register operands in asm statements. */
1634 if (gimple_code (stmt) == GIMPLE_ASM
1635 && !may_propagate_copy_into_asm (op))
1636 return;
1638 /* Certain operands are not allowed to be copy propagated due
1639 to their interaction with exception handling and some GCC
1640 extensions. */
1641 if (!may_propagate_copy (op, val))
1642 return;
1644 /* Do not propagate copies into BIVs.
1645 See PR23821 and PR62217 for how this can disturb IV and
1646 number of iteration analysis. */
1647 if (TREE_CODE (val) != INTEGER_CST)
1649 gimple *def = SSA_NAME_DEF_STMT (op);
1650 if (gimple_code (def) == GIMPLE_PHI
1651 && gimple_bb (def)->loop_father->header == gimple_bb (def))
1652 return;
1655 /* Dump details. */
1656 if (dump_file && (dump_flags & TDF_DETAILS))
1658 fprintf (dump_file, " Replaced '");
1659 print_generic_expr (dump_file, op, dump_flags);
1660 fprintf (dump_file, "' with %s '",
1661 (TREE_CODE (val) != SSA_NAME ? "constant" : "variable"));
1662 print_generic_expr (dump_file, val, dump_flags);
1663 fprintf (dump_file, "'\n");
1666 if (TREE_CODE (val) != SSA_NAME)
1667 opt_stats.num_const_prop++;
1668 else
1669 opt_stats.num_copy_prop++;
1671 propagate_value (op_p, val);
1673 /* And note that we modified this statement. This is now
1674 safe, even if we changed virtual operands since we will
1675 rescan the statement and rewrite its operands again. */
1676 gimple_set_modified (stmt, true);
1680 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
1681 known value for that SSA_NAME (or NULL if no value is known).
1683 Propagate values from CONST_AND_COPIES into the uses, vuses and
1684 vdef_ops of STMT. */
1686 static void
1687 cprop_into_stmt (gimple *stmt)
1689 use_operand_p op_p;
1690 ssa_op_iter iter;
1691 tree last_copy_propagated_op = NULL;
1693 FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_USE)
1695 tree old_op = USE_FROM_PTR (op_p);
1697 /* If we have A = B and B = A in the copy propagation tables
1698 (due to an equality comparison), avoid substituting B for A
1699 then A for B in the trivially discovered cases. This allows
1700 optimization of statements were A and B appear as input
1701 operands. */
1702 if (old_op != last_copy_propagated_op)
1704 cprop_operand (stmt, op_p);
1706 tree new_op = USE_FROM_PTR (op_p);
1707 if (new_op != old_op && TREE_CODE (new_op) == SSA_NAME)
1708 last_copy_propagated_op = new_op;
1713 /* Optimize the statement in block BB pointed to by iterator SI
1714 using equivalences from CONST_AND_COPIES and AVAIL_EXPRS_STACK.
1716 We try to perform some simplistic global redundancy elimination and
1717 constant propagation:
1719 1- To detect global redundancy, we keep track of expressions that have
1720 been computed in this block and its dominators. If we find that the
1721 same expression is computed more than once, we eliminate repeated
1722 computations by using the target of the first one.
1724 2- Constant values and copy assignments. This is used to do very
1725 simplistic constant and copy propagation. When a constant or copy
1726 assignment is found, we map the value on the RHS of the assignment to
1727 the variable in the LHS in the CONST_AND_COPIES table. */
1729 static edge
1730 optimize_stmt (basic_block bb, gimple_stmt_iterator si,
1731 class const_and_copies *const_and_copies,
1732 class avail_exprs_stack *avail_exprs_stack)
1734 gimple *stmt, *old_stmt;
1735 bool may_optimize_p;
1736 bool modified_p = false;
1737 bool was_noreturn;
1738 edge retval = NULL;
1740 old_stmt = stmt = gsi_stmt (si);
1741 was_noreturn = is_gimple_call (stmt) && gimple_call_noreturn_p (stmt);
1743 if (dump_file && (dump_flags & TDF_DETAILS))
1745 fprintf (dump_file, "Optimizing statement ");
1746 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1749 update_stmt_if_modified (stmt);
1750 opt_stats.num_stmts++;
1752 /* Const/copy propagate into USES, VUSES and the RHS of VDEFs. */
1753 cprop_into_stmt (stmt);
1755 /* If the statement has been modified with constant replacements,
1756 fold its RHS before checking for redundant computations. */
1757 if (gimple_modified_p (stmt))
1759 tree rhs = NULL;
1761 /* Try to fold the statement making sure that STMT is kept
1762 up to date. */
1763 if (fold_stmt (&si))
1765 stmt = gsi_stmt (si);
1766 gimple_set_modified (stmt, true);
1768 if (dump_file && (dump_flags & TDF_DETAILS))
1770 fprintf (dump_file, " Folded to: ");
1771 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1775 /* We only need to consider cases that can yield a gimple operand. */
1776 if (gimple_assign_single_p (stmt))
1777 rhs = gimple_assign_rhs1 (stmt);
1778 else if (gimple_code (stmt) == GIMPLE_GOTO)
1779 rhs = gimple_goto_dest (stmt);
1780 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
1781 /* This should never be an ADDR_EXPR. */
1782 rhs = gimple_switch_index (swtch_stmt);
1784 if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
1785 recompute_tree_invariant_for_addr_expr (rhs);
1787 /* Indicate that maybe_clean_or_replace_eh_stmt needs to be called,
1788 even if fold_stmt updated the stmt already and thus cleared
1789 gimple_modified_p flag on it. */
1790 modified_p = true;
1793 /* Check for redundant computations. Do this optimization only
1794 for assignments that have no volatile ops and conditionals. */
1795 may_optimize_p = (!gimple_has_side_effects (stmt)
1796 && (is_gimple_assign (stmt)
1797 || (is_gimple_call (stmt)
1798 && gimple_call_lhs (stmt) != NULL_TREE)
1799 || gimple_code (stmt) == GIMPLE_COND
1800 || gimple_code (stmt) == GIMPLE_SWITCH));
1802 if (may_optimize_p)
1804 if (gimple_code (stmt) == GIMPLE_CALL)
1806 /* Resolve __builtin_constant_p. If it hasn't been
1807 folded to integer_one_node by now, it's fairly
1808 certain that the value simply isn't constant. */
1809 tree callee = gimple_call_fndecl (stmt);
1810 if (callee
1811 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
1812 && DECL_FUNCTION_CODE (callee) == BUILT_IN_CONSTANT_P)
1814 propagate_tree_value_into_stmt (&si, integer_zero_node);
1815 stmt = gsi_stmt (si);
1819 if (gimple_code (stmt) == GIMPLE_COND)
1821 tree lhs = gimple_cond_lhs (stmt);
1822 tree rhs = gimple_cond_rhs (stmt);
1824 /* If the LHS has a range [0..1] and the RHS has a range ~[0..1],
1825 then this conditional is computable at compile time. We can just
1826 shove either 0 or 1 into the LHS, mark the statement as modified
1827 and all the right things will just happen below.
1829 Note this would apply to any case where LHS has a range
1830 narrower than its type implies and RHS is outside that
1831 narrower range. Future work. */
1832 if (TREE_CODE (lhs) == SSA_NAME
1833 && ssa_name_has_boolean_range (lhs)
1834 && TREE_CODE (rhs) == INTEGER_CST
1835 && ! (integer_zerop (rhs) || integer_onep (rhs)))
1837 gimple_cond_set_lhs (as_a <gcond *> (stmt),
1838 fold_convert (TREE_TYPE (lhs),
1839 integer_zero_node));
1840 gimple_set_modified (stmt, true);
1844 update_stmt_if_modified (stmt);
1845 eliminate_redundant_computations (&si, const_and_copies,
1846 avail_exprs_stack);
1847 stmt = gsi_stmt (si);
1849 /* Perform simple redundant store elimination. */
1850 if (gimple_assign_single_p (stmt)
1851 && TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
1853 tree lhs = gimple_assign_lhs (stmt);
1854 tree rhs = gimple_assign_rhs1 (stmt);
1855 tree cached_lhs;
1856 gassign *new_stmt;
1857 rhs = dom_valueize (rhs);
1858 /* Build a new statement with the RHS and LHS exchanged. */
1859 if (TREE_CODE (rhs) == SSA_NAME)
1861 gimple *defstmt = SSA_NAME_DEF_STMT (rhs);
1862 new_stmt = gimple_build_assign (rhs, lhs);
1863 SSA_NAME_DEF_STMT (rhs) = defstmt;
1865 else
1866 new_stmt = gimple_build_assign (rhs, lhs);
1867 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
1868 cached_lhs = lookup_avail_expr (new_stmt, false, avail_exprs_stack,
1869 false);
1870 if (cached_lhs
1871 && rhs == cached_lhs)
1873 basic_block bb = gimple_bb (stmt);
1874 unlink_stmt_vdef (stmt);
1875 if (gsi_remove (&si, true))
1877 bitmap_set_bit (need_eh_cleanup, bb->index);
1878 if (dump_file && (dump_flags & TDF_DETAILS))
1879 fprintf (dump_file, " Flagged to clear EH edges.\n");
1881 release_defs (stmt);
1882 return retval;
1887 /* Record any additional equivalences created by this statement. */
1888 if (is_gimple_assign (stmt))
1889 record_equivalences_from_stmt (stmt, may_optimize_p, avail_exprs_stack);
1891 /* If STMT is a COND_EXPR or SWITCH_EXPR and it was modified, then we may
1892 know where it goes. */
1893 if (gimple_modified_p (stmt) || modified_p)
1895 tree val = NULL;
1897 if (gimple_code (stmt) == GIMPLE_COND)
1898 val = fold_binary_loc (gimple_location (stmt),
1899 gimple_cond_code (stmt), boolean_type_node,
1900 gimple_cond_lhs (stmt),
1901 gimple_cond_rhs (stmt));
1902 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
1903 val = gimple_switch_index (swtch_stmt);
1905 if (val && TREE_CODE (val) == INTEGER_CST)
1907 retval = find_taken_edge (bb, val);
1908 if (retval)
1910 /* Fix the condition to be either true or false. */
1911 if (gimple_code (stmt) == GIMPLE_COND)
1913 if (integer_zerop (val))
1914 gimple_cond_make_false (as_a <gcond *> (stmt));
1915 else if (integer_onep (val))
1916 gimple_cond_make_true (as_a <gcond *> (stmt));
1917 else
1918 gcc_unreachable ();
1920 gimple_set_modified (stmt, true);
1923 /* Further simplifications may be possible. */
1924 cfg_altered = true;
1928 update_stmt_if_modified (stmt);
1930 /* If we simplified a statement in such a way as to be shown that it
1931 cannot trap, update the eh information and the cfg to match. */
1932 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
1934 bitmap_set_bit (need_eh_cleanup, bb->index);
1935 if (dump_file && (dump_flags & TDF_DETAILS))
1936 fprintf (dump_file, " Flagged to clear EH edges.\n");
1939 if (!was_noreturn
1940 && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
1941 need_noreturn_fixup.safe_push (stmt);
1943 return retval;
1946 /* Helper for walk_non_aliased_vuses. Determine if we arrived at
1947 the desired memory state. */
1949 static void *
1950 vuse_eq (ao_ref *, tree vuse1, unsigned int cnt, void *data)
1952 tree vuse2 = (tree) data;
1953 if (vuse1 == vuse2)
1954 return data;
1956 /* This bounds the stmt walks we perform on reference lookups
1957 to O(1) instead of O(N) where N is the number of dominating
1958 stores leading to a candidate. We re-use the SCCVN param
1959 for this as it is basically the same complexity. */
1960 if (cnt > (unsigned) PARAM_VALUE (PARAM_SCCVN_MAX_ALIAS_QUERIES_PER_ACCESS))
1961 return (void *)-1;
1963 return NULL;
1966 /* Search for an existing instance of STMT in the AVAIL_EXPRS_STACK table.
1967 If found, return its LHS. Otherwise insert STMT in the table and
1968 return NULL_TREE.
1970 Also, when an expression is first inserted in the table, it is also
1971 is also added to AVAIL_EXPRS_STACK, so that it can be removed when
1972 we finish processing this block and its children. */
1974 static tree
1975 lookup_avail_expr (gimple *stmt, bool insert,
1976 class avail_exprs_stack *avail_exprs_stack, bool tbaa_p)
1978 expr_hash_elt **slot;
1979 tree lhs;
1981 /* Get LHS of phi, assignment, or call; else NULL_TREE. */
1982 if (gimple_code (stmt) == GIMPLE_PHI)
1983 lhs = gimple_phi_result (stmt);
1984 else
1985 lhs = gimple_get_lhs (stmt);
1987 class expr_hash_elt element (stmt, lhs);
1989 if (dump_file && (dump_flags & TDF_DETAILS))
1991 fprintf (dump_file, "LKUP ");
1992 element.print (dump_file);
1995 /* Don't bother remembering constant assignments and copy operations.
1996 Constants and copy operations are handled by the constant/copy propagator
1997 in optimize_stmt. */
1998 if (element.expr()->kind == EXPR_SINGLE
1999 && (TREE_CODE (element.expr()->ops.single.rhs) == SSA_NAME
2000 || is_gimple_min_invariant (element.expr()->ops.single.rhs)))
2001 return NULL_TREE;
2003 /* Finally try to find the expression in the main expression hash table. */
2004 hash_table<expr_elt_hasher> *avail_exprs = avail_exprs_stack->avail_exprs ();
2005 slot = avail_exprs->find_slot (&element, (insert ? INSERT : NO_INSERT));
2006 if (slot == NULL)
2008 return NULL_TREE;
2010 else if (*slot == NULL)
2012 class expr_hash_elt *element2 = new expr_hash_elt (element);
2013 *slot = element2;
2015 avail_exprs_stack->record_expr (element2, NULL, '2');
2016 return NULL_TREE;
2019 /* If we found a redundant memory operation do an alias walk to
2020 check if we can re-use it. */
2021 if (gimple_vuse (stmt) != (*slot)->vop ())
2023 tree vuse1 = (*slot)->vop ();
2024 tree vuse2 = gimple_vuse (stmt);
2025 /* If we have a load of a register and a candidate in the
2026 hash with vuse1 then try to reach its stmt by walking
2027 up the virtual use-def chain using walk_non_aliased_vuses.
2028 But don't do this when removing expressions from the hash. */
2029 ao_ref ref;
2030 if (!(vuse1 && vuse2
2031 && gimple_assign_single_p (stmt)
2032 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME
2033 && (ao_ref_init (&ref, gimple_assign_rhs1 (stmt)),
2034 ref.base_alias_set = ref.ref_alias_set = tbaa_p ? -1 : 0, true)
2035 && walk_non_aliased_vuses (&ref, vuse2,
2036 vuse_eq, NULL, NULL, vuse1) != NULL))
2038 if (insert)
2040 class expr_hash_elt *element2 = new expr_hash_elt (element);
2042 /* Insert the expr into the hash by replacing the current
2043 entry and recording the value to restore in the
2044 avail_exprs_stack. */
2045 avail_exprs_stack->record_expr (element2, *slot, '2');
2046 *slot = element2;
2048 return NULL_TREE;
2052 /* Extract the LHS of the assignment so that it can be used as the current
2053 definition of another variable. */
2054 lhs = (*slot)->lhs ();
2056 lhs = dom_valueize (lhs);
2058 if (dump_file && (dump_flags & TDF_DETAILS))
2060 fprintf (dump_file, "FIND: ");
2061 print_generic_expr (dump_file, lhs, 0);
2062 fprintf (dump_file, "\n");
2065 return lhs;