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()`.
This commit is contained in:
Svyatoslav Scherbina
2016-12-29 12:23:17 +07:00
committed by SvyatoslavScherbina
parent 7ba096b169
commit 4f1004c8f5
2 changed files with 14 additions and 11 deletions
@@ -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 indexInClass(p:PropertyDescriptor):Int = (p.containingDeclaration as ClassDescriptor).fields.indexOf(p)
fun basicBlock(function: LLVMValueRef, name: String = "label_"): LLVMBasicBlockRef = fun basicBlock(name: String = "label_"): LLVMBasicBlockRef {
LLVMAppendBasicBlock(function, name)!! val currentBlock = this.currentBlock
val result = LLVMInsertBasicBlock(currentBlock, name)!!
LLVMMoveBasicBlockAfter(result, currentBlock)
return result
}
fun lastBasicBlock(): LLVMBasicBlockRef? = LLVMGetLastBasicBlock(function) fun lastBasicBlock(): LLVMBasicBlockRef? = LLVMGetLastBasicBlock(function)
@@ -296,7 +300,7 @@ internal class CodeGenerator(override val context: Context) : ContextUtils {
fun getBuilder(): LLVMBuilderRef { fun getBuilder(): LLVMBuilderRef {
if (isAfterTerminator) { if (isAfterTerminator) {
positionAtEnd(basicBlock(function!!, "unreachable")) positionAtEnd(basicBlock("unreachable"))
} }
return builder return builder
@@ -313,8 +313,8 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
//-------------------------------------------------------------------------// //-------------------------------------------------------------------------//
private inner class LoopScope(val loop: IrLoop) : InnerScopeImpl() { private inner class LoopScope(val loop: IrLoop) : InnerScopeImpl() {
val loopCheck = codegen.basicBlock()
val loopExit = codegen.basicBlock() val loopExit = codegen.basicBlock()
val loopCheck = codegen.basicBlock()
override fun genBreak(destination: IrBreak) { override fun genBreak(destination: IrBreak) {
if (destination.label == null || destination.label == loop.label) 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. // The call inside [CatchingScope] must be configured to dispatch exception to the scope's handler.
override fun genCall(function: LLVMValueRef, args: List<LLVMValueRef>): LLVMValueRef { override fun genCall(function: LLVMValueRef, args: List<LLVMValueRef>): LLVMValueRef {
val landingpad = this.landingpad
val then = codegen.basicBlock() val then = codegen.basicBlock()
val res = codegen.invoke(function, args, then, landingpad) val res = codegen.invoke(function, args, then, landingpad)
codegen.positionAtEnd(then) codegen.positionAtEnd(then)
return res return res
@@ -976,8 +978,8 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
// TODO: optimize for `Throwable` clause. // TODO: optimize for `Throwable` clause.
catches.forEach { catches.forEach {
val isInstance = genInstanceOf(exception, it.parameter.type) val isInstance = genInstanceOf(exception, it.parameter.type)
val body = codegen.basicBlock("catch")
val nextCheck = codegen.basicBlock("catchCheck") val nextCheck = codegen.basicBlock("catchCheck")
val body = codegen.basicBlock("catch")
codegen.condBr(isInstance, body, nextCheck) codegen.condBr(isInstance, body, nextCheck)
codegen.appendingTo(body) { codegen.appendingTo(body) {
@@ -1295,9 +1297,9 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
val type = value.typeOperand val type = value.typeOperand
val srcArg = evaluateExpression(value.argument) // Evaluate src expression. val srcArg = evaluateExpression(value.argument) // Evaluate src expression.
val bbNull = codegen.basicBlock()
val bbInstanceOf = codegen.basicBlock()
val bbExit = codegen.basicBlock() val bbExit = codegen.basicBlock()
val bbInstanceOf = codegen.basicBlock()
val bbNull = codegen.basicBlock()
val condition = codegen.icmpEq(srcArg, codegen.kNullObjHeaderPtr) val condition = codegen.icmpEq(srcArg, codegen.kNullObjHeaderPtr)
codegen.condBr(condition, bbNull, bbInstanceOf) 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. * and working with local variables with [FunctionScope] and other enhancements.
*/ */
fun CodeGenerator.basicBlock(name: String = "label_"): LLVMBasicBlockRef { // TODO: is described refactoring still comming?
val functionScope:FunctionScope = currentCodeContext.functionScope() as FunctionScope
return basicBlock(functionScope.llvmFunction!!)
}
fun CodeGenerator.basicBlock(name: String, code: () -> Unit) = basicBlock(name).apply { fun CodeGenerator.basicBlock(name: String, code: () -> Unit) = basicBlock(name).apply {
appendingTo(this) { appendingTo(this) {