2025-03-25 15:18:49 +03:00
|
|
|
#include <map>
|
2026-02-19 09:21:10 +03:00
|
|
|
#include <set>
|
2025-03-25 15:18:49 +03:00
|
|
|
#include <vector>
|
|
|
|
|
#include <queue>
|
|
|
|
|
#include <iostream>
|
2025-10-23 14:54:43 +03:00
|
|
|
#include <algorithm>
|
2026-02-19 09:21:10 +03:00
|
|
|
#include <limits>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
#include <cstdlib>
|
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
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
string getNameByArg(SAPFOR::Argument* arg);
|
|
|
|
|
|
|
|
|
|
static vector<SAPFOR::IR_Block*> findInstructionsFromStatement(SgStatement* st, const vector<SAPFOR::BasicBlock*>& blocks)
|
|
|
|
|
{
|
2025-05-13 00:46:32 +03:00
|
|
|
vector<SAPFOR::IR_Block*> result;
|
2026-02-19 09:21:10 +03:00
|
|
|
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;
|
2025-12-29 21:10:55 +03:00
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
SgStatement* op = ir->getInstruction()->getOperator();
|
|
|
|
|
if (op && op->id() == stmtId)
|
|
|
|
|
result.push_back(ir);
|
2025-05-13 00:46:32 +03:00
|
|
|
}
|
|
|
|
|
}
|
2026-02-19 09:21:10 +03:00
|
|
|
|
|
|
|
|
sort(result.begin(), result.end(),
|
|
|
|
|
[](const SAPFOR::IR_Block* a, const SAPFOR::IR_Block* b) { return a->getNumber() < b->getNumber(); });
|
2025-05-13 00:46:32 +03:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
set<int> loop_tags = {FOR_NODE};
|
|
|
|
|
set<int> control_tags = {IF_NODE, ELSEIF_NODE, DO_WHILE_NODE, WHILE_NODE, LOGIF_NODE};
|
|
|
|
|
set<int> control_end_tags = {CONTROL_END};
|
2025-05-13 00:46:32 +03:00
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
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<SAPFOR::IR_Block*>& irs,
|
|
|
|
|
set<SAPFOR::Argument*>& uses,
|
|
|
|
|
set<SAPFOR::Argument*>& 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;
|
|
|
|
|
}
|
2025-05-13 00:46:32 +03:00
|
|
|
|
2025-12-11 01:41:58 +03:00
|
|
|
static bool isStatementEmbedded(SgStatement* stmt, SgStatement* parent) {
|
2025-12-29 21:10:55 +03:00
|
|
|
if (!stmt || !parent || stmt == parent)
|
|
|
|
|
return false;
|
2025-12-11 01:41:58 +03:00
|
|
|
|
|
|
|
|
if (parent->variant() == LOGIF_NODE) {
|
2025-12-29 21:10:55 +03:00
|
|
|
if (stmt->lineNumber() == parent->lineNumber())
|
2025-12-11 01:41:58 +03:00
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
SgStatement* current = parent;
|
|
|
|
|
SgStatement* lastNode = parent->lastNodeOfStmt();
|
|
|
|
|
|
|
|
|
|
while (current && current != lastNode) {
|
2025-12-29 21:10:55 +03:00
|
|
|
if (current == stmt)
|
2025-12-11 01:41:58 +03:00
|
|
|
return true;
|
2025-12-29 21:10:55 +03:00
|
|
|
|
|
|
|
|
if (current->isIncludedInStmt(*stmt))
|
2025-12-11 01:41:58 +03:00
|
|
|
return true;
|
2025-12-29 21:10:55 +03:00
|
|
|
|
2025-12-11 01:41:58 +03:00
|
|
|
current = current->lexNext();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-29 21:10:55 +03:00
|
|
|
if (parent->isIncludedInStmt(*stmt))
|
2025-12-11 01:41:58 +03:00
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool isLoopBoundary(SgStatement* stmt) {
|
2025-12-29 21:10:55 +03:00
|
|
|
if (!stmt)
|
|
|
|
|
return false;
|
2025-12-11 01:41:58 +03:00
|
|
|
|
2025-12-29 21:10:55 +03:00
|
|
|
if (stmt->variant() == FOR_NODE || stmt->variant() == CONTROL_END)
|
2025-12-11 01:41:58 +03:00
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool isPartOfNestedLoop(SgStatement* stmt, SgForStmt* loop) {
|
2025-12-29 21:10:55 +03:00
|
|
|
if (!stmt || !loop)
|
|
|
|
|
return false;
|
2025-12-11 01:41:58 +03:00
|
|
|
|
|
|
|
|
SgStatement* loopStart = loop->lexNext();
|
|
|
|
|
SgStatement* loopEnd = loop->lastNodeOfStmt();
|
2025-12-29 21:10:55 +03:00
|
|
|
if (!loopStart || !loopEnd)
|
|
|
|
|
return false;
|
2025-12-11 01:41:58 +03:00
|
|
|
|
2025-12-29 21:10:55 +03:00
|
|
|
if (stmt->lineNumber() < loopStart->lineNumber() || stmt->lineNumber() > loopEnd->lineNumber())
|
2025-12-11 01:41:58 +03:00
|
|
|
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-11 01:41:58 +03:00
|
|
|
|
2025-12-29 21:10:55 +03:00
|
|
|
if (isLoopBoundary(stmt))
|
2025-12-11 01:41:58 +03:00
|
|
|
return false;
|
|
|
|
|
|
2025-12-29 21:10:55 +03:00
|
|
|
if (control_tags.find(stmt->variant()) != control_tags.end())
|
2025-12-11 01:41:58 +03:00
|
|
|
return false;
|
|
|
|
|
|
2025-12-29 21:10:55 +03:00
|
|
|
if (isPartOfNestedLoop(stmt, loop))
|
2025-12-11 01:41:58 +03:00
|
|
|
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-11 01:41:58 +03:00
|
|
|
|
|
|
|
|
SgStatement* current = loopStart;
|
|
|
|
|
|
|
|
|
|
while (current && current != loopEnd) {
|
2025-12-29 21:10:55 +03:00
|
|
|
if (current->variant() == LOGIF_NODE && current->lineNumber() == stmt->lineNumber())
|
2025-12-11 01:41:58 +03:00
|
|
|
return false;
|
|
|
|
|
|
2025-12-29 21:10:55 +03:00
|
|
|
if (control_tags.find(current->variant()) != control_tags.end())
|
|
|
|
|
if (isStatementEmbedded(stmt, current))
|
2025-12-11 01:41:58 +03:00
|
|
|
return false;
|
2025-12-29 21:10:55 +03:00
|
|
|
|
|
|
|
|
if (current == stmt)
|
|
|
|
|
break;
|
2025-12-11 01:41:58 +03:00
|
|
|
current = current->lexNext();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
struct StmtInfo
|
|
|
|
|
{
|
|
|
|
|
SgStatement* stmt = nullptr;
|
|
|
|
|
SAPFOR::BasicBlock* bb = nullptr;
|
|
|
|
|
int firstInstr = std::numeric_limits<int>::max();
|
|
|
|
|
int lastInstr = std::numeric_limits<int>::min();
|
|
|
|
|
vector<SAPFOR::IR_Block*> irs;
|
|
|
|
|
set<SAPFOR::Argument*> uses;
|
|
|
|
|
set<SAPFOR::Argument*> defs;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using RDState = map<SAPFOR::Argument*, set<int>>;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2025-12-29 21:10:55 +03:00
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
const bool kill = a->isMemGlobal() || a->isParameter();
|
|
|
|
|
if (kill)
|
|
|
|
|
it = st.erase(it);
|
2025-12-29 21:10:55 +03:00
|
|
|
else
|
2026-02-19 09:21:10 +03:00
|
|
|
++it;
|
2025-12-11 01:41:58 +03:00
|
|
|
}
|
2026-02-19 09:21:10 +03:00
|
|
|
}
|
2025-12-29 21:10:55 +03:00
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
static void transferRD(RDState& st, const SAPFOR::Instruction* instr)
|
|
|
|
|
{
|
|
|
|
|
if (!instr)
|
|
|
|
|
return;
|
2025-12-29 21:10:55 +03:00
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
if (isBarrierIR(instr))
|
|
|
|
|
{
|
|
|
|
|
killGlobalsAndParams(st);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-12-29 21:10:55 +03:00
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
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() };
|
2025-05-22 22:41:09 +03:00
|
|
|
}
|
|
|
|
|
}
|
2025-05-13 00:46:32 +03:00
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
static RDState computeRD_BeforeInstr(const SAPFOR::BasicBlock* bb, int beforeInstrNum)
|
|
|
|
|
{
|
|
|
|
|
RDState st;
|
|
|
|
|
if (!bb)
|
|
|
|
|
return st;
|
2025-12-29 21:22:53 +03:00
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
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;
|
|
|
|
|
}
|
2025-05-24 19:56:15 +03:00
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
static bool topoSchedule(const vector<SgStatement*>& original,
|
|
|
|
|
const vector<vector<int>>& succ,
|
|
|
|
|
const vector<int>& indegInit,
|
|
|
|
|
vector<SgStatement*>& scheduled)
|
|
|
|
|
{
|
|
|
|
|
const int n = (int)original.size();
|
|
|
|
|
scheduled.clear();
|
|
|
|
|
scheduled.reserve(n);
|
2025-12-29 21:10:55 +03:00
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
vector<int> indeg = indegInit;
|
|
|
|
|
vector<int> maxPredPos(n, 0);
|
2025-05-24 19:56:15 +03:00
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-12-29 21:10:55 +03:00
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
std::priority_queue<Node, vector<Node>, 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] });
|
2025-12-25 15:01:01 +03:00
|
|
|
}
|
|
|
|
|
}
|
2026-02-19 09:21:10 +03:00
|
|
|
|
|
|
|
|
return (int)scheduled.size() == n;
|
2025-12-25 15:01:01 +03:00
|
|
|
}
|
2026-02-19 09:21:10 +03:00
|
|
|
|
|
|
|
|
static bool applyReorderContiguous(SgForStmt* loop,
|
|
|
|
|
const vector<SgStatement*>& oldOrder,
|
|
|
|
|
const vector<SgStatement*>& 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;
|
2025-05-24 19:56:15 +03:00
|
|
|
}
|
2026-02-19 09:21:10 +03:00
|
|
|
if (!changed)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
SgStatement* loopStart = loop->lexNext();
|
|
|
|
|
SgStatement* loopEnd = loop->lastNodeOfStmt();
|
|
|
|
|
if (!loopStart || !loopEnd)
|
|
|
|
|
return false;
|
2025-12-29 21:10:55 +03:00
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
SgStatement* first = oldOrder.front();
|
|
|
|
|
SgStatement* anchor = loop;
|
|
|
|
|
for (SgStatement* cur = loopStart; cur && cur != loopEnd; cur = cur->lexNext())
|
|
|
|
|
{
|
|
|
|
|
if (cur == first)
|
2025-12-29 21:10:55 +03:00
|
|
|
break;
|
2026-02-19 09:21:10 +03:00
|
|
|
anchor = cur;
|
2025-12-25 15:01:01 +03:00
|
|
|
}
|
2026-02-19 09:21:10 +03:00
|
|
|
|
|
|
|
|
map<SgStatement*, int> savedLine;
|
|
|
|
|
map<SgStatement*, char*> savedComments;
|
|
|
|
|
map<SgStatement*, SgStatement*> 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;
|
2025-05-24 19:56:15 +03:00
|
|
|
}
|
2026-02-19 09:21:10 +03:00
|
|
|
|
|
|
|
|
for (auto& kv : savedComments)
|
|
|
|
|
if (kv.second)
|
|
|
|
|
free(kv.second);
|
|
|
|
|
|
|
|
|
|
return true;
|
2025-05-22 22:41:09 +03:00
|
|
|
}
|
2025-05-24 19:56:15 +03:00
|
|
|
}
|
|
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
static bool reorderMovableRegionsInLoop(SgForStmt* loop, const vector<SAPFOR::BasicBlock*>& blocks)
|
|
|
|
|
{
|
|
|
|
|
if (!loop)
|
2025-12-29 21:10:55 +03:00
|
|
|
return false;
|
2026-02-19 09:21:10 +03:00
|
|
|
|
2025-10-08 23:14:19 +03:00
|
|
|
SgStatement* loopStart = loop->lexNext();
|
|
|
|
|
SgStatement* loopEnd = loop->lastNodeOfStmt();
|
2026-02-19 09:21:10 +03:00
|
|
|
if (!loopStart || !loopEnd)
|
2025-12-25 15:01:01 +03:00
|
|
|
return false;
|
2025-12-29 21:10:55 +03:00
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
bool anyChange = false;
|
|
|
|
|
|
|
|
|
|
vector<SgStatement*> region;
|
|
|
|
|
auto flushRegion = [&]() {
|
|
|
|
|
if (region.size() < 2)
|
|
|
|
|
{
|
|
|
|
|
region.clear();
|
|
|
|
|
return;
|
2025-10-23 14:54:43 +03:00
|
|
|
}
|
2025-10-08 23:14:19 +03:00
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
vector<StmtInfo> infos;
|
|
|
|
|
infos.reserve(region.size());
|
|
|
|
|
map<SgStatement*, int> idxOf;
|
2025-12-29 21:10:55 +03:00
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
map<int, int> 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;
|
2025-10-08 23:14:19 +03:00
|
|
|
}
|
2025-12-29 21:10:55 +03:00
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
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;
|
2025-12-25 15:01:01 +03:00
|
|
|
}
|
|
|
|
|
}
|
2025-12-29 21:22:53 +03:00
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
bool spansMultiple = false;
|
|
|
|
|
if (!isMovableStmtIR(info.irs, info.uses, info.defs, spansMultiple) || spansMultiple)
|
|
|
|
|
{
|
|
|
|
|
infos.clear();
|
|
|
|
|
region.clear();
|
|
|
|
|
return;
|
2025-10-23 14:54:43 +03:00
|
|
|
}
|
2026-02-19 09:21:10 +03:00
|
|
|
|
|
|
|
|
infos.push_back(std::move(info));
|
2025-10-08 23:14:19 +03:00
|
|
|
}
|
2026-02-19 09:21:10 +03:00
|
|
|
|
|
|
|
|
const int n = (int)infos.size();
|
|
|
|
|
vector<vector<int>> succ(n);
|
|
|
|
|
vector<int> 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)
|
2025-12-29 21:10:55 +03:00
|
|
|
continue;
|
2026-02-19 09:21:10 +03:00
|
|
|
|
|
|
|
|
auto it = stRD.find(use);
|
|
|
|
|
if (it == stRD.end())
|
2025-12-25 15:01:01 +03:00
|
|
|
continue;
|
2025-12-29 21:10:55 +03:00
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
for (int defInstr : it->second)
|
|
|
|
|
{
|
|
|
|
|
if (defInstr < 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
auto itDef = defInstrToIdx.find(defInstr);
|
|
|
|
|
if (itDef != defInstrToIdx.end())
|
|
|
|
|
addEdge(itDef->second, i);
|
2025-12-25 15:01:01 +03:00
|
|
|
}
|
|
|
|
|
}
|
2026-02-19 09:21:10 +03:00
|
|
|
}
|
2025-12-29 21:22:53 +03:00
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
map<SAPFOR::Argument*, vector<int>> 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]);
|
2025-12-25 15:01:01 +03:00
|
|
|
}
|
2025-05-27 15:55:02 +03:00
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
map<SAPFOR::Argument*, vector<int>> defPositions;
|
|
|
|
|
for (auto& kv : defsByVar)
|
|
|
|
|
defPositions[kv.first] = kv.second;
|
2025-12-29 21:10:55 +03:00
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
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);
|
2025-12-11 01:41:58 +03:00
|
|
|
}
|
|
|
|
|
}
|
2025-12-29 21:10:55 +03:00
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
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<SgStatement*> scheduled;
|
|
|
|
|
vector<SgStatement*> original = region;
|
|
|
|
|
if (!topoSchedule(original, succ, indeg, scheduled))
|
|
|
|
|
{
|
|
|
|
|
region.clear();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (applyReorderContiguous(loop, original, scheduled))
|
|
|
|
|
anyChange = true;
|
|
|
|
|
|
|
|
|
|
region.clear();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SgStatement* current = loopStart;
|
|
|
|
|
set<SgStatement*> visited;
|
|
|
|
|
|
|
|
|
|
while (current && current != loopEnd)
|
|
|
|
|
{
|
|
|
|
|
if (!visited.insert(current).second)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (isLoopBoundary(current))
|
|
|
|
|
{
|
|
|
|
|
flushRegion();
|
|
|
|
|
current = current->lexNext();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (current->variant() == FOR_NODE && current != loop)
|
|
|
|
|
{
|
|
|
|
|
flushRegion();
|
|
|
|
|
SgStatement* nestedEnd = current->lastNodeOfStmt();
|
|
|
|
|
current = nestedEnd ? nestedEnd->lexNext() : current->lexNext();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isSgExecutableStatement(current) || control_tags.count(current->variant()))
|
|
|
|
|
{
|
|
|
|
|
flushRegion();
|
|
|
|
|
current = current->lexNext();
|
|
|
|
|
continue;
|
2025-10-08 23:14:19 +03:00
|
|
|
}
|
2025-12-29 21:22:53 +03:00
|
|
|
|
2026-02-19 09:21:10 +03:00
|
|
|
const bool isTopLevel = (current->controlParent() == loop);
|
|
|
|
|
if (isTopLevel && current->variant() == ASSIGN_STAT && canSafelyExtract(current, loop))
|
|
|
|
|
{
|
|
|
|
|
auto irs = findInstructionsFromStatement(current, blocks);
|
|
|
|
|
set<SAPFOR::Argument*> uses, defs;
|
|
|
|
|
bool spansMultiple = false;
|
|
|
|
|
if (isMovableStmtIR(irs, uses, defs, spansMultiple) && !spansMultiple)
|
|
|
|
|
{
|
|
|
|
|
region.push_back(current);
|
|
|
|
|
current = current->lexNext();
|
2025-12-29 21:22:53 +03:00
|
|
|
continue;
|
2025-12-25 15:01:01 +03:00
|
|
|
}
|
2025-10-08 23:14:19 +03:00
|
|
|
}
|
2026-02-19 09:21:10 +03:00
|
|
|
|
|
|
|
|
flushRegion();
|
|
|
|
|
current = current->lexNext();
|
2025-10-08 23:14:19 +03:00
|
|
|
}
|
2026-02-19 09:21:10 +03:00
|
|
|
|
|
|
|
|
flushRegion();
|
|
|
|
|
return anyChange;
|
2025-10-08 23:14:19 +03:00
|
|
|
}
|
|
|
|
|
|
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) {
|
2025-12-29 21:22:53 +03:00
|
|
|
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-12-29 21:22:53 +03:00
|
|
|
}
|
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-12-11 01:41:58 +03:00
|
|
|
|
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();
|
2026-02-19 09:21:10 +03:00
|
|
|
set<int> blocks_nums;
|
2025-12-11 01:41:58 +03:00
|
|
|
|
2025-10-23 14:54:43 +03:00
|
|
|
while (loopBody && loopBody != lastLoopNode) {
|
2026-02-19 09:21:10 +03:00
|
|
|
vector<SAPFOR::IR_Block*> irBlocks = findInstructionsFromStatement(loopBody, blocks);
|
2025-12-11 01:41:58 +03:00
|
|
|
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) {
|
2025-12-29 21:22:53 +03:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-19 09:21:10 +03:00
|
|
|
|
|
|
|
|
reorderMovableRegionsInLoop(loop, blocks);
|
2025-12-25 15:01:01 +03:00
|
|
|
}
|
|
|
|
|
|
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-05-13 00:46:32 +03:00
|
|
|
|
2025-05-27 01:41:50 +03:00
|
|
|
const int funcNum = file -> numberOfFunctions();
|
2026-02-19 09:21:10 +03:00
|
|
|
|
2025-10-23 14:54:43 +03:00
|
|
|
for (int i = 0; i < funcNum; ++i) {
|
2025-05-27 01:41:50 +03:00
|
|
|
SgStatement *st = file -> functions(i);
|
2025-12-11 01:41:58 +03:00
|
|
|
|
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-10-23 14:54:43 +03:00
|
|
|
}
|