Files
SAPFOR/src/Transformations/MoveOperators/move_operators.cpp

826 lines
29 KiB
C++
Raw Normal View History

2025-03-25 15:18:49 +03:00
#include <map>
#include <unordered_set>
#include <vector>
#include <queue>
#include <iostream>
2025-10-23 14:54:43 +03:00
#include <algorithm>
2025-03-25 15:18:49 +03:00
2025-06-02 08:54:45 +03:00
#include "../../Utils/errors.h"
#include "../../Utils/SgUtils.h"
#include "../../GraphCall/graph_calls.h"
#include "../../GraphCall/graph_calls_func.h"
#include "../../CFGraph/CFGraph.h"
#include "../../CFGraph/IR.h"
#include "../../GraphLoop/graph_loops.h"
2025-12-29 21:10:55 +03:00
#include "move_operators.h"
2025-03-25 15:18:49 +03:00
2025-05-13 00:46:32 +03:00
using namespace std;
2025-03-25 15:18:49 +03:00
2025-12-29 21:10:55 +03:00
static vector<SAPFOR::IR_Block*> findInstructionsFromOperator(SgStatement* st, const vector<SAPFOR::BasicBlock*>& Blocks) {
2025-05-13 00:46:32 +03:00
vector<SAPFOR::IR_Block*> result;
2025-10-23 14:54:43 +03:00
string filename = st->fileName();
for (auto& block: Blocks) {
vector<SAPFOR::IR_Block*> instructionsInBlock = block->getInstructions();
2025-12-29 21:10:55 +03:00
2025-10-23 14:54:43 +03:00
for (auto& instruction: instructionsInBlock) {
SgStatement* curOperator = instruction->getInstruction()->getOperator();
2025-12-29 21:10:55 +03:00
if (curOperator->lineNumber() == st->lineNumber())
2025-05-13 00:46:32 +03:00
result.push_back(instruction);
}
}
return result;
}
2025-12-29 21:10:55 +03:00
const unordered_set<int> loop_tags = { FOR_NODE };
const unordered_set<int> control_tags = { IF_NODE, ELSEIF_NODE, DO_WHILE_NODE, WHILE_NODE, LOGIF_NODE };
const unordered_set<int> control_end_tags = { CONTROL_END };
2025-05-13 00:46:32 +03:00
2025-10-23 14:54:43 +03:00
struct OperatorInfo {
SgStatement* stmt;
set<string> usedVars;
set<string> definedVars;
int lineNumber;
bool isMovable;
2025-12-29 21:10:55 +03:00
OperatorInfo(SgStatement* s) : stmt(s), lineNumber(s->lineNumber()), isMovable(true) { }
2025-10-23 14:54:43 +03:00
};
2025-05-13 00:46:32 +03:00
static bool isStatementEmbedded(SgStatement* stmt, SgStatement* parent) {
2025-12-29 21:10:55 +03:00
if (!stmt || !parent || stmt == parent)
return false;
if (parent->variant() == LOGIF_NODE) {
2025-12-29 21:10:55 +03:00
if (stmt->lineNumber() == parent->lineNumber())
return true;
SgStatement* current = parent;
SgStatement* lastNode = parent->lastNodeOfStmt();
while (current && current != lastNode) {
2025-12-29 21:10:55 +03:00
if (current == stmt)
return true;
2025-12-29 21:10:55 +03:00
if (current->isIncludedInStmt(*stmt))
return true;
2025-12-29 21:10:55 +03:00
current = current->lexNext();
}
}
2025-12-29 21:10:55 +03:00
if (parent->isIncludedInStmt(*stmt))
return true;
return false;
}
static bool isLoopBoundary(SgStatement* stmt) {
2025-12-29 21:10:55 +03:00
if (!stmt)
return false;
2025-12-29 21:10:55 +03:00
if (stmt->variant() == FOR_NODE || stmt->variant() == CONTROL_END)
return true;
return false;
}
static bool isPartOfNestedLoop(SgStatement* stmt, SgForStmt* loop) {
2025-12-29 21:10:55 +03:00
if (!stmt || !loop)
return false;
SgStatement* loopStart = loop->lexNext();
SgStatement* loopEnd = loop->lastNodeOfStmt();
2025-12-29 21:10:55 +03:00
if (!loopStart || !loopEnd)
return false;
2025-12-29 21:10:55 +03:00
if (stmt->lineNumber() < loopStart->lineNumber() || stmt->lineNumber() > loopEnd->lineNumber())
return false;
SgStatement* current = loopStart;
while (current && current != loopEnd) {
if (current->variant() == FOR_NODE && current != loop) {
SgForStmt* nestedLoop = (SgForStmt*)current;
SgStatement* nestedStart = nestedLoop->lexNext();
SgStatement* nestedEnd = nestedLoop->lastNodeOfStmt();
if (nestedStart && nestedEnd &&
stmt->lineNumber() >= nestedStart->lineNumber() &&
stmt->lineNumber() <= nestedEnd->lineNumber()) {
return true;
}
}
current = current->lexNext();
}
return false;
}
static bool canSafelyExtract(SgStatement* stmt, SgForStmt* loop) {
2025-12-29 21:10:55 +03:00
if (!stmt || !loop)
return false;
2025-12-29 21:10:55 +03:00
if (isLoopBoundary(stmt))
return false;
2025-12-29 21:10:55 +03:00
if (control_tags.find(stmt->variant()) != control_tags.end())
return false;
2025-12-29 21:10:55 +03:00
if (isPartOfNestedLoop(stmt, loop))
return false;
SgStatement* loopStart = loop->lexNext();
SgStatement* loopEnd = loop->lastNodeOfStmt();
2025-12-29 21:10:55 +03:00
if (!loopStart || !loopEnd)
return false;
SgStatement* current = loopStart;
while (current && current != loopEnd) {
2025-12-29 21:10:55 +03:00
if (current->variant() == LOGIF_NODE && current->lineNumber() == stmt->lineNumber())
return false;
2025-12-29 21:10:55 +03:00
if (control_tags.find(current->variant()) != control_tags.end())
if (isStatementEmbedded(stmt, current))
return false;
2025-12-29 21:10:55 +03:00
if (current == stmt)
break;
current = current->lexNext();
}
return true;
}
2025-12-29 21:10:55 +03:00
static vector<OperatorInfo> analyzeOperatorsInLoop(SgForStmt* loop, const vector<SAPFOR::BasicBlock*>& blocks,
const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& FullIR) {
2025-10-23 14:54:43 +03:00
vector<OperatorInfo> operators;
SgStatement* loopStart = loop->lexNext();
SgStatement* loopEnd = loop->lastNodeOfStmt();
2025-12-29 21:10:55 +03:00
if (!loopStart || !loopEnd)
return operators;
2025-10-23 14:54:43 +03:00
SgStatement* current = loopStart;
unordered_set<SgStatement*> visited;
2025-10-23 14:54:43 +03:00
while (current && current != loopEnd) {
2025-12-29 21:10:55 +03:00
if (visited.find(current) != visited.end())
break;
2025-12-29 21:10:55 +03:00
visited.insert(current);
if (isLoopBoundary(current)) {
current = current->lexNext();
continue;
}
if (current->variant() == FOR_NODE && current != loop) {
SgStatement* nestedEnd = current->lastNodeOfStmt();
2025-12-29 21:10:55 +03:00
if (nestedEnd)
current = nestedEnd->lexNext();
2025-12-29 21:10:55 +03:00
else
current = current->lexNext();
continue;
}
2025-10-23 14:54:43 +03:00
if (isSgExecutableStatement(current)) {
if (control_tags.find(current->variant()) != control_tags.end()) {
current = current->lexNext();
continue;
}
2025-12-29 21:10:55 +03:00
if (current->variant() != ASSIGN_STAT) {
current = current->lexNext();
continue;
}
2025-10-23 14:54:43 +03:00
OperatorInfo opInfo(current);
vector<SAPFOR::IR_Block*> irBlocks = findInstructionsFromOperator(current, blocks);
for (auto irBlock : irBlocks) {
2025-12-29 21:10:55 +03:00
if (!irBlock || !irBlock->getInstruction())
continue;
2025-10-23 14:54:43 +03:00
2025-12-29 21:10:55 +03:00
const SAPFOR::Instruction* instr = irBlock->getInstruction();
2025-10-23 14:54:43 +03:00
if (instr->getArg1()) {
string varName = getNameByArg(instr->getArg1());
2025-12-29 21:10:55 +03:00
if (!varName.empty())
2025-10-23 14:54:43 +03:00
opInfo.usedVars.insert(varName);
2025-05-22 22:41:09 +03:00
}
2025-12-29 21:10:55 +03:00
2025-10-23 14:54:43 +03:00
if (instr->getArg2()) {
string varName = getNameByArg(instr->getArg2());
2025-12-29 21:10:55 +03:00
if (!varName.empty())
2025-10-23 14:54:43 +03:00
opInfo.usedVars.insert(varName);
}
2025-12-29 21:10:55 +03:00
2025-10-23 14:54:43 +03:00
if (instr->getResult()) {
string varName = getNameByArg(instr->getResult());
2025-12-29 21:10:55 +03:00
if (!varName.empty())
2025-10-23 14:54:43 +03:00
opInfo.definedVars.insert(varName);
2025-05-22 22:41:09 +03:00
}
}
2025-10-23 14:54:43 +03:00
operators.push_back(opInfo);
2025-05-22 22:41:09 +03:00
}
2025-10-23 14:54:43 +03:00
current = current->lexNext();
2025-05-22 22:41:09 +03:00
}
2025-10-23 14:54:43 +03:00
return operators;
2025-05-13 00:46:32 +03:00
}
2025-10-23 14:54:43 +03:00
static map<string, vector<SgStatement*>> findVariableDefinitions(SgForStmt* loop, vector<OperatorInfo>& operators) {
map<string, vector<SgStatement*>> varDefinitions;
for (auto& op : operators)
for (const string& var : op.definedVars)
2025-10-23 14:54:43 +03:00
varDefinitions[var].push_back(op.stmt);
2025-10-23 14:54:43 +03:00
return varDefinitions;
2025-05-24 19:56:15 +03:00
}
2025-10-23 14:54:43 +03:00
static int calculateDistance(SgStatement* from, SgStatement* to) {
2025-12-29 21:10:55 +03:00
if (!from || !to)
return INT_MAX;
2025-10-23 14:54:43 +03:00
return abs(to->lineNumber() - from->lineNumber());
}
2025-05-24 19:56:15 +03:00
2025-12-29 21:10:55 +03:00
static SgStatement* findBestPosition(SgStatement* operatorStmt, const vector<OperatorInfo>& operators,
const map<string, vector<SgStatement*>>& varDefinitions, SgForStmt* loop) {
const OperatorInfo* opInfo = nullptr;
2025-10-23 14:54:43 +03:00
for (auto& op : operators) {
if (op.stmt == operatorStmt) {
opInfo = &op;
break;
2025-05-27 15:55:02 +03:00
}
}
2025-12-29 21:10:55 +03:00
if (!opInfo || !opInfo->isMovable)
return nullptr;
2025-10-23 14:54:43 +03:00
SgStatement* bestPos = nullptr;
2025-12-25 15:01:01 +03:00
int bestLine = -1;
2025-10-23 14:54:43 +03:00
for (const string& usedVar : opInfo->usedVars) {
if (varDefinitions.find(usedVar) != varDefinitions.end()) {
2025-12-29 21:10:55 +03:00
for (SgStatement* defStmt : varDefinitions.at(usedVar)) {
2025-12-25 15:01:01 +03:00
if (defStmt->lineNumber() < operatorStmt->lineNumber()) {
if (defStmt->controlParent() == operatorStmt->controlParent()) {
if (defStmt->lineNumber() > bestLine) {
bestLine = defStmt->lineNumber();
bestPos = defStmt;
}
}
}
}
}
}
if (!bestPos) {
bool allLoopCarried = true;
bool hasAnyDefinition = false;
for (const string& usedVar : opInfo->usedVars) {
if (varDefinitions.find(usedVar) != varDefinitions.end()) {
2025-12-29 21:10:55 +03:00
for (SgStatement* defStmt : varDefinitions.at(usedVar)) {
if (defStmt == operatorStmt)
2025-12-25 15:01:01 +03:00
continue;
hasAnyDefinition = true;
if (defStmt->lineNumber() < operatorStmt->lineNumber() &&
defStmt->controlParent() == operatorStmt->controlParent()) {
allLoopCarried = false;
break;
}
2025-05-24 19:56:15 +03:00
}
}
2025-12-29 21:10:55 +03:00
if (!allLoopCarried)
break;
2025-12-25 15:01:01 +03:00
}
if (allLoopCarried || (!hasAnyDefinition && !opInfo->usedVars.empty())) {
SgStatement* loopStart = loop->lexNext();
return loopStart;
2025-05-24 19:56:15 +03:00
}
2025-05-22 22:41:09 +03:00
}
2025-10-23 14:54:43 +03:00
return bestPos;
2025-05-24 19:56:15 +03:00
}
2025-10-23 14:54:43 +03:00
static bool canMoveTo(SgStatement* from, SgStatement* to, SgForStmt* loop) {
2025-12-29 21:10:55 +03:00
if (!from || !to || from == to)
return false;
2025-10-23 14:54:43 +03:00
2025-10-08 23:14:19 +03:00
SgStatement* loopStart = loop->lexNext();
SgStatement* loopEnd = loop->lastNodeOfStmt();
2025-10-23 14:54:43 +03:00
2025-12-29 21:10:55 +03:00
if (!loopStart || !loopEnd)
return false;
2025-12-25 15:01:01 +03:00
if (to == loopStart) {
SgStatement* fromControlParent = from->controlParent();
if (!fromControlParent) fromControlParent = loop;
return fromControlParent == loop || fromControlParent == loopStart->controlParent();
}
2025-12-29 21:10:55 +03:00
if (from->lineNumber() < loopStart->lineNumber() || from->lineNumber() > loopEnd->lineNumber())
2025-12-25 15:01:01 +03:00
return false;
2025-12-29 21:10:55 +03:00
if (to->lineNumber() < loopStart->lineNumber() || to->lineNumber() > loopEnd->lineNumber())
2025-10-23 14:54:43 +03:00
return false;
2025-12-29 21:10:55 +03:00
if (to->lineNumber() >= from->lineNumber())
2025-12-25 15:01:01 +03:00
return false;
2025-12-29 21:10:55 +03:00
if (from->controlParent() != to->controlParent())
2025-12-25 15:01:01 +03:00
return false;
SgStatement* current = to->lexNext();
while (current && current != from && current != loopEnd) {
2025-10-23 14:54:43 +03:00
if (control_tags.find(current->variant()) != control_tags.end()) {
2025-12-25 15:01:01 +03:00
SgStatement* controlEnd = current->lastNodeOfStmt();
if (controlEnd && from->lineNumber() <= controlEnd->lineNumber()) {
if (from->controlParent() == current && to->controlParent() != current) {
return false;
}
}
2025-10-23 14:54:43 +03:00
}
current = current->lexNext();
}
return true;
}
2025-10-08 23:14:19 +03:00
2025-12-29 21:10:55 +03:00
static vector<SgStatement*> optimizeOperatorOrder(SgForStmt* loop,
const vector<OperatorInfo>& operators,
const map<string, vector<SgStatement*>>& varDefinitions) {
2025-10-23 14:54:43 +03:00
vector<SgStatement*> newOrder;
2025-12-29 21:10:55 +03:00
for (auto& op : operators)
2025-12-25 15:01:01 +03:00
newOrder.push_back(op.stmt);
2025-10-23 14:54:43 +03:00
2025-12-29 21:10:55 +03:00
map<SgStatement*, const OperatorInfo*> stmtToOpInfo;
for (auto& op : operators)
2025-12-25 15:01:01 +03:00
stmtToOpInfo[op.stmt] = &op;
bool changed = true;
while (changed) {
2025-12-25 15:01:01 +03:00
changed = false;
2025-10-23 14:54:43 +03:00
2025-12-25 15:01:01 +03:00
for (int i = operators.size() - 1; i >= 0; i--) {
2025-12-29 21:10:55 +03:00
if (!operators[i].isMovable)
continue;
2025-12-25 15:01:01 +03:00
SgStatement* stmt = operators[i].stmt;
2025-12-29 21:10:55 +03:00
const OperatorInfo* opInfo = stmtToOpInfo[stmt];
if (!opInfo)
continue;
2025-12-25 15:01:01 +03:00
size_t currentPos = 0;
2025-10-23 14:54:43 +03:00
for (size_t j = 0; j < newOrder.size(); j++) {
2025-12-25 15:01:01 +03:00
if (newOrder[j] == stmt) {
currentPos = j;
2025-10-08 23:14:19 +03:00
break;
}
}
2025-12-25 15:01:01 +03:00
SgStatement* bestPos = findBestPosition(stmt, operators, varDefinitions, loop);
if (!bestPos) {
bool hasDependents = false;
for (size_t j = currentPos + 1; j < newOrder.size(); j++) {
SgStatement* candidate = newOrder[j];
2025-12-29 21:10:55 +03:00
const OperatorInfo* candidateOpInfo = stmtToOpInfo[candidate];
2025-12-25 15:01:01 +03:00
if (candidateOpInfo) {
for (const string& definedVar : opInfo->definedVars) {
if (candidateOpInfo->usedVars.find(definedVar) != candidateOpInfo->usedVars.end()) {
hasDependents = true;
break;
}
}
2025-12-29 21:10:55 +03:00
if (hasDependents)
break;
2025-12-25 15:01:01 +03:00
}
}
continue;
}
size_t targetPos = 0;
bool foundTarget = false;
if (bestPos == loop->lexNext()) {
targetPos = 0;
for (size_t j = 0; j < currentPos && j < newOrder.size(); j++) {
SgStatement* candidate = newOrder[j];
2025-12-29 21:10:55 +03:00
const OperatorInfo* candidateOpInfo = stmtToOpInfo[candidate];
2025-12-25 15:01:01 +03:00
if (candidateOpInfo) {
bool usesDefinedVar = false;
for (const string& definedVar : opInfo->definedVars) {
if (candidateOpInfo->usedVars.find(definedVar) != candidateOpInfo->usedVars.end()) {
usesDefinedVar = true;
break;
}
}
2025-12-25 15:01:01 +03:00
if (usesDefinedVar) {
targetPos = j;
break;
}
}
}
foundTarget = true;
if (currentPos != targetPos && canMoveTo(stmt, bestPos, loop)) {
newOrder.erase(newOrder.begin() + currentPos);
newOrder.insert(newOrder.begin() + targetPos, stmt);
changed = true;
}
} else {
size_t bestPosIdx = 0;
bool foundBestPos = false;
for (size_t j = 0; j < newOrder.size(); j++) {
if (newOrder[j] == bestPos) {
bestPosIdx = j;
foundBestPos = true;
break;
}
}
if (foundBestPos) {
targetPos = bestPosIdx + 1;
for (size_t j = bestPosIdx + 1; j < currentPos && j < newOrder.size(); j++) {
SgStatement* candidate = newOrder[j];
2025-12-29 21:10:55 +03:00
const OperatorInfo* candidateOpInfo = stmtToOpInfo[candidate];
2025-12-25 15:01:01 +03:00
if (candidateOpInfo) {
bool definesUsedVar = false;
for (const string& usedVar : opInfo->usedVars) {
if (candidateOpInfo->definedVars.find(usedVar) != candidateOpInfo->definedVars.end()) {
definesUsedVar = true;
break;
}
}
2025-12-29 21:10:55 +03:00
if (definesUsedVar)
2025-12-25 15:01:01 +03:00
targetPos = j + 1;
}
}
bool wouldBreakDependency = false;
for (size_t j = targetPos; j < currentPos && j < newOrder.size(); j++) {
SgStatement* candidate = newOrder[j];
2025-12-29 21:10:55 +03:00
const OperatorInfo* candidateOpInfo = stmtToOpInfo[candidate];
2025-12-25 15:01:01 +03:00
if (candidateOpInfo) {
for (const string& definedVar : opInfo->definedVars) {
if (candidateOpInfo->usedVars.find(definedVar) != candidateOpInfo->usedVars.end()) {
wouldBreakDependency = true;
break;
}
}
2025-12-29 21:10:55 +03:00
if (wouldBreakDependency)
break;
2025-12-25 15:01:01 +03:00
}
}
if (!wouldBreakDependency && currentPos > targetPos && canMoveTo(stmt, bestPos, loop)) {
newOrder.erase(newOrder.begin() + currentPos);
newOrder.insert(newOrder.begin() + targetPos, stmt);
changed = true;
}
}
2025-10-23 14:54:43 +03:00
}
2025-10-08 23:14:19 +03:00
}
2025-12-25 15:01:01 +03:00
}
bool dependencyViolation = true;
set<pair<SgStatement*, SgStatement*>> triedPairs;
while (dependencyViolation) {
dependencyViolation = false;
triedPairs.clear();
for (size_t i = 0; i < newOrder.size(); i++) {
SgStatement* stmt = newOrder[i];
2025-12-29 21:10:55 +03:00
const OperatorInfo* opInfo = stmtToOpInfo[stmt];
if (!opInfo)
continue;
2025-12-25 15:01:01 +03:00
for (size_t j = 0; j < i; j++) {
SgStatement* prevStmt = newOrder[j];
2025-12-29 21:10:55 +03:00
const OperatorInfo* prevOpInfo = stmtToOpInfo[prevStmt];
if (!prevOpInfo)
continue;
2025-12-25 15:01:01 +03:00
pair<SgStatement*, SgStatement*> key = make_pair(stmt, prevStmt);
2025-12-29 21:10:55 +03:00
if (triedPairs.find(key) != triedPairs.end())
2025-12-25 15:01:01 +03:00
continue;
bool violation = false;
for (const string& definedVar : opInfo->definedVars) {
if (prevOpInfo->usedVars.find(definedVar) != prevOpInfo->usedVars.end()) {
violation = true;
break;
}
}
if (violation) {
triedPairs.insert(key);
bool wouldCreateViolation = false;
for (size_t k = j; k < i; k++) {
SgStatement* betweenStmt = newOrder[k];
2025-12-29 21:10:55 +03:00
const OperatorInfo* betweenOpInfo = stmtToOpInfo[betweenStmt];
if (!betweenOpInfo)
continue;
2025-12-25 15:01:01 +03:00
for (const string& usedVar : opInfo->usedVars) {
if (betweenOpInfo->definedVars.find(usedVar) != betweenOpInfo->definedVars.end()) {
wouldCreateViolation = true;
break;
}
}
2025-12-29 21:10:55 +03:00
if (wouldCreateViolation)
break;
2025-12-25 15:01:01 +03:00
}
if (!wouldCreateViolation) {
newOrder.erase(newOrder.begin() + i);
newOrder.insert(newOrder.begin() + j, stmt);
dependencyViolation = true;
break;
}
}
}
2025-12-29 21:10:55 +03:00
if (dependencyViolation)
break;
2025-12-25 15:01:01 +03:00
}
2025-05-27 15:55:02 +03:00
}
2025-10-23 14:54:43 +03:00
return newOrder;
}
2025-05-27 15:55:02 +03:00
2025-12-29 21:10:55 +03:00
static bool applyOperatorReordering(SgForStmt* loop, const vector<SgStatement*>& newOrder) {
if (!loop || newOrder.empty())
return false;
2025-10-23 14:54:43 +03:00
SgStatement* loopStart = loop->lexNext();
SgStatement* loopEnd = loop->lastNodeOfStmt();
2025-12-29 21:10:55 +03:00
if (!loopStart || !loopEnd)
return false;
vector<SgStatement*> originalOrder;
SgStatement* current = loopStart;
while (current && current != loopEnd) {
2025-12-29 21:10:55 +03:00
if (isSgExecutableStatement(current) && current->variant() == ASSIGN_STAT)
originalOrder.push_back(current);
2025-12-29 21:10:55 +03:00
current = current->lexNext();
}
bool orderChanged = false;
if (originalOrder.size() == newOrder.size()) {
for (size_t i = 0; i < originalOrder.size(); i++) {
if (originalOrder[i] != newOrder[i]) {
orderChanged = true;
break;
}
}
2025-12-29 21:10:55 +03:00
}
else
orderChanged = true;
2025-12-29 21:10:55 +03:00
if (!orderChanged)
return false;
2025-12-29 21:10:55 +03:00
2025-10-08 23:14:19 +03:00
vector<SgStatement*> extractedStatements;
vector<char*> savedComments;
unordered_set<SgStatement*> extractedSet;
map<SgStatement*, int> originalLineNumbers;
2025-12-25 15:01:01 +03:00
map<SgStatement*, SgStatement*> stmtToExtracted;
2025-10-23 14:54:43 +03:00
for (SgStatement* stmt : newOrder) {
if (stmt && stmt != loop && stmt != loopEnd && extractedSet.find(stmt) == extractedSet.end()) {
2025-12-29 21:10:55 +03:00
if (control_tags.find(stmt->variant()) != control_tags.end())
continue;
2025-12-29 21:10:55 +03:00
if (!canSafelyExtract(stmt, loop))
continue;
bool isMoving = false;
for (size_t i = 0; i < originalOrder.size(); i++) {
if (originalOrder[i] == stmt) {
for (size_t j = 0; j < newOrder.size(); j++) {
if (newOrder[j] == stmt && i != j) {
isMoving = true;
break;
}
}
break;
}
}
2025-12-29 21:10:55 +03:00
if (!isMoving)
continue;
originalLineNumbers[stmt] = stmt->lineNumber();
2025-10-08 23:14:19 +03:00
savedComments.push_back(stmt->comments() ? strdup(stmt->comments()) : nullptr);
SgStatement* extracted = stmt->extractStmt();
2025-10-23 14:54:43 +03:00
if (extracted) {
extractedStatements.push_back(extracted);
extractedSet.insert(stmt);
2025-12-25 15:01:01 +03:00
stmtToExtracted[stmt] = extracted;
2025-10-23 14:54:43 +03:00
}
2025-10-08 23:14:19 +03:00
}
}
2025-10-23 14:54:43 +03:00
2025-12-25 15:01:01 +03:00
map<SgStatement*, SgStatement*> insertedStatements;
2025-10-08 23:14:19 +03:00
2025-12-25 15:01:01 +03:00
for (size_t idx = 0; idx < newOrder.size(); idx++) {
SgStatement* stmt = newOrder[idx];
if (extractedSet.find(stmt) != extractedSet.end()) {
SgStatement* stmtToInsert = stmtToExtracted[stmt];
if (!stmtToInsert)
continue;
2025-12-25 15:01:01 +03:00
SgStatement* insertAfter = loop;
for (int i = idx - 1; i >= 0; i--) {
SgStatement* prevStmt = newOrder[i];
if (extractedSet.find(prevStmt) != extractedSet.end()) {
if (insertedStatements.find(prevStmt) != insertedStatements.end()) {
insertAfter = insertedStatements[prevStmt];
break;
}
} else {
SgStatement* search = loop->lexNext();
while (search && search != loopEnd) {
bool skip = false;
for (size_t j = idx; j < newOrder.size(); j++) {
if (extractedSet.find(newOrder[j]) != extractedSet.end() &&
search == newOrder[j]) {
skip = true;
break;
}
}
if (skip) {
search = search->lexNext();
continue;
}
if (search == prevStmt) {
insertAfter = search;
break;
}
search = search->lexNext();
}
if (insertAfter != loop) break;
}
2025-12-25 15:01:01 +03:00
}
size_t commentIdx = 0;
for (size_t i = 0; i < extractedStatements.size(); i++) {
if (extractedStatements[i] == stmtToInsert) {
commentIdx = i;
break;
}
}
2025-12-29 21:10:55 +03:00
if (commentIdx < savedComments.size() && savedComments[commentIdx])
2025-12-25 15:01:01 +03:00
stmtToInsert->setComments(savedComments[commentIdx]);
2025-12-29 21:10:55 +03:00
if (originalLineNumbers.find(stmt) != originalLineNumbers.end())
2025-12-25 15:01:01 +03:00
stmtToInsert->setlineNumber(originalLineNumbers[stmt]);
2025-12-25 15:01:01 +03:00
SgStatement* controlParent = stmt->controlParent();
2025-12-29 21:10:55 +03:00
if (!controlParent)
controlParent = loop;
2025-12-25 15:01:01 +03:00
insertAfter->insertStmtAfter(*stmtToInsert, *controlParent);
insertedStatements[stmt] = stmtToInsert;
2025-10-08 23:14:19 +03:00
}
}
2025-10-23 14:54:43 +03:00
2025-10-08 23:14:19 +03:00
for (char* comment : savedComments) {
2025-12-29 21:10:55 +03:00
if (comment)
2025-10-08 23:14:19 +03:00
free(comment);
}
2025-10-23 14:54:43 +03:00
2025-10-08 23:14:19 +03:00
return true;
}
2025-12-29 21:10:55 +03:00
vector<SAPFOR::BasicBlock*> findFuncBlocksByFuncStatement(SgStatement *st, const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& FullIR) {
2025-10-23 14:54:43 +03:00
vector<SAPFOR::BasicBlock*> result;
Statement* forSt = (Statement*)st;
2025-12-29 21:10:55 +03:00
2025-10-23 14:54:43 +03:00
for (auto& func: FullIR) {
if (func.first->funcPointer->getCurrProcessFile() == forSt->getCurrProcessFile()
&& func.first->funcPointer->lineNumber() == forSt->lineNumber())
{
2025-10-23 14:54:43 +03:00
result = func.second;
}
2025-10-08 23:14:19 +03:00
}
2025-10-23 14:54:43 +03:00
return result;
}
2025-12-29 21:10:55 +03:00
map<SgForStmt*, vector<SAPFOR::BasicBlock*>> findAndAnalyzeLoops(SgStatement *st, const vector<SAPFOR::BasicBlock*>& blocks) {
2025-10-23 14:54:43 +03:00
map<SgForStmt*, vector<SAPFOR::BasicBlock*>> result;
SgStatement *lastNode = st->lastNodeOfStmt();
2025-10-23 14:54:43 +03:00
while (st && st != lastNode) {
if (loop_tags.find(st -> variant()) != loop_tags.end()) {
SgForStmt *forSt = (SgForStmt*)st;
SgStatement *loopBody = forSt -> body();
SgStatement *lastLoopNode = st->lastNodeOfStmt();
unordered_set<int> blocks_nums;
2025-10-23 14:54:43 +03:00
while (loopBody && loopBody != lastLoopNode) {
vector<SAPFOR::IR_Block*> irBlocks = findInstructionsFromOperator(loopBody, blocks);
if (!irBlocks.empty()) {
SAPFOR::IR_Block* IR = irBlocks.front();
if (IR && IR->getBasicBlock()) {
if (blocks_nums.find(IR -> getBasicBlock() -> getNumber()) == blocks_nums.end()) {
result[forSt].push_back(IR -> getBasicBlock());
blocks_nums.insert(IR -> getBasicBlock() -> getNumber());
}
}
2025-10-23 14:54:43 +03:00
}
loopBody = loopBody -> lexNext();
2025-10-08 23:14:19 +03:00
}
2025-12-29 21:10:55 +03:00
sort(result[forSt].begin(), result[forSt].end());
2025-10-08 23:14:19 +03:00
}
2025-10-23 14:54:43 +03:00
st = st -> lexNext();
2025-10-08 23:14:19 +03:00
}
2025-10-23 14:54:43 +03:00
return result;
2025-10-08 23:14:19 +03:00
}
2025-05-22 22:41:09 +03:00
2025-12-29 21:10:55 +03:00
static void processLoopRecursively(SgForStmt* loop, const vector<SAPFOR::BasicBlock*>& blocks,
const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& FullIR) {
if (!loop)
return;
2025-12-25 15:01:01 +03:00
SgStatement* loopStart = loop->lexNext();
SgStatement* loopEnd = loop->lastNodeOfStmt();
if (loopStart && loopEnd) {
SgStatement* current = loopStart;
while (current && current != loopEnd) {
if (current->variant() == FOR_NODE && current != loop) {
SgForStmt* nestedLoop = (SgForStmt*)current;
processLoopRecursively(nestedLoop, blocks, FullIR);
SgStatement* nestedEnd = nestedLoop->lastNodeOfStmt();
2025-12-29 21:10:55 +03:00
if (nestedEnd)
2025-12-25 15:01:01 +03:00
current = nestedEnd->lexNext();
2025-12-29 21:10:55 +03:00
else
2025-12-25 15:01:01 +03:00
current = current->lexNext();
2025-12-29 21:10:55 +03:00
}
else
2025-12-25 15:01:01 +03:00
current = current->lexNext();
}
}
vector<OperatorInfo> operators = analyzeOperatorsInLoop(loop, blocks, FullIR);
if (!operators.empty()) {
map<string, vector<SgStatement*>> varDefinitions = findVariableDefinitions(loop, operators);
vector<SgStatement*> newOrder = optimizeOperatorOrder(loop, operators, varDefinitions);
applyOperatorReordering(loop, newOrder);
}
}
2025-12-29 21:10:55 +03:00
void moveOperators(SgFile *file, map<string, vector<LoopGraph*>>& loopGraph,
const map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& FullIR,
int& countOfTransform) {
2025-10-23 14:54:43 +03:00
countOfTransform += 1;
2025-12-29 21:10:55 +03:00
//cout << "MOVE_OPERATORS Pass Started" << endl;
2025-05-13 00:46:32 +03:00
const int funcNum = file -> numberOfFunctions();
2025-10-23 14:54:43 +03:00
for (int i = 0; i < funcNum; ++i) {
SgStatement *st = file -> functions(i);
2025-12-29 21:10:55 +03:00
vector<SAPFOR::BasicBlock*> blocks = findFuncBlocksByFuncStatement(st, FullIR);
2025-05-13 00:46:32 +03:00
map<SgForStmt*, vector<SAPFOR::BasicBlock*>> loopsMapping = findAndAnalyzeLoops(st, blocks);
2025-10-23 14:54:43 +03:00
2025-12-29 21:10:55 +03:00
for (auto& loopForAnalyze: loopsMapping)
2025-12-25 15:01:01 +03:00
processLoopRecursively(loopForAnalyze.first, loopForAnalyze.second, FullIR);
2025-05-13 00:46:32 +03:00
}
2025-12-29 21:10:55 +03:00
//cout << "MOVE_OPERATORS Pass Completed" << endl;
2025-10-23 14:54:43 +03:00
}