Merge branch 'master' into analyze_loops_with_IR

This commit is contained in:
2025-06-18 23:22:33 +03:00
4 changed files with 56 additions and 49 deletions

View File

@@ -1254,4 +1254,49 @@ map<FuncInfo*, vector<SAPFOR::BasicBlock*>>
fileFuncInfoMap[callFrom->fileName].push_back(callFrom); fileFuncInfoMap[callFrom->fileName].push_back(callFrom);
return buildCFG(commonBlocks, fileFuncInfoMap, settings); return buildCFG(commonBlocks, fileFuncInfoMap, settings);
}
void removedUnreachableBlocks(vector<SAPFOR::BasicBlock*>& blocks)
{
set<SAPFOR::BasicBlock*> reachable;
for (auto& b : blocks)
if (b->getInstructions().front()->isHeader() ||
b->getInstructions().front()->getInstruction()->getOperation() == SAPFOR::CFG_OP::ENTRY)
reachable.insert(b);
set<SAPFOR::BasicBlock*> worklist = reachable;
while (worklist.size() != 0)
{
set<SAPFOR::BasicBlock*> to_insert;
for (auto& b : worklist)
for (auto& next : b->getNext())
if (reachable.insert(next).second)
to_insert.insert(next);
worklist = to_insert;
}
auto remove_unreachable_it = remove_if(blocks.begin(), blocks.end(),
[&reachable](SAPFOR::BasicBlock* b)
{
if (reachable.find(b) == reachable.end())
{
for (auto& next : b->getNext())
if (reachable.find(next) != reachable.end())
next->removePrev(b);
delete b;
return true;
}
return false;
}
);
reachable.clear();
blocks.erase(remove_unreachable_it, blocks.end());
} }

View File

@@ -161,3 +161,5 @@ static inline void deleteCFG(std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*
} }
cfg.clear(); cfg.clear();
} }
void removedUnreachableBlocks(std::vector<SAPFOR::BasicBlock*>& blocks);

View File

@@ -6,6 +6,8 @@
#include <set> #include <set>
#include <algorithm> #include <algorithm>
#include "../../CFGraph/CFGraph.h"
using std::map; using std::map;
using std::string; using std::string;
using std::vector; using std::vector;
@@ -88,13 +90,13 @@ static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instru
instr_operation == SAPFOR::CFG_OP::STORE) instr_operation == SAPFOR::CFG_OP::STORE)
last_stack_op_useful = true; last_stack_op_useful = true;
for (auto e : res) for (auto& e : res)
{ {
def.insert(e); def.insert(e);
use.erase(e); use.erase(e);
} }
for (auto e : args) for (auto& e : args)
{ {
use.insert(e); use.insert(e);
def.erase(e); def.erase(e);
@@ -164,7 +166,7 @@ public:
{ {
const auto& example = dynamic_cast<DeadCodeAnalysisNode*>(*prev.begin())->next_notempty_out; const auto& example = dynamic_cast<DeadCodeAnalysisNode*>(*prev.begin())->next_notempty_out;
for (auto p : prev) for (auto& p : prev)
{ {
DeadCodeAnalysisNode* next = dynamic_cast<DeadCodeAnalysisNode*>(p); DeadCodeAnalysisNode* next = dynamic_cast<DeadCodeAnalysisNode*>(p);
@@ -188,7 +190,7 @@ public:
{ {
set<DeadCodeAnalysisNode*> current = {}; set<DeadCodeAnalysisNode*> current = {};
for (auto nextP : getPrevBlocks()) for (auto& nextP : getPrevBlocks())
{ {
DeadCodeAnalysisNode* next = dynamic_cast<DeadCodeAnalysisNode*>(nextP); DeadCodeAnalysisNode* next = dynamic_cast<DeadCodeAnalysisNode*>(nextP);
@@ -428,49 +430,7 @@ int removeDeadCode(SgStatement* func,
printInternalError(convertFileName(__FILE__).c_str(), __LINE__); printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
auto& cfg_pair = *(cfg.begin()); auto& cfg_pair = *(cfg.begin());
removedUnreachableBlocks(cfg_pair.second);
// delete unreachable blocks
set<SAPFOR::BasicBlock*> reachable;
for (auto b : cfg_pair.second)
if(b->getInstructions().front()->isHeader() ||
b->getInstructions().front()->getInstruction()->getOperation() == SAPFOR::CFG_OP::ENTRY)
reachable.insert(b);
set<SAPFOR::BasicBlock*> worklist = reachable;
while (worklist.size() != 0)
{
set<SAPFOR::BasicBlock*> to_insert;
for(auto b : worklist)
for(auto next: b->getNext())
if(reachable.insert(next).second)
to_insert.insert(next);
worklist = to_insert;
}
auto remove_unreachable_it = remove_if(cfg_pair.second.begin(), cfg_pair.second.end(),
[&reachable](SAPFOR::BasicBlock* b)
{
if (reachable.find(b) == reachable.end())
{
for(auto next: b->getNext())
if(reachable.find(next) != reachable.end())
next->removePrev(b);
delete b;
return true;
}
return false;
}
);
reachable.clear();
cfg_pair.second.erase(remove_unreachable_it, cfg_pair.second.end());
#if DEBUG_IR #if DEBUG_IR
dumpCFG({ cfg_pair }, false); dumpCFG({ cfg_pair }, false);
@@ -481,7 +441,7 @@ int removeDeadCode(SgStatement* func,
map<string, FuncInfo*> funcByName; map<string, FuncInfo*> funcByName;
for (auto& byFile : allFuncs) for (auto& byFile : allFuncs)
for (auto byFunc : byFile.second) for (auto& byFunc : byFile.second)
funcByName[byFunc->funcName] = byFunc; funcByName[byFunc->funcName] = byFunc;
DeadCodeAnalysis analysis_object(func_parameters, funcByName); DeadCodeAnalysis analysis_object(func_parameters, funcByName);

View File

@@ -1,3 +1,3 @@
#pragma once #pragma once
#define VERSION_SPF "2429" #define VERSION_SPF "2430"