From a2e3d3635ee0d4339effdbb42f9d68b87789cd66 Mon Sep 17 00:00:00 2001 From: Mark Punzalan Date: Tue, 19 Mar 2019 22:30:51 -0700 Subject: [PATCH] Replace HeaderInfo.increasing property with a "direction" enum property, which is computed based on the value of "step" (if possible). --- .../backend/common/lower/loops/HeaderInfo.kt | 22 ++++++++++++++----- .../common/lower/loops/HeaderProcessor.kt | 5 +++-- .../common/lower/loops/ProgressionHandlers.kt | 3 +-- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderInfo.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderInfo.kt index 2aefa6c28f6..1fbcf28552f 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderInfo.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderInfo.kt @@ -12,6 +12,7 @@ import org.jetbrains.kotlin.ir.IrElement import org.jetbrains.kotlin.ir.declarations.IrVariable import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns import org.jetbrains.kotlin.ir.expressions.IrCall +import org.jetbrains.kotlin.ir.expressions.IrConst import org.jetbrains.kotlin.ir.expressions.IrExpression import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.types.isSubtypeOfClass @@ -46,24 +47,36 @@ enum class ProgressionType(val numberCastFunctionName: Name) { } } +internal enum class ProgressionDirection { DECREASING, INCREASING, UNKNOWN } + // Information about loop that is required by HeaderProcessor to build ForLoopHeader internal sealed class HeaderInfo( val progressionType: ProgressionType, val lowerBound: IrExpression, val upperBound: IrExpression, val step: IrExpression, - val increasing: Boolean, val closed: Boolean -) +) { + val direction: ProgressionDirection by lazy { + // If step is a constant (either Int or Long), then we can determine the direction. + val stepValue = (step as? IrConst<*>)?.value as? Number? + val stepValueAsLong = stepValue?.toLong() + when { + stepValueAsLong == null -> ProgressionDirection.UNKNOWN + stepValueAsLong < 0L -> ProgressionDirection.DECREASING + stepValueAsLong > 0L -> ProgressionDirection.INCREASING + else -> ProgressionDirection.UNKNOWN + } + } +} internal class ProgressionHeaderInfo( progressionType: ProgressionType, lowerBound: IrExpression, upperBound: IrExpression, step: IrExpression, - increasing: Boolean = true, closed: Boolean = true -) : HeaderInfo(progressionType, lowerBound, upperBound, step, increasing, closed) +) : HeaderInfo(progressionType, lowerBound, upperBound, step, closed) internal class ArrayHeaderInfo( lowerBound: IrExpression, @@ -75,7 +88,6 @@ internal class ArrayHeaderInfo( lowerBound, upperBound, step, - increasing = true, closed = false ) diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderProcessor.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderProcessor.kt index cd39256eb92..a4afcb06cf9 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderProcessor.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderProcessor.kt @@ -54,9 +54,10 @@ internal class ProgressionLoopHeader( val closed = headerInfo.closed - private val increasing = headerInfo.increasing + private val direction = headerInfo.direction - fun comparingFunction(builtIns: IrBuiltIns) = if (increasing) + // TODO: Handle UNKNOWN direction, which requires a complex expression for the emptiness check + fun comparingFunction(builtIns: IrBuiltIns) = if (direction == ProgressionDirection.INCREASING) builtIns.lessOrEqualFunByOperandType[builtIns.int]?.symbol!! else builtIns.greaterOrEqualFunByOperandType[builtIns.int]?.symbol!! diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/ProgressionHandlers.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/ProgressionHandlers.kt index 114ba8a701e..003312b55f5 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/ProgressionHandlers.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/ProgressionHandlers.kt @@ -59,8 +59,7 @@ internal class DownToHandler(private val context: CommonBackendContext, private data, lowerBound = call.extensionReceiver!!, upperBound = call.getValueArgument(0)!!, - step = irInt(-1), - increasing = false + step = irInt(-1) ) } }