[wasm] Optimize bounds checking bounds

For bounds checking, we have a special path which avoids one conditional
branch. This path can actually be one value wider, which will avoid some
unneeded code if that case it hit. This will in particular be the case
for single-byte loads at offset 0 with a minimum memory size of 0.

R=jkummerow@chromium.org

Bug: v8:10949
Change-Id: Id16af8debc38c56c520183aec81a48249979ec96
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2595290
Reviewed-by: Jakob Kummerow <jkummerow@chromium.org>
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#71822}
This commit is contained in:
Clemens Backes 2020-12-16 17:14:19 +01:00 committed by Commit Bot
parent 8f02ad408e
commit 63b78f2b01
2 changed files with 6 additions and 5 deletions

View File

@ -3804,13 +3804,13 @@ Node* WasmGraphBuilder::BoundsCheckMem(uint8_t access_size, Node* index,
// - checking that {index < effective_size}.
Node* mem_size = instance_cache_->mem_size;
if (end_offset >= env_->min_memory_size) {
if (end_offset > env_->min_memory_size) {
// The end offset is larger than the smallest memory.
// Dynamically check the end offset against the dynamic memory size.
Node* cond = gasm_->UintLessThan(end_offset_node, mem_size);
TrapIfFalse(wasm::kTrapMemOutOfBounds, cond, position);
} else {
// The end offset is smaller than the smallest memory, so only one check is
// The end offset is <= the smallest memory, so only one check is
// required. Check to see if the index is also a constant.
UintPtrMatcher match(index);
if (match.HasResolvedValue()) {
@ -3823,7 +3823,7 @@ Node* WasmGraphBuilder::BoundsCheckMem(uint8_t access_size, Node* index,
}
}
// This produces a positive number, since {end_offset < min_size <= mem_size}.
// This produces a positive number since {end_offset <= min_size <= mem_size}.
Node* effective_size = gasm_->IntSub(mem_size, end_offset_node);
// Introduce the actual bounds check.

View File

@ -2257,13 +2257,14 @@ class LiftoffCompiler {
__ LoadConstant(end_offset_reg, WasmValue::ForUintPtr(end_offset));
if (end_offset >= env_->min_memory_size) {
if (end_offset > env_->min_memory_size) {
__ emit_cond_jump(kUnsignedGreaterEqual, trap_label,
LiftoffAssembler::kWasmIntPtr, end_offset_reg.gp(),
mem_size);
}
// Just reuse the end_offset register for computing the effective size.
// Just reuse the end_offset register for computing the effective size
// (which is >= 0 because of the check above).
LiftoffRegister effective_size_reg = end_offset_reg;
__ emit_ptrsize_sub(effective_size_reg.gp(), mem_size, end_offset_reg.gp());