attempt to build new ast
This commit is contained in:
@@ -290,22 +290,88 @@ vector<SgStatement*> scheduleOperations(const map<SgStatement*, set<SgStatement*
|
|||||||
return executionOrder;
|
return executionOrder;
|
||||||
}
|
}
|
||||||
|
|
||||||
void buildNewAST(SgStatement* loop, vector<SgStatement*>& newBody)
|
static bool buildNewAST(SgStatement* loop, vector<SgStatement*>& newBody)
|
||||||
{
|
{
|
||||||
SgStatement* endDo = loop->lastNodeOfStmt();
|
if (!loop) {return false;}
|
||||||
SgStatement* st = loop;
|
if (newBody.empty()) {return true;}
|
||||||
int lineNum = loop -> lineNumber() + 1;
|
if (loop->variant() != FOR_NODE) {return false;}
|
||||||
for (int i = 0; i < newBody.size(); i++)
|
|
||||||
{
|
SgStatement* loopStart = loop->lexNext();
|
||||||
st -> setLexNext(*newBody[i]);
|
SgStatement* loopEnd = loop->lastNodeOfStmt();
|
||||||
st = st -> lexNext();
|
if (!loopStart || !loopEnd) {return false;}
|
||||||
st -> setlineNumber(lineNum);
|
|
||||||
lineNum++;
|
for (SgStatement* stmt : newBody) {
|
||||||
|
if (stmt && stmt != loop && stmt != loopEnd) {
|
||||||
|
SgStatement* current = loopStart;
|
||||||
|
bool found = false;
|
||||||
|
while (current && current != loopEnd->lexNext()) {
|
||||||
|
if (current == stmt) {
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
st -> setLexNext(*endDo);
|
current = current->lexNext();
|
||||||
|
}
|
||||||
|
if (!found) {return false;}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<SgStatement*> extractedStatements;
|
||||||
|
vector<char*> savedComments;
|
||||||
|
vector<int> savedLineNumbers;
|
||||||
|
|
||||||
|
for (SgStatement* stmt : newBody) {
|
||||||
|
if (stmt && stmt != loop && stmt != loopEnd) {
|
||||||
|
savedComments.push_back(stmt->comments() ? strdup(stmt->comments()) : nullptr);
|
||||||
|
savedLineNumbers.push_back(stmt->lineNumber());
|
||||||
|
SgStatement* extracted = stmt->extractStmt();
|
||||||
|
if (extracted) {extractedStatements.push_back(extracted);}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SgStatement* currentPos = loop;
|
||||||
|
int lineCounter = loop->lineNumber() + 1;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < extractedStatements.size(); i++) {
|
||||||
|
SgStatement* stmt = extractedStatements[i];
|
||||||
|
if (stmt) {
|
||||||
|
if (i < savedComments.size() && savedComments[i]) {
|
||||||
|
stmt->setComments(savedComments[i]);
|
||||||
|
}
|
||||||
|
stmt->setlineNumber(lineCounter++);
|
||||||
|
currentPos->insertStmtAfter(*stmt, *loop);
|
||||||
|
currentPos = stmt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (char* comment : savedComments) {
|
||||||
|
if (comment) {
|
||||||
|
free(comment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentPos && currentPos->lexNext() != loopEnd) {
|
||||||
|
currentPos->setLexNext(*loopEnd);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool validateNewOrder(SgStatement* loop, const vector<SgStatement*>& newOrder)
|
||||||
|
{
|
||||||
|
if (!loop || newOrder.empty()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
unordered_set<SgStatement*> seen;
|
||||||
|
for (SgStatement* stmt : newOrder) {
|
||||||
|
if (stmt && stmt != loop && stmt != loop->lastNodeOfStmt()) {
|
||||||
|
if (seen.count(stmt)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
seen.insert(stmt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
@@ -339,7 +405,10 @@ void runSwapOperators(SgFile *file, std::map<std::string, std::vector<LoopGraph*
|
|||||||
for (auto v: new_order)
|
for (auto v: new_order)
|
||||||
if (v -> lineNumber() > firstLine)
|
if (v -> lineNumber() > firstLine)
|
||||||
cout << v -> lineNumber() << endl;
|
cout << v -> lineNumber() << endl;
|
||||||
|
|
||||||
|
if (validateNewOrder(loopForAnalyze.first, new_order)) {
|
||||||
buildNewAST(loopForAnalyze.first, new_order);
|
buildNewAST(loopForAnalyze.first, new_order);
|
||||||
|
}
|
||||||
st = loopForAnalyze.first -> lexNext();
|
st = loopForAnalyze.first -> lexNext();
|
||||||
while (st != loopForAnalyze.first -> lastNodeOfStmt())
|
while (st != loopForAnalyze.first -> lastNodeOfStmt())
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user