Skip to content

Commit e90c8f0

Browse files
committed
add RemoveUnreachableBlocks pass to clean up padding blocks
1 parent 8b6893c commit e90c8f0

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package ir.transforms
2+
3+
import ir.*
4+
import ir.cilvisitor.*
5+
6+
import analysis.{LatticeSet}
7+
8+
import collection.immutable
9+
10+
/**
11+
* Find blocks which are mentioned syntactically within a given procedure.
12+
*
13+
* Includes the entry block, and overapproximates in the presence of indirect calls.
14+
*/
15+
class FindGotoTargets extends CILVisitor {
16+
17+
var keep: LatticeSet[Block] = LatticeSet.Bottom()
18+
19+
override def vproc(p: Procedure) = {
20+
keep = keep ++ p.entryBlock
21+
DoChildren()
22+
}
23+
24+
override def vstmt(s: Statement) = {
25+
s match {
26+
case IndirectCall(_, _) => keep = LatticeSet.Top()
27+
case _ => ()
28+
}
29+
SkipChildren()
30+
}
31+
32+
override def vjump(s: Jump) = {
33+
s match {
34+
case GoTo(targs, _) => keep = keep ++ targs
35+
case _ => ()
36+
}
37+
SkipChildren()
38+
}
39+
}
40+
41+
/**
42+
* Very coarse transform pass to remove obviously unreachable blocks.
43+
*
44+
* This only removes in the most simple cases.
45+
* Does NOT remove:
46+
* - mutually-recursive unreachable blocks,
47+
* - blocks only reachable from unreachable blocks.
48+
* - any blocks in the presence of indirect calls.
49+
*/
50+
object RemoveUnreachableBlocks {
51+
def apply(p: Procedure) = {
52+
val vis = FindGotoTargets()
53+
visit_proc(vis, p)
54+
val keepBlocks = vis.keep
55+
p.blocks.toList.filterNot(keepBlocks.contains).foreach(p.removeBlocks)
56+
}
57+
}

src/main/scala/util/RunUtils.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,8 @@ object RunUtils {
894894

895895
if (!q.loading.keepPC) {
896896
visit_prog(transforms.RemovePCStatements(), ctx.program)
897-
Logger.info(s"[!] Removed PC-related statements")
897+
ctx.program.procedures.foreach(transforms.RemoveUnreachableBlocks.apply)
898+
Logger.info(s"[!] Removed PC-related statements and unreachable blocks")
898899
}
899900

900901
if (q.loading.parameterForm && !q.simplify) {

0 commit comments

Comments
 (0)