1 package Analysis.Pointer;
5 import Analysis.Pointer.BasicBlock.BBlock;
6 import Analysis.Pointer.AllocFactory.AllocNode;
9 HashMap<FlatMethod, BasicBlock> blockMap;
10 HashMap<BBlock, Graph> bbgraphMap;
11 HashMap<FlatNode, Graph> graphMap;
12 HashMap<FlatCall, Set<BBlock>> callMap;
13 HashMap<BBlock, Set<PPoint>> returnMap;
17 AllocFactory allocFactory;
18 LinkedList<Delta> toprocess;
19 TempDescriptor returntmp;
21 public Pointer(State state, TypeUtil typeUtil) {
23 this.blockMap=new HashMap<FlatMethod, BasicBlock>();
24 this.bbgraphMap=new HashMap<BBlock, Graph>();
25 this.graphMap=new HashMap<FlatNode, Graph>();
26 this.callMap=new HashMap<FlatCall, Set<BBlock>>();
27 this.returnMap=new HashMap<BBlock, Set<PPoint>>();
28 this.typeUtil=typeUtil;
29 this.allocFactory=new AllocFactory(state, typeUtil);
30 this.toprocess=new LinkedList<Delta>();
31 ClassDescriptor stringcd=typeUtil.getClass(TypeUtil.ObjectClass);
32 this.returntmp=new TempDescriptor("RETURNVAL", stringcd);
35 public BasicBlock getBBlock(FlatMethod fm) {
36 if (!blockMap.containsKey(fm))
37 blockMap.put(fm, BasicBlock.getBBlock(fm));
38 return blockMap.get(fm);
41 Delta buildInitialContext() {
42 MethodDescriptor md=typeUtil.getMain();
43 FlatMethod fm=state.getMethodFlat(md);
44 BasicBlock bb=getBBlock(fm);
45 BBlock start=bb.getStart();
46 Delta delta=new Delta(new PPoint(start), true);
47 MySet<Edge> arrayset=new MySet<Edge>();
48 MySet<Edge> varset=new MySet<Edge>();
49 Edge arrayedge=new Edge(allocFactory.StringArray, null, allocFactory.Strings);
50 arrayset.add(arrayedge);
51 Edge stringedge=new Edge(fm.getParameter(0), allocFactory.StringArray);
52 varset.add(stringedge);
53 delta.heapedgeadd.put(allocFactory.StringArray, arrayset);
54 delta.varedgeadd.put(fm.getParameter(0), varset);
58 public void doAnalysis() {
59 toprocess.add(buildInitialContext());
61 while(!toprocess.isEmpty()) {
62 Delta delta=toprocess.remove();
63 PPoint ppoint=delta.getBlock();
64 BBlock bblock=ppoint.getBBlock();
65 Vector<FlatNode> nodes=bblock.nodes();
67 //Build base graph for entrance to this basic block
68 delta=applyInitDelta(delta, bblock);
69 Graph graph=bbgraphMap.get(bblock);
72 //Compute delta at exit of each node
73 for(int i=0; i<nodes.size();i++) {
74 FlatNode currNode=nodes.get(i);
75 if (!graphMap.containsKey(currNode)) {
76 graphMap.put(currNode, new Graph(graph));
78 nodeGraph=graphMap.get(currNode);
79 delta=processNode(bblock, i, currNode, delta, nodeGraph);
81 generateFinalDelta(bblock, delta, nodeGraph);
85 void buildInitDelta(Graph graph, Delta newDelta) {
86 //First compute the set of temps
87 HashSet<TempDescriptor> tmpSet=new HashSet<TempDescriptor>();
88 tmpSet.addAll(graph.varMap.keySet());
89 tmpSet.addAll(graph.parent.varMap.keySet());
91 //Next build the temp map part of the delta
92 for(TempDescriptor tmp:tmpSet) {
93 MySet<Edge> edgeSet=new MySet<Edge>();
95 if (graph.varMap.containsKey(tmp))
96 edgeSet.addAll(graph.varMap.get(tmp));
98 edgeSet.addAll(graph.parent.varMap.get(tmp));
99 newDelta.varedgeadd.put(tmp, edgeSet);
102 //Next compute the set of src allocnodes
103 HashSet<AllocNode> nodeSet=new HashSet<AllocNode>();
104 nodeSet.addAll(graph.nodeMap.keySet());
105 nodeSet.addAll(graph.parent.nodeMap.keySet());
107 for(AllocNode node:nodeSet) {
108 MySet<Edge> edgeSet=new MySet<Edge>();
110 if (graph.nodeMap.containsKey(node))
111 edgeSet.addAll(graph.nodeMap.get(node));
113 edgeSet.addAll(graph.parent.nodeMap.get(node));
114 newDelta.heapedgeadd.put(node, edgeSet);
117 if (graph.nodeAges.contains(node))
118 newDelta.addNodeAges.add(node);
119 else if (graph.parent.nodeAges.contains(node))
120 newDelta.addNodeAges.add(node);
124 void generateFinalDelta(BBlock bblock, Delta delta, Graph graph) {
125 Delta newDelta=new Delta(null, false);
126 if (delta.getInit()) {
127 buildInitDelta(graph, newDelta);
129 /* We can break the old delta...it is done being used */
130 /* First we will build variable edges */
131 HashSet<TempDescriptor> tmpSet=new HashSet<TempDescriptor>();
132 tmpSet.addAll(delta.basevaredge.keySet());
133 tmpSet.addAll(delta.varedgeadd.keySet());
134 for(TempDescriptor tmp:tmpSet) {
135 /* Start with the new incoming edges */
136 MySet<Edge> newbaseedge=delta.basevaredge.get(tmp);
137 /* Remove the remove set */
138 newbaseedge.removeAll(delta.varedgeremove.get(tmp));
139 /* Add in the new set*/
140 newbaseedge.addAll(delta.varedgeadd.get(tmp));
141 /* Store the results */
142 newDelta.varedgeadd.put(tmp, newbaseedge);
145 /* Next we build heap edges */
146 HashSet<AllocNode> nodeSet=new HashSet<AllocNode>();
147 nodeSet.addAll(delta.baseheapedge.keySet());
148 nodeSet.addAll(delta.heapedgeadd.keySet());
149 nodeSet.addAll(delta.heapedgeremove.keySet());
150 for(AllocNode node:nodeSet) {
151 /* Start with the new incoming edges */
152 MySet<Edge> newheapedge=(MySet<Edge>) delta.baseheapedge.get(node).clone();
153 /* Remove the remove set */
154 newheapedge.removeAll(delta.heapedgeremove.get(node));
155 /* Add in the add set */
156 newheapedge.addAll(delta.heapedgeadd.get(node));
157 newDelta.heapedgeadd.put(node, newheapedge);
159 /* Also need to subtract off some edges */
160 MySet<Edge> removeset=delta.heapedgeremove.get(node);
162 /* Remove the newly created edges..no need to propagate a diff for those */
163 removeset.removeAll(delta.baseheapedge.get(node));
164 newDelta.heapedgeremove.put(node, removeset);
167 /* Compute new ages */
168 newDelta.addNodeAges.addAll(delta.baseNodeAges);
169 newDelta.addNodeAges.addAll(delta.addNodeAges);
172 /* Now we need to propagate newdelta */
173 if (!newDelta.heapedgeadd.isEmpty()||!newDelta.heapedgeremove.isEmpty()||!newDelta.varedgeadd.isEmpty()||!newDelta.addNodeAges.isEmpty()) {
174 /* We have a delta to propagate */
175 Vector<BBlock> blockvector=bblock.next();
176 for(int i=0;i<blockvector.size();i++) {
178 newDelta.setBlock(new PPoint(blockvector.get(i)));
179 toprocess.add(newDelta);
181 toprocess.add(newDelta.diffBlock(new PPoint(blockvector.get(i))));
187 Delta processNode(BBlock bblock, int index, FlatNode node, Delta delta, Graph newgraph) {
188 switch(node.kind()) {
190 return processNewNode((FlatNew)node, delta, newgraph);
191 case FKind.FlatFieldNode:
192 case FKind.FlatElementNode:
193 return processFieldElementNode(node, delta, newgraph);
194 case FKind.FlatCastNode:
195 case FKind.FlatOpNode:
196 case FKind.FlatReturnNode:
197 return processCopyNode(node, delta, newgraph);
198 case FKind.FlatSetFieldNode:
199 case FKind.FlatSetElementNode:
200 return processSetFieldElementNode(node, delta, newgraph);
201 case FKind.FlatMethod:
203 return processFlatNop(node, delta, newgraph);
205 return processFlatCall(bblock, index, (FlatCall) node, delta, newgraph);
206 case FKind.FlatSESEEnterNode:
207 case FKind.FlatSESEExitNode:
208 throw new Error("Unimplemented node:"+node);
210 throw new Error("Unrecognized node:"+node);
214 Delta processFlatCall(BBlock callblock, int callindex, FlatCall fcall, Delta delta, Graph graph) {
215 Delta newDelta=new Delta(null, false);
217 if (delta.getInit()) {
218 MySet<Edge> edgeset=new MySet<Edge>();
219 HashSet<AllocNode> nodeset=new HashSet<AllocNode>();
220 HashSet<ClassDescriptor> targetSet=new HashSet<ClassDescriptor>();
221 Stack<AllocNode> tovisit=new Stack<AllocNode>();
222 TempDescriptor tmpthis=fcall.getThis();
224 //Handle the this temp
226 MySet<Edge> edges=GraphManip.getEdges(graph, delta, tmpthis);
227 newDelta.varedgeadd.put(tmpthis, (MySet<Edge>) edges.clone());
228 edgeset.addAll(edges);
230 AllocNode dstnode=e.dst;
231 if (!nodeset.contains(dstnode)) {
232 TypeDescriptor type=dstnode.getType();
233 if (!type.isArray()) {
234 targetSet.add(type.getClassDesc());
236 //arrays don't have code
237 targetSet.add(typeUtil.getClass(TypeUtil.ObjectClass));
239 nodeset.add(dstnode);
240 tovisit.add(dstnode);
245 //Go through each temp
246 for(int i=0;i<fcall.numArgs();i++) {
247 TempDescriptor tmp=fcall.getArg(i);
248 MySet<Edge> edges=GraphManip.getEdges(graph, delta, tmp);
249 newDelta.varedgeadd.put(tmp, (MySet<Edge>) edges.clone());
250 edgeset.addAll(edges);
252 if (!nodeset.contains(e.dst)) {
259 //Traverse all reachable nodes
260 while(!tovisit.isEmpty()) {
261 AllocNode node=tovisit.pop();
262 MySet<Edge> edges=GraphManip.getEdges(graph, delta, node);
263 newDelta.heapedgeadd.put(node, edges);
264 edgeset.addAll(edges);
266 if (!nodeset.contains(e.dst)) {
273 //Compute call targets
274 MethodDescriptor md=fcall.getMethod();
275 HashSet<MethodDescriptor> targets=new HashSet<MethodDescriptor>();
280 for(Edge e:newDelta.varedgeadd.get(tmpthis)) {
281 AllocNode node=e.dst;
282 ClassDescriptor cd=node.getType().getClassDesc();
283 //Figure out exact method called and add to set
284 targets.add(cd.getCalledMethod(md));
290 for(MethodDescriptor calledmd:targets) {
291 FlatMethod fm=state.getMethodFlat(calledmd);
294 HashMap<TempDescriptor, TempDescriptor> tmpMap=new HashMap<TempDescriptor, TempDescriptor>();
297 tmpMap.put(tmpthis, fm.getParameter(offset++));
299 for(int i=0;i<fcall.numArgs();i++) {
300 TempDescriptor tmp=fcall.getArg(i);
301 tmpMap.put(tmp,fm.getParameter(i+offset));
304 //Get basicblock for the method
305 BasicBlock block=getBBlock(fm);
307 //Build and enqueue delta
308 Delta d=newDelta.changeParams(tmpMap, new PPoint(block.getStart()));
312 if (!callMap.containsKey(fcall)) {
313 callMap.put(fcall, new HashSet<BBlock>());
315 callMap.get(fcall).add(block.getStart());
317 //If we have an existing exit, build delta
318 Delta returnDelta=null;
321 if (!returnMap.containsKey(block.getExit())) {
322 returnMap.put(block.getExit(), new HashSet<PPoint>());
324 returnMap.get(block.getExit()).add(new PPoint(callblock, callindex));
326 if (bbgraphMap.containsKey(block.getExit())) {
327 //Need to push existing results to current node
328 if (returnDelta==null) {
329 returnDelta=new Delta(null, false);
330 buildInitDelta(bbgraphMap.get(block.getExit()), returnDelta);
331 if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
332 returnDelta.setBlock(new PPoint(callblock, callindex));
333 toprocess.add(returnDelta);
336 if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
337 toprocess.add(returnDelta.diffBlock(new PPoint(callblock, callindex)));
342 graph.reachNode=nodeset;
343 graph.reachEdge=edgeset;
345 //Apply diffs to graph
346 applyDiffs(graph, delta);
348 MySet<Edge> edgeset=new MySet<Edge>();
349 HashSet<AllocNode> nodeset=new HashSet<AllocNode>();
350 MySet<Edge> oldedgeset=graph.reachEdge;
351 HashSet<AllocNode> oldnodeset=graph.reachNode;
353 HashSet<ClassDescriptor> targetSet=new HashSet<ClassDescriptor>();
354 Stack<AllocNode> tovisit=new Stack<AllocNode>();
355 TempDescriptor tmpthis=fcall.getThis();
357 //Handle the this temp
359 MySet<Edge> edges=GraphManip.getDiffEdges(delta, tmpthis);
360 newDelta.varedgeadd.put(tmpthis, (MySet<Edge>) edges.clone());
361 edgeset.addAll(edges);
363 AllocNode dstnode=e.dst;
364 if (!nodeset.contains(dstnode)&&!oldnodeset.contains(dstnode)) {
365 TypeDescriptor type=dstnode.getType();
366 if (!type.isArray()) {
367 targetSet.add(type.getClassDesc());
369 //arrays don't have code
370 targetSet.add(typeUtil.getClass(TypeUtil.ObjectClass));
372 nodeset.add(dstnode);
373 tovisit.add(dstnode);
378 //Go through each temp
379 for(int i=0;i<fcall.numArgs();i++) {
380 TempDescriptor tmp=fcall.getArg(i);
381 MySet<Edge> edges=GraphManip.getDiffEdges(delta, tmp);
382 newDelta.varedgeadd.put(tmp, (MySet<Edge>) edges.clone());
383 edgeset.addAll(edges);
385 if (!nodeset.contains(e.dst)&&!oldnodeset.contains(e.dst)) {
392 //Go through each new heap edge that starts from old node
393 MySet<Edge> newedges=GraphManip.getDiffEdges(delta, oldnodeset);
394 edgeset.addAll(newedges);
395 for(Edge e:newedges) {
396 if (!nodeset.contains(e.dst)&&!oldnodeset.contains(e.dst)) {
402 //Traverse all reachable nodes
403 while(!tovisit.isEmpty()) {
404 AllocNode node=tovisit.pop();
405 MySet<Edge> edges=GraphManip.getEdges(graph, delta, node);
407 newDelta.heapedgeadd.put(node, edges);
408 edgeset.addAll(edges);
410 if (!nodeset.contains(e.dst)&&!oldnodeset.contains(e.dst)) {
417 //Compute call targets
418 MethodDescriptor md=fcall.getMethod();
419 HashSet<MethodDescriptor> targets=new HashSet<MethodDescriptor>();
424 for(Edge e:newDelta.varedgeadd.get(tmpthis)) {
425 AllocNode node=e.dst;
426 ClassDescriptor cd=node.getType().getClassDesc();
427 //Figure out exact method called and add to set
428 targets.add(cd.getCalledMethod(md));
432 //add in new nodeset and edgeset
433 oldnodeset.addAll(nodeset);
434 oldedgeset.addAll(edgeset);
435 Delta basedelta=null;
438 for(MethodDescriptor calledmd:targets) {
439 FlatMethod fm=state.getMethodFlat(calledmd);
440 boolean newmethod=false;
442 //Get basicblock for the method
443 BasicBlock block=getBBlock(fm);
446 if (!callMap.containsKey(fcall)) {
447 callMap.put(fcall, new HashSet<BBlock>());
450 Delta returnDelta=null;
452 if (!callMap.get(fcall).contains(block.getStart())) {
453 callMap.get(fcall).add(block.getStart());
457 if (!returnMap.containsKey(block.getExit())) {
458 returnMap.put(block.getExit(), new HashSet<PPoint>());
460 returnMap.get(block.getExit()).add(new PPoint(callblock, callindex));
462 if (bbgraphMap.containsKey(block.getExit())) {
463 //Need to push existing results to current node
464 if (returnDelta==null) {
465 returnDelta=new Delta(null, false);
466 buildInitDelta(bbgraphMap.get(block.getExit()), returnDelta);
467 if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
468 returnDelta.setBlock(new PPoint(callblock, callindex));
469 toprocess.add(returnDelta);
472 if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
473 toprocess.add(returnDelta.diffBlock(new PPoint(callblock, callindex)));
480 HashMap<TempDescriptor, TempDescriptor> tmpMap=new HashMap<TempDescriptor, TempDescriptor>();
483 tmpMap.put(tmpthis, fm.getParameter(offset++));
485 for(int i=0;i<fcall.numArgs();i++) {
486 TempDescriptor tmp=fcall.getArg(i);
487 tmpMap.put(tmp,fm.getParameter(i+offset));
491 if (basedelta==null) {
492 basedelta=newDelta.buildBase(oldedgeset);
494 //Build and enqueue delta
495 Delta d=basedelta.changeParams(tmpMap, new PPoint(block.getStart()));
498 //Build and enqueue delta
499 Delta d=newDelta.changeParams(tmpMap, new PPoint(block.getStart()));
504 //Apply diffs to graph
505 applyDiffs(graph, delta);
510 void applyDiffs(Graph graph, Delta delta) {
511 //Add hidden base edges
512 for(Map.Entry<AllocNode, MySet<Edge>> e: delta.baseheapedge.entrySet()) {
513 AllocNode node=e.getKey();
514 MySet<Edge> edges=e.getValue();
515 if (graph.nodeMap.containsKey(node)) {
516 MySet<Edge> nodeEdges=graph.nodeMap.get(node);
517 nodeEdges.addAll(edges);
522 for(Map.Entry<AllocNode, MySet<Edge>> e: delta.heapedgeremove.entrySet()) {
523 AllocNode node=e.getKey();
524 MySet<Edge> edgestoremove=e.getValue();
525 if (graph.nodeMap.containsKey(node)) {
526 //Just apply diff to current map
527 graph.nodeMap.get(node).removeAll(edgestoremove);
529 //Generate diff from parent graph
530 MySet<Edge> parentedges=graph.parent.nodeMap.get(node);
531 MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
532 graph.nodeMap.put(node, newedgeset);
537 for(Map.Entry<AllocNode, MySet<Edge>> e: delta.heapedgeadd.entrySet()) {
538 AllocNode node=e.getKey();
539 MySet<Edge> edgestoadd=e.getValue();
540 //If we have not done a subtract, then
541 if (!graph.nodeMap.containsKey(node)) {
542 //Copy the parent entry
543 graph.nodeMap.put(node, (MySet<Edge>)graph.parent.nodeMap.get(node).clone());
545 graph.nodeMap.get(node).addAll(edgestoadd);
549 for(Map.Entry<TempDescriptor, MySet<Edge>> e: delta.varedgeremove.entrySet()) {
550 TempDescriptor tmp=e.getKey();
551 MySet<Edge> edgestoremove=e.getValue();
553 if (graph.varMap.containsKey(tmp)) {
554 //Just apply diff to current map
555 graph.varMap.get(tmp).removeAll(edgestoremove);
557 //Generate diff from parent graph
558 MySet<Edge> parentedges=graph.parent.varMap.get(tmp);
559 MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
560 graph.varMap.put(tmp, newedgeset);
565 for(Map.Entry<TempDescriptor, MySet<Edge>> e: delta.varedgeadd.entrySet()) {
566 TempDescriptor tmp=e.getKey();
567 MySet<Edge> edgestoadd=e.getValue();
568 graph.varMap.put(tmp, (MySet<Edge>) edgestoadd.clone());
572 for(AllocNode node:delta.addNodeAges) {
573 graph.nodeAges.add(node);
577 Delta processSetFieldElementNode(FlatNode node, Delta delta, Graph graph) {
581 if (node.kind()==FKind.FlatSetElementNode) {
582 FlatSetElementNode fen=(FlatSetElementNode) node;
587 FlatSetFieldNode ffn=(FlatSetFieldNode) node;
592 if (delta.getInit()) {
593 HashSet<AllocNode> srcNodes=GraphManip.getNodes(graph, delta, src);
594 HashSet<AllocNode> dstNodes=GraphManip.getNodes(graph, delta, dst);
595 MySet<Edge> edgesToAdd=GraphManip.genEdges(srcNodes, fd, dstNodes);
596 MySet<Edge> edgesToRemove=null;
597 if (dstNodes.size()==1&&!dstNodes.iterator().next().isSummary()) {
598 /* Can do a strong update */
599 edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
602 updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
603 applyDiffs(graph, delta);
605 /* First look at new sources */
606 MySet<Edge> edgesToAdd=new MySet<Edge>();
607 HashSet<AllocNode> newSrcNodes=GraphManip.getDiffNodes(delta, src);
608 HashSet<AllocNode> dstNodes=GraphManip.getDiffNodes(delta, dst);
609 edgesToAdd.addAll(GraphManip.genEdges(newSrcNodes, fd, dstNodes));
610 HashSet<AllocNode> newDstNodes=GraphManip.getDiffNodes(delta, dst);
611 MySet<Edge> edgesToRemove=null;
612 if (newDstNodes.size()!=0) {
613 if (dstNodes.size()==1&&!dstNodes.iterator().next().isSummary()) {
614 /* Need to undo strong update */
615 if (graph.strongUpdateSet!=null) {
616 edgesToAdd.addAll(graph.strongUpdateSet);
617 graph.strongUpdateSet.clear();
619 } else if (dstNodes.size()==0&&newDstNodes.size()==1&&!newDstNodes.iterator().next().isSummary()&&graph.strongUpdateSet==null) {
620 edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
622 HashSet<AllocNode> srcNodes=GraphManip.getDiffNodes(delta, src);
623 edgesToAdd.addAll(GraphManip.genEdges(srcNodes, fd, newDstNodes));
626 updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
627 applyDiffs(graph, delta);
632 Delta processCopyNode(FlatNode node, Delta delta, Graph graph) {
635 if (node.kind()==FKind.FlatOpNode) {
636 FlatOpNode fon=(FlatOpNode) node;
639 } else if (node.kind()==FKind.FlatReturnNode) {
640 FlatReturnNode frn=(FlatReturnNode)node;
641 src=frn.getReturnTemp();
644 FlatCastNode fcn=(FlatCastNode) node;
648 if (delta.getInit()) {
649 HashSet<AllocNode> srcnodes=GraphManip.getNodes(graph, delta, src);
650 MySet<Edge> edgesToAdd=GraphManip.genEdges(src, srcnodes);
651 MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
652 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
653 applyDiffs(graph, delta);
655 /* First compute new src nodes */
656 HashSet<AllocNode> newSrcNodes=GraphManip.getDiffNodes(delta, src);
658 /* Compute the union, and then the set of edges */
659 MySet<Edge> edgesToAdd=GraphManip.genEdges(src, newSrcNodes);
661 /* Compute set of edges to remove */
662 MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);
665 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
666 applyDiffs(graph, delta);
671 Delta processFieldElementNode(FlatNode node, Delta delta, Graph graph) {
675 if (node.kind()==FKind.FlatElementNode) {
676 FlatElementNode fen=(FlatElementNode) node;
681 FlatFieldNode ffn=(FlatFieldNode) node;
686 if (delta.getInit()) {
687 HashSet<AllocNode> srcnodes=GraphManip.getNodes(graph, delta, src);
688 HashSet<AllocNode> fdnodes=GraphManip.getNodes(graph, delta, srcnodes, fd);
689 MySet<Edge> edgesToAdd=GraphManip.genEdges(src, fdnodes);
690 MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
691 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
692 applyDiffs(graph, delta);
694 /* First compute new objects we read fields of */
695 HashSet<AllocNode> allsrcnodes=GraphManip.getNodes(graph, delta, src);
696 HashSet<AllocNode> difffdnodes=GraphManip.getDiffNodes(delta, allsrcnodes, fd);
697 /* Next compute new targets of fields */
698 HashSet<AllocNode> newsrcnodes=GraphManip.getDiffNodes(delta, src);
699 HashSet<AllocNode> newfdnodes=GraphManip.getNodes(graph, delta, newsrcnodes, fd);
700 /* Compute the union, and then the set of edges */
701 HashSet<AllocNode> newTargets=new HashSet<AllocNode>();
702 newTargets.addAll(newfdnodes);
703 newTargets.addAll(difffdnodes);
704 MySet<Edge> edgesToAdd=GraphManip.genEdges(src, newTargets);
706 /* Compute set of edges to remove */
707 MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);
710 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
711 applyDiffs(graph, delta);
716 static void updateVarDelta(Graph graph, Delta delta, TempDescriptor tmp, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
717 MySet<Edge> edgeAdd=delta.varedgeadd.get(tmp);
718 MySet<Edge> edgeRemove=delta.varedgeremove.get(tmp);
719 MySet<Edge> existingEdges=graph.getEdges(tmp);
720 for(Edge e: edgestoRemove) {
721 //remove edge from delta
723 //if the edge is already in the graph, add an explicit remove to the delta
724 if (existingEdges.contains(e))
727 for(Edge e: edgestoAdd) {
728 //Remove the edge from the remove set
729 edgeRemove.remove(e);
730 //Explicitly add it to the add set unless it is already in the graph
731 if (!existingEdges.contains(e))
736 static void updateHeapDelta(Graph graph, Delta delta, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
737 for(Edge e: edgestoRemove) {
739 MySet<Edge> edgeAdd=delta.heapedgeadd.get(src);
740 MySet<Edge> existingEdges=graph.getEdges(src);
741 //remove edge from delta
743 //if the edge is already in the graph, add an explicit remove to the delta
744 if (existingEdges.contains(e)) {
745 MySet<Edge> edgeRemove=delta.heapedgeremove.get(src);
749 for(Edge e: edgestoAdd) {
751 MySet<Edge> edgeRemove=delta.heapedgeremove.get(src);
752 MySet<Edge> existingEdges=graph.getEdges(src);
753 //Remove the edge from the remove set
754 edgeRemove.remove(e);
755 //Explicitly add it to the add set unless it is already in the graph
756 if (!existingEdges.contains(e)) {
757 MySet<Edge> edgeAdd=delta.heapedgeadd.get(src);
763 Delta processFlatNop(FlatNode node, Delta delta, Graph graph) {
764 applyDiffs(graph, delta);
768 Delta processNewNode(FlatNew node, Delta delta, Graph graph) {
769 AllocNode summary=allocFactory.getAllocNode(node, true);
770 AllocNode single=allocFactory.getAllocNode(node, false);
771 TempDescriptor tmp=node.getDst();
773 if (delta.getInit()) {
774 /* We don't have to deal with summarization here... The
775 * intuition is that this is the only place where we generate
776 * nodes for this allocation site and this is the first time
777 * we've analyzed this site */
780 Edge e=new Edge(tmp, single);
782 MySet<Edge> newedges=new MySet<Edge>();
784 //Add it into the diffs
785 delta.varedgeadd.put(tmp, newedges);
786 //Remove the old edges
787 delta.varedgeremove.put(tmp, graph.getEdges(tmp));
788 //Apply incoming diffs to graph
789 applyDiffs(graph, delta);
790 //Note that we create a single node
791 delta.addNodeAges.add(single);
793 /* 1. Fix up the variable edge additions */
795 for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.varedgeadd.entrySet().iterator();entryIt.hasNext();) {
796 Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
798 if (entry.getKey()==tmp) {
799 /* Check if this is the tmp we overwrite */
802 /* Otherwise, check if the target of the edge is changed... */
803 summarizeSet(entry.getValue(), graph.varMap.get(entry.getKey()), single, summary);
807 /* 2. Fix up the base variable edges */
809 for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.basevaredge.entrySet().iterator();entryIt.hasNext();) {
810 Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
811 TempDescriptor entrytmp=entry.getKey();
813 /* Check is this is the tmp we overwrite, if so add to remove set */
814 Util.relationUpdate(delta.varedgeremove, tmp, null, entry.getValue());
816 /* Check if the target of the edge is changed */
817 MySet<Edge> newset=(MySet<Edge>)entry.getValue().clone();
818 MySet<Edge> removeset=shrinkSet(newset, graph.varMap.get(entrytmp), single, summary);
819 Util.relationUpdate(delta.varedgeremove, entrytmp, newset, removeset);
820 Util.relationUpdate(delta.varedgeadd, entrytmp, null, newset);
825 /* 3. Fix up heap edge additions */
827 HashMap<AllocNode, MySet<Edge>> addheapedge=new HashMap<AllocNode, MySet<Edge>>();
828 for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.heapedgeadd.entrySet().iterator();entryIt.hasNext();) {
829 Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
830 MySet<Edge> edgeset=entry.getValue();
831 AllocNode allocnode=entry.getKey();
832 if (allocnode==single) {
834 summarizeSet(edgeset, graph.nodeMap.get(summary), single, summary);
835 addheapedge.put(summary, edgeset);
837 summarizeSet(edgeset, graph.nodeMap.get(allocnode), single, summary);
843 for(Map.Entry<AllocNode, MySet<Edge>> entry:addheapedge.entrySet()) {
844 AllocNode allocnode=entry.getKey();
845 Util.relationUpdate(delta.heapedgeadd, allocnode, null, entry.getValue());
848 /* 4. Fix up the base heap edges */
850 for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.baseheapedge.entrySet().iterator();entryIt.hasNext();) {
851 Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
852 MySet<Edge> edgeset=entry.getValue();
853 AllocNode allocnode=entry.getKey();
854 if (allocnode==single) {
857 AllocNode addnode=(allocnode==single)?summary:allocnode;
859 MySet<Edge> newset=(MySet<Edge>) edgeset.clone();
860 MySet<Edge> removeset=shrinkSet(newset, graph.nodeMap.get(addnode), single, summary);
861 Util.relationUpdate(delta.heapedgeadd, addnode, null, newset);
862 Util.relationUpdate(delta.heapedgeremove, allocnode, null, removeset);
865 /* Update Node Ages...If the base or addNodeAges set contains a
866 * single node, it now should also contain a summary node... No
867 * need to generate a single node as that has already been
869 if (delta.baseNodeAges.contains(single)||delta.addNodeAges.contains(single)) {
870 delta.addNodeAges.add(summary);
873 //Apply incoming diffs to graph
874 applyDiffs(graph, delta);
879 /* This function builds a new edge set where oldnode is summarized into new node */
881 void summarizeSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode sumnode) {
882 MySet<Edge> newSet=null;
883 for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
884 Edge e=edgeit.next();
885 if (e.dst==oldnode||e.src==oldnode) {
887 newSet=new MySet<Edge>();
892 if (e.dst==oldnode) {
895 if (e.src==oldnode) {
898 if (oldedgeset==null||!oldedgeset.contains(e))
903 edgeset.addAll(newSet);
906 /* Shrinks the incoming set to just include rewritten values.
907 * Returns a set of the original rewritten values */
909 MySet<Edge> shrinkSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode newnode) {
910 MySet<Edge> newSet=null;
911 MySet<Edge> removeSet=null;
912 for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
913 Edge e=edgeit.next();
915 if (e.dst==oldnode||e.src==oldnode) {
917 newSet=new MySet<Edge>();
918 removeSet=new MySet<Edge>();
921 removeSet.add(e.copy());
926 if (oldedgeset==null||!oldedgeset.contains(e))
931 edgeset.addAll(newSet);
935 /* This function returns a completely new Delta... It is safe to
938 Delta applyInitDelta(Delta delta, BBlock block) {
939 //Apply delta to graph
940 boolean newGraph=false;
941 if (!bbgraphMap.containsKey(block)) {
942 bbgraphMap.put(block, new Graph(null));
946 Graph graph=bbgraphMap.get(block);
949 newdelta=new Delta(null, true);
950 //Add in heap edges and throw away original diff
951 graph.nodeMap.putAll(delta.heapedgeadd);
952 //Add in var edges and throw away original diff
953 graph.varMap.putAll(delta.varedgeadd);
954 //Record that this is initial set...
955 graph.nodeAges.addAll(delta.addNodeAges);
957 newdelta=new Delta(null, false);
958 //merge in heap edges and variables
959 mergeHeapEdges(graph, delta, newdelta);
960 mergeVarEdges(graph, delta, newdelta);
961 mergeAges(graph, delta, newdelta);
966 /* This function merges in the heap edges. It updates delta to be
969 void mergeHeapEdges(Graph graph, Delta delta, Delta newdelta) {
971 for(Map.Entry<AllocNode, MySet<Edge>> heapedge:delta.heapedgeadd.entrySet()) {
972 AllocNode nsrc=heapedge.getKey();
973 MySet<Edge> edges=heapedge.getValue();
974 if (!graph.nodeMap.containsKey(nsrc)) {
975 graph.nodeMap.put(nsrc, new MySet<Edge>());
977 MySet<Edge> dstedges=graph.nodeMap.get(nsrc);
978 MySet<Edge> diffedges=new MySet<Edge>();
980 if (!dstedges.contains(e)) {
986 //Done with edge set...
987 if (diffedges.size()>0) {
989 newdelta.baseheapedge.put(nsrc, diffedges);
994 /* This function merges in the var edges. It updates delta to be
997 void mergeVarEdges(Graph graph, Delta delta, Delta newdelta) {
999 for(Map.Entry<TempDescriptor, MySet<Edge>> varedge:delta.varedgeadd.entrySet()) {
1000 TempDescriptor tmpsrc=varedge.getKey();
1001 MySet<Edge> edges=varedge.getValue();
1002 if (!graph.varMap.containsKey(tmpsrc)) {
1003 graph.varMap.put(tmpsrc, new MySet<Edge>());
1005 MySet<Edge> dstedges=graph.varMap.get(tmpsrc);
1006 MySet<Edge> diffedges=new MySet<Edge>();
1008 if (!dstedges.contains(e)) {
1009 //We have a new edge
1014 //Done with edge set...
1015 if (diffedges.size()>=0) {
1017 newdelta.basevaredge.put(tmpsrc,diffedges);
1022 void mergeAges(Graph graph, Delta delta, Delta newDelta) {
1024 for(AllocNode node:delta.addNodeAges) {
1025 if (!graph.nodeAges.contains(node)) {
1026 graph.nodeAges.add(node);
1027 newDelta.baseNodeAges.add(node);