1 19 20 package soot.toolkits.graph; 21 22 import soot.*; 23 import soot.util.*; 24 import java.util.*; 25 import soot.toolkits.scalar.*; 26 import soot.options.*; 27 28 public class DominatorAnalysis extends ForwardFlowAnalysis { 39 40 private UnitGraph g; 41 private FlowSet allNodes; 42 43 public DominatorAnalysis(UnitGraph g) 44 { 45 super(g); 46 this.g = g; 47 48 initAllNodes(); 49 50 doAnalysis(); 51 52 } 53 54 private void initAllNodes(){ 55 allNodes = new ArraySparseSet(); 56 Iterator it = g.iterator(); 57 while (it.hasNext()){ 58 allNodes.add(it.next()); 59 } 60 } 61 62 63 66 protected void merge(Object in1, Object in2, Object out) 67 { 68 FlowSet inSet1 = (FlowSet) in1; 69 FlowSet inSet2 = (FlowSet) in2; 70 FlowSet outSet = (FlowSet) out; 71 72 inSet1.intersection(inSet2, outSet); 73 74 } 75 76 protected void copy(Object source, Object dest) { 77 78 FlowSet sourceIn = (FlowSet)source; 79 FlowSet destOut = (FlowSet)dest; 80 81 sourceIn.copy(destOut); 82 83 } 84 85 90 protected void flowThrough(Object inValue, Object unit, 91 Object outValue) 92 { 93 FlowSet in = (FlowSet) inValue; 94 FlowSet out = (FlowSet) outValue; 95 Unit s = (Unit) unit; 96 97 if (isUnitStartNode(s)){ 98 out.clear(); 100 out.add(s); 101 } 103 else { 104 105 FlowSet domsOfPreds = (FlowSet) allNodes.clone(); 107 108 Iterator predsIt = g.getPredsOf(s).iterator(); 110 while (predsIt.hasNext()){ 111 Unit pred = (Unit)predsIt.next(); 112 FlowSet next = (FlowSet) unitToAfterFlow.get(pred); 115 in.intersection(next, in); 118 } 120 121 123 out.intersection(in, out); 125 out.add(s); 126 } 128 } 129 130 private boolean isUnitStartNode(Unit s){ 131 if (s.equals(g.getHeads().get(0))) return true; 133 return false; 134 } 135 136 protected Object entryInitialFlow() 142 { 143 144 FlowSet fs = new ArraySparseSet(); 145 List heads = g.getHeads(); 146 if (heads.size() != 1) { 147 throw new RuntimeException ("Expect one start node only."); 148 } 149 fs.add(heads.get(0)); 150 return fs; 151 } 152 153 154 protected Object newInitialFlow() 155 { 156 return allNodes.clone(); 157 } 158 159 160 } 161 | Popular Tags |