Fix a bug in loop analyzer
In getDepthFirstSpanningTree(), using map "number" to avoid revisiting is buggy. The map "number" updates only after a node is visited. before visiting, multiple pointers to one basicblocks may already be pushed to the worklist. Rather, we should record the basicblock immediately after the basicblock being pushed.
MODIFY
ir/function.cpp
ir/function.cpp
@@ -484,6 +484,7 @@ void LoopAnalysis::getDepthFirstSpanningTree() {
unsigned current = 0;
vector<pair<const BasicBlock*, bool>> worklist = { {&f.getFirstBB(), false} };
+ set<const BasicBlock *> visited;
while(!worklist.empty()) {
auto &[bb, flag] = worklist.back();
if (flag) {
@@ -495,7 +496,7 @@ void LoopAnalysis::getDepthFirstSpanningTree() {
flag = true;
for (auto &tgt : bb->targets())
- if (!number.count(&tgt))
+ if (visited.insert(&tgt).second)
worklist.emplace_back(&tgt, false);
}
}

Fix a bug in loop analyzer (#543)

In getDepthFirstSpanningTree(), using map "number" to avoid revisiting is buggy. The map "number" updates only after a node is visited. before visiting, multiple pointers to one basicblocks may already be pushed to the worklist. Rather, we should record the basicblock immediately after the basicblock being pushed.