diff options
author | chriseth <chris@ethereum.org> | 2018-10-16 23:21:14 +0800 |
---|---|---|
committer | chriseth <chris@ethereum.org> | 2018-10-16 23:32:21 +0800 |
commit | 2ab6430303406b03191cb7e14ecd1384104b12fa (patch) | |
tree | c506bc291f598afe9fd12bbcc3e76dfe01be9261 /libyul | |
parent | a435a14e135a4e31ce3cd2bacd32544b2b342074 (diff) | |
download | dexon-solidity-2ab6430303406b03191cb7e14ecd1384104b12fa.tar.gz dexon-solidity-2ab6430303406b03191cb7e14ecd1384104b12fa.tar.zst dexon-solidity-2ab6430303406b03191cb7e14ecd1384104b12fa.zip |
Inline each function separately.
Diffstat (limited to 'libyul')
-rw-r--r-- | libyul/optimiser/FullInliner.cpp | 35 | ||||
-rw-r--r-- | libyul/optimiser/FullInliner.h | 6 |
2 files changed, 13 insertions, 28 deletions
diff --git a/libyul/optimiser/FullInliner.cpp b/libyul/optimiser/FullInliner.cpp index 75cd9d5c..cd0ed52a 100644 --- a/libyul/optimiser/FullInliner.cpp +++ b/libyul/optimiser/FullInliner.cpp @@ -50,7 +50,6 @@ FullInliner::FullInliner(Block& _ast): assertThrow(m_ast.statements.at(i).type() == typeid(FunctionDefinition), OptimizerException, ""); FunctionDefinition& fun = boost::get<FunctionDefinition>(m_ast.statements.at(i)); m_functions[fun.name] = &fun; - m_functionsToVisit.insert(&fun); } } @@ -59,17 +58,8 @@ void FullInliner::run() assertThrow(m_ast.statements[0].type() == typeid(Block), OptimizerException, ""); handleBlock("", boost::get<Block>(m_ast.statements[0])); - while (!m_functionsToVisit.empty()) - handleFunction(**m_functionsToVisit.begin()); -} - -void FullInliner::handleFunction(FunctionDefinition& _fun) -{ - if (!m_functionsToVisit.count(&_fun)) - return; - m_functionsToVisit.erase(&_fun); - - handleBlock(_fun.name, _fun.body); + for (auto const& fun: m_functions) + handleBlock(fun.second->name, fun.second->body); } void FullInliner::handleBlock(string const& _currentFunctionName, Block& _block) @@ -102,28 +92,27 @@ boost::optional<vector<Statement>> InlineModifier::tryInlineStatement(Statement& ), *e); if (funCall) { - FunctionDefinition& fun = m_driver.function(funCall->functionName.name); - m_driver.handleFunction(fun); - // TODO: Insert good heuristic here. Perhaps implement that inside the driver. bool doInline = funCall->functionName.name != m_currentFunction; if (doInline) - return performInline(_statement, *funCall, fun); + return performInline(_statement, *funCall); } } return {}; } -vector<Statement> InlineModifier::performInline(Statement& _statement, FunctionCall& _funCall, FunctionDefinition& _function) +vector<Statement> InlineModifier::performInline(Statement& _statement, FunctionCall& _funCall) { vector<Statement> newStatements; map<string, string> variableReplacements; + FunctionDefinition& function = m_driver.function(_funCall.functionName.name); + // helper function to create a new variable that is supposed to model // an existing variable. auto newVariable = [&](TypedName const& _existingVariable, Expression* _value) { - string newName = m_nameDispenser.newName(_function.name + "_" + _existingVariable.name); + string newName = m_nameDispenser.newName(function.name + "_" + _existingVariable.name); variableReplacements[_existingVariable.name] = newName; VariableDeclaration varDecl{_funCall.location, {{_funCall.location, newName, _existingVariable.type}}, {}}; if (_value) @@ -132,11 +121,11 @@ vector<Statement> InlineModifier::performInline(Statement& _statement, FunctionC }; for (size_t i = 0; i < _funCall.arguments.size(); ++i) - newVariable(_function.parameters[i], &_funCall.arguments[i]); - for (auto const& var: _function.returnVariables) + newVariable(function.parameters[i], &_funCall.arguments[i]); + for (auto const& var: function.returnVariables) newVariable(var, nullptr); - Statement newBody = BodyCopier(m_nameDispenser, _function.name + "_", variableReplacements)(_function.body); + Statement newBody = BodyCopier(m_nameDispenser, function.name + "_", variableReplacements)(function.body); newStatements += std::move(boost::get<Block>(newBody).statements); boost::apply_visitor(GenericFallbackVisitor<Assignment, VariableDeclaration>{ @@ -148,7 +137,7 @@ vector<Statement> InlineModifier::performInline(Statement& _statement, FunctionC {_assignment.variableNames[i]}, make_shared<Expression>(Identifier{ _assignment.location, - variableReplacements.at(_function.returnVariables[i].name) + variableReplacements.at(function.returnVariables[i].name) }) }); }, @@ -160,7 +149,7 @@ vector<Statement> InlineModifier::performInline(Statement& _statement, FunctionC {std::move(_varDecl.variables[i])}, make_shared<Expression>(Identifier{ _varDecl.location, - variableReplacements.at(_function.returnVariables[i].name) + variableReplacements.at(function.returnVariables[i].name) }) }); } diff --git a/libyul/optimiser/FullInliner.h b/libyul/optimiser/FullInliner.h index b69350a6..495286c0 100644 --- a/libyul/optimiser/FullInliner.h +++ b/libyul/optimiser/FullInliner.h @@ -75,9 +75,6 @@ public: void run(); - /// Perform inlining operations inside the given function. - void handleFunction(FunctionDefinition& _function); - FunctionDefinition& function(std::string _name) { return *m_functions.at(_name); } private: @@ -87,7 +84,6 @@ private: /// we store pointers to functions. Block& m_ast; std::map<std::string, FunctionDefinition*> m_functions; - std::set<FunctionDefinition*> m_functionsToVisit; NameDispenser m_nameDispenser; }; @@ -108,7 +104,7 @@ public: private: boost::optional<std::vector<Statement>> tryInlineStatement(Statement& _statement); - std::vector<Statement> performInline(Statement& _statement, FunctionCall& _funCall, FunctionDefinition& _function); + std::vector<Statement> performInline(Statement& _statement, FunctionCall& _funCall); std::string newName(std::string const& _prefix); |