switch simple loops that don't need indices to being iterator-based

This commit is contained in:
Vern Paxson 2021-08-19 09:38:50 -07:00
parent ffd1905f90
commit d609a11312
8 changed files with 34 additions and 52 deletions

View file

@ -145,9 +145,8 @@ void CPPCompile::ExpandListTypeVar(const TypePtr& t, string& tn)
const auto& tl = t->AsTypeList()->GetTypes();
auto t_name = tn + "->AsTypeList()";
for ( auto i = 0u; i < tl.size(); ++i )
AddInit(t, t_name + "->Append(" +
GenTypeName(tl[i]) + ");");
for ( auto& tl_i : tl )
AddInit(t, t_name + "->Append(" + GenTypeName(tl_i) + ");");
}
void CPPCompile::ExpandRecordTypeVar(const TypePtr& t, string& tn)
@ -459,10 +458,10 @@ void CPPCompile::RegisterListType(const TypePtr& t)
{
const auto& tl = t->AsTypeList()->GetTypes();
for ( auto i = 0u; i < tl.size(); ++i )
for ( auto& tl_i : tl )
{
NoteNonRecordInitDependency(t, tl[i]);
RegisterType(tl[i]);
NoteNonRecordInitDependency(t, tl_i);
RegisterType(tl_i);
}
}
@ -489,10 +488,8 @@ void CPPCompile::RegisterRecordType(const TypePtr& t)
if ( ! r )
return;
for ( auto i = 0; i < r->length(); ++i )
for ( const auto& r_i : *r )
{
const auto& r_i = (*r)[i];
NoteNonRecordInitDependency(t, r_i->type);
RegisterType(r_i->type);

View file

@ -1107,8 +1107,8 @@ static ExprPtr build_disjunction(std::vector<ConstExprPtr>& patterns)
ExprPtr e = patterns[0];
for ( unsigned int i = 1; i < patterns.size(); ++i )
e = make_intrusive<BitExpr>(EXPR_OR, e, patterns[i]);
for ( auto& p : patterns )
e = make_intrusive<BitExpr>(EXPR_OR, e, p);
return e;
}

View file

@ -514,8 +514,8 @@ int IDOptInfo::ActiveRegionIndex()
void IDOptInfo::DumpBlocks() const
{
for ( auto i = 0; i < usage_regions.size(); ++i )
usage_regions[i].Dump();
for ( auto& ur : usage_regions )
ur.Dump();
printf("<end>\n");
}

View file

