compiler(amd64): eliminate a bounds check in memory copy (#1509)

Signed-off-by: Nuno Cruces <ncruces@users.noreply.github.com>
This commit is contained in:
Nuno Cruces
2023-06-09 01:58:53 +01:00
committed by GitHub
parent 83ac0f6b87
commit 8c7f97d39d

View File

@@ -3845,15 +3845,14 @@ func (c *amd64Compiler) compileMemoryCopy() error {
c.assembler.CompileRegisterToRegister(amd64.ADDQ, copySize.register, sourceOffset.register)
// destinationOffset += size.
c.assembler.CompileRegisterToRegister(amd64.ADDQ, copySize.register, destinationOffset.register)
// tmp = max(sourceOffset, destinationOffset).
c.assembler.CompileRegisterToRegister(amd64.CMPQ, sourceOffset.register, destinationOffset.register)
c.assembler.CompileRegisterToRegister(amd64.MOVQ, sourceOffset.register, tmp)
c.assembler.CompileRegisterToRegister(amd64.CMOVQCS, destinationOffset.register, tmp)
// Check source bounds and if exceeds the length, exit with out of bounds error.
// Check maximum bounds and if exceeds the length, exit with out of bounds error.
c.assembler.CompileMemoryToRegister(amd64.CMPQ,
amd64ReservedRegisterForCallEngine, callEngineModuleContextMemorySliceLenOffset, sourceOffset.register)
c.compileTrapFromNativeCode(amd64.JCC, nativeCallStatusCodeMemoryOutOfBounds)
// Check destination bounds and if exceeds the length, exit with out of bounds error.
c.assembler.CompileMemoryToRegister(amd64.CMPQ,
amd64ReservedRegisterForCallEngine, callEngineModuleContextMemorySliceLenOffset, destinationOffset.register)
amd64ReservedRegisterForCallEngine, callEngineModuleContextMemorySliceLenOffset, tmp)
c.compileTrapFromNativeCode(amd64.JCC, nativeCallStatusCodeMemoryOutOfBounds)
// Skip zero size.