fixed code style

This commit is contained in:
ALEXks
2025-05-30 12:45:05 +03:00
parent 8dcbd587ec
commit 26e36bed46
6 changed files with 86 additions and 158 deletions

View File

@@ -39,25 +39,17 @@ void Collapse(Region* region)
}
ArrayAccessingIndexes useUnion;
for (auto& byBlock : region->getBasickBlocks())
{
for (auto& [arrayName, arrayRanges] : byBlock->array_use)
{
useUnion[arrayName] = useUnion[arrayName].Union(byBlock->array_use[arrayName]);
}
}
for (auto& [arrayName, arrayRanges] : useUnion)
{
region->array_priv[arrayName] = useUnion[arrayName].Diff(region->array_use[arrayName]);
}
for (Region* prevBlock : region->getHeader()->getPrevRegions())
{
prevBlock->replaceInNextRegions(region, region->getHeader());
}
for (Region* nextBlock : region->getHeader()->getNextRegions())
{
nextBlock->replaceInPrevRegions(region, region->getHeader());
}
}
static void SolveDataFlowIteratively(Region* DFG)
@@ -85,50 +77,36 @@ static void SolveDataFlowIteratively(Region* DFG)
for (const auto& [arrayName, accessSet] : prevBlock->array_out)
{
if (newIn.find(arrayName) != newIn.end())
{
newIn[arrayName] = newIn[arrayName].Intersect(accessSet);
}
else
{
newIn[arrayName] = AccessingSet();
}
}
}
}
b->array_in = move(newIn);
ArrayAccessingIndexes newOut;
if (b->array_def.empty())
{
newOut = b->array_in;
}
else if (b->array_in.empty())
{
newOut = b->array_def;
}
else
{
for (auto& [arrayName, accessSet] : b->array_def)
{
if (newOut.find(arrayName) != newOut.end())
{
newOut[arrayName] = b->array_def[arrayName].Union(b->array_in[arrayName]);
}
else
{
newOut[arrayName] = accessSet;
}
}
}
/* can not differ */
if (newOut != b->array_out)
{
b->array_out = newOut;
}
else
{
worklist.erase(b);
}
}
while (!worklist.empty());
}
@@ -136,11 +114,10 @@ static void SolveDataFlow(Region* DFG)
{
if (!DFG)
return;
SolveDataFlowIteratively(DFG);
for (Region* subRegion : DFG->getSubRegions())
{
SolveDataFlow(subRegion);
}
Collapse(DFG);
}

View File

@@ -1,8 +1,8 @@
#pragma once
#include<vector>
#include<map>
#include<unordered_set>
#include <vector>
#include <map>
#include <unordered_set>
#include "range_structures.h"
#include "region.h"

View File

