Skip to content

Commit 40451d8

Browse files
opt checkpoint. changed DFSUtil from recursive to iterative
1 parent 2c30316 commit 40451d8

1 file changed

Lines changed: 35 additions & 8 deletions

File tree

src/FlowAware.cpp

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,18 +1380,46 @@ void IR2Vec_FA::traverseRD(
13801380
timeStack.push_back(inst);
13811381
}
13821382

1383+
// void IR2Vec_FA::DFSUtil(
1384+
// const llvm::Instruction *inst,
1385+
// std::unordered_map<const llvm::Instruction *, bool> &Visited,
1386+
// llvm::SmallVector<const llvm::Instruction *, 10> &nodeList) {
1387+
1388+
// nodeList.push_back(inst);
1389+
// Visited[inst] = true;
1390+
// auto RD = reverseReachingDefsMap[inst];
1391+
1392+
// for (auto defs : RD) {
1393+
// if (Visited.find(defs) == Visited.end()) {
1394+
// DFSUtil(defs, Visited, nodeList);
1395+
// }
1396+
// }
1397+
// }
1398+
13831399
void IR2Vec_FA::DFSUtil(
13841400
const llvm::Instruction *inst,
13851401
std::unordered_map<const llvm::Instruction *, bool> &Visited,
1386-
llvm::SmallVector<const llvm::Instruction *, 10> &set) {
1402+
llvm::SmallVector<const llvm::Instruction *, 10> &nodeList) {
13871403

1388-
Visited[inst] = true;
1389-
auto RD = reverseReachingDefsMap[inst];
1404+
std::stack<const llvm::Instruction *> stack;
1405+
stack.push(inst);
1406+
1407+
while (!stack.empty()) {
1408+
auto current = stack.top();
1409+
stack.pop();
13901410

1391-
for (auto defs : RD) {
1392-
if (Visited.find(defs) == Visited.end()) {
1393-
set.push_back(defs);
1394-
DFSUtil(defs, Visited, set);
1411+
// Only process the node if it hasn't been visited
1412+
nodeList.push_back(current);
1413+
Visited[current] = true;
1414+
1415+
auto RD = reverseReachingDefsMap[current];
1416+
// Push elements onto the stack in reverse order to preserve the order
1417+
// of visitation that would be achieved with recursion.
1418+
// However, only push nodes that have not been visited
1419+
for (auto it = RD.rbegin(); it != RD.rend(); ++it) {
1420+
if (Visited.find(*it) == Visited.end()) {
1421+
stack.push(*it);
1422+
}
13951423
}
13961424
}
13971425
}
@@ -1423,7 +1451,6 @@ void IR2Vec_FA::getAllSCC() {
14231451
timeStack.pop_back();
14241452
if (Visited.find(inst) == Visited.end()) {
14251453
llvm::SmallVector<const llvm::Instruction *, 10> set;
1426-
set.push_back(inst);
14271454
DFSUtil(inst, Visited, set);
14281455
if (set.size() != 0)
14291456
allSCCs.push_back(set);

0 commit comments

Comments
 (0)