This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[tree-ssa] tsi_link_*after fixes/improvements
- From: Richard Henderson <rth at redhat dot com>
- To: gcc-patches at gcc dot gnu dot org
- Date: Wed, 17 Sep 2003 16:57:16 -0700
- Subject: [tree-ssa] tsi_link_*after fixes/improvements
(1) Bug in tsi_link_chain_after wrt TSI_CHAIN_END. Copied code
from tsi_link_chain_before that did handle this case.
(2) Allow tsi_link_after and tsi_link_chain_after to work with
*loc = NULL
i = tsi_start (loc);
tsi_link_after (&i, x, TSI_NEW_STMT);
tsi_link_after (&i, y, TSI_NEW_STMT);
and have *loc have contain a nicely chained sequence
at the end.
r~
* tree.c (tsi_link_before): Remove unnecessary parens.
(tsi_link_chain_before, tsi_delink): Likewise.
(tsi_link_after): Accept the case if the iterator points
to a NULL node; treat it as an empty list.
(tsi_link_chain_after): Likewise. Update iterator properly
for TSI_CHAIN_END.
Index: tree.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/tree.c,v
retrieving revision 1.263.2.51
diff -c -p -d -r1.263.2.51 tree.c
*** tree.c 6 Sep 2003 17:13:03 -0000 1.263.2.51
--- tree.c 17 Sep 2003 23:48:58 -0000
*************** tsi_link_before (tree_stmt_iterator *i,
*** 5123,5138 ****
mode = TSI_NEW_STMT;
/* Build a new CE which points to the current node. */
! ce = build (COMPOUND_EXPR, void_type_node, t, *(i->tp));
/* Make the parent pointer point to this new node. At this point, the
iterator will be pointing to the new node we just inserted. */
! *(i->tp) = ce;
/* Update the iterator to points to the address of the next ptr in our new
node, which is the current stmt again. */
if (mode == TSI_SAME_STMT)
! i->tp = &(TREE_OPERAND (*(i->tp), 1));
}
/* Links a stmt after the current stmt. */
--- 5123,5138 ----
mode = TSI_NEW_STMT;
/* Build a new CE which points to the current node. */
! ce = build (COMPOUND_EXPR, void_type_node, t, *i->tp);
/* Make the parent pointer point to this new node. At this point, the
iterator will be pointing to the new node we just inserted. */
! *i->tp = ce;
/* Update the iterator to points to the address of the next ptr in our new
node, which is the current stmt again. */
if (mode == TSI_SAME_STMT)
! i->tp = &TREE_OPERAND (ce, 1);
}
/* Links a stmt after the current stmt. */
*************** void
*** 5141,5170 ****
tsi_link_after (tree_stmt_iterator *i, tree t, enum tsi_iterator_update mode)
{
tree ce;
! tree next;
if (mode == TSI_CHAIN_START || mode == TSI_CHAIN_END)
abort ();
if (mode == TSI_CONTINUE_LINKING)
mode = TSI_NEW_STMT;
/* If this node isnt a COMPUND_EXPR, we need to insert a CE node. */
! if (TREE_CODE (*(i->tp)) != COMPOUND_EXPR)
{
/* Create a new node with the current stmt on the left, and the new
stmt on the right. */
! ce = build (COMPOUND_EXPR, void_type_node, *(i->tp), t);
/* Update link to point to this CE node. */
! *(i->tp) = ce;
/* Change new iterator to point to the new stmt. */
if (mode == TSI_NEW_STMT)
! i->tp = &(TREE_OPERAND (ce, 1));
}
else
{
! next = TREE_OPERAND (*(i->tp), 1);
/* Create a new node with the same 'next' link as the current one. */
ce = build (COMPOUND_EXPR, void_type_node, t, next);
--- 5141,5176 ----
tsi_link_after (tree_stmt_iterator *i, tree t, enum tsi_iterator_update mode)
{
tree ce;
! tree next, cur = *i->tp;
if (mode == TSI_CHAIN_START || mode == TSI_CHAIN_END)
abort ();
if (mode == TSI_CONTINUE_LINKING)
mode = TSI_NEW_STMT;
+ /* If this node is empty, we can just add it. */
+ if (cur == NULL)
+ {
+ *i->tp = t;
+ }
+
/* If this node isnt a COMPUND_EXPR, we need to insert a CE node. */
! else if (TREE_CODE (cur) != COMPOUND_EXPR)
{
/* Create a new node with the current stmt on the left, and the new
stmt on the right. */
! ce = build (COMPOUND_EXPR, void_type_node, cur, t);
/* Update link to point to this CE node. */
! *i->tp = ce;
/* Change new iterator to point to the new stmt. */
if (mode == TSI_NEW_STMT)
! i->tp = &TREE_OPERAND (ce, 1);
}
else
{
! next = TREE_OPERAND (cur, 1);
/* Create a new node with the same 'next' link as the current one. */
ce = build (COMPOUND_EXPR, void_type_node, t, next);
*************** tsi_link_after (tree_stmt_iterator *i, t
*** 5175,5181 ****
position down in the CE chain. */
{
basic_block bb = bb_for_stmt (next);
! if (bb && bb->head_tree_p == &TREE_OPERAND (*(i->tp), 1))
{
/* We may also need to update end_tree_p. */
if (bb->head_tree_p == bb->end_tree_p)
--- 5181,5187 ----
position down in the CE chain. */
{
basic_block bb = bb_for_stmt (next);
! if (bb && bb->head_tree_p == &TREE_OPERAND (cur, 1))
{
/* We may also need to update end_tree_p. */
if (bb->head_tree_p == bb->end_tree_p)
*************** tsi_link_after (tree_stmt_iterator *i, t
*** 5185,5195 ****
}
/* Make the current one's 'next' link point to our new stmt. */
! TREE_OPERAND (*(i->tp), 1) = ce;
/* Update the iterator to the new stmt. */
if (mode == TSI_NEW_STMT)
! i->tp = &(TREE_OPERAND (*(i->tp), 1));
}
}
--- 5191,5201 ----
}
/* Make the current one's 'next' link point to our new stmt. */
! TREE_OPERAND (cur, 1) = ce;
/* Update the iterator to the new stmt. */
if (mode == TSI_NEW_STMT)
! i->tp = &TREE_OPERAND (cur, 1);
}
}
*************** tsi_link_chain_before (tree_stmt_iterato
*** 5212,5227 ****
continue;
/* Build a new CE which points to the current node. */
! *last = build (COMPOUND_EXPR, void_type_node, *last, *(i->tp));
/* Make the parent pointer point to this new node. At this point, the
iterator will be pointing to the new node we just inserted. */
! *(i->tp) = t;
/* Update the iterator to points to the address of the next ptr in our new
node, which is the current stmt again. */
if (mode == TSI_SAME_STMT)
! i->tp = &(TREE_OPERAND (*last, 1));
else if (mode == TSI_CHAIN_END)
i->tp = last != &t ? last : i->tp;
}
--- 5218,5233 ----
continue;
/* Build a new CE which points to the current node. */
! *last = build (COMPOUND_EXPR, void_type_node, *last, *i->tp);
/* Make the parent pointer point to this new node. At this point, the
iterator will be pointing to the new node we just inserted. */
! *i->tp = t;
/* Update the iterator to points to the address of the next ptr in our new
node, which is the current stmt again. */
if (mode == TSI_SAME_STMT)
! i->tp = &TREE_OPERAND (*last, 1);
else if (mode == TSI_CHAIN_END)
i->tp = last != &t ? last : i->tp;
}
*************** tsi_link_chain_after (tree_stmt_iterator
*** 5233,5262 ****
enum tsi_iterator_update mode)
{
tree ce;
! tree next;
tree *last;
if (mode == TSI_NEW_STMT)
abort ();
if (mode == TSI_CONTINUE_LINKING)
mode = TSI_CHAIN_END;
/* If this node isnt a COMPUND_EXPR, we need to insert a CE node. */
! if (TREE_CODE (*(i->tp)) != COMPOUND_EXPR)
{
/* Create a new node with the current stmt on the left, and the new
stmt on the right. */
! ce = build (COMPOUND_EXPR, void_type_node, *(i->tp), t);
/* Update link to point to this CE node. */
! *(i->tp) = ce;
/* Change new iterator to point to the new stmt. */
if (mode == TSI_CHAIN_START)
! i->tp = &(TREE_OPERAND (ce, 1));
else if (mode == TSI_CHAIN_END)
! while (TREE_CODE (*i->tp) == COMPOUND_EXPR)
! i->tp = &(TREE_OPERAND (*i->tp, 1));
}
else
{
--- 5239,5295 ----
enum tsi_iterator_update mode)
{
tree ce;
! tree next, *nextp;
tree *last;
+ tree cur = *i->tp;
if (mode == TSI_NEW_STMT)
abort ();
if (mode == TSI_CONTINUE_LINKING)
mode = TSI_CHAIN_END;
+ /* If this node is empty, we can just add it. */
+ if (cur == NULL)
+ {
+ *i->tp = t;
+
+ if (mode == TSI_CHAIN_END)
+ {
+ cur = t;
+ last = i->tp;
+ while (TREE_CODE (cur) == COMPOUND_EXPR)
+ {
+ last = &TREE_OPERAND (cur, 1);
+ cur = *last;
+ }
+ i->tp = last;
+ }
+ }
+
/* If this node isnt a COMPUND_EXPR, we need to insert a CE node. */
! else if (TREE_CODE (cur) != COMPOUND_EXPR)
{
/* Create a new node with the current stmt on the left, and the new
stmt on the right. */
! ce = build (COMPOUND_EXPR, void_type_node, cur, t);
/* Update link to point to this CE node. */
! *i->tp = ce;
/* Change new iterator to point to the new stmt. */
if (mode == TSI_CHAIN_START)
! i->tp = &TREE_OPERAND (ce, 1);
else if (mode == TSI_CHAIN_END)
! {
! cur = ce;
! last = i->tp;
! while (TREE_CODE (cur) == COMPOUND_EXPR)
! {
! last = &TREE_OPERAND (cur, 1);
! cur = *last;
! }
! i->tp = last;
! }
}
else
{
*************** tsi_link_chain_after (tree_stmt_iterator
*** 5265,5295 ****
last = &TREE_OPERAND (*last, 1))
continue;
! next = TREE_OPERAND (*(i->tp), 1);
/* Create a new node with the same 'next' link as the current one. */
! *last = build (COMPOUND_EXPR, void_type_node, *last, next);
! TREE_OPERAND (*(i->tp), 1) = t;
/* Update the iterator to the new stmt. */
if (mode == TSI_CHAIN_START)
! i->tp = &(TREE_OPERAND (*(i->tp), 1));
else if (mode == TSI_CHAIN_END)
! i->tp = last != &t ? last : &(TREE_OPERAND (*(i->tp), 1));
}
}
/* Remove a stmt from the tree list. The iterator is updated to point to
the next stmt. */
void
tsi_delink (tree_stmt_iterator *i)
{
! if (TREE_CODE (*(i->tp)) == COMPOUND_EXPR)
{
/* All that is needed here is to change the parent to point to the next
stmt in the chain. */
! *(i->tp) = TREE_OPERAND (*(i->tp), 1);
}
else
{
--- 5298,5348 ----
last = &TREE_OPERAND (*last, 1))
continue;
! nextp = &TREE_OPERAND (cur, 1);
! next = *nextp;
/* Create a new node with the same 'next' link as the current one. */
! ce = build (COMPOUND_EXPR, void_type_node, *last, next);
!
! /* If the 'next' statement is a COMPOUND_EXPR and was the first
! statement of a basic block, we need to adjust the 'head_tree_p'
! field of that block because we are about to move the statement one
! position down in the CE chain. */
! {
! basic_block bb = bb_for_stmt (next);
! if (bb && bb->head_tree_p == nextp)
! {
! /* We may also need to update end_tree_p. */
! if (bb->head_tree_p == bb->end_tree_p)
! bb->end_tree_p = &TREE_OPERAND (ce, 1);
! bb->head_tree_p = &TREE_OPERAND (ce, 1);
! }
! }
!
! *last = ce;
! TREE_OPERAND (cur, 1) = t;
/* Update the iterator to the new stmt. */
if (mode == TSI_CHAIN_START)
! i->tp = nextp;
else if (mode == TSI_CHAIN_END)
! i->tp = (last != &t ? last : nextp);
}
}
+
/* Remove a stmt from the tree list. The iterator is updated to point to
the next stmt. */
void
tsi_delink (tree_stmt_iterator *i)
{
+ tree cur = *i->tp;
! if (TREE_CODE (cur) == COMPOUND_EXPR)
{
/* All that is needed here is to change the parent to point to the next
stmt in the chain. */
! *i->tp = TREE_OPERAND (cur, 1);
}
else
{
*************** tsi_delink (tree_stmt_iterator *i)
*** 5299,5305 ****
this node with an empty statement. Choose the latter for now, and
make the iterator return NULL, so that no further iterating takes
place. */
! *(i->tp) = build_empty_stmt ();
i->tp = NULL;
}
}
--- 5352,5358 ----
this node with an empty statement. Choose the latter for now, and
make the iterator return NULL, so that no further iterating takes
place. */
! *i->tp = build_empty_stmt ();
i->tp = NULL;
}
}