egormayorov #72
@@ -26,7 +26,6 @@ static vector<SAPFOR::IR_Block*> findInstructionsFromOperator(SgStatement* st, v
|
|||||||
vector<SAPFOR::IR_Block*> instructionsInBlock = block->getInstructions();
|
vector<SAPFOR::IR_Block*> instructionsInBlock = block->getInstructions();
|
||||||
for (auto& instruction: instructionsInBlock) {
|
for (auto& instruction: instructionsInBlock) {
|
||||||
SgStatement* curOperator = instruction->getInstruction()->getOperator();
|
SgStatement* curOperator = instruction->getInstruction()->getOperator();
|
||||||
// Match by line number to find corresponding IR instruction
|
|
||||||
if (curOperator->lineNumber() == st->lineNumber()) {
|
if (curOperator->lineNumber() == st->lineNumber()) {
|
||||||
result.push_back(instruction);
|
result.push_back(instruction);
|
||||||
}
|
}
|
||||||
@@ -35,9 +34,9 @@ static vector<SAPFOR::IR_Block*> findInstructionsFromOperator(SgStatement* st, v
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
unordered_set<int> loop_tags = {FOR_NODE}; // Loop statements
|
unordered_set<int> loop_tags = {FOR_NODE};
|
||||||
unordered_set<int> control_tags = {IF_NODE, ELSEIF_NODE, DO_WHILE_NODE, WHILE_NODE, LOGIF_NODE}; // Control structures that cannot be moved
|
unordered_set<int> control_tags = {IF_NODE, ELSEIF_NODE, DO_WHILE_NODE, WHILE_NODE, LOGIF_NODE};
|
||||||
unordered_set<int> control_end_tags = {CONTROL_END}; // End marker
|
unordered_set<int> control_end_tags = {CONTROL_END};
|
||||||
|
|
||||||
struct OperatorInfo {
|
struct OperatorInfo {
|
||||||
SgStatement* stmt;
|
SgStatement* stmt;
|
||||||
@@ -254,7 +253,7 @@ static int calculateDistance(SgStatement* from, SgStatement* to) {
|
|||||||
return abs(to->lineNumber() - from->lineNumber());
|
return abs(to->lineNumber() - from->lineNumber());
|
||||||
}
|
}
|
||||||
|
|
||||||
static SgStatement* findBestPosition(SgStatement* operatorStmt, vector<OperatorInfo>& operators, map<string, vector<SgStatement*>>& varDefinitions) {
|
static SgStatement* findBestPosition(SgStatement* operatorStmt, vector<OperatorInfo>& operators, map<string, vector<SgStatement*>>& varDefinitions, SgForStmt* loop) {
|
||||||
OperatorInfo* opInfo = nullptr;
|
OperatorInfo* opInfo = nullptr;
|
||||||
for (auto& op : operators) {
|
for (auto& op : operators) {
|
||||||
if (op.stmt == operatorStmt) {
|
if (op.stmt == operatorStmt) {
|
||||||
@@ -268,20 +267,52 @@ static SgStatement* findBestPosition(SgStatement* operatorStmt, vector<OperatorI
|
|||||||
}
|
}
|
||||||
|
|
||||||
SgStatement* bestPos = nullptr;
|
SgStatement* bestPos = nullptr;
|
||||||
int minDistance = INT_MAX;
|
int bestLine = -1;
|
||||||
|
|
||||||
for (const string& usedVar : opInfo->usedVars) {
|
for (const string& usedVar : opInfo->usedVars) {
|
||||||
if (varDefinitions.find(usedVar) != varDefinitions.end()) {
|
if (varDefinitions.find(usedVar) != varDefinitions.end()) {
|
||||||
for (SgStatement* defStmt : varDefinitions[usedVar]) {
|
for (SgStatement* defStmt : varDefinitions[usedVar]) {
|
||||||
int distance = calculateDistance(operatorStmt, defStmt);
|
if (defStmt->lineNumber() < operatorStmt->lineNumber()) {
|
||||||
if (distance < minDistance) {
|
if (defStmt->controlParent() == operatorStmt->controlParent()) {
|
||||||
minDistance = distance;
|
if (defStmt->lineNumber() > bestLine) {
|
||||||
bestPos = defStmt;
|
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[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;
|
return bestPos;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -293,24 +324,37 @@ static bool canMoveTo(SgStatement* from, SgStatement* to, SgForStmt* loop) {
|
|||||||
|
|
||||||
if (!loopStart || !loopEnd) return false;
|
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()) {
|
if (to->lineNumber() < loopStart->lineNumber() || to->lineNumber() > loopEnd->lineNumber()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
SgStatement* current = from;
|
if (to->lineNumber() >= from->lineNumber()) {
|
||||||
unordered_set<SgStatement*> visited;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
while (current && current != loopEnd) {
|
if (from->controlParent() != to->controlParent()) {
|
||||||
|
return false;
|
||||||
if (visited.find(current) != visited.end()) {
|
}
|
||||||
return false;
|
|
||||||
}
|
|
||||||
visited.insert(current);
|
|
||||||
|
|
||||||
|
SgStatement* current = to->lexNext();
|
||||||
|
while (current && current != from && current != loopEnd) {
|
||||||
if (control_tags.find(current->variant()) != control_tags.end()) {
|
if (control_tags.find(current->variant()) != control_tags.end()) {
|
||||||
return false;
|
SgStatement* controlEnd = current->lastNodeOfStmt();
|
||||||
|
if (controlEnd && from->lineNumber() <= controlEnd->lineNumber()) {
|
||||||
|
if (from->controlParent() == current && to->controlParent() != current) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (current == to) break;
|
|
||||||
current = current->lexNext();
|
current = current->lexNext();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -319,33 +363,204 @@ static bool canMoveTo(SgStatement* from, SgStatement* to, SgForStmt* loop) {
|
|||||||
|
|
||||||
static vector<SgStatement*> optimizeOperatorOrder(SgForStmt* loop, vector<OperatorInfo>& operators, map<string, vector<SgStatement*>>& varDefinitions) {
|
static vector<SgStatement*> optimizeOperatorOrder(SgForStmt* loop, vector<OperatorInfo>& operators, map<string, vector<SgStatement*>>& varDefinitions) {
|
||||||
vector<SgStatement*> newOrder;
|
vector<SgStatement*> newOrder;
|
||||||
vector<bool> moved(operators.size(), false);
|
for (auto& op : operators) {
|
||||||
|
newOrder.push_back(op.stmt);
|
||||||
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < operators.size(); i++) {
|
map<SgStatement*, OperatorInfo*> stmtToOpInfo;
|
||||||
if (moved[i] || !operators[i].isMovable) {
|
for (auto& op : operators) {
|
||||||
newOrder.push_back(operators[i].stmt);
|
stmtToOpInfo[op.stmt] = &op;
|
||||||
moved[i] = true;
|
}
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
SgStatement* bestPos = findBestPosition(operators[i].stmt, operators, varDefinitions);
|
bool changed = true;
|
||||||
|
int iterations = 0;
|
||||||
|
const int MAX_ITERATIONS = 50;
|
||||||
|
|
||||||
if (bestPos && canMoveTo(operators[i].stmt, bestPos, loop)) {
|
while (changed && iterations < MAX_ITERATIONS) {
|
||||||
bool inserted = false;
|
changed = false;
|
||||||
|
iterations++;
|
||||||
|
|
||||||
|
for (int i = operators.size() - 1; i >= 0; i--) {
|
||||||
|
if (!operators[i].isMovable) continue;
|
||||||
|
|
||||||
|
SgStatement* stmt = operators[i].stmt;
|
||||||
|
OperatorInfo* opInfo = stmtToOpInfo[stmt];
|
||||||
|
if (!opInfo) continue;
|
||||||
|
|
||||||
|
size_t currentPos = 0;
|
||||||
for (size_t j = 0; j < newOrder.size(); j++) {
|
for (size_t j = 0; j < newOrder.size(); j++) {
|
||||||
if (newOrder[j] == bestPos) {
|
if (newOrder[j] == stmt) {
|
||||||
newOrder.insert(newOrder.begin() + j + 1, operators[i].stmt);
|
currentPos = j;
|
||||||
inserted = true;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!inserted) {
|
|
||||||
newOrder.push_back(operators[i].stmt);
|
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];
|
||||||
|
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];
|
||||||
|
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];
|
||||||
|
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];
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
newOrder.push_back(operators[i].stmt);
|
|
||||||
}
|
}
|
||||||
moved[i] = true;
|
}
|
||||||
|
|
||||||
|
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];
|
||||||
|
OperatorInfo* opInfo = stmtToOpInfo[stmt];
|
||||||
|
if (!opInfo) continue;
|
||||||
|
|
||||||
|
for (size_t j = 0; j < i; j++) {
|
||||||
|
SgStatement* prevStmt = newOrder[j];
|
||||||
|
OperatorInfo* prevOpInfo = stmtToOpInfo[prevStmt];
|
||||||
|
if (!prevOpInfo) continue;
|
||||||
|
|
||||||
|
pair<SgStatement*, SgStatement*> 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];
|
||||||
|
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;
|
return newOrder;
|
||||||
@@ -388,6 +603,7 @@ static bool applyOperatorReordering(SgForStmt* loop, vector<SgStatement*>& newOr
|
|||||||
vector<char*> savedComments;
|
vector<char*> savedComments;
|
||||||
unordered_set<SgStatement*> extractedSet;
|
unordered_set<SgStatement*> extractedSet;
|
||||||
map<SgStatement*, int> originalLineNumbers;
|
map<SgStatement*, int> originalLineNumbers;
|
||||||
|
map<SgStatement*, SgStatement*> stmtToExtracted;
|
||||||
|
|
||||||
for (SgStatement* stmt : newOrder) {
|
for (SgStatement* stmt : newOrder) {
|
||||||
if (stmt && stmt != loop && stmt != loopEnd && extractedSet.find(stmt) == extractedSet.end()) {
|
if (stmt && stmt != loop && stmt != loopEnd && extractedSet.find(stmt) == extractedSet.end()) {
|
||||||
@@ -422,35 +638,77 @@ static bool applyOperatorReordering(SgForStmt* loop, vector<SgStatement*>& newOr
|
|||||||
if (extracted) {
|
if (extracted) {
|
||||||
extractedStatements.push_back(extracted);
|
extractedStatements.push_back(extracted);
|
||||||
extractedSet.insert(stmt);
|
extractedSet.insert(stmt);
|
||||||
|
stmtToExtracted[stmt] = extracted;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SgStatement* currentPos = loop;
|
map<SgStatement*, SgStatement*> insertedStatements;
|
||||||
|
|
||||||
for (size_t i = 0; i < extractedStatements.size(); i++) {
|
for (size_t idx = 0; idx < newOrder.size(); idx++) {
|
||||||
SgStatement* stmt = extractedStatements[i];
|
SgStatement* stmt = newOrder[idx];
|
||||||
if (stmt) {
|
|
||||||
SgStatement* nextPos = currentPos->lexNext();
|
if (extractedSet.find(stmt) != extractedSet.end()) {
|
||||||
if (nextPos && nextPos != loopEnd) {
|
SgStatement* stmtToInsert = stmtToExtracted[stmt];
|
||||||
if (nextPos->variant() == FOR_NODE && nextPos != loop) {
|
if (!stmtToInsert) continue;
|
||||||
continue;
|
|
||||||
}
|
SgStatement* insertAfter = loop;
|
||||||
if (nextPos->variant() == CONTROL_END) {
|
|
||||||
continue;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i < savedComments.size() && savedComments[i]) {
|
size_t commentIdx = 0;
|
||||||
stmt->setComments(savedComments[i]);
|
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()) {
|
if (originalLineNumbers.find(stmt) != originalLineNumbers.end()) {
|
||||||
stmt->setlineNumber(originalLineNumbers[stmt]);
|
stmtToInsert->setlineNumber(originalLineNumbers[stmt]);
|
||||||
}
|
}
|
||||||
|
|
||||||
currentPos->insertStmtAfter(*stmt, *loop);
|
SgStatement* controlParent = stmt->controlParent();
|
||||||
currentPos = stmt;
|
if (!controlParent) controlParent = loop;
|
||||||
|
|
||||||
|
insertAfter->insertStmtAfter(*stmtToInsert, *controlParent);
|
||||||
|
insertedStatements[stmt] = stmtToInsert;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -460,10 +718,6 @@ static bool applyOperatorReordering(SgForStmt* loop, vector<SgStatement*>& newOr
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (currentPos && currentPos->lexNext() != loopEnd) {
|
|
||||||
currentPos->setLexNext(*loopEnd);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -509,6 +763,37 @@ map<SgForStmt*, vector<SAPFOR::BasicBlock*>> findAndAnalyzeLoops(SgStatement *st
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void processLoopRecursively(SgForStmt* loop, vector<SAPFOR::BasicBlock*> blocks, map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& FullIR) {
|
||||||
|
if (!loop) return;
|
||||||
|
|
||||||
|
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();
|
||||||
|
if (nestedEnd) {
|
||||||
|
current = nestedEnd->lexNext();
|
||||||
|
} else {
|
||||||
|
current = current->lexNext();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void runSwapOperators(SgFile *file, std::map<std::string, std::vector<LoopGraph*>>& loopGraph, std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>& FullIR, int& countOfTransform) {
|
void runSwapOperators(SgFile *file, std::map<std::string, std::vector<LoopGraph*>>& loopGraph, std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>& FullIR, int& countOfTransform) {
|
||||||
countOfTransform += 1;
|
countOfTransform += 1;
|
||||||
|
|
||||||
@@ -524,10 +809,7 @@ void runSwapOperators(SgFile *file, std::map<std::string, std::vector<LoopGraph*
|
|||||||
map<SgForStmt*, vector<SAPFOR::BasicBlock*>> loopsMapping = findAndAnalyzeLoops(st, blocks);
|
map<SgForStmt*, vector<SAPFOR::BasicBlock*>> loopsMapping = findAndAnalyzeLoops(st, blocks);
|
||||||
|
|
||||||
for (pair<SgForStmt*, vector<SAPFOR::BasicBlock*>> loopForAnalyze: loopsMapping) {
|
for (pair<SgForStmt*, vector<SAPFOR::BasicBlock*>> loopForAnalyze: loopsMapping) {
|
||||||
vector<OperatorInfo> operators = analyzeOperatorsInLoop(loopForAnalyze.first, loopForAnalyze.second, FullIR);
|
processLoopRecursively(loopForAnalyze.first, loopForAnalyze.second, FullIR);
|
||||||
map<string, vector<SgStatement*>> varDefinitions = findVariableDefinitions(loopForAnalyze.first, operators);
|
|
||||||
vector<SgStatement*> newOrder = optimizeOperatorOrder(loopForAnalyze.first, operators, varDefinitions);
|
|
||||||
applyOperatorReordering(loopForAnalyze.first, newOrder);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user