From 1e04945609a37a175ea86954da14555f780d8679 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Thu, 20 Oct 2016 16:08:21 +0300 Subject: [PATCH] CFG refactoring: get rid of mergeWithLocalDeclarations (always true) + minor style fixes --- .../kotlin/cfg/PseudocodeTraverser.kt | 31 +++++++------------ .../cfg/PseudocodeVariableDataCollector.kt | 3 +- .../kotlin/cfg/PseudocodeVariablesData.kt | 8 ++--- 3 files changed, 14 insertions(+), 28 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/cfg/PseudocodeTraverser.kt b/compiler/frontend/src/org/jetbrains/kotlin/cfg/PseudocodeTraverser.kt index 50176e5efea..7fbd205157f 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/cfg/PseudocodeTraverser.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/cfg/PseudocodeTraverser.kt @@ -57,7 +57,6 @@ fun Pseudocode.traverse( fun > Pseudocode.collectData( traversalOrder: TraversalOrder, - mergeDataWithLocalDeclarations: Boolean, mergeEdges: (Instruction, Collection) -> Edges, updateEdge: (Instruction, Instruction, I) -> I, initialInfo: I @@ -70,7 +69,7 @@ fun > Pseudocode.collectData( while (changed[0]) { changed[0] = false collectDataFromSubgraph( - traversalOrder, mergeDataWithLocalDeclarations, edgesMap, + traversalOrder, edgesMap, mergeEdges, updateEdge, Collections.emptyList(), changed, false) } return edgesMap @@ -78,7 +77,6 @@ fun > Pseudocode.collectData( private fun > Pseudocode.collectDataFromSubgraph( traversalOrder: TraversalOrder, - mergeDataWithLocalDeclarations: Boolean, edgesMap: MutableMap>, mergeEdges: (Instruction, Collection) -> Edges, updateEdge: (Instruction, Instruction, I) -> I, @@ -98,30 +96,23 @@ private fun > Pseudocode.collectDataFromSubgraph( if (instruction is LocalFunctionDeclarationInstruction) { val subroutinePseudocode = instruction.body - val previous = if (mergeDataWithLocalDeclarations) previousInstructions else Collections.emptyList() subroutinePseudocode.collectDataFromSubgraph( - traversalOrder, mergeDataWithLocalDeclarations, - edgesMap, mergeEdges, updateEdge, previous, changed, true) - if (mergeDataWithLocalDeclarations) { - val lastInstruction = subroutinePseudocode.getLastInstruction(traversalOrder) - val previousValue = edgesMap.get(instruction) - val newValue = edgesMap.get(lastInstruction) - val updatedValue = - if (newValue == null) - null - else - Edges(updateEdge(lastInstruction, instruction, newValue.incoming), - updateEdge(lastInstruction, instruction, newValue.outgoing)) - updateEdgeDataForInstruction(instruction, previousValue, updatedValue, edgesMap, changed) - continue + traversalOrder, edgesMap, mergeEdges, updateEdge, previousInstructions, changed, true) + val lastInstruction = subroutinePseudocode.getLastInstruction(traversalOrder) + val previousValue = edgesMap[instruction] + val newValue = edgesMap[lastInstruction] + val updatedValue = newValue?.let { + Edges(updateEdge(lastInstruction, instruction, it.incoming), updateEdge(lastInstruction, instruction, it.outgoing)) } + updateEdgeDataForInstruction(instruction, previousValue, updatedValue, edgesMap, changed) + continue } - val previousDataValue = edgesMap.get(instruction) + val previousDataValue = edgesMap[instruction] val incomingEdgesData = HashSet() for (previousInstruction in previousInstructions) { - val previousData = edgesMap.get(previousInstruction) + val previousData = edgesMap[previousInstruction] if (previousData != null) { incomingEdgesData.add(updateEdge( previousInstruction, instruction, previousData.outgoing)) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/cfg/PseudocodeVariableDataCollector.kt b/compiler/frontend/src/org/jetbrains/kotlin/cfg/PseudocodeVariableDataCollector.kt index c0b25ac78a3..fa97040924a 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/cfg/PseudocodeVariableDataCollector.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/cfg/PseudocodeVariableDataCollector.kt @@ -40,12 +40,11 @@ class PseudocodeVariableDataCollector( fun > collectData( traversalOrder: TraversalOrder, - mergeDataWithLocalDeclarations: Boolean, initialInfo: I, instructionDataMergeStrategy: (Instruction, Collection) -> Edges ): Map> { return pseudocode.collectData( - traversalOrder, mergeDataWithLocalDeclarations, + traversalOrder, instructionDataMergeStrategy, { from, to, info -> filterOutVariablesOutOfScope(from, to, info) }, initialInfo diff --git a/compiler/frontend/src/org/jetbrains/kotlin/cfg/PseudocodeVariablesData.kt b/compiler/frontend/src/org/jetbrains/kotlin/cfg/PseudocodeVariablesData.kt index 1a33af1ceb1..40ecd4f56f3 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/cfg/PseudocodeVariablesData.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/cfg/PseudocodeVariablesData.kt @@ -89,9 +89,7 @@ class PseudocodeVariablesData(val pseudocode: Pseudocode, private val bindingCon val blockScopeVariableInfo = pseudocodeVariableDataCollector.blockScopeVariableInfo - return pseudocodeVariableDataCollector.collectData( - TraversalOrder.FORWARD, /*mergeDataWithLocalDeclarations=*/ true, InitControlFlowInfo() - ) { + return pseudocodeVariableDataCollector.collectData(TraversalOrder.FORWARD, InitControlFlowInfo()) { instruction: Instruction, incomingEdgesData: Collection -> val enterInstructionData = mergeIncomingEdgesDataForInitializers(instruction, incomingEdgesData, blockScopeVariableInfo) @@ -148,9 +146,7 @@ class PseudocodeVariablesData(val pseudocode: Pseudocode, private val bindingCon // variable use val variableUseStatusData: Map> - get() = pseudocodeVariableDataCollector.collectData( - TraversalOrder.BACKWARD, true, UseControlFlowInfo() - ) { + get() = pseudocodeVariableDataCollector.collectData(TraversalOrder.BACKWARD, UseControlFlowInfo()) { instruction: Instruction, incomingEdgesData: Collection -> val enterResult: UseControlFlowInfo