@@ -1,7 +1,7 @@
#include<vector>
#include<map>
#include<unordered_set>
#include<string>
#include <vector>
#include <map>
#include <unordered_set>
#include <string>
#include <numeric>
#include "range_structures.h"
@@ -17,11 +17,9 @@ static vector<uint64_t> FindParticularSolution(const ArrayDimension& dim1, const
{
uint64_t rightPart = dim2.start + j * dim2.step;
if (leftPart == rightPart)
{
return { i, j };
}
}
}
return {};
}
@@ -30,9 +28,8 @@ static ArrayDimension* DimensionIntersection(const ArrayDimension& dim1, const A
{
vector<uint64_t> partSolution = FindParticularSolution(dim1, dim2);
if (partSolution.empty())
{
return NULL;
}
int64_t x0 = partSolution[0], y0 = partSolution[1];
/* x = x_0 + c * t */
/* y = y_0 + d * t */
@@ -46,9 +43,8 @@ static ArrayDimension* DimensionIntersection(const ArrayDimension& dim1, const A
int64_t tMin = max(tXMin, tYMin);
uint64_t tMax = min(tXMax, tYMax);
if (tMin > tMax)
{
return NULL;
}
uint64_t start3 = dim1.start + x0 * dim1.step;
uint64_t step3 = c * dim1.step;
ArrayDimension* result = new(ArrayDimension){ start3, step3, tMax + 1 };
@@ -60,15 +56,13 @@ static vector<ArrayDimension> DimensionDifference(const ArrayDimension& dim1, co
{
ArrayDimension* intersection = DimensionIntersection(dim1, dim2);
if (!intersection)
{
return { dim1 };
}
vector<ArrayDimension> result;
/* add the part before intersection */
if (dim1.start < intersection->start)
{
result.push_back({ dim1.start, dim1.step, (intersection->start - dim1.start) / dim1.step });
}
/* add the parts between intersection steps */
uint64_t start = (intersection->start - dim1.start) / dim1.step;
uint64_t interValue = intersection->start;
@@ -103,11 +97,11 @@ static vector<ArrayDimension> DimensionUnion(const ArrayDimension& dim1, const A
vector<ArrayDimension> res;
ArrayDimension* inter = DimensionIntersection(dim1, dim2);
if (!inter)
{
return { dim1, dim2 };
}
res.push_back(*inter);
delete(inter);
vector<ArrayDimension> diff1, diff2;
diff1 = DimensionDifference(dim1, dim2);
diff2 = DimensionDifference(dim2, dim1);
@@ -118,29 +112,24 @@ static vector<ArrayDimension> DimensionUnion(const ArrayDimension& dim1, const A
static vector<ArrayDimension> ElementsIntersection(const vector<ArrayDimension>& firstElement, const vector<ArrayDimension>& secondElement)
{
if (firstElement.empty() || secondElement.empty()) {
if (firstElement.empty() || secondElement.empty())
return {};
}
size_t dimAmount = firstElement.size();
/* check if there is no intersecction */
for (size_t i = 0; i < dimAmount; i++)
{
if (FindParticularSolution(firstElement[i], secondElement[i]).empty()) {
if (FindParticularSolution(firstElement[i], secondElement[i]).empty())
return {};
}
}
vector<ArrayDimension> result(dimAmount);
for (size_t i = 0; i < dimAmount; i++)
{
ArrayDimension* resPtr = DimensionIntersection(firstElement[i], secondElement[i]);
if (resPtr)
{
result[i] = *resPtr;
}
else
{
return {};
}
}
return result;
}
@@ -148,15 +137,14 @@ static vector<ArrayDimension> ElementsIntersection(const vector<ArrayDimension>&
static vector<vector<ArrayDimension>> ElementsDifference(const vector<ArrayDimension>& firstElement,
const vector<ArrayDimension>& secondElement)
{
if (firstElement.empty() || secondElement.empty()) {
if (firstElement.empty() || secondElement.empty())
return {};
}
vector<ArrayDimension> intersection = ElementsIntersection(firstElement, secondElement);
vector<vector<ArrayDimension>> result;
if (intersection.empty())
{
return { firstElement };
}
for (int i = 0; i < firstElement.size(); i++)
{
auto dimDiff = DimensionDifference(firstElement[i], secondElement[i]);
@@ -212,10 +200,9 @@ void AccessingSet::FindCoveredBy(const vector<ArrayDimension>& element, vector<v
for (const auto& currentElement : allElements)
{
auto intersection = ElementsIntersection(element, currentElement);
if (!intersection.empty()) {
if (!intersection.empty())
result.push_back(intersection);
}
}
}
vector<vector<ArrayDimension>> AccessingSet::GetElements() const { return allElements; }
@@ -229,13 +216,11 @@ void AccessingSet::Insert(const vector<ArrayDimension>& element)
AccessingSet AccessingSet::Union(const AccessingSet& source) {
AccessingSet result;
for (auto& element : source.GetElements()) {
for (auto& element : source.GetElements())
result.Insert(element);
}
for (auto& element : allElements)
{
result.Insert(element);
}
return result;
}
@@ -244,22 +229,20 @@ AccessingSet AccessingSet::Intersect(const AccessingSet& secondSet) const
vector<vector<ArrayDimension>> result;
if (secondSet.GetElements().empty() || this->allElements.empty())
return AccessingSet(result);
for (const auto& element : allElements)
{
if (secondSet.ContainsElement(element))
{
result.push_back(element);
}
else
{
vector<vector<ArrayDimension>> coveredBy;
secondSet.FindCoveredBy(element, coveredBy);
if (!coveredBy.empty())
{
result.insert(result.end(), coveredBy.begin(), coveredBy.end());
}
}
}
return AccessingSet(result);
}
@@ -267,6 +250,7 @@ AccessingSet AccessingSet::Diff(const AccessingSet& secondSet) const
{
if (secondSet.GetElements().empty() || allElements.empty())
return *this;
AccessingSet intersection = this->Intersect(secondSet);
AccessingSet uncovered = *this;
vector<vector<ArrayDimension>> result;
@@ -288,30 +272,21 @@ bool operator!=(const ArrayDimension& lhs, const ArrayDimension& rhs)
bool operator!=(const AccessingSet& lhs, const AccessingSet& rhs)
{
for (size_t i = 0; i < lhs.allElements.size(); i++)
{
for (size_t j = 0; j < lhs.allElements[i].size(); j++)
{
if (lhs.allElements[i][j] != rhs.allElements[i][j])
{
return true;
}
}
}
return false;
}
bool operator!=(const ArrayAccessingIndexes& lhs, const ArrayAccessingIndexes& rhs)
{
if (lhs.size() != rhs.size())
{
return true;
}
for (auto& [key, value] : lhs)
{
if (rhs.find(key) == rhs.end())
{
return true;
}
}
return false;
}

View File

@@ -1,9 +1,9 @@
#pragma once
#include<vector>
#include<map>
#include<unordered_set>
#include<string>
#include <vector>
#include <map>
#include <unordered_set>
#include <string>
struct ArrayDimension
{

View File

@@ -16,9 +16,8 @@ static bool isParentStmt(SgStatement* stmt, SgStatement* parent)
{
for (; stmt; stmt = stmt->controlParent())
if (stmt == parent)
{
return true;
}
return false;
}
@@ -31,9 +30,8 @@ pair<SAPFOR::BasicBlock*, unordered_set<SAPFOR::BasicBlock*>> GetBasicBlocksForL
for (const auto& block : blocks)
{
if (!block || (block->getInstructions().size() == 0))
{
continue;
}
SgStatement* first = block->getInstructions().front()->getInstruction()->getOperator();
SgStatement* last = block->getInstructions().back()->getInstruction()->getOperator();
if (isParentStmt(first, loop_operator) && isParentStmt(last, loop_operator))
@@ -55,39 +53,42 @@ pair<SAPFOR::BasicBlock*, unordered_set<SAPFOR::BasicBlock*>> GetBasicBlocksForL
static void BuildLoopIndex(map<string, LoopGraph*>& loopForIndex, LoopGraph* loop) {
string index = loop->loopSymbol;
loopForIndex[index] = loop;
for (const auto& childLoop : loop->children) {
for (const auto& childLoop : loop->children)
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() };
for (int i = pos - 1; i >= 0; i--) {
for (int i = pos - 1; i >= 0; i--)
{
SAPFOR::Argument* res = block->getInstructions()[i]->getInstruction()->getResult();
if (res && args.find(res) != args.end()) {
if (res && args.find(res) != args.end())
{
SAPFOR::Argument* arg1 = block->getInstructions()[i]->getInstruction()->getArg1();
SAPFOR::Argument* arg2 = block->getInstructions()[i]->getInstruction()->getArg2();
if (arg1) {
if (arg1)
{
string name = arg1->getValue();
int idx = name.find('%');
if (idx != -1 && loopForIndex.find(name.substr(idx + 1)) != loopForIndex.end())
return name.substr(idx + 1);
else {
else
args.insert(arg1);
}
}
if (arg2) {
if (arg2)
{
string name = arg2->getValue();
int idx = name.find('%');
if (idx != -1 && loopForIndex.find(name.substr(idx + 1)) != loopForIndex.end())
return name.substr(idx + 1);
else {
else
args.insert(arg2);
}
}
}
}
return "";
}
@@ -98,9 +99,9 @@ static int GetDefUseArray(SAPFOR::BasicBlock* block, LoopGraph* loop, ArrayAcces
for (int i = 0; i < instructions.size(); i++)
{
auto instruction = instructions[i];
if (!instruction->getInstruction()->getArg1()) {
if (!instruction->getInstruction()->getArg1())
continue;
}
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)
@@ -109,13 +110,10 @@ static int GetDefUseArray(SAPFOR::BasicBlock* block, LoopGraph* loop, ArrayAcces
vector<int> refPos;
string array_name;
if (operation == SAPFOR::CFG_OP::STORE)
{
array_name = instruction->getInstruction()->getArg1()->getValue();
}
else
{
array_name = instruction->getInstruction()->getArg2()->getValue();
}
int j = i - 1;
while (j >= 0 && instructions[j]->getInstruction()->getOperation() == SAPFOR::CFG_OP::REF)
{
@@ -123,12 +121,12 @@ static int GetDefUseArray(SAPFOR::BasicBlock* block, LoopGraph* loop, ArrayAcces
refPos.push_back(j);
j--;
}
/*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)
{
@@ -147,54 +145,50 @@ static int GetDefUseArray(SAPFOR::BasicBlock* block, LoopGraph* loop, ArrayAcces
int currentVarPos = refPos.back();
pair currentCoefs = coefsForDims.back();
ArrayDimension current_dim;
if (var->getType() == SAPFOR::CFG_ARG_TYPE::CONST) {
if (var->getType() == SAPFOR::CFG_ARG_TYPE::CONST)
current_dim = { stoul(var->getValue()), 1, 1 };
}
else
{
string name, full_name = var->getValue();
int pos = full_name.find('%');
LoopGraph* currentLoop;
if (pos != -1) {
if (pos != -1)
{
name = full_name.substr(pos + 1);
if (loopForIndex.find(name) != loopForIndex.end()) {
if (loopForIndex.find(name) != loopForIndex.end())
currentLoop = loopForIndex[name];
}
else {
else
return -1;
}
}
else {
else
{
name = FindIndexName(currentVarPos, block, loopForIndex);
if (name == "") {
if (name == "")
return -1;
}
if (loopForIndex.find(name) != loopForIndex.end()) {
if (loopForIndex.find(name) != loopForIndex.end())
currentLoop = loopForIndex[name];
}
else {
else
return -1;
}
}
uint64_t start = currentLoop->startVal * currentCoefs.first + currentCoefs.second;
uint64_t step = currentCoefs.first;
current_dim = { start, step, (uint64_t)currentLoop->calculatedCountOfIters };
}
accessPoint[n - index_vars.size()] = current_dim;
index_vars.pop_back();
refPos.pop_back();
coefsForDims.pop_back();
}
if (operation == SAPFOR::CFG_OP::STORE)
{
def[array_name].Insert(accessPoint);
}
else
{
use[array_name].Insert(accessPoint);
}
}
}
return 0;
}
@@ -204,20 +198,13 @@ static void SetConnections(unordered_map<SAPFOR::BasicBlock*, Region*>& bbToRegi
for (SAPFOR::BasicBlock* block : blockSet)
{
for (SAPFOR::BasicBlock* nextBlock : block->getNext())
{
if (bbToRegion.find(nextBlock) != bbToRegion.end())
{
bbToRegion[block]->addNextRegion(bbToRegion[nextBlock]);
}
}
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)
@@ -225,24 +212,17 @@ static Region* CreateSubRegion(LoopGraph* loop, const vector<SAPFOR::BasicBlock*
Region* region = new Region;
auto [header, blockSet] = GetBasicBlocksForLoop(loop, Blocks);
if (bbToRegion.find(header) != bbToRegion.end())
{
region->setHeader(bbToRegion.at(header));
}
else
{
return NULL;
}
for (SAPFOR::BasicBlock* block : blockSet)
{
if (bbToRegion.find(block) != bbToRegion.end())
{
region->addBasickBlocks(bbToRegion.at(block));
}
}
for (LoopGraph* childLoop : loop->children)
{
region->addSubRegions(CreateSubRegion(childLoop, Blocks, bbToRegion));
}
cout << header << endl;
return region;
}
@@ -262,7 +242,5 @@ Region::Region(LoopGraph* loop, const vector<SAPFOR::BasicBlock*>& Blocks)
SetConnections(bbToRegion, blockSet);
//create subRegions
for (LoopGraph* childLoop : loop->children)
{
subRegions.insert(CreateSubRegion(childLoop, Blocks, bbToRegion));
}
}

View File

@@ -1030,9 +1030,7 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne
countOfTransform += removeDeadCode(func->funcPointer, allFuncInfo, commonBlocks);
}
else if (curr_regime == FIND_PRIVATE_ARRAYS)
{
FindPrivateArrays(loopGraph, fullIR);
}
else if (curr_regime == TEST_PASS)
{
//test pass