fixed code style

This commit is contained in:
ALEXks
2025-05-30 18:01:58 +03:00
parent 2969f92013
commit 7eee10bdad
5 changed files with 335 additions and 302 deletions

View File

@@ -21,38 +21,37 @@ using std::to_string;
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;
static 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) {
if (visit[block->getNumber()] == VISITED)
{
cout << "error";
return;
}
if (visit[block->getNumber()] == VISITING) {
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()) {
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())
@@ -60,53 +59,59 @@ static void printBlock(SAPFOR::BasicBlock* block) {
string resValue = "";
string arg1Value = "";
string arg2Value = "";
if (i->getInstruction()->getResult() != nullptr && i->getInstruction()->getResult()->getType() == SAPFOR::CFG_ARG_TYPE::VAR) {
if (i->getInstruction()->getResult() != NULL && i->getInstruction()->getResult()->getType() == SAPFOR::CFG_ARG_TYPE::VAR) {
resValue = i->getInstruction()->getResult()->getValue();
i->getInstruction()->getResult()->setValue(i->getInstruction()->getResult()->getValue() + to_string(i->getInstruction()->getResult()->getNumber()));
}
if (i->getInstruction()->getArg1() != nullptr && i->getInstruction()->getArg1()->getType() == SAPFOR::CFG_ARG_TYPE::VAR) {
if (i->getInstruction()->getArg1() != NULL && i->getInstruction()->getArg1()->getType() == SAPFOR::CFG_ARG_TYPE::VAR) {
arg1Value = i->getInstruction()->getArg1()->getValue();
i->getInstruction()->getArg1()->setValue(i->getInstruction()->getArg1()->getValue() + to_string(i->getInstruction()->getArg1()->getNumber()));
}
if (i->getInstruction()->getArg2() != nullptr && i->getInstruction()->getArg2()->getType() == SAPFOR::CFG_ARG_TYPE::VAR) {
if (i->getInstruction()->getArg2() != NULL && i->getInstruction()->getArg2()->getType() == SAPFOR::CFG_ARG_TYPE::VAR) {
arg2Value = i->getInstruction()->getArg2()->getValue();
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() == SAPFOR::CFG_ARG_TYPE::VAR) {
i->getInstruction()->getResult()->setValue(resValue);
}
if (i->getInstruction()->getArg1() != nullptr && i->getInstruction()->getArg1()->getType() == SAPFOR::CFG_ARG_TYPE::VAR) {
if (i->getInstruction()->getResult() != NULL && i->getInstruction()->getResult()->getType() == SAPFOR::CFG_ARG_TYPE::VAR)
i->getInstruction()->getResult()->setValue(resValue);
if (i->getInstruction()->getArg1() != NULL && i->getInstruction()->getArg1()->getType() == SAPFOR::CFG_ARG_TYPE::VAR)
i->getInstruction()->getArg1()->setValue(arg1Value);
}
if (i->getInstruction()->getArg2() != nullptr && i->getInstruction()->getArg2()->getType() == SAPFOR::CFG_ARG_TYPE::VAR) {
if (i->getInstruction()->getArg2() != NULL && i->getInstruction()->getArg2()->getType() == SAPFOR::CFG_ARG_TYPE::VAR)
i->getInstruction()->getArg2()->setValue(arg2Value);
}
}
cout << endl;
}
void getLoopBody(SAPFOR::BasicBlock* loopHeader, const set<SAPFOR::BasicBlock*>& loopExits, std::vector<SAPFOR::BasicBlock*>& loopBody) {
static void getLoopBody(SAPFOR::BasicBlock* loopHeader, const set<SAPFOR::BasicBlock*>& loopExits, std::vector<SAPFOR::BasicBlock*>& loopBody)
{
set<SAPFOR::BasicBlock*> visited;
std::stack<SAPFOR::BasicBlock*> stack;
stack.push(loopHeader);
while (!stack.empty()) {
while (!stack.empty())
{
auto block = stack.top();
stack.pop();
if (visited.count(block)) continue;
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);
}
for (auto succ : block->getNext())
{
if (loopExits.count(succ))
continue;
if (!visited.count(succ))
stack.push(succ);
}
}
@@ -114,29 +119,27 @@ void getLoopBody(SAPFOR::BasicBlock* loopHeader, const set<SAPFOR::BasicBlock*>&
std::stack<SAPFOR::BasicBlock*> reverseStack;
reverseStack.push(loopHeader);
while (!reverseStack.empty()) {
while (!reverseStack.empty())
{
auto block = reverseStack.top();
reverseStack.pop();
if (backReachable.count(block)) continue;
if (backReachable.count(block))
continue;
backReachable.insert(block);
for (auto pred : block->getPrev()) {
if (visited.count(pred) && !backReachable.count(pred)) {
for (auto pred : block->getPrev())
if (visited.count(pred) && !backReachable.count(pred))
reverseStack.push(pred);
}
}
}
for (auto block : visited) {
if (backReachable.count(block)) {
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)
static set<SAPFOR::Argument*> findRegisterSourceVariables(const std::vector<SAPFOR::BasicBlock*>& blocks, SAPFOR::Argument* var)
{
set<SAPFOR::Argument*> result;
set<SAPFOR::Argument*> visited;
@@ -159,7 +162,8 @@ set<SAPFOR::Argument*> findRegisterSourceVariables(const std::vector<SAPFOR::Bas
op == SAPFOR::CFG_OP::NOT || op == SAPFOR::CFG_OP::ASSIGN;
};
while (!workStack.empty()) {
while (!workStack.empty())
{
auto variable = workStack.top();
workStack.pop();
if (!variable || visited.count(variable))
@@ -167,8 +171,10 @@ set<SAPFOR::Argument*> findRegisterSourceVariables(const std::vector<SAPFOR::Bas
visited.insert(variable);
for (auto block : blocks) {
for (auto instrWrapper : block->getInstructions()) {
for (auto block : blocks)
{
for (auto instrWrapper : block->getInstructions())
{
auto instr = instrWrapper->getInstruction();
if (!instr || instr->getResult() != variable)
continue;
@@ -177,7 +183,8 @@ set<SAPFOR::Argument*> findRegisterSourceVariables(const std::vector<SAPFOR::Bas
auto arg1 = instr->getArg1();
auto arg2 = instr->getArg2();
if (isBinaryOp(op) && arg1 && arg2) {
if (isBinaryOp(op) && arg1 && arg2)
{
if (arg1->getType() == SAPFOR::CFG_ARG_TYPE::VAR)
result.insert(arg1);
else if (arg1->getType() == SAPFOR::CFG_ARG_TYPE::REG)
@@ -188,7 +195,8 @@ set<SAPFOR::Argument*> findRegisterSourceVariables(const std::vector<SAPFOR::Bas
else if (arg2->getType() == SAPFOR::CFG_ARG_TYPE::REG)
workStack.push(arg2);
}
else if (isUnaryOp(op) && arg1) {
else if (isUnaryOp(op) && arg1)
{
if (arg1->getType() == SAPFOR::CFG_ARG_TYPE::VAR)
result.insert(arg1);
else if (arg1->getType() == SAPFOR::CFG_ARG_TYPE::REG)
@@ -201,7 +209,8 @@ set<SAPFOR::Argument*> findRegisterSourceVariables(const std::vector<SAPFOR::Bas
return result;
}
std::vector<SAPFOR::Instruction*> getPhiArguments(SAPFOR::BasicBlock* block, SAPFOR::Instruction* phiInstr) {
static std::vector<SAPFOR::Instruction*> getPhiArguments(SAPFOR::BasicBlock* block, SAPFOR::Instruction* phiInstr)
{
std::vector<SAPFOR::Instruction*> result;
auto& instructions = block->getInstructions();
@@ -209,21 +218,23 @@ std::vector<SAPFOR::Instruction*> getPhiArguments(SAPFOR::BasicBlock* block, SAP
for (int i = instructions.size() - 1; i >= 0; --i) {
auto instr = instructions[i]->getInstruction();
if (collecting) {
if (instr->getOperation() == SAPFOR::CFG_OP::PARAM) {
if (collecting)
{
if (instr->getOperation() == SAPFOR::CFG_OP::PARAM)
{
auto arg = instr->getArg1();
if (arg) {
result.push_back(instr);
}
}
else {
break;
if (arg)
result.push_back(instr);
}
else
break;
}
if (!instr) continue;
if (!instr)
continue;
if (instr == phiInstr) {
if (instr == phiInstr)
{
collecting = true;
continue;
}
@@ -233,55 +244,64 @@ std::vector<SAPFOR::Instruction*> getPhiArguments(SAPFOR::BasicBlock* block, SAP
return result;
}
SAPFOR::BasicBlock* findInstructionBlock(SAPFOR::Instruction* targetInstr, const std::vector<SAPFOR::BasicBlock*>& blocks) {
for (auto block : blocks) {
for (auto instrWrapper : block->getInstructions()) {
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) {
if (instr == targetInstr)
return block;
}
}
}
return nullptr;
return NULL;
}
SAPFOR::BasicBlock* findInstructionBlockByNumber(int number, const std::vector<SAPFOR::BasicBlock*>& blocks) {
for (auto block : blocks) {
for (auto instrWrapper : block->getInstructions()) {
static 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) {
if (instr->getNumber() == number)
return block;
}
}
}
return nullptr;
return NULL;
}
void findInductiveVars(const std::vector<SAPFOR::BasicBlock*>& blocks, const std::vector<SAPFOR::BasicBlock*>& Loopblocks, SAPFOR::BasicBlock* loopHeader, const set<SAPFOR::BasicBlock*>& loopExits) {
static void findInductiveVars(const std::vector<SAPFOR::BasicBlock*>& blocks,
const std::vector<SAPFOR::BasicBlock*>& Loopblocks, SAPFOR::BasicBlock* loopHeader,
const set<SAPFOR::BasicBlock*>& loopExits)
{
set<string> inductiveVars;
set<SAPFOR::BasicBlock*> relevantBlocks = { loopHeader };
for (auto block : relevantBlocks) {
for (auto instrWrapper : block->getInstructions()) {
for (auto block : relevantBlocks)
{
for (auto instrWrapper : block->getInstructions())
{
auto instr = instrWrapper->getInstruction();
if (!instr) continue;
if (!instr)
continue;
auto op = instr->getOperation();
auto res = instr->getResult();
auto arg1 = instr->getArg1();
auto arg2 = instr->getArg2();
if (op == SAPFOR::CFG_OP::JUMP_IF) {
if (arg1 && arg1->getType() == SAPFOR::CFG_ARG_TYPE::VAR) {
if (op == SAPFOR::CFG_OP::JUMP_IF)
{
if (arg1 && arg1->getType() == SAPFOR::CFG_ARG_TYPE::VAR)
inductiveVars.insert(arg1->getValue());
}
if (arg1 && arg1->getType() == SAPFOR::CFG_ARG_TYPE::REG) {
if (arg1 && arg1->getType() == SAPFOR::CFG_ARG_TYPE::REG)
{
auto foundVariables = findRegisterSourceVariables(blocks, arg1);
for (auto var : foundVariables) {
for (auto var : foundVariables)
inductiveVars.insert(var->getValue());
}
}
}
}
@@ -289,70 +309,71 @@ void findInductiveVars(const std::vector<SAPFOR::BasicBlock*>& blocks, const std
set<string> finalInductiveVars;
for (auto instrWrapper : loopHeader->getInstructions()) {
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;
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;
if (!phiRes || !inductiveVars.count(phiRes->getValue()))
continue;
auto currentBlock = findInstructionBlock(instr, blocks);
if (!currentBlock) continue;
if (!currentBlock)
continue;
auto phiArgs = getPhiArguments(currentBlock, instr);
bool hasInLoopDefinition = false;
for (const auto& argInstr : phiArgs) {
if (!argInstr) continue;
for (const auto& argInstr : phiArgs)
{
if (!argInstr)
continue;
int definitionInstrNumber = stoi(argInstr->getArg1()->getValue());
if (definitionInstrNumber == -1) continue;
if (definitionInstrNumber == -1)
continue;
auto phiBlock = findInstructionBlockByNumber(definitionInstrNumber, blocks);
if (!phiBlock) continue;
if (!phiBlock)
continue;
if (std::find(Loopblocks.begin(), Loopblocks.end(), phiBlock) != Loopblocks.end()) {
if (std::find(Loopblocks.begin(), Loopblocks.end(), phiBlock) != Loopblocks.end())
hasInLoopDefinition = true;
}
}
if (hasInLoopDefinition) {
if (hasInLoopDefinition)
finalInductiveVars.insert(phiRes->getValue());
}
}
for (auto i : finalInductiveVars) {
std::cout << "Confirmed inductive variable: " << i << std::endl;
}
for (auto i : finalInductiveVars)
cout << "Confirmed inductive variable: " << i << endl;
if (finalInductiveVars.empty()) {
std::cout << "No confirmed inductive variables found." << std::endl;
}
if (finalInductiveVars.empty())
cout << "No confirmed inductive variables found." << endl;
}
SAPFOR::Instruction* findInstructionAfterLoop(const std::vector<SAPFOR::BasicBlock*>& loopBody) {
static SAPFOR::Instruction* findInstructionAfterLoop(const std::vector<SAPFOR::BasicBlock*>& loopBody)
{
set<SAPFOR::BasicBlock*> loopSet(loopBody.begin(), loopBody.end());
for (auto block : loopBody) {
for (auto succ : block->getNext()) {
if (!loopSet.count(succ)) {
for (auto block : loopBody)
{
for (auto succ : block->getNext())
{
if (!loopSet.count(succ))
{
// <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
auto instructions = succ->getInstructions();
for (auto wrapper : instructions) {
if (auto instr = wrapper->getInstruction()) {
for (auto wrapper : instructions)
if (auto instr = wrapper->getInstruction())
return instr;
}
}
}
}
}
return nullptr; // <20><> <20><><EFBFBD><EFBFBD><EFBFBD>
}
bool isEqual(const char* cstr, const std::string& str) {
return str == cstr;
return NULL; // <20><> <20><><EFBFBD><EFBFBD><EFBFBD>
}
void findImplicitLoops(const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& fullIR_SSA, const char* fileName)
@@ -362,7 +383,7 @@ void findImplicitLoops(const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& fullIR
//for (auto j : i.second)
// printblock(j);
if (!isEqual(fileName, i.first->fileName))
if (fileName != i.first->fileName)
continue;
map<int, int> visited;
@@ -398,7 +419,6 @@ void findImplicitLoops(const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& fullIR
findInductiveVars(i.second, loopBody, header, loopExits);
SAPFOR::Instruction* instructionAfterLoop = findInstructionAfterLoop(loopBody);
if (instructionAfterLoop == NULL) {
@@ -419,24 +439,20 @@ void findImplicitLoops(const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& fullIR
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;
}