dead code: improve perfomance by removing unnecessary checks

This commit is contained in:
2024-04-07 17:40:30 +03:00
parent e33fe45a2b
commit 9bc4fa246c

View File

@@ -16,39 +16,6 @@ using std::remove_if;
#define PRINT_USELESS_STATEMENTS 1
#define DEBUG_IR 0
// detect wich registers are used at more than one block
// such registers should participate in live analysis to spread information about useful instructions
// such registers appears at loops
static void fillSharedRegs(const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& cfg, set<SAPFOR::Argument*>& shared_regs)
{
map<SAPFOR::Argument*, SAPFOR::BasicBlock*> used_at;
for (const auto& byFunc : cfg)
{
for (const auto& byBlock : byFunc.second)
{
for (const auto& byIrBlock : byBlock->getInstructions())
{
auto instr = byIrBlock->getInstruction();
set<SAPFOR::Argument*> used = { instr->getResult(), instr->getArg1(), instr->getArg2() };
for (auto arg : used)
{
if(arg && arg->getType() == SAPFOR::CFG_ARG_TYPE::REG)
{
auto it = used_at.find(arg);
if (it == used_at.end())
used_at[arg] = byBlock;
else if(it->second != byBlock)
shared_regs.insert(arg);
}
}
}
}
}
}
static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* instr,
set<SAPFOR::Argument*>& use, set<SAPFOR::Argument*>& def,
vector<SAPFOR::Argument*>& formal_parameters,
@@ -179,7 +146,6 @@ private:
vector<SAPFOR::Argument*>& formal_parameters;
const map<string, FuncInfo*>& funcByName;
const set<SAPFOR::Argument*>& shared_regs;
public:
bool updateJumpStatus()
{
@@ -284,8 +250,7 @@ public:
for (SAPFOR::Argument* arg : use)
{
if(arg && (arg->getType() == SAPFOR::CFG_ARG_TYPE::VAR ||
(arg->getType() == SAPFOR::CFG_ARG_TYPE::REG && shared_regs.find(arg) != shared_regs.end())))
if(arg && (arg->getType() == SAPFOR::CFG_ARG_TYPE::VAR || arg->getType() == SAPFOR::CFG_ARG_TYPE::REG))
{
bool this_block = usedByThisBlock.find(arg) != usedByThisBlock.end();
@@ -344,12 +309,10 @@ public:
DeadCodeAnalysisNode(SAPFOR::BasicBlock* block,
vector<SAPFOR::Argument*>& formal_parameters,
const map<string, FuncInfo*>& funcByName,
const set<SAPFOR::Argument*>& shared_regs)
const map<string, FuncInfo*>& funcByName)
:
formal_parameters(formal_parameters),
funcByName(funcByName),
shared_regs(shared_regs)
funcByName(funcByName)
{
setBlock(block);
useful.resize(block->getInstructions().size(), false);
@@ -363,20 +326,17 @@ class DeadCodeAnalysis : public BackwardDataFlowAnalysis<DeadCodeAnalysisNode>
protected:
vector<SAPFOR::Argument*>& formal_parameters;
const map<string, FuncInfo*>& funcByName;
const set<SAPFOR::Argument*>& shared_regs;
DeadCodeAnalysisNode* createNode(SAPFOR::BasicBlock* block) override
{
return new DeadCodeAnalysisNode(block, formal_parameters, funcByName, shared_regs);
return new DeadCodeAnalysisNode(block, formal_parameters, funcByName);
}
public:
DeadCodeAnalysis(vector<SAPFOR::Argument*>& formal_parameters,
const map<string, FuncInfo*>& funcByName,
const set<SAPFOR::Argument*>& shared_regs)
const map<string, FuncInfo*>& funcByName)
:
formal_parameters(formal_parameters),
funcByName(funcByName),
shared_regs(shared_regs)
funcByName(funcByName)
{ }
};
@@ -460,10 +420,7 @@ void removeDeadCode(SgStatement* func,
for (auto byFunc : byFile.second)
funcByName[byFunc->funcName] = byFunc;
set<SAPFOR::Argument*> shared_regs;
fillSharedRegs({ cfg_pair }, shared_regs);
DeadCodeAnalysis analysis_object(func_parameters, funcByName, shared_regs);
DeadCodeAnalysis analysis_object(func_parameters, funcByName);
analysis_object.fit(cfg_pair.second);
analysis_object.analyze();