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);
215 void processThisTargets(HashSet<ClassDescriptor> targetSet, Graph graph, Delta delta, Delta newDelta, HashSet<AllocNode> nodeset, Stack<AllocNode> tovisit, MySet<Edge> edgeset, TempDescriptor tmpthis, HashSet<AllocNode> oldnodeset) {
216 //Handle the this temp
218 MySet<Edge> edges=(oldnodeset!=null)?GraphManip.getDiffEdges(delta, tmpthis):GraphManip.getEdges(graph, delta, tmpthis);
219 newDelta.varedgeadd.put(tmpthis, (MySet<Edge>) edges.clone());
220 edgeset.addAll(edges);
222 AllocNode dstnode=e.dst;
223 if (!nodeset.contains(dstnode)&&(oldnodeset==null||!oldnodeset.contains(dstnode))) {
224 TypeDescriptor type=dstnode.getType();
225 if (!type.isArray()) {
226 targetSet.add(type.getClassDesc());
228 //arrays don't have code
229 targetSet.add(typeUtil.getClass(TypeUtil.ObjectClass));
231 nodeset.add(dstnode);
232 tovisit.add(dstnode);
238 void processParams(Graph graph, Delta delta, Delta newDelta, HashSet<AllocNode> nodeset, Stack<AllocNode> tovisit, MySet<Edge> edgeset, FlatCall fcall, boolean diff) {
239 //Go through each temp
240 for(int i=0;i<fcall.numArgs();i++) {
241 TempDescriptor tmp=fcall.getArg(i);
242 MySet<Edge> edges=diff?GraphManip.getDiffEdges(delta, tmp):GraphManip.getEdges(graph, delta, tmp);
243 newDelta.varedgeadd.put(tmp, (MySet<Edge>) edges.clone());
244 edgeset.addAll(edges);
246 if (!nodeset.contains(e.dst)) {
254 void computeReachableNodes(Graph graph, Delta delta, Delta newDelta, HashSet<AllocNode> nodeset, Stack<AllocNode> tovisit, MySet<Edge> edgeset, HashSet<AllocNode> oldnodeset) {
255 while(!tovisit.isEmpty()) {
256 AllocNode node=tovisit.pop();
257 MySet<Edge> edges=GraphManip.getEdges(graph, delta, node);
258 newDelta.heapedgeadd.put(node, edges);
259 edgeset.addAll(edges);
261 if (!nodeset.contains(e.dst)&&(oldnodeset==null||!oldnodeset.contains(e.dst))) {
269 HashSet<MethodDescriptor> computeTargets(FlatCall fcall, Delta newDelta) {
270 TempDescriptor tmpthis=fcall.getThis();
271 MethodDescriptor md=fcall.getMethod();
272 HashSet<MethodDescriptor> targets=new HashSet<MethodDescriptor>();
277 for(Edge e:newDelta.varedgeadd.get(tmpthis)) {
278 AllocNode node=e.dst;
279 ClassDescriptor cd=node.getType().getClassDesc();
280 //Figure out exact method called and add to set
281 targets.add(cd.getCalledMethod(md));
287 Delta processFlatCall(BBlock callblock, int callindex, FlatCall fcall, Delta delta, Graph graph) {
288 Delta newDelta=new Delta(null, false);
290 if (delta.getInit()) {
291 MySet<Edge> edgeset=new MySet<Edge>();
292 HashSet<AllocNode> nodeset=new HashSet<AllocNode>();
293 HashSet<ClassDescriptor> targetSet=new HashSet<ClassDescriptor>();
294 Stack<AllocNode> tovisit=new Stack<AllocNode>();
295 TempDescriptor tmpthis=fcall.getThis();
297 //Handle the this temp
298 processThisTargets(targetSet, graph, delta, newDelta, nodeset, tovisit, edgeset, tmpthis, null);
300 //Go through each temp
301 processParams(graph, delta, newDelta, nodeset, tovisit, edgeset, fcall, false);
303 //Traverse all reachable nodes
304 computeReachableNodes(graph, delta, newDelta, nodeset, tovisit, edgeset, null);
306 //Compute call targets
307 HashSet<MethodDescriptor> targets=computeTargets(fcall, newDelta);
310 for(MethodDescriptor calledmd:targets) {
311 FlatMethod fm=state.getMethodFlat(calledmd);
314 HashMap<TempDescriptor, TempDescriptor> tmpMap=new HashMap<TempDescriptor, TempDescriptor>();
317 tmpMap.put(tmpthis, fm.getParameter(offset++));
319 for(int i=0;i<fcall.numArgs();i++) {
320 TempDescriptor tmp=fcall.getArg(i);
321 tmpMap.put(tmp,fm.getParameter(i+offset));
324 //Get basicblock for the method
325 BasicBlock block=getBBlock(fm);
327 //Build and enqueue delta
328 Delta d=newDelta.changeParams(tmpMap, new PPoint(block.getStart()));
332 if (!callMap.containsKey(fcall)) {
333 callMap.put(fcall, new HashSet<BBlock>());
335 callMap.get(fcall).add(block.getStart());
337 //If we have an existing exit, build delta
338 Delta returnDelta=null;
341 if (!returnMap.containsKey(block.getExit())) {
342 returnMap.put(block.getExit(), new HashSet<PPoint>());
344 returnMap.get(block.getExit()).add(new PPoint(callblock, callindex));
346 if (bbgraphMap.containsKey(block.getExit())) {
347 //Need to push existing results to current node
348 if (returnDelta==null) {
349 returnDelta=new Delta(null, false);
350 buildInitDelta(bbgraphMap.get(block.getExit()), returnDelta);
351 if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
352 returnDelta.setBlock(new PPoint(callblock, callindex));
353 toprocess.add(returnDelta);
356 if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
357 toprocess.add(returnDelta.diffBlock(new PPoint(callblock, callindex)));
362 graph.reachNode=nodeset;
363 graph.reachEdge=edgeset;
365 //Apply diffs to graph
366 applyDiffs(graph, delta);
368 MySet<Edge> edgeset=new MySet<Edge>();
369 HashSet<AllocNode> nodeset=new HashSet<AllocNode>();
370 MySet<Edge> oldedgeset=graph.reachEdge;
371 HashSet<AllocNode> oldnodeset=graph.reachNode;
373 HashSet<ClassDescriptor> targetSet=new HashSet<ClassDescriptor>();
374 Stack<AllocNode> tovisit=new Stack<AllocNode>();
375 TempDescriptor tmpthis=fcall.getThis();
377 //Handle the this temp
378 processThisTargets(targetSet, graph, delta, newDelta, nodeset, tovisit, edgeset, tmpthis, oldnodeset);
380 //Go through each temp
381 processParams(graph, delta, newDelta, nodeset, tovisit, edgeset, fcall, true);
383 //Go through each new heap edge that starts from old node
384 MySet<Edge> newedges=GraphManip.getDiffEdges(delta, oldnodeset);
385 edgeset.addAll(newedges);
386 for(Edge e:newedges) {
387 if (!nodeset.contains(e.dst)&&!oldnodeset.contains(e.dst)) {
393 //Traverse all reachable nodes
394 computeReachableNodes(graph, delta, newDelta, nodeset, tovisit, edgeset, oldnodeset);
396 //Compute call targets
397 HashSet<MethodDescriptor> targets=computeTargets(fcall, newDelta);
399 //add in new nodeset and edgeset
400 oldnodeset.addAll(nodeset);
401 oldedgeset.addAll(edgeset);
402 Delta basedelta=null;
405 for(MethodDescriptor calledmd:targets) {
406 FlatMethod fm=state.getMethodFlat(calledmd);
407 boolean newmethod=false;
409 //Get basicblock for the method
410 BasicBlock block=getBBlock(fm);
413 if (!callMap.containsKey(fcall)) {
414 callMap.put(fcall, new HashSet<BBlock>());
417 Delta returnDelta=null;
419 if (!callMap.get(fcall).contains(block.getStart())) {
420 callMap.get(fcall).add(block.getStart());
424 if (!returnMap.containsKey(block.getExit())) {
425 returnMap.put(block.getExit(), new HashSet<PPoint>());
427 returnMap.get(block.getExit()).add(new PPoint(callblock, callindex));
429 if (bbgraphMap.containsKey(block.getExit())) {
430 //Need to push existing results to current node
431 if (returnDelta==null) {
432 returnDelta=new Delta(null, false);
433 buildInitDelta(bbgraphMap.get(block.getExit()), returnDelta);
434 if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
435 returnDelta.setBlock(new PPoint(callblock, callindex));
436 toprocess.add(returnDelta);
439 if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
440 toprocess.add(returnDelta.diffBlock(new PPoint(callblock, callindex)));
447 HashMap<TempDescriptor, TempDescriptor> tmpMap=new HashMap<TempDescriptor, TempDescriptor>();
450 tmpMap.put(tmpthis, fm.getParameter(offset++));
452 for(int i=0;i<fcall.numArgs();i++) {
453 TempDescriptor tmp=fcall.getArg(i);
454 tmpMap.put(tmp,fm.getParameter(i+offset));
458 if (basedelta==null) {
459 basedelta=newDelta.buildBase(oldedgeset);
461 //Build and enqueue delta
462 Delta d=basedelta.changeParams(tmpMap, new PPoint(block.getStart()));
465 //Build and enqueue delta
466 Delta d=newDelta.changeParams(tmpMap, new PPoint(block.getStart()));
471 //Apply diffs to graph
472 applyDiffs(graph, delta);
477 void applyDiffs(Graph graph, Delta delta) {
478 //Add hidden base edges
479 for(Map.Entry<AllocNode, MySet<Edge>> e: delta.baseheapedge.entrySet()) {
480 AllocNode node=e.getKey();
481 MySet<Edge> edges=e.getValue();
482 if (graph.nodeMap.containsKey(node)) {
483 MySet<Edge> nodeEdges=graph.nodeMap.get(node);
484 nodeEdges.addAll(edges);
489 for(Map.Entry<AllocNode, MySet<Edge>> e: delta.heapedgeremove.entrySet()) {
490 AllocNode node=e.getKey();
491 MySet<Edge> edgestoremove=e.getValue();
492 if (graph.nodeMap.containsKey(node)) {
493 //Just apply diff to current map
494 graph.nodeMap.get(node).removeAll(edgestoremove);
496 //Generate diff from parent graph
497 MySet<Edge> parentedges=graph.parent.nodeMap.get(node);
498 MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
499 graph.nodeMap.put(node, newedgeset);
504 for(Map.Entry<AllocNode, MySet<Edge>> e: delta.heapedgeadd.entrySet()) {
505 AllocNode node=e.getKey();
506 MySet<Edge> edgestoadd=e.getValue();
507 //If we have not done a subtract, then
508 if (!graph.nodeMap.containsKey(node)) {
509 //Copy the parent entry
510 graph.nodeMap.put(node, (MySet<Edge>)graph.parent.nodeMap.get(node).clone());
512 graph.nodeMap.get(node).addAll(edgestoadd);
516 for(Map.Entry<TempDescriptor, MySet<Edge>> e: delta.varedgeremove.entrySet()) {
517 TempDescriptor tmp=e.getKey();
518 MySet<Edge> edgestoremove=e.getValue();
520 if (graph.varMap.containsKey(tmp)) {
521 //Just apply diff to current map
522 graph.varMap.get(tmp).removeAll(edgestoremove);
524 //Generate diff from parent graph
525 MySet<Edge> parentedges=graph.parent.varMap.get(tmp);
526 MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
527 graph.varMap.put(tmp, newedgeset);
532 for(Map.Entry<TempDescriptor, MySet<Edge>> e: delta.varedgeadd.entrySet()) {
533 TempDescriptor tmp=e.getKey();
534 MySet<Edge> edgestoadd=e.getValue();
535 graph.varMap.put(tmp, (MySet<Edge>) edgestoadd.clone());
539 for(AllocNode node:delta.addNodeAges) {
540 graph.nodeAges.add(node);
544 Delta processSetFieldElementNode(FlatNode node, Delta delta, Graph graph) {
548 if (node.kind()==FKind.FlatSetElementNode) {
549 FlatSetElementNode fen=(FlatSetElementNode) node;
554 FlatSetFieldNode ffn=(FlatSetFieldNode) node;
559 if (delta.getInit()) {
560 HashSet<AllocNode> srcNodes=GraphManip.getNodes(graph, delta, src);
561 HashSet<AllocNode> dstNodes=GraphManip.getNodes(graph, delta, dst);
562 MySet<Edge> edgesToAdd=GraphManip.genEdges(srcNodes, fd, dstNodes);
563 MySet<Edge> edgesToRemove=null;
564 if (dstNodes.size()==1&&!dstNodes.iterator().next().isSummary()) {
565 /* Can do a strong update */
566 edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
569 updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
570 applyDiffs(graph, delta);
572 /* First look at new sources */
573 MySet<Edge> edgesToAdd=new MySet<Edge>();
574 HashSet<AllocNode> newSrcNodes=GraphManip.getDiffNodes(delta, src);
575 HashSet<AllocNode> dstNodes=GraphManip.getDiffNodes(delta, dst);
576 edgesToAdd.addAll(GraphManip.genEdges(newSrcNodes, fd, dstNodes));
577 HashSet<AllocNode> newDstNodes=GraphManip.getDiffNodes(delta, dst);
578 MySet<Edge> edgesToRemove=null;
579 if (newDstNodes.size()!=0) {
580 if (dstNodes.size()==1&&!dstNodes.iterator().next().isSummary()) {
581 /* Need to undo strong update */
582 if (graph.strongUpdateSet!=null) {
583 edgesToAdd.addAll(graph.strongUpdateSet);
584 graph.strongUpdateSet.clear();
586 } else if (dstNodes.size()==0&&newDstNodes.size()==1&&!newDstNodes.iterator().next().isSummary()&&graph.strongUpdateSet==null) {
587 edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
589 HashSet<AllocNode> srcNodes=GraphManip.getDiffNodes(delta, src);
590 edgesToAdd.addAll(GraphManip.genEdges(srcNodes, fd, newDstNodes));
593 updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
594 applyDiffs(graph, delta);
599 Delta processCopyNode(FlatNode node, Delta delta, Graph graph) {
602 if (node.kind()==FKind.FlatOpNode) {
603 FlatOpNode fon=(FlatOpNode) node;
606 } else if (node.kind()==FKind.FlatReturnNode) {
607 FlatReturnNode frn=(FlatReturnNode)node;
608 src=frn.getReturnTemp();
611 FlatCastNode fcn=(FlatCastNode) node;
615 if (delta.getInit()) {
616 HashSet<AllocNode> srcnodes=GraphManip.getNodes(graph, delta, src);
617 MySet<Edge> edgesToAdd=GraphManip.genEdges(src, srcnodes);
618 MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
619 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
620 applyDiffs(graph, delta);
622 /* First compute new src nodes */
623 HashSet<AllocNode> newSrcNodes=GraphManip.getDiffNodes(delta, src);
625 /* Compute the union, and then the set of edges */
626 MySet<Edge> edgesToAdd=GraphManip.genEdges(src, newSrcNodes);
628 /* Compute set of edges to remove */
629 MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);
632 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
633 applyDiffs(graph, delta);
638 Delta processFieldElementNode(FlatNode node, Delta delta, Graph graph) {
642 if (node.kind()==FKind.FlatElementNode) {
643 FlatElementNode fen=(FlatElementNode) node;
648 FlatFieldNode ffn=(FlatFieldNode) node;
653 if (delta.getInit()) {
654 HashSet<AllocNode> srcnodes=GraphManip.getNodes(graph, delta, src);
655 HashSet<AllocNode> fdnodes=GraphManip.getNodes(graph, delta, srcnodes, fd);
656 MySet<Edge> edgesToAdd=GraphManip.genEdges(src, fdnodes);
657 MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
658 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
659 applyDiffs(graph, delta);
661 /* First compute new objects we read fields of */
662 HashSet<AllocNode> allsrcnodes=GraphManip.getNodes(graph, delta, src);
663 HashSet<AllocNode> difffdnodes=GraphManip.getDiffNodes(delta, allsrcnodes, fd);
664 /* Next compute new targets of fields */
665 HashSet<AllocNode> newsrcnodes=GraphManip.getDiffNodes(delta, src);
666 HashSet<AllocNode> newfdnodes=GraphManip.getNodes(graph, delta, newsrcnodes, fd);
667 /* Compute the union, and then the set of edges */
668 HashSet<AllocNode> newTargets=new HashSet<AllocNode>();
669 newTargets.addAll(newfdnodes);
670 newTargets.addAll(difffdnodes);
671 MySet<Edge> edgesToAdd=GraphManip.genEdges(src, newTargets);
673 /* Compute set of edges to remove */
674 MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);
677 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
678 applyDiffs(graph, delta);
683 static void updateVarDelta(Graph graph, Delta delta, TempDescriptor tmp, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
684 MySet<Edge> edgeAdd=delta.varedgeadd.get(tmp);
685 MySet<Edge> edgeRemove=delta.varedgeremove.get(tmp);
686 MySet<Edge> existingEdges=graph.getEdges(tmp);
687 for(Edge e: edgestoRemove) {
688 //remove edge from delta
690 //if the edge is already in the graph, add an explicit remove to the delta
691 if (existingEdges.contains(e))
694 for(Edge e: edgestoAdd) {
695 //Remove the edge from the remove set
696 edgeRemove.remove(e);
697 //Explicitly add it to the add set unless it is already in the graph
698 if (!existingEdges.contains(e))
703 static void updateHeapDelta(Graph graph, Delta delta, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
704 for(Edge e: edgestoRemove) {
706 MySet<Edge> edgeAdd=delta.heapedgeadd.get(src);
707 MySet<Edge> existingEdges=graph.getEdges(src);
708 //remove edge from delta
710 //if the edge is already in the graph, add an explicit remove to the delta
711 if (existingEdges.contains(e)) {
712 MySet<Edge> edgeRemove=delta.heapedgeremove.get(src);
716 for(Edge e: edgestoAdd) {
718 MySet<Edge> edgeRemove=delta.heapedgeremove.get(src);
719 MySet<Edge> existingEdges=graph.getEdges(src);
720 //Remove the edge from the remove set
721 edgeRemove.remove(e);
722 //Explicitly add it to the add set unless it is already in the graph
723 if (!existingEdges.contains(e)) {
724 MySet<Edge> edgeAdd=delta.heapedgeadd.get(src);
730 Delta processFlatNop(FlatNode node, Delta delta, Graph graph) {
731 applyDiffs(graph, delta);
735 Delta processNewNode(FlatNew node, Delta delta, Graph graph) {
736 AllocNode summary=allocFactory.getAllocNode(node, true);
737 AllocNode single=allocFactory.getAllocNode(node, false);
738 TempDescriptor tmp=node.getDst();
740 if (delta.getInit()) {
741 /* We don't have to deal with summarization here... The
742 * intuition is that this is the only place where we generate
743 * nodes for this allocation site and this is the first time
744 * we've analyzed this site */
747 Edge e=new Edge(tmp, single);
749 MySet<Edge> newedges=new MySet<Edge>();
751 //Add it into the diffs
752 delta.varedgeadd.put(tmp, newedges);
753 //Remove the old edges
754 delta.varedgeremove.put(tmp, graph.getEdges(tmp));
755 //Apply incoming diffs to graph
756 applyDiffs(graph, delta);
757 //Note that we create a single node
758 delta.addNodeAges.add(single);
760 /* 1. Fix up the variable edge additions */
762 for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.varedgeadd.entrySet().iterator();entryIt.hasNext();) {
763 Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
765 if (entry.getKey()==tmp) {
766 /* Check if this is the tmp we overwrite */
769 /* Otherwise, check if the target of the edge is changed... */
770 summarizeSet(entry.getValue(), graph.varMap.get(entry.getKey()), single, summary);
774 /* 2. Fix up the base variable edges */
776 for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.basevaredge.entrySet().iterator();entryIt.hasNext();) {
777 Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
778 TempDescriptor entrytmp=entry.getKey();
780 /* Check is this is the tmp we overwrite, if so add to remove set */
781 Util.relationUpdate(delta.varedgeremove, tmp, null, entry.getValue());
783 /* Check if the target of the edge is changed */
784 MySet<Edge> newset=(MySet<Edge>)entry.getValue().clone();
785 MySet<Edge> removeset=shrinkSet(newset, graph.varMap.get(entrytmp), single, summary);
786 Util.relationUpdate(delta.varedgeremove, entrytmp, newset, removeset);
787 Util.relationUpdate(delta.varedgeadd, entrytmp, null, newset);
792 /* 3. Fix up heap edge additions */
794 HashMap<AllocNode, MySet<Edge>> addheapedge=new HashMap<AllocNode, MySet<Edge>>();
795 for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.heapedgeadd.entrySet().iterator();entryIt.hasNext();) {
796 Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
797 MySet<Edge> edgeset=entry.getValue();
798 AllocNode allocnode=entry.getKey();
799 if (allocnode==single) {
801 summarizeSet(edgeset, graph.nodeMap.get(summary), single, summary);
802 addheapedge.put(summary, edgeset);
804 summarizeSet(edgeset, graph.nodeMap.get(allocnode), single, summary);
810 for(Map.Entry<AllocNode, MySet<Edge>> entry:addheapedge.entrySet()) {
811 AllocNode allocnode=entry.getKey();
812 Util.relationUpdate(delta.heapedgeadd, allocnode, null, entry.getValue());
815 /* 4. Fix up the base heap edges */
817 for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.baseheapedge.entrySet().iterator();entryIt.hasNext();) {
818 Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
819 MySet<Edge> edgeset=entry.getValue();
820 AllocNode allocnode=entry.getKey();
821 if (allocnode==single) {
824 AllocNode addnode=(allocnode==single)?summary:allocnode;
826 MySet<Edge> newset=(MySet<Edge>) edgeset.clone();
827 MySet<Edge> removeset=shrinkSet(newset, graph.nodeMap.get(addnode), single, summary);
828 Util.relationUpdate(delta.heapedgeadd, addnode, null, newset);
829 Util.relationUpdate(delta.heapedgeremove, allocnode, null, removeset);
832 /* Update Node Ages...If the base or addNodeAges set contains a
833 * single node, it now should also contain a summary node... No
834 * need to generate a single node as that has already been
836 if (delta.baseNodeAges.contains(single)||delta.addNodeAges.contains(single)) {
837 delta.addNodeAges.add(summary);
840 //Apply incoming diffs to graph
841 applyDiffs(graph, delta);
846 /* This function builds a new edge set where oldnode is summarized into new node */
848 void summarizeSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode sumnode) {
849 MySet<Edge> newSet=null;
850 for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
851 Edge e=edgeit.next();
852 if (e.dst==oldnode||e.src==oldnode) {
854 newSet=new MySet<Edge>();
859 if (e.dst==oldnode) {
862 if (e.src==oldnode) {
865 if (oldedgeset==null||!oldedgeset.contains(e))
870 edgeset.addAll(newSet);
873 /* Shrinks the incoming set to just include rewritten values.
874 * Returns a set of the original rewritten values */
876 MySet<Edge> shrinkSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode newnode) {
877 MySet<Edge> newSet=null;
878 MySet<Edge> removeSet=null;
879 for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
880 Edge e=edgeit.next();
882 if (e.dst==oldnode||e.src==oldnode) {
884 newSet=new MySet<Edge>();
885 removeSet=new MySet<Edge>();
888 removeSet.add(e.copy());
893 if (oldedgeset==null||!oldedgeset.contains(e))
898 edgeset.addAll(newSet);
902 /* This function returns a completely new Delta... It is safe to
905 Delta applyInitDelta(Delta delta, BBlock block) {
906 //Apply delta to graph
907 boolean newGraph=false;
908 if (!bbgraphMap.containsKey(block)) {
909 bbgraphMap.put(block, new Graph(null));
913 Graph graph=bbgraphMap.get(block);
916 newdelta=new Delta(null, true);
917 //Add in heap edges and throw away original diff
918 graph.nodeMap.putAll(delta.heapedgeadd);
919 //Add in var edges and throw away original diff
920 graph.varMap.putAll(delta.varedgeadd);
921 //Record that this is initial set...
922 graph.nodeAges.addAll(delta.addNodeAges);
924 newdelta=new Delta(null, false);
925 //merge in heap edges and variables
926 mergeHeapEdges(graph, delta, newdelta);
927 mergeVarEdges(graph, delta, newdelta);
928 mergeAges(graph, delta, newdelta);
933 /* This function merges in the heap edges. It updates delta to be
936 void mergeHeapEdges(Graph graph, Delta delta, Delta newdelta) {
938 for(Map.Entry<AllocNode, MySet<Edge>> heapedge:delta.heapedgeadd.entrySet()) {
939 AllocNode nsrc=heapedge.getKey();
940 MySet<Edge> edges=heapedge.getValue();
941 if (!graph.nodeMap.containsKey(nsrc)) {
942 graph.nodeMap.put(nsrc, new MySet<Edge>());
944 MySet<Edge> dstedges=graph.nodeMap.get(nsrc);
945 MySet<Edge> diffedges=new MySet<Edge>();
947 if (!dstedges.contains(e)) {
953 //Done with edge set...
954 if (diffedges.size()>0) {
956 newdelta.baseheapedge.put(nsrc, diffedges);
961 /* This function merges in the var edges. It updates delta to be
964 void mergeVarEdges(Graph graph, Delta delta, Delta newdelta) {
966 for(Map.Entry<TempDescriptor, MySet<Edge>> varedge:delta.varedgeadd.entrySet()) {
967 TempDescriptor tmpsrc=varedge.getKey();
968 MySet<Edge> edges=varedge.getValue();
969 if (!graph.varMap.containsKey(tmpsrc)) {
970 graph.varMap.put(tmpsrc, new MySet<Edge>());
972 MySet<Edge> dstedges=graph.varMap.get(tmpsrc);
973 MySet<Edge> diffedges=new MySet<Edge>();
975 if (!dstedges.contains(e)) {
981 //Done with edge set...
982 if (diffedges.size()>=0) {
984 newdelta.basevaredge.put(tmpsrc,diffedges);
989 void mergeAges(Graph graph, Delta delta, Delta newDelta) {
991 for(AllocNode node:delta.addNodeAges) {
992 if (!graph.nodeAges.contains(node)) {
993 graph.nodeAges.add(node);
994 newDelta.baseNodeAges.add(node);