Optimize range operations for 'until' extension from stdlib (KT-9900)

NB: for-in-until loop is generated as precondition loop, because the
corresponding range is right-exclusive (and thus we have no problems
with integer overflows).
This commit is contained in:
Dmitry Petrov
2017-05-02 15:00:14 +03:00
parent b83620fdb9
commit 506941e7e0
27 changed files with 533 additions and 8 deletions
@@ -134,6 +134,17 @@ public class RangeCodegenUtil {
return true;
}
public static boolean isPrimitiveNumberUntil(@NotNull CallableDescriptor descriptor) {
if (!isTopLevelInPackage(descriptor, "until", "kotlin.ranges")) return false;
ReceiverParameterDescriptor extensionReceiver = descriptor.getExtensionReceiverParameter();
if (extensionReceiver == null) return false;
ClassifierDescriptor extensionReceiverClassifier = extensionReceiver.getType().getConstructor().getDeclarationDescriptor();
if (!isPrimitiveNumberClassDescriptor(extensionReceiverClassifier)) return false;
return true;
}
public static boolean isArrayOrPrimitiveArrayIndices(@NotNull CallableDescriptor descriptor) {
if (!isTopLevelInPackage(descriptor, "indices", "kotlin.collections")) return false;
@@ -66,9 +66,13 @@ abstract class AbstractForInRangeLoopGenerator : AbstractForInProgressionOrRange
override fun assignToLoopParameter() {}
override fun increment(loopExit: Label) {
override fun checkPostConditionAndIncrement(loopExit: Label) {
checkPostCondition(loopExit)
incrementLoopVariable()
}
protected fun incrementLoopVariable() {
if (loopParameterType === Type.INT_TYPE) {
v.iinc(loopParameterVar, step)
}
@@ -116,7 +116,7 @@ abstract class AbstractForLoopGenerator(
protected abstract fun assignToLoopParameter()
protected abstract fun increment(loopExit: Label)
protected abstract fun checkPostConditionAndIncrement(loopExit: Label)
fun body() {
codegen.generateLoopBody(forExpression.body)
@@ -135,7 +135,7 @@ abstract class AbstractForLoopGenerator(
fun afterBody(loopExit: Label) {
codegen.markStartLineNumber(forExpression)
increment(loopExit)
checkPostConditionAndIncrement(loopExit)
v.mark(bodyEnd)
}
@@ -74,7 +74,7 @@ class ForInArrayLoopGenerator(codegen: ExpressionCodegen, forExpression: KtForEx
v.store(loopParameterVar, asmElementType)
}
override fun increment(loopExit: Label) {
override fun checkPostConditionAndIncrement(loopExit: Label) {
v.iinc(indexVar, 1)
}
}
@@ -67,7 +67,7 @@ class ForInCharSequenceLoopGenerator(codegen: ExpressionCodegen, forExpression:
v.store(loopParameterVar, asmElementType)
}
override fun increment(loopExit: Label) {
override fun checkPostConditionAndIncrement(loopExit: Label) {
v.iinc(indexVar, 1)
}
@@ -92,7 +92,7 @@ class ForInProgressionExpressionLoopGenerator(codegen: ExpressionCodegen, forExp
override fun assignToLoopParameter() {}
override fun increment(loopExit: Label) {
override fun checkPostConditionAndIncrement(loopExit: Label) {
checkPostCondition(loopExit)
val loopParameter = loopParameter()
@@ -0,0 +1,58 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.codegen.forLoop
import org.jetbrains.kotlin.codegen.ExpressionCodegen
import org.jetbrains.kotlin.codegen.StackValue
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtForExpression
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue
import org.jetbrains.org.objectweb.asm.Label
import org.jetbrains.org.objectweb.asm.Type
class ForInUntilRangeLoopGenerator(
codegen: ExpressionCodegen,
forExpression: KtForExpression,
loopRangeCall: ResolvedCall<*>
) : AbstractForInRangeLoopGenerator(codegen, forExpression) {
private val from: ReceiverValue = loopRangeCall.extensionReceiver!!
private val to: KtExpression = ExpressionCodegen.getSingleArgumentExpression(loopRangeCall)!!
override fun storeRangeStartAndEnd() {
loopParameter().store(codegen.generateReceiverValue(from, false), v)
StackValue.local(endVar, asmElementType).store(codegen.gen(to), v)
}
override fun checkEmptyLoop(loopExit: Label) {}
override fun checkPreCondition(loopExit: Label) {
loopParameter().put(asmElementType, v)
v.load(endVar, asmElementType)
if (asmElementType.sort == Type.LONG) {
v.lcmp()
v.ifge(loopExit)
}
else {
v.ificmpge(loopExit)
}
}
override fun checkPostConditionAndIncrement(loopExit: Label) {
incrementLoopVariable()
}
}
@@ -68,6 +68,8 @@ private fun ExpressionCodegen.createOptimizedForLoopGeneratorOrNull(
ForInRangeLiteralLoopGenerator(this, forExpression, loopRangeCall)
RangeCodegenUtil.isPrimitiveNumberDownTo(loopRangeCallee) ->
ForInDownToProgressionLoopGenerator(this, forExpression, loopRangeCall)
RangeCodegenUtil.isPrimitiveNumberUntil(loopRangeCallee) ->
ForInUntilRangeLoopGenerator(this, forExpression, loopRangeCall)
RangeCodegenUtil.isArrayOrPrimitiveArrayIndices(loopRangeCallee) ->
ForInArrayIndicesRangeLoopGenerator(this, forExpression, loopRangeCall)
RangeCodegenUtil.isCollectionIndices(loopRangeCallee) ->
@@ -83,5 +83,5 @@ class IteratorForLoopGenerator(codegen: ExpressionCodegen, forExpression: KtForE
StackValue.local(loopParameterVar, loopParameterType).store(value, v)
}
override fun increment(loopExit: Label) {}
override fun checkPostConditionAndIncrement(loopExit: Label) {}
}