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 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
@@ -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>): 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) {