DATA_FLOW code style improvements: move template definitions to separate files, encapsulate CNT_NOTINIT constant
This commit is contained in:
@@ -223,7 +223,9 @@ set(CFG _src/CFGraph/IR.cpp
|
|||||||
|
|
||||||
set(DATA_FLOW
|
set(DATA_FLOW
|
||||||
_src/CFGraph/DataFlow/data_flow.h
|
_src/CFGraph/DataFlow/data_flow.h
|
||||||
|
_src/CFGraph/DataFlow/data_flow_impl.h
|
||||||
_src/CFGraph/DataFlow/backward_data_flow.h
|
_src/CFGraph/DataFlow/backward_data_flow.h
|
||||||
|
_src/CFGraph/DataFlow/backward_data_flow_impl.h
|
||||||
)
|
)
|
||||||
|
|
||||||
set(CREATE_INTER_T _src/CreateInterTree/CreateInterTree.cpp
|
set(CREATE_INTER_T _src/CreateInterTree/CreateInterTree.cpp
|
||||||
|
|||||||
@@ -8,7 +8,6 @@
|
|||||||
#include "../../Utils/SgUtils.h"
|
#include "../../Utils/SgUtils.h"
|
||||||
#include "../CFGraph.h"
|
#include "../CFGraph.h"
|
||||||
#include "../IR.h"
|
#include "../IR.h"
|
||||||
#include "../RD_subst.h"
|
|
||||||
|
|
||||||
template <class DataType, class NodeType>
|
template <class DataType, class NodeType>
|
||||||
class BackwardDataFlowAnalysis : public DataFlowAnalysis<DataType, NodeType> {
|
class BackwardDataFlowAnalysis : public DataFlowAnalysis<DataType, NodeType> {
|
||||||
@@ -18,91 +17,4 @@ public:
|
|||||||
void fit(const std::vector<SAPFOR::BasicBlock*>& blocks);
|
void fit(const std::vector<SAPFOR::BasicBlock*>& blocks);
|
||||||
};
|
};
|
||||||
|
|
||||||
// minimizes the number of blocks beween the ends of back edges
|
#include "backward_data_flow_impl.h"
|
||||||
template <class DataType, class NodeType>
|
|
||||||
std::vector<SAPFOR::BasicBlock*> BackwardDataFlowAnalysis<DataType, NodeType>::reorderSequence(const std::vector<SAPFOR::BasicBlock*>& blocks,
|
|
||||||
const std::set<SAPFOR::BasicBlock*> back_edge_sources)
|
|
||||||
{
|
|
||||||
std::vector<SAPFOR::BasicBlock*> res = { };
|
|
||||||
|
|
||||||
auto blocks_end = blocks.rend();
|
|
||||||
for (auto it = blocks.rbegin(); it < blocks_end; it++)
|
|
||||||
{
|
|
||||||
SAPFOR::BasicBlock* curr = *it;
|
|
||||||
auto res_end = res.end();
|
|
||||||
auto inserter = res.begin();
|
|
||||||
if (back_edge_sources.count(curr) == 0)
|
|
||||||
{
|
|
||||||
auto curr_next_begin = curr->getNext().begin();
|
|
||||||
auto curr_next_end = curr->getNext().end();
|
|
||||||
while (inserter < res_end && std::find(curr_next_begin, curr_next_end, *inserter) == curr_next_end)
|
|
||||||
inserter++;
|
|
||||||
}
|
|
||||||
|
|
||||||
res.insert(inserter, curr);
|
|
||||||
}
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <class DataType, class NodeType>
|
|
||||||
void BackwardDataFlowAnalysis<DataType, NodeType>::fit(const std::vector<SAPFOR::BasicBlock*>& blocks)
|
|
||||||
{
|
|
||||||
std::set<std::pair<SAPFOR::BasicBlock*, SAPFOR::BasicBlock*>> back_edges = {};
|
|
||||||
|
|
||||||
bool returned = false;
|
|
||||||
std::map<SAPFOR::BasicBlock*, std::set<SAPFOR::BasicBlock*>> back_edges_by_src;
|
|
||||||
|
|
||||||
auto blocks_sorted = sortCfgNodes(blocks, &back_edges);
|
|
||||||
|
|
||||||
std::set<SAPFOR::BasicBlock*> back_edge_sources;
|
|
||||||
|
|
||||||
for (auto& edge : back_edges)
|
|
||||||
{
|
|
||||||
back_edges_by_src[edge.first].insert(edge.second);
|
|
||||||
back_edge_sources.insert(edge.first);
|
|
||||||
}
|
|
||||||
|
|
||||||
back_edges.clear();
|
|
||||||
|
|
||||||
blocks_sorted = reorderSequence(blocks_sorted, back_edge_sources);
|
|
||||||
back_edge_sources.clear();
|
|
||||||
|
|
||||||
std::reverse(blocks_sorted.begin(), blocks_sorted.end());
|
|
||||||
|
|
||||||
nodes.clear();
|
|
||||||
std::map<SAPFOR::BasicBlock*, NodeType*> node_by_block;
|
|
||||||
|
|
||||||
for (auto block : blocks_sorted)
|
|
||||||
{
|
|
||||||
NodeType* node = createNode(block);
|
|
||||||
nodes.push_back(node);
|
|
||||||
node_by_block[block] = node;
|
|
||||||
}
|
|
||||||
|
|
||||||
int nodes_size = nodes.size();
|
|
||||||
|
|
||||||
for (int i = 0; i < nodes_size; i++)
|
|
||||||
{
|
|
||||||
NodeType* node = nodes[i];
|
|
||||||
|
|
||||||
auto back_edges_by_src_it = back_edges_by_src.find(node->getBlock());
|
|
||||||
if (back_edges_by_src_it != back_edges_by_src.end())
|
|
||||||
{
|
|
||||||
// This node is a source for back edge
|
|
||||||
for (auto dest : back_edges_by_src_it->second)
|
|
||||||
{
|
|
||||||
auto node_by_block_it = node_by_block.find(dest);
|
|
||||||
if (node_by_block_it != node_by_block.end())
|
|
||||||
node_by_block_it->second->getRollback().insert(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto next : node->getBlock()->getNext())
|
|
||||||
{
|
|
||||||
auto node_by_block_it = node_by_block.find(next);
|
|
||||||
if (node_by_block_it != node_by_block.end())
|
|
||||||
node->getPrevBlocks().insert(node_by_block_it->second);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,103 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "backward_data_flow.h"
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <set>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
#include "../../Utils/SgUtils.h"
|
||||||
|
#include "../CFGraph.h"
|
||||||
|
#include "../IR.h"
|
||||||
|
#include "../RD_subst.h"
|
||||||
|
|
||||||
|
/* Note: this file should be included in backward_data_flow.h to provide template definitions */
|
||||||
|
|
||||||
|
// minimizes the number of blocks beween the ends of back edges
|
||||||
|
template <class DataType, class NodeType>
|
||||||
|
std::vector<SAPFOR::BasicBlock*>
|
||||||
|
BackwardDataFlowAnalysis<DataType, NodeType>::reorderSequence(const std::vector<SAPFOR::BasicBlock*>& blocks,
|
||||||
|
const std::set<SAPFOR::BasicBlock*> back_edge_sources)
|
||||||
|
{
|
||||||
|
std::vector<SAPFOR::BasicBlock*> res = { };
|
||||||
|
|
||||||
|
auto blocks_end = blocks.rend();
|
||||||
|
for (auto it = blocks.rbegin(); it < blocks_end; it++)
|
||||||
|
{
|
||||||
|
SAPFOR::BasicBlock* curr = *it;
|
||||||
|
auto res_end = res.end();
|
||||||
|
auto inserter = res.begin();
|
||||||
|
if (back_edge_sources.count(curr) == 0)
|
||||||
|
{
|
||||||
|
auto curr_next_begin = curr->getNext().begin();
|
||||||
|
auto curr_next_end = curr->getNext().end();
|
||||||
|
while (inserter < res_end && std::find(curr_next_begin, curr_next_end, *inserter) == curr_next_end)
|
||||||
|
inserter++;
|
||||||
|
}
|
||||||
|
|
||||||
|
res.insert(inserter, curr);
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class DataType, class NodeType>
|
||||||
|
void BackwardDataFlowAnalysis<DataType, NodeType>::fit(const std::vector<SAPFOR::BasicBlock*>& blocks)
|
||||||
|
{
|
||||||
|
std::set<std::pair<SAPFOR::BasicBlock*, SAPFOR::BasicBlock*>> back_edges = {};
|
||||||
|
|
||||||
|
bool returned = false;
|
||||||
|
std::map<SAPFOR::BasicBlock*, std::set<SAPFOR::BasicBlock*>> back_edges_by_src;
|
||||||
|
|
||||||
|
auto blocks_sorted = sortCfgNodes(blocks, &back_edges);
|
||||||
|
|
||||||
|
std::set<SAPFOR::BasicBlock*> back_edge_sources;
|
||||||
|
|
||||||
|
for (auto& edge : back_edges)
|
||||||
|
{
|
||||||
|
back_edges_by_src[edge.first].insert(edge.second);
|
||||||
|
back_edge_sources.insert(edge.first);
|
||||||
|
}
|
||||||
|
|
||||||
|
back_edges.clear();
|
||||||
|
|
||||||
|
blocks_sorted = reorderSequence(blocks_sorted, back_edge_sources);
|
||||||
|
back_edge_sources.clear();
|
||||||
|
|
||||||
|
std::reverse(blocks_sorted.begin(), blocks_sorted.end());
|
||||||
|
|
||||||
|
nodes.clear();
|
||||||
|
std::map<SAPFOR::BasicBlock*, NodeType*> node_by_block;
|
||||||
|
|
||||||
|
for (auto block : blocks_sorted)
|
||||||
|
{
|
||||||
|
NodeType* node = createNode(block);
|
||||||
|
nodes.push_back(node);
|
||||||
|
node_by_block[block] = node;
|
||||||
|
}
|
||||||
|
|
||||||
|
int nodes_size = nodes.size();
|
||||||
|
|
||||||
|
for (int i = 0; i < nodes_size; i++)
|
||||||
|
{
|
||||||
|
NodeType* node = nodes[i];
|
||||||
|
|
||||||
|
auto back_edges_by_src_it = back_edges_by_src.find(node->getBlock());
|
||||||
|
if (back_edges_by_src_it != back_edges_by_src.end())
|
||||||
|
{
|
||||||
|
// This node is a source for back edge
|
||||||
|
for (auto dest : back_edges_by_src_it->second)
|
||||||
|
{
|
||||||
|
auto node_by_block_it = node_by_block.find(dest);
|
||||||
|
if (node_by_block_it != node_by_block.end())
|
||||||
|
node_by_block_it->second->getRollback().insert(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto next : node->getBlock()->getNext())
|
||||||
|
{
|
||||||
|
auto node_by_block_it = node_by_block.find(next);
|
||||||
|
if (node_by_block_it != node_by_block.end())
|
||||||
|
node->getPrevBlocks().insert(node_by_block_it->second);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,13 +6,10 @@
|
|||||||
#include "../CFGraph.h"
|
#include "../CFGraph.h"
|
||||||
#include "../IR.h"
|
#include "../IR.h"
|
||||||
|
|
||||||
enum
|
|
||||||
{
|
|
||||||
CNT_NOTINIT = 0
|
|
||||||
};
|
|
||||||
|
|
||||||
template <class DataType>
|
template <class DataType>
|
||||||
class DataFlowAnalysisNode {
|
class DataFlowAnalysisNode {
|
||||||
|
static const int CNT_NOTINIT = 0;
|
||||||
|
|
||||||
int in_cnt = CNT_NOTINIT, out_cnt = CNT_NOTINIT;
|
int in_cnt = CNT_NOTINIT, out_cnt = CNT_NOTINIT;
|
||||||
|
|
||||||
std::set<int> rollback;
|
std::set<int> rollback;
|
||||||
@@ -41,6 +38,7 @@ public:
|
|||||||
|
|
||||||
void setInCnt(int cnt) { in_cnt = cnt; }
|
void setInCnt(int cnt) { in_cnt = cnt; }
|
||||||
void setOutCnt(int cnt) { out_cnt = cnt; }
|
void setOutCnt(int cnt) { out_cnt = cnt; }
|
||||||
|
static int getStartCounter() { return CNT_NOTINIT; }
|
||||||
|
|
||||||
std::set<int>& getRollback() { return rollback; }
|
std::set<int>& getRollback() { return rollback; }
|
||||||
std::set<int>& getIgnoreRollback() { return ignore_rollback; }
|
std::set<int>& getIgnoreRollback() { return ignore_rollback; }
|
||||||
@@ -66,96 +64,4 @@ public:
|
|||||||
~DataFlowAnalysis();
|
~DataFlowAnalysis();
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class DataType>
|
#include "data_flow_impl.h"
|
||||||
DataFlowAnalysisNode<DataType>::DataFlowAnalysisNode() {
|
|
||||||
getRollback() = {};
|
|
||||||
getIgnoreRollback() = {};
|
|
||||||
prev_blocks = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
template <class DataType>
|
|
||||||
void DataFlowAnalysisNode<DataType>::doStep()
|
|
||||||
{
|
|
||||||
int in_max_cnt = CNT_NOTINIT, out_max_cnt = CNT_NOTINIT;
|
|
||||||
for (auto next : prev_blocks)
|
|
||||||
{
|
|
||||||
if (in_cnt < next->out_cnt)
|
|
||||||
{
|
|
||||||
for (const auto& byOut : next->getOut())
|
|
||||||
{
|
|
||||||
bool inserted = addIn({ byOut });
|
|
||||||
|
|
||||||
if (inserted)
|
|
||||||
{
|
|
||||||
if (next->out_cnt > in_max_cnt)
|
|
||||||
in_max_cnt = next->out_cnt;
|
|
||||||
|
|
||||||
inserted = forwardData({ byOut });
|
|
||||||
|
|
||||||
if (inserted && next->out_cnt > out_max_cnt)
|
|
||||||
out_max_cnt = next->out_cnt;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool was_notinit = (out_cnt == CNT_NOTINIT);
|
|
||||||
|
|
||||||
if (out_max_cnt != CNT_NOTINIT)
|
|
||||||
out_cnt = out_max_cnt;
|
|
||||||
|
|
||||||
if (in_max_cnt != CNT_NOTINIT)
|
|
||||||
in_cnt = in_max_cnt;
|
|
||||||
|
|
||||||
// TODO: fix counter overflow
|
|
||||||
if (was_notinit)
|
|
||||||
{
|
|
||||||
out_cnt++;
|
|
||||||
in_cnt++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <class DataType, class NodeType>
|
|
||||||
void DataFlowAnalysis<DataType, NodeType>::analyze() {
|
|
||||||
auto curr = 0;
|
|
||||||
auto stop = nodes.size();
|
|
||||||
|
|
||||||
while (curr != stop)
|
|
||||||
{
|
|
||||||
auto curr_bb = nodes[curr];
|
|
||||||
curr_bb->doStep();
|
|
||||||
|
|
||||||
const auto& jumps = curr_bb->getRollback();
|
|
||||||
if (jumps.size() != 0)
|
|
||||||
{
|
|
||||||
auto& ignored_jumps = curr_bb->getIgnoreRollback();
|
|
||||||
|
|
||||||
bool jump = false;
|
|
||||||
for (const auto& jump_to : jumps)
|
|
||||||
{
|
|
||||||
if (ignored_jumps.insert(jump_to).second && curr_bb->newerThan(nodes[jump_to]))
|
|
||||||
{
|
|
||||||
jump = true;
|
|
||||||
curr = jump_to;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!jump)
|
|
||||||
curr_bb->getIgnoreRollback().clear();
|
|
||||||
else
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
curr++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <class DataType, class NodeType>
|
|
||||||
DataFlowAnalysis<DataType, NodeType>::~DataFlowAnalysis()
|
|
||||||
{
|
|
||||||
for (DataFlowAnalysisNode<DataType>* node : nodes)
|
|
||||||
delete node;
|
|
||||||
|
|
||||||
nodes.clear();
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "data_flow.h"
|
||||||
|
|
||||||
|
#include<vector>
|
||||||
|
#include<set>
|
||||||
|
|
||||||
|
#include "../../Utils/SgUtils.h"
|
||||||
|
#include "../CFGraph.h"
|
||||||
|
#include "../IR.h"
|
||||||
|
|
||||||
|
/* Note: this file should be included in data_flow.h to provide template definitions */
|
||||||
|
|
||||||
|
/* definitions for DataFlowAnalysisNode class */
|
||||||
|
|
||||||
|
template <class DataType>
|
||||||
|
const int DataFlowAnalysisNode<DataType>::CNT_NOTINIT;
|
||||||
|
|
||||||
|
template <class DataType>
|
||||||
|
DataFlowAnalysisNode<DataType>::DataFlowAnalysisNode()
|
||||||
|
{
|
||||||
|
getRollback() = {};
|
||||||
|
getIgnoreRollback() = {};
|
||||||
|
prev_blocks = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class DataType>
|
||||||
|
void DataFlowAnalysisNode<DataType>::doStep()
|
||||||
|
{
|
||||||
|
int in_max_cnt = CNT_NOTINIT, out_max_cnt = CNT_NOTINIT;
|
||||||
|
for (auto next : prev_blocks)
|
||||||
|
{
|
||||||
|
if (in_cnt < next->out_cnt)
|
||||||
|
{
|
||||||
|
for (const auto& byOut : next->getOut())
|
||||||
|
{
|
||||||
|
bool inserted = addIn({ byOut });
|
||||||
|
|
||||||
|
if (inserted)
|
||||||
|
{
|
||||||
|
if (next->out_cnt > in_max_cnt)
|
||||||
|
in_max_cnt = next->out_cnt;
|
||||||
|
|
||||||
|
inserted = forwardData({ byOut });
|
||||||
|
|
||||||
|
if (inserted && next->out_cnt > out_max_cnt)
|
||||||
|
out_max_cnt = next->out_cnt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool was_notinit = (out_cnt == CNT_NOTINIT);
|
||||||
|
|
||||||
|
if (out_max_cnt != CNT_NOTINIT)
|
||||||
|
out_cnt = out_max_cnt;
|
||||||
|
|
||||||
|
if (in_max_cnt != CNT_NOTINIT)
|
||||||
|
in_cnt = in_max_cnt;
|
||||||
|
|
||||||
|
// TODO: fix counter overflow
|
||||||
|
if (was_notinit)
|
||||||
|
{
|
||||||
|
out_cnt++;
|
||||||
|
in_cnt++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* definitions for DataFlowAnalysis class */
|
||||||
|
|
||||||
|
template <class DataType, class NodeType>
|
||||||
|
void DataFlowAnalysis<DataType, NodeType>::analyze() {
|
||||||
|
auto curr = 0;
|
||||||
|
auto stop = nodes.size();
|
||||||
|
|
||||||
|
while (curr != stop)
|
||||||
|
{
|
||||||
|
auto curr_bb = nodes[curr];
|
||||||
|
curr_bb->doStep();
|
||||||
|
|
||||||
|
const auto& jumps = curr_bb->getRollback();
|
||||||
|
if (jumps.size() != 0)
|
||||||
|
{
|
||||||
|
auto& ignored_jumps = curr_bb->getIgnoreRollback();
|
||||||
|
|
||||||
|
bool jump = false;
|
||||||
|
for (const auto& jump_to : jumps)
|
||||||
|
{
|
||||||
|
if (ignored_jumps.insert(jump_to).second && curr_bb->newerThan(nodes[jump_to]))
|
||||||
|
{
|
||||||
|
jump = true;
|
||||||
|
curr = jump_to;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!jump)
|
||||||
|
curr_bb->getIgnoreRollback().clear();
|
||||||
|
else
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
curr++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class DataType, class NodeType>
|
||||||
|
DataFlowAnalysis<DataType, NodeType>::~DataFlowAnalysis()
|
||||||
|
{
|
||||||
|
for (DataFlowAnalysisNode<DataType>* node : nodes)
|
||||||
|
delete node;
|
||||||
|
|
||||||
|
nodes.clear();
|
||||||
|
}
|
||||||
@@ -684,7 +684,7 @@ void runLiveVariableAnalysis(const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>&
|
|||||||
}
|
}
|
||||||
|
|
||||||
set<LiveVarAnalysisNode*> exits;
|
set<LiveVarAnalysisNode*> exits;
|
||||||
int max_cnt = CNT_NOTINIT;
|
int max_cnt = LiveVarAnalysisNode::getStartCounter();
|
||||||
for (auto block : func_it->second->getNodes())
|
for (auto block : func_it->second->getNodes())
|
||||||
{
|
{
|
||||||
if (block->getBlock()->getNext().size() == 0)
|
if (block->getBlock()->getNext().size() == 0)
|
||||||
|
|||||||
Reference in New Issue
Block a user