dvm updated, fixed and improved dead_code pass

This commit is contained in:
ALEXks
2024-03-22 12:00:01 +03:00
parent 0f39f6474d
commit d27b9d1ed3
7 changed files with 64 additions and 80 deletions

View File

@@ -26,7 +26,6 @@ extern "C" void exit(int status);
#include "extcxx_low.h" #include "extcxx_low.h"
extern "C" int number_of_ll_node; extern "C" int number_of_ll_node;
extern "C" PTR_SYMB last_file_symbol; extern "C" PTR_SYMB last_file_symbol;
extern "C" PTR_SYMB FileLastSymbol(...);
#undef USER #undef USER
@@ -1647,7 +1646,7 @@ SgFile &SgProject::file(int i)
#ifdef __SPF #ifdef __SPF
SgStatement::setCurrProcessFile(pt->filename()); SgStatement::setCurrProcessFile(pt->filename());
SgStatement::setCurrProcessLine(0); SgStatement::setCurrProcessLine(0);
last_file_symbol = FileLastSymbol(pt->filename()); last_file_symbol = file->cur_symb;
#endif #endif
return *pt; return *pt;
} }
@@ -1775,13 +1774,13 @@ int SgFile::switchToFile(const std::string &name)
SgFile *file = &(CurrentProject->file(it->second.second)); SgFile *file = &(CurrentProject->file(it->second.second));
current_file_id = it->second.second; current_file_id = it->second.second;
current_file = file; current_file = file;
SgStatement::setCurrProcessFile(file->filename()); SgStatement::setCurrProcessFile(file->filename());
SgStatement::setCurrProcessLine(0); SgStatement::setCurrProcessLine(0);
last_file_symbol = current_file->filept->cur_symb;
} }
} }
last_file_symbol = FileLastSymbol(name.c_str());
return it->second.second; return it->second.second;
} }

View File

@@ -83,12 +83,6 @@ int out_upper_case;
int out_line_unlimit; int out_line_unlimit;
int out_line_length; // out_line_length = 132 for -ffo mode; out_line_length = 72 for -uniForm mode int out_line_length; // out_line_length = 132 for -ffo mode; out_line_length = 72 for -uniForm mode
PTR_SYMB last_file_symbol; PTR_SYMB last_file_symbol;
struct file_symbol {
char *fname;
PTR_SYMB last_symb;
struct file_symbol *next;
} ;
struct file_symbol *file_last_symbol = NULL;
static int CountNullBifNext = 0; /* for internal debugging */ static int CountNullBifNext = 0; /* for internal debugging */
@@ -253,24 +247,6 @@ char* mymalloc(int size)
return pt1; return pt1;
} }
PTR_SYMB FileLastSymbol(char *file_name)
{
struct file_symbol *fsl;
for(fsl=file_last_symbol; fsl; fsl=fsl->next)
if(!strcmp(file_name, fsl->fname))
return fsl->last_symb;
return NULL;
}
void addFileSymbolList(char *file_name, PTR_SYMB symb)
{
struct file_symbol *fsl;
fsl = (struct file_symbol *) xmalloc(sizeof (struct file_symbol));
fsl->last_symb = symb;
fsl->fname = file_name;
fsl->next = file_last_symbol;
file_last_symbol = fsl;
}
/***************** Provides infos on nodes ******************************** /***************** Provides infos on nodes ********************************
* * * *
* based on the table info in include dir *.def * * based on the table info in include dir *.def *
@@ -1228,7 +1204,6 @@ int Init_Tool_Box()
number_of_ll_node = CUR_FILE_NUM_LLNDS() + 1; number_of_ll_node = CUR_FILE_NUM_LLNDS() + 1;
number_of_bif_node = CUR_FILE_NUM_BIFS() + 1; number_of_bif_node = CUR_FILE_NUM_BIFS() + 1;
number_of_symb_node = CUR_FILE_NUM_SYMBS() + 1; number_of_symb_node = CUR_FILE_NUM_SYMBS() + 1;
addFileSymbolList(CUR_FILE_NAME(), CUR_FILE_CUR_SYMB()); /* podd 01.03.24 */
if (CUR_FILE_NAME()) strcpy(Current_File_name, CUR_FILE_NAME()); if (CUR_FILE_NAME()) strcpy(Current_File_name, CUR_FILE_NAME());
if (ToolBOX_INIT) if (ToolBOX_INIT)
return 0; return 0;

View File

@@ -82,7 +82,6 @@ extern "C" int out_upper_case;
extern "C" int out_line_unlimit; extern "C" int out_line_unlimit;
extern "C" int out_line_length; extern "C" int out_line_length;
extern "C" PTR_SYMB last_file_symbol; extern "C" PTR_SYMB last_file_symbol;
extern "C" PTR_SYMB FileLastSymbol(...);
Options options; Options options;
@@ -418,7 +417,7 @@ int main(int argc, char *argv[])
fout_name_info_C = ChangeFto_info_C(fout_name); /*ACC*/ fout_name_info_C = ChangeFto_info_C(fout_name); /*ACC*/
//set the last symbol of file //set the last symbol of file
last_file_symbol = FileLastSymbol(file->filename()); //for low_level.c and not only last_file_symbol = file->filept->cur_symb; //for low_level.c and not only
initLibNames(); //for every file initLibNames(); //for every file
InitDVM(file); //for every file InitDVM(file); //for every file
current_file = file; // global variable (used in SgTypeComplex) current_file = file; // global variable (used in SgTypeComplex)

View File

@@ -31,7 +31,6 @@ extern "C" int out_upper_case;
extern "C" int out_line_unlimit; extern "C" int out_line_unlimit;
extern "C" int out_line_length; extern "C" int out_line_length;
extern "C" PTR_SYMB last_file_symbol; extern "C" PTR_SYMB last_file_symbol;
extern "C" PTR_SYMB FileLastSymbol(...);
static int convertFile(int argc, char* argv[], const set<string>& filesInProj, const set<string>& moduleDeclsInFiles) static int convertFile(int argc, char* argv[], const set<string>& filesInProj, const set<string>& moduleDeclsInFiles)
{ {
@@ -353,7 +352,6 @@ static int convertFile(int argc, char* argv[], const set<string>& filesInProj, c
fout_name_info_C = ChangeFto_info_C(fout_name); /*ACC*/ fout_name_info_C = ChangeFto_info_C(fout_name); /*ACC*/
//set the last symbol of file //set the last symbol of file
last_file_symbol = FileLastSymbol(file->filename());
initLibNames(); //for every file initLibNames(); //for every file
InitDVM(file); //for every file InitDVM(file); //for every file
current_file = file; // global variable (used in SgTypeComplex) current_file = file; // global variable (used in SgTypeComplex)

View File

@@ -13,7 +13,7 @@ using std::set;
using std::remove_if; using std::remove_if;
#define PRINT_USELESS_STATEMENTS 0 #define PRINT_USELESS_STATEMENTS 1
static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* instr, static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instruction* instr,
set<SAPFOR::Argument*>& use, set<SAPFOR::Argument*>& def, set<SAPFOR::Argument*>& use, set<SAPFOR::Argument*>& def,
@@ -329,10 +329,22 @@ public:
void removeDeadCode(SgStatement* func, void removeDeadCode(SgStatement* func,
const map<string, vector<FuncInfo*>>& allFuncs, const map<string, vector<FuncInfo*>>& allFuncs,
const map<string, CommonBlock*>& commonBlocks) const map<string, CommonBlock*>& commonBlocks,
SgStatement* intervalDelStart, SgStatement* intervalDelEnd)
{ {
if (intervalDelStart && !intervalDelEnd || !intervalDelStart && intervalDelEnd)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
SgProgHedrStmt* prog = isSgProgHedrStmt(func); SgProgHedrStmt* prog = isSgProgHedrStmt(func);
if (intervalDelStart)
if (intervalDelStart->lineNumber() < prog->lineNumber() || intervalDelStart->lineNumber() > prog->lastNodeOfStmt()->lineNumber())
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
if (intervalDelEnd)
if (intervalDelEnd->lineNumber() < prog->lineNumber() || intervalDelEnd->lineNumber() > prog->lastNodeOfStmt()->lineNumber())
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
auto cfg = buildCFGforCurrentFunc(func, SAPFOR::CFG_Settings(true, false, false, false, false, false, false), commonBlocks, allFuncs); auto cfg = buildCFGforCurrentFunc(func, SAPFOR::CFG_Settings(true, false, false, false, false, false, false), commonBlocks, allFuncs);
if(cfg.size() != 1) if(cfg.size() != 1)
@@ -422,11 +434,8 @@ void removeDeadCode(SgStatement* func,
} }
} }
} }
//TODO: need to add [intervalDelStart; intervalDelEnd]
// remove dead statements // remove dead statements
SgStatement* end = func->lastNodeOfStmt(), *st = func;
set<int> removable = set<int> removable =
{ {
ASSIGN_STAT, ASSIGN_STAT,
@@ -435,49 +444,52 @@ void removeDeadCode(SgStatement* func,
READ_STAT READ_STAT
}; };
while (st != end) vector<SgStatement*> remove;
for (auto st = func; st != func->lastNodeOfStmt(); st = st->lexNext())
{ {
SgStatement* next = st->lexNext();
if (removable.find(st->variant()) != removable.end() && useful.find(st) == useful.end()) if (removable.find(st->variant()) != removable.end() && useful.find(st) == useful.end())
{
remove.push_back(st);
st = st->lastNodeOfStmt();
}
}
for (auto& rem : remove)
{
__spf_print(PRINT_USELESS_STATEMENTS, "[Useless statement on line %d and file %s]\n", rem->lineNumber(), rem->fileName());
rem->deleteStmt();
}
remove.clear();
//remove empty blocks
for (auto st = func; st != func->lastNodeOfStmt(); st = st->lexNext())
{
const int var = st->variant();
if ((var == FOR_NODE || var == WHILE_NODE || var == IF_NODE || var == SWITCH_NODE) &&
st->lexNext()->variant() == CONTROL_END)
{ {
__spf_print(PRINT_USELESS_STATEMENTS, "[Useless statement '%s' on line %d]\n", st->unparse(), st->lineNumber()); remove.push_back(st);
continue;
st->deleteStmt();
}
else
{
if (isSgControlEndStmt(st))
{
SgStatement* parent = st->controlParent();
SgStatement* parent_end;
if (parent && (parent_end = parent->lastNodeOfStmt()) && parent_end == st)
{
bool empty_parent = false;
switch (parent->variant())
{
case IF_NODE:
empty_parent =
parent->lexNext() == parent_end || // IF THEN ENDIF
isSgControlEndStmt(parent->lexNext()) &&
parent->lexNext()->lexNext() == parent_end; // IF THEN ELSE ENDIF
break;
default:
empty_parent = parent->lexNext() == parent_end; // DO, WHILE
break;
}
if (empty_parent)
parent->deleteStmt();
else if(isSgIfStmt(parent) && isSgControlEndStmt(parent_end->lexPrev())) // IF with empty ELSE branch
parent_end->deleteStmt();
}
}
} }
st = next; if (var == IF_NODE)
{
SgStatement* ifS = st;
while (ifS->lexNext()->variant() == ELSEIF_NODE)
ifS = ifS->lexNext();
if (ifS->lexNext()->variant() == CONTROL_END)
remove.push_back(st);
}
//TODO: SWITCH and other block statements
}
for (auto& rem : remove)
{
__spf_print(PRINT_USELESS_STATEMENTS, "[Useless block statement on line %d and file %s]\n", rem->lineNumber(), rem->fileName());
rem->deleteStmt();
} }
deleteCFG(cfg); deleteCFG(cfg);

View File

@@ -11,4 +11,5 @@
void removeDeadCode(SgStatement* func, void removeDeadCode(SgStatement* func,
const std::map<std::string, std::vector<FuncInfo*>>&allFuncs, const std::map<std::string, std::vector<FuncInfo*>>&allFuncs,
const std::map<std::string, CommonBlock*>& commonBlocks); const std::map<std::string, CommonBlock*>& commonBlocks,
SgStatement* intervalDelStart = NULL, SgStatement* intervalDelEnd = NULL);

View File

@@ -1,3 +1,3 @@
#pragma once #pragma once
#define VERSION_SPF "2289" #define VERSION_SPF "2290"