[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
@@ -35,7 +35,7 @@ class BodyGenerator(val context: WasmFunctionCodegenContext) : IrElementVisitorV
private val unitGetInstance by lazy { backendContext.findUnitGetInstanceFunction() }
fun WasmExpressionBuilder.buildGetUnit() {
buildCall(context.referenceFunction(unitGetInstance.symbol))
buildInstr(WasmOp.GET_UNIT, WasmImmediate.FuncIdx(context.referenceFunction(unitGetInstance.symbol)))
}
// Generates code for the given IR element and *always* leaves something on the stack
@@ -264,6 +264,12 @@ class BodyGenerator(val context: WasmFunctionCodegenContext) : IrElementVisitorV
return
}
// Get unit is a special case because it is the only function which returns the real unit instance.
if (call.symbol == unitGetInstance.symbol) {
body.buildGetUnit()
return
}
call.dispatchReceiver?.let { generateExpression(it) }
call.extensionReceiver?.let { generateExpression(it) }
for (i in 0 until call.valueArgumentsCount) {
@@ -319,7 +325,7 @@ class BodyGenerator(val context: WasmFunctionCodegenContext) : IrElementVisitorV
}
// Unit types don't cross function boundaries
if (function.returnType == irBuiltIns.unitType && function.symbol != unitGetInstance.symbol)
if (function.returnType == irBuiltIns.unitType)
body.buildGetUnit()
}