1
2
3
4 package joeq.Compiler.Quad;
5
6 /***
7 * @author Michael Martin <mcmartin@stanford.edu>
8 * @version $Id: DeadCode.java 2258 2005-05-05 19:37:36Z joewhaley $
9 */
10 public class DeadCode extends DataflowFramework.EmptyAnalysis {
11 static class TraceFact implements DataflowFramework.Fact {
12 boolean _val;
13 public TraceFact(boolean t) { _val = t; }
14
15 public DataflowFramework.Fact deepCopy() { return new TraceFact(_val); }
16 public DataflowFramework.Fact meetWith(DataflowFramework.Fact f) {
17 TraceFact other = (TraceFact) f;
18 _val = _val || other._val;
19 return this;
20 }
21 public boolean equals(Object o) {
22 if (o instanceof TraceFact) {
23 return ((TraceFact) o)._val == _val;
24 }
25 return false;
26 }
27 public int hashCode() {
28 return _val ? 1 : 0;
29 }
30 }
31
32 public void preprocess(ControlFlowGraph cfg) {
33 _fc.setInitial(new TraceFact(true));
34 _fc.setFinal(new TraceFact(false));
35 QuadIterator qi = new QuadIterator(cfg);
36 while (qi.hasNext()) {
37 Quad q = qi.nextQuad();
38 _fc.setPre(q, new TraceFact(false));
39 _fc.setPost(q, new TraceFact(false));
40 }
41 }
42
43 public boolean transfer(Quad q) {
44 DataflowFramework.Fact older = _fc.getPost(q).deepCopy();
45 DataflowFramework.Fact newer = _fc.getPre(q).deepCopy();
46 _fc.setPost(q, newer);
47 return !newer.equals(older);
48 }
49
50 public void postprocess(ControlFlowGraph cfg) {
51 QuadIterator qi = new QuadIterator(cfg);
52 System.out.println("Results:");
53 int deadCount = 0;
54 while (qi.hasNext()) {
55 Quad q = qi.nextQuad();
56 if (((TraceFact)(_fc.getPre(q)))._val) continue;
57 ++deadCount;
58 System.out.println("UNREACHABLE: "+q);
59 }
60 if (deadCount == 0) {
61 System.out.println("All quads are reachable.");
62 } else if (deadCount == 1) {
63 System.out.println("1 quad is unreachable.");
64 } else {
65 System.out.println(deadCount + " quads are unreachable.");
66 }
67 }
68 }