From 136850a0819a44880bf016a24624701480b6e2c4 Mon Sep 17 00:00:00 2001 From: Egor Mayorov Date: Thu, 19 Feb 2026 09:21:10 +0300 Subject: [PATCH] use rd --- .../MoveOperators/move_operators.cpp | 1116 ++++++++--------- 1 file changed, 529 insertions(+), 587 deletions(-) diff --git a/src/Transformations/MoveOperators/move_operators.cpp b/src/Transformations/MoveOperators/move_operators.cpp index d310afe..6cf14d1 100644 --- a/src/Transformations/MoveOperators/move_operators.cpp +++ b/src/Transformations/MoveOperators/move_operators.cpp @@ -1,9 +1,12 @@ #include -#include +#include #include #include #include #include +#include +#include +#include #include "../../Utils/errors.h" #include "../../Utils/SgUtils.h" @@ -16,35 +19,130 @@ using namespace std; -static vector findInstructionsFromOperator(SgStatement* st, const vector& Blocks) { - vector result; - string filename = st->fileName(); - - for (auto& block: Blocks) { - vector instructionsInBlock = block->getInstructions(); +string getNameByArg(SAPFOR::Argument* arg); - for (auto& instruction: instructionsInBlock) { - SgStatement* curOperator = instruction->getInstruction()->getOperator(); - if (curOperator->lineNumber() == st->lineNumber()) - result.push_back(instruction); +static vector findInstructionsFromStatement(SgStatement* st, const vector& blocks) +{ + vector result; + if (!st) + return result; + + const int stmtId = st->id(); + for (auto* bb : blocks) + { + if (!bb) + continue; + + for (auto* ir : bb->getInstructions()) + { + if (!ir || !ir->getInstruction()) + continue; + + SgStatement* op = ir->getInstruction()->getOperator(); + if (op && op->id() == stmtId) + result.push_back(ir); } } + + sort(result.begin(), result.end(), + [](const SAPFOR::IR_Block* a, const SAPFOR::IR_Block* b) { return a->getNumber() < b->getNumber(); }); return result; } -const unordered_set loop_tags = { FOR_NODE }; -const unordered_set control_tags = { IF_NODE, ELSEIF_NODE, DO_WHILE_NODE, WHILE_NODE, LOGIF_NODE }; -const unordered_set control_end_tags = { CONTROL_END }; +set loop_tags = {FOR_NODE}; +set control_tags = {IF_NODE, ELSEIF_NODE, DO_WHILE_NODE, WHILE_NODE, LOGIF_NODE}; +set control_end_tags = {CONTROL_END}; -struct OperatorInfo { - SgStatement* stmt; - set usedVars; - set definedVars; - int lineNumber; - bool isMovable; - - OperatorInfo(SgStatement* s) : stmt(s), lineNumber(s->lineNumber()), isMovable(true) { } -}; +static bool isBarrierIR(const SAPFOR::Instruction* instr) +{ + if (!instr) + return true; + + using SAPFOR::CFG_OP; + const auto op = instr->getOperation(); + + switch (op) + { + case CFG_OP::F_CALL: + case CFG_OP::STORE: + case CFG_OP::REC_REF_STORE: + case CFG_OP::IO_PARAM: + case CFG_OP::DVM_DIR: + case CFG_OP::SPF_DIR: + case CFG_OP::POINTER_ASS: + case CFG_OP::EXIT: + return true; + default: + return false; + } +} + +static bool isMovableStmtIR(const vector& irs, + set& uses, + set& defs, + bool& spansMultipleBB) +{ + uses.clear(); + defs.clear(); + spansMultipleBB = false; + + if (irs.empty()) + return false; + + SAPFOR::BasicBlock* bb = irs.front()->getBasicBlock(); + for (auto* ir : irs) + if (ir && ir->getBasicBlock() != bb) + spansMultipleBB = true; + + for (auto* ir : irs) + { + if (!ir || !ir->getInstruction()) + return false; + + const auto* instr = ir->getInstruction(); + if (isBarrierIR(instr)) + return false; + + auto addUse = [&](SAPFOR::Argument* a) + { + if (!a) + return; + if (a->getType() == SAPFOR::CFG_ARG_TYPE::VAR) + uses.insert(a); + else if (a->getType() == SAPFOR::CFG_ARG_TYPE::ARRAY || a->getType() == SAPFOR::CFG_ARG_TYPE::RECORD) + uses.insert(a); + }; + + auto addDef = [&](SAPFOR::Argument* a) + { + if (!a) + return; + if (a->getType() == SAPFOR::CFG_ARG_TYPE::VAR) + defs.insert(a); + else if (a->getType() == SAPFOR::CFG_ARG_TYPE::ARRAY || a->getType() == SAPFOR::CFG_ARG_TYPE::RECORD) + defs.insert(a); + }; + + addUse(instr->getArg1()); + addUse(instr->getArg2()); + addDef(instr->getResult()); + } + + for (auto* a : uses) + if (a && (a->getType() == SAPFOR::CFG_ARG_TYPE::ARRAY || a->getType() == SAPFOR::CFG_ARG_TYPE::RECORD)) + return false; + for (auto* a : defs) + if (a && (a->getType() == SAPFOR::CFG_ARG_TYPE::ARRAY || a->getType() == SAPFOR::CFG_ARG_TYPE::RECORD)) + return false; + + if (spansMultipleBB) + return false; + + if (defs.empty()) + return false; + + return true; +} static bool isStatementEmbedded(SgStatement* stmt, SgStatement* parent) { if (!stmt || !parent || stmt == parent) @@ -154,575 +252,428 @@ static bool canSafelyExtract(SgStatement* stmt, SgForStmt* loop) { return true; } -static vector analyzeOperatorsInLoop(SgForStmt* loop, const vector& blocks, - const map>& FullIR) { - vector operators; +namespace +{ + struct StmtInfo + { + SgStatement* stmt = nullptr; + SAPFOR::BasicBlock* bb = nullptr; + int firstInstr = std::numeric_limits::max(); + int lastInstr = std::numeric_limits::min(); + vector irs; + set uses; + set defs; + }; + + using RDState = map>; + + static void killGlobalsAndParams(RDState& st) + { + for (auto it = st.begin(); it != st.end();) + { + SAPFOR::Argument* a = it->first; + if (!a) + { + it = st.erase(it); + continue; + } + + const bool kill = a->isMemGlobal() || a->isParameter(); + if (kill) + it = st.erase(it); + else + ++it; + } + } + + static void transferRD(RDState& st, const SAPFOR::Instruction* instr) + { + if (!instr) + return; + + if (isBarrierIR(instr)) + { + killGlobalsAndParams(st); + return; + } + + using SAPFOR::CFG_OP; + const auto op = instr->getOperation(); + if (op == CFG_OP::ASSIGN || op == CFG_OP::LOAD) + { + SAPFOR::Argument* res = instr->getResult(); + if (res && res->getType() == SAPFOR::CFG_ARG_TYPE::VAR) + st[res] = { instr->getNumber() }; + } + } + + static RDState computeRD_BeforeInstr(const SAPFOR::BasicBlock* bb, int beforeInstrNum) + { + RDState st; + if (!bb) + return st; + + st = bb->getRD_In(); + for (auto* ir : bb->getInstructions()) + { + if (!ir || !ir->getInstruction()) + continue; + if (ir->getNumber() >= beforeInstrNum) + break; + transferRD(st, ir->getInstruction()); + } + return st; + } + + static bool topoSchedule(const vector& original, + const vector>& succ, + const vector& indegInit, + vector& scheduled) + { + const int n = (int)original.size(); + scheduled.clear(); + scheduled.reserve(n); + + vector indeg = indegInit; + vector maxPredPos(n, 0); + + struct Node + { + int idx; + int keyMaxPredPos; + }; + struct Cmp + { + bool operator()(const Node& a, const Node& b) const + { + if (a.keyMaxPredPos != b.keyMaxPredPos) + return a.keyMaxPredPos < b.keyMaxPredPos; + return a.idx > b.idx; + } + }; + + std::priority_queue, Cmp> ready; + for (int i = 0; i < n; ++i) + if (indeg[i] == 0) + ready.push(Node{ i, 0 }); + + int outPos = 0; + while (!ready.empty()) + { + Node cur = ready.top(); + ready.pop(); + + const int u = cur.idx; + scheduled.push_back(original[u]); + ++outPos; + + for (int v : succ[u]) + { + maxPredPos[v] = std::max(maxPredPos[v], outPos); + if (--indeg[v] == 0) + ready.push(Node{ v, maxPredPos[v] }); + } + } + + return (int)scheduled.size() == n; + } + + static bool applyReorderContiguous(SgForStmt* loop, + const vector& oldOrder, + const vector& newOrder) + { + if (!loop || oldOrder.size() != newOrder.size() || oldOrder.size() < 2) + return false; + + bool changed = false; + for (size_t i = 0; i < oldOrder.size(); ++i) + if (oldOrder[i] != newOrder[i]) + { + changed = true; + break; + } + if (!changed) + return false; + + SgStatement* loopStart = loop->lexNext(); + SgStatement* loopEnd = loop->lastNodeOfStmt(); + if (!loopStart || !loopEnd) + return false; + + SgStatement* first = oldOrder.front(); + SgStatement* anchor = loop; + for (SgStatement* cur = loopStart; cur && cur != loopEnd; cur = cur->lexNext()) + { + if (cur == first) + break; + anchor = cur; + } + + map savedLine; + map savedComments; + map extracted; + + for (SgStatement* st : oldOrder) + { + if (!st || st == loop || st == loopEnd) + return false; + if (!canSafelyExtract(st, loop)) + return false; + + savedLine[st] = st->lineNumber(); + savedComments[st] = st->comments() ? strdup(st->comments()) : nullptr; + + SgStatement* ex = st->extractStmt(); + if (!ex) + return false; + extracted[st] = ex; + } + + SgStatement* insertAfter = anchor; + for (SgStatement* st : newOrder) + { + SgStatement* ex = extracted[st]; + if (!ex) + continue; + + auto itC = savedComments.find(st); + if (itC != savedComments.end() && itC->second) + ex->setComments(itC->second); + + auto itL = savedLine.find(st); + if (itL != savedLine.end()) + ex->setlineNumber(itL->second); + + insertAfter->insertStmtAfter(*ex, *loop); + insertAfter = ex; + } + + for (auto& kv : savedComments) + if (kv.second) + free(kv.second); + + return true; + } +} + +static bool reorderMovableRegionsInLoop(SgForStmt* loop, const vector& blocks) +{ + if (!loop) + return false; + SgStatement* loopStart = loop->lexNext(); SgStatement* loopEnd = loop->lastNodeOfStmt(); - if (!loopStart || !loopEnd) - return operators; - + return false; + + bool anyChange = false; + + vector region; + auto flushRegion = [&]() { + if (region.size() < 2) + { + region.clear(); + return; + } + + vector infos; + infos.reserve(region.size()); + map idxOf; + + map defInstrToIdx; + + for (int i = 0; i < (int)region.size(); ++i) + { + SgStatement* st = region[i]; + idxOf[st] = i; + + StmtInfo info; + info.stmt = st; + info.irs = findInstructionsFromStatement(st, blocks); + if (info.irs.empty()) + { + infos.clear(); + region.clear(); + return; + } + + info.bb = info.irs.front()->getBasicBlock(); + for (auto* ir : info.irs) + { + info.firstInstr = std::min(info.firstInstr, ir->getNumber()); + info.lastInstr = std::max(info.lastInstr, ir->getNumber()); + + const auto* instr = ir->getInstruction(); + if (!instr) + continue; + + if (instr->getResult() && instr->getResult()->getType() == SAPFOR::CFG_ARG_TYPE::VAR && + (instr->getOperation() == SAPFOR::CFG_OP::ASSIGN || instr->getOperation() == SAPFOR::CFG_OP::LOAD)) + { + defInstrToIdx[instr->getNumber()] = i; + } + } + + bool spansMultiple = false; + if (!isMovableStmtIR(info.irs, info.uses, info.defs, spansMultiple) || spansMultiple) + { + infos.clear(); + region.clear(); + return; + } + + infos.push_back(std::move(info)); + } + + const int n = (int)infos.size(); + vector> succ(n); + vector indeg(n, 0); + + auto addEdge = [&](int u, int v) { + if (u == v) + return; + succ[u].push_back(v); + indeg[v]++; + }; + + for (int i = 0; i < n; ++i) + { + const StmtInfo& cur = infos[i]; + RDState stRD = computeRD_BeforeInstr(cur.bb, cur.firstInstr); + + for (SAPFOR::Argument* use : cur.uses) + { + if (!use || use->getType() != SAPFOR::CFG_ARG_TYPE::VAR) + continue; + + auto it = stRD.find(use); + if (it == stRD.end()) + continue; + + for (int defInstr : it->second) + { + if (defInstr < 0) + continue; + + auto itDef = defInstrToIdx.find(defInstr); + if (itDef != defInstrToIdx.end()) + addEdge(itDef->second, i); + } + } + } + + map> defsByVar; + for (int i = 0; i < n; ++i) + for (auto* d : infos[i].defs) + if (d && d->getType() == SAPFOR::CFG_ARG_TYPE::VAR) + defsByVar[d].push_back(i); + + for (auto& kv : defsByVar) + { + auto& list = kv.second; + sort(list.begin(), list.end()); + list.erase(unique(list.begin(), list.end()), list.end()); + for (int k = 0; k + 1 < (int)list.size(); ++k) + addEdge(list[k], list[k + 1]); + } + + map> defPositions; + for (auto& kv : defsByVar) + defPositions[kv.first] = kv.second; + + for (int i = 0; i < n; ++i) + { + for (SAPFOR::Argument* use : infos[i].uses) + { + if (!use || use->getType() != SAPFOR::CFG_ARG_TYPE::VAR) + continue; + + auto itDefs = defPositions.find(use); + if (itDefs == defPositions.end()) + continue; + + const auto& dpos = itDefs->second; + auto it = std::upper_bound(dpos.begin(), dpos.end(), i); + if (it != dpos.end()) + addEdge(i, *it); + } + } + + for (int u = 0; u < n; ++u) + { + auto& out = succ[u]; + sort(out.begin(), out.end()); + out.erase(unique(out.begin(), out.end()), out.end()); + } + fill(indeg.begin(), indeg.end(), 0); + for (int u = 0; u < n; ++u) + for (int v : succ[u]) + indeg[v]++; + + vector scheduled; + vector original = region; + if (!topoSchedule(original, succ, indeg, scheduled)) + { + region.clear(); + return; + } + + if (applyReorderContiguous(loop, original, scheduled)) + anyChange = true; + + region.clear(); + }; + SgStatement* current = loopStart; - unordered_set visited; - - while (current && current != loopEnd) { - - if (visited.find(current) != visited.end()) + set visited; + + while (current && current != loopEnd) + { + if (!visited.insert(current).second) break; - visited.insert(current); - - if (isLoopBoundary(current)) { + if (isLoopBoundary(current)) + { + flushRegion(); current = current->lexNext(); continue; } - - if (current->variant() == FOR_NODE && current != loop) { + + if (current->variant() == FOR_NODE && current != loop) + { + flushRegion(); SgStatement* nestedEnd = current->lastNodeOfStmt(); - if (nestedEnd) - current = nestedEnd->lexNext(); - else - current = current->lexNext(); + current = nestedEnd ? nestedEnd->lexNext() : current->lexNext(); continue; } - - if (isSgExecutableStatement(current)) { - if (control_tags.find(current->variant()) != control_tags.end()) { + + if (!isSgExecutableStatement(current) || control_tags.count(current->variant())) + { + flushRegion(); + current = current->lexNext(); + continue; + } + + const bool isTopLevel = (current->controlParent() == loop); + if (isTopLevel && current->variant() == ASSIGN_STAT && canSafelyExtract(current, loop)) + { + auto irs = findInstructionsFromStatement(current, blocks); + set uses, defs; + bool spansMultiple = false; + if (isMovableStmtIR(irs, uses, defs, spansMultiple) && !spansMultiple) + { + region.push_back(current); current = current->lexNext(); continue; } - - if (current->variant() != ASSIGN_STAT) { - current = current->lexNext(); - continue; - } - - OperatorInfo opInfo(current); - - vector irBlocks = findInstructionsFromOperator(current, blocks); - for (auto irBlock : irBlocks) { - if (!irBlock || !irBlock->getInstruction()) - continue; - - const SAPFOR::Instruction* instr = irBlock->getInstruction(); - if (instr->getArg1()) { - string varName = getNameByArg(instr->getArg1()); - if (!varName.empty()) - opInfo.usedVars.insert(varName); - } - - if (instr->getArg2()) { - string varName = getNameByArg(instr->getArg2()); - if (!varName.empty()) - opInfo.usedVars.insert(varName); - } - - if (instr->getResult()) { - string varName = getNameByArg(instr->getResult()); - if (!varName.empty()) - opInfo.definedVars.insert(varName); - } - } - - operators.push_back(opInfo); } + + flushRegion(); current = current->lexNext(); } - - return operators; -} -static map> findVariableDefinitions(SgForStmt* loop, vector& operators) { - map> varDefinitions; - for (auto& op : operators) - for (const string& var : op.definedVars) - varDefinitions[var].push_back(op.stmt); - - return varDefinitions; -} - -static int calculateDistance(SgStatement* from, SgStatement* to) { - if (!from || !to) - return INT_MAX; - - return abs(to->lineNumber() - from->lineNumber()); -} - -static SgStatement* findBestPosition(SgStatement* operatorStmt, const vector& operators, - const map>& varDefinitions, SgForStmt* loop) { - const OperatorInfo* opInfo = nullptr; - for (auto& op : operators) { - if (op.stmt == operatorStmt) { - opInfo = &op; - break; - } - } - - if (!opInfo || !opInfo->isMovable) - return nullptr; - - SgStatement* bestPos = nullptr; - int bestLine = -1; - - for (const string& usedVar : opInfo->usedVars) { - if (varDefinitions.find(usedVar) != varDefinitions.end()) { - for (SgStatement* defStmt : varDefinitions.at(usedVar)) { - 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()) { - for (SgStatement* defStmt : varDefinitions.at(usedVar)) { - if (defStmt == operatorStmt) - continue; - - hasAnyDefinition = true; - if (defStmt->lineNumber() < operatorStmt->lineNumber() && - defStmt->controlParent() == operatorStmt->controlParent()) { - allLoopCarried = false; - break; - } - } - } - - if (!allLoopCarried) - break; - } - - if (allLoopCarried || (!hasAnyDefinition && !opInfo->usedVars.empty())) { - SgStatement* loopStart = loop->lexNext(); - return loopStart; - } - } - - return bestPos; -} - -static bool canMoveTo(SgStatement* from, SgStatement* to, SgForStmt* loop) { - if (!from || !to || from == to) - return false; - - SgStatement* loopStart = loop->lexNext(); - SgStatement* loopEnd = loop->lastNodeOfStmt(); - - if (!loopStart || !loopEnd) - return false; - - if (to == loopStart) { - SgStatement* fromControlParent = from->controlParent(); - if (!fromControlParent) fromControlParent = loop; - return fromControlParent == loop || fromControlParent == loopStart->controlParent(); - } - - if (from->lineNumber() < loopStart->lineNumber() || from->lineNumber() > loopEnd->lineNumber()) - return false; - - if (to->lineNumber() < loopStart->lineNumber() || to->lineNumber() > loopEnd->lineNumber()) - return false; - - if (to->lineNumber() >= from->lineNumber()) - return false; - - if (from->controlParent() != to->controlParent()) - return false; - - SgStatement* current = to->lexNext(); - while (current && current != from && current != loopEnd) { - if (control_tags.find(current->variant()) != control_tags.end()) { - SgStatement* controlEnd = current->lastNodeOfStmt(); - if (controlEnd && from->lineNumber() <= controlEnd->lineNumber()) { - if (from->controlParent() == current && to->controlParent() != current) { - return false; - } - } - } - current = current->lexNext(); - } - - return true; -} - -static vector optimizeOperatorOrder(SgForStmt* loop, - const vector& operators, - const map>& varDefinitions) { - vector newOrder; - for (auto& op : operators) - newOrder.push_back(op.stmt); - - map stmtToOpInfo; - for (auto& op : operators) - stmtToOpInfo[op.stmt] = &op; - - bool changed = true; - - while (changed) { - changed = false; - - for (int i = operators.size() - 1; i >= 0; i--) { - if (!operators[i].isMovable) - continue; - - SgStatement* stmt = operators[i].stmt; - const OperatorInfo* opInfo = stmtToOpInfo[stmt]; - - if (!opInfo) - continue; - - size_t currentPos = 0; - for (size_t j = 0; j < newOrder.size(); j++) { - if (newOrder[j] == stmt) { - currentPos = j; - break; - } - } - - 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]; - const OperatorInfo* candidateOpInfo = stmtToOpInfo[candidate]; - if (candidateOpInfo) { - for (const string& definedVar : opInfo->definedVars) { - if (candidateOpInfo->usedVars.find(definedVar) != candidateOpInfo->usedVars.end()) { - hasDependents = true; - break; - } - } - - if (hasDependents) - break; - } - } - - 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]; - const OperatorInfo* candidateOpInfo = stmtToOpInfo[candidate]; - if (candidateOpInfo) { - bool usesDefinedVar = false; - for (const string& definedVar : opInfo->definedVars) { - if (candidateOpInfo->usedVars.find(definedVar) != candidateOpInfo->usedVars.end()) { - usesDefinedVar = true; - break; - } - } - - 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]; - const OperatorInfo* candidateOpInfo = stmtToOpInfo[candidate]; - if (candidateOpInfo) { - bool definesUsedVar = false; - for (const string& usedVar : opInfo->usedVars) { - if (candidateOpInfo->definedVars.find(usedVar) != candidateOpInfo->definedVars.end()) { - definesUsedVar = true; - break; - } - } - if (definesUsedVar) - targetPos = j + 1; - } - } - - bool wouldBreakDependency = false; - for (size_t j = targetPos; j < currentPos && j < newOrder.size(); j++) { - SgStatement* candidate = newOrder[j]; - const OperatorInfo* candidateOpInfo = stmtToOpInfo[candidate]; - if (candidateOpInfo) { - for (const string& definedVar : opInfo->definedVars) { - if (candidateOpInfo->usedVars.find(definedVar) != candidateOpInfo->usedVars.end()) { - wouldBreakDependency = true; - break; - } - } - - if (wouldBreakDependency) - break; - } - } - - if (!wouldBreakDependency && currentPos > targetPos && canMoveTo(stmt, bestPos, loop)) { - newOrder.erase(newOrder.begin() + currentPos); - newOrder.insert(newOrder.begin() + targetPos, stmt); - changed = true; - } - } - } - } - } - - bool dependencyViolation = true; - set> triedPairs; - - while (dependencyViolation) { - dependencyViolation = false; - triedPairs.clear(); - - for (size_t i = 0; i < newOrder.size(); i++) { - SgStatement* stmt = newOrder[i]; - const OperatorInfo* opInfo = stmtToOpInfo[stmt]; - if (!opInfo) - continue; - - for (size_t j = 0; j < i; j++) { - SgStatement* prevStmt = newOrder[j]; - const OperatorInfo* prevOpInfo = stmtToOpInfo[prevStmt]; - if (!prevOpInfo) - continue; - - pair key = make_pair(stmt, prevStmt); - if (triedPairs.find(key) != triedPairs.end()) - 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]; - const OperatorInfo* betweenOpInfo = stmtToOpInfo[betweenStmt]; - - if (!betweenOpInfo) - continue; - - for (const string& usedVar : opInfo->usedVars) { - if (betweenOpInfo->definedVars.find(usedVar) != betweenOpInfo->definedVars.end()) { - wouldCreateViolation = true; - break; - } - } - - if (wouldCreateViolation) - break; - } - - if (!wouldCreateViolation) { - newOrder.erase(newOrder.begin() + i); - newOrder.insert(newOrder.begin() + j, stmt); - dependencyViolation = true; - break; - } - } - } - - if (dependencyViolation) - break; - } - } - - return newOrder; -} - -static bool applyOperatorReordering(SgForStmt* loop, const vector& newOrder) { - if (!loop || newOrder.empty()) - return false; - - SgStatement* loopStart = loop->lexNext(); - SgStatement* loopEnd = loop->lastNodeOfStmt(); - - if (!loopStart || !loopEnd) - return false; - - vector originalOrder; - SgStatement* current = loopStart; - while (current && current != loopEnd) { - if (isSgExecutableStatement(current) && current->variant() == ASSIGN_STAT) - originalOrder.push_back(current); - - 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; - } - } - } - else - orderChanged = true; - - if (!orderChanged) - return false; - - vector extractedStatements; - vector savedComments; - unordered_set extractedSet; - map originalLineNumbers; - map stmtToExtracted; - - for (SgStatement* stmt : newOrder) { - if (stmt && stmt != loop && stmt != loopEnd && extractedSet.find(stmt) == extractedSet.end()) { - if (control_tags.find(stmt->variant()) != control_tags.end()) - continue; - - 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; - } - } - - if (!isMoving) - continue; - - originalLineNumbers[stmt] = stmt->lineNumber(); - savedComments.push_back(stmt->comments() ? strdup(stmt->comments()) : nullptr); - SgStatement* extracted = stmt->extractStmt(); - if (extracted) { - extractedStatements.push_back(extracted); - extractedSet.insert(stmt); - stmtToExtracted[stmt] = extracted; - } - } - } - - map insertedStatements; - - 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; - - 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; - } - } - - size_t commentIdx = 0; - for (size_t i = 0; i < extractedStatements.size(); i++) { - if (extractedStatements[i] == stmtToInsert) { - commentIdx = i; - break; - } - } - - if (commentIdx < savedComments.size() && savedComments[commentIdx]) - stmtToInsert->setComments(savedComments[commentIdx]); - - if (originalLineNumbers.find(stmt) != originalLineNumbers.end()) - stmtToInsert->setlineNumber(originalLineNumbers[stmt]); - - SgStatement* controlParent = stmt->controlParent(); - if (!controlParent) - controlParent = loop; - - insertAfter->insertStmtAfter(*stmtToInsert, *controlParent); - insertedStatements[stmt] = stmtToInsert; - } - } - - for (char* comment : savedComments) { - if (comment) - free(comment); - } - - return true; + flushRegion(); + return anyChange; } vector findFuncBlocksByFuncStatement(SgStatement *st, const map>& FullIR) { @@ -748,10 +699,10 @@ map> findAndAnalyzeLoops(SgStatement *st SgForStmt *forSt = (SgForStmt*)st; SgStatement *loopBody = forSt -> body(); SgStatement *lastLoopNode = st->lastNodeOfStmt(); - unordered_set blocks_nums; + set blocks_nums; while (loopBody && loopBody != lastLoopNode) { - vector irBlocks = findInstructionsFromOperator(loopBody, blocks); + vector irBlocks = findInstructionsFromStatement(loopBody, blocks); if (!irBlocks.empty()) { SAPFOR::IR_Block* IR = irBlocks.front(); if (IR && IR->getBasicBlock()) { @@ -794,24 +745,17 @@ static void processLoopRecursively(SgForStmt* loop, const vectorlexNext(); } } - - vector operators = analyzeOperatorsInLoop(loop, blocks, FullIR); - if (!operators.empty()) { - map> varDefinitions = findVariableDefinitions(loop, operators); - vector newOrder = optimizeOperatorOrder(loop, operators, varDefinitions); - applyOperatorReordering(loop, newOrder); - } + + reorderMovableRegionsInLoop(loop, blocks); } void moveOperators(SgFile *file, map>& loopGraph, const map>& FullIR, int& countOfTransform) { countOfTransform += 1; - - //cout << "MOVE_OPERATORS Pass Started" << endl; const int funcNum = file -> numberOfFunctions(); - + for (int i = 0; i < funcNum; ++i) { SgStatement *st = file -> functions(i); @@ -821,6 +765,4 @@ void moveOperators(SgFile *file, map>& loopGraph, for (auto& loopForAnalyze: loopsMapping) processLoopRecursively(loopForAnalyze.first, loopForAnalyze.second, FullIR); } - - //cout << "MOVE_OPERATORS Pass Completed" << endl; } \ No newline at end of file