[FIR] Reduce coping of flow and inline default value of it

This commit is contained in:
Dmitriy Novozhilov
2019-09-02 11:35:51 +03:00
parent c2180b9361
commit 546bbceeea
2 changed files with 85 additions and 99 deletions
@@ -37,7 +37,7 @@ class FirDataFlowAnalyzer(transformer: FirBodyResolveTransformer) : BodyResolveC
private val graphBuilder = ControlFlowGraphBuilder()
private val logicSystem = LogicSystem(context)
private val variableStorage = DataFlowVariableStorage()
private val edges = mutableMapOf<CFGNode<*>, Flow>().withDefault { Flow.EMPTY }
private val edges = mutableMapOf<CFGNode<*>, Flow>()
/*
* If there is no types from smartcasts function returns null
@@ -92,7 +92,6 @@ class FirDataFlowAnalyzer(transformer: FirBodyResolveTransformer) : BodyResolveC
if (previousNode != null) {
node.flow = logicSystem.approveFactsInsideFlow(previousNode.variable, EqTrue, node.flow)
}
node.flow.freeze()
}
fun exitBlock(block: FirBlock) {
@@ -111,7 +110,6 @@ class FirDataFlowAnalyzer(transformer: FirBodyResolveTransformer) : BodyResolveC
fun exitTypeOperatorCall(typeOperatorCall: FirTypeOperatorCall) {
val node = graphBuilder.exitTypeOperatorCall(typeOperatorCall).passFlow(false)
try {
if (typeOperatorCall.operation !in FirOperation.TYPES) return
val symbol: FirCallableSymbol<*> = typeOperatorCall.argument.getResolvedSymbol() ?: return
val type = typeOperatorCall.conversionTypeRef.coneTypeSafe<ConeKotlinType>() ?: return
@@ -166,14 +164,10 @@ class FirDataFlowAnalyzer(transformer: FirBodyResolveTransformer) : BodyResolveC
}
node.flow = flow
} finally {
node.flow.freeze()
}
}
fun exitOperatorCall(operatorCall: FirOperatorCall) {
val node = graphBuilder.exitOperatorCall(operatorCall).passFlow(false)
try {
when (val operation = operatorCall.operation) {
FirOperation.EQ, FirOperation.NOT_EQ, FirOperation.IDENTITY, FirOperation.NOT_IDENTITY -> {
val leftOperand = operatorCall.arguments[0]
@@ -192,9 +186,6 @@ class FirDataFlowAnalyzer(transformer: FirBodyResolveTransformer) : BodyResolveC
}
}
}
} finally {
node.flow.freeze()
}
}
private fun processEqWithConst(node: OperatorCallNode, operand: FirExpression, const: FirConstExpression<*>, operation: FirOperation) {
@@ -327,7 +318,7 @@ class FirDataFlowAnalyzer(transformer: FirBodyResolveTransformer) : BodyResolveC
}
fun exitWhenBranchCondition(whenBranch: FirWhenBranch) {
val node = graphBuilder.exitWhenBranchCondition(whenBranch).passFlow()
val node = graphBuilder.exitWhenBranchCondition(whenBranch).passFlow(true)
val conditionVariable = getVariable(whenBranch.condition)
node.variable = conditionVariable
@@ -336,7 +327,7 @@ class FirDataFlowAnalyzer(transformer: FirBodyResolveTransformer) : BodyResolveC
fun exitWhenBranchResult(whenBranch: FirWhenBranch) {
val node = graphBuilder.exitWhenBranchResult(whenBranch).passFlow(false)
val conditionVariable = getVariable(whenBranch.condition)
node.flow = node.flow.removeSyntheticVariable(conditionVariable)
node.flow = node.flow.removeSyntheticVariable(conditionVariable).apply { freeze() }
}
fun exitWhenExpression(whenExpression: FirWhenExpression) {
@@ -448,7 +439,6 @@ class FirDataFlowAnalyzer(transformer: FirBodyResolveTransformer) : BodyResolveC
fun exitVariableDeclaration(variable: FirVariable<*>) {
val node = graphBuilder.exitVariableDeclaration(variable).passFlow(false)
try {
val initializer = variable.initializer ?: return
/*
@@ -470,10 +460,6 @@ class FirDataFlowAnalyzer(transformer: FirBodyResolveTransformer) : BodyResolveC
val rhsVariable = variableStorage[initializerSymbol]?.takeIf { !it.isSynthetic } ?: return
variableStorage.createAliasVariable(variable.symbol, rhsVariable)
}
} finally {
node.flow.freeze()
}
}
fun exitVariableAssignment(assignment: FirVariableAssignment) {
@@ -495,7 +481,7 @@ class FirDataFlowAnalyzer(transformer: FirBodyResolveTransformer) : BodyResolveC
fun exitLeftBinaryAndArgument(binaryLogicExpression: FirBinaryLogicExpression) {
val (leftNode, rightNode) = graphBuilder.exitLeftBinaryAndArgument(binaryLogicExpression)
leftNode.passFlow()
leftNode.passFlow(true)
rightNode.passFlow(false)
val leftOperandVariable = getVariable(leftNode.previousNodes.first().fir)
rightNode.flow = logicSystem.approveFactsInsideFlow(leftOperandVariable, EqTrue, rightNode.flow).also { it.freeze() }
@@ -531,7 +517,7 @@ class FirDataFlowAnalyzer(transformer: FirBodyResolveTransformer) : BodyResolveC
flow.addNotApprovedFact(andVariable, UnapprovedFirDataFlowInfo(EqFalse, variable, info))
}
}
node.flow = flow.removeSyntheticVariable(leftVariable).removeSyntheticVariable(rightVariable).also { it.freeze() }
node.flow = flow.removeSyntheticVariable(leftVariable).removeSyntheticVariable(rightVariable)
}
fun enterBinaryOr(binaryLogicExpression: FirBinaryLogicExpression) {
@@ -540,7 +526,7 @@ class FirDataFlowAnalyzer(transformer: FirBodyResolveTransformer) : BodyResolveC
fun exitLeftBinaryOrArgument(binaryLogicExpression: FirBinaryLogicExpression) {
val (leftNode, rightNode) = graphBuilder.exitLeftBinaryOrArgument(binaryLogicExpression)
leftNode.passFlow()
leftNode.passFlow(true)
rightNode.passFlow(false)
val leftOperandVariable = getVariable(leftNode.previousNodes.first().fir)
rightNode.flow = logicSystem.approveFactsInsideFlow(leftOperandVariable, EqFalse, rightNode.flow).also { it.freeze() }
@@ -571,13 +557,13 @@ class FirDataFlowAnalyzer(transformer: FirBodyResolveTransformer) : BodyResolveC
flow.addNotApprovedFact(orVariable, UnapprovedFirDataFlowInfo(EqFalse, variable, info))
}
}
node.flow = flow.removeSyntheticVariable(leftVariable).removeSyntheticVariable(rightVariable).also { it.freeze() }
node.flow = flow.removeSyntheticVariable(leftVariable).removeSyntheticVariable(rightVariable)
}
private fun exitBooleanNot(functionCall: FirFunctionCall, node: FunctionCallNode) {
val booleanExpressionVariable = getVariable(node.previousNodes.first().fir)
val variable = getVariable(functionCall)
node.flow = node.flow.copyNotApprovedFacts(booleanExpressionVariable, variable) { it.invert() }.also { it.freeze() }
node.flow = node.flow.copyNotApprovedFacts(booleanExpressionVariable, variable) { it.invert() }
}
// ----------------------------------- Annotations -----------------------------------
@@ -609,12 +595,12 @@ class FirDataFlowAnalyzer(transformer: FirBodyResolveTransformer) : BodyResolveC
getVariable(leftOperand) to getVariable(rightOperand)
private var CFGNode<*>.flow: Flow
get() = edges.getValue(this)
get() = edges[this] ?: Flow.EMPTY
set(value) {
edges[this] = value
}
private fun <T : CFGNode<*>> T.passFlow(shouldFreeze: Boolean = true): T = this.also { node ->
private fun <T : CFGNode<*>> T.passFlow(shouldFreeze: Boolean = false): T = this.also { node ->
node.flow = logicSystem.or(node.usefulPreviousNodes.map { it.flow }).also {
if (shouldFreeze) it.freeze()
}
@@ -12,7 +12,7 @@ class LogicSystem(private val context: DataFlowInferenceContext) {
fun or(storages: Collection<Flow>): Flow {
storages.singleOrNull()?.let {
return it.copy()
return it
}
val approvedFacts = mutableMapOf<DataFlowVariable, FirDataFlowInfo>().apply {
storages.map { it.approvedFacts.keys }