-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #340 from hkapp/topic/optimizations/dominator-tree
Dominator tree algorithm
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
tools/src/main/scala/scala/scalanative/compiler/analysis/DominatorTree.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package scala.scalanative | ||
package compiler | ||
package analysis | ||
|
||
import scala.collection.immutable | ||
import scala.collection.mutable | ||
import ControlFlow.Block | ||
|
||
object DominatorTree { | ||
|
||
private def pred(block: Block) = block.pred.map(_.from) | ||
private def succ(block: Block) = block.succ.map(_.to) | ||
|
||
/** Fixpoint-based method to build the dominator tree | ||
* from the CFG. The dominator tree is simply represented | ||
* as a Map from a CFG block to the set of blocks dominating | ||
* this block. | ||
*/ | ||
def build(cfg: ControlFlow.Graph): Map[Block, Set[Block]] = { | ||
val domination = mutable.HashMap.empty[Block, Set[Block]] | ||
var workList = immutable.Queue(cfg.entry) | ||
|
||
while (workList.nonEmpty) { | ||
val (block, dequeued) = workList.dequeue | ||
workList = dequeued.filterNot(_ == block) // remove duplicates | ||
|
||
val visitedPreds = pred(block).filter(domination.contains) | ||
val predDomination = | ||
visitedPreds.toList.map(pred => domination.getOrElse(pred, Set.empty)) | ||
val correctPredDomination = predDomination.filterNot( | ||
predDominators => | ||
predDominators | ||
.contains(block)) // removes the potential dominators that are dominated by this block (counters loop problems) | ||
|
||
val blockDomination = | ||
if (correctPredDomination.isEmpty) | ||
Set.empty[Block] | ||
else | ||
correctPredDomination.tail.foldLeft(correctPredDomination.head)( | ||
(a, b) => a.intersect(b)) | ||
|
||
val oldDomination = domination.getOrElse(block, Set.empty) | ||
val newDomination = blockDomination + block // a block dominates itself | ||
|
||
if (oldDomination != newDomination) { | ||
domination += (block -> newDomination) | ||
workList ++= succ(block) | ||
} | ||
} | ||
|
||
domination.toMap | ||
} | ||
|
||
} |