1 19 20 package soot.toolkits.graph; 21 22 import soot.*; 23 import soot.toolkits.scalar.*; 24 import soot.toolkits.graph.*; 25 import soot.jimple.*; 26 import soot.options.*; 27 import java.util.*; 28 import soot.util.*; 29 30 48 public class DominatorTree 49 { 50 protected DominatorsFinder dominators; 51 protected DirectedGraph graph; 52 protected DominatorNode head; 53 protected ArrayList tails; 54 55 59 protected HashMap godeToDode; 60 61 public DominatorTree(DominatorsFinder dominators) 62 { 63 67 this.dominators = dominators; 68 this.graph = dominators.getGraph(); 69 70 head = null; 71 tails = new ArrayList(); 72 godeToDode = new HashMap(); 73 74 buildTree(); 75 } 76 77 81 public DirectedGraph getGraph() 82 { 83 return dominators.getGraph(); 84 } 85 86 89 public DominatorNode getHead() 90 { 91 return head; 92 } 93 94 97 public List getTails() 98 { 99 return (List) tails.clone(); 100 } 101 102 106 public DominatorNode getParentOf(DominatorNode node) 107 { 108 return node.getParent(); 109 } 110 111 114 public List getChildrenOf(DominatorNode node) 115 { 116 return (List)((ArrayList)node.getChildren()).clone(); 117 } 118 119 124 public List getPredsOf(DominatorNode node) 125 { 126 List preds = graph.getPredsOf(node.getGode()); 127 128 List predNodes = new ArrayList(); 129 130 for(Iterator predsIt = preds.iterator(); predsIt.hasNext();){ 131 Object pred = predsIt.next(); 132 predNodes.add(getDode(pred)); 133 } 134 135 return predNodes; 136 } 137 138 142 public List getSuccsOf(DominatorNode node) 143 { 144 List succs = graph.getSuccsOf(node.getGode()); 145 146 List succNodes = new ArrayList(); 147 148 for(Iterator succsIt = succs.iterator(); succsIt.hasNext();){ 149 Object succ = succsIt.next(); 150 succNodes.add(getDode(succ)); 151 } 152 153 return succNodes; 154 } 155 156 159 public boolean isImmediateDominatorOf(DominatorNode idom, DominatorNode node) 160 { 161 return (node.getParent() == idom); 163 } 164 165 168 public boolean isDominatorOf(DominatorNode dom, DominatorNode node) 169 { 170 return dominators.isDominatedBy(node.getGode(), dom.getGode()); 171 } 172 173 177 public DominatorNode getDode(Object gode) 178 { 179 DominatorNode dode = (DominatorNode) godeToDode.get(gode); 180 181 if(dode == null) 182 throw new RuntimeException ("Assertion failed: Dominator tree does not have a corresponding dode for gode (" + gode + ")"); 183 184 return dode; 185 } 186 187 191 public Iterator iterator() 192 { 193 return godeToDode.values().iterator(); 194 } 195 196 199 public int size() 200 { 201 return godeToDode.size(); 202 } 203 204 208 protected void buildTree() 209 { 210 { 212 for(Iterator godesIt = graph.iterator(); godesIt.hasNext();){ 213 Object gode = godesIt.next(); 214 215 DominatorNode dode = fetchDode(gode); 216 DominatorNode parent = fetchParent(gode); 217 218 if(parent == null){ 219 if(head != null) 220 throw new RuntimeException ("Assertion failed."); 221 222 head = dode; 223 } 224 else{ 225 parent.addChild(dode); 226 dode.setParent(parent); 227 } 228 } 229 } 230 231 { 233 for(Iterator dodesIt = this.iterator(); dodesIt.hasNext();){ 234 DominatorNode dode = (DominatorNode) dodesIt.next(); 235 236 if(dode.isTail()) 237 tails.add(dode); 238 } 239 } 240 } 241 242 246 protected DominatorNode fetchDode(Object gode) 247 { 248 DominatorNode dode; 249 250 if(godeToDode.containsKey(gode)){ 251 dode = (DominatorNode) godeToDode.get(gode); 252 } 253 else{ 254 dode = new DominatorNode(gode); 255 godeToDode.put(gode, dode); 256 } 257 258 return dode; 259 } 260 261 protected DominatorNode fetchParent(Object gode) 262 { 263 Object immediateDominator = dominators.getImmediateDominator(gode); 264 265 if(immediateDominator == null) 266 return null; 267 268 return fetchDode(immediateDominator); 269 } 270 } 271 | Popular Tags |