aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity/codegen/CompilerUtils.cpp
diff options
context:
space:
mode:
authorAlex Beregszaszi <alex@rtfs.hu>2018-04-03 22:58:11 +0800
committerGitHub <noreply@github.com>2018-04-03 22:58:11 +0800
commit0edce4b570c157927933697b30f0f94cbdf173b2 (patch)
treea7ec50f920090e529b435e878e4df8cedd1f0eb4 /libsolidity/codegen/CompilerUtils.cpp
parent7753249f646f239819c62ab6847438dc84b6e04b (diff)
parentdeadff263fbf4d5da911d7c544821cc77081a6d3 (diff)
downloaddexon-solidity-0edce4b570c157927933697b30f0f94cbdf173b2.tar
dexon-solidity-0edce4b570c157927933697b30f0f94cbdf173b2.tar.gz
dexon-solidity-0edce4b570c157927933697b30f0f94cbdf173b2.tar.bz2
dexon-solidity-0edce4b570c157927933697b30f0f94cbdf173b2.tar.lz
dexon-solidity-0edce4b570c157927933697b30f0f94cbdf173b2.tar.xz
dexon-solidity-0edce4b570c157927933697b30f0f94cbdf173b2.tar.zst
dexon-solidity-0edce4b570c157927933697b30f0f94cbdf173b2.zip
Merge pull request #3693 from ethereum/optimizeMLOAD
Optimize across MLOAD if MSIZE is not used.
Diffstat (limited to 'libsolidity/codegen/CompilerUtils.cpp')
-rw-r--r--libsolidity/codegen/CompilerUtils.cpp36
1 files changed, 28 insertions, 8 deletions
diff --git a/libsolidity/codegen/CompilerUtils.cpp b/libsolidity/codegen/CompilerUtils.cpp
index 676d5d4e..deaef017 100644
--- a/libsolidity/codegen/CompilerUtils.cpp
+++ b/libsolidity/codegen/CompilerUtils.cpp
@@ -495,14 +495,34 @@ void CompilerUtils::abiDecodeV2(TypePointers const& _parameterTypes, bool _fromM
void CompilerUtils::zeroInitialiseMemoryArray(ArrayType const& _type)
{
- auto repeat = m_context.newTag();
- m_context << repeat;
- pushZeroValue(*_type.baseType());
- storeInMemoryDynamic(*_type.baseType());
- m_context << Instruction::SWAP1 << u256(1) << Instruction::SWAP1;
- m_context << Instruction::SUB << Instruction::SWAP1;
- m_context << Instruction::DUP2;
- m_context.appendConditionalJumpTo(repeat);
+ if (_type.baseType()->hasSimpleZeroValueInMemory())
+ {
+ solAssert(_type.baseType()->isValueType(), "");
+ Whiskers templ(R"({
+ let size := mul(length, <element_size>)
+ // cheap way of zero-initializing a memory range
+ codecopy(memptr, codesize(), size)
+ memptr := add(memptr, size)
+ })");
+ templ("element_size", to_string(_type.baseType()->memoryHeadSize()));
+ m_context.appendInlineAssembly(templ.render(), {"length", "memptr"});
+ }
+ else
+ {
+ // TODO: Potential optimization:
+ // When we create a new multi-dimensional dynamic array, each element
+ // is initialized to an empty array. It actually does not hurt
+ // to re-use exactly the same empty array for all elements. Currently,
+ // a new one is created each time.
+ auto repeat = m_context.newTag();
+ m_context << repeat;
+ pushZeroValue(*_type.baseType());
+ storeInMemoryDynamic(*_type.baseType());
+ m_context << Instruction::SWAP1 << u256(1) << Instruction::SWAP1;
+ m_context << Instruction::SUB << Instruction::SWAP1;
+ m_context << Instruction::DUP2;
+ m_context.appendConditionalJumpTo(repeat);
+ }
m_context << Instruction::SWAP1 << Instruction::POP;
}