JVM_IR KT-48640 generate for-in-downTo as a counter loop

This commit is contained in:
Dmitry Petrov
2021-09-21 17:55:13 +03:00
committed by TeamCityServer
parent 42e8017cc7
commit 2cc6b589f3
16 changed files with 262 additions and 32 deletions
@@ -12,14 +12,19 @@ import org.jetbrains.kotlin.backend.common.lower.matchers.SimpleCalleeMatcher
import org.jetbrains.kotlin.backend.common.lower.matchers.singleArgumentExtension
import org.jetbrains.kotlin.ir.builders.irInt
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrConst
import org.jetbrains.kotlin.ir.expressions.IrConstKind
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.name.FqName
/** Builds a [HeaderInfo] for progressions built using the `downTo` extension function. */
internal class DownToHandler(private val context: CommonBackendContext) :
ProgressionHandler {
private val preferJavaLikeCounterLoop = context.preferJavaLikeCounterLoop
private val progressionElementTypes = context.ir.symbols.progressionElementTypes
override val matcher = SimpleCalleeMatcher {
@@ -28,14 +33,78 @@ internal class DownToHandler(private val context: CommonBackendContext) :
parameter(0) { it.type in progressionElementTypes }
}
override fun build(expression: IrCall, data: ProgressionType, scopeOwner: IrSymbol): HeaderInfo? =
override fun build(expression: IrCall, data: ProgressionType, scopeOwner: IrSymbol) =
with(context.createIrBuilder(scopeOwner, expression.startOffset, expression.endOffset)) {
ProgressionHeaderInfo(
data,
first = expression.extensionReceiver!!,
last = expression.getValueArgument(0)!!,
step = irInt(-1),
direction = ProgressionDirection.DECREASING
)
val first = expression.extensionReceiver!!
val last = expression.getValueArgument(0)!!
val step = irInt(-1)
val direction = ProgressionDirection.DECREASING
if (preferJavaLikeCounterLoop) {
// Convert range with inclusive lower bound to exclusive lower bound if possible.
// This affects loop code performance on JVM.
val lastExclusive = last.convertToExclusiveLowerBound(data)
if (lastExclusive != null) {
return@with ProgressionHeaderInfo(
data,
first = first,
last = lastExclusive,
step = step,
direction = direction,
isLastInclusive = false,
canOverflow = false,
originalLastInclusive = last
)
}
}
ProgressionHeaderInfo(data, first = first, last = last, step = step, direction = direction)
}
private fun IrExpression.convertToExclusiveLowerBound(progressionType: ProgressionType): IrExpression? {
if (progressionType is UnsignedProgressionType) {
if (this.constLongValue == 0L) return null
}
val irConst = this as? IrConst<*> ?: return null
return when (irConst.kind) {
IrConstKind.Char -> {
val charValue = IrConstKind.Char.valueOf(irConst)
if (charValue != Char.MIN_VALUE)
IrConstImpl.char(startOffset, endOffset, type, charValue.dec())
else
null
}
IrConstKind.Byte -> {
val byteValue = IrConstKind.Byte.valueOf(irConst)
if (byteValue != Byte.MIN_VALUE)
IrConstImpl.byte(startOffset, endOffset, type, byteValue.dec())
else
null
}
IrConstKind.Short -> {
val shortValue = IrConstKind.Short.valueOf(irConst)
if (shortValue != Short.MIN_VALUE)
IrConstImpl.short(startOffset, endOffset, type, shortValue.dec())
else
null
}
IrConstKind.Int -> {
val intValue = IrConstKind.Int.valueOf(irConst)
if (intValue != Int.MIN_VALUE)
IrConstImpl.int(startOffset, endOffset, type, intValue.dec())
else
null
}
IrConstKind.Long -> {
val longValue = IrConstKind.Long.valueOf(irConst)
if (longValue != Long.MIN_VALUE)
IrConstImpl.long(startOffset, endOffset, type, longValue.dec())
else
null
}
else ->
null
}
}
}
@@ -35,45 +35,37 @@ internal class RangeToHandler(private val context: CommonBackendContext) :
override fun build(expression: IrCall, data: ProgressionType, scopeOwner: IrSymbol) =
with(context.createIrBuilder(scopeOwner, expression.startOffset, expression.endOffset)) {
val first = expression.dispatchReceiver!!
val last = expression.getValueArgument(0)!!
val step = irInt(1)
val direction = ProgressionDirection.INCREASING
if (preferJavaLikeCounterLoop && canUseExclusiveUpperBound(last, data)) {
if (preferJavaLikeCounterLoop) {
// Convert range with inclusive upper bound to exclusive upper bound if possible.
// This affects loop code performance on JVM.
val lastExclusive = last.convertToExclusiveUpperBound()
val lastExclusive = last.convertToExclusiveUpperBound(data)
if (lastExclusive != null) {
return@with ProgressionHeaderInfo(
data,
first = expression.dispatchReceiver!!,
first = first,
last = lastExclusive,
step = irInt(1),
direction = ProgressionDirection.INCREASING,
step = step,
direction = direction,
isLastInclusive = false,
canOverflow = false,
originalLastInclusive = last
)
}
}
ProgressionHeaderInfo(
data,
first = expression.dispatchReceiver!!,
last = last,
step = irInt(1),
direction = ProgressionDirection.INCREASING
)
ProgressionHeaderInfo(data, first = first, last = last, step = step, direction = direction)
}
private fun canUseExclusiveUpperBound(last: IrExpression, progressionType: ProgressionType): Boolean {
val lastLongValue = last.constLongValue
?: return false
return if (progressionType is UnsignedProgressionType) {
lastLongValue != -1L
} else {
lastLongValue != progressionType.maxValueAsLong
private fun IrExpression.convertToExclusiveUpperBound(progressionType: ProgressionType): IrExpression? {
if (progressionType is UnsignedProgressionType) {
if (this.constLongValue == -1L) return null
}
}
private fun IrExpression.convertToExclusiveUpperBound(): IrConstImpl<out Any>? {
val irConst = this as? IrConst<*> ?: return null
return when (irConst.kind) {
IrConstKind.Char -> {