2 Commits

Author SHA1 Message Date
6bdc31698f codestyle issues 2024-04-03 21:16:50 +03:00
1ac7fcca2a fixes for dead code removing pass 2024-04-03 21:13:56 +03:00
5 changed files with 48 additions and 31 deletions

View File

@@ -10,7 +10,8 @@
#include "../IR.h"
template <class NodeType>
class BackwardDataFlowAnalysis : public DataFlowAnalysis<NodeType> {
class BackwardDataFlowAnalysis : public DataFlowAnalysis<NodeType>
{
std::vector<SAPFOR::BasicBlock*> reorderSequence(const std::vector<SAPFOR::BasicBlock*>& blocks,
const std::set<SAPFOR::BasicBlock*> back_edge_sources);
public:

View File

@@ -29,6 +29,7 @@ public:
virtual bool addIn(const DataType& data) = 0;
virtual bool addOut(const DataType& data) = 0;
virtual bool updateState() { return false; }
virtual bool forwardData(const DataType& data) = 0;
bool newerThan(const DataFlowAnalysisNode<DataType>* block) const { return out_cnt > block->in_cnt; }

View File

@@ -27,6 +27,8 @@ template <class DataType>
void DataFlowAnalysisNode<DataType>::doStep()
{
int in_max_cnt = CNT_NOTINIT, out_max_cnt = CNT_NOTINIT;
bool uniq_change = updateState();
for (auto next : prev_blocks)
{
if (in_cnt < next->out_cnt)
@@ -49,7 +51,7 @@ void DataFlowAnalysisNode<DataType>::doStep()
}
}
bool was_notinit = (out_cnt == CNT_NOTINIT);
uniq_change |= (out_cnt == CNT_NOTINIT);
if (out_max_cnt != CNT_NOTINIT)
out_cnt = out_max_cnt;
@@ -58,7 +60,7 @@ void DataFlowAnalysisNode<DataType>::doStep()
in_cnt = in_max_cnt;
// TODO: fix counter overflow
if (was_notinit)
if (uniq_change)
{
out_cnt++;
in_cnt++;
@@ -68,7 +70,8 @@ void DataFlowAnalysisNode<DataType>::doStep()
/* definitions for DataFlowAnalysis class */
template <class NodeType>
void DataFlowAnalysis<NodeType>::analyze() {
void DataFlowAnalysis<NodeType>::analyze()
{
auto curr = 0;
auto stop = nodes.size();

View File

@@ -39,7 +39,9 @@ static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instru
{
for (SAPFOR::Argument* r : res)
{
if (use.find(r) != use.end() || r->getMemType() != SAPFOR::CFG_MEM_TYPE::LOCAL_)
if (use.find(r) != use.end() ||
r->getMemType() != SAPFOR::CFG_MEM_TYPE::LOCAL_ ||
r->getType() != SAPFOR::CFG_ARG_TYPE::VAR)
{
useful = true;
break;
@@ -88,7 +90,7 @@ static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instru
use.insert(e);
def.erase(e);
}
insertIfVar(args.begin(), args.end(), usedByThisBlock);
}
@@ -133,7 +135,8 @@ static void buildUseDef(SAPFOR::BasicBlock* block, set<SAPFOR::Argument*>& use,
}
class DeadCodeAnalysisNode : public DataFlowAnalysisNode<map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>>> {
class DeadCodeAnalysisNode : public DataFlowAnalysisNode<map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>>>
{
private:
vector<bool> useful;
bool useful_block = false;
@@ -196,30 +199,44 @@ public:
return updated;
}
map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>> getIn() {
map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>> getIn()
{
return getBlock()->getLiveOut();
}
map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>> getOut() {
map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>> getOut()
{
return getBlock()->getLiveIn();
}
bool addIn(const map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>>& data) {
bool inserted = getBlock()->addLiveOut(data);
if (!useful_block)
inserted |= updateNextNotEmpty();
inserted |= updateJumpStatus();
return inserted;
bool addIn(const map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>>& data)
{
return getBlock()->addLiveOut(data);
}
bool addOut(const map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>>& data) {
bool addOut(const map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>>& data)
{
return getBlock()->addLiveIn(data);
}
bool forwardData(const map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>>& data) {
bool updateState()
{
if(getBlock()->getInstructions()[0]->getInstruction()->getOperator()->lineNumber() == 58)
int b = 51;
bool updated = false;
if (!useful_block)
updated |= updateNextNotEmpty();
updated |= updateJumpStatus();
updated |= this->forwardData({ });
return updated;
}
bool forwardData(const map<SAPFOR::Argument*, vector<SAPFOR::BasicBlock*>>& data)
{
bool inserted = false;
SAPFOR::BasicBlock* bb= getBlock();
@@ -298,24 +315,19 @@ public:
{
setBlock(block);
useful.resize(block->getInstructions().size(), false);
set<SAPFOR::Argument*> use, def;
set<SAPFOR::Argument*> usedByThisBlock;
buildUseDef(getBlock(), use, def, this->formal_parameters, useful, usedByThisBlock, funcByName);
for (SAPFOR::Argument* arg : use)
getBlock()->addLiveIn({ { arg, { getBlock() } } });
}
const vector<bool>& getResult() { return useful; }
};
class DeadCodeAnalysis : public BackwardDataFlowAnalysis<DeadCodeAnalysisNode> {
class DeadCodeAnalysis : public BackwardDataFlowAnalysis<DeadCodeAnalysisNode>
{
protected:
vector<SAPFOR::Argument*>& formal_parameters;
const map<string, FuncInfo*>& funcByName;
DeadCodeAnalysisNode* createNode(SAPFOR::BasicBlock* block) override {
DeadCodeAnalysisNode* createNode(SAPFOR::BasicBlock* block) override
{
return new DeadCodeAnalysisNode(block, formal_parameters, funcByName);
}
public:

View File

@@ -307,8 +307,8 @@ void InitPassesDependencies(map<passes, vector<passes>> &passDepsIn, set<passes>
Pass(REMOVE_OMP_DIRS) <= Pass(REMOVE_OMP_DIRS_TRANSFORM);
Pass(CALL_GRAPH2) <= Pass(REMOVE_DEAD_CODE);
list({ REMOVE_DEAD_CODE, REVERT_SUBST_EXPR_RD, CONVERT_LOOP_TO_ASSIGN, RESTORE_LOOP_FROM_ASSIGN }) <= Pass(REMOVE_DEAD_CODE_AND_UNPARSE);
list({ CALL_GRAPH2, REVERT_SUBST_EXPR_RD }) <= Pass(REMOVE_DEAD_CODE);
list({ REMOVE_DEAD_CODE, CONVERT_LOOP_TO_ASSIGN, RESTORE_LOOP_FROM_ASSIGN }) <= Pass(REMOVE_DEAD_CODE_AND_UNPARSE);
passesIgnoreStateDone.insert({ CREATE_PARALLEL_DIRS, INSERT_PARALLEL_DIRS, INSERT_SHADOW_DIRS, EXTRACT_PARALLEL_DIRS,
EXTRACT_SHADOW_DIRS, CREATE_REMOTES, UNPARSE_FILE, REMOVE_AND_CALC_SHADOW,