mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Special case code generation for for loops.
This commit is contained in:
parent
3677c7a3ba
commit
e6015626cf
@ -4,6 +4,7 @@ Language Features:
|
||||
|
||||
|
||||
Compiler Features:
|
||||
* Code Generator: Remove redundant overflow checks in specific for loops.
|
||||
|
||||
|
||||
Bugfixes:
|
||||
|
@ -130,6 +130,17 @@ void PostTypeChecker::endVisit(ModifierInvocation const& _modifierInvocation)
|
||||
callEndVisit(_modifierInvocation);
|
||||
}
|
||||
|
||||
|
||||
bool PostTypeChecker::visit(ForStatement const& _forStatement)
|
||||
{
|
||||
return callVisit(_forStatement);
|
||||
}
|
||||
|
||||
void PostTypeChecker::endVisit(ForStatement const& _forStatement)
|
||||
{
|
||||
callEndVisit(_forStatement);
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
struct ConstStateVarCircularReferenceChecker: public PostTypeChecker::Checker
|
||||
@ -422,6 +433,59 @@ struct ReservedErrorSelector: public PostTypeChecker::Checker
|
||||
}
|
||||
};
|
||||
|
||||
class LValueChecker: public ASTConstVisitor
|
||||
{
|
||||
public:
|
||||
LValueChecker(Identifier const& _identifier): m_declaration(_identifier.annotation().referencedDeclaration) {}
|
||||
bool willBeWrittenTo() const { return m_willBeWrittenTo; }
|
||||
void endVisit(Identifier const& _identifier) override
|
||||
{
|
||||
if (
|
||||
_identifier.annotation().referencedDeclaration == m_declaration &&
|
||||
_identifier.annotation().willBeWrittenTo
|
||||
)
|
||||
m_willBeWrittenTo = true;
|
||||
}
|
||||
private:
|
||||
Declaration const* m_declaration;
|
||||
bool m_willBeWrittenTo = false;
|
||||
};
|
||||
|
||||
struct SimpleCounterForLoopChecker: public PostTypeChecker::Checker
|
||||
{
|
||||
SimpleCounterForLoopChecker(ErrorReporter& _errorReporter): Checker(_errorReporter) {}
|
||||
bool visit(ForStatement const& _forStatement) override
|
||||
{
|
||||
_forStatement.annotation().isSimpleCounterLoop = isSimpleCounterLoop(_forStatement);
|
||||
return true;
|
||||
}
|
||||
bool isSimpleCounterLoop(ForStatement const& _forStatement) const
|
||||
{
|
||||
auto const* cond = dynamic_cast<BinaryOperation const*>(_forStatement.condition());
|
||||
if (!cond || cond->getOperator() != Token::LessThan)
|
||||
return false;
|
||||
if (!_forStatement.loopExpression())
|
||||
return false;
|
||||
|
||||
auto const* post = dynamic_cast<UnaryOperation const*>(&_forStatement.loopExpression()->expression());
|
||||
if (!post || post->getOperator() != Token::Inc)
|
||||
return false;
|
||||
|
||||
auto const* lhsIdentifier = dynamic_cast<Identifier const*>(&cond->leftExpression());
|
||||
auto const* lhsType = dynamic_cast<IntegerType const*>(cond->leftExpression().annotation().type);
|
||||
auto const *commonType = dynamic_cast<IntegerType const*>(cond->annotation().commonType);
|
||||
|
||||
if (lhsIdentifier && lhsType && commonType && *lhsType == *commonType)
|
||||
{
|
||||
LValueChecker lValueChecker{*lhsIdentifier};
|
||||
_forStatement.body().accept(lValueChecker);
|
||||
if (!lValueChecker.willBeWrittenTo())
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -433,4 +497,5 @@ PostTypeChecker::PostTypeChecker(langutil::ErrorReporter& _errorReporter): m_err
|
||||
m_checkers.push_back(make_shared<EventOutsideEmitErrorOutsideRevertChecker>(_errorReporter));
|
||||
m_checkers.push_back(make_shared<NoVariablesInInterfaceChecker>(_errorReporter));
|
||||
m_checkers.push_back(make_shared<ReservedErrorSelector>(_errorReporter));
|
||||
m_checkers.push_back(make_shared<SimpleCounterForLoopChecker>(_errorReporter));
|
||||
}
|
||||
|
@ -97,6 +97,9 @@ private:
|
||||
bool visit(ModifierInvocation const& _modifierInvocation) override;
|
||||
void endVisit(ModifierInvocation const& _modifierInvocation) override;
|
||||
|
||||
bool visit(ForStatement const& _forStatement) override;
|
||||
void endVisit(ForStatement const& _forStatement) override;
|
||||
|
||||
template <class T>
|
||||
bool callVisit(T const& _node)
|
||||
{
|
||||
|
@ -235,6 +235,7 @@ struct TryCatchClauseAnnotation: ASTAnnotation, ScopableAnnotation
|
||||
|
||||
struct ForStatementAnnotation: StatementAnnotation, ScopableAnnotation
|
||||
{
|
||||
util::SetOnce<bool> isSimpleCounterLoop;
|
||||
};
|
||||
|
||||
struct ReturnAnnotation: StatementAnnotation
|
||||
|
@ -710,12 +710,18 @@ bool ASTJsonExporter::visit(WhileStatement const& _node)
|
||||
|
||||
bool ASTJsonExporter::visit(ForStatement const& _node)
|
||||
{
|
||||
setJsonNode(_node, "ForStatement", {
|
||||
|
||||
std::vector<pair<string, Json::Value>> attributes = {
|
||||
make_pair("initializationExpression", toJsonOrNull(_node.initializationExpression())),
|
||||
make_pair("condition", toJsonOrNull(_node.condition())),
|
||||
make_pair("loopExpression", toJsonOrNull(_node.loopExpression())),
|
||||
make_pair("body", toJson(_node.body()))
|
||||
});
|
||||
};
|
||||
|
||||
if (_node.annotation().isSimpleCounterLoop.set())
|
||||
attributes.emplace_back(make_pair("isSimpleCounterLoop", *_node.annotation().isSimpleCounterLoop));
|
||||
|
||||
setJsonNode(_node, "ForStatement", std::move(attributes));
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -1246,7 +1246,13 @@ bool ContractCompiler::visit(ForStatement const& _forStatement)
|
||||
|
||||
// for's loop expression if existing
|
||||
if (_forStatement.loopExpression())
|
||||
{
|
||||
Arithmetic previousArithmetic = m_context.arithmetic();
|
||||
if (*_forStatement.annotation().isSimpleCounterLoop)
|
||||
m_context.setArithmetic(Arithmetic::Wrapping);
|
||||
_forStatement.loopExpression()->accept(*this);
|
||||
m_context.setArithmetic(previousArithmetic);
|
||||
}
|
||||
|
||||
m_context.appendJumpTo(loopStart);
|
||||
|
||||
|
@ -618,7 +618,9 @@ bool IRGeneratorForStatements::visit(ForStatement const& _forStatement)
|
||||
_forStatement.body(),
|
||||
_forStatement.condition(),
|
||||
_forStatement.initializationExpression(),
|
||||
_forStatement.loopExpression()
|
||||
_forStatement.loopExpression(),
|
||||
false,
|
||||
*_forStatement.annotation().isSimpleCounterLoop
|
||||
);
|
||||
|
||||
return false;
|
||||
@ -3139,7 +3141,8 @@ void IRGeneratorForStatements::generateLoop(
|
||||
Expression const* _conditionExpression,
|
||||
Statement const* _initExpression,
|
||||
ExpressionStatement const* _loopExpression,
|
||||
bool _isDoWhile
|
||||
bool _isDoWhile,
|
||||
bool _isSimpleCounterLoop
|
||||
)
|
||||
{
|
||||
string firstRun;
|
||||
@ -3156,7 +3159,13 @@ void IRGeneratorForStatements::generateLoop(
|
||||
_initExpression->accept(*this);
|
||||
appendCode() << "} 1 {\n";
|
||||
if (_loopExpression)
|
||||
{
|
||||
Arithmetic previousArithmetic = m_context.arithmetic();
|
||||
if (_isSimpleCounterLoop)
|
||||
m_context.setArithmetic(Arithmetic::Wrapping);
|
||||
_loopExpression->accept(*this);
|
||||
m_context.setArithmetic(previousArithmetic);
|
||||
}
|
||||
appendCode() << "}\n";
|
||||
appendCode() << "{\n";
|
||||
|
||||
|
@ -235,7 +235,8 @@ private:
|
||||
Expression const* _conditionExpression,
|
||||
Statement const* _initExpression = nullptr,
|
||||
ExpressionStatement const* _loopExpression = nullptr,
|
||||
bool _isDoWhile = false
|
||||
bool _isDoWhile = false,
|
||||
bool _isSimpleCounterLoop = false
|
||||
);
|
||||
|
||||
static Type const& type(Expression const& _expression);
|
||||
|
Loading…
Reference in New Issue
Block a user