libyul: changing some AST members from shared_ptr<> to unique_ptr<>

* Some spaces look a little more verbose now, but that shouln't be a problem as it also should raise readability, too.
* This makes some use of return-value-optimizations also.
This commit is contained in:
Christian Parpart
2019-01-16 14:58:59 +01:00
parent 778b14de26
commit 065c3c87af
11 changed files with 68 additions and 50 deletions
+3 -2
View File
@@ -93,10 +93,11 @@ protected:
std::vector<T> translateVector(std::vector<T> const& _values);
template <typename T>
std::shared_ptr<T> translate(std::shared_ptr<T> const& _v)
std::unique_ptr<T> translate(std::unique_ptr<T> const& _v)
{
return _v ? std::make_shared<T>(translate(*_v)) : nullptr;
return _v ? std::make_unique<T>(translate(*_v)) : nullptr;
}
Block translate(Block const& _block);
Case translate(Case const& _case);
Identifier translate(Identifier const& _identifier);
+2
View File
@@ -51,8 +51,10 @@ void DataFlowAnalyzer::operator()(VariableDeclaration& _varDecl)
for (auto const& var: _varDecl.variables)
names.emplace(var.name);
m_variableScopes.back().variables += names;
if (_varDecl.value)
visit(*_varDecl.value);
handleAssignment(names, _varDecl.value.get());
}
+1 -1
View File
@@ -106,7 +106,7 @@ void ExpressionSplitter::outlineExpression(Expression& _expr)
m_statementsToPrefix.emplace_back(VariableDeclaration{
location,
{{TypedName{location, var, {}}}},
make_shared<Expression>(std::move(_expr))
make_unique<Expression>(std::move(_expr))
});
_expr = Identifier{location, var};
}
+6 -6
View File
@@ -181,9 +181,9 @@ vector<Statement> InlineModifier::performInline(Statement& _statement, FunctionC
variableReplacements[_existingVariable.name] = newName;
VariableDeclaration varDecl{_funCall.location, {{_funCall.location, newName, _existingVariable.type}}, {}};
if (_value)
varDecl.value = make_shared<Expression>(std::move(*_value));
varDecl.value = make_unique<Expression>(std::move(*_value));
else
varDecl.value = make_shared<Expression>(zero);
varDecl.value = make_unique<Expression>(zero);
newStatements.emplace_back(std::move(varDecl));
};
@@ -202,7 +202,7 @@ vector<Statement> InlineModifier::performInline(Statement& _statement, FunctionC
newStatements.emplace_back(Assignment{
_assignment.location,
{_assignment.variableNames[i]},
make_shared<Expression>(Identifier{
make_unique<Expression>(Identifier{
_assignment.location,
variableReplacements.at(function->returnVariables[i].name)
})
@@ -214,7 +214,7 @@ vector<Statement> InlineModifier::performInline(Statement& _statement, FunctionC
newStatements.emplace_back(VariableDeclaration{
_varDecl.location,
{std::move(_varDecl.variables[i])},
make_shared<Expression>(Identifier{
make_unique<Expression>(Identifier{
_varDecl.location,
variableReplacements.at(function->returnVariables[i].name)
})
@@ -232,10 +232,10 @@ Statement BodyCopier::operator()(VariableDeclaration const& _varDecl)
return ASTCopier::operator()(_varDecl);
}
Statement BodyCopier::operator()(FunctionDefinition const& _funDef)
Statement BodyCopier::operator()(FunctionDefinition const&)
{
assertThrow(false, OptimizerException, "Function hoisting has to be done before function inlining.");
return _funDef;
return {};
}
YulString BodyCopier::translateIdentifier(YulString _name)
+14 -9
View File
@@ -66,12 +66,13 @@ void SSATransform::operator()(Block& _block)
// Creates a new variable (and returns its declaration) with value _value
// and replaces _value by a reference to that new variable.
auto replaceByNew = [&](SourceLocation _loc, YulString _varName, YulString _type, shared_ptr<Expression>& _value) -> VariableDeclaration
auto replaceByNew = [&](SourceLocation _loc, YulString _varName, YulString _type, unique_ptr<Expression>& _value) -> VariableDeclaration
{
YulString newName = m_nameDispenser.newName(_varName);
m_currentVariableValues[_varName] = newName;
variablesToClearAtEnd.emplace(_varName);
shared_ptr<Expression> v = make_shared<Expression>(Identifier{_loc, newName});
unique_ptr<Expression> v = make_unique<Expression>(Identifier{_loc, newName});
_value.swap(v);
return VariableDeclaration{_loc, {TypedName{_loc, std::move(newName), std::move(_type)}}, std::move(v)};
};
@@ -87,14 +88,16 @@ void SSATransform::operator()(Block& _block)
visit(*varDecl.value);
if (varDecl.variables.size() != 1 || !m_variablesToReplace.count(varDecl.variables.front().name))
return {};
// Replace "let a := v" by "let a_1 := v let a := v"
VariableDeclaration newVarDecl = replaceByNew(
vector<Statement> v;
// Replace "let a := v" by "let a_1 := v let a := a_1"
v.emplace_back(replaceByNew(
varDecl.location,
varDecl.variables.front().name,
varDecl.variables.front().type,
varDecl.value
);
return vector<Statement>{std::move(newVarDecl), std::move(varDecl)};
));
v.emplace_back(move(varDecl));
return v;
}
else if (_s.type() == typeid(Assignment))
{
@@ -103,14 +106,16 @@ void SSATransform::operator()(Block& _block)
if (assignment.variableNames.size() != 1)
return {};
assertThrow(m_variablesToReplace.count(assignment.variableNames.front().name), OptimizerException, "");
vector<Statement> v;
// Replace "a := v" by "let a_1 := v a := v"
VariableDeclaration newVarDecl = replaceByNew(
v.emplace_back(replaceByNew(
assignment.location,
assignment.variableNames.front().name,
{}, // TODO determine type
assignment.value
);
return vector<Statement>{std::move(newVarDecl), std::move(assignment)};
));
v.emplace_back(move(assignment));
return v;
}
else
visit(_s);
+18 -8
View File
@@ -51,7 +51,11 @@ void StructuralSimplifier::simplify(std::vector<yul::Statement>& _statements)
GenericFallbackReturnsVisitor<OptionalStatements, If, Switch, ForLoop> const visitor(
[&](If& _ifStmt) -> OptionalStatements {
if (_ifStmt.body.statements.empty())
return {{makePopExpressionStatement(_ifStmt.location, std::move(*_ifStmt.condition))}};
{
OptionalStatements s = vector<Statement>{};
s->emplace_back(makePopExpressionStatement(_ifStmt.location, std::move(*_ifStmt.condition)));
return s;
}
if (expressionAlwaysTrue(*_ifStmt.condition))
return {std::move(_ifStmt.body.statements)};
else if (expressionAlwaysFalse(*_ifStmt.condition))
@@ -64,18 +68,24 @@ void StructuralSimplifier::simplify(std::vector<yul::Statement>& _statements)
auto& switchCase = _switchStmt.cases.front();
auto loc = locationOf(*_switchStmt.expression);
if (switchCase.value)
return {{If{
{
OptionalStatements s = vector<Statement>{};
s->emplace_back(If{
std::move(_switchStmt.location),
make_shared<Expression>(FunctionalInstruction{
make_unique<Expression>(FunctionalInstruction{
std::move(loc),
solidity::Instruction::EQ,
{std::move(*switchCase.value), std::move(*_switchStmt.expression)}
}), std::move(switchCase.body)}}};
}), std::move(switchCase.body)});
return s;
}
else
return {{
makePopExpressionStatement(loc, std::move(*_switchStmt.expression)),
std::move(switchCase.body)
}};
{
OptionalStatements s = vector<Statement>{};
s->emplace_back(makePopExpressionStatement(loc, std::move(*_switchStmt.expression)));
s->emplace_back(std::move(switchCase.body));
return s;
}
}
else
return {};
+5 -5
View File
@@ -100,7 +100,7 @@ bool SyntacticallyEqual::statementEqual(Assignment const& _lhs, Assignment const
bool SyntacticallyEqual::statementEqual(VariableDeclaration const& _lhs, VariableDeclaration const& _rhs)
{
// first visit expression, then variable declarations
if (!compareSharedPtr<Expression, &SyntacticallyEqual::operator()>(_lhs.value, _rhs.value))
if (!compareUniquePtr<Expression, &SyntacticallyEqual::operator()>(_lhs.value, _rhs.value))
return false;
return containerEqual(_lhs.variables, _rhs.variables, [this](TypedName const& _lhsVarName, TypedName const& _rhsVarName) -> bool {
return this->visitDeclaration(_lhsVarName, _rhsVarName);
@@ -123,7 +123,7 @@ bool SyntacticallyEqual::statementEqual(FunctionDefinition const& _lhs, Function
bool SyntacticallyEqual::statementEqual(If const& _lhs, If const& _rhs)
{
return
compareSharedPtr<Expression, &SyntacticallyEqual::operator()>(_lhs.condition, _rhs.condition) &&
compareUniquePtr<Expression, &SyntacticallyEqual::operator()>(_lhs.condition, _rhs.condition) &&
statementEqual(_lhs.body, _rhs.body);
}
@@ -139,7 +139,7 @@ bool SyntacticallyEqual::statementEqual(Switch const& _lhs, Switch const& _rhs)
for (auto const& rhsCase: _rhs.cases)
rhsCases.insert(&rhsCase);
return
compareSharedPtr<Expression, &SyntacticallyEqual::operator()>(_lhs.expression, _rhs.expression) &&
compareUniquePtr<Expression, &SyntacticallyEqual::operator()>(_lhs.expression, _rhs.expression) &&
containerEqual(lhsCases, rhsCases, [this](Case const* _lhsCase, Case const* _rhsCase) -> bool {
return this->switchCaseEqual(*_lhsCase, *_rhsCase);
});
@@ -149,7 +149,7 @@ bool SyntacticallyEqual::statementEqual(Switch const& _lhs, Switch const& _rhs)
bool SyntacticallyEqual::switchCaseEqual(Case const& _lhs, Case const& _rhs)
{
return
compareSharedPtr<Literal, &SyntacticallyEqual::expressionEqual>(_lhs.value, _rhs.value) &&
compareUniquePtr<Literal, &SyntacticallyEqual::expressionEqual>(_lhs.value, _rhs.value) &&
statementEqual(_lhs.body, _rhs.body);
}
@@ -157,7 +157,7 @@ bool SyntacticallyEqual::statementEqual(ForLoop const& _lhs, ForLoop const& _rhs
{
return
statementEqual(_lhs.pre, _rhs.pre) &&
compareSharedPtr<Expression, &SyntacticallyEqual::operator()>(_lhs.condition, _rhs.condition) &&
compareUniquePtr<Expression, &SyntacticallyEqual::operator()>(_lhs.condition, _rhs.condition) &&
statementEqual(_lhs.body, _rhs.body) &&
statementEqual(_lhs.post, _rhs.post);
}
+1 -1
View File
@@ -76,7 +76,7 @@ private:
}
template<typename T, bool (SyntacticallyEqual::*CompareMember)(T const&, T const&)>
bool compareSharedPtr(std::shared_ptr<T> const& _lhs, std::shared_ptr<T> const& _rhs)
bool compareUniquePtr(std::unique_ptr<T> const& _lhs, std::unique_ptr<T> const& _rhs)
{
return (_lhs == _rhs) || (_lhs && _rhs && (this->*CompareMember)(*_lhs, *_rhs));
}
+2 -2
View File
@@ -39,7 +39,7 @@ void VarDeclInitializer::operator()(Block& _block)
return {};
else if (_varDecl.variables.size() == 1)
{
_varDecl.value = make_shared<Expression>(zero);
_varDecl.value = make_unique<Expression>(zero);
return {};
}
else
@@ -47,7 +47,7 @@ void VarDeclInitializer::operator()(Block& _block)
OptionalStatements ret{vector<Statement>{}};
langutil::SourceLocation loc{std::move(_varDecl.location)};
for (auto& var: _varDecl.variables)
ret->push_back(VariableDeclaration{loc, {std::move(var)}, make_shared<Expression>(zero)});
ret->push_back(VariableDeclaration{loc, {std::move(var)}, make_unique<Expression>(zero)});
return ret;
}
}