@ -79,10 +79,8 @@ bool UseDefs::RemoveUnused(int iter)
bool did_omission = false;
for ( unsigned int i = 0; i < stmts.size(); ++i )
for ( const auto& s : stmts )
{
const auto& s = stmts[i];
if ( s->Tag() == STMT_INIT )
{
auto init = s->AsInitStmt();

View file

@ -155,9 +155,8 @@ bool ZAMCompiler::RemoveDeadCode()
bool did_removal = false;
for ( unsigned int i = 0; i < insts1.size() - 1; ++i )
for ( auto& i0 : insts1 )
{
auto i0 = insts1[i];
if ( ! i0->live )
continue;
@ -205,9 +204,8 @@ bool ZAMCompiler::CollapseGoTos()
{
bool did_change = false;
for ( unsigned int i = 0; i < insts1.size(); ++i )
for ( auto& i0 : insts1 )
{
auto i0 = insts1[i];
auto orig_t = i0->target;
if ( ! i0->live || ! orig_t || orig_t == pending_inst )
@ -524,9 +522,8 @@ void ZAMCompiler::ReMapFrame()
std::vector<GlobalInfo> used_globals;
std::vector<int> remapped_globals;
for ( unsigned int i = 0; i < globalsI.size(); ++i )
for ( auto& g : globalsI )
{
auto& g = globalsI[i];
g.slot = frame1_to_frame2[g.slot];
if ( g.slot >= 0 )
{
@ -890,12 +887,9 @@ const ZInstI* ZAMCompiler::EndOfLoop(const ZInstI* inst, int depth) const
bool ZAMCompiler::VarIsAssigned(int slot) const
{
for ( unsigned int i = 0; i < insts1.size(); ++i )
{
auto& inst = insts1[i];
for ( auto& inst : insts1 )
if ( inst->live && VarIsAssigned(slot, inst) )
return true;
}
return false;
}
@ -932,9 +926,8 @@ bool ZAMCompiler::VarIsAssigned(int slot, const ZInstI* i) const
bool ZAMCompiler::VarIsUsed(int slot) const
{
for ( unsigned int i = 0; i < insts1.size(); ++i )
for ( auto& inst : insts1 )
{
auto& inst = insts1[i];
if ( inst->live && inst->UsesSlot(slot) )
return true;

View file

@ -17,10 +17,8 @@ void ZAMCompiler::PushGoTos(GoToSets& gotos)
void ZAMCompiler::ResolveGoTos(GoToSets& gotos, const InstLabel l)
{
auto& g = gotos.back();
for ( auto i = 0U; i < g.size(); ++i )
SetGoTo(g[i], l);
for ( auto& gi : gotos.back() )
SetGoTo(gi, l);
gotos.pop_back();
}

View file

@ -173,12 +173,12 @@ StmtPtr ZAMCompiler::CompileBody()
// Dead instructions map to -1.
std::vector<int> inst1_to_inst2;
for ( auto i = 0U; i < insts1.size(); ++i )
for ( auto& i1 : insts1 )
{
if ( insts1[i]->live )
if ( i1->live )
{
inst1_to_inst2.push_back(insts2.size());
insts2.push_back(insts1[i]);
insts2.push_back(i1);
}
else
inst1_to_inst2.push_back(-1);
@ -284,9 +284,8 @@ void ZAMCompiler::ComputeLoopLevels()
void ZAMCompiler::AdjustBranches()
{
// Move branches to dead code forward to their successor live code.
for ( auto i = 0U; i < insts1.size(); ++i )
for ( auto& inst : insts1 )
{
auto inst = insts1[i];
if ( ! inst->live )
continue;
@ -301,9 +300,8 @@ void ZAMCompiler::AdjustBranches()
void ZAMCompiler::RetargetBranches()
{
for ( auto i = 0U; i < insts2.size(); ++i )
for ( auto& inst : insts2 )
{
auto inst = insts2[i];
if ( ! inst->target )
continue;
@ -313,10 +311,8 @@ void ZAMCompiler::RetargetBranches()
void ZAMCompiler::RemapFrameDenizens(const std::vector<int>& inst1_to_inst2)
{
for ( auto i = 0U; i < shared_frame_denizens.size(); ++i )
for ( auto& info : shared_frame_denizens )
{
auto& info = shared_frame_denizens[i];
for ( auto& start : info.id_start )
{
// It can happen that the identifier's
@ -339,10 +335,10 @@ void ZAMCompiler::RemapFrameDenizens(const std::vector<int>& inst1_to_inst2)
void ZAMCompiler::CreateSharedFrameDenizens()
{
for ( auto i = 0U; i < frame_denizens.size(); ++i )
for ( auto& fd : frame_denizens )
{
FrameSharingInfo info;
info.ids.push_back(frame_denizens[i]);
info.ids.push_back(fd);
info.id_start.push_back(0);
info.scope_end = insts2.size();

View file

@ -132,8 +132,8 @@ ZBody::ZBody(const char* _func_name, const ZAMCompiler* zc)
// Concretize the names of the frame denizens.
for ( auto& f : frame_denizens )
for ( auto i = 0U; i < f.ids.size(); ++i )
f.names.push_back(f.ids[i]->Name());
for ( auto& id : f.ids )
f.names.push_back(id->Name());
managed_slots = zc->ManagedSlots();
@ -149,8 +149,8 @@ ZBody::ZBody(const char* _func_name, const ZAMCompiler* zc)
{
fixed_frame = new ZVal[frame_size];
for ( auto i = 0U; i < managed_slots.size(); ++i )
fixed_frame[managed_slots[i]].ClearManagedVal();
for ( auto& ms : managed_slots )
fixed_frame[ms].ClearManagedVal();
}
table_iters = zc->GetTableIters();
@ -334,9 +334,9 @@ ValPtr ZBody::DoExec(Frame* f, int start_pc, StmtFlowType& flow)
// Free slots for which we do explicit memory management,
// preparing them for reuse.
for ( auto i = 0U; i < managed_slots.size(); ++i )
for ( auto& ms : managed_slots )
{
auto& v = frame[managed_slots[i]];
auto& v = frame[ms];
ZVal::DeleteManagedType(v);
v.ClearManagedVal();
}
@ -346,9 +346,9 @@ ValPtr ZBody::DoExec(Frame* f, int start_pc, StmtFlowType& flow)
// Free those slots for which we do explicit memory management.
// No need to then clear them, as we're about to throw away
// the entire frame.
for ( auto i = 0U; i < managed_slots.size(); ++i )
for ( auto& ms : managed_slots )
{
auto& v = frame[managed_slots[i]];
auto& v = frame[ms];
ZVal::DeleteManagedType(v);
}