/* Iterator routines for GIMPLE statements.
- Copyright (C) 2007, 2008, 2010 Free Software Foundation, Inc.
+ Copyright (C) 2007-2019 Free Software Foundation, Inc.
Contributed by Aldy Hernandez <aldy@quesejoda.com>
This file is part of GCC.
#include "config.h"
#include "system.h"
#include "coretypes.h"
-#include "tm.h"
+#include "backend.h"
#include "tree.h"
#include "gimple.h"
-#include "tree-flow.h"
+#include "cfghooks.h"
+#include "ssa.h"
+#include "cgraph.h"
+#include "tree-eh.h"
+#include "gimple-iterator.h"
+#include "tree-cfg.h"
+#include "tree-ssa.h"
#include "value-prof.h"
/* Mark the statement STMT as modified, and update it. */
static inline void
-update_modified_stmt (gimple stmt)
+update_modified_stmt (gimple *stmt)
{
- if (!ssa_operands_active ())
+ if (!ssa_operands_active (cfun))
return;
update_stmt_if_modified (stmt);
}
/* Mark the statements in SEQ as modified, and update them. */
-static void
+void
update_modified_stmts (gimple_seq seq)
{
gimple_stmt_iterator gsi;
- if (!ssa_operands_active ())
+ if (!ssa_operands_active (cfun))
return;
for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
update_stmt_if_modified (gsi_stmt (gsi));
{
gimple_seq_node n;
- for (n = first; n; n = n->gsbase.next)
+ for (n = first; n; n = n->next)
{
gimple_set_bb (n, bb);
if (n == last)
update_call_edge_frequencies (gimple_seq_node first, basic_block bb)
{
struct cgraph_node *cfun_node = NULL;
- int bb_freq = 0;
gimple_seq_node n;
- for (n = first; n ; n = n->gsbase.next)
+ for (n = first; n ; n = n->next)
if (is_gimple_call (n))
{
struct cgraph_edge *e;
/* These function calls are expensive enough that we want
to avoid calling them if we never see any calls. */
if (cfun_node == NULL)
- {
- cfun_node = cgraph_get_node (current_function_decl);
- bb_freq = (compute_call_stmt_bb_frequency
- (current_function_decl, bb));
- }
+ cfun_node = cgraph_node::get (current_function_decl);
- e = cgraph_edge (cfun_node, n);
+ e = cfun_node->get_edge (n);
if (e != NULL)
- e->frequency = bb_freq;
+ e->count = bb->count;
}
}
basic_block bb;
gimple_seq_node cur = i->ptr;
- gcc_assert (!cur || cur->gsbase.prev);
+ gcc_assert (!cur || cur->prev);
if ((bb = gsi_bb (*i)) != NULL)
update_bb_for_stmts (first, last, bb);
/* Link SEQ before CUR in the sequence. */
if (cur)
{
- first->gsbase.prev = cur->gsbase.prev;
- if (first->gsbase.prev->gsbase.next)
- first->gsbase.prev->gsbase.next = first;
+ first->prev = cur->prev;
+ if (first->prev->next)
+ first->prev->next = first;
else
gimple_seq_set_first (i->seq, first);
- last->gsbase.next = cur;
- cur->gsbase.prev = last;
+ last->next = cur;
+ cur->prev = last;
}
else
{
labels, so it returns an iterator after the end of the block, and
we need to insert before it; it might be cleaner to add a flag to the
iterator saying whether we are at the start or end of the list). */
- last->gsbase.next = NULL;
+ last->next = NULL;
if (itlast)
{
- first->gsbase.prev = itlast;
- itlast->gsbase.next = first;
+ first->prev = itlast;
+ itlast->next = first;
}
else
gimple_seq_set_first (i->seq, first);
basic_block bb;
gimple_seq_node cur = i->ptr;
- gcc_assert (!cur || cur->gsbase.prev);
+ gcc_assert (!cur || cur->prev);
/* If the iterator is inside a basic block, we need to update the
basic block information for all the nodes between FIRST and LAST. */
/* Link SEQ after CUR. */
if (cur)
{
- last->gsbase.next = cur->gsbase.next;
- if (last->gsbase.next)
+ last->next = cur->next;
+ if (last->next)
{
- last->gsbase.next->gsbase.prev = last;
+ last->next->prev = last;
}
else
gimple_seq_set_last (i->seq, last);
- first->gsbase.prev = cur;
- cur->gsbase.next = first;
+ first->prev = cur;
+ cur->next = first;
}
else
{
gcc_assert (!gimple_seq_last (*i->seq));
- last->gsbase.next = NULL;
+ last->next = NULL;
gimple_seq_set_first (i->seq, first);
gimple_seq_set_last (i->seq, last);
}
cur = i.ptr;
/* How can we possibly split after the end, or before the beginning? */
- gcc_assert (cur && cur->gsbase.next);
- next = cur->gsbase.next;
+ gcc_assert (cur && cur->next);
+ next = cur->next;
pold_seq = i.seq;
gimple_seq_set_first (&new_seq, next);
gimple_seq_set_last (&new_seq, gimple_seq_last (*pold_seq));
gimple_seq_set_last (pold_seq, cur);
- cur->gsbase.next = NULL;
+ cur->next = NULL;
return new_seq;
}
of gsi_replace. */
void
-gsi_set_stmt (gimple_stmt_iterator *gsi, gimple stmt)
+gsi_set_stmt (gimple_stmt_iterator *gsi, gimple *stmt)
{
- gimple orig_stmt = gsi_stmt (*gsi);
- gimple prev, next;
+ gimple *orig_stmt = gsi_stmt (*gsi);
+ gimple *prev, *next;
- stmt->gsbase.next = next = orig_stmt->gsbase.next;
- stmt->gsbase.prev = prev = orig_stmt->gsbase.prev;
+ stmt->next = next = orig_stmt->next;
+ stmt->prev = prev = orig_stmt->prev;
/* Note how we don't clear next/prev of orig_stmt. This is so that
copies of *GSI our callers might still hold (to orig_stmt)
can be advanced as if they too were replaced. */
- if (prev->gsbase.next)
- prev->gsbase.next = stmt;
+ if (prev->next)
+ prev->next = stmt;
else
gimple_seq_set_first (gsi->seq, stmt);
if (next)
- next->gsbase.prev = stmt;
+ next->prev = stmt;
else
gimple_seq_set_last (gsi->seq, stmt);
/* How can we possibly split after the end? */
gcc_assert (cur);
- prev = cur->gsbase.prev;
+ prev = cur->prev;
old_seq = *i->seq;
- if (!prev->gsbase.next)
+ if (!prev->next)
*i->seq = NULL;
i->seq = pnew_seq;
/* Cut OLD_SEQ before I. */
gimple_seq_set_last (&old_seq, prev);
- if (prev->gsbase.next)
- prev->gsbase.next = NULL;
+ if (prev->next)
+ prev->next = NULL;
}
/* Replace the statement pointed-to by GSI to STMT. If UPDATE_EH_INFO
is true, the exception handling information of the original
statement is moved to the new statement. Assignments must only be
- replaced with assignments to the same LHS. */
+ replaced with assignments to the same LHS. Returns whether EH edge
+ cleanup is required. */
-void
-gsi_replace (gimple_stmt_iterator *gsi, gimple stmt, bool update_eh_info)
+bool
+gsi_replace (gimple_stmt_iterator *gsi, gimple *stmt, bool update_eh_info)
{
- gimple orig_stmt = gsi_stmt (*gsi);
+ gimple *orig_stmt = gsi_stmt (*gsi);
+ bool require_eh_edge_purge = false;
if (stmt == orig_stmt)
- return;
+ return false;
- gcc_assert (!gimple_has_lhs (orig_stmt)
+ gcc_assert (!gimple_has_lhs (orig_stmt) || !gimple_has_lhs (stmt)
|| gimple_get_lhs (orig_stmt) == gimple_get_lhs (stmt));
gimple_set_location (stmt, gimple_location (orig_stmt));
/* Preserve EH region information from the original statement, if
requested by the caller. */
if (update_eh_info)
- maybe_clean_or_replace_eh_stmt (orig_stmt, stmt);
+ require_eh_edge_purge = maybe_clean_or_replace_eh_stmt (orig_stmt, stmt);
gimple_duplicate_stmt_histograms (cfun, stmt, cfun, orig_stmt);
gsi_set_stmt (gsi, stmt);
gimple_set_modified (stmt, true);
update_modified_stmt (stmt);
+ return require_eh_edge_purge;
}
bool update_eh_info)
{
gimple_stmt_iterator seqi;
- gimple last;
+ gimple *last;
if (gimple_seq_empty_p (seq))
{
gsi_remove (gsi, true);
should use gsi_insert_before. */
void
-gsi_insert_before_without_update (gimple_stmt_iterator *i, gimple stmt,
+gsi_insert_before_without_update (gimple_stmt_iterator *i, gimple *stmt,
enum gsi_iterator_update m)
{
gsi_insert_seq_nodes_before (i, stmt, stmt, m);
gsi_iterator_update). */
void
-gsi_insert_before (gimple_stmt_iterator *i, gimple stmt,
+gsi_insert_before (gimple_stmt_iterator *i, gimple *stmt,
enum gsi_iterator_update m)
{
update_modified_stmt (stmt);
should use gsi_insert_after. */
void
-gsi_insert_after_without_update (gimple_stmt_iterator *i, gimple stmt,
+gsi_insert_after_without_update (gimple_stmt_iterator *i, gimple *stmt,
enum gsi_iterator_update m)
{
gsi_insert_seq_nodes_after (i, stmt, stmt, m);
gsi_iterator_update). */
void
-gsi_insert_after (gimple_stmt_iterator *i, gimple stmt,
+gsi_insert_after (gimple_stmt_iterator *i, gimple *stmt,
enum gsi_iterator_update m)
{
update_modified_stmt (stmt);
gsi_remove (gimple_stmt_iterator *i, bool remove_permanently)
{
gimple_seq_node cur, next, prev;
- gimple stmt = gsi_stmt (*i);
+ gimple *stmt = gsi_stmt (*i);
bool require_eh_edge_purge = false;
if (gimple_code (stmt) != GIMPLE_PHI)
if (remove_permanently)
{
+ if (gimple_debug_nonbind_marker_p (stmt))
+ /* We don't need this to be exact, but try to keep it at least
+ close. */
+ cfun->debug_marker_count--;
require_eh_edge_purge = remove_stmt_from_eh_lp (stmt);
gimple_remove_stmt_histograms (cfun, stmt);
}
/* Update the iterator and re-wire the links in I->SEQ. */
cur = i->ptr;
- next = cur->gsbase.next;
- prev = cur->gsbase.prev;
+ next = cur->next;
+ prev = cur->prev;
/* See gsi_set_stmt for why we don't reset prev/next of STMT. */
if (next)
/* Cur is not last. */
- next->gsbase.prev = prev;
- else if (prev->gsbase.next)
+ next->prev = prev;
+ else if (prev->next)
/* Cur is last but not first. */
gimple_seq_set_last (i->seq, prev);
- if (prev->gsbase.next)
+ if (prev->next)
/* Cur is not first. */
- prev->gsbase.next = next;
+ prev->next = next;
else
/* Cur is first. */
*i->seq = next;
/* Finds iterator for STMT. */
gimple_stmt_iterator
-gsi_for_stmt (gimple stmt)
+gsi_for_stmt (gimple *stmt)
{
gimple_stmt_iterator i;
basic_block bb = gimple_bb (stmt);
return i;
}
+/* Get an iterator for STMT, which is known to belong to SEQ. This is
+ equivalent to starting at the beginning of SEQ and searching forward
+ until STMT is found. */
+
+gimple_stmt_iterator
+gsi_for_stmt (gimple *stmt, gimple_seq *seq)
+{
+ gimple_stmt_iterator i = gsi_start_1 (seq);
+ i.ptr = stmt;
+ return i;
+}
+
+/* Finds iterator for PHI. */
+
+gphi_iterator
+gsi_for_phi (gphi *phi)
+{
+ gphi_iterator i;
+ basic_block bb = gimple_bb (phi);
+
+ i = gsi_start_phis (bb);
+ i.ptr = phi;
+
+ return i;
+}
/* Move the statement at FROM so it comes right after the statement at TO. */
void
gsi_move_after (gimple_stmt_iterator *from, gimple_stmt_iterator *to)
{
- gimple stmt = gsi_stmt (*from);
+ gimple *stmt = gsi_stmt (*from);
gsi_remove (from, false);
/* We must have GSI_NEW_STMT here, as gsi_move_after is sometimes used to
void
gsi_move_before (gimple_stmt_iterator *from, gimple_stmt_iterator *to)
{
- gimple stmt = gsi_stmt (*from);
+ gimple *stmt = gsi_stmt (*from);
gsi_remove (from, false);
/* For consistency with gsi_move_after, it might be better to have
made until a call to gsi_commit_edge_inserts () is made. */
void
-gsi_insert_on_edge (edge e, gimple stmt)
+gsi_insert_on_edge (edge e, gimple *stmt)
{
gimple_seq_add_stmt (&PENDING_STMT (e), stmt);
}
gimple_seq_add_seq (&PENDING_STMT (e), seq);
}
+/* Return a new iterator pointing to the first statement in sequence of
+ statements on edge E. Such statements need to be subsequently moved into a
+ basic block by calling gsi_commit_edge_inserts. */
+
+gimple_stmt_iterator
+gsi_start_edge (edge e)
+{
+ return gsi_start (PENDING_STMT (e));
+}
/* Insert the statement pointed-to by GSI into edge E. Every attempt
is made to place the statement in an existing basic block, but
basic_block *new_bb)
{
basic_block dest, src;
- gimple tmp;
+ gimple *tmp;
dest = e->dest;
restart:
if (single_pred_p (dest)
&& gimple_seq_empty_p (phi_nodes (dest))
- && dest != EXIT_BLOCK_PTR)
+ && dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
{
*gsi = gsi_start_bb (dest);
if (gsi_end_p (*gsi))
Except for the entry block. */
src = e->src;
if ((e->flags & EDGE_ABNORMAL) == 0
- && single_succ_p (src)
- && src != ENTRY_BLOCK_PTR)
+ && (single_succ_p (src)
+ /* Do not count a fake edge as successor as added to infinite
+ loops by connect_infinite_loops_to_exit. */
+ || (EDGE_COUNT (src->succs) == 2
+ && (EDGE_SUCC (src, 0)->flags & EDGE_FAKE
+ || EDGE_SUCC (src, 1)->flags & EDGE_FAKE)))
+ && src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
{
*gsi = gsi_last_bb (src);
if (gsi_end_p (*gsi))
return true;
tmp = gsi_stmt (*gsi);
- if (!stmt_ends_bb_p (tmp))
+ if (is_gimple_debug (tmp))
+ {
+ gimple_stmt_iterator si = *gsi;
+ gsi_prev_nondebug (&si);
+ if (!gsi_end_p (si))
+ tmp = gsi_stmt (si);
+ /* If we don't have a BB-ending nondebug stmt, we want to
+ insert after the trailing debug stmts. Otherwise, we may
+ insert before the BB-ending nondebug stmt, or split the
+ edge. */
+ if (!stmt_ends_bb_p (tmp))
+ return true;
+ *gsi = si;
+ }
+ else if (!stmt_ends_bb_p (tmp))
return true;
switch (gimple_code (tmp))
block has to be created, it is returned. */
basic_block
-gsi_insert_on_edge_immediate (edge e, gimple stmt)
+gsi_insert_on_edge_immediate (edge e, gimple *stmt)
{
gimple_stmt_iterator gsi;
basic_block new_bb = NULL;
edge e;
edge_iterator ei;
- gsi_commit_one_edge_insert (single_succ_edge (ENTRY_BLOCK_PTR), NULL);
+ gsi_commit_one_edge_insert (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
+ NULL);
- FOR_EACH_BB (bb)
+ FOR_EACH_BB_FN (bb, cfun)
FOR_EACH_EDGE (e, ei, bb->succs)
gsi_commit_one_edge_insert (e, NULL);
}
/* Returns iterator at the start of the list of phi nodes of BB. */
-gimple_stmt_iterator
+gphi_iterator
gsi_start_phis (basic_block bb)
{
gimple_seq *pseq = phi_nodes_ptr (bb);
- /* XXX check only necessary because ENTRY/EXIT blocks don't have il.gimple */
- return pseq ? gsi_start_1 (pseq) : gsi_none ();
+
+ /* Adapted from gsi_start_1. */
+ gphi_iterator i;
+
+ i.ptr = gimple_seq_first (*pseq);
+ i.seq = pseq;
+ i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
+
+ return i;
}