From 21a08f751bd641f9658455cbf44ecadbf895f5b2 Mon Sep 17 00:00:00 2001 From: Max Graey Date: Tue, 25 Aug 2020 00:06:06 +0300 Subject: Avoid wasted work when there are no locals (#3060) Adds early returns to local optimizations passes in cases where there are no locals to optimize. --- src/passes/RedundantSetElimination.cpp | 3 +++ src/passes/ReorderLocals.cpp | 9 +++++---- src/passes/SimplifyLocals.cpp | 3 +++ 3 files changed, 11 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/passes/RedundantSetElimination.cpp b/src/passes/RedundantSetElimination.cpp index a891653df..e36cefc9e 100644 --- a/src/passes/RedundantSetElimination.cpp +++ b/src/passes/RedundantSetElimination.cpp @@ -81,6 +81,9 @@ struct RedundantSetElimination void doWalkFunction(Function* func) { numLocals = func->getNumLocals(); + if (numLocals == 0) { + return; // nothing to do + } // create the CFG by walking the IR CFGWalker, Info>:: doWalkFunction(func); diff --git a/src/passes/ReorderLocals.cpp b/src/passes/ReorderLocals.cpp index 7b4c99e78..8d7c0f1d9 100644 --- a/src/passes/ReorderLocals.cpp +++ b/src/passes/ReorderLocals.cpp @@ -46,6 +46,9 @@ struct ReorderLocals : public WalkerPass> { enum { Unseen = 0 }; void doWalkFunction(Function* curr) { + if (curr->getNumVars() == 0) { + return; // nothing to do. All locals are parameters + } Index num = curr->getNumLocals(); counts.resize(num); std::fill(counts.begin(), counts.end(), 0); @@ -54,9 +57,9 @@ struct ReorderLocals : public WalkerPass> { // Gather information about local usages. walk(curr->body); // Use the information about local usages. - std::vector newToOld; + std::vector newToOld(num); for (size_t i = 0; i < num; i++) { - newToOld.push_back(i); + newToOld[i] = i; } // sort, keeping params in front (where they will not be moved) sort( @@ -82,8 +85,6 @@ struct ReorderLocals : public WalkerPass> { size_t numParams = curr->sig.params.size(); for (size_t i = 0; i < numParams; i++) { assert(newToOld[i] < numParams); - } - for (size_t i = 0; i < numParams; i++) { newToOld[i] = i; } // sort vars, and drop unused ones diff --git a/src/passes/SimplifyLocals.cpp b/src/passes/SimplifyLocals.cpp index ca3d714f4..9c831a551 100644 --- a/src/passes/SimplifyLocals.cpp +++ b/src/passes/SimplifyLocals.cpp @@ -811,6 +811,9 @@ struct SimplifyLocals } void doWalkFunction(Function* func) { + if (func->getNumLocals() == 0) { + return; // nothing to do + } // scan local.gets getCounter.analyze(func); // multiple passes may be required per function, consider this: -- cgit v1.2.3