Changed an order of processing loops in ForLoopsLowering

This commit is contained in:
Elena Lepilkina
2021-06-22 12:57:13 +03:00
committed by Space
parent 0e04c21625
commit 9503627864
11 changed files with 122 additions and 132 deletions
@@ -332,9 +332,9 @@ abstract class Symbols<out T : CommonBackendContext>(val context: T, irBuiltIns:
open val unsafeCoerceIntrinsic: IrSimpleFunctionSymbol? = null
open val getWithoutBCName: Name? = null
open val getWithoutBoundCheckName: Name? = null
open val setWithoutBCName: Name? = null
open val setWithoutBoundCheckName: Name? = null
companion object {
fun isLateinitIsInitializedPropertyGetter(symbol: IrFunctionSymbol): Boolean =
@@ -118,35 +118,14 @@ class ForLoopsLowering(val context: CommonBackendContext, val loopBodyTransforme
* Abstract class for additional for-loop bodies transformations.
*/
abstract class ForLoopBodyTransformer : IrElementTransformerVoid() {
protected lateinit var mainLoopVariable: IrVariable
protected lateinit var loopHeader: ForLoopHeader
protected lateinit var loopVariableComponents: Map<Int, IrVariable>
protected lateinit var context: CommonBackendContext
open fun initialize(
context: CommonBackendContext,
loopVariable: IrVariable,
forLoopHeader: ForLoopHeader,
loopComponents: Map<Int, IrVariable>
) {
this.context = context
mainLoopVariable = loopVariable
loopHeader = forLoopHeader
loopVariableComponents = loopComponents
}
fun transform(
abstract fun transform(
context: CommonBackendContext,
irExpression: IrExpression,
loopVariable: IrVariable,
forLoopHeader: ForLoopHeader,
loopComponents: Map<Int, IrVariable>
) {
initialize(context, loopVariable, forLoopHeader, loopComponents)
irExpression.transformChildrenVoid(this)
}
open fun shouldTransform(context: CommonBackendContext) = true
)
}
private class RangeLoopTransformer(
@@ -162,6 +141,7 @@ private class RangeLoopTransformer(
fun getScopeOwnerSymbol() = currentScope?.scope?.scopeOwnerSymbol ?: container.symbol
override fun visitBlock(expression: IrBlock): IrExpression {
val returnExpression = super.visitBlock(expression) as IrBlock
// LoopExpressionGenerator in psi2ir lowers `for (loopVar in <someIterable>) { // Loop body }` into an IrBlock with origin FOR_LOOP.
// This block has 2 statements:
//
@@ -178,21 +158,21 @@ private class RangeLoopTransformer(
// `withIndex()` call, a progression such as `10 downTo 1`). However in some cases (e.g., for `withIndex()`), we also need to
// examine the while loop to determine if we CAN optimize the loop.
if (expression.origin != IrStatementOrigin.FOR_LOOP) {
return super.visitBlock(expression) // Not a for-loop block.
return returnExpression // Not a for-loop block.
}
with(expression.statements) {
assert(size == 2) { "Expected 2 statements in for-loop block, was:\n${expression.dump()}" }
with(returnExpression.statements) {
assert(size == 2) { "Expected 2 statements in for-loop block, was:\n${returnExpression.dump()}" }
val iteratorVariable = get(0) as IrVariable
assert(iteratorVariable.origin == IrDeclarationOrigin.FOR_LOOP_ITERATOR) { "Expected FOR_LOOP_ITERATOR origin for iterator variable, was:\n${iteratorVariable.dump()}" }
val loopHeader = headerProcessor.extractHeader(iteratorVariable)
?: return super.visitBlock(expression) // The iterable in the header is not supported.
?: return returnExpression // The iterable in the header is not supported.
val loweredHeader = lowerHeader(iteratorVariable, loopHeader)
val oldLoop = get(1) as IrWhileLoop
assert(oldLoop.origin == IrStatementOrigin.FOR_LOOP_INNER_WHILE) { "Expected FOR_LOOP_INNER_WHILE origin for while loop, was:\n${oldLoop.dump()}" }
val (newLoop, loopReplacementExpression) = lowerWhileLoop(oldLoop, loopHeader)
?: return super.visitBlock(expression) // Cannot lower the loop.
?: return returnExpression // Cannot lower the loop.
// We can lower both the header and while loop.
// Update mapping from old to new loop so we can later update references in break/continue.
@@ -202,7 +182,7 @@ private class RangeLoopTransformer(
set(1, loopReplacementExpression)
}
return super.visitBlock(expression)
return returnExpression
}
/**
@@ -296,7 +276,7 @@ private class RangeLoopTransformer(
it
}
}
if (newBody != null && loopBodyTransformer != null && loopBodyTransformer.shouldTransform(context)) {
if (newBody != null && loopBodyTransformer != null) {
loopBodyTransformer.transform(context, newBody, mainLoopVariable, loopHeader, loopVariableComponents)
}
@@ -81,7 +81,7 @@ internal class ArrayIterationHandler(context: CommonBackendContext) : IndexedGet
get() = getClass()!!.getPropertyGetter("size")!!.owner
private val getFunctionName: Name
get() = context.ir.symbols.getWithoutBCName ?: OperatorNameConventions.GET
get() = context.ir.symbols.getWithoutBoundCheckName ?: OperatorNameConventions.GET
override val IrType.getFunction
get() = getClass()!!.functions.single {