From 4f1004c8f586598671edd5543209f21e48cba880 Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Thu, 29 Dec 2016 12:23:17 +0700 Subject: [PATCH] backend: reorder basic blocks to improve bitcode readability. * Insert created basic block after the current one in `codegen.basicBlock()`. * Slightly reorder calls to `codegen.basicBlock()`. --- .../kotlin/backend/konan/llvm/CodeGenerator.kt | 10 +++++++--- .../kotlin/backend/konan/llvm/IrToBitcode.kt | 15 +++++++-------- 2 files changed, 14 insertions(+), 11 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 4de0469baec..380d1a8504e 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 @@ -241,8 +241,12 @@ internal class CodeGenerator(override val context: Context) : ContextUtils { fun indexInClass(p:PropertyDescriptor):Int = (p.containingDeclaration as ClassDescriptor).fields.indexOf(p) - fun basicBlock(function: LLVMValueRef, name: String = "label_"): LLVMBasicBlockRef = - LLVMAppendBasicBlock(function, name)!! + fun basicBlock(name: String = "label_"): LLVMBasicBlockRef { + val currentBlock = this.currentBlock + val result = LLVMInsertBasicBlock(currentBlock, name)!! + LLVMMoveBasicBlockAfter(result, currentBlock) + return result + } fun lastBasicBlock(): LLVMBasicBlockRef? = LLVMGetLastBasicBlock(function) @@ -296,7 +300,7 @@ internal class CodeGenerator(override val context: Context) : ContextUtils { fun getBuilder(): LLVMBuilderRef { if (isAfterTerminator) { - positionAtEnd(basicBlock(function!!, "unreachable")) + positionAtEnd(basicBlock("unreachable")) } return builder 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 5d449166539..f325c316333 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 @@ -313,8 +313,8 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid //-------------------------------------------------------------------------// private inner class LoopScope(val loop: IrLoop) : InnerScopeImpl() { - val loopCheck = codegen.basicBlock() val loopExit = codegen.basicBlock() + val loopCheck = codegen.basicBlock() override fun genBreak(destination: IrBreak) { if (destination.label == null || destination.label == loop.label) @@ -950,7 +950,9 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid // The call inside [CatchingScope] must be configured to dispatch exception to the scope's handler. override fun genCall(function: LLVMValueRef, args: List): LLVMValueRef { + val landingpad = this.landingpad val then = codegen.basicBlock() + val res = codegen.invoke(function, args, then, landingpad) codegen.positionAtEnd(then) return res @@ -976,8 +978,8 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid // TODO: optimize for `Throwable` clause. catches.forEach { val isInstance = genInstanceOf(exception, it.parameter.type) - val body = codegen.basicBlock("catch") val nextCheck = codegen.basicBlock("catchCheck") + val body = codegen.basicBlock("catch") codegen.condBr(isInstance, body, nextCheck) codegen.appendingTo(body) { @@ -1295,9 +1297,9 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid val type = value.typeOperand val srcArg = evaluateExpression(value.argument) // Evaluate src expression. - val bbNull = codegen.basicBlock() - val bbInstanceOf = codegen.basicBlock() val bbExit = codegen.basicBlock() + val bbInstanceOf = codegen.basicBlock() + val bbNull = codegen.basicBlock() val condition = codegen.icmpEq(srcArg, codegen.kNullObjHeaderPtr) codegen.condBr(condition, bbNull, bbInstanceOf) @@ -1931,10 +1933,7 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid * and working with local variables with [FunctionScope] and other enhancements. */ - fun CodeGenerator.basicBlock(name: String = "label_"): LLVMBasicBlockRef { - val functionScope:FunctionScope = currentCodeContext.functionScope() as FunctionScope - return basicBlock(functionScope.llvmFunction!!) - } + // TODO: is described refactoring still comming? fun CodeGenerator.basicBlock(name: String, code: () -> Unit) = basicBlock(name).apply { appendingTo(this) {