Store copies of instruction only once
removed reference to 'original' instruction
This commit is contained in:
+2
-2
@@ -23,7 +23,7 @@ import org.jetbrains.jet.lang.cfg.pseudocode.PseudoValue
|
||||
public trait Instruction {
|
||||
public var owner: Pseudocode
|
||||
|
||||
public val previousInstructions: MutableCollection<Instruction>
|
||||
public val previousInstructions: Collection<Instruction>
|
||||
public val nextInstructions: Collection<Instruction>
|
||||
|
||||
public val dead: Boolean
|
||||
@@ -32,7 +32,7 @@ public trait Instruction {
|
||||
|
||||
public val inputValues: List<PseudoValue>
|
||||
|
||||
public fun getCopies(): Collection<Instruction>
|
||||
public val copies: Collection<Instruction>
|
||||
|
||||
public fun accept(visitor: InstructionVisitor)
|
||||
public fun <R> accept(visitor: InstructionVisitorWithResult<R>): R
|
||||
|
||||
+11
-25
@@ -33,46 +33,32 @@ public abstract class InstructionImpl(public override val lexicalScope: LexicalS
|
||||
_owner = value
|
||||
}
|
||||
|
||||
private val _copies = HashSet<Instruction>()
|
||||
private var allCopies: MutableSet<InstructionImpl>? = null
|
||||
|
||||
override fun getCopies(): Collection<Instruction> {
|
||||
return original?.let { original ->
|
||||
val originalCopies = Sets.newHashSet(original.getCopies())
|
||||
originalCopies.remove(this)
|
||||
originalCopies.add(original)
|
||||
originalCopies
|
||||
} ?: _copies
|
||||
}
|
||||
override val copies: Collection<Instruction>
|
||||
get() = allCopies?.filter { it != this } ?: Collections.emptyList()
|
||||
|
||||
private var original: Instruction? = null
|
||||
|
||||
private fun setOriginalInstruction(value: Instruction?) {
|
||||
assert(original == null) { "Instruction can't have two originals: this.original = ${original}; new original = $this" }
|
||||
original = value
|
||||
}
|
||||
|
||||
public fun copy(): Instruction {
|
||||
return updateCopyInfo(createCopy())
|
||||
}
|
||||
fun copy(): Instruction = updateCopyInfo(createCopy())
|
||||
|
||||
protected abstract fun createCopy(): InstructionImpl
|
||||
|
||||
protected fun updateCopyInfo(instruction: InstructionImpl): Instruction {
|
||||
_copies.add(instruction)
|
||||
instruction.setOriginalInstruction(this)
|
||||
if (allCopies == null) {
|
||||
allCopies = hashSetOf(this)
|
||||
}
|
||||
instruction.allCopies = allCopies
|
||||
allCopies!!.add(instruction)
|
||||
return instruction
|
||||
}
|
||||
|
||||
public var markedAsDead: Boolean = false
|
||||
|
||||
override val dead: Boolean get() = markedAsDead && getCopies().all { (it as InstructionImpl).markedAsDead }
|
||||
override val dead: Boolean get() = allCopies?.all { it.markedAsDead } ?: markedAsDead
|
||||
|
||||
override val previousInstructions: MutableCollection<Instruction> = LinkedHashSet()
|
||||
|
||||
protected fun outgoingEdgeTo(target: Instruction?): Instruction? {
|
||||
if (target != null) {
|
||||
target.previousInstructions.add(this)
|
||||
}
|
||||
(target as InstructionImpl?)?.previousInstructions?.add(this)
|
||||
return target
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user