Files
SAPFOR/src/PrivateAnalyzer/region.cpp

247 lines
9.2 KiB
C++
Raw Normal View History

2025-05-27 02:25:39 +03:00
#include<vector>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<string>
#include <numeric>
#include "range_structures.h"
#include "region.h"
#include "../Utils/SgUtils.h"
using namespace std;
static bool isParentStmt(SgStatement* stmt, SgStatement* parent)
{
for (; stmt; stmt = stmt->controlParent())
if (stmt == parent)
return true;
2025-05-30 12:45:05 +03:00
2025-05-27 02:25:39 +03:00
return false;
}
/*returns head block and loop*/
pair<SAPFOR::BasicBlock*, unordered_set<SAPFOR::BasicBlock*>> GetBasicBlocksForLoop(const LoopGraph* loop, const vector<SAPFOR::BasicBlock*> blocks)
{
unordered_set<SAPFOR::BasicBlock*> block_loop;
SAPFOR::BasicBlock* head_block = nullptr;
auto loop_operator = loop->loop->GetOriginal();
for (const auto& block : blocks)
{
if (!block || (block->getInstructions().size() == 0))
continue;
2025-05-30 12:45:05 +03:00
2025-05-27 02:25:39 +03:00
SgStatement* first = block->getInstructions().front()->getInstruction()->getOperator();
SgStatement* last = block->getInstructions().back()->getInstruction()->getOperator();
if (isParentStmt(first, loop_operator) && isParentStmt(last, loop_operator))
{
block_loop.insert(block);
if ((!head_block) && (first == loop_operator) && (last == loop_operator) &&
(block->getInstructions().size() == 2) &&
(block->getInstructions().back()->getInstruction()->getOperation() == SAPFOR::CFG_OP::JUMP_IF))
{
head_block = block;
}
}
}
return { head_block, block_loop };
}
static void BuildLoopIndex(map<string, LoopGraph*>& loopForIndex, LoopGraph* loop) {
string index = loop->loopSymbol;
loopForIndex[index] = loop;
2025-05-30 12:45:05 +03:00
for (const auto& childLoop : loop->children)
2025-05-27 02:25:39 +03:00
BuildLoopIndex(loopForIndex, childLoop);
}
static string FindIndexName(int pos, SAPFOR::BasicBlock* block, map<string, LoopGraph*>& loopForIndex) {
unordered_set<SAPFOR::Argument*> args = { block->getInstructions()[pos]->getInstruction()->getArg1() };
2025-05-30 12:45:05 +03:00
for (int i = pos - 1; i >= 0; i--)
{
2025-05-27 02:25:39 +03:00
SAPFOR::Argument* res = block->getInstructions()[i]->getInstruction()->getResult();
2025-05-30 12:45:05 +03:00
if (res && args.find(res) != args.end())
{
2025-05-27 02:25:39 +03:00
SAPFOR::Argument* arg1 = block->getInstructions()[i]->getInstruction()->getArg1();
SAPFOR::Argument* arg2 = block->getInstructions()[i]->getInstruction()->getArg2();
2025-05-30 12:45:05 +03:00
if (arg1)
{
2025-05-27 02:25:39 +03:00
string name = arg1->getValue();
int idx = name.find('%');
if (idx != -1 && loopForIndex.find(name.substr(idx + 1)) != loopForIndex.end())
return name.substr(idx + 1);
2025-05-30 12:45:05 +03:00
else
2025-05-27 02:25:39 +03:00
args.insert(arg1);
}
2025-05-30 12:45:05 +03:00
if (arg2)
{
2025-05-27 02:25:39 +03:00
string name = arg2->getValue();
int idx = name.find('%');
if (idx != -1 && loopForIndex.find(name.substr(idx + 1)) != loopForIndex.end())
return name.substr(idx + 1);
2025-05-30 12:45:05 +03:00
else
2025-05-27 02:25:39 +03:00
args.insert(arg2);
}
}
}
return "";
}
static int GetDefUseArray(SAPFOR::BasicBlock* block, LoopGraph* loop, ArrayAccessingIndexes& def, ArrayAccessingIndexes& use) {
auto instructions = block->getInstructions();
map<string, LoopGraph*> loopForIndex;
BuildLoopIndex(loopForIndex, loop);
for (int i = 0; i < instructions.size(); i++)
{
auto instruction = instructions[i];
2025-05-30 12:45:05 +03:00
if (!instruction->getInstruction()->getArg1())
2025-05-27 02:25:39 +03:00
continue;
2025-05-30 12:45:05 +03:00
2025-05-27 02:25:39 +03:00
auto operation = instruction->getInstruction()->getOperation();
auto type = instruction->getInstruction()->getArg1()->getType();
if ((operation == SAPFOR::CFG_OP::STORE || operation == SAPFOR::CFG_OP::LOAD) && type == SAPFOR::CFG_ARG_TYPE::ARRAY)
{
vector<SAPFOR::Argument*> index_vars;
vector<int> refPos;
string array_name;
if (operation == SAPFOR::CFG_OP::STORE)
2025-05-30 12:45:05 +03:00
array_name = instruction->getInstruction()->getArg1()->getValue();
2025-05-27 02:25:39 +03:00
else
array_name = instruction->getInstruction()->getArg2()->getValue();
2025-05-30 12:45:05 +03:00
2025-05-27 02:25:39 +03:00
int j = i - 1;
while (j >= 0 && instructions[j]->getInstruction()->getOperation() == SAPFOR::CFG_OP::REF)
{
index_vars.push_back(instructions[j]->getInstruction()->getArg1());
refPos.push_back(j);
j--;
}
2025-05-30 12:45:05 +03:00
2025-05-27 02:25:39 +03:00
/*to choose correct dimension*/
int n = index_vars.size();
vector<ArrayDimension> accessPoint(n);
auto* ref = isSgArrayRefExp(instruction->getInstruction()->getExpression());
vector<pair<int, int>> coefsForDims;
for (int i = 0; ref && i < ref->numberOfSubscripts(); ++i)
{
const vector<int*>& coefs = getAttributes<SgExpression*, int*>(ref->subscript(i), set<int>{ INT_VAL });
if (coefs.size() == 1)
{
const pair<int, int> coef(coefs[0][0], coefs[0][1]);
coefsForDims.push_back(coef);
}
}
while (!index_vars.empty())
{
auto var = index_vars.back();
int currentVarPos = refPos.back();
pair currentCoefs = coefsForDims.back();
ArrayDimension current_dim;
2025-05-30 12:45:05 +03:00
if (var->getType() == SAPFOR::CFG_ARG_TYPE::CONST)
2025-05-27 02:25:39 +03:00
current_dim = { stoul(var->getValue()), 1, 1 };
else
{
string name, full_name = var->getValue();
int pos = full_name.find('%');
LoopGraph* currentLoop;
2025-05-30 12:45:05 +03:00
if (pos != -1)
{
2025-05-27 02:25:39 +03:00
name = full_name.substr(pos + 1);
2025-05-30 12:45:05 +03:00
if (loopForIndex.find(name) != loopForIndex.end())
currentLoop = loopForIndex[name];
else
2025-05-27 02:25:39 +03:00
return -1;
}
2025-05-30 12:45:05 +03:00
else
{
2025-05-27 02:25:39 +03:00
name = FindIndexName(currentVarPos, block, loopForIndex);
2025-05-30 12:45:05 +03:00
if (name == "")
2025-05-27 02:25:39 +03:00
return -1;
2025-05-30 12:45:05 +03:00
if (loopForIndex.find(name) != loopForIndex.end())
currentLoop = loopForIndex[name];
else
2025-05-27 02:25:39 +03:00
return -1;
}
2025-05-30 12:45:05 +03:00
2025-05-27 02:25:39 +03:00
uint64_t start = currentLoop->startVal * currentCoefs.first + currentCoefs.second;
uint64_t step = currentCoefs.first;
current_dim = { start, step, (uint64_t)currentLoop->calculatedCountOfIters };
}
2025-05-30 12:45:05 +03:00
2025-05-27 02:25:39 +03:00
accessPoint[n - index_vars.size()] = current_dim;
index_vars.pop_back();
refPos.pop_back();
coefsForDims.pop_back();
}
2025-05-30 12:45:05 +03:00
if (operation == SAPFOR::CFG_OP::STORE)
2025-05-27 02:25:39 +03:00
def[array_name].Insert(accessPoint);
else
use[array_name].Insert(accessPoint);
}
}
return 0;
}
static void SetConnections(unordered_map<SAPFOR::BasicBlock*, Region*>& bbToRegion, const unordered_set<SAPFOR::BasicBlock*>& blockSet)
{
for (SAPFOR::BasicBlock* block : blockSet)
{
for (SAPFOR::BasicBlock* nextBlock : block->getNext())
if (bbToRegion.find(nextBlock) != bbToRegion.end())
bbToRegion[block]->addNextRegion(bbToRegion[nextBlock]);
2025-05-30 12:45:05 +03:00
2025-05-27 02:25:39 +03:00
for (SAPFOR::BasicBlock* prevBlock : block->getPrev())
if (bbToRegion.find(prevBlock) != bbToRegion.end())
bbToRegion[block]->addPrevRegion(bbToRegion[prevBlock]);
}
}
static Region* CreateSubRegion(LoopGraph* loop, const vector<SAPFOR::BasicBlock*>& Blocks, const unordered_map<SAPFOR::BasicBlock*, Region*>& bbToRegion)
{
Region* region = new Region;
auto [header, blockSet] = GetBasicBlocksForLoop(loop, Blocks);
if (bbToRegion.find(header) != bbToRegion.end())
2025-05-30 12:45:05 +03:00
region->setHeader(bbToRegion.at(header));
2025-05-27 02:25:39 +03:00
else
return NULL;
2025-05-30 12:45:05 +03:00
2025-05-27 02:25:39 +03:00
for (SAPFOR::BasicBlock* block : blockSet)
if (bbToRegion.find(block) != bbToRegion.end())
region->addBasickBlocks(bbToRegion.at(block));
2025-05-30 12:45:05 +03:00
2025-05-27 02:25:39 +03:00
for (LoopGraph* childLoop : loop->children)
region->addSubRegions(CreateSubRegion(childLoop, Blocks, bbToRegion));
2025-05-30 12:45:05 +03:00
2025-05-27 02:25:39 +03:00
cout << header << endl;
return region;
}
Region::Region(LoopGraph* loop, const vector<SAPFOR::BasicBlock*>& Blocks)
{
auto [header, blockSet] = GetBasicBlocksForLoop(loop, Blocks);
unordered_map<SAPFOR::BasicBlock*, Region*> bbToRegion;
for (auto poiner : blockSet)
{
bbToRegion[poiner] = new Region(*poiner);
this->basickBlocks.insert(bbToRegion[poiner]);
GetDefUseArray(poiner, loop, bbToRegion[poiner]->array_def, bbToRegion[poiner]->array_use);
}
this->header = bbToRegion[header];
SetConnections(bbToRegion, blockSet);
//create subRegions
for (LoopGraph* childLoop : loop->children)
subRegions.insert(CreateSubRegion(childLoop, Blocks, bbToRegion));
}