From 26e36bed46687afee96f93b7863c54b7c48afd04 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Fri, 30 May 2025 12:45:05 +0300 Subject: [PATCH] fixed code style --- src/PrivateAnalyzer/private_arrays_search.cpp | 41 ++----- src/PrivateAnalyzer/private_arrays_search.h | 6 +- src/PrivateAnalyzer/range_structures.cpp | 81 +++++-------- src/PrivateAnalyzer/range_structures.h | 8 +- src/PrivateAnalyzer/region.cpp | 106 +++++++----------- src/Sapfor.cpp | 2 - 6 files changed, 86 insertions(+), 158 deletions(-) diff --git a/src/PrivateAnalyzer/private_arrays_search.cpp b/src/PrivateAnalyzer/private_arrays_search.cpp index c603178..985baf6 100644 --- a/src/PrivateAnalyzer/private_arrays_search.cpp +++ b/src/PrivateAnalyzer/private_arrays_search.cpp @@ -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 (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,49 +77,35 @@ 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); - } + newIn[arrayName] = newIn[arrayName].Intersect(accessSet); else - { newIn[arrayName] = AccessingSet(); - } } } } + b->array_in = move(newIn); ArrayAccessingIndexes newOut; - if (b->array_def.empty()) - { + 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); } diff --git a/src/PrivateAnalyzer/private_arrays_search.h b/src/PrivateAnalyzer/private_arrays_search.h index 1b8dfdd..2faaa27 100644 --- a/src/PrivateAnalyzer/private_arrays_search.h +++ b/src/PrivateAnalyzer/private_arrays_search.h @@ -1,8 +1,8 @@ #pragma once -#include -#include -#include +#include +#include +#include #include "range_structures.h" #include "region.h" diff --git a/src/PrivateAnalyzer/range_structures.cpp b/src/PrivateAnalyzer/range_structures.cpp index 618fcfa..9a2f437 100644 --- a/src/PrivateAnalyzer/range_structures.cpp +++ b/src/PrivateAnalyzer/range_structures.cpp @@ -1,7 +1,7 @@ -#include -#include -#include -#include +#include +#include +#include +#include #include #include "range_structures.h" @@ -17,9 +17,7 @@ static vector 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 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 DimensionDifference(const ArrayDimension& dim1, co { ArrayDimension* intersection = DimensionIntersection(dim1, dim2); if (!intersection) - { return { dim1 }; - } + vector result; /* add the part before intersection */ - if (dim1.start < intersection->start) - { + 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 DimensionUnion(const ArrayDimension& dim1, const A vector res; ArrayDimension* inter = DimensionIntersection(dim1, dim2); if (!inter) - { return { dim1, dim2 }; - } + res.push_back(*inter); delete(inter); + vector diff1, diff2; diff1 = DimensionDifference(dim1, dim2); diff2 = DimensionDifference(dim2, dim1); @@ -118,29 +112,24 @@ static vector DimensionUnion(const ArrayDimension& dim1, const A static vector ElementsIntersection(const vector& firstElement, const vector& 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 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 ElementsIntersection(const vector& static vector> ElementsDifference(const vector& firstElement, const vector& secondElement) { - if (firstElement.empty() || secondElement.empty()) { + if (firstElement.empty() || secondElement.empty()) return {}; - } + vector intersection = ElementsIntersection(firstElement, secondElement); vector> result; if (intersection.empty()) - { return { firstElement }; - } + for (int i = 0; i < firstElement.size(); i++) { auto dimDiff = DimensionDifference(firstElement[i], secondElement[i]); @@ -212,9 +200,8 @@ void AccessingSet::FindCoveredBy(const vector& element, vector& 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> 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> 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> 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; } diff --git a/src/PrivateAnalyzer/range_structures.h b/src/PrivateAnalyzer/range_structures.h index 004f73e..257fbcf 100644 --- a/src/PrivateAnalyzer/range_structures.h +++ b/src/PrivateAnalyzer/range_structures.h @@ -1,9 +1,9 @@ #pragma once -#include -#include -#include -#include +#include +#include +#include +#include struct ArrayDimension { diff --git a/src/PrivateAnalyzer/region.cpp b/src/PrivateAnalyzer/region.cpp index 64375d9..dfe30b1 100644 --- a/src/PrivateAnalyzer/region.cpp +++ b/src/PrivateAnalyzer/region.cpp @@ -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> 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,36 +53,39 @@ pair> GetBasicBlocksForL static void BuildLoopIndex(map& 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& loopForIndex) { unordered_set 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); - } } } } @@ -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 refPos; string array_name; if (operation == SAPFOR::CFG_OP::STORE) - { - array_name = instruction->getInstruction()->getArg1()->getValue(); - } + 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 accessPoint(n); auto* ref = isSgArrayRefExp(instruction->getInstruction()->getExpression()); - vector> coefsForDims; for (int i = 0; ref && i < ref->numberOfSubscripts(); ++i) { @@ -147,52 +145,48 @@ 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()) { - currentLoop = loopForIndex[name]; - } - else { + if (loopForIndex.find(name) != loopForIndex.end()) + currentLoop = loopForIndex[name]; + else return -1; - } } - else { + else + { name = FindIndexName(currentVarPos, block, loopForIndex); - if (name == "") { + if (name == "") return -1; - } - if (loopForIndex.find(name) != loopForIndex.end()) { - currentLoop = loopForIndex[name]; - } - else { + + if (loopForIndex.find(name) != loopForIndex.end()) + currentLoop = loopForIndex[name]; + 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) - { + + if (operation == SAPFOR::CFG_OP::STORE) def[array_name].Insert(accessPoint); - } else - { use[array_name].Insert(accessPoint); - } } } return 0; @@ -204,19 +198,12 @@ static void SetConnections(unordered_map& 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]); - } - } } } @@ -225,24 +212,17 @@ static Region* CreateSubRegion(LoopGraph* loop, const vectorsetHeader(bbToRegion.at(header)); - } + 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& Blocks) SetConnections(bbToRegion, blockSet); //create subRegions for (LoopGraph* childLoop : loop->children) - { subRegions.insert(CreateSubRegion(childLoop, Blocks, bbToRegion)); - } } diff --git a/src/Sapfor.cpp b/src/Sapfor.cpp index 8583e19..9f5ac64 100644 --- a/src/Sapfor.cpp +++ b/src/Sapfor.cpp @@ -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