From 4dd94d7f2636a3bc36299af4aa4f404e1fb6b111 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Tue, 9 Apr 2019 10:37:15 +0200 Subject: [PATCH] Avoid unneeded frame entering/leaving when there's no slots to release. (#2854) --- .../kotlin/backend/konan/llvm/CodeGenerator.kt | 14 +++++++++----- .../kotlin/backend/konan/llvm/IrToBitcode.kt | 8 ++------ .../kotlin/backend/konan/llvm/VariableManager.kt | 8 +++++--- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt index 4e0505cd17c..31f88d96bd8 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt @@ -8,7 +8,6 @@ package org.jetbrains.kotlin.backend.konan.llvm import kotlinx.cinterop.* import llvm.* -import org.jetbrains.kotlin.backend.common.serialization.KotlinMangler import org.jetbrains.kotlin.backend.konan.* import org.jetbrains.kotlin.backend.konan.descriptors.isInterface import org.jetbrains.kotlin.backend.konan.llvm.objc.* @@ -875,7 +874,6 @@ internal class FunctionGenerationContext(val function: LLVMValueRef, private fun position() = basicBlockToLastLocation[currentBlock] - internal fun mapParameterForDebug(index: Int, value: LLVMValueRef) { appendingTo(localsInitBb) { LLVMBuildStore(builder, value, vars.addressOf(index)) @@ -897,7 +895,7 @@ internal class FunctionGenerationContext(val function: LLVMValueRef, internal fun epilogue() { appendingTo(prologueBb) { - val slots = if (needSlots) + val slots = if (needSlotsPhi) LLVMBuildArrayAlloca(builder, kObjHeaderPtr, Int32(slotCount).llvm, "")!! else kNullObjHeaderPtrPtr @@ -909,7 +907,7 @@ internal class FunctionGenerationContext(val function: LLVMValueRef, Int32(slotCount * codegen.runtime.pointerSize).llvm, Int32(codegen.runtime.pointerAlignment).llvm, Int1(0).llvm)) - call(context.llvm.enterFrameFunction, listOf(slots, Int32(vars.skip).llvm, Int32(slotCount).llvm)) + call(context.llvm.enterFrameFunction, listOf(slots, Int32(vars.skipSlots).llvm, Int32(slotCount).llvm)) } addPhiIncoming(slotsPhi!!, prologueBb to slots) memScoped { @@ -1096,14 +1094,20 @@ internal class FunctionGenerationContext(val function: LLVMValueRef, } private val needSlots: Boolean + get() { + return slotCount - vars.skipSlots > frameOverlaySlotCount + } + + private val needSlotsPhi: Boolean get() { return slotCount > frameOverlaySlotCount || localAllocs > 0 } + private fun releaseVars() { if (needSlots) { call(context.llvm.leaveFrameFunction, - listOf(slotsPhi!!, Int32(vars.skip).llvm, Int32(slotCount).llvm)) + listOf(slotsPhi!!, Int32(vars.skipSlots).llvm, Int32(slotCount).llvm)) } } } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt index d52ba8736cc..e89f81e0e57 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt @@ -562,11 +562,9 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map functionGenerationContext.call(function, args) } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/VariableManager.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/VariableManager.kt index ccd934d8c9c..1c431878856 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/VariableManager.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/VariableManager.kt @@ -48,6 +48,7 @@ internal class VariableManager(val functionGenerationContext: FunctionGeneration // Clears inner state of variable manager. fun clear() { + skipSlots = 0 variables.clear() contextVariablesToIndex.clear() } @@ -78,17 +79,18 @@ internal class VariableManager(val functionGenerationContext: FunctionGeneration return index } - internal var skip = 0 + internal var skipSlots = 0 internal fun createParameter(valueDeclaration: IrValueDeclaration, variableLocation: VariableDebugLocation?) : Int { assert(!contextVariablesToIndex.contains(valueDeclaration)) val index = variables.size val type = functionGenerationContext.getLLVMType(valueDeclaration.type) - val slot = functionGenerationContext.alloca(type, "p-${valueDeclaration.name.asString()}", variableLocation) + val slot = functionGenerationContext.alloca( + type, "p-${valueDeclaration.name.asString()}", variableLocation) val isObject = functionGenerationContext.isObjectType(type) variables.add(ParameterRecord(slot, isObject)) contextVariablesToIndex[valueDeclaration] = index if (isObject) - skip++ + skipSlots++ return index }