Skip to content

Commit 5f2329b

Browse files
committed
Simplify br_table dispatch (avoid conditional around index).
1 parent c449287 commit 5f2329b

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/nodes/WasmFunctionNode.java

+16-16
Original file line numberDiff line numberDiff line change
@@ -436,13 +436,14 @@ public Object executeBodyFromOffset(WasmContext context, WasmInstance instance,
436436
int index = popInt(frame, stackPointer);
437437
final int size = rawPeekU8(bytecode, offset);
438438
final int counterOffset = offset + 1;
439-
if (index < 0 || index >= size) {
440-
// If unsigned index is larger or equal to the table size use the
441-
// default (last) index.
442-
index = size - 1;
443-
}
444439

445440
if (CompilerDirectives.inInterpreter()) {
441+
if (index < 0 || index >= size) {
442+
// If unsigned index is larger or equal to the table size use the
443+
// default (last) index.
444+
index = size - 1;
445+
}
446+
446447
final int indexOffset = offset + 3 + index * 6;
447448
updateBranchTableProfile(bytecode, counterOffset, indexOffset + 4);
448449
final int offsetDelta = rawPeekI32(bytecode, indexOffset);
@@ -454,28 +455,28 @@ public Object executeBodyFromOffset(WasmContext context, WasmInstance instance,
454455
// time constants, since the loop is unrolled.
455456
for (int i = 0; i < size; i++) {
456457
final int indexOffset = offset + 3 + i * 6;
457-
if (profileBranchTable(bytecode, counterOffset, indexOffset + 4, i == index)) {
458+
if (profileBranchTable(bytecode, counterOffset, indexOffset + 4, i == index || i == size - 1)) {
458459
final int offsetDelta = rawPeekI32(bytecode, indexOffset);
459460
offset = indexOffset + offsetDelta;
460461
continue loop;
461462
}
462463
}
464+
throw CompilerDirectives.shouldNotReachHere("br_table");
463465
}
464-
enterErrorBranch();
465-
throw WasmException.create(Failure.UNSPECIFIED_INTERNAL, this, "Should not reach here");
466466
}
467467
case Bytecode.BR_TABLE_I32: {
468468
stackPointer--;
469469
int index = popInt(frame, stackPointer);
470470
final int size = rawPeekI32(bytecode, offset);
471471
final int counterOffset = offset + 4;
472-
if (index < 0 || index >= size) {
473-
// If unsigned index is larger or equal to the table size use the
474-
// default (last) index.
475-
index = size - 1;
476-
}
477472

478473
if (CompilerDirectives.inInterpreter()) {
474+
if (index < 0 || index >= size) {
475+
// If unsigned index is larger or equal to the table size use the
476+
// default (last) index.
477+
index = size - 1;
478+
}
479+
479480
final int indexOffset = offset + 6 + index * 6;
480481
updateBranchTableProfile(bytecode, counterOffset, indexOffset + 4);
481482
final int offsetDelta = rawPeekI32(bytecode, indexOffset);
@@ -487,15 +488,14 @@ public Object executeBodyFromOffset(WasmContext context, WasmInstance instance,
487488
// time constants, since the loop is unrolled.
488489
for (int i = 0; i < size; i++) {
489490
final int indexOffset = offset + 6 + i * 6;
490-
if (profileBranchTable(bytecode, counterOffset, indexOffset + 4, i == index)) {
491+
if (profileBranchTable(bytecode, counterOffset, indexOffset + 4, i == index || i == size - 1)) {
491492
final int offsetDelta = rawPeekI32(bytecode, indexOffset);
492493
offset = indexOffset + offsetDelta;
493494
continue loop;
494495
}
495496
}
497+
throw CompilerDirectives.shouldNotReachHere("br_table");
496498
}
497-
enterErrorBranch();
498-
throw WasmException.create(Failure.UNSPECIFIED_INTERNAL, this, "Should not reach here");
499499
}
500500
case Bytecode.CALL_U8:
501501
case Bytecode.CALL_I32: {

0 commit comments

Comments
 (0)