Compare commits
64 Commits
master
...
f322306344
| Author | SHA1 | Date | |
|---|---|---|---|
| f322306344 | |||
| f7c66f537d | |||
| 267f85ae27 | |||
| b137ea5ef3 | |||
| 6a84171382 | |||
| cd209a587a | |||
|
|
0b0d7d373b | ||
|
|
781a892497 | ||
|
|
a55440c071 | ||
|
|
996f7ead1b | ||
|
|
5231eeacd8 | ||
|
|
7460cf6e59 | ||
|
|
5596b57021 | ||
| 3ad972c188 | |||
| dde0bcdee5 | |||
| 65cdbef201 | |||
| 2ad239d1e3 | |||
| 12f311077b | |||
|
|
a2e0a99891 | ||
| 27a350dac0 | |||
| 3c1032bfd0 | |||
| 189374274e | |||
| de4690513b | |||
| 86ab34e7f3 | |||
| 06cfe83666 | |||
| a3c1e1e5d1 | |||
| 75e89ab868 | |||
| d4fb323f86 | |||
|
|
0c9f0664fd | ||
|
|
90a608230c | ||
|
|
d6df2f6b5f | ||
|
|
90894a4723 | ||
|
|
26fe1d3f61 | ||
|
|
68d2f3253c | ||
|
|
09401376c7 | ||
|
|
a0c8f78868 | ||
|
|
2aa9e569f4 | ||
|
|
9e5ee78b80 | ||
|
|
d5d5514e17 | ||
|
|
68c779790d | ||
|
|
c6b09ad285 | ||
|
|
fd402b6ab0 | ||
|
|
b76753c285 | ||
|
|
44600a50c1 | ||
|
|
d2f5e5fcc1 | ||
|
|
18ac0ae47c | ||
|
|
00b6026761 | ||
|
|
2036fab86f | ||
|
|
da4e992926 | ||
|
|
8e4a4c78ad | ||
| b4038b532f | |||
| dec1a853db | |||
| 7df02737c7 | |||
| d267dc047a | |||
| 12a810ad35 | |||
| 1522dc7f27 | |||
| bd52d5c6ec | |||
| 4ba2bb4c94 | |||
| 60544ea4d6 | |||
|
|
b40e969d02 | ||
| d062e52dd6 | |||
| 392ad97738 | |||
| c2c111586c | |||
| 172eedfef1 |
@@ -209,7 +209,8 @@ set(TRANSFORMS
|
||||
${TR_IMPLICIT_NONE}
|
||||
${TR_REPLACE_ARRAYS_IN_IO})
|
||||
|
||||
set(CFG src/CFGraph/IR.cpp
|
||||
set(CFG
|
||||
src/CFGraph/IR.cpp
|
||||
src/CFGraph/IR.h
|
||||
src/CFGraph/CFGraph.cpp
|
||||
src/CFGraph/CFGraph.h
|
||||
@@ -219,6 +220,8 @@ set(CFG src/CFGraph/IR.cpp
|
||||
src/CFGraph/live_variable_analysis.h
|
||||
src/CFGraph/private_variables_analysis.cpp
|
||||
src/CFGraph/private_variables_analysis.h
|
||||
src/CFGraph/IRSSAForm.cpp
|
||||
src/CFGraph/IRSSAForm.h
|
||||
)
|
||||
|
||||
set(DATA_FLOW
|
||||
|
||||
@@ -56,6 +56,23 @@ void BBlock::addInstruction(IR_Block* item)
|
||||
item->setBasicBlock(this);
|
||||
}
|
||||
|
||||
void BBlock::addInstructionInFront(IR_Block* item)
|
||||
{
|
||||
instructions.insert(instructions.begin(), item);
|
||||
item->setBasicBlock(this);
|
||||
}
|
||||
|
||||
void BBlock::addInstructionBeforeInstruction(IR_Block* item, Instruction* instruction)
|
||||
{
|
||||
for (auto it = instructions.begin(); it != instructions.end(); ++it) {
|
||||
if ((*it)->getInstruction() == instruction) {
|
||||
instructions.insert(it, item);
|
||||
item->setBasicBlock(this);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int BBlock::removePrev(BBlock* removed)
|
||||
{
|
||||
auto it = std::remove(prev.begin(), prev.end(), removed);
|
||||
@@ -512,7 +529,7 @@ static int buildReachingDefs(const vector<BBlock*>& CFG, const FuncInfo* currF,
|
||||
return iter;
|
||||
}
|
||||
|
||||
//Kosaraju<6A>Sharir algorithm
|
||||
//Kosaraju<6A>Sharir algorithm
|
||||
static vector<int> getStronglyConnectedComps(vector<vector<int>>& g) {
|
||||
// 1. For each vertex u of the graph, mark u as unvisited. Let l be empty.
|
||||
auto size = g.size();
|
||||
|
||||
@@ -39,6 +39,8 @@ namespace SAPFOR
|
||||
BasicBlock(IR_Block* item);
|
||||
BasicBlock(const BasicBlock& copyFrom);
|
||||
|
||||
void addInstructionBeforeInstruction(IR_Block* item, Instruction* istruction);
|
||||
void addInstructionInFront(IR_Block* item);
|
||||
void addInstruction(IR_Block* item);
|
||||
void addPrev(BasicBlock* prev_) { prev.push_back(prev_); }
|
||||
void addNext(BasicBlock* next_) { next.push_back(next_); }
|
||||
|
||||
@@ -1613,3 +1613,429 @@ vector<IR_Block*> buildIR(SgStatement* function, const FuncInfo* func, const vec
|
||||
|
||||
return blocks;
|
||||
}
|
||||
|
||||
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) {
|
||||
if (!block) return;
|
||||
|
||||
if (visit[block->getNumber()] == VISITED) {
|
||||
cout << "error";
|
||||
return;
|
||||
}
|
||||
|
||||
if (visit[block->getNumber()] == VISITING) {
|
||||
visit[block->getNumber()] = VISITED;
|
||||
startAndEnd.push_back(make_pair(prev, block));
|
||||
return;
|
||||
}
|
||||
|
||||
visit[block->getNumber()] = VISITING;
|
||||
for (auto i : block->getNext()) {
|
||||
dfs(i, visit, startAndEnd, block);
|
||||
}
|
||||
}
|
||||
|
||||
static void printBlock(SAPFOR::BasicBlock* 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())
|
||||
{
|
||||
string resValue = "";
|
||||
string arg1Value = "";
|
||||
string arg2Value = "";
|
||||
if (i->getInstruction()->getResult() != nullptr && 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) {
|
||||
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) {
|
||||
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) {
|
||||
i->getInstruction()->getResult()->setValue(resValue);
|
||||
}
|
||||
if (i->getInstruction()->getArg1() != nullptr && 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) {
|
||||
i->getInstruction()->getArg2()->setValue(arg2Value);
|
||||
}
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
void getLoopBody(SAPFOR::BasicBlock* loopHeader, const std::set<SAPFOR::BasicBlock*>& loopExits, std::vector<SAPFOR::BasicBlock*>& loopBody) {
|
||||
std::set<SAPFOR::BasicBlock*> visited;
|
||||
std::stack<SAPFOR::BasicBlock*> stack;
|
||||
|
||||
stack.push(loopHeader);
|
||||
|
||||
while (!stack.empty()) {
|
||||
auto block = stack.top();
|
||||
stack.pop();
|
||||
|
||||
if (visited.count(block)) continue;
|
||||
visited.insert(block);
|
||||
|
||||
for (auto succ : block->getNext()) {
|
||||
if (loopExits.count(succ)) continue;
|
||||
if (!visited.count(succ)) {
|
||||
stack.push(succ);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::set<SAPFOR::BasicBlock*> backReachable;
|
||||
std::stack<SAPFOR::BasicBlock*> reverseStack;
|
||||
reverseStack.push(loopHeader);
|
||||
|
||||
while (!reverseStack.empty()) {
|
||||
auto block = reverseStack.top();
|
||||
reverseStack.pop();
|
||||
|
||||
if (backReachable.count(block)) continue;
|
||||
backReachable.insert(block);
|
||||
|
||||
for (auto pred : block->getPrev()) {
|
||||
if (visited.count(pred) && !backReachable.count(pred)) {
|
||||
reverseStack.push(pred);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (auto block : visited) {
|
||||
if (backReachable.count(block)) {
|
||||
loopBody.push_back(block);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
set<SAPFOR::Argument*> findRegisterSourceVariables(const std::vector<SAPFOR::BasicBlock*>& blocks, SAPFOR::Argument* var) {
|
||||
std::set<SAPFOR::Argument*> result;
|
||||
std::set<SAPFOR::Argument*> visited;
|
||||
std::stack<SAPFOR::Argument*> workStack;
|
||||
workStack.push(var);
|
||||
|
||||
auto isBinaryOp = [](SAPFOR::CFG_OP op) {
|
||||
return op == SAPFOR::CFG_OP::ADD || op == SAPFOR::CFG_OP::SUBT ||
|
||||
op == SAPFOR::CFG_OP::MULT || op == SAPFOR::CFG_OP::DIV ||
|
||||
op == SAPFOR::CFG_OP::POW ||
|
||||
op == SAPFOR::CFG_OP::GE || op == SAPFOR::CFG_OP::LE ||
|
||||
op == SAPFOR::CFG_OP::GT || op == SAPFOR::CFG_OP::LT ||
|
||||
op == SAPFOR::CFG_OP::EQ || op == SAPFOR::CFG_OP::NEQV ||
|
||||
op == SAPFOR::CFG_OP::EQV || op == SAPFOR::CFG_OP::EMPTY ||
|
||||
op == SAPFOR::CFG_OP::OR || op == SAPFOR::CFG_OP::AND;
|
||||
};
|
||||
|
||||
auto isUnaryOp = [](SAPFOR::CFG_OP op) {
|
||||
return op == SAPFOR::CFG_OP::UN_ADD || op == SAPFOR::CFG_OP::UN_MINUS ||
|
||||
op == SAPFOR::CFG_OP::NOT || op == SAPFOR::CFG_OP::ASSIGN;
|
||||
};
|
||||
|
||||
while (!workStack.empty()) {
|
||||
auto variable = workStack.top();
|
||||
workStack.pop();
|
||||
if (!variable || visited.count(variable))
|
||||
continue;
|
||||
|
||||
visited.insert(variable);
|
||||
|
||||
for (auto block : blocks) {
|
||||
for (auto instrWrapper : block->getInstructions()) {
|
||||
auto instr = instrWrapper->getInstruction();
|
||||
if (!instr || instr->getResult() != variable)
|
||||
continue;
|
||||
|
||||
auto op = instr->getOperation();
|
||||
auto arg1 = instr->getArg1();
|
||||
auto arg2 = instr->getArg2();
|
||||
|
||||
if (isBinaryOp(op) && arg1 && arg2) {
|
||||
if (arg1->getType() == CFG_ARG_TYPE::VAR)
|
||||
result.insert(arg1);
|
||||
else if (arg1->getType() == CFG_ARG_TYPE::REG)
|
||||
workStack.push(arg1);
|
||||
|
||||
if (arg2->getType() == CFG_ARG_TYPE::VAR)
|
||||
result.insert(arg2);
|
||||
else if (arg2->getType() == CFG_ARG_TYPE::REG)
|
||||
workStack.push(arg2);
|
||||
}
|
||||
else if (isUnaryOp(op) && arg1) {
|
||||
if (arg1->getType() == CFG_ARG_TYPE::VAR)
|
||||
result.insert(arg1);
|
||||
else if (arg1->getType() == CFG_ARG_TYPE::REG)
|
||||
workStack.push(arg1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<SAPFOR::Instruction*> getPhiArguments(SAPFOR::BasicBlock* block, SAPFOR::Instruction* phiInstr) {
|
||||
std::vector<SAPFOR::Instruction*> result;
|
||||
|
||||
auto& instructions = block->getInstructions();
|
||||
bool collecting = false;
|
||||
for (int i = instructions.size() - 1; i >= 0; --i) {
|
||||
auto instr = instructions[i]->getInstruction();
|
||||
|
||||
if (collecting) {
|
||||
if (instr->getOperation() == SAPFOR::CFG_OP::PARAM) {
|
||||
auto arg = instr->getArg1();
|
||||
if (arg) {
|
||||
result.push_back(instr);
|
||||
}
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!instr) continue;
|
||||
|
||||
if (instr == phiInstr) {
|
||||
collecting = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
std::reverse(result.begin(), result.end());
|
||||
return result;
|
||||
}
|
||||
|
||||
SAPFOR::BasicBlock* findInstructionBlock(SAPFOR::Instruction* targetInstr, const std::vector<SAPFOR::BasicBlock*>& blocks) {
|
||||
for (auto block : blocks) {
|
||||
for (auto instrWrapper : block->getInstructions()) {
|
||||
auto instr = instrWrapper->getInstruction();
|
||||
if (instr == targetInstr) {
|
||||
return block;
|
||||
}
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
SAPFOR::BasicBlock* findInstructionBlockByNumber(int number, const std::vector<SAPFOR::BasicBlock*>& blocks) {
|
||||
for (auto block : blocks) {
|
||||
for (auto instrWrapper : block->getInstructions()) {
|
||||
auto instr = instrWrapper->getInstruction();
|
||||
if (instr->getNumber() == number) {
|
||||
return block;
|
||||
}
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
void findInductiveVars(const std::vector<SAPFOR::BasicBlock*>& blocks, const std::vector<SAPFOR::BasicBlock*>& Loopblocks, SAPFOR::BasicBlock* loopHeader, const std::set<SAPFOR::BasicBlock*>& loopExits) {
|
||||
std::set<string> inductiveVars;
|
||||
std::set<SAPFOR::BasicBlock*> relevantBlocks = { loopHeader };
|
||||
|
||||
for (auto block : relevantBlocks) {
|
||||
|
||||
for (auto instrWrapper : block->getInstructions()) {
|
||||
auto instr = instrWrapper->getInstruction();
|
||||
if (!instr) continue;
|
||||
|
||||
auto op = instr->getOperation();
|
||||
auto res = instr->getResult();
|
||||
auto arg1 = instr->getArg1();
|
||||
auto arg2 = instr->getArg2();
|
||||
|
||||
if (op == CFG_OP::JUMP_IF) {
|
||||
if (arg1 && arg1->getType() == CFG_ARG_TYPE::VAR) {
|
||||
inductiveVars.insert(arg1->getValue());
|
||||
}
|
||||
if (arg1 && arg1->getType() == CFG_ARG_TYPE::REG) {
|
||||
auto foundVariables = findRegisterSourceVariables(blocks, arg1);
|
||||
for (auto var : foundVariables) {
|
||||
inductiveVars.insert(var->getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::set<string> finalInductiveVars;
|
||||
|
||||
for (auto instrWrapper : loopHeader->getInstructions()) {
|
||||
auto instr = instrWrapper->getInstruction();
|
||||
if (!instr || instr->getOperation() != SAPFOR::CFG_OP::F_CALL || !instr->getArg1() || instr->getArg1()->getValue() != "FI_FUNCTION") continue;
|
||||
|
||||
auto phiRes = instr->getResult();
|
||||
if (!phiRes || !inductiveVars.count(phiRes->getValue())) continue;
|
||||
|
||||
auto currentBlock = findInstructionBlock(instr, blocks);
|
||||
if (!currentBlock) continue;
|
||||
|
||||
auto phiArgs = getPhiArguments(currentBlock, instr);
|
||||
|
||||
bool hasInLoopDefinition = false;
|
||||
|
||||
for (const auto& argInstr : phiArgs) {
|
||||
if (!argInstr) continue;
|
||||
|
||||
int definitionInstrNumber = stoi(argInstr->getArg1()->getValue());
|
||||
if (definitionInstrNumber == -1) continue;
|
||||
|
||||
auto phiBlock = findInstructionBlockByNumber(definitionInstrNumber, blocks);
|
||||
if (!phiBlock) continue;
|
||||
|
||||
if (std::find(Loopblocks.begin(), Loopblocks.end(), phiBlock) != Loopblocks.end()) {
|
||||
hasInLoopDefinition = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (hasInLoopDefinition) {
|
||||
finalInductiveVars.insert(phiRes->getValue());
|
||||
}
|
||||
}
|
||||
|
||||
for (auto i : finalInductiveVars) {
|
||||
std::cout << "Confirmed inductive variable: " << i << std::endl;
|
||||
}
|
||||
|
||||
if (finalInductiveVars.empty()) {
|
||||
std::cout << "No confirmed inductive variables found." << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
Instruction* findInstructionAfterLoop(const std::vector<SAPFOR::BasicBlock*>& loopBody) {
|
||||
std::set<SAPFOR::BasicBlock*> loopSet(loopBody.begin(), loopBody.end());
|
||||
|
||||
for (auto block : loopBody) {
|
||||
for (auto succ : block->getNext()) {
|
||||
if (!loopSet.count(succ)) {
|
||||
// Нашли выход из цикла — возьмём первую инструкцию
|
||||
auto instructions = succ->getInstructions();
|
||||
for (auto wrapper : instructions) {
|
||||
if (auto instr = wrapper->getInstruction()) {
|
||||
return instr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr; // не нашли
|
||||
}
|
||||
|
||||
bool isEqual(const char* cstr, const std::string& str) {
|
||||
return str == cstr;
|
||||
}
|
||||
|
||||
void exploreLoops(map<FuncInfo*, vector<SAPFOR::BasicBlock*>>* fullIR, const char* fileName) {
|
||||
for (auto& i : *fullIR)
|
||||
{
|
||||
//for (auto j : i.second)
|
||||
// printblock(j);
|
||||
|
||||
if (!isEqual(fileName, i.first->fileName))
|
||||
continue;
|
||||
|
||||
map<int, int> visited;
|
||||
for (auto i : i.second)
|
||||
visited[i->getNumber()] = UNVISITED;
|
||||
|
||||
//vector<int> visited(i.second.size(), UNVISITED);
|
||||
vector<pair<SAPFOR::BasicBlock*, SAPFOR::BasicBlock*>> startAndEnd;
|
||||
dfs(i.second[0], visited, startAndEnd, NULL);
|
||||
|
||||
vector<LoopGraph*> loops;
|
||||
|
||||
for (auto& [tail, header] : startAndEnd) {
|
||||
set<SAPFOR::BasicBlock*> loopExits;
|
||||
|
||||
for (auto succ : tail->getNext()) {
|
||||
if (succ != header) {
|
||||
loopExits.insert(succ);
|
||||
}
|
||||
}
|
||||
|
||||
vector<SAPFOR::BasicBlock*> loopBody;
|
||||
getLoopBody(header, loopExits, loopBody);
|
||||
|
||||
cout << "LOOP DETECTED:" << endl;
|
||||
cout << " Header: " << header->getNumber() << endl;
|
||||
cout << " Tail: " << tail->getNumber() << endl;
|
||||
cout << " Body blocks: ";
|
||||
for (auto block : loopBody) {
|
||||
cout << block->getNumber() << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
findInductiveVars(i.second, loopBody, header, loopExits);
|
||||
|
||||
|
||||
Instruction* instructionAfterLoop = findInstructionAfterLoop(loopBody);
|
||||
|
||||
if (instructionAfterLoop == NULL) {
|
||||
cout << "Warning: instruction after loop not found!" << endl;
|
||||
continue;
|
||||
}
|
||||
auto firstInstruction = header->getInstructions()[0]->getInstruction();
|
||||
auto lastInstruction = tail->getInstructions().back()->getInstruction();
|
||||
|
||||
cout << "first - " << firstInstruction->getNumber() << " last - " << lastInstruction->getNumber() << " after - " << instructionAfterLoop->getNumber() << endl;
|
||||
auto x = firstInstruction->getOperator();
|
||||
|
||||
auto tmpLoop = new LoopGraph();
|
||||
tmpLoop->isFor = true;
|
||||
tmpLoop->lineNum = firstInstruction->getOperator()->lineNumber();
|
||||
tmpLoop->lineNumAfterLoop = instructionAfterLoop->getOperator()->lineNumber();
|
||||
|
||||
if (firstInstruction->getOperator()->variant() == FOR_NODE) {
|
||||
SgForStmt* stmt = isSgForStmt(firstInstruction->getOperator());
|
||||
|
||||
cout << "for loop" << endl;// << stmt->sunparse() << endl;
|
||||
}
|
||||
else if (firstInstruction->getOperator()->variant() == WHILE_NODE) {
|
||||
SgWhileStmt* stmt = isSgWhileStmt(firstInstruction->getOperator());
|
||||
|
||||
cout << (stmt->conditional() == NULL ? "infinit" : "") << "while loop" << endl;//<< stmt->sunparse() << endl;
|
||||
}
|
||||
else if (firstInstruction->getOperator()->variant() == DO_WHILE_NODE) {
|
||||
SgWhileStmt* stmt = isSgDoWhileStmt(firstInstruction->getOperator());
|
||||
|
||||
cout << "do while loop" << endl;// << stmt->sunparse() << endl;
|
||||
}
|
||||
else if (firstInstruction->getOperator()->variant() == LOOP_NODE) {
|
||||
cout << "not known loop" << endl;// << firstInstruction->getOperator()->sunparse() << endl;
|
||||
}
|
||||
else {
|
||||
|
||||
cout << "goto loop" << endl;// firstInstruction->getOperator()->sunparse() << endl;
|
||||
}
|
||||
|
||||
cout << "loop start line " << tmpLoop->lineNum << endl;
|
||||
cout << "after loop line " << tmpLoop->lineNumAfterLoop << endl << endl;
|
||||
|
||||
loops.push_back(tmpLoop);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,6 +42,7 @@ namespace SAPFOR
|
||||
Argument() : number(lastNumArg++), type(CFG_ARG_TYPE::NONE), value(""), mType(CFG_MEM_TYPE::NONE_) { }
|
||||
Argument(CFG_ARG_TYPE type, CFG_MEM_TYPE mType) : number(lastNumArg++), type(type), mType(mType), value("") { }
|
||||
Argument(CFG_ARG_TYPE type, CFG_MEM_TYPE mType, const std::string& value) : number(lastNumArg++), type(type), mType(mType), value(value) { }
|
||||
Argument(CFG_ARG_TYPE type, CFG_MEM_TYPE mType, const std::string& value, int num) : number(num), type(type), mType(mType), value(value) { }
|
||||
Argument(CFG_ARG_TYPE type, const std::string& value) : number(lastNumArg++), type(type), mType(CFG_MEM_TYPE::NONE_), value(value)
|
||||
{
|
||||
if (type != CFG_ARG_TYPE::INSTR && type == CFG_ARG_TYPE::LAB &&
|
||||
@@ -50,6 +51,12 @@ namespace SAPFOR
|
||||
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
|
||||
}
|
||||
}
|
||||
Argument(const Argument& other)
|
||||
: number(other.number),
|
||||
type(other.type),
|
||||
mType(other.mType),
|
||||
value(other.value)
|
||||
{ }
|
||||
|
||||
void setType(CFG_ARG_TYPE newType) { type = newType; }
|
||||
CFG_ARG_TYPE getType() const { return type; }
|
||||
@@ -344,3 +351,5 @@ SAPFOR::Instruction* getInstructionByNumber(const std::vector<SAPFOR::IR_Block*>
|
||||
std::pair<SAPFOR::Instruction*, SAPFOR::BasicBlock*> getInstructionAndBlockByNumber(const std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>& CFGraph, int num);
|
||||
std::pair<SAPFOR::Instruction*, SAPFOR::BasicBlock*> getInstructionAndBlockByStatement(const std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>& CFGraph, SgStatement* stmt);
|
||||
int getParamIndex(SAPFOR::Argument* func_param, int max_index);
|
||||
|
||||
void exploreLoops(std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>* fullIR, const char* fileName);
|
||||
633
src/CFGraph/IRSSAForm.cpp
Normal file
633
src/CFGraph/IRSSAForm.cpp
Normal file
@@ -0,0 +1,633 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
|
||||
#include "../Utils/SgUtils.h"
|
||||
#include "../Utils/CommonBlock.h"
|
||||
#include "../GraphCall/graph_calls.h"
|
||||
#include "../ExpressionTransform/expr_transform.h"
|
||||
|
||||
#include "dvm.h"
|
||||
#include "IR.h"
|
||||
#include "CFGraph.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace SAPFOR;
|
||||
|
||||
typedef SAPFOR::BasicBlock BBlock;
|
||||
typedef SAPFOR::Argument BArgument;
|
||||
|
||||
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())
|
||||
{
|
||||
string resValue = "";
|
||||
string arg1Value = "";
|
||||
string arg2Value = "";
|
||||
if (i->getInstruction()->getResult() != nullptr && 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) {
|
||||
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) {
|
||||
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) {
|
||||
i->getInstruction()->getResult()->setValue(resValue);
|
||||
}
|
||||
if (i->getInstruction()->getArg1() != nullptr && 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) {
|
||||
i->getInstruction()->getArg2()->setValue(arg2Value);
|
||||
}
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static bool compareVectors(const vector<T>* vec1, const vector<T>* vec2) {
|
||||
if (vec1 == vec2) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!vec1 || !vec2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
vector<T> sortedVec1 = *vec1;
|
||||
vector<T> sortedVec2 = *vec2;
|
||||
|
||||
sort(sortedVec1.begin(), sortedVec1.end());
|
||||
sort(sortedVec2.begin(), sortedVec2.end());
|
||||
|
||||
return sortedVec1 == sortedVec2;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
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) {
|
||||
vector<T> tempCommon;
|
||||
|
||||
// Sort the current vector and common result for intersection
|
||||
vector<T> sortedVec = (*vectors)[i];
|
||||
sort(commonElements->begin(), commonElements->end());
|
||||
sort(sortedVec.begin(), sortedVec.end());
|
||||
|
||||
// Find the intersection
|
||||
set_intersection(
|
||||
commonElements->begin(), commonElements->end(),
|
||||
sortedVec.begin(), sortedVec.end(),
|
||||
back_inserter(tempCommon)
|
||||
);
|
||||
|
||||
// Update common result
|
||||
*commonElements = tempCommon;
|
||||
|
||||
// If no common elements left, break early
|
||||
if (commonElements->empty()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return commonElements;
|
||||
}
|
||||
|
||||
|
||||
static map<BBlock*, vector<BBlock*>> findDominators(vector<BBlock*> blocks) {
|
||||
map<BBlock*, vector<BBlock*>> result;
|
||||
|
||||
bool changed = true;
|
||||
while (changed) {
|
||||
changed = false;
|
||||
|
||||
for (auto currentBlock : blocks) {
|
||||
auto pred = currentBlock->getPrev();
|
||||
auto prevDominators = new vector<vector<BBlock*>>();
|
||||
|
||||
for (auto predBlock : pred)
|
||||
prevDominators->push_back(result.find(predBlock) != result.end() ? result[predBlock] : blocks);
|
||||
|
||||
auto currentBlockResult = getCommonElements(prevDominators);
|
||||
currentBlockResult->push_back(currentBlock);
|
||||
|
||||
if (result.find(currentBlock) == result.end() || !compareVectors(currentBlockResult, &result[currentBlock])) {
|
||||
result[currentBlock] = *currentBlockResult;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
(*res)[current->getNumber()] = *n;
|
||||
*n -= 1;
|
||||
}
|
||||
|
||||
static map<BBlock*, vector<BBlock*>> findDominatorBorders(vector<BBlock*>& blocks, map<BBlock*, BBlock*>& iDominators) {
|
||||
map<BBlock*, vector<BBlock*>> result;
|
||||
|
||||
for (auto block : blocks) {
|
||||
result[block] = *(new vector<BBlock*>());
|
||||
}
|
||||
|
||||
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]) {
|
||||
result[tmpBlock].push_back(block);
|
||||
tmpBlock = iDominators[tmpBlock];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
visited.push_back(currentBlock);
|
||||
|
||||
if (find(dominators[block].begin(), dominators[block].end(), currentBlock) != dominators[block].end()) {
|
||||
currentImmediateDominator = currentBlock;
|
||||
}
|
||||
|
||||
for (auto nextBlock : currentBlock->getNext()) {
|
||||
auto result = findImmediateDominatorsDfsHelper(block, nextBlock, currentImmediateDominator, visited, dominators);
|
||||
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static map<BBlock*, BBlock*> findImmediateDominators1(const map<BBlock*, vector<BBlock*>>& dominators, BBlock* entry) {
|
||||
map<BBlock*, BBlock*> iDom;
|
||||
|
||||
for (const auto& pair : dominators) {
|
||||
BBlock* b = pair.first;
|
||||
|
||||
if (b == entry) continue;
|
||||
|
||||
const auto& doms = pair.second;
|
||||
|
||||
BBlock* candidate = nullptr;
|
||||
for (auto d : doms) {
|
||||
if (d == b) continue;
|
||||
bool isImmediate = true;
|
||||
|
||||
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()) {
|
||||
isImmediate = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (isImmediate) {
|
||||
candidate = d;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (candidate) {
|
||||
iDom[b] = candidate;
|
||||
}
|
||||
}
|
||||
|
||||
return iDom;
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
vector<BBlock*> visited;
|
||||
|
||||
if (block == fistBlock) {
|
||||
continue;
|
||||
}
|
||||
|
||||
iDominators[block] = findImmediateDominatorsDfsHelper(block, fistBlock, fistBlock, visited, dominators);
|
||||
}
|
||||
|
||||
return iDominators;
|
||||
}
|
||||
|
||||
static vector<BArgument*> getDefForBlock(const BBlock& block) {
|
||||
vector<BArgument*> def;
|
||||
const auto& instructions = block.getInstructions();
|
||||
|
||||
for (const auto& irBlock : instructions) {
|
||||
if (irBlock) {
|
||||
Instruction* instr = irBlock->getInstruction();
|
||||
|
||||
if (instr) {
|
||||
BArgument* result = instr->getResult();
|
||||
if (result) {
|
||||
def.push_back(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return def;
|
||||
}
|
||||
|
||||
static pair<set<BArgument*>, map<BArgument*, set<BBlock*>>> getGlobalsAndVarBlocks(vector<BBlock*> blocks) {
|
||||
set<BArgument*> globals;
|
||||
map<BArgument*, set<BBlock*>> varBlocks;
|
||||
|
||||
for (auto block : blocks) {
|
||||
set<BArgument*> def;
|
||||
const auto& instructions = block->getInstructions();
|
||||
|
||||
for (const auto& irBlock : instructions) {
|
||||
if (irBlock) {
|
||||
Instruction* instr = irBlock->getInstruction();
|
||||
|
||||
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()) {
|
||||
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) {
|
||||
def.insert(res);
|
||||
varBlocks[res].insert(block);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return make_pair(globals, varBlocks);
|
||||
}
|
||||
|
||||
static void getBlocksWithFiFunctions(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) {
|
||||
auto worklist = varBlocks[var];
|
||||
set<BBlock*> hasFiFunction;
|
||||
|
||||
while (!worklist.empty()) {
|
||||
auto block = *worklist.begin();
|
||||
worklist.erase(block);
|
||||
|
||||
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);
|
||||
dfBlock->addInstructionInFront(phiBlock);
|
||||
|
||||
//blocksWithFiFunctions.push_back(dfBlock);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//return blocksWithFiFunctions;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
string ToString(CFG_ARG_TYPE type) {
|
||||
switch (type) {
|
||||
case CFG_ARG_TYPE::NONE: return "NONE";
|
||||
case CFG_ARG_TYPE::REG: return "REG";
|
||||
case CFG_ARG_TYPE::VAR: return "VAR";
|
||||
case CFG_ARG_TYPE::ARRAY: return "ARRAY";
|
||||
case CFG_ARG_TYPE::CONST: return "CONST";
|
||||
case CFG_ARG_TYPE::FUNC: return "FUNC";
|
||||
case CFG_ARG_TYPE::LAB: return "LAB";
|
||||
case CFG_ARG_TYPE::INSTR: return "INSTR";
|
||||
case CFG_ARG_TYPE::CONST_STR: return "CONST_STR";
|
||||
case CFG_ARG_TYPE::RECORD: return "RECORD";
|
||||
case CFG_ARG_TYPE::CONSTR_REF: return "CONSTR_REF";
|
||||
default: return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
void restoreConnections(const vector<BBlock*>& originalBlocks, vector<BBlock*>& copiedBlocks) {
|
||||
// Создаем отображение оригинальных блоков в их копии
|
||||
map<BBlock*, BBlock*> blockMapping;
|
||||
for (size_t i = 0; i < originalBlocks.size(); ++i) {
|
||||
blockMapping[originalBlocks[i]] = copiedBlocks[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) {
|
||||
copiedBlock->removePrev(j);
|
||||
}
|
||||
|
||||
// Копируем, затем удаляем next связи
|
||||
auto nextCopy = copiedBlock->getNext();
|
||||
for (auto j : nextCopy) {
|
||||
copiedBlock->removeNext(j);
|
||||
}
|
||||
|
||||
// Восстанавливаем связи succ (следующих блоков)
|
||||
for (auto* succ : originalBlock->getNext()) {
|
||||
copiedBlock->addNext(blockMapping[succ]);
|
||||
}
|
||||
|
||||
// Восстанавливаем связи prev (предыдущих блоков)
|
||||
for (auto* prev : originalBlock->getPrev()) {
|
||||
copiedBlock->addPrev(blockMapping[prev]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
BArgument* NewName(BArgument* var, map<string, int>& counter, map<string, stack<BArgument*>>& stack, int number) {
|
||||
//int index = counter[var->getValue()];
|
||||
counter[var->getValue()]++;
|
||||
|
||||
BArgument* newName = new BArgument(var->getType(), var->getMemType(), var->getValue(), number);
|
||||
stack[var->getValue()].push(newName);
|
||||
return newName;
|
||||
}
|
||||
|
||||
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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
instruction->setArg1(stack[instruction->getArg1()->getValue()].top());
|
||||
}
|
||||
|
||||
if (instruction->getArg2() != nullptr && 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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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++) {
|
||||
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) {
|
||||
|
||||
Instruction* paramInstruction;
|
||||
|
||||
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 {
|
||||
BArgument* tmp = new BArgument(CFG_ARG_TYPE::CONST, CFG_MEM_TYPE::COMMON_, "-1");
|
||||
paramInstruction = new Instruction(CFG_OP::PARAM, tmp);
|
||||
}
|
||||
|
||||
paramInstruction->setOperator(block->getInstructions()[0]->getInstruction()->getOperator());
|
||||
block->addInstructionBeforeInstruction(new IR_Block(paramInstruction), instruction);
|
||||
|
||||
instruction->getArg2()->setValue(to_string(stoi(instruction->getArg2()->getValue()) + 1));
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vector<BBlock*> findBlocksWithValue(map<BBlock*, BBlock*>& iDominators, BBlock* x) {
|
||||
vector<BBlock*> result;
|
||||
|
||||
// Проходим по всем элементам map
|
||||
for (auto& pair : iDominators) {
|
||||
// Если значение равно x, добавляем ключ в результат
|
||||
if (pair.second == x) {
|
||||
result.push_back(pair.first);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void RenameIR(BBlock* block, map<BBlock*, BBlock*>& iDominators, map<string, int>& counter, map<string, stack<BArgument*>>& stack) {
|
||||
|
||||
RenameFiFunctionResultVar(block, counter, stack);
|
||||
|
||||
RenameInstructionVars(block, counter, stack);
|
||||
|
||||
for (auto* successor : block->getNext()) {
|
||||
RenameFiFunctionArgsVar(successor, stack);
|
||||
}
|
||||
|
||||
for (auto* child : findBlocksWithValue(iDominators, block)) {
|
||||
RenameIR(child, iDominators, counter, stack);
|
||||
}
|
||||
|
||||
for (auto& irBlock : block->getInstructions()) {
|
||||
auto instruction = irBlock->getInstruction();
|
||||
if (instruction->getResult() != nullptr && instruction->getResult()->getType() == CFG_ARG_TYPE::VAR) {
|
||||
string varName = instruction->getResult()->getValue();
|
||||
stack[varName].pop();
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
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;
|
||||
|
||||
|
||||
|
||||
vector<BBlock*> funcIR;
|
||||
|
||||
for (auto i : funcIRConst) {
|
||||
funcIR.push_back(new BBlock(*i));
|
||||
}
|
||||
restoreConnections(funcIRConst, funcIR);
|
||||
|
||||
for (auto i : funcIR) {
|
||||
//printBlock(i);
|
||||
}
|
||||
auto dominators = findDominators(funcIR);
|
||||
|
||||
/*cout << endl << endl << endl << "DOMINATORS" << endl << endl << endl;
|
||||
|
||||
for (auto i : dominators) {
|
||||
cout << "block - " << i.first->getNumber() << endl;
|
||||
for (auto j : i.second) {
|
||||
cout << "dominators - " << j->getNumber() << endl;
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
}*/
|
||||
|
||||
auto iDominators = findImmediateDominators1(dominators, funcIR[0]);
|
||||
|
||||
/*for (auto i : iDominators) {
|
||||
cout << "block - " << i.first->getNumber() << endl;
|
||||
cout << "Idominators - " << i.second->getNumber() << endl;
|
||||
cout << endl;
|
||||
}
|
||||
*/
|
||||
auto dominatorBorders = findDominatorBorders(funcIR, iDominators);
|
||||
|
||||
|
||||
/*for (auto i : dominatorBorders) {
|
||||
cout << "block - " << i.first->getNumber() << endl;
|
||||
for (auto j : i.second) {
|
||||
cout << "border - " << j->getNumber() << endl;
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
}*/
|
||||
|
||||
|
||||
auto globalsAndVarBlocks = getGlobalsAndVarBlocks(funcIR);
|
||||
auto globals = globalsAndVarBlocks.first;
|
||||
auto varBlocks = globalsAndVarBlocks.second;
|
||||
|
||||
/*for (auto i : globals) {
|
||||
cout << i->getValue() << " " << ToString(i->getType()) << " " << i->getNumber() << endl;
|
||||
|
||||
}
|
||||
cout << endl;
|
||||
for (auto i : varBlocks) {
|
||||
cout << i.first->getValue() << " - ";
|
||||
for (auto j : i.second) {
|
||||
cout << j->getNumber() << ", ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
cout << endl;
|
||||
*/
|
||||
|
||||
getBlocksWithFiFunctions(funcIR, globals, varBlocks, dominatorBorders);
|
||||
|
||||
map<string, int> count;
|
||||
map<string, stack<BArgument*>> varStack;
|
||||
|
||||
for (auto var : globals) {
|
||||
count[var->getValue()] = 0;
|
||||
|
||||
stack<BArgument*> tmp;
|
||||
BArgument* tmpArgument = new BArgument(CFG_ARG_TYPE::CONST, CFG_MEM_TYPE::COMMON_, "-1");
|
||||
tmp.push(tmpArgument);
|
||||
varStack[var->getValue()] = tmp;
|
||||
}
|
||||
|
||||
RenameIR(funcIR[0], iDominators, count, varStack);
|
||||
|
||||
for (auto i : funcIR) {
|
||||
//printBlock(i);
|
||||
}
|
||||
//cout << endl << endl << endl << endl << endl;
|
||||
//for (auto i : funcIRConst) {
|
||||
// printBlock(i);
|
||||
//}
|
||||
|
||||
(*result)[funcinfo] = funcIR;
|
||||
}
|
||||
}
|
||||
6
src/CFGraph/IRSSAForm.h
Normal file
6
src/CFGraph/IRSSAForm.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "CFGraph.h"
|
||||
#include "IR.h"
|
||||
|
||||
void buildIRSSAForm(std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>> fullIR, std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>* result);
|
||||
@@ -95,6 +95,7 @@
|
||||
#include "CFGraph/IR.h"
|
||||
#include "CFGraph/RD_subst.h"
|
||||
#include "CFGraph/CFGraph.h"
|
||||
#include "CFGraph/IRSSAForm.h"
|
||||
|
||||
#include "CFGraph/live_variable_analysis.h"
|
||||
#include "CFGraph/private_variables_analysis.h"
|
||||
@@ -1029,6 +1030,13 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne
|
||||
if(func->funcPointer->variant() != ENTRY_STAT)
|
||||
countOfTransform += removeDeadCode(func->funcPointer, allFuncInfo, commonBlocks);
|
||||
}
|
||||
else if (curr_regime == BUILD_IR_SSA_FORM)
|
||||
{
|
||||
if (ssaFormIR.size() == 0)
|
||||
buildIRSSAForm(fullIR, &ssaFormIR);
|
||||
}
|
||||
else if (curr_regime == EXPLORE_IR_LOOPS)
|
||||
exploreLoops(&ssaFormIR, file_name);
|
||||
else if (curr_regime == FIND_PRIVATE_ARRAYS)
|
||||
FindPrivateArrays(loopGraph, fullIR);
|
||||
else if (curr_regime == TEST_PASS)
|
||||
|
||||
@@ -182,6 +182,8 @@ enum passes {
|
||||
|
||||
SET_IMPLICIT_NONE,
|
||||
RENAME_INLCUDES,
|
||||
EXPLORE_IR_LOOPS,
|
||||
BUILD_IR_SSA_FORM,
|
||||
|
||||
FIND_PRIVATE_ARRAYS,
|
||||
|
||||
@@ -369,6 +371,8 @@ static void setPassValues()
|
||||
passNames[SET_IMPLICIT_NONE] = "SET_IMPLICIT_NONE";
|
||||
passNames[RENAME_INLCUDES] = "RENAME_INLCUDES";
|
||||
passNames[INSERT_NO_DISTR_FLAGS_FROM_GUI] = "INSERT_NO_DISTR_FLAGS_FROM_GUI";
|
||||
passNames[EXPLORE_IR_LOOPS] = "EXPLORE_IR_LOOPS";
|
||||
passNames[BUILD_IR_SSA_FORM] = "BUILD_IR_SSA_FORM";
|
||||
passNames[FIND_PRIVATE_ARRAYS] = "FIND_PRIVATE_ARRAYS";
|
||||
|
||||
passNames[TEST_PASS] = "TEST_PASS";
|
||||
|
||||
@@ -180,3 +180,9 @@ bool passNamesWasInit = false;
|
||||
|
||||
std::map<PTR_BFND, std::pair<std::string, int>> sgStats;
|
||||
std::map<PTR_LLND, std::pair<std::string, int>> sgExprs;
|
||||
|
||||
//for EXPLORE_IR_LOOPS and BUILD_IR_SSA_FORM
|
||||
map<FuncInfo*, vector<SAPFOR::BasicBlock*>> ssaFormIR;
|
||||
//
|
||||
|
||||
int test = 0;
|
||||
@@ -316,6 +316,8 @@ void InitPassesDependencies(map<passes, vector<passes>> &passDepsIn, set<passes>
|
||||
|
||||
list({ VERIFY_INCLUDES, CORRECT_VAR_DECL }) <= Pass(SET_IMPLICIT_NONE);
|
||||
|
||||
list({ CALL_GRAPH, LOOP_GRAPH, CALL_GRAPH2 }) <= Pass(BUILD_IR_SSA_FORM) <= Pass(EXPLORE_IR_LOOPS);
|
||||
|
||||
list({ CALL_GRAPH2, CALL_GRAPH, BUILD_IR, LOOP_GRAPH, LOOP_ANALYZER_DATA_DIST_S2 }) <= Pass(FIND_PRIVATE_ARRAYS);
|
||||
|
||||
passesIgnoreStateDone.insert({ CREATE_PARALLEL_DIRS, INSERT_PARALLEL_DIRS, INSERT_SHADOW_DIRS, EXTRACT_PARALLEL_DIRS,
|
||||
|
||||
Reference in New Issue
Block a user