fixed code style
This commit is contained in:
@@ -52,10 +52,7 @@ namespace SAPFOR
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Argument(const Argument& other)
|
Argument(const Argument& other)
|
||||||
: number(other.number),
|
: number(other.number), type(other.type), mType(other.mType), value(other.value)
|
||||||
type(other.type),
|
|
||||||
mType(other.mType),
|
|
||||||
value(other.value)
|
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
void setType(CFG_ARG_TYPE newType) { type = newType; }
|
void setType(CFG_ARG_TYPE newType) { type = newType; }
|
||||||
|
|||||||
@@ -18,18 +18,16 @@ using namespace SAPFOR;
|
|||||||
typedef SAPFOR::BasicBlock BBlock;
|
typedef SAPFOR::BasicBlock BBlock;
|
||||||
typedef SAPFOR::Argument BArgument;
|
typedef SAPFOR::Argument BArgument;
|
||||||
|
|
||||||
static void printBlock(BBlock* block) {
|
static void printBlock(BBlock* block)
|
||||||
|
{
|
||||||
cout << "block - " << block->getNumber() << endl;
|
cout << "block - " << block->getNumber() << endl;
|
||||||
cout << "next -";
|
cout << "next -";
|
||||||
for (auto i : block->getNext())
|
for (auto i : block->getNext())
|
||||||
{
|
|
||||||
cout << " " << i->getNumber();
|
cout << " " << i->getNumber();
|
||||||
}
|
|
||||||
cout << endl << "prev -";
|
cout << endl << "prev -";
|
||||||
for (auto i : block->getPrev())
|
for (auto i : block->getPrev())
|
||||||
{
|
|
||||||
cout << " " << i->getNumber();
|
cout << " " << i->getNumber();
|
||||||
}
|
|
||||||
cout << endl;
|
cout << endl;
|
||||||
|
|
||||||
for (auto i : block->getInstructions())
|
for (auto i : block->getInstructions())
|
||||||
@@ -37,44 +35,48 @@ static void printBlock(BBlock* block) {
|
|||||||
string resValue = "";
|
string resValue = "";
|
||||||
string arg1Value = "";
|
string arg1Value = "";
|
||||||
string arg2Value = "";
|
string arg2Value = "";
|
||||||
if (i->getInstruction()->getResult() != nullptr && i->getInstruction()->getResult()->getType() == CFG_ARG_TYPE::VAR) {
|
|
||||||
|
if (i->getInstruction()->getResult() != NULL && i->getInstruction()->getResult()->getType() == CFG_ARG_TYPE::VAR)
|
||||||
|
{
|
||||||
resValue = i->getInstruction()->getResult()->getValue();
|
resValue = i->getInstruction()->getResult()->getValue();
|
||||||
i->getInstruction()->getResult()->setValue(i->getInstruction()->getResult()->getValue() + to_string(i->getInstruction()->getResult()->getNumber()));
|
i->getInstruction()->getResult()->setValue(i->getInstruction()->getResult()->getValue() + to_string(i->getInstruction()->getResult()->getNumber()));
|
||||||
}
|
}
|
||||||
if (i->getInstruction()->getArg1() != nullptr && i->getInstruction()->getArg1()->getType() == CFG_ARG_TYPE::VAR) {
|
|
||||||
|
if (i->getInstruction()->getArg1() != NULL && i->getInstruction()->getArg1()->getType() == CFG_ARG_TYPE::VAR)
|
||||||
|
{
|
||||||
arg1Value = i->getInstruction()->getArg1()->getValue();
|
arg1Value = i->getInstruction()->getArg1()->getValue();
|
||||||
i->getInstruction()->getArg1()->setValue(i->getInstruction()->getArg1()->getValue() + to_string(i->getInstruction()->getArg1()->getNumber()));
|
i->getInstruction()->getArg1()->setValue(i->getInstruction()->getArg1()->getValue() + to_string(i->getInstruction()->getArg1()->getNumber()));
|
||||||
}
|
}
|
||||||
if (i->getInstruction()->getArg2() != nullptr && i->getInstruction()->getArg2()->getType() == CFG_ARG_TYPE::VAR) {
|
|
||||||
|
if (i->getInstruction()->getArg2() != NULL && i->getInstruction()->getArg2()->getType() == CFG_ARG_TYPE::VAR)
|
||||||
|
{
|
||||||
arg2Value = i->getInstruction()->getArg2()->getValue();
|
arg2Value = i->getInstruction()->getArg2()->getValue();
|
||||||
i->getInstruction()->getArg2()->setValue(i->getInstruction()->getArg2()->getValue() + to_string(i->getInstruction()->getArg2()->getNumber()));
|
i->getInstruction()->getArg2()->setValue(i->getInstruction()->getArg2()->getValue() + to_string(i->getInstruction()->getArg2()->getNumber()));
|
||||||
}
|
}
|
||||||
|
|
||||||
cout << i->getNumber() << " " << i->getInstruction()->dump() << endl;
|
cout << i->getNumber() << " " << i->getInstruction()->dump() << endl;
|
||||||
|
|
||||||
if (i->getInstruction()->getResult() != nullptr && i->getInstruction()->getResult()->getType() == CFG_ARG_TYPE::VAR) {
|
if (i->getInstruction()->getResult() != NULL && i->getInstruction()->getResult()->getType() == CFG_ARG_TYPE::VAR)
|
||||||
i->getInstruction()->getResult()->setValue(resValue);
|
i->getInstruction()->getResult()->setValue(resValue);
|
||||||
}
|
|
||||||
if (i->getInstruction()->getArg1() != nullptr && i->getInstruction()->getArg1()->getType() == CFG_ARG_TYPE::VAR) {
|
if (i->getInstruction()->getArg1() != NULL && i->getInstruction()->getArg1()->getType() == CFG_ARG_TYPE::VAR)
|
||||||
i->getInstruction()->getArg1()->setValue(arg1Value);
|
i->getInstruction()->getArg1()->setValue(arg1Value);
|
||||||
}
|
|
||||||
if (i->getInstruction()->getArg2() != nullptr && i->getInstruction()->getArg2()->getType() == CFG_ARG_TYPE::VAR) {
|
if (i->getInstruction()->getArg2() != NULL && i->getInstruction()->getArg2()->getType() == CFG_ARG_TYPE::VAR)
|
||||||
i->getInstruction()->getArg2()->setValue(arg2Value);
|
i->getInstruction()->getArg2()->setValue(arg2Value);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cout << endl;
|
cout << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
static bool compareVectors(const vector<T>* vec1, const vector<T>* vec2) {
|
static bool compareVectors(const vector<T>* vec1, const vector<T>* vec2)
|
||||||
if (vec1 == vec2) {
|
{
|
||||||
|
if (vec1 == vec2)
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
|
|
||||||
if (!vec1 || !vec2) {
|
if (!vec1 || !vec2)
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
vector<T> sortedVec1 = *vec1;
|
vector<T> sortedVec1 = *vec1;
|
||||||
vector<T> sortedVec2 = *vec2;
|
vector<T> sortedVec2 = *vec2;
|
||||||
@@ -86,15 +88,16 @@ static bool compareVectors(const vector<T>* vec1, const vector<T>* vec2) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
static vector<T>* getCommonElements(const vector<vector<T>>* vectors) {
|
static vector<T>* getCommonElements(const vector<vector<T>>* vectors)
|
||||||
if (!vectors || vectors->empty()) {
|
{
|
||||||
|
if (!vectors || vectors->empty())
|
||||||
return new vector<T>(); // Return an empty vector if input is null or empty
|
return new vector<T>(); // Return an empty vector if input is null or empty
|
||||||
}
|
|
||||||
|
|
||||||
// Start with the first vector
|
// Start with the first vector
|
||||||
vector<T>* commonElements = new vector<T>((*vectors)[0]);
|
vector<T>* commonElements = new vector<T>((*vectors)[0]);
|
||||||
|
|
||||||
for (size_t i = 1; i < vectors->size(); ++i) {
|
for (size_t i = 1; i < vectors->size(); ++i)
|
||||||
|
{
|
||||||
vector<T> tempCommon;
|
vector<T> tempCommon;
|
||||||
|
|
||||||
// Sort the current vector and common result for intersection
|
// Sort the current vector and common result for intersection
|
||||||
@@ -113,23 +116,25 @@ static vector<T>* getCommonElements(const vector<vector<T>>* vectors) {
|
|||||||
*commonElements = tempCommon;
|
*commonElements = tempCommon;
|
||||||
|
|
||||||
// If no common elements left, break early
|
// If no common elements left, break early
|
||||||
if (commonElements->empty()) {
|
if (commonElements->empty())
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return commonElements;
|
return commonElements;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static map<BBlock*, vector<BBlock*>> findDominators(vector<BBlock*> blocks) {
|
static map<BBlock*, vector<BBlock*>> findDominators(vector<BBlock*> blocks)
|
||||||
|
{
|
||||||
map<BBlock*, vector<BBlock*>> result;
|
map<BBlock*, vector<BBlock*>> result;
|
||||||
|
|
||||||
bool changed = true;
|
bool changed = true;
|
||||||
while (changed) {
|
while (changed)
|
||||||
|
{
|
||||||
changed = false;
|
changed = false;
|
||||||
|
|
||||||
for (auto currentBlock : blocks) {
|
for (auto currentBlock : blocks)
|
||||||
|
{
|
||||||
auto pred = currentBlock->getPrev();
|
auto pred = currentBlock->getPrev();
|
||||||
auto prevDominators = new vector<vector<BBlock*>>();
|
auto prevDominators = new vector<vector<BBlock*>>();
|
||||||
|
|
||||||
@@ -139,7 +144,8 @@ static map<BBlock*, vector<BBlock*>> findDominators(vector<BBlock*> blocks) {
|
|||||||
auto currentBlockResult = getCommonElements(prevDominators);
|
auto currentBlockResult = getCommonElements(prevDominators);
|
||||||
currentBlockResult->push_back(currentBlock);
|
currentBlockResult->push_back(currentBlock);
|
||||||
|
|
||||||
if (result.find(currentBlock) == result.end() || !compareVectors(currentBlockResult, &result[currentBlock])) {
|
if (result.find(currentBlock) == result.end() || !compareVectors(currentBlockResult, &result[currentBlock]))
|
||||||
|
{
|
||||||
result[currentBlock] = *currentBlockResult;
|
result[currentBlock] = *currentBlockResult;
|
||||||
changed = true;
|
changed = true;
|
||||||
}
|
}
|
||||||
@@ -149,42 +155,42 @@ static map<BBlock*, vector<BBlock*>> findDominators(vector<BBlock*> blocks) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void RenumberBlocks(BBlock* current, int* n, map<int, int>* res, set<BBlock*>* visited) {
|
static void renumberBlocks(BBlock* current, int* n, map<int, int>* res, set<BBlock*>* visited) {
|
||||||
if (visited->find(current) != visited->end()) {
|
if (visited->find(current) != visited->end())
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
visited->insert(current);
|
visited->insert(current);
|
||||||
|
|
||||||
vector<BBlock*> nextBlocks = current->getNext();
|
vector<BBlock*> nextBlocks = current->getNext();
|
||||||
|
|
||||||
sort(nextBlocks.begin(), nextBlocks.end(), [](BBlock* a, BBlock* b) {
|
sort(nextBlocks.begin(), nextBlocks.end(), [](BBlock* a, BBlock* b) {
|
||||||
return a->getInstructions()[0]->getInstruction()->getOperator()->lineNumber() > b->getInstructions()[0]->getInstruction()->getOperator()->lineNumber();
|
return a->getInstructions()[0]->getInstruction()->getOperator()->lineNumber() > b->getInstructions()[0]->getInstruction()->getOperator()->lineNumber();
|
||||||
});
|
});
|
||||||
|
|
||||||
for (auto i : nextBlocks) {
|
for (auto i : nextBlocks)
|
||||||
RenumberBlocks(i, n, res, visited);
|
renumberBlocks(i, n, res, visited);
|
||||||
}
|
|
||||||
|
|
||||||
(*res)[current->getNumber()] = *n;
|
(*res)[current->getNumber()] = *n;
|
||||||
*n -= 1;
|
*n -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static map<BBlock*, vector<BBlock*>> findDominatorBorders(vector<BBlock*>& blocks, map<BBlock*, BBlock*>& iDominators) {
|
static map<BBlock*, vector<BBlock*>> findDominatorBorders(const vector<BBlock*>& blocks, map<BBlock*, BBlock*>& iDominators) {
|
||||||
map<BBlock*, vector<BBlock*>> result;
|
map<BBlock*, vector<BBlock*>> result;
|
||||||
|
|
||||||
for (auto block : blocks) {
|
for (auto& block : blocks)
|
||||||
result[block] = *(new vector<BBlock*>());
|
result[block] = *(new vector<BBlock*>());
|
||||||
}
|
|
||||||
|
|
||||||
for (auto block : blocks) {
|
for (auto& block : blocks)
|
||||||
if (block->getPrev().size() > 1) {
|
{
|
||||||
for (auto prev : block->getPrev()) {
|
if (block->getPrev().size() > 1)
|
||||||
|
{
|
||||||
|
for (auto prev : block->getPrev())
|
||||||
|
{
|
||||||
auto tmpBlock = prev;
|
auto tmpBlock = prev;
|
||||||
auto test = iDominators[block];
|
auto test = iDominators[block];
|
||||||
auto test2 = iDominators[prev];
|
auto test2 = iDominators[prev];
|
||||||
|
|
||||||
while (tmpBlock != iDominators[block]) {
|
while (tmpBlock != iDominators[block])
|
||||||
|
{
|
||||||
result[tmpBlock].push_back(block);
|
result[tmpBlock].push_back(block);
|
||||||
tmpBlock = iDominators[tmpBlock];
|
tmpBlock = iDominators[tmpBlock];
|
||||||
}
|
}
|
||||||
@@ -195,82 +201,87 @@ static map<BBlock*, vector<BBlock*>> findDominatorBorders(vector<BBlock*>& block
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static BBlock* findImmediateDominatorsDfsHelper(BBlock* block, BBlock* currentBlock, BBlock* currentImmediateDominator, vector<BBlock*> &visited, map<BBlock*, vector<BBlock*>>& dominators) {
|
static BBlock* findImmediateDominatorsDfsHelper(BBlock* block, BBlock* currentBlock, BBlock* currentImmediateDominator, vector<BBlock*> &visited, map<BBlock*, vector<BBlock*>>& dominators)
|
||||||
if (block == currentBlock) {
|
{
|
||||||
|
if (block == currentBlock)
|
||||||
return currentImmediateDominator;
|
return currentImmediateDominator;
|
||||||
}
|
|
||||||
|
|
||||||
if (find(visited.begin(), visited.end(), currentBlock) != visited.end()) {
|
if (find(visited.begin(), visited.end(), currentBlock) != visited.end())
|
||||||
return nullptr;
|
return NULL;
|
||||||
}
|
|
||||||
|
|
||||||
visited.push_back(currentBlock);
|
visited.push_back(currentBlock);
|
||||||
|
|
||||||
if (find(dominators[block].begin(), dominators[block].end(), currentBlock) != dominators[block].end()) {
|
if (find(dominators[block].begin(), dominators[block].end(), currentBlock) != dominators[block].end())
|
||||||
currentImmediateDominator = currentBlock;
|
currentImmediateDominator = currentBlock;
|
||||||
}
|
|
||||||
|
|
||||||
for (auto nextBlock : currentBlock->getNext()) {
|
for (auto nextBlock : currentBlock->getNext())
|
||||||
|
{
|
||||||
auto result = findImmediateDominatorsDfsHelper(block, nextBlock, currentImmediateDominator, visited, dominators);
|
auto result = findImmediateDominatorsDfsHelper(block, nextBlock, currentImmediateDominator, visited, dominators);
|
||||||
|
|
||||||
if (result) {
|
if (result)
|
||||||
return result;
|
return result;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nullptr;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static map<BBlock*, BBlock*> findImmediateDominators1(const map<BBlock*, vector<BBlock*>>& dominators, BBlock* entry) {
|
static map<BBlock*, BBlock*> findImmediateDominators1(const map<BBlock*, vector<BBlock*>>& dominators, BBlock* entry)
|
||||||
|
{
|
||||||
map<BBlock*, BBlock*> iDom;
|
map<BBlock*, BBlock*> iDom;
|
||||||
|
|
||||||
for (const auto& pair : dominators) {
|
for (const auto& pair : dominators)
|
||||||
|
{
|
||||||
BBlock* b = pair.first;
|
BBlock* b = pair.first;
|
||||||
|
|
||||||
if (b == entry) continue;
|
if (b == entry)
|
||||||
|
continue;
|
||||||
|
|
||||||
const auto& doms = pair.second;
|
const auto& doms = pair.second;
|
||||||
|
|
||||||
BBlock* candidate = nullptr;
|
BBlock* candidate = NULL;
|
||||||
for (auto d : doms) {
|
for (auto d : doms)
|
||||||
if (d == b) continue;
|
{
|
||||||
bool isImmediate = true;
|
if (d == b)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
bool isImmediate = true;
|
||||||
|
for (auto other : doms)
|
||||||
|
{
|
||||||
|
if (other == b || other == d)
|
||||||
|
continue;
|
||||||
|
|
||||||
for (auto other : doms) {
|
|
||||||
if (other == b || other == d) continue;
|
|
||||||
const auto& domsOfOther = dominators.at(other);
|
const auto& domsOfOther = dominators.at(other);
|
||||||
if (std::find(domsOfOther.begin(), domsOfOther.end(), d) != domsOfOther.end()) {
|
if (std::find(domsOfOther.begin(), domsOfOther.end(), d) != domsOfOther.end())
|
||||||
|
{
|
||||||
isImmediate = false;
|
isImmediate = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isImmediate) {
|
if (isImmediate)
|
||||||
|
{
|
||||||
candidate = d;
|
candidate = d;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (candidate) {
|
if (candidate)
|
||||||
iDom[b] = candidate;
|
iDom[b] = candidate;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return iDom;
|
return iDom;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static map<BBlock*, BBlock*> findImmediateDominators(map<BBlock*, vector<BBlock*>>& dominators, BBlock* fistBlock) {
|
static map<BBlock*, BBlock*> findImmediateDominators(map<BBlock*, vector<BBlock*>>& dominators, BBlock* fistBlock)
|
||||||
|
{
|
||||||
map<BBlock*, BBlock*> iDominators;
|
map<BBlock*, BBlock*> iDominators;
|
||||||
|
|
||||||
for (const auto& domPair : dominators) {
|
for (const auto& [block, domBlocks] : dominators) {
|
||||||
BBlock* block = domPair.first;
|
|
||||||
const vector<BBlock*>& domBlocks = domPair.second;
|
|
||||||
vector<BBlock*> visited;
|
vector<BBlock*> visited;
|
||||||
|
|
||||||
if (block == fistBlock) {
|
if (block == fistBlock)
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
|
|
||||||
iDominators[block] = findImmediateDominatorsDfsHelper(block, fistBlock, fistBlock, visited, dominators);
|
iDominators[block] = findImmediateDominatorsDfsHelper(block, fistBlock, fistBlock, visited, dominators);
|
||||||
}
|
}
|
||||||
@@ -278,19 +289,22 @@ static map<BBlock*, BBlock*> findImmediateDominators(map<BBlock*, vector<BBlock*
|
|||||||
return iDominators;
|
return iDominators;
|
||||||
}
|
}
|
||||||
|
|
||||||
static vector<BArgument*> getDefForBlock(const BBlock& block) {
|
static vector<BArgument*> getDefForBlock(const BBlock& block)
|
||||||
|
{
|
||||||
vector<BArgument*> def;
|
vector<BArgument*> def;
|
||||||
const auto& instructions = block.getInstructions();
|
const auto& instructions = block.getInstructions();
|
||||||
|
|
||||||
for (const auto& irBlock : instructions) {
|
for (const auto& irBlock : instructions)
|
||||||
if (irBlock) {
|
{
|
||||||
|
if (irBlock)
|
||||||
|
{
|
||||||
Instruction* instr = irBlock->getInstruction();
|
Instruction* instr = irBlock->getInstruction();
|
||||||
|
|
||||||
if (instr) {
|
if (instr)
|
||||||
|
{
|
||||||
BArgument* result = instr->getResult();
|
BArgument* result = instr->getResult();
|
||||||
if (result) {
|
if (result)
|
||||||
def.push_back(result);
|
def.push_back(result);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -298,31 +312,35 @@ static vector<BArgument*> getDefForBlock(const BBlock& block) {
|
|||||||
return def;
|
return def;
|
||||||
}
|
}
|
||||||
|
|
||||||
static pair<set<BArgument*>, map<BArgument*, set<BBlock*>>> getGlobalsAndVarBlocks(vector<BBlock*> blocks) {
|
static pair<set<BArgument*>, map<BArgument*, set<BBlock*>>> getGlobalsAndVarBlocks(const vector<BBlock*>& blocks) {
|
||||||
set<BArgument*> globals;
|
set<BArgument*> globals;
|
||||||
map<BArgument*, set<BBlock*>> varBlocks;
|
map<BArgument*, set<BBlock*>> varBlocks;
|
||||||
|
|
||||||
for (auto block : blocks) {
|
for (auto& block : blocks)
|
||||||
|
{
|
||||||
set<BArgument*> def;
|
set<BArgument*> def;
|
||||||
const auto& instructions = block->getInstructions();
|
const auto& instructions = block->getInstructions();
|
||||||
|
|
||||||
for (const auto& irBlock : instructions) {
|
for (const auto& irBlock : instructions)
|
||||||
if (irBlock) {
|
{
|
||||||
|
if (irBlock)
|
||||||
|
{
|
||||||
Instruction* instr = irBlock->getInstruction();
|
Instruction* instr = irBlock->getInstruction();
|
||||||
|
|
||||||
if (instr) {
|
if (instr)
|
||||||
|
{
|
||||||
auto arg1 = instr->getArg1();
|
auto arg1 = instr->getArg1();
|
||||||
auto arg2 = instr->getArg2();
|
auto arg2 = instr->getArg2();
|
||||||
auto res = instr->getResult();
|
auto res = instr->getResult();
|
||||||
|
|
||||||
if (arg1 && arg1->getType() == CFG_ARG_TYPE::VAR && find(def.begin(), def.end(), arg1) == def.end()) {
|
if (arg1 && arg1->getType() == CFG_ARG_TYPE::VAR && find(def.begin(), def.end(), arg1) == def.end())
|
||||||
globals.insert(arg1);
|
globals.insert(arg1);
|
||||||
}
|
|
||||||
if (arg2 && arg2->getType() == CFG_ARG_TYPE::VAR && find(def.begin(), def.end(), arg2) == def.end()) {
|
|
||||||
globals.insert(arg2);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (res && res->getType() == CFG_ARG_TYPE::VAR) {
|
if (arg2 && arg2->getType() == CFG_ARG_TYPE::VAR && find(def.begin(), def.end(), arg2) == def.end())
|
||||||
|
globals.insert(arg2);
|
||||||
|
|
||||||
|
if (res && res->getType() == CFG_ARG_TYPE::VAR)
|
||||||
|
{
|
||||||
def.insert(res);
|
def.insert(res);
|
||||||
varBlocks[res].insert(block);
|
varBlocks[res].insert(block);
|
||||||
}
|
}
|
||||||
@@ -334,23 +352,29 @@ static pair<set<BArgument*>, map<BArgument*, set<BBlock*>>> getGlobalsAndVarBloc
|
|||||||
return make_pair(globals, varBlocks);
|
return make_pair(globals, varBlocks);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void getBlocksWithFiFunctions(vector<BBlock*> blocks, set<BArgument*> globals, map<BArgument*, set<BBlock*>> varBlocks, map<BBlock*, vector<BBlock*>> dominatorBorders) {
|
static void getBlocksWithFiFunctions(const vector<BBlock*> blocks, set<BArgument*>& globals,
|
||||||
|
map<BArgument*, set<BBlock*>>& varBlocks,
|
||||||
|
map<BBlock*, vector<BBlock*>>& dominatorBorders)
|
||||||
|
{
|
||||||
vector<BBlock*> blocksWithFiFunctions;
|
vector<BBlock*> blocksWithFiFunctions;
|
||||||
auto fiFunc = new BArgument(CFG_ARG_TYPE::FUNC, CFG_MEM_TYPE::NONE_, "FI_FUNCTION");
|
auto fiFunc = new BArgument(CFG_ARG_TYPE::FUNC, CFG_MEM_TYPE::NONE_, "FI_FUNCTION");
|
||||||
auto paramCount = new BArgument(CFG_ARG_TYPE::CONST, CFG_MEM_TYPE::LOCAL_, "0");
|
auto paramCount = new BArgument(CFG_ARG_TYPE::CONST, CFG_MEM_TYPE::LOCAL_, "0");
|
||||||
|
|
||||||
for (auto var : globals) {
|
for (auto& var : globals)
|
||||||
|
{
|
||||||
auto worklist = varBlocks[var];
|
auto worklist = varBlocks[var];
|
||||||
set<BBlock*> hasFiFunction;
|
set<BBlock*> hasFiFunction;
|
||||||
|
|
||||||
while (!worklist.empty()) {
|
while (!worklist.empty())
|
||||||
|
{
|
||||||
auto block = *worklist.begin();
|
auto block = *worklist.begin();
|
||||||
worklist.erase(block);
|
worklist.erase(block);
|
||||||
|
|
||||||
for (auto dfBlock : dominatorBorders[block]) {
|
for (auto dfBlock : dominatorBorders[block])
|
||||||
if (hasFiFunction.find(dfBlock) == hasFiFunction.end()) {
|
{
|
||||||
|
if (hasFiFunction.find(dfBlock) == hasFiFunction.end())
|
||||||
|
{
|
||||||
hasFiFunction.insert(dfBlock);
|
hasFiFunction.insert(dfBlock);
|
||||||
|
|
||||||
Instruction* phiInstruction = new Instruction(CFG_OP::F_CALL, new BArgument(*fiFunc), new BArgument(*paramCount), var, dfBlock->getInstructions()[0]->getInstruction()->getOperator());
|
Instruction* phiInstruction = new Instruction(CFG_OP::F_CALL, new BArgument(*fiFunc), new BArgument(*paramCount), var, dfBlock->getInstructions()[0]->getInstruction()->getOperator());
|
||||||
|
|
||||||
IR_Block* phiBlock = new IR_Block(phiInstruction);
|
IR_Block* phiBlock = new IR_Block(phiInstruction);
|
||||||
@@ -365,7 +389,8 @@ static void getBlocksWithFiFunctions(vector<BBlock*> blocks, set<BArgument*> glo
|
|||||||
//return blocksWithFiFunctions;
|
//return blocksWithFiFunctions;
|
||||||
}
|
}
|
||||||
|
|
||||||
static string ToString(CFG_ARG_TYPE type) {
|
static string ToString(CFG_ARG_TYPE type)
|
||||||
|
{
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case CFG_ARG_TYPE::NONE: return "NONE";
|
case CFG_ARG_TYPE::NONE: return "NONE";
|
||||||
case CFG_ARG_TYPE::REG: return "REG";
|
case CFG_ARG_TYPE::REG: return "REG";
|
||||||
@@ -382,43 +407,40 @@ static string ToString(CFG_ARG_TYPE type) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void restoreConnections(const vector<BBlock*>& originalBlocks, vector<BBlock*>& copiedBlocks) {
|
static void restoreConnections(const vector<BBlock*>& originalBlocks, vector<BBlock*>& copiedBlocks)
|
||||||
|
{
|
||||||
// Создаем отображение оригинальных блоков в их копии
|
// Создаем отображение оригинальных блоков в их копии
|
||||||
map<BBlock*, BBlock*> blockMapping;
|
map<BBlock*, BBlock*> blockMapping;
|
||||||
for (size_t i = 0; i < originalBlocks.size(); ++i) {
|
for (size_t i = 0; i < originalBlocks.size(); ++i)
|
||||||
blockMapping[originalBlocks[i]] = copiedBlocks[i];
|
blockMapping[originalBlocks[i]] = copiedBlocks[i];
|
||||||
}
|
|
||||||
|
|
||||||
// Восстанавливаем связи между копиями
|
// Восстанавливаем связи между копиями
|
||||||
for (size_t i = 0; i < originalBlocks.size(); ++i) {
|
for (size_t i = 0; i < originalBlocks.size(); ++i)
|
||||||
|
{
|
||||||
BBlock* originalBlock = originalBlocks[i];
|
BBlock* originalBlock = originalBlocks[i];
|
||||||
BBlock* copiedBlock = copiedBlocks[i];
|
BBlock* copiedBlock = copiedBlocks[i];
|
||||||
|
|
||||||
auto prevCopy = copiedBlock->getPrev();
|
auto prevCopy = copiedBlock->getPrev();
|
||||||
for (auto j : prevCopy) {
|
for (auto j : prevCopy)
|
||||||
copiedBlock->removePrev(j);
|
copiedBlock->removePrev(j);
|
||||||
}
|
|
||||||
|
|
||||||
// Копируем, затем удаляем next связи
|
// Копируем, затем удаляем next связи
|
||||||
auto nextCopy = copiedBlock->getNext();
|
auto nextCopy = copiedBlock->getNext();
|
||||||
for (auto j : nextCopy) {
|
for (auto j : nextCopy)
|
||||||
copiedBlock->removeNext(j);
|
copiedBlock->removeNext(j);
|
||||||
}
|
|
||||||
|
|
||||||
// Восстанавливаем связи succ (следующих блоков)
|
// Восстанавливаем связи succ (следующих блоков)
|
||||||
for (auto* succ : originalBlock->getNext()) {
|
for (auto* succ : originalBlock->getNext())
|
||||||
copiedBlock->addNext(blockMapping[succ]);
|
copiedBlock->addNext(blockMapping[succ]);
|
||||||
}
|
|
||||||
|
|
||||||
// Восстанавливаем связи prev (предыдущих блоков)
|
// Восстанавливаем связи prev (предыдущих блоков)
|
||||||
for (auto* prev : originalBlock->getPrev()) {
|
for (auto* prev : originalBlock->getPrev())
|
||||||
copiedBlock->addPrev(blockMapping[prev]);
|
copiedBlock->addPrev(blockMapping[prev]);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static BArgument* NewName(BArgument* var, map<string, int>& counter, map<string, stack<BArgument*>>& stack, int number) {
|
static BArgument* newName(BArgument* var, map<string, int>& counter, map<string, stack<BArgument*>>& stack, int number) {
|
||||||
//int index = counter[var->getValue()];
|
//int index = counter[var->getValue()];
|
||||||
counter[var->getValue()]++;
|
counter[var->getValue()]++;
|
||||||
|
|
||||||
@@ -427,48 +449,57 @@ static BArgument* NewName(BArgument* var, map<string, int>& counter, map<string,
|
|||||||
return newName;
|
return newName;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void RenameFiFunctionResultVar(BBlock* block, map<string, int>& counter, map<string, stack<BArgument*>>& stack) {
|
static void renameFiFunctionResultVar(BBlock* block, map<string, int>& counter, map<string, stack<BArgument*>>& stack) {
|
||||||
for (auto irBlock : block->getInstructions()) {
|
for (auto irBlock : block->getInstructions())
|
||||||
|
{
|
||||||
auto instruction = irBlock->getInstruction();
|
auto instruction = irBlock->getInstruction();
|
||||||
if (instruction->getOperation() == CFG_OP::F_CALL && instruction->getArg1() != nullptr && instruction->getArg1()->getValue() == "FI_FUNCTION" && instruction->getResult() != nullptr) {
|
if (instruction->getOperation() == CFG_OP::F_CALL && instruction->getArg1() != NULL &&
|
||||||
instruction->setResult(NewName(instruction->getResult(), counter, stack, instruction->getNumber()));
|
instruction->getArg1()->getValue() == "FI_FUNCTION" && instruction->getResult() != NULL)
|
||||||
|
{
|
||||||
|
instruction->setResult(newName(instruction->getResult(), counter, stack, instruction->getNumber()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void RenameInstructionVars(BBlock* block, map<string, int>& counter, map<string, stack<BArgument*>>& stack) {
|
static void renameInstructionVars(BBlock* block, map<string, int>& counter, map<string, stack<BArgument*>>& stack)
|
||||||
for (auto irBlock : block->getInstructions()) {
|
{
|
||||||
|
for (auto irBlock : block->getInstructions())
|
||||||
|
{
|
||||||
auto instruction = irBlock->getInstruction();
|
auto instruction = irBlock->getInstruction();
|
||||||
|
|
||||||
if (instruction->getArg1() != nullptr && instruction->getArg1()->getType() == CFG_ARG_TYPE::VAR) {
|
if (instruction->getArg1() != NULL && instruction->getArg1()->getType() == CFG_ARG_TYPE::VAR)
|
||||||
instruction->setArg1(stack[instruction->getArg1()->getValue()].top());
|
instruction->setArg1(stack[instruction->getArg1()->getValue()].top());
|
||||||
}
|
|
||||||
|
|
||||||
if (instruction->getArg2() != nullptr && instruction->getArg2()->getType() == CFG_ARG_TYPE::VAR) {
|
if (instruction->getArg2() != NULL && instruction->getArg2()->getType() == CFG_ARG_TYPE::VAR)
|
||||||
instruction->setArg2(stack[instruction->getArg2()->getValue()].top());
|
instruction->setArg2(stack[instruction->getArg2()->getValue()].top());
|
||||||
}
|
|
||||||
|
|
||||||
if (instruction->getResult() != nullptr && instruction->getResult()->getType() == CFG_ARG_TYPE::VAR) {
|
if (instruction->getResult() != NULL && instruction->getResult()->getType() == CFG_ARG_TYPE::VAR)
|
||||||
instruction->setResult(NewName(instruction->getResult(), counter, stack, instruction->getNumber()));
|
instruction->setResult(newName(instruction->getResult(), counter, stack, instruction->getNumber()));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void RenameFiFunctionArgsVar(BBlock* block, map<string, stack<BArgument*>>& stack) {
|
static void renameFiFunctionArgsVar(BBlock* block, map<string, stack<BArgument*>>& stack)
|
||||||
|
{
|
||||||
auto size = block->getInstructions().size();
|
auto size = block->getInstructions().size();
|
||||||
auto& instructions = block->getInstructions();
|
auto& instructions = block->getInstructions();
|
||||||
for (auto i = 0; i < size; i++) {
|
|
||||||
|
for (auto i = 0; i < size; i++)
|
||||||
|
{
|
||||||
auto irBlock = instructions[i];
|
auto irBlock = instructions[i];
|
||||||
auto instruction = irBlock->getInstruction();
|
auto instruction = irBlock->getInstruction();
|
||||||
if (instruction->getOperation() == CFG_OP::F_CALL && instruction->getArg1() != nullptr && instruction->getArg1()->getValue() == "FI_FUNCTION" && instruction->getResult() != nullptr && instruction->getArg2() != nullptr) {
|
if (instruction->getOperation() == CFG_OP::F_CALL && instruction->getArg1() != NULL &&
|
||||||
|
instruction->getArg1()->getValue() == "FI_FUNCTION" && instruction->getResult() != NULL &&
|
||||||
|
instruction->getArg2() != NULL)
|
||||||
|
{
|
||||||
Instruction* paramInstruction;
|
Instruction* paramInstruction;
|
||||||
|
|
||||||
if (stack[instruction->getResult()->getValue()].size() > 0) {
|
if (stack[instruction->getResult()->getValue()].size() > 0)
|
||||||
|
{
|
||||||
BArgument* tmp = new BArgument(CFG_ARG_TYPE::CONST, CFG_MEM_TYPE::COMMON_, to_string(stack[instruction->getResult()->getValue()].top()->getNumber()));
|
BArgument* tmp = new BArgument(CFG_ARG_TYPE::CONST, CFG_MEM_TYPE::COMMON_, to_string(stack[instruction->getResult()->getValue()].top()->getNumber()));
|
||||||
paramInstruction = new Instruction(CFG_OP::PARAM, tmp);
|
paramInstruction = new Instruction(CFG_OP::PARAM, tmp);
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
BArgument* tmp = new BArgument(CFG_ARG_TYPE::CONST, CFG_MEM_TYPE::COMMON_, "-1");
|
BArgument* tmp = new BArgument(CFG_ARG_TYPE::CONST, CFG_MEM_TYPE::COMMON_, "-1");
|
||||||
paramInstruction = new Instruction(CFG_OP::PARAM, tmp);
|
paramInstruction = new Instruction(CFG_OP::PARAM, tmp);
|
||||||
}
|
}
|
||||||
@@ -482,67 +513,63 @@ static void RenameFiFunctionArgsVar(BBlock* block, map<string, stack<BArgument*>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static vector<BBlock*> findBlocksWithValue(map<BBlock*, BBlock*>& iDominators, BBlock* x) {
|
static vector<BBlock*> findBlocksWithValue(map<BBlock*, BBlock*>& iDominators, BBlock* x)
|
||||||
|
{
|
||||||
vector<BBlock*> result;
|
vector<BBlock*> result;
|
||||||
|
|
||||||
// Проходим по всем элементам map
|
// Проходим по всем элементам map
|
||||||
for (auto& pair : iDominators) {
|
for (auto& pair : iDominators)
|
||||||
// Если значение равно x, добавляем ключ в результат
|
// Если значение равно x, добавляем ключ в результат
|
||||||
if (pair.second == x) {
|
if (pair.second == x)
|
||||||
result.push_back(pair.first);
|
result.push_back(pair.first);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void RenameIR(BBlock* block, map<BBlock*, BBlock*>& iDominators, map<string, int>& counter, map<string, stack<BArgument*>>& stack) {
|
static void renameIR(BBlock* block, map<BBlock*, BBlock*>& iDominators, map<string, int>& counter, map<string, stack<BArgument*>>& stack) {
|
||||||
|
|
||||||
RenameFiFunctionResultVar(block, counter, stack);
|
renameFiFunctionResultVar(block, counter, stack);
|
||||||
|
renameInstructionVars(block, counter, stack);
|
||||||
|
|
||||||
RenameInstructionVars(block, counter, stack);
|
for (auto* successor : block->getNext())
|
||||||
|
renameFiFunctionArgsVar(successor, stack);
|
||||||
|
|
||||||
for (auto* successor : block->getNext()) {
|
for (auto* child : findBlocksWithValue(iDominators, block))
|
||||||
RenameFiFunctionArgsVar(successor, stack);
|
renameIR(child, iDominators, counter, stack);
|
||||||
}
|
|
||||||
|
|
||||||
for (auto* child : findBlocksWithValue(iDominators, block)) {
|
for (auto& irBlock : block->getInstructions())
|
||||||
RenameIR(child, iDominators, counter, stack);
|
{
|
||||||
}
|
|
||||||
|
|
||||||
for (auto& irBlock : block->getInstructions()) {
|
|
||||||
auto instruction = irBlock->getInstruction();
|
auto instruction = irBlock->getInstruction();
|
||||||
if (instruction->getResult() != nullptr && instruction->getResult()->getType() == CFG_ARG_TYPE::VAR) {
|
if (instruction->getResult() != NULL && instruction->getResult()->getType() == CFG_ARG_TYPE::VAR)
|
||||||
|
{
|
||||||
string varName = instruction->getResult()->getValue();
|
string varName = instruction->getResult()->getValue();
|
||||||
stack[varName].pop();
|
stack[varName].pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto& irBlock : block->getInstructions()) {
|
for (auto& irBlock : block->getInstructions())
|
||||||
|
{
|
||||||
auto instruction = irBlock->getInstruction();
|
auto instruction = irBlock->getInstruction();
|
||||||
if (instruction->getOperation() == CFG_OP::F_CALL && instruction->getArg1() != nullptr && instruction->getArg1()->getValue() == "FI_FUNCTION" && instruction->getResult() != nullptr) {
|
if (instruction->getOperation() == CFG_OP::F_CALL && instruction->getArg1() != NULL &&
|
||||||
|
instruction->getArg1()->getValue() == "FI_FUNCTION" && instruction->getResult() != NULL)
|
||||||
|
{
|
||||||
string varName = instruction->getResult()->getValue();
|
string varName = instruction->getResult()->getValue();
|
||||||
stack[varName].pop();
|
stack[varName].pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void buildIRSSAForm(map<FuncInfo*, vector<BBlock*>> fullIR, map<FuncInfo*, vector<BBlock*>>* result) {
|
void buildIRSSAForm(const map<FuncInfo*, vector<BBlock*>>& fullIR,
|
||||||
|
map<FuncInfo*, vector<BBlock*>>& result) {
|
||||||
for (auto item : fullIR) {
|
|
||||||
|
|
||||||
auto funcinfo = item.first;
|
|
||||||
auto funcIRConst = item.second;
|
|
||||||
|
|
||||||
cout << "Testing " << item.first->funcName << endl;
|
|
||||||
|
|
||||||
|
|
||||||
|
for (auto& [funcinfo, funcIRConst]: fullIR) {
|
||||||
|
cout << "Testing " << funcinfo->funcName << endl;
|
||||||
|
|
||||||
vector<BBlock*> funcIR;
|
vector<BBlock*> funcIR;
|
||||||
|
|
||||||
for (auto i : funcIRConst) {
|
for (auto i : funcIRConst)
|
||||||
funcIR.push_back(new BBlock(*i));
|
funcIR.push_back(new BBlock(*i));
|
||||||
}
|
|
||||||
restoreConnections(funcIRConst, funcIR);
|
restoreConnections(funcIRConst, funcIR);
|
||||||
|
|
||||||
for (auto i : funcIR) {
|
for (auto i : funcIR) {
|
||||||
@@ -571,7 +598,6 @@ void buildIRSSAForm(map<FuncInfo*, vector<BBlock*>> fullIR, map<FuncInfo*, vecto
|
|||||||
*/
|
*/
|
||||||
auto dominatorBorders = findDominatorBorders(funcIR, iDominators);
|
auto dominatorBorders = findDominatorBorders(funcIR, iDominators);
|
||||||
|
|
||||||
|
|
||||||
/*for (auto i : dominatorBorders) {
|
/*for (auto i : dominatorBorders) {
|
||||||
cout << "block - " << i.first->getNumber() << endl;
|
cout << "block - " << i.first->getNumber() << endl;
|
||||||
for (auto j : i.second) {
|
for (auto j : i.second) {
|
||||||
@@ -581,7 +607,6 @@ void buildIRSSAForm(map<FuncInfo*, vector<BBlock*>> fullIR, map<FuncInfo*, vecto
|
|||||||
cout << endl;
|
cout << endl;
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
|
|
||||||
auto globalsAndVarBlocks = getGlobalsAndVarBlocks(funcIR);
|
auto globalsAndVarBlocks = getGlobalsAndVarBlocks(funcIR);
|
||||||
auto globals = globalsAndVarBlocks.first;
|
auto globals = globalsAndVarBlocks.first;
|
||||||
auto varBlocks = globalsAndVarBlocks.second;
|
auto varBlocks = globalsAndVarBlocks.second;
|
||||||
@@ -606,7 +631,8 @@ void buildIRSSAForm(map<FuncInfo*, vector<BBlock*>> fullIR, map<FuncInfo*, vecto
|
|||||||
map<string, int> count;
|
map<string, int> count;
|
||||||
map<string, stack<BArgument*>> varStack;
|
map<string, stack<BArgument*>> varStack;
|
||||||
|
|
||||||
for (auto var : globals) {
|
for (auto var : globals)
|
||||||
|
{
|
||||||
count[var->getValue()] = 0;
|
count[var->getValue()] = 0;
|
||||||
|
|
||||||
stack<BArgument*> tmp;
|
stack<BArgument*> tmp;
|
||||||
@@ -615,7 +641,7 @@ void buildIRSSAForm(map<FuncInfo*, vector<BBlock*>> fullIR, map<FuncInfo*, vecto
|
|||||||
varStack[var->getValue()] = tmp;
|
varStack[var->getValue()] = tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
RenameIR(funcIR[0], iDominators, count, varStack);
|
renameIR(funcIR[0], iDominators, count, varStack);
|
||||||
|
|
||||||
for (auto i : funcIR) {
|
for (auto i : funcIR) {
|
||||||
//printBlock(i);
|
//printBlock(i);
|
||||||
@@ -625,6 +651,6 @@ void buildIRSSAForm(map<FuncInfo*, vector<BBlock*>> fullIR, map<FuncInfo*, vecto
|
|||||||
// printBlock(i);
|
// printBlock(i);
|
||||||
//}
|
//}
|
||||||
|
|
||||||
(*result)[funcinfo] = funcIR;
|
result[funcinfo] = funcIR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,4 +3,4 @@
|
|||||||
#include "CFGraph.h"
|
#include "CFGraph.h"
|
||||||
#include "IR.h"
|
#include "IR.h"
|
||||||
|
|
||||||
void buildIRSSAForm(std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>> fullIR, std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>* result);
|
void buildIRSSAForm(const std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>& fullIR, std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>& result);
|
||||||
@@ -21,38 +21,37 @@ using std::to_string;
|
|||||||
|
|
||||||
enum VisitState { UNVISITED = 0, VISITING = 1, VISITED = 2 };
|
enum VisitState { UNVISITED = 0, VISITING = 1, VISITED = 2 };
|
||||||
|
|
||||||
void dfs(SAPFOR::BasicBlock* block, map<int, int>& visit, vector<pair<SAPFOR::BasicBlock*, SAPFOR::BasicBlock*>>& startAndEnd, SAPFOR::BasicBlock* prev) {
|
static void dfs(SAPFOR::BasicBlock* block, map<int, int>& visit, vector<pair<SAPFOR::BasicBlock*, SAPFOR::BasicBlock*>>& startAndEnd, SAPFOR::BasicBlock* prev) {
|
||||||
if (!block) return;
|
if (!block)
|
||||||
|
return;
|
||||||
|
|
||||||
if (visit[block->getNumber()] == VISITED) {
|
if (visit[block->getNumber()] == VISITED)
|
||||||
|
{
|
||||||
cout << "error";
|
cout << "error";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (visit[block->getNumber()] == VISITING) {
|
if (visit[block->getNumber()] == VISITING)
|
||||||
|
{
|
||||||
visit[block->getNumber()] = VISITED;
|
visit[block->getNumber()] = VISITED;
|
||||||
startAndEnd.push_back(make_pair(prev, block));
|
startAndEnd.push_back(make_pair(prev, block));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
visit[block->getNumber()] = VISITING;
|
visit[block->getNumber()] = VISITING;
|
||||||
for (auto i : block->getNext()) {
|
for (auto i : block->getNext())
|
||||||
dfs(i, visit, startAndEnd, block);
|
dfs(i, visit, startAndEnd, block);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void printBlock(SAPFOR::BasicBlock* block) {
|
static void printBlock(SAPFOR::BasicBlock* block) {
|
||||||
cout << "block - " << block->getNumber() << endl;
|
cout << "block - " << block->getNumber() << endl;
|
||||||
cout << "next -";
|
cout << "next -";
|
||||||
for (auto i : block->getNext())
|
for (auto i : block->getNext())
|
||||||
{
|
|
||||||
cout << " " << i->getNumber();
|
cout << " " << i->getNumber();
|
||||||
}
|
|
||||||
cout << endl << "prev -";
|
cout << endl << "prev -";
|
||||||
for (auto i : block->getPrev())
|
for (auto i : block->getPrev())
|
||||||
{
|
|
||||||
cout << " " << i->getNumber();
|
cout << " " << i->getNumber();
|
||||||
}
|
|
||||||
cout << endl;
|
cout << endl;
|
||||||
|
|
||||||
for (auto i : block->getInstructions())
|
for (auto i : block->getInstructions())
|
||||||
@@ -60,53 +59,59 @@ static void printBlock(SAPFOR::BasicBlock* block) {
|
|||||||
string resValue = "";
|
string resValue = "";
|
||||||
string arg1Value = "";
|
string arg1Value = "";
|
||||||
string arg2Value = "";
|
string arg2Value = "";
|
||||||
if (i->getInstruction()->getResult() != nullptr && i->getInstruction()->getResult()->getType() == SAPFOR::CFG_ARG_TYPE::VAR) {
|
if (i->getInstruction()->getResult() != NULL && i->getInstruction()->getResult()->getType() == SAPFOR::CFG_ARG_TYPE::VAR) {
|
||||||
resValue = i->getInstruction()->getResult()->getValue();
|
resValue = i->getInstruction()->getResult()->getValue();
|
||||||
i->getInstruction()->getResult()->setValue(i->getInstruction()->getResult()->getValue() + to_string(i->getInstruction()->getResult()->getNumber()));
|
i->getInstruction()->getResult()->setValue(i->getInstruction()->getResult()->getValue() + to_string(i->getInstruction()->getResult()->getNumber()));
|
||||||
}
|
}
|
||||||
if (i->getInstruction()->getArg1() != nullptr && i->getInstruction()->getArg1()->getType() == SAPFOR::CFG_ARG_TYPE::VAR) {
|
|
||||||
|
if (i->getInstruction()->getArg1() != NULL && i->getInstruction()->getArg1()->getType() == SAPFOR::CFG_ARG_TYPE::VAR) {
|
||||||
arg1Value = i->getInstruction()->getArg1()->getValue();
|
arg1Value = i->getInstruction()->getArg1()->getValue();
|
||||||
i->getInstruction()->getArg1()->setValue(i->getInstruction()->getArg1()->getValue() + to_string(i->getInstruction()->getArg1()->getNumber()));
|
i->getInstruction()->getArg1()->setValue(i->getInstruction()->getArg1()->getValue() + to_string(i->getInstruction()->getArg1()->getNumber()));
|
||||||
}
|
}
|
||||||
if (i->getInstruction()->getArg2() != nullptr && i->getInstruction()->getArg2()->getType() == SAPFOR::CFG_ARG_TYPE::VAR) {
|
|
||||||
|
if (i->getInstruction()->getArg2() != NULL && i->getInstruction()->getArg2()->getType() == SAPFOR::CFG_ARG_TYPE::VAR) {
|
||||||
arg2Value = i->getInstruction()->getArg2()->getValue();
|
arg2Value = i->getInstruction()->getArg2()->getValue();
|
||||||
i->getInstruction()->getArg2()->setValue(i->getInstruction()->getArg2()->getValue() + to_string(i->getInstruction()->getArg2()->getNumber()));
|
i->getInstruction()->getArg2()->setValue(i->getInstruction()->getArg2()->getValue() + to_string(i->getInstruction()->getArg2()->getNumber()));
|
||||||
}
|
}
|
||||||
|
|
||||||
cout << i->getNumber() << " " << i->getInstruction()->dump() << endl;
|
cout << i->getNumber() << " " << i->getInstruction()->dump() << endl;
|
||||||
|
|
||||||
if (i->getInstruction()->getResult() != nullptr && i->getInstruction()->getResult()->getType() == SAPFOR::CFG_ARG_TYPE::VAR) {
|
if (i->getInstruction()->getResult() != NULL && i->getInstruction()->getResult()->getType() == SAPFOR::CFG_ARG_TYPE::VAR)
|
||||||
i->getInstruction()->getResult()->setValue(resValue);
|
i->getInstruction()->getResult()->setValue(resValue);
|
||||||
}
|
|
||||||
if (i->getInstruction()->getArg1() != nullptr && i->getInstruction()->getArg1()->getType() == SAPFOR::CFG_ARG_TYPE::VAR) {
|
if (i->getInstruction()->getArg1() != NULL && i->getInstruction()->getArg1()->getType() == SAPFOR::CFG_ARG_TYPE::VAR)
|
||||||
i->getInstruction()->getArg1()->setValue(arg1Value);
|
i->getInstruction()->getArg1()->setValue(arg1Value);
|
||||||
}
|
|
||||||
if (i->getInstruction()->getArg2() != nullptr && i->getInstruction()->getArg2()->getType() == SAPFOR::CFG_ARG_TYPE::VAR) {
|
if (i->getInstruction()->getArg2() != NULL && i->getInstruction()->getArg2()->getType() == SAPFOR::CFG_ARG_TYPE::VAR)
|
||||||
i->getInstruction()->getArg2()->setValue(arg2Value);
|
i->getInstruction()->getArg2()->setValue(arg2Value);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cout << endl;
|
cout << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void getLoopBody(SAPFOR::BasicBlock* loopHeader, const set<SAPFOR::BasicBlock*>& loopExits, std::vector<SAPFOR::BasicBlock*>& loopBody) {
|
static void getLoopBody(SAPFOR::BasicBlock* loopHeader, const set<SAPFOR::BasicBlock*>& loopExits, std::vector<SAPFOR::BasicBlock*>& loopBody)
|
||||||
|
{
|
||||||
set<SAPFOR::BasicBlock*> visited;
|
set<SAPFOR::BasicBlock*> visited;
|
||||||
std::stack<SAPFOR::BasicBlock*> stack;
|
std::stack<SAPFOR::BasicBlock*> stack;
|
||||||
|
|
||||||
stack.push(loopHeader);
|
stack.push(loopHeader);
|
||||||
|
|
||||||
while (!stack.empty()) {
|
while (!stack.empty())
|
||||||
|
{
|
||||||
auto block = stack.top();
|
auto block = stack.top();
|
||||||
stack.pop();
|
stack.pop();
|
||||||
|
|
||||||
if (visited.count(block)) continue;
|
if (visited.count(block))
|
||||||
|
continue;
|
||||||
visited.insert(block);
|
visited.insert(block);
|
||||||
|
|
||||||
for (auto succ : block->getNext()) {
|
for (auto succ : block->getNext())
|
||||||
if (loopExits.count(succ)) continue;
|
{
|
||||||
if (!visited.count(succ)) {
|
if (loopExits.count(succ))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (!visited.count(succ))
|
||||||
stack.push(succ);
|
stack.push(succ);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,29 +119,27 @@ void getLoopBody(SAPFOR::BasicBlock* loopHeader, const set<SAPFOR::BasicBlock*>&
|
|||||||
std::stack<SAPFOR::BasicBlock*> reverseStack;
|
std::stack<SAPFOR::BasicBlock*> reverseStack;
|
||||||
reverseStack.push(loopHeader);
|
reverseStack.push(loopHeader);
|
||||||
|
|
||||||
while (!reverseStack.empty()) {
|
while (!reverseStack.empty())
|
||||||
|
{
|
||||||
auto block = reverseStack.top();
|
auto block = reverseStack.top();
|
||||||
reverseStack.pop();
|
reverseStack.pop();
|
||||||
|
|
||||||
if (backReachable.count(block)) continue;
|
if (backReachable.count(block))
|
||||||
|
continue;
|
||||||
backReachable.insert(block);
|
backReachable.insert(block);
|
||||||
|
|
||||||
for (auto pred : block->getPrev()) {
|
for (auto pred : block->getPrev())
|
||||||
if (visited.count(pred) && !backReachable.count(pred)) {
|
if (visited.count(pred) && !backReachable.count(pred))
|
||||||
reverseStack.push(pred);
|
reverseStack.push(pred);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto block : visited) {
|
for (auto block : visited)
|
||||||
if (backReachable.count(block)) {
|
if (backReachable.count(block))
|
||||||
loopBody.push_back(block);
|
loopBody.push_back(block);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
set<SAPFOR::Argument*> findRegisterSourceVariables(const std::vector<SAPFOR::BasicBlock*>& blocks, SAPFOR::Argument* var)
|
static set<SAPFOR::Argument*> findRegisterSourceVariables(const std::vector<SAPFOR::BasicBlock*>& blocks, SAPFOR::Argument* var)
|
||||||
{
|
{
|
||||||
set<SAPFOR::Argument*> result;
|
set<SAPFOR::Argument*> result;
|
||||||
set<SAPFOR::Argument*> visited;
|
set<SAPFOR::Argument*> visited;
|
||||||
@@ -159,7 +162,8 @@ set<SAPFOR::Argument*> findRegisterSourceVariables(const std::vector<SAPFOR::Bas
|
|||||||
op == SAPFOR::CFG_OP::NOT || op == SAPFOR::CFG_OP::ASSIGN;
|
op == SAPFOR::CFG_OP::NOT || op == SAPFOR::CFG_OP::ASSIGN;
|
||||||
};
|
};
|
||||||
|
|
||||||
while (!workStack.empty()) {
|
while (!workStack.empty())
|
||||||
|
{
|
||||||
auto variable = workStack.top();
|
auto variable = workStack.top();
|
||||||
workStack.pop();
|
workStack.pop();
|
||||||
if (!variable || visited.count(variable))
|
if (!variable || visited.count(variable))
|
||||||
@@ -167,8 +171,10 @@ set<SAPFOR::Argument*> findRegisterSourceVariables(const std::vector<SAPFOR::Bas
|
|||||||
|
|
||||||
visited.insert(variable);
|
visited.insert(variable);
|
||||||
|
|
||||||
for (auto block : blocks) {
|
for (auto block : blocks)
|
||||||
for (auto instrWrapper : block->getInstructions()) {
|
{
|
||||||
|
for (auto instrWrapper : block->getInstructions())
|
||||||
|
{
|
||||||
auto instr = instrWrapper->getInstruction();
|
auto instr = instrWrapper->getInstruction();
|
||||||
if (!instr || instr->getResult() != variable)
|
if (!instr || instr->getResult() != variable)
|
||||||
continue;
|
continue;
|
||||||
@@ -177,7 +183,8 @@ set<SAPFOR::Argument*> findRegisterSourceVariables(const std::vector<SAPFOR::Bas
|
|||||||
auto arg1 = instr->getArg1();
|
auto arg1 = instr->getArg1();
|
||||||
auto arg2 = instr->getArg2();
|
auto arg2 = instr->getArg2();
|
||||||
|
|
||||||
if (isBinaryOp(op) && arg1 && arg2) {
|
if (isBinaryOp(op) && arg1 && arg2)
|
||||||
|
{
|
||||||
if (arg1->getType() == SAPFOR::CFG_ARG_TYPE::VAR)
|
if (arg1->getType() == SAPFOR::CFG_ARG_TYPE::VAR)
|
||||||
result.insert(arg1);
|
result.insert(arg1);
|
||||||
else if (arg1->getType() == SAPFOR::CFG_ARG_TYPE::REG)
|
else if (arg1->getType() == SAPFOR::CFG_ARG_TYPE::REG)
|
||||||
@@ -188,7 +195,8 @@ set<SAPFOR::Argument*> findRegisterSourceVariables(const std::vector<SAPFOR::Bas
|
|||||||
else if (arg2->getType() == SAPFOR::CFG_ARG_TYPE::REG)
|
else if (arg2->getType() == SAPFOR::CFG_ARG_TYPE::REG)
|
||||||
workStack.push(arg2);
|
workStack.push(arg2);
|
||||||
}
|
}
|
||||||
else if (isUnaryOp(op) && arg1) {
|
else if (isUnaryOp(op) && arg1)
|
||||||
|
{
|
||||||
if (arg1->getType() == SAPFOR::CFG_ARG_TYPE::VAR)
|
if (arg1->getType() == SAPFOR::CFG_ARG_TYPE::VAR)
|
||||||
result.insert(arg1);
|
result.insert(arg1);
|
||||||
else if (arg1->getType() == SAPFOR::CFG_ARG_TYPE::REG)
|
else if (arg1->getType() == SAPFOR::CFG_ARG_TYPE::REG)
|
||||||
@@ -201,7 +209,8 @@ set<SAPFOR::Argument*> findRegisterSourceVariables(const std::vector<SAPFOR::Bas
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<SAPFOR::Instruction*> getPhiArguments(SAPFOR::BasicBlock* block, SAPFOR::Instruction* phiInstr) {
|
static std::vector<SAPFOR::Instruction*> getPhiArguments(SAPFOR::BasicBlock* block, SAPFOR::Instruction* phiInstr)
|
||||||
|
{
|
||||||
std::vector<SAPFOR::Instruction*> result;
|
std::vector<SAPFOR::Instruction*> result;
|
||||||
|
|
||||||
auto& instructions = block->getInstructions();
|
auto& instructions = block->getInstructions();
|
||||||
@@ -209,21 +218,23 @@ std::vector<SAPFOR::Instruction*> getPhiArguments(SAPFOR::BasicBlock* block, SAP
|
|||||||
for (int i = instructions.size() - 1; i >= 0; --i) {
|
for (int i = instructions.size() - 1; i >= 0; --i) {
|
||||||
auto instr = instructions[i]->getInstruction();
|
auto instr = instructions[i]->getInstruction();
|
||||||
|
|
||||||
if (collecting) {
|
if (collecting)
|
||||||
if (instr->getOperation() == SAPFOR::CFG_OP::PARAM) {
|
{
|
||||||
|
if (instr->getOperation() == SAPFOR::CFG_OP::PARAM)
|
||||||
|
{
|
||||||
auto arg = instr->getArg1();
|
auto arg = instr->getArg1();
|
||||||
if (arg) {
|
if (arg)
|
||||||
result.push_back(instr);
|
result.push_back(instr);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!instr) continue;
|
if (!instr)
|
||||||
|
continue;
|
||||||
|
|
||||||
if (instr == phiInstr) {
|
if (instr == phiInstr)
|
||||||
|
{
|
||||||
collecting = true;
|
collecting = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -233,55 +244,64 @@ std::vector<SAPFOR::Instruction*> getPhiArguments(SAPFOR::BasicBlock* block, SAP
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
SAPFOR::BasicBlock* findInstructionBlock(SAPFOR::Instruction* targetInstr, const std::vector<SAPFOR::BasicBlock*>& blocks) {
|
SAPFOR::BasicBlock* findInstructionBlock(SAPFOR::Instruction* targetInstr, const std::vector<SAPFOR::BasicBlock*>& blocks)
|
||||||
for (auto block : blocks) {
|
{
|
||||||
for (auto instrWrapper : block->getInstructions()) {
|
for (auto& block : blocks)
|
||||||
|
{
|
||||||
|
for (auto& instrWrapper : block->getInstructions())
|
||||||
|
{
|
||||||
auto instr = instrWrapper->getInstruction();
|
auto instr = instrWrapper->getInstruction();
|
||||||
if (instr == targetInstr) {
|
if (instr == targetInstr)
|
||||||
return block;
|
return block;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nullptr;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
SAPFOR::BasicBlock* findInstructionBlockByNumber(int number, const std::vector<SAPFOR::BasicBlock*>& blocks) {
|
static SAPFOR::BasicBlock* findInstructionBlockByNumber(int number, const std::vector<SAPFOR::BasicBlock*>& blocks)
|
||||||
for (auto block : blocks) {
|
{
|
||||||
for (auto instrWrapper : block->getInstructions()) {
|
for (auto& block : blocks)
|
||||||
|
{
|
||||||
|
for (auto& instrWrapper : block->getInstructions())
|
||||||
|
{
|
||||||
auto instr = instrWrapper->getInstruction();
|
auto instr = instrWrapper->getInstruction();
|
||||||
if (instr->getNumber() == number) {
|
if (instr->getNumber() == number)
|
||||||
return block;
|
return block;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nullptr;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void findInductiveVars(const std::vector<SAPFOR::BasicBlock*>& blocks,
|
||||||
void findInductiveVars(const std::vector<SAPFOR::BasicBlock*>& blocks, const std::vector<SAPFOR::BasicBlock*>& Loopblocks, SAPFOR::BasicBlock* loopHeader, const set<SAPFOR::BasicBlock*>& loopExits) {
|
const std::vector<SAPFOR::BasicBlock*>& Loopblocks, SAPFOR::BasicBlock* loopHeader,
|
||||||
|
const set<SAPFOR::BasicBlock*>& loopExits)
|
||||||
|
{
|
||||||
set<string> inductiveVars;
|
set<string> inductiveVars;
|
||||||
set<SAPFOR::BasicBlock*> relevantBlocks = { loopHeader };
|
set<SAPFOR::BasicBlock*> relevantBlocks = { loopHeader };
|
||||||
|
|
||||||
for (auto block : relevantBlocks) {
|
for (auto block : relevantBlocks)
|
||||||
|
{
|
||||||
for (auto instrWrapper : block->getInstructions()) {
|
for (auto instrWrapper : block->getInstructions())
|
||||||
|
{
|
||||||
auto instr = instrWrapper->getInstruction();
|
auto instr = instrWrapper->getInstruction();
|
||||||
if (!instr) continue;
|
if (!instr)
|
||||||
|
continue;
|
||||||
|
|
||||||
auto op = instr->getOperation();
|
auto op = instr->getOperation();
|
||||||
auto res = instr->getResult();
|
auto res = instr->getResult();
|
||||||
auto arg1 = instr->getArg1();
|
auto arg1 = instr->getArg1();
|
||||||
auto arg2 = instr->getArg2();
|
auto arg2 = instr->getArg2();
|
||||||
|
|
||||||
if (op == SAPFOR::CFG_OP::JUMP_IF) {
|
if (op == SAPFOR::CFG_OP::JUMP_IF)
|
||||||
if (arg1 && arg1->getType() == SAPFOR::CFG_ARG_TYPE::VAR) {
|
{
|
||||||
|
if (arg1 && arg1->getType() == SAPFOR::CFG_ARG_TYPE::VAR)
|
||||||
inductiveVars.insert(arg1->getValue());
|
inductiveVars.insert(arg1->getValue());
|
||||||
}
|
|
||||||
if (arg1 && arg1->getType() == SAPFOR::CFG_ARG_TYPE::REG) {
|
if (arg1 && arg1->getType() == SAPFOR::CFG_ARG_TYPE::REG)
|
||||||
|
{
|
||||||
auto foundVariables = findRegisterSourceVariables(blocks, arg1);
|
auto foundVariables = findRegisterSourceVariables(blocks, arg1);
|
||||||
for (auto var : foundVariables) {
|
for (auto var : foundVariables)
|
||||||
inductiveVars.insert(var->getValue());
|
inductiveVars.insert(var->getValue());
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -289,70 +309,71 @@ void findInductiveVars(const std::vector<SAPFOR::BasicBlock*>& blocks, const std
|
|||||||
|
|
||||||
set<string> finalInductiveVars;
|
set<string> finalInductiveVars;
|
||||||
|
|
||||||
for (auto instrWrapper : loopHeader->getInstructions()) {
|
for (auto instrWrapper : loopHeader->getInstructions())
|
||||||
|
{
|
||||||
auto instr = instrWrapper->getInstruction();
|
auto instr = instrWrapper->getInstruction();
|
||||||
if (!instr || instr->getOperation() != SAPFOR::CFG_OP::F_CALL || !instr->getArg1() || instr->getArg1()->getValue() != "FI_FUNCTION") continue;
|
if (!instr || instr->getOperation() != SAPFOR::CFG_OP::F_CALL || !instr->getArg1() || instr->getArg1()->getValue() != "FI_FUNCTION")
|
||||||
|
continue;
|
||||||
|
|
||||||
auto phiRes = instr->getResult();
|
auto phiRes = instr->getResult();
|
||||||
if (!phiRes || !inductiveVars.count(phiRes->getValue())) continue;
|
if (!phiRes || !inductiveVars.count(phiRes->getValue()))
|
||||||
|
continue;
|
||||||
|
|
||||||
auto currentBlock = findInstructionBlock(instr, blocks);
|
auto currentBlock = findInstructionBlock(instr, blocks);
|
||||||
if (!currentBlock) continue;
|
if (!currentBlock)
|
||||||
|
continue;
|
||||||
|
|
||||||
auto phiArgs = getPhiArguments(currentBlock, instr);
|
auto phiArgs = getPhiArguments(currentBlock, instr);
|
||||||
|
|
||||||
bool hasInLoopDefinition = false;
|
bool hasInLoopDefinition = false;
|
||||||
|
|
||||||
for (const auto& argInstr : phiArgs) {
|
for (const auto& argInstr : phiArgs)
|
||||||
if (!argInstr) continue;
|
{
|
||||||
|
if (!argInstr)
|
||||||
|
continue;
|
||||||
|
|
||||||
int definitionInstrNumber = stoi(argInstr->getArg1()->getValue());
|
int definitionInstrNumber = stoi(argInstr->getArg1()->getValue());
|
||||||
if (definitionInstrNumber == -1) continue;
|
if (definitionInstrNumber == -1)
|
||||||
|
continue;
|
||||||
|
|
||||||
auto phiBlock = findInstructionBlockByNumber(definitionInstrNumber, blocks);
|
auto phiBlock = findInstructionBlockByNumber(definitionInstrNumber, blocks);
|
||||||
if (!phiBlock) continue;
|
if (!phiBlock)
|
||||||
|
continue;
|
||||||
|
|
||||||
if (std::find(Loopblocks.begin(), Loopblocks.end(), phiBlock) != Loopblocks.end()) {
|
if (std::find(Loopblocks.begin(), Loopblocks.end(), phiBlock) != Loopblocks.end())
|
||||||
hasInLoopDefinition = true;
|
hasInLoopDefinition = true;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasInLoopDefinition) {
|
if (hasInLoopDefinition)
|
||||||
finalInductiveVars.insert(phiRes->getValue());
|
finalInductiveVars.insert(phiRes->getValue());
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto i : finalInductiveVars) {
|
for (auto i : finalInductiveVars)
|
||||||
std::cout << "Confirmed inductive variable: " << i << std::endl;
|
cout << "Confirmed inductive variable: " << i << endl;
|
||||||
}
|
|
||||||
|
|
||||||
if (finalInductiveVars.empty()) {
|
if (finalInductiveVars.empty())
|
||||||
std::cout << "No confirmed inductive variables found." << std::endl;
|
cout << "No confirmed inductive variables found." << endl;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SAPFOR::Instruction* findInstructionAfterLoop(const std::vector<SAPFOR::BasicBlock*>& loopBody) {
|
static SAPFOR::Instruction* findInstructionAfterLoop(const std::vector<SAPFOR::BasicBlock*>& loopBody)
|
||||||
|
{
|
||||||
set<SAPFOR::BasicBlock*> loopSet(loopBody.begin(), loopBody.end());
|
set<SAPFOR::BasicBlock*> loopSet(loopBody.begin(), loopBody.end());
|
||||||
|
|
||||||
for (auto block : loopBody) {
|
for (auto block : loopBody)
|
||||||
for (auto succ : block->getNext()) {
|
{
|
||||||
if (!loopSet.count(succ)) {
|
for (auto succ : block->getNext())
|
||||||
|
{
|
||||||
|
if (!loopSet.count(succ))
|
||||||
|
{
|
||||||
// <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
// <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
auto instructions = succ->getInstructions();
|
auto instructions = succ->getInstructions();
|
||||||
for (auto wrapper : instructions) {
|
for (auto wrapper : instructions)
|
||||||
if (auto instr = wrapper->getInstruction()) {
|
if (auto instr = wrapper->getInstruction())
|
||||||
return instr;
|
return instr;
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nullptr; // <20><> <20><><EFBFBD><EFBFBD><EFBFBD>
|
return NULL; // <20><> <20><><EFBFBD><EFBFBD><EFBFBD>
|
||||||
}
|
|
||||||
|
|
||||||
bool isEqual(const char* cstr, const std::string& str) {
|
|
||||||
return str == cstr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void findImplicitLoops(const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& fullIR_SSA, const char* fileName)
|
void findImplicitLoops(const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& fullIR_SSA, const char* fileName)
|
||||||
@@ -362,7 +383,7 @@ void findImplicitLoops(const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& fullIR
|
|||||||
//for (auto j : i.second)
|
//for (auto j : i.second)
|
||||||
// printblock(j);
|
// printblock(j);
|
||||||
|
|
||||||
if (!isEqual(fileName, i.first->fileName))
|
if (fileName != i.first->fileName)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
map<int, int> visited;
|
map<int, int> visited;
|
||||||
@@ -398,7 +419,6 @@ void findImplicitLoops(const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& fullIR
|
|||||||
|
|
||||||
findInductiveVars(i.second, loopBody, header, loopExits);
|
findInductiveVars(i.second, loopBody, header, loopExits);
|
||||||
|
|
||||||
|
|
||||||
SAPFOR::Instruction* instructionAfterLoop = findInstructionAfterLoop(loopBody);
|
SAPFOR::Instruction* instructionAfterLoop = findInstructionAfterLoop(loopBody);
|
||||||
|
|
||||||
if (instructionAfterLoop == NULL) {
|
if (instructionAfterLoop == NULL) {
|
||||||
@@ -419,24 +439,20 @@ void findImplicitLoops(const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& fullIR
|
|||||||
|
|
||||||
if (firstInstruction->getOperator()->variant() == FOR_NODE) {
|
if (firstInstruction->getOperator()->variant() == FOR_NODE) {
|
||||||
SgForStmt* stmt = isSgForStmt(firstInstruction->getOperator());
|
SgForStmt* stmt = isSgForStmt(firstInstruction->getOperator());
|
||||||
|
|
||||||
cout << "for loop" << endl;// << stmt->sunparse() << endl;
|
cout << "for loop" << endl;// << stmt->sunparse() << endl;
|
||||||
}
|
}
|
||||||
else if (firstInstruction->getOperator()->variant() == WHILE_NODE) {
|
else if (firstInstruction->getOperator()->variant() == WHILE_NODE) {
|
||||||
SgWhileStmt* stmt = isSgWhileStmt(firstInstruction->getOperator());
|
SgWhileStmt* stmt = isSgWhileStmt(firstInstruction->getOperator());
|
||||||
|
|
||||||
cout << (stmt->conditional() == NULL ? "infinit" : "") << "while loop" << endl;//<< stmt->sunparse() << endl;
|
cout << (stmt->conditional() == NULL ? "infinit" : "") << "while loop" << endl;//<< stmt->sunparse() << endl;
|
||||||
}
|
}
|
||||||
else if (firstInstruction->getOperator()->variant() == DO_WHILE_NODE) {
|
else if (firstInstruction->getOperator()->variant() == DO_WHILE_NODE) {
|
||||||
SgWhileStmt* stmt = isSgDoWhileStmt(firstInstruction->getOperator());
|
SgWhileStmt* stmt = isSgDoWhileStmt(firstInstruction->getOperator());
|
||||||
|
|
||||||
cout << "do while loop" << endl;// << stmt->sunparse() << endl;
|
cout << "do while loop" << endl;// << stmt->sunparse() << endl;
|
||||||
}
|
}
|
||||||
else if (firstInstruction->getOperator()->variant() == LOOP_NODE) {
|
else if (firstInstruction->getOperator()->variant() == LOOP_NODE) {
|
||||||
cout << "not known loop" << endl;// << firstInstruction->getOperator()->sunparse() << endl;
|
cout << "not known loop" << endl;// << firstInstruction->getOperator()->sunparse() << endl;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
||||||
cout << "goto loop" << endl;// firstInstruction->getOperator()->sunparse() << endl;
|
cout << "goto loop" << endl;// firstInstruction->getOperator()->sunparse() << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1032,10 +1032,7 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne
|
|||||||
countOfTransform += removeDeadCode(func->funcPointer, allFuncInfo, commonBlocks);
|
countOfTransform += removeDeadCode(func->funcPointer, allFuncInfo, commonBlocks);
|
||||||
}
|
}
|
||||||
else if (curr_regime == BUILD_IR_SSA_FORM)
|
else if (curr_regime == BUILD_IR_SSA_FORM)
|
||||||
{
|
buildIRSSAForm(fullIR, fullIR_SSA);
|
||||||
if (fullIR_SSA.size() == 0)
|
|
||||||
buildIRSSAForm(fullIR, &fullIR_SSA);
|
|
||||||
}
|
|
||||||
else if (curr_regime == FIND_IMPLICIT_LOOPS)
|
else if (curr_regime == FIND_IMPLICIT_LOOPS)
|
||||||
findImplicitLoops(fullIR_SSA, file_name);
|
findImplicitLoops(fullIR_SSA, file_name);
|
||||||
else if (curr_regime == FIND_PRIVATE_ARRAYS)
|
else if (curr_regime == FIND_PRIVATE_ARRAYS)
|
||||||
|
|||||||
Reference in New Issue
Block a user