Drop excessive lowerings that are replaced by ConstEvaluationLowering

This commit is contained in:
Ivan Kylchik
2021-12-15 17:32:36 +03:00
parent 0f0b48f87b
commit dca22d745a
7 changed files with 8 additions and 121 deletions
@@ -1,96 +0,0 @@
/*
* 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.backend.common.lower
import org.jetbrains.kotlin.backend.common.BackendContext
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrProperty
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.IrCompositeImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl
import org.jetbrains.kotlin.ir.util.isSubclassOf
import org.jetbrains.kotlin.ir.util.render
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
import org.jetbrains.kotlin.name.Name
val kCallableNamePropertyPhase = makeIrFilePhase(
::KCallableNamePropertyLowering,
name = "KCallableNameProperty",
description = "Replace name references for callables with constants"
)
private class KCallableNamePropertyLowering(val context: BackendContext) : FileLoweringPass {
override fun lower(irFile: IrFile) {
irFile.transformChildrenVoid(KCallableNamePropertyTransformer(this))
}
}
private class KCallableNamePropertyTransformer(val lower: KCallableNamePropertyLowering) : IrElementTransformerVoid() {
private fun nameForCallableMember(reference: IrCallableReference<*>): Name {
return when (reference) {
is IrFunctionReference -> reference.symbol.owner.name
is IrPropertyReference -> reference.symbol.owner.name
is IrLocalDelegatedPropertyReference -> reference.symbol.owner.name
else -> error("Unexpected callable reference type ${reference.render()}")
}
}
override fun visitCall(expression: IrCall): IrExpression {
val callableReference = expression.dispatchReceiver as? IrCallableReference<*>
?: return super.visitCall(expression)
val directMember = expression.symbol.owner.let {
(it as? IrSimpleFunction)?.correspondingPropertySymbol?.owner ?: it
}
val irClass = directMember.parent as? IrClass
?: return super.visitCall(expression)
if (!irClass.isSubclassOf(lower.context.irBuiltIns.kCallableClass.owner)) {
return super.visitCall(expression)
}
val name = when (directMember) {
is IrSimpleFunction -> directMember.name
is IrProperty -> directMember.name
else -> throw AssertionError("Should be IrSimpleFunction or IrProperty, got $directMember")
}
if (name.asString() != "name") return expression
val receiver = callableReference.dispatchReceiver ?: callableReference.extensionReceiver
return IrCompositeImpl(expression.startOffset, expression.endOffset, lower.context.irBuiltIns.stringType).apply {
receiver?.let {
//put receiver for bound callable reference
statements.add(it)
}
statements.add(
IrConstImpl.string(
expression.startOffset,
expression.endOffset,
lower.context.irBuiltIns.stringType,
nameForCallableMember(callableReference).asString()
)
)
}
}
}
@@ -7,8 +7,11 @@ package org.jetbrains.kotlin.backend.common.lower.optimizations
import org.jetbrains.kotlin.backend.common.BodyLoweringPass
import org.jetbrains.kotlin.backend.common.CommonBackendContext
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase
import org.jetbrains.kotlin.builtins.PrimitiveType
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.config.languageVersionSettings
import org.jetbrains.kotlin.ir.BuiltInOperatorNames
import org.jetbrains.kotlin.ir.IrBuiltIns
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
@@ -30,12 +33,6 @@ import kotlin.math.floor
import kotlin.math.max
import kotlin.math.min
val foldConstantLoweringPhase = makeIrFilePhase(
{ ctx: CommonBackendContext -> FoldConstantLowering(ctx) },
name = "FoldConstantLowering",
description = "Constant Folding"
)
/**
* A pass to fold constant expressions of most common types.
*
@@ -10,7 +10,6 @@ import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.checkDeclarationParents
import org.jetbrains.kotlin.backend.common.lower.*
import org.jetbrains.kotlin.backend.common.lower.loops.forLoopsPhase
import org.jetbrains.kotlin.backend.common.lower.optimizations.foldConstantLoweringPhase
import org.jetbrains.kotlin.backend.common.phaser.*
import org.jetbrains.kotlin.backend.jvm.ir.constantValue
import org.jetbrains.kotlin.backend.jvm.ir.shouldContainSuspendMarkers
@@ -161,17 +160,6 @@ private val jvmLocalClassExtractionPhase = makeIrFilePhase(
description = "Move local classes from field initializers and anonymous init blocks into the containing class"
)
private val computeStringTrimPhase = makeIrFilePhase<JvmBackendContext>(
{ context ->
if (context.state.canReplaceStdlibRuntimeApiBehavior)
StringTrimLowering(context)
else
FileLoweringPass.Empty
},
name = "StringTrimLowering",
description = "Compute trimIndent and trimMargin operations on constant strings"
)
private val defaultArgumentStubPhase = makeIrFilePhase(
::JvmDefaultArgumentStubGenerator,
name = "DefaultArgumentsStubGenerator",
@@ -290,7 +278,6 @@ private val jvmFilePhases = listOf(
mainMethodGenerationPhase,
inventNamesForLocalClassesPhase,
kCallableNamePropertyPhase,
annotationPhase,
annotationImplementationPhase,
polymorphicSignaturePhase,
@@ -338,8 +325,6 @@ private val jvmFilePhases = listOf(
jvmDefaultConstructorPhase,
flattenStringConcatenationPhase,
foldConstantLoweringPhase,
computeStringTrimPhase,
jvmStringConcatenationLowering,
defaultArgumentStubPhase,
@@ -7,7 +7,7 @@ class A {
val b = A::b.name
val c = ::A.name
val d = this::a.name
val e = A()::b.name
// val d = this::a.name // Not supported
// val e = A()::b.name // Not supported
}
}
@@ -15,4 +15,4 @@ const val stringClassName = ::String.name
const val lengthPropName = String::length.name
const val errorAccess = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>SomeClassWithName(1)::property.name<!>
const val errorPlus = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>"" + ::SomeClassWithName.<!UNRESOLVED_REFERENCE!>property<!><!>
const val errorPlus = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>"" + SomeClassWithName(1)::property<!>
@@ -15,4 +15,4 @@ const val stringClassName = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>::String.nam
const val lengthPropName = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>String::length.name<!>
const val errorAccess = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>SomeClassWithName(1)::property.name<!>
const val errorPlus = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>"" + ::SomeClassWithName.property<!>
const val errorPlus = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>"" + SomeClassWithName(1)::property<!>
@@ -4,6 +4,7 @@ public const val anotherPropName: kotlin.String
public const val barName: kotlin.String
public const val className: kotlin.String
public const val errorAccess: kotlin.String
public const val errorPlus: kotlin.String
public const val fooName: kotlin.String
public const val lengthPropName: kotlin.String
public const val propName: kotlin.String