[WASM] Fold redundant patterns with drop instruction on a wasm level

This commit is contained in:
Igor Laevsky
2021-09-08 20:30:25 +03:00
committed by teamcityserver
parent 1db45faba2
commit 9685695769
3 changed files with 21 additions and 2 deletions
@@ -367,6 +367,9 @@ enum class WasmOp(
REF_CAST("ref.cast", 0xFB_41),
BR_ON_CAST("br_on_cast", 0xFB_42, listOf(LABEL_IDX)),
// Pseudo-instruction, just alias for a normal call. It's used to easily spot get_unit on the wasm level.
GET_UNIT("call", 0x10, FUNC_IDX)
;
constructor(mnemonic: String, opcode: Int, vararg immediates: WasmImmediateKind) : this(mnemonic, opcode, immediates.toList())
@@ -42,6 +42,16 @@ fun foldWasmInstructions(prev: WasmInstr?, next: WasmInstr): List<WasmInstr>? {
if (next.operator == WasmOp.UNREACHABLE && prev.operator in listOf(WasmOp.UNREACHABLE, WasmOp.RETURN))
return listOf(prev)
if (next.operator == WasmOp.DROP) {
// simple pure instruction + drop -> nothing
if (prev.operator == WasmOp.GET_UNIT || prev.operator == WasmOp.REF_NULL)
return listOf()
// return + drop -> return
if (prev.operator == WasmOp.RETURN)
return listOf(prev)
}
return null
}