Skip to content

Commit

Permalink
Copy objects less frequently (#1279)
Browse files Browse the repository at this point in the history
  • Loading branch information
KuechA authored Aug 30, 2023
1 parent 4308616 commit 2ec0759
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@
package de.fraunhofer.aisec.cpg.graph.statements

import de.fraunhofer.aisec.cpg.graph.AST
import de.fraunhofer.aisec.cpg.graph.StatementHolder
import de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge
import java.util.Objects
import org.apache.commons.lang3.builder.ToStringBuilder

/**
* A label attached to a statement that is used to change control flow by labeled continue and
* breaks (Java) or goto(C++).
*/
class LabelStatement : Statement() {
class LabelStatement : Statement(), StatementHolder {
/** Statement that the label is attached to. Can be a simple or compound statement. */
@AST var subStatement: Statement? = null

Expand All @@ -48,6 +50,12 @@ class LabelStatement : Statement() {
.toString()
}

override var statementEdges: MutableList<PropertyEdge<Statement>>
get() = subStatement?.let { PropertyEdge.wrap(listOf(it), this) } ?: mutableListOf()
set(value) {
subStatement = PropertyEdge.unwrap(value).firstOrNull()
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is LabelStatement) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ abstract class LatticeElement<T>(open val elements: T) : Comparable<LatticeEleme
*/
class PowersetLattice(override val elements: Set<Node>) : LatticeElement<Set<Node>>(elements) {
override fun lub(other: LatticeElement<Set<Node>>) =
PowersetLattice((other.elements).union(this.elements))
PowersetLattice(other.elements.union(this.elements))

override fun duplicate() = PowersetLattice(this.elements.toSet())

Expand Down Expand Up @@ -248,9 +248,21 @@ inline fun <reified K : Node, V> iterateEOG(
while (worklist.isNotEmpty()) {
val (nextNode, state) = worklist.pop()

val newState = transformation(nextNode, state.duplicate(), worklist)
// This should check if we're not near the beginning/end of a basic block (i.e., there are
// no merge points or branches of the EOG nearby). If that's the case, we just parse the
// whole basic block and do not want to duplicate the state. Near the beginning/end, we do
// want to copy the state to avoid terminating the iteration too early by messing up with
// the state-changing checks.
val insideBB =
(nextNode.nextEOG.size == 1 && nextNode.prevEOG.singleOrNull()?.nextEOG?.size == 1)
val newState =
transformation(nextNode, if (insideBB) state else state.duplicate(), worklist)
if (worklist.update(nextNode, newState)) {
nextNode.nextEOG.forEach { if (it is K) worklist.push(it, newState.duplicate()) }
nextNode.nextEOG.forEach {
if (it is K) {
worklist.push(it, newState)
}
}
}
}
return worklist.mop()
Expand All @@ -271,10 +283,22 @@ inline fun <reified K : PropertyEdge<Node>, N : Any, V> iterateEOG(
while (worklist.isNotEmpty()) {
val (nextEdge, state) = worklist.pop()

val newState = transformation(nextEdge, state.duplicate(), worklist)
if (worklist.update(nextEdge, newState)) {
// This should check if we're not near the beginning/end of a basic block (i.e., there are
// no merge points or branches of the EOG nearby). If that's the case, we just parse the
// whole basic block and do not want to duplicate the state. Near the beginning/end, we do
// want to copy the state to avoid terminating the iteration too early by messing up with
// the state-changing checks.
val insideBB =
(nextEdge.end.nextEOG.size == 1 &&
nextEdge.end.prevEOG.size == 1 &&
nextEdge.start.nextEOG.size == 1)
val newState =
transformation(nextEdge, if (insideBB) state else state.duplicate(), worklist)
if (insideBB || worklist.update(nextEdge, newState)) {
nextEdge.end.nextEOGEdges.forEach {
if (it is K) worklist.push(it, newState.duplicate())
if (it is K) {
worklist.push(it, newState)
}
}
}
}
Expand Down

0 comments on commit 2ec0759

Please sign in to comment.