fixed code style
This commit is contained in:
@@ -52,10 +52,7 @@ namespace SAPFOR
|
||||
}
|
||||
}
|
||||
Argument(const Argument& other)
|
||||
: number(other.number),
|
||||
type(other.type),
|
||||
mType(other.mType),
|
||||
value(other.value)
|
||||
: number(other.number), type(other.type), mType(other.mType), value(other.value)
|
||||
{ }
|
||||
|
||||
void setType(CFG_ARG_TYPE newType) { type = newType; }
|
||||
@@ -137,7 +134,7 @@ namespace SAPFOR
|
||||
{
|
||||
if (arg == NULL)
|
||||
return "";
|
||||
return arg->getValue();
|
||||
return arg->getValue();
|
||||
}
|
||||
public:
|
||||
|
||||
@@ -205,7 +202,7 @@ namespace SAPFOR
|
||||
{
|
||||
std::string res = "";
|
||||
|
||||
std::string resultVal = getArgValue(result);
|
||||
std::string resultVal = getArgValue(result);
|
||||
std::string arg1Val = getArgValue(arg1);
|
||||
std::string arg2Val = getArgValue(arg2);
|
||||
|
||||
|
||||
@@ -18,18 +18,16 @@ using namespace SAPFOR;
|
||||
typedef SAPFOR::BasicBlock BBlock;
|
||||
typedef SAPFOR::Argument BArgument;
|
||||
|
||||
static void printBlock(BBlock* block) {
|
||||
static void printBlock(BBlock* block)
|
||||
{
|
||||
cout << "block - " << block->getNumber() << endl;
|
||||
cout << "next -";
|
||||
for (auto i : block->getNext())
|
||||
{
|
||||
cout << " " << i->getNumber();
|
||||
}
|
||||
|
||||
cout << endl << "prev -";
|
||||
for (auto i : block->getPrev())
|
||||
{
|
||||
cout << " " << i->getNumber();
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
for (auto i : block->getInstructions())
|
||||
@@ -37,45 +35,49 @@ static void printBlock(BBlock* block) {
|
||||
string resValue = "";
|
||||
string arg1Value = "";
|
||||
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();
|
||||
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();
|
||||
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();
|
||||
i->getInstruction()->getArg2()->setValue(i->getInstruction()->getArg2()->getValue() + to_string(i->getInstruction()->getArg2()->getNumber()));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static bool compareVectors(const vector<T>* vec1, const vector<T>* vec2) {
|
||||
if (vec1 == vec2) {
|
||||
static bool compareVectors(const vector<T>* vec1, const vector<T>* vec2)
|
||||
{
|
||||
if (vec1 == vec2)
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!vec1 || !vec2) {
|
||||
|
||||
if (!vec1 || !vec2)
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
vector<T> sortedVec1 = *vec1;
|
||||
vector<T> sortedVec2 = *vec2;
|
||||
|
||||
@@ -86,15 +88,16 @@ static bool compareVectors(const vector<T>* vec1, const vector<T>* vec2) {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static vector<T>* getCommonElements(const vector<vector<T>>* vectors) {
|
||||
if (!vectors || vectors->empty()) {
|
||||
static vector<T>* getCommonElements(const vector<vector<T>>* vectors)
|
||||
{
|
||||
if (!vectors || vectors->empty())
|
||||
return new vector<T>(); // Return an empty vector if input is null or empty
|
||||
}
|
||||
|
||||
|
||||
// Start with the first vector
|
||||
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;
|
||||
|
||||
// Sort the current vector and common result for intersection
|
||||
@@ -113,23 +116,25 @@ static vector<T>* getCommonElements(const vector<vector<T>>* vectors) {
|
||||
*commonElements = tempCommon;
|
||||
|
||||
// If no common elements left, break early
|
||||
if (commonElements->empty()) {
|
||||
if (commonElements->empty())
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
bool changed = true;
|
||||
while (changed) {
|
||||
while (changed)
|
||||
{
|
||||
changed = false;
|
||||
|
||||
for (auto currentBlock : blocks) {
|
||||
for (auto currentBlock : blocks)
|
||||
{
|
||||
auto pred = currentBlock->getPrev();
|
||||
auto prevDominators = new vector<vector<BBlock*>>();
|
||||
|
||||
@@ -139,7 +144,8 @@ static map<BBlock*, vector<BBlock*>> findDominators(vector<BBlock*> blocks) {
|
||||
auto currentBlockResult = getCommonElements(prevDominators);
|
||||
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;
|
||||
changed = true;
|
||||
}
|
||||
@@ -149,42 +155,42 @@ static map<BBlock*, vector<BBlock*>> findDominators(vector<BBlock*> blocks) {
|
||||
return result;
|
||||
}
|
||||
|
||||
static void RenumberBlocks(BBlock* current, int* n, map<int, int>* res, set<BBlock*>* visited) {
|
||||
if (visited->find(current) != visited->end()) {
|
||||
static void renumberBlocks(BBlock* current, int* n, map<int, int>* res, set<BBlock*>* visited) {
|
||||
if (visited->find(current) != visited->end())
|
||||
return;
|
||||
}
|
||||
|
||||
visited->insert(current);
|
||||
|
||||
vector<BBlock*> nextBlocks = current->getNext();
|
||||
|
||||
sort(nextBlocks.begin(), nextBlocks.end(), [](BBlock* a, BBlock* b) {
|
||||
return a->getInstructions()[0]->getInstruction()->getOperator()->lineNumber() > b->getInstructions()[0]->getInstruction()->getOperator()->lineNumber();
|
||||
});
|
||||
|
||||
for (auto i : nextBlocks) {
|
||||
RenumberBlocks(i, n, res, visited);
|
||||
}
|
||||
|
||||
for (auto i : nextBlocks)
|
||||
renumberBlocks(i, n, res, visited);
|
||||
|
||||
(*res)[current->getNumber()] = *n;
|
||||
*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;
|
||||
|
||||
for (auto block : blocks) {
|
||||
for (auto& block : blocks)
|
||||
result[block] = *(new vector<BBlock*>());
|
||||
}
|
||||
|
||||
for (auto block : blocks) {
|
||||
if (block->getPrev().size() > 1) {
|
||||
for (auto prev : block->getPrev()) {
|
||||
for (auto& block : blocks)
|
||||
{
|
||||
if (block->getPrev().size() > 1)
|
||||
{
|
||||
for (auto prev : block->getPrev())
|
||||
{
|
||||
auto tmpBlock = prev;
|
||||
auto test = iDominators[block];
|
||||
auto test2 = iDominators[prev];
|
||||
|
||||
while (tmpBlock != iDominators[block]) {
|
||||
while (tmpBlock != iDominators[block])
|
||||
{
|
||||
result[tmpBlock].push_back(block);
|
||||
tmpBlock = iDominators[tmpBlock];
|
||||
}
|
||||
@@ -195,82 +201,87 @@ static map<BBlock*, vector<BBlock*>> findDominatorBorders(vector<BBlock*>& block
|
||||
return result;
|
||||
}
|
||||
|
||||
static BBlock* findImmediateDominatorsDfsHelper(BBlock* block, BBlock* currentBlock, BBlock* currentImmediateDominator, vector<BBlock*> &visited, map<BBlock*, vector<BBlock*>>& dominators) {
|
||||
if (block == currentBlock) {
|
||||
static BBlock* findImmediateDominatorsDfsHelper(BBlock* block, BBlock* currentBlock, BBlock* currentImmediateDominator, vector<BBlock*> &visited, map<BBlock*, vector<BBlock*>>& dominators)
|
||||
{
|
||||
if (block == currentBlock)
|
||||
return currentImmediateDominator;
|
||||
}
|
||||
|
||||
if (find(visited.begin(), visited.end(), currentBlock) != visited.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
if (find(visited.begin(), visited.end(), currentBlock) != visited.end())
|
||||
return NULL;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
for (auto nextBlock : currentBlock->getNext()) {
|
||||
for (auto nextBlock : currentBlock->getNext())
|
||||
{
|
||||
auto result = findImmediateDominatorsDfsHelper(block, nextBlock, currentImmediateDominator, visited, dominators);
|
||||
|
||||
if (result) {
|
||||
if (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;
|
||||
|
||||
for (const auto& pair : dominators) {
|
||||
for (const auto& pair : dominators)
|
||||
{
|
||||
BBlock* b = pair.first;
|
||||
|
||||
if (b == entry) continue;
|
||||
if (b == entry)
|
||||
continue;
|
||||
|
||||
const auto& doms = pair.second;
|
||||
|
||||
BBlock* candidate = nullptr;
|
||||
for (auto d : doms) {
|
||||
if (d == b) continue;
|
||||
bool isImmediate = true;
|
||||
BBlock* candidate = NULL;
|
||||
for (auto d : doms)
|
||||
{
|
||||
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);
|
||||
if (std::find(domsOfOther.begin(), domsOfOther.end(), d) != domsOfOther.end()) {
|
||||
if (std::find(domsOfOther.begin(), domsOfOther.end(), d) != domsOfOther.end())
|
||||
{
|
||||
isImmediate = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (isImmediate) {
|
||||
if (isImmediate)
|
||||
{
|
||||
candidate = d;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (candidate) {
|
||||
if (candidate)
|
||||
iDom[b] = candidate;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
for (const auto& domPair : dominators) {
|
||||
BBlock* block = domPair.first;
|
||||
const vector<BBlock*>& domBlocks = domPair.second;
|
||||
for (const auto& [block, domBlocks] : dominators) {
|
||||
vector<BBlock*> visited;
|
||||
|
||||
if (block == fistBlock) {
|
||||
if (block == fistBlock)
|
||||
continue;
|
||||
}
|
||||
|
||||
iDominators[block] = findImmediateDominatorsDfsHelper(block, fistBlock, fistBlock, visited, dominators);
|
||||
}
|
||||
@@ -278,19 +289,22 @@ static map<BBlock*, BBlock*> findImmediateDominators(map<BBlock*, vector<BBlock*
|
||||
return iDominators;
|
||||
}
|
||||
|
||||
static vector<BArgument*> getDefForBlock(const BBlock& block) {
|
||||
static vector<BArgument*> getDefForBlock(const BBlock& block)
|
||||
{
|
||||
vector<BArgument*> def;
|
||||
const auto& instructions = block.getInstructions();
|
||||
|
||||
for (const auto& irBlock : instructions) {
|
||||
if (irBlock) {
|
||||
for (const auto& irBlock : instructions)
|
||||
{
|
||||
if (irBlock)
|
||||
{
|
||||
Instruction* instr = irBlock->getInstruction();
|
||||
|
||||
if (instr) {
|
||||
if (instr)
|
||||
{
|
||||
BArgument* result = instr->getResult();
|
||||
if (result) {
|
||||
if (result)
|
||||
def.push_back(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -298,31 +312,35 @@ static vector<BArgument*> getDefForBlock(const BBlock& block) {
|
||||
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;
|
||||
map<BArgument*, set<BBlock*>> varBlocks;
|
||||
|
||||
for (auto block : blocks) {
|
||||
for (auto& block : blocks)
|
||||
{
|
||||
set<BArgument*> def;
|
||||
const auto& instructions = block->getInstructions();
|
||||
|
||||
for (const auto& irBlock : instructions) {
|
||||
if (irBlock) {
|
||||
for (const auto& irBlock : instructions)
|
||||
{
|
||||
if (irBlock)
|
||||
{
|
||||
Instruction* instr = irBlock->getInstruction();
|
||||
|
||||
if (instr) {
|
||||
if (instr)
|
||||
{
|
||||
auto arg1 = instr->getArg1();
|
||||
auto arg2 = instr->getArg2();
|
||||
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);
|
||||
}
|
||||
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);
|
||||
varBlocks[res].insert(block);
|
||||
}
|
||||
@@ -334,23 +352,29 @@ static pair<set<BArgument*>, map<BArgument*, set<BBlock*>>> getGlobalsAndVarBloc
|
||||
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;
|
||||
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");
|
||||
|
||||
for (auto var : globals) {
|
||||
for (auto& var : globals)
|
||||
{
|
||||
auto worklist = varBlocks[var];
|
||||
set<BBlock*> hasFiFunction;
|
||||
|
||||
while (!worklist.empty()) {
|
||||
while (!worklist.empty())
|
||||
{
|
||||
auto block = *worklist.begin();
|
||||
worklist.erase(block);
|
||||
|
||||
for (auto dfBlock : dominatorBorders[block]) {
|
||||
if (hasFiFunction.find(dfBlock) == hasFiFunction.end()) {
|
||||
for (auto dfBlock : dominatorBorders[block])
|
||||
{
|
||||
if (hasFiFunction.find(dfBlock) == hasFiFunction.end())
|
||||
{
|
||||
hasFiFunction.insert(dfBlock);
|
||||
|
||||
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);
|
||||
@@ -365,7 +389,8 @@ static void getBlocksWithFiFunctions(vector<BBlock*> blocks, set<BArgument*> glo
|
||||
//return blocksWithFiFunctions;
|
||||
}
|
||||
|
||||
static string ToString(CFG_ARG_TYPE type) {
|
||||
static string ToString(CFG_ARG_TYPE type)
|
||||
{
|
||||
switch (type) {
|
||||
case CFG_ARG_TYPE::NONE: return "NONE";
|
||||
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;
|
||||
for (size_t i = 0; i < originalBlocks.size(); ++i) {
|
||||
for (size_t i = 0; i < originalBlocks.size(); ++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* copiedBlock = copiedBlocks[i];
|
||||
|
||||
auto prevCopy = copiedBlock->getPrev();
|
||||
for (auto j : prevCopy) {
|
||||
for (auto j : prevCopy)
|
||||
copiedBlock->removePrev(j);
|
||||
}
|
||||
|
||||
|
||||
// Копируем, затем удаляем next связи
|
||||
auto nextCopy = copiedBlock->getNext();
|
||||
for (auto j : nextCopy) {
|
||||
for (auto j : nextCopy)
|
||||
copiedBlock->removeNext(j);
|
||||
}
|
||||
|
||||
|
||||
// Восстанавливаем связи succ (следующих блоков)
|
||||
for (auto* succ : originalBlock->getNext()) {
|
||||
for (auto* succ : originalBlock->getNext())
|
||||
copiedBlock->addNext(blockMapping[succ]);
|
||||
}
|
||||
|
||||
|
||||
// Восстанавливаем связи prev (предыдущих блоков)
|
||||
for (auto* prev : originalBlock->getPrev()) {
|
||||
for (auto* prev : originalBlock->getPrev())
|
||||
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()];
|
||||
counter[var->getValue()]++;
|
||||
|
||||
@@ -427,48 +449,57 @@ static BArgument* NewName(BArgument* var, map<string, int>& counter, map<string,
|
||||
return newName;
|
||||
}
|
||||
|
||||
static void RenameFiFunctionResultVar(BBlock* block, map<string, int>& counter, map<string, stack<BArgument*>>& stack) {
|
||||
for (auto irBlock : block->getInstructions()) {
|
||||
static void renameFiFunctionResultVar(BBlock* block, map<string, int>& counter, map<string, stack<BArgument*>>& stack) {
|
||||
for (auto irBlock : block->getInstructions())
|
||||
{
|
||||
auto instruction = irBlock->getInstruction();
|
||||
if (instruction->getOperation() == CFG_OP::F_CALL && instruction->getArg1() != nullptr && instruction->getArg1()->getValue() == "FI_FUNCTION" && instruction->getResult() != nullptr) {
|
||||
instruction->setResult(NewName(instruction->getResult(), counter, stack, instruction->getNumber()));
|
||||
if (instruction->getOperation() == CFG_OP::F_CALL && instruction->getArg1() != NULL &&
|
||||
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) {
|
||||
for (auto irBlock : block->getInstructions()) {
|
||||
static void renameInstructionVars(BBlock* block, map<string, int>& counter, map<string, stack<BArgument*>>& stack)
|
||||
{
|
||||
for (auto irBlock : block->getInstructions())
|
||||
{
|
||||
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());
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
if (instruction->getResult() != nullptr && instruction->getResult()->getType() == CFG_ARG_TYPE::VAR) {
|
||||
instruction->setResult(NewName(instruction->getResult(), counter, stack, instruction->getNumber()));
|
||||
}
|
||||
if (instruction->getResult() != NULL && instruction->getResult()->getType() == CFG_ARG_TYPE::VAR)
|
||||
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& instructions = block->getInstructions();
|
||||
for (auto i = 0; i < size; i++) {
|
||||
|
||||
for (auto i = 0; i < size; i++)
|
||||
{
|
||||
auto irBlock = instructions[i];
|
||||
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;
|
||||
|
||||
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()));
|
||||
paramInstruction = new Instruction(CFG_OP::PARAM, tmp);
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
BArgument* tmp = new BArgument(CFG_ARG_TYPE::CONST, CFG_MEM_TYPE::COMMON_, "-1");
|
||||
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;
|
||||
|
||||
// Проходим по всем элементам map
|
||||
for (auto& pair : iDominators) {
|
||||
for (auto& pair : iDominators)
|
||||
// Если значение равно x, добавляем ключ в результат
|
||||
if (pair.second == x) {
|
||||
if (pair.second == x)
|
||||
result.push_back(pair.first);
|
||||
}
|
||||
}
|
||||
|
||||
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()) {
|
||||
RenameFiFunctionArgsVar(successor, stack);
|
||||
}
|
||||
for (auto* child : findBlocksWithValue(iDominators, block))
|
||||
renameIR(child, iDominators, counter, stack);
|
||||
|
||||
for (auto* child : findBlocksWithValue(iDominators, block)) {
|
||||
RenameIR(child, iDominators, counter, stack);
|
||||
}
|
||||
|
||||
for (auto& irBlock : block->getInstructions()) {
|
||||
for (auto& irBlock : block->getInstructions())
|
||||
{
|
||||
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();
|
||||
stack[varName].pop();
|
||||
}
|
||||
}
|
||||
|
||||
for (auto& irBlock : block->getInstructions()) {
|
||||
for (auto& irBlock : block->getInstructions())
|
||||
{
|
||||
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();
|
||||
stack[varName].pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void buildIRSSAForm(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;
|
||||
|
||||
void buildIRSSAForm(const map<FuncInfo*, vector<BBlock*>>& fullIR,
|
||||
map<FuncInfo*, vector<BBlock*>>& result) {
|
||||
|
||||
for (auto& [funcinfo, funcIRConst]: fullIR) {
|
||||
cout << "Testing " << funcinfo->funcName << endl;
|
||||
|
||||
vector<BBlock*> funcIR;
|
||||
|
||||
for (auto i : funcIRConst) {
|
||||
for (auto i : funcIRConst)
|
||||
funcIR.push_back(new BBlock(*i));
|
||||
}
|
||||
|
||||
restoreConnections(funcIRConst, funcIR);
|
||||
|
||||
for (auto i : funcIR) {
|
||||
@@ -571,7 +598,6 @@ void buildIRSSAForm(map<FuncInfo*, vector<BBlock*>> fullIR, map<FuncInfo*, vecto
|
||||
*/
|
||||
auto dominatorBorders = findDominatorBorders(funcIR, iDominators);
|
||||
|
||||
|
||||
/*for (auto i : dominatorBorders) {
|
||||
cout << "block - " << i.first->getNumber() << endl;
|
||||
for (auto j : i.second) {
|
||||
@@ -581,7 +607,6 @@ void buildIRSSAForm(map<FuncInfo*, vector<BBlock*>> fullIR, map<FuncInfo*, vecto
|
||||
cout << endl;
|
||||
}*/
|
||||
|
||||
|
||||
auto globalsAndVarBlocks = getGlobalsAndVarBlocks(funcIR);
|
||||
auto globals = globalsAndVarBlocks.first;
|
||||
auto varBlocks = globalsAndVarBlocks.second;
|
||||
@@ -606,7 +631,8 @@ void buildIRSSAForm(map<FuncInfo*, vector<BBlock*>> fullIR, map<FuncInfo*, vecto
|
||||
map<string, int> count;
|
||||
map<string, stack<BArgument*>> varStack;
|
||||
|
||||
for (auto var : globals) {
|
||||
for (auto var : globals)
|
||||
{
|
||||
count[var->getValue()] = 0;
|
||||
|
||||
stack<BArgument*> tmp;
|
||||
@@ -615,7 +641,7 @@ void buildIRSSAForm(map<FuncInfo*, vector<BBlock*>> fullIR, map<FuncInfo*, vecto
|
||||
varStack[var->getValue()] = tmp;
|
||||
}
|
||||
|
||||
RenameIR(funcIR[0], iDominators, count, varStack);
|
||||
renameIR(funcIR[0], iDominators, count, varStack);
|
||||
|
||||
for (auto i : funcIR) {
|
||||
//printBlock(i);
|
||||
@@ -625,6 +651,6 @@ void buildIRSSAForm(map<FuncInfo*, vector<BBlock*>> fullIR, map<FuncInfo*, vecto
|
||||
// printBlock(i);
|
||||
//}
|
||||
|
||||
(*result)[funcinfo] = funcIR;
|
||||
result[funcinfo] = funcIR;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,4 +3,4 @@
|
||||
#include "CFGraph.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);
|
||||
Reference in New Issue
Block a user