2025-03-31 02:50:30 +03:00
|
|
|
#include <map>
|
|
|
|
|
#include <unordered_set>
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <queue>
|
|
|
|
|
#include <numeric>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
#include "private_arrays_search.h"
|
2025-05-27 02:25:39 +03:00
|
|
|
#include "range_structures.h"
|
|
|
|
|
#include "region.h"
|
2025-06-04 13:08:38 +03:00
|
|
|
#include "SgUtils.h"
|
|
|
|
|
#include "graph_loops.h"
|
2025-09-23 08:21:05 +03:00
|
|
|
#include "CFGraph/CFGraph.h"
|
2025-12-11 12:26:39 +03:00
|
|
|
#include "utils.h"
|
2025-03-31 02:50:30 +03:00
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
2025-12-19 01:55:23 +03:00
|
|
|
static void RemoveEmptyPoints(ArrayAccessingIndexes& container)
|
|
|
|
|
{
|
|
|
|
|
ArrayAccessingIndexes resultContainer;
|
|
|
|
|
unordered_set<string> toRemove;
|
2025-12-19 21:06:55 +03:00
|
|
|
|
2025-12-19 01:55:23 +03:00
|
|
|
for (auto& [arrayName, accessingSet] : container)
|
|
|
|
|
{
|
|
|
|
|
vector<vector<ArrayDimension>> points;
|
|
|
|
|
for (auto& arrayPoint : accessingSet.GetElements())
|
|
|
|
|
{
|
|
|
|
|
if (!arrayPoint.empty())
|
|
|
|
|
points.push_back(arrayPoint);
|
|
|
|
|
}
|
2025-12-19 21:06:55 +03:00
|
|
|
|
2025-12-19 01:55:23 +03:00
|
|
|
if (points.size() < accessingSet.GetElements().size() && !points.empty())
|
|
|
|
|
resultContainer[arrayName] = points;
|
|
|
|
|
|
|
|
|
|
if (points.empty())
|
|
|
|
|
toRemove.insert(arrayName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const string& name : toRemove)
|
|
|
|
|
container.erase(name);
|
2025-12-19 21:06:55 +03:00
|
|
|
|
2025-12-19 01:55:23 +03:00
|
|
|
for (auto& [arrayName, accessingSet] : resultContainer)
|
|
|
|
|
container[arrayName] = accessingSet;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-23 08:21:05 +03:00
|
|
|
static void Collapse(Region* region)
|
2025-03-31 02:50:30 +03:00
|
|
|
{
|
2025-05-19 20:50:35 +03:00
|
|
|
if (region->getBasickBlocks().empty())
|
|
|
|
|
return;
|
2025-05-27 02:25:39 +03:00
|
|
|
|
2025-04-08 15:25:39 +03:00
|
|
|
for (auto& [arrayName, arrayRanges] : region->getHeader()->array_out)
|
2025-03-31 02:50:30 +03:00
|
|
|
{
|
2025-04-08 15:25:39 +03:00
|
|
|
for (Region* byBlock : region->getBasickBlocks())
|
2025-03-31 02:50:30 +03:00
|
|
|
{
|
|
|
|
|
AccessingSet intersection = byBlock->array_def[arrayName].Intersect(arrayRanges);
|
2025-04-29 17:55:51 +03:00
|
|
|
region->array_def[arrayName] = region->array_def[arrayName].Union(intersection);
|
2025-03-31 02:50:30 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-19 20:50:35 +03:00
|
|
|
for (auto& byBlock : region->getBasickBlocks())
|
|
|
|
|
{
|
2025-03-31 02:50:30 +03:00
|
|
|
for (auto& [arrayName, arrayRanges] : byBlock->array_use)
|
|
|
|
|
{
|
|
|
|
|
AccessingSet diff = byBlock->array_use[arrayName].Diff(byBlock->array_in[arrayName]);
|
2025-04-29 17:55:51 +03:00
|
|
|
region->array_use[arrayName] = region->array_use[arrayName].Union(diff);
|
2025-03-31 02:50:30 +03:00
|
|
|
}
|
|
|
|
|
}
|
2025-12-19 21:06:55 +03:00
|
|
|
|
2025-05-19 20:50:35 +03:00
|
|
|
ArrayAccessingIndexes useUnion;
|
|
|
|
|
for (auto& byBlock : region->getBasickBlocks())
|
|
|
|
|
for (auto& [arrayName, arrayRanges] : byBlock->array_use)
|
|
|
|
|
useUnion[arrayName] = useUnion[arrayName].Union(byBlock->array_use[arrayName]);
|
2025-05-30 12:45:05 +03:00
|
|
|
|
2025-05-19 20:50:35 +03:00
|
|
|
for (auto& [arrayName, arrayRanges] : useUnion)
|
|
|
|
|
region->array_priv[arrayName] = useUnion[arrayName].Diff(region->array_use[arrayName]);
|
2025-05-30 12:45:05 +03:00
|
|
|
|
2025-04-08 15:25:39 +03:00
|
|
|
for (Region* prevBlock : region->getHeader()->getPrevRegions())
|
|
|
|
|
prevBlock->replaceInNextRegions(region, region->getHeader());
|
2025-05-30 12:45:05 +03:00
|
|
|
|
2025-04-08 15:25:39 +03:00
|
|
|
for (Region* nextBlock : region->getHeader()->getNextRegions())
|
|
|
|
|
nextBlock->replaceInPrevRegions(region, region->getHeader());
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-05 22:49:53 +03:00
|
|
|
static void SolveDataFlowIteratively(Region* DFG)
|
2025-04-29 17:55:51 +03:00
|
|
|
{
|
|
|
|
|
unordered_set<Region*> worklist(DFG->getBasickBlocks());
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
Region* b = *worklist.begin();
|
|
|
|
|
ArrayAccessingIndexes newIn;
|
2025-05-27 02:25:39 +03:00
|
|
|
bool flagFirst = true;
|
2025-04-29 17:55:51 +03:00
|
|
|
for (Region* prevBlock : b->getPrevRegions())
|
|
|
|
|
{
|
2025-05-27 02:25:39 +03:00
|
|
|
if (flagFirst)
|
2025-04-29 17:55:51 +03:00
|
|
|
{
|
2025-05-27 02:25:39 +03:00
|
|
|
newIn = prevBlock->array_out;
|
|
|
|
|
flagFirst = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (prevBlock->array_out.empty())
|
2025-05-19 20:50:35 +03:00
|
|
|
{
|
2025-05-27 02:25:39 +03:00
|
|
|
newIn.clear();
|
|
|
|
|
continue;
|
2025-05-19 20:50:35 +03:00
|
|
|
}
|
2025-12-19 21:06:55 +03:00
|
|
|
|
2025-05-27 02:25:39 +03:00
|
|
|
for (const auto& [arrayName, accessSet] : prevBlock->array_out)
|
2025-05-19 20:50:35 +03:00
|
|
|
{
|
2025-05-27 02:25:39 +03:00
|
|
|
if (newIn.find(arrayName) != newIn.end())
|
2025-05-30 12:45:05 +03:00
|
|
|
newIn[arrayName] = newIn[arrayName].Intersect(accessSet);
|
2025-05-27 02:25:39 +03:00
|
|
|
else
|
|
|
|
|
newIn[arrayName] = AccessingSet();
|
2025-05-19 20:50:35 +03:00
|
|
|
}
|
2025-04-29 17:55:51 +03:00
|
|
|
}
|
|
|
|
|
}
|
2025-05-30 12:45:05 +03:00
|
|
|
|
2025-05-27 02:25:39 +03:00
|
|
|
b->array_in = move(newIn);
|
2025-04-29 17:55:51 +03:00
|
|
|
ArrayAccessingIndexes newOut;
|
2025-12-19 21:06:55 +03:00
|
|
|
|
2025-05-30 12:45:05 +03:00
|
|
|
if (b->array_def.empty())
|
2025-05-27 02:25:39 +03:00
|
|
|
newOut = b->array_in;
|
|
|
|
|
else if (b->array_in.empty())
|
|
|
|
|
newOut = b->array_def;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (auto& [arrayName, accessSet] : b->array_def)
|
2025-05-19 20:50:35 +03:00
|
|
|
{
|
2025-05-27 02:25:39 +03:00
|
|
|
if (newOut.find(arrayName) != newOut.end())
|
|
|
|
|
newOut[arrayName] = b->array_def[arrayName].Union(b->array_in[arrayName]);
|
|
|
|
|
else
|
|
|
|
|
newOut[arrayName] = accessSet;
|
2025-05-19 20:50:35 +03:00
|
|
|
}
|
2025-04-29 17:55:51 +03:00
|
|
|
}
|
2025-05-30 12:45:05 +03:00
|
|
|
|
2025-04-29 17:55:51 +03:00
|
|
|
/* can not differ */
|
|
|
|
|
if (newOut != b->array_out)
|
|
|
|
|
b->array_out = newOut;
|
|
|
|
|
else
|
|
|
|
|
worklist.erase(b);
|
|
|
|
|
}
|
|
|
|
|
while (!worklist.empty());
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-05 22:49:53 +03:00
|
|
|
static void SolveDataFlow(Region* DFG)
|
2025-04-08 15:25:39 +03:00
|
|
|
{
|
2025-05-27 02:25:39 +03:00
|
|
|
if (!DFG)
|
|
|
|
|
return;
|
2025-05-30 12:45:05 +03:00
|
|
|
|
2025-04-29 17:55:51 +03:00
|
|
|
SolveDataFlowIteratively(DFG);
|
2025-04-08 15:25:39 +03:00
|
|
|
for (Region* subRegion : DFG->getSubRegions())
|
|
|
|
|
SolveDataFlow(subRegion);
|
|
|
|
|
Collapse(DFG);
|
2025-03-31 02:50:30 +03:00
|
|
|
}
|
|
|
|
|
|
2025-12-19 21:06:55 +03:00
|
|
|
/*unsigned long long CalculateLength(const AccessingSet& array)
|
2025-12-04 08:54:09 +03:00
|
|
|
{
|
|
|
|
|
if (array.GetElements().empty())
|
|
|
|
|
return 0;
|
2025-12-19 21:06:55 +03:00
|
|
|
|
2025-12-04 08:54:09 +03:00
|
|
|
unsigned long long result = 1;
|
|
|
|
|
for (const auto& range : array.GetElements())
|
|
|
|
|
for (const auto& dim : range)
|
|
|
|
|
result *= (dim.step * dim.tripCount);
|
2025-12-19 21:06:55 +03:00
|
|
|
|
2025-12-04 08:54:09 +03:00
|
|
|
return result;
|
2025-12-19 21:06:55 +03:00
|
|
|
}*/
|
2025-12-04 08:54:09 +03:00
|
|
|
|
2025-12-19 21:06:55 +03:00
|
|
|
static void AddPrivateArraysToLoop(LoopGraph* loop, const ArrayAccessingIndexes& privates, set<SgStatement*>& insertedPrivates)
|
2025-12-04 08:54:09 +03:00
|
|
|
{
|
|
|
|
|
SgStatement* spfStat = new SgStatement(SPF_ANALYSIS_DIR);
|
2025-12-11 12:26:39 +03:00
|
|
|
spfStat->setlineNumber(loop->loop->lineNumber());
|
|
|
|
|
spfStat->setFileName(loop->loop->fileName());
|
2025-12-04 08:54:09 +03:00
|
|
|
SgExpression* toAdd = new SgExpression(EXPR_LIST, new SgExpression(ACC_PRIVATE_OP), NULL, NULL);
|
|
|
|
|
set<SgSymbol*> arraysToInsert;
|
|
|
|
|
for (const auto& [_, accessingSet] : privates)
|
|
|
|
|
{
|
|
|
|
|
for (const auto& arrayElement : accessingSet.GetElements())
|
|
|
|
|
{
|
|
|
|
|
if (arrayElement.empty())
|
|
|
|
|
continue;
|
|
|
|
|
arraysToInsert.insert(arrayElement[0].array->symbol());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
spfStat->setExpression(0, *toAdd);
|
|
|
|
|
toAdd = toAdd->lhs();
|
|
|
|
|
bool first = true;
|
|
|
|
|
for (auto& elem : arraysToInsert)
|
|
|
|
|
{
|
|
|
|
|
if (first)
|
|
|
|
|
{
|
|
|
|
|
toAdd->setLhs(new SgExpression(EXPR_LIST));
|
|
|
|
|
toAdd = toAdd->lhs();
|
|
|
|
|
first = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
toAdd->setRhs(new SgExpression(EXPR_LIST));
|
|
|
|
|
toAdd = toAdd->rhs();
|
|
|
|
|
}
|
|
|
|
|
toAdd->setLhs(new SgVarRefExp(elem));
|
|
|
|
|
}
|
2025-12-11 12:26:39 +03:00
|
|
|
|
|
|
|
|
if (arraysToInsert.size() == 0)
|
|
|
|
|
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
|
2025-12-04 08:54:09 +03:00
|
|
|
|
2025-12-11 12:26:39 +03:00
|
|
|
loop->loop->insertStmtBefore(*spfStat, *loop->loop->controlParent());
|
|
|
|
|
insertedPrivates.insert(spfStat);
|
2025-12-04 08:54:09 +03:00
|
|
|
}
|
|
|
|
|
|
2025-12-11 12:26:39 +03:00
|
|
|
void FindPrivateArrays(map<string, vector<LoopGraph*>> &loopGraph, map<FuncInfo*, vector<SAPFOR::BasicBlock*>>& FullIR, set<SgStatement*> &insertedPrivates)
|
2025-03-31 02:50:30 +03:00
|
|
|
{
|
2025-05-19 20:50:35 +03:00
|
|
|
map<LoopGraph*, ArrayAccessingIndexes> result;
|
2025-10-30 06:04:02 +03:00
|
|
|
for (const auto& [fileName, loops] : loopGraph)
|
2025-03-31 02:50:30 +03:00
|
|
|
{
|
2025-10-30 06:04:02 +03:00
|
|
|
SgFile::switchToFile(fileName);
|
2025-05-19 20:50:35 +03:00
|
|
|
for (const auto& loop : loops)
|
2025-03-31 02:50:30 +03:00
|
|
|
{
|
2025-12-19 01:55:23 +03:00
|
|
|
if (!loop->isFor())
|
|
|
|
|
continue;
|
2025-10-30 06:04:02 +03:00
|
|
|
SgStatement* search_func = loop->loop->GetOriginal();
|
|
|
|
|
|
|
|
|
|
while (search_func && (!isSgProgHedrStmt(search_func)))
|
|
|
|
|
search_func = search_func->controlParent();
|
|
|
|
|
|
2025-05-19 20:50:35 +03:00
|
|
|
for (const auto& [funcInfo, blocks]: FullIR)
|
|
|
|
|
{
|
2025-10-30 06:04:02 +03:00
|
|
|
if (funcInfo->fileName == fileName && funcInfo->funcPointer->GetOriginal() == search_func)
|
|
|
|
|
{
|
|
|
|
|
Region* loopRegion = new Region(loop, blocks);
|
|
|
|
|
if (loopRegion->getBasickBlocks().size() <= 1)
|
|
|
|
|
{
|
|
|
|
|
delete(loopRegion);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
SolveDataFlow(loopRegion);
|
2025-12-19 01:55:23 +03:00
|
|
|
RemoveEmptyPoints(loopRegion->array_priv);
|
2025-10-30 06:04:02 +03:00
|
|
|
result[loop] = loopRegion->array_priv;
|
|
|
|
|
delete(loopRegion);
|
|
|
|
|
}
|
2025-03-31 02:50:30 +03:00
|
|
|
}
|
2025-12-11 12:26:39 +03:00
|
|
|
|
2025-12-04 08:54:09 +03:00
|
|
|
if (result.find(loop) != result.end() && !result[loop].empty())
|
2025-12-11 12:26:39 +03:00
|
|
|
AddPrivateArraysToLoop(loop, result[loop], insertedPrivates);
|
2025-03-31 02:50:30 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|