fix for AST optimization altering top-level body statement

This commit is contained in:
Vern Paxson 2021-05-30 17:37:44 -07:00
parent 22af54dda2
commit 143d306883
3 changed files with 17 additions and 5 deletions

View file

@ -130,13 +130,19 @@ void optimize_func(ScriptFunc* f, std::shared_ptr<ProfileFunc> pf,
rc->SetDefSetsMgr(reduced_rds.GetDefSetsMgr()); rc->SetDefSetsMgr(reduced_rds.GetDefSetsMgr());
auto ud = std::make_unique<UseDefs>(body, rc); auto ud = std::make_shared<UseDefs>(body, rc);
ud->Analyze(); ud->Analyze();
if ( analysis_options.dump_uds ) if ( analysis_options.dump_uds )
ud->Dump(); ud->Dump();
ud->RemoveUnused(); new_body = ud->RemoveUnused();
if ( new_body != body )
{
f->ReplaceBody(body, new_body);
body = new_body;
}
int new_frame_size = int new_frame_size =
scope->Length() + rc->NumTemps() + rc->NumNewLocals(); scope->Length() + rc->NumTemps() + rc->NumNewLocals();

View file

@ -37,7 +37,7 @@ void UseDefs::Analyze()
(void) PropagateUDs(body, nullptr, nullptr, false); (void) PropagateUDs(body, nullptr, nullptr, false);
} }
void UseDefs::RemoveUnused() StmtPtr UseDefs::RemoveUnused()
{ {
int iter = 0; int iter = 0;
while ( RemoveUnused(++iter) ) while ( RemoveUnused(++iter) )
@ -48,6 +48,8 @@ void UseDefs::RemoveUnused()
if ( reporter->Errors() > 0 ) if ( reporter->Errors() > 0 )
break; break;
} }
return body;
} }
void UseDefs::Dump() void UseDefs::Dump()

View file

@ -57,14 +57,18 @@ public:
// True if we've computed use-defs for the given statement. // True if we've computed use-defs for the given statement.
bool HasUsage(const Stmt* s) const bool HasUsage(const Stmt* s) const
{ return use_defs_map.find(s) != use_defs_map.end(); } { return use_defs_map.find(s) != use_defs_map.end(); }
bool HasUsage(const StmtPtr& s) const
{ return HasUsage(s.get()); }
// Returns the use-defs for the given statement. // Returns the use-defs for the given statement.
UDs GetUsage(const Stmt* s) const { return FindUsage(s); } UDs GetUsage(const Stmt* s) const { return FindUsage(s); }
UDs GetUsage(const StmtPtr& s) const { return FindUsage(s.get()); }
// Removes assignments corresponding to unused temporaries. // Removes assignments corresponding to unused temporaries.
// In the process, reports on locals that are assigned // In the process, reports on locals that are assigned
// but never used. // but never used. Returns the body, which may have been
void RemoveUnused(); // changed if the original first statement has been pruned.
StmtPtr RemoveUnused();
void Dump(); void Dump();