From 09d0195693c7f33f83c62b16eabba94227656670 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Thu, 28 Mar 2024 21:57:06 +0300 Subject: [PATCH] improved privated removing --- .../_src/Transformations/dead_code.cpp | 9 ++- .../private_arrays_resizing.cpp | 76 ++++++++++++++----- .../_src/Transformations/private_removing.cpp | 10 ++- .../experts/Sapfor_2017/_src/Utils/version.h | 2 +- 4 files changed, 73 insertions(+), 24 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp index 1ec6519..f7493d2 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp @@ -434,7 +434,7 @@ void removeDeadCode(SgStatement* func, } } } -//TODO: need to add [intervalDelStart; intervalDelEnd] + // remove dead statements set removable = { @@ -445,7 +445,10 @@ void removeDeadCode(SgStatement* func, }; vector remove; - for (auto st = func; st != func->lastNodeOfStmt(); st = st->lexNext()) + SgStatement* start = intervalDelStart ? intervalDelStart : func; + SgStatement* end = intervalDelEnd ? intervalDelEnd : func->lastNodeOfStmt(); + + for (auto st = start; st != end; st = st->lexNext()) { if (removable.find(st->variant()) != removable.end() && useful.find(st) == useful.end()) { @@ -462,7 +465,7 @@ void removeDeadCode(SgStatement* func, remove.clear(); //remove empty blocks - for (auto st = func; st != func->lastNodeOfStmt(); st = st->lexNext()) + for (auto st = start; st != end; st = st->lexNext()) { const int var = st->variant(); if ((var == FOR_NODE || var == WHILE_NODE || var == IF_NODE || var == SWITCH_NODE) && diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/private_arrays_resizing.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/private_arrays_resizing.cpp index 013f3c6..f0df286 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/private_arrays_resizing.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/private_arrays_resizing.cpp @@ -89,19 +89,24 @@ static void fillIterationVariables(const LoopGraph* loop, set& vars, int } } -static SgExpression* findSymbolInExprList(SgSymbol* symbol, SgExpression* list) +static SgExpression* findSymbol(SgSymbol* symbol, SgExpression* list) { - while (list) + if (list) { - if (list->variant() != EXPR_LIST || list->lhs()->symbol() == NULL) - return NULL; + if (list->variant() == VAR_REF || list->variant() == ARRAY_REF) + { + if (isEqSymbols(list->symbol(), symbol)) + return list; + } - if (isEqSymbols(list->lhs()->symbol(), symbol)) - return list->lhs(); + auto left = findSymbol(symbol, list->lhs()); + if (left) + return left; - list = list->rhs(); + auto right = findSymbol(symbol, list->rhs()); + if (right) + return right; } - return NULL; } @@ -342,11 +347,40 @@ static void setAllocatable(SgStatement *newDecl, SgSymbol *origSymbol) } } +static SgExpression* checkArrayDecl(SgExpression* array, SgSymbol* arraySymbol, SgStatement* checkedDecl) +{ + if (array->lhs() == NULL) + { + vector allDecls; + auto mainDecl = declaratedInStmt(arraySymbol, &allDecls); + if (allDecls.size() < 2) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + + for (auto& decl : allDecls) + { + if (decl == mainDecl) + continue; + if (decl == checkedDecl) + continue; + + array = findSymbol(arraySymbol, decl->expr(0)); + if (array->lhs()) + break; + array = NULL; + } + + checkNull(array, convertFileName(__FILE__).c_str(), __LINE__); + } + + return array; +} + static SgSymbol* alterExtendArrayDeclaration(const LoopGraph* forLoop, SgStatement* declarationStmt, SgSymbol* arraySymbol, bool canBeStatic, const vector& indexes) { SgSymbol* newArraySymbol = NULL; - SgExpression* array = findSymbolInExprList(arraySymbol, declarationStmt->expr(0)); + SgExpression* array = findSymbol(arraySymbol, declarationStmt->expr(0)); + array = checkArrayDecl(array, arraySymbol, declarationStmt); SgExpression* newArray = array->copyPtr(); newArraySymbol = createNewArrayNameSymbol(newArray, true, canBeStatic); @@ -368,7 +402,7 @@ static SgSymbol* alterShrinkArrayDeclaration(SgStatement *declarationStatement, vector &dimensions, bool canBeStatic) { SgSymbol *newArraySymbol = NULL; - SgExpression *array = findSymbolInExprList(arraySymbol, declarationStatement->expr(0)); + SgExpression *array = findSymbol(arraySymbol, declarationStatement->expr(0)); SgExpression *newArray = array->copyPtr(); newArraySymbol = createNewArrayNameSymbol(newArray, false, canBeStatic); @@ -460,7 +494,9 @@ static SgStatement* createNewDeclarationStatemnet(SgStatement *loop, SgStatement while (!isEqSymbols(exprList->lhs()->symbol(), arraySymbol)) exprList = exprList->rhs(); - SgExpression newExprList(EXPR_LIST, exprList->lhs(), NULL, NULL); + checkNull(exprList, convertFileName(__FILE__).c_str(), __LINE__); + + SgExpression newExprList(EXPR_LIST, exprList->lhs()->copyPtr(), NULL, NULL); SgStatement *newDeclaration = originalDeclaration->copyPtr(); newDeclaration->setExpression(0, newExprList); @@ -504,7 +540,7 @@ static SgStatement* getAllocationStmt(const LoopGraph* loop, SgSymbol* symbol) if (alloc->variant() != ALLOCATE_STMT) continue; - if (NULL == findSymbolInExprList(symbol, alloc->expr(0))) + if (findSymbol(symbol, alloc->expr(0)) == NULL) continue; if (allocationStmt == NULL) @@ -527,10 +563,10 @@ static void fillLowBounds(const LoopGraph* forLoop, SgSymbol* origArraySymbol, S if (isAllocatable(origArraySymbol)) { SgStatement* allocationStmt = getAllocationStmt(forLoop, origArraySymbol); - origArray = findSymbolInExprList(origArraySymbol, allocationStmt->expr(0)); + origArray = findSymbol(origArraySymbol, allocationStmt->expr(0)); } else - origArray = findSymbolInExprList(origArraySymbol, originalDecl->expr(0)); + origArray = findSymbol(origArraySymbol, originalDecl->expr(0)); SgExpression* arrayRef = origArray->copyPtr(); SgExpression* oldTail = arrayRef->lhs(); @@ -596,10 +632,13 @@ static void insertAllocDealloc(const LoopGraph* forLoop, SgSymbol* origArraySymb if (isAllocatable(origArraySymbol)) { SgStatement *allocationStmt = getAllocationStmt(forLoop, origArraySymbol); - origArray = findSymbolInExprList(origArraySymbol, allocationStmt->expr(0)); + origArray = findSymbol(origArraySymbol, allocationStmt->expr(0)); } else - origArray = findSymbolInExprList(origArraySymbol, originalDeclaration->expr(0)); + { + origArray = findSymbol(origArraySymbol, originalDeclaration->expr(0)); + origArray = checkArrayDecl(origArray, origArraySymbol, originalDeclaration); + } int depthOfResize = 0; if (isExpansion) @@ -765,7 +804,7 @@ static void reduceArrayRefs(SgStatement *st, SgSymbol *arraySymbol, SgSymbol *ne static void fillIndexesToShrink(SgSymbol* arr, SgExprListExp* listExp, vector& indexes) { - SgExpression* sym = findSymbolInExprList(arr, listExp); + SgExpression* sym = findSymbol(arr, listExp); if (sym) { SgExpression* expr = sym->lhs(); @@ -1130,7 +1169,8 @@ static SgSymbol* resizeArray(const LoopGraph *forLoop, SgSymbol *arraySymbol, in for (int i = 0; i < depthOfResize; ++i) { - if (curLoop->calculatedCountOfIters == 0) + //TODO: add checking for PARAMETER + //if (curLoop->calculatedCountOfIters == 0) canBeStatic = false; if (areIndexesFilled != 0) diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp index eef5fca..3bcfc20 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/private_removing.cpp @@ -833,6 +833,7 @@ void removePrivates(string filename, vector& messages, const map>& allFuncInfo, int& countOfTransform) { + set removedDC; for (auto& varToRemove : privatesToRemoveGlobal) { if (filename != varToRemove.loop->fileName) @@ -850,7 +851,12 @@ void removePrivates(string filename, vector& messages, if (currFunc == nullptr) printInternalError(convertFileName(__FILE__).c_str(), __LINE__); - removeDeadCode(currFunc->funcPointer, allFuncInfo, commonBlocks); + if (removedDC.find(varToRemove.loop) == removedDC.end()) + { + removeDeadCode(currFunc->funcPointer, allFuncInfo, commonBlocks, + varToRemove.loop->loop, varToRemove.loop->loop->lastNodeOfStmt()); + removedDC.insert(varToRemove.loop); + } vector varRefs = getDirectArrayRefs(loopStmt, varToRemove.varSymbol); int loopLineNum = varToRemove.loop->lineNum; @@ -863,7 +869,7 @@ void removePrivates(string filename, vector& messages, } else { - varRefs = removeDuplicateArrayRefs(varRefs, fixedDimensions, varToRemove.regime, + varRefs = removeDuplicateArrayRefs(varRefs, fixedDimensions, varToRemove.regime, varToRemove.arrayRefToIterationVarsMap); for (auto& varRef : varRefs) { diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/version.h b/sapfor/experts/Sapfor_2017/_src/Utils/version.h index 1d19453..c7604bc 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/version.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/version.h @@ -1,3 +1,3 @@ #pragma once -#define VERSION_SPF "2294" +#define VERSION_SPF "2296"