Subzero: Improve the use of NodeList objects.
Currently NodeList is defined as std::vector<CfgNode*>, but in the future it may be desirable to change it to something like std::list<CfgNode*> so that it is easier to split edges and insert the new nodes at the right locations, rather than re-sorting them in a separate pass.
This gets us closer by using foo.front() instead of foo[0]. There are still a couple more places using the [] operator, but the changes would be more intrusive.
Also, a few instances of ".size()==0" are changed to the possibly more efficient ".empty()".
BUG= none
R=jvoung@chromium.org, kschimpf@google.com
Review URL: https://codereview.chromium.org/704753007
diff --git a/src/IceCfgNode.cpp b/src/IceCfgNode.cpp
index 494ab4b..52789b9 100644
--- a/src/IceCfgNode.cpp
+++ b/src/IceCfgNode.cpp
@@ -750,7 +750,7 @@
// unconditional branch, contract the node by repointing all its
// in-edges to its successor.
void CfgNode::contractIfEmpty() {
- if (InEdges.size() == 0)
+ if (InEdges.empty())
return;
Inst *Branch = NULL;
for (Inst *I : Insts) {
@@ -763,18 +763,23 @@
}
Branch->setDeleted();
assert(OutEdges.size() == 1);
- // Repoint all this node's in-edges to this node's successor.
- for (CfgNode *Pred : InEdges) {
- for (auto I = Pred->OutEdges.begin(), E = Pred->OutEdges.end(); I != E;
- ++I) {
- if (*I == this) {
- *I = OutEdges[0];
- OutEdges[0]->InEdges.push_back(Pred);
+ // Repoint all this node's in-edges to this node's successor, unless
+ // this node's successor is actually itself (in which case the
+ // statement "OutEdges.front()->InEdges.push_back(Pred)" could
+ // invalidate the iterator over this->InEdges).
+ if (OutEdges.front() != this) {
+ for (CfgNode *Pred : InEdges) {
+ for (auto I = Pred->OutEdges.begin(), E = Pred->OutEdges.end(); I != E;
+ ++I) {
+ if (*I == this) {
+ *I = OutEdges.front();
+ OutEdges.front()->InEdges.push_back(Pred);
+ }
}
- }
- for (Inst *I : Pred->getInsts()) {
- if (!I->isDeleted())
- I->repointEdge(this, OutEdges[0]);
+ for (Inst *I : Pred->getInsts()) {
+ if (!I->isDeleted())
+ I->repointEdge(this, OutEdges.front());
+ }
}
}
InEdges.clear();