-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
42 additions
and
39 deletions.
There are no files selected for viewing
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
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
38 changes: 38 additions & 0 deletions
38
src/main/scala/ir/transforms/StripUnreachableFunctions.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,38 @@ | ||
package ir.transforms | ||
import ir._ | ||
import collection.mutable | ||
|
||
// This shouldn't be run before indirect calls are resolved | ||
def stripUnreachableFunctions(p: Program, depth: Int = Int.MaxValue): Unit = { | ||
val procedureCalleeNames = p.procedures.map(f => f.name -> f.calls.map(_.name)).toMap | ||
|
||
val toVisit: mutable.LinkedHashSet[(Int, String)] = mutable.LinkedHashSet((0, p.mainProcedure.name)) | ||
var reachableFound = true | ||
val reachableNames = mutable.HashMap[String, Int]() | ||
while (toVisit.nonEmpty) { | ||
val next = toVisit.head | ||
toVisit.remove(next) | ||
|
||
if (next._1 <= depth) { | ||
|
||
def addName(depth: Int, name: String): Unit = { | ||
val oldDepth = reachableNames.getOrElse(name, Integer.MAX_VALUE) | ||
reachableNames.put(next._2, if depth < oldDepth then depth else oldDepth) | ||
} | ||
addName(next._1, next._2) | ||
|
||
val callees = procedureCalleeNames(next._2) | ||
|
||
toVisit.addAll(callees.diff(reachableNames.keySet).map(c => (next._1 + 1, c))) | ||
callees.foreach(c => addName(next._1 + 1, c)) | ||
} | ||
} | ||
p.procedures = p.procedures.filter(f => reachableNames.keySet.contains(f.name)) | ||
|
||
for (elem <- p.procedures.filter(c => c.calls.exists(s => !p.procedures.contains(s)))) { | ||
// last layer is analysed only as specifications so we remove the body for anything that calls | ||
// a function we have removed | ||
|
||
elem.clearBlocks() | ||
} | ||
} |
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
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