finish inductive variables

This commit is contained in:
2025-04-22 16:14:29 +03:00
parent cd209a587a
commit 6a84171382
9 changed files with 431 additions and 144 deletions

View File

@@ -198,18 +198,19 @@ set(TRANSFORMS
${TR_IMPLICIT_NONE}
${TR_REPLACE_ARRAYS_IN_IO})
set(CFG _src/CFGraph/IR.cpp
_src/CFGraph/IR.h
_src/CFGraph/CFGraph.cpp
_src/CFGraph/CFGraph.h
_src/CFGraph/RD_subst.cpp
_src/CFGraph/RD_subst.h
_src/CFGraph/live_variable_analysis.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(CFG
src/CFGraph/IR.cpp
src/CFGraph/IR.h
src/CFGraph/CFGraph.cpp
src/CFGraph/CFGraph.h
src/CFGraph/RD_subst.cpp
src/CFGraph/RD_subst.h
src/CFGraph/live_variable_analysis.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

View File

@@ -66,7 +66,7 @@ void BBlock::addInstructionBeforeInstruction(IR_Block* item, Instruction* instru
{
for (auto it = instructions.begin(); it != instructions.end(); ++it) {
if ((*it)->getInstruction() == instruction) {
instructions.insert(instructions.begin(), item);
instructions.insert(it, item);
item->setBasicBlock(this);
return;
}

View File

@@ -1614,25 +1614,29 @@ vector<IR_Block*> buildIR(SgStatement* function, const FuncInfo* func, const vec
return blocks;
}
void dfs(SAPFOR::BasicBlock* block, vector<int>& visit, vector<pair<SAPFOR::BasicBlock*, SAPFOR::BasicBlock*>>& startAndEnd, SAPFOR::BasicBlock* prev) {
if (visit[block->getNumber()] == 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) {
if (!block) return;
if (visit[block->getNumber()] == VISITED) {
cout << "error";
return;
}
if (visit[block->getNumber()] == 1) {
visit[block->getNumber()] = 2;
if (visit[block->getNumber()] == VISITING) {
visit[block->getNumber()] = VISITED;
startAndEnd.push_back(make_pair(prev, block));
return;
}
visit[block->getNumber()] = 1;
visit[block->getNumber()] = VISITING;
for (auto i : block->getNext()) {
dfs(i, visit, startAndEnd, block);
}
}
void printBlock(SAPFOR::BasicBlock* block) {
static void printBlock(SAPFOR::BasicBlock* block) {
cout << "block - " << block->getNumber() << endl;
cout << "next -";
for (auto i : block->getNext())
@@ -1648,105 +1652,383 @@ void printBlock(SAPFOR::BasicBlock* block) {
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 testIR(map<FuncInfo*, vector<SAPFOR::BasicBlock*>> fullIR) {
for (auto& i : fullIR)
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; // не нашли
}
void exploreLoops(map<FuncInfo*, vector<SAPFOR::BasicBlock*>>* fullIR) {
for (auto& i : *fullIR)
{
for (auto j : i.second)
printBlock(j);
//for (auto j : i.second)
// printBlock(j);
vector<IR_Block*> all;
for (auto j : i.second)
for (auto k : j->getInstructions())
all.push_back(k);
map<int, int> visited;
for (auto i : i.second)
visited[i->getNumber()] = UNVISITED;
vector<int> visited(i.second.size(), 0);
//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 j : startAndEnd)
{
auto firstInstruction = j.second->getInstructions()[0]->getInstruction();
auto lastInstruction = j.first->getInstructions().back()->getInstruction();
Instruction* instructionAfterLoop;
for (auto a : fullIR)
for(auto b : a.second)
for (auto c : b->getInstructions())
if (c->getInstruction()->getNumber() == lastInstruction->getNumber() + 1)
{
instructionAfterLoop = c->getInstruction();
break;
for (auto& [tail, header] : startAndEnd) {
set<SAPFOR::BasicBlock*> loopExits;
for (auto succ : tail->getNext()) {
if (succ != header) {
loopExits.insert(succ);
}
}
//auto instructionAfterLoop = getInstructionByNumber(all, lastInstruction->getNumber() + 1);
cout << "first - " << firstInstruction->getNumber() << " last - " << lastInstruction->getNumber() << " after - " << instructionAfterLoop->getNumber() << endl;
vector<SAPFOR::BasicBlock*> loopBody;
getLoopBody(header, loopExits, loopBody);
if (firstInstruction->getOperator()->variant() == FOR_NODE) {
SgForStmt* stmt = isSgForStmt(firstInstruction->getOperator());
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();
cout << "for" << endl << stmt->sunparse() << endl;
cout << "loop start line " << tmpLoop->lineNum << endl;
cout << "after loop line " << tmpLoop->lineNumAfterLoop << endl << endl;
loops.push_back(tmpLoop);
} else if (firstInstruction->getOperator()->variant() == WHILE_NODE) {
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());
auto tmpLoop = new LoopGraph();
tmpLoop->lineNum = firstInstruction->getOperator()->lineNumber();
tmpLoop->lineNumAfterLoop = instructionAfterLoop->getOperator()->lineNumber();
if (stmt->conditional() == NULL)
{
//infinit loop
cout << "infinit loop " << endl << stmt->sunparse() << endl;
cout << (stmt->conditional() == NULL ? "infinit" : "") << "while loop" << endl << stmt->sunparse() << endl;
}
else
{
//while
cout << "while " << endl << stmt->sunparse();
else if (firstInstruction->getOperator()->variant() == DO_WHILE_NODE) {
SgWhileStmt* stmt = isSgDoWhileStmt(firstInstruction->getOperator());
cout << "do while loop" << endl << stmt->sunparse() << endl;
}
cout << "loop start line " << tmpLoop->lineNum << endl;
cout << "after loop line " << tmpLoop->lineNumAfterLoop << endl << endl;
loops.push_back(tmpLoop);
} else if (firstInstruction->getOperator()->variant() == LOOP_NODE) {
else if (firstInstruction->getOperator()->variant() == LOOP_NODE) {
cout << "not known loop" << endl << firstInstruction->getOperator()->sunparse() << endl;
}
else {
cout << "goto loop - " << firstInstruction->getOperator()->sunparse() << endl;
}
cout << "goto loop" << firstInstruction->getOperator()->sunparse() << endl;
}
cout << "loop start line " << tmpLoop->lineNum << endl;
cout << "after loop line " << tmpLoop->lineNumAfterLoop << endl << endl;
/*for (auto j : i.second) {
cout << j->getNumber() << endl << "in" << endl;
for (auto k : j->getRD_In()) {
cout << k.first->getMemTypeStr() << " - ";
for (auto h : k.second) {
cout << h << " ";
loops.push_back(tmpLoop);
}
cout << endl;
}
cout << "out" << endl;
for (auto k : j->getRD_Out()) {
cout << k.first->getMemTypeStr() << " - ";
for (auto h : k.second) {
cout << h << " ";
}
cout << endl;
}*/
}
}

View File

@@ -51,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; }
@@ -346,4 +352,4 @@ std::pair<SAPFOR::Instruction*, SAPFOR::BasicBlock*> getInstructionAndBlockByNum
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 testIR(std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>> fullIR);
void exploreLoops(std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>* fullIR);

View File

@@ -50,16 +50,7 @@ static void printBlock(BBlock* block) {
i->getInstruction()->getArg2()->setValue(i->getInstruction()->getArg2()->getValue() + to_string(i->getInstruction()->getArg2()->getNumber()));
}
cout << i->getNumber() << " " << i->getInstruction()->dump() << endl;// << (i->getInstruction()->getResult() != nullptr ? " 1 " : " 0 ") << (i->getInstruction()->getArg1() != nullptr ? "1 " : "0 ") << (i->getInstruction()->getArg2() != nullptr ? "1 " : "0 ") << endl;
/*if (i->getInstruction()->getResult() != nullptr) {
cout << "num - " << i->getInstruction()->getResult()->getNumber() << endl;
})*/
/*if (i->getInstruction()->getOperation() == CFG_OP::F_CALL) {
cout << endl;
cout << "arg1 - " << (i->getInstruction()->getArg1()->getType() == CFG_ARG_TYPE::FUNC ? "1" : "0") << "arg2 - " << (i->getInstruction()->getArg2()->getType() == CFG_ARG_TYPE::CONST ? "1" : "0") << "res - " << i->getInstruction()->getResult() << endl;
}*/
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);
@@ -74,6 +65,7 @@ static void printBlock(BBlock* block) {
cout << endl;
}
template <typename T>
static bool compareVectors(const vector<T>* vec1, const vector<T>* vec2) {
if (vec1 == vec2) {
@@ -301,7 +293,7 @@ static pair<set<BArgument*>, map<BArgument*, set<BBlock*>>> getGlobalsAndVarBloc
return make_pair(globals, varBlocks);
}
static vector<BBlock*> getBlocksWithFiFunctions(vector<BBlock*> blocks, set<BArgument*> globals, map<BArgument*, set<BBlock*>> varBlocks, map<BBlock*, vector<BBlock*>> dominatorBorders) {
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");
@@ -318,20 +310,18 @@ static vector<BBlock*> getBlocksWithFiFunctions(vector<BBlock*> blocks, set<BArg
if (hasFiFunction.find(dfBlock) == hasFiFunction.end()) {
hasFiFunction.insert(dfBlock);
Instruction* phiInstruction = new Instruction(CFG_OP::F_CALL, fiFunc, paramCount, var);
//cout << "insert fi functio9n in " << dfBlock->getNumber() << " with var " << var->getValue() << endl;
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);
//blocksWithFiFunctions.push_back(dfBlock);
}
}
}
}
return blocksWithFiFunctions;
//return blocksWithFiFunctions;
}
@@ -386,11 +376,11 @@ void restoreConnections(const vector<BBlock*>& originalBlocks, vector<BBlock*>&
}
BArgument* NewName(BArgument* var, map<string, int>& counter, map<string, stack<BArgument*>>& stack) {
int index = counter[var->getValue()];
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(), index + 1);
BArgument* newName = new BArgument(var->getType(), var->getMemType(), var->getValue(), number);
stack[var->getValue()].push(newName);
return newName;
}
@@ -399,7 +389,7 @@ void RenameFiFunctionResultVar(BBlock* block, map<string, int>& counter, map<str
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->setResult(NewName(instruction->getResult(), counter, stack, instruction->getNumber()));
}
}
}
@@ -417,23 +407,34 @@ void RenameInstructionVars(BBlock* block, map<string, int>& counter, map<string,
}
if (instruction->getResult() != nullptr && instruction->getResult()->getType() == CFG_ARG_TYPE::VAR) {
instruction->setResult(NewName(instruction->getResult(), counter, stack));
instruction->setResult(NewName(instruction->getResult(), counter, stack, instruction->getNumber()));
}
}
}
void RenameFiFunctionArgsVar(BBlock* block, map<string, stack<BArgument*>>& stack) {
cout << "test" << endl;
auto size = block->getInstructions().size();
auto& instructions = block->getInstructions();
for (auto i = 0; i < size; i++) {
auto irBlock = block->getInstructions()[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) {
//cout << "test" << endl;
if (instruction->getOperation() == CFG_OP::F_CALL && instruction->getArg1() != nullptr && instruction->getArg1()->getValue() == "FI_FUNCTION" && instruction->getResult() != nullptr && instruction->getArg2() != nullptr) {
auto paramInstruction = new Instruction(CFG_OP::PARAM, stack[instruction->getResult()->getValue()].top());
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++;
}
}
@@ -464,7 +465,6 @@ void RenameIR(BBlock* block, map<BBlock*, BBlock*>& iDominators, map<string, int
}
for (auto* child : findBlocksWithValue(iDominators, block)) {
cout << "sec" << endl;
RenameIR(child, iDominators, counter, stack);
}
@@ -485,16 +485,13 @@ void RenameIR(BBlock* block, map<BBlock*, BBlock*>& iDominators, map<string, int
}
}
map<FuncInfo*, vector<BBlock*>> buildIRSSAForm(map<FuncInfo*, vector<BBlock*>> fullIR) {
map<FuncInfo*, vector<BBlock*>> result;
void buildIRSSAForm(map<FuncInfo*, vector<BBlock*>> fullIR, map<FuncInfo*, vector<BBlock*>>* result) {
for (auto item : fullIR) {
auto funcinfo = item.first;
auto funcIRConst = item.second;
vector<BBlock*> funcIR;
vector<BBlock*> funcResult;
for (auto i : funcIRConst) {
funcIR.push_back(new BBlock(*i));
@@ -558,7 +555,7 @@ map<FuncInfo*, vector<BBlock*>> buildIRSSAForm(map<FuncInfo*, vector<BBlock*>> f
cout << endl;
*/
funcResult = getBlocksWithFiFunctions(funcIR, globals, varBlocks, dominatorBorders);
getBlocksWithFiFunctions(funcIR, globals, varBlocks, dominatorBorders);
map<string, int> count;
map<string, stack<BArgument*>> varStack;
@@ -572,17 +569,14 @@ map<FuncInfo*, vector<BBlock*>> buildIRSSAForm(map<FuncInfo*, vector<BBlock*>> f
RenameIR(funcIR[0], iDominators, count, varStack);
for (auto i : funcIR) {
printBlock(i);
}
cout << endl << endl << endl << endl << endl;
for (auto i : funcIRConst) {
printBlock(i);
}
//for (auto i : funcIR) {
// printBlock(i);
//}
//cout << endl << endl << endl << endl << endl;
//for (auto i : funcIRConst) {
// printBlock(i);
//}
result[funcinfo] = funcResult;
(*result)[funcinfo] = funcIR;
}
return result;
}

View File

@@ -3,4 +3,4 @@
#include "CFGraph.h"
#include "IR.h"
std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>> buildIRSSAForm(std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>> fullIR);
void buildIRSSAForm(std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>> fullIR, std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>* result);

View File

@@ -1019,10 +1019,10 @@ 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 == EXPLORE_IR_LOOPS)
testIR(fullIR);
else if (curr_regime == BUILD_IR_SSA_FORM)
buildIRSSAForm(fullIR);
buildIRSSAForm(fullIR, &ssaFormIR);
else if (curr_regime == EXPLORE_IR_LOOPS)
exploreLoops(&ssaFormIR);
else if (curr_regime == TEST_PASS)
{
//test pass

View File

@@ -180,3 +180,7 @@ 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;
//

View File

@@ -314,8 +314,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(EXPLORE_IR_LOOPS);
list({ CALL_GRAPH, LOOP_GRAPH, CALL_GRAPH2 }) <= Pass(BUILD_IR_SSA_FORM);
Pass(BUILD_IR_SSA_FORM) <= Pass(EXPLORE_IR_LOOPS);
passesIgnoreStateDone.insert({ CREATE_PARALLEL_DIRS, INSERT_PARALLEL_DIRS, INSERT_SHADOW_DIRS, EXTRACT_PARALLEL_DIRS,
EXTRACT_SHADOW_DIRS, CREATE_REMOTES, UNPARSE_FILE, REMOVE_AND_CALC_SHADOW,