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);
123 if (graph.oldNodes.containsKey(node)) {
124 if (graph.oldNodes.get(node).booleanValue())
125 newDelta.addOldNodes.put(node, Boolean.TRUE);
126 } else if (graph.parent.oldNodes.containsKey(node)) {
127 //parent graphs only contain true...no need to check
128 newDelta.addOldNodes.put(node, Boolean.TRUE);
133 void generateFinalDelta(BBlock bblock, Delta delta, Graph graph) {
134 Delta newDelta=new Delta(null, false);
135 if (delta.getInit()) {
136 buildInitDelta(graph, newDelta);
138 /* We can break the old delta...it is done being used */
139 /* First we will build variable edges */
140 HashSet<TempDescriptor> tmpSet=new HashSet<TempDescriptor>();
141 tmpSet.addAll(delta.basevaredge.keySet());
142 tmpSet.addAll(delta.varedgeadd.keySet());
143 for(TempDescriptor tmp:tmpSet) {
144 /* Start with the new incoming edges */
145 MySet<Edge> newbaseedge=delta.basevaredge.get(tmp);
146 /* Remove the remove set */
147 newbaseedge.removeAll(delta.varedgeremove.get(tmp));
148 /* Add in the new set*/
149 newbaseedge.addAll(delta.varedgeadd.get(tmp));
150 /* Store the results */
151 newDelta.varedgeadd.put(tmp, newbaseedge);
154 /* Next we build heap edges */
155 HashSet<AllocNode> nodeSet=new HashSet<AllocNode>();
156 nodeSet.addAll(delta.baseheapedge.keySet());
157 nodeSet.addAll(delta.heapedgeadd.keySet());
158 nodeSet.addAll(delta.heapedgeremove.keySet());
159 for(AllocNode node:nodeSet) {
160 /* Start with the new incoming edges */
161 MySet<Edge> newheapedge=(MySet<Edge>) delta.baseheapedge.get(node).clone();
162 /* Remove the remove set */
163 newheapedge.removeAll(delta.heapedgeremove.get(node));
164 /* Add in the add set */
165 newheapedge.addAll(delta.heapedgeadd.get(node));
166 newDelta.heapedgeadd.put(node, newheapedge);
168 /* Also need to subtract off some edges */
169 MySet<Edge> removeset=delta.heapedgeremove.get(node);
171 /* Remove the newly created edges..no need to propagate a diff for those */
172 removeset.removeAll(delta.baseheapedge.get(node));
173 newDelta.heapedgeremove.put(node, removeset);
176 /* Compute new ages */
177 newDelta.addNodeAges.addAll(delta.baseNodeAges);
178 newDelta.addNodeAges.addAll(delta.addNodeAges);
179 HashSet<AllocNode> oldNodes=new HashSet<AllocNode>();
181 /* Compute whether old nodes survive */
182 oldNodes.addAll(delta.baseOldNodes.keySet());
183 oldNodes.addAll(delta.addOldNodes.keySet());
184 for(AllocNode node:oldNodes) {
185 if (delta.addOldNodes.containsKey(node)) {
186 if (delta.addOldNodes.get(node).booleanValue()) {
187 newDelta.addOldNodes.put(node, Boolean.TRUE);
190 if (delta.baseOldNodes.get(node).booleanValue()) {
191 newDelta.addOldNodes.put(node, Boolean.TRUE);
197 /* Now we need to propagate newdelta */
198 if (!newDelta.heapedgeadd.isEmpty()||!newDelta.heapedgeremove.isEmpty()||!newDelta.varedgeadd.isEmpty()||!newDelta.addNodeAges.isEmpty()||!newDelta.addOldNodes.isEmpty()) {
199 /* We have a delta to propagate */
200 Vector<BBlock> blockvector=bblock.next();
201 for(int i=0;i<blockvector.size();i++) {
203 newDelta.setBlock(new PPoint(blockvector.get(i)));
204 toprocess.add(newDelta);
206 toprocess.add(newDelta.diffBlock(new PPoint(blockvector.get(i))));
212 Delta processNode(BBlock bblock, int index, FlatNode node, Delta delta, Graph newgraph) {
213 switch(node.kind()) {
215 return processNewNode((FlatNew)node, delta, newgraph);
216 case FKind.FlatFieldNode:
217 case FKind.FlatElementNode:
218 return processFieldElementNode(node, delta, newgraph);
219 case FKind.FlatCastNode:
220 case FKind.FlatOpNode:
221 case FKind.FlatReturnNode:
222 return processCopyNode(node, delta, newgraph);
223 case FKind.FlatSetFieldNode:
224 case FKind.FlatSetElementNode:
225 return processSetFieldElementNode(node, delta, newgraph);
226 case FKind.FlatMethod:
228 return processFlatNop(node, delta, newgraph);
230 return processFlatCall(bblock, index, (FlatCall) node, delta, newgraph);
231 case FKind.FlatSESEEnterNode:
232 case FKind.FlatSESEExitNode:
233 throw new Error("Unimplemented node:"+node);
235 throw new Error("Unrecognized node:"+node);
240 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) {
241 //Handle the this temp
243 MySet<Edge> edges=(oldnodeset!=null)?GraphManip.getDiffEdges(delta, tmpthis):GraphManip.getEdges(graph, delta, tmpthis);
244 newDelta.varedgeadd.put(tmpthis, (MySet<Edge>) edges.clone());
245 edgeset.addAll(edges);
247 AllocNode dstnode=e.dst;
248 if (!nodeset.contains(dstnode)&&(oldnodeset==null||!oldnodeset.contains(dstnode))) {
249 TypeDescriptor type=dstnode.getType();
250 if (!type.isArray()) {
251 targetSet.add(type.getClassDesc());
253 //arrays don't have code
254 targetSet.add(typeUtil.getClass(TypeUtil.ObjectClass));
256 nodeset.add(dstnode);
257 tovisit.add(dstnode);
263 void processParams(Graph graph, Delta delta, Delta newDelta, HashSet<AllocNode> nodeset, Stack<AllocNode> tovisit, MySet<Edge> edgeset, FlatCall fcall, boolean diff) {
264 //Go through each temp
265 for(int i=0;i<fcall.numArgs();i++) {
266 TempDescriptor tmp=fcall.getArg(i);
267 MySet<Edge> edges=diff?GraphManip.getDiffEdges(delta, tmp):GraphManip.getEdges(graph, delta, tmp);
268 newDelta.varedgeadd.put(tmp, (MySet<Edge>) edges.clone());
269 edgeset.addAll(edges);
271 if (!nodeset.contains(e.dst)) {
279 void computeReachableNodes(Graph graph, Delta delta, Delta newDelta, HashSet<AllocNode> nodeset, Stack<AllocNode> tovisit, MySet<Edge> edgeset, HashSet<AllocNode> oldnodeset) {
280 while(!tovisit.isEmpty()) {
281 AllocNode node=tovisit.pop();
282 MySet<Edge> edges=GraphManip.getEdges(graph, delta, node);
283 newDelta.heapedgeadd.put(node, edges);
284 edgeset.addAll(edges);
286 if (!nodeset.contains(e.dst)&&(oldnodeset==null||!oldnodeset.contains(e.dst))) {
294 HashSet<MethodDescriptor> computeTargets(FlatCall fcall, Delta newDelta) {
295 TempDescriptor tmpthis=fcall.getThis();
296 MethodDescriptor md=fcall.getMethod();
297 HashSet<MethodDescriptor> targets=new HashSet<MethodDescriptor>();
302 for(Edge e:newDelta.varedgeadd.get(tmpthis)) {
303 AllocNode node=e.dst;
304 ClassDescriptor cd=node.getType().getClassDesc();
305 //Figure out exact method called and add to set
306 targets.add(cd.getCalledMethod(md));
313 void fixMapping(FlatCall fcall, HashSet<MethodDescriptor> targets, MySet<Edge> oldedgeset, Delta newDelta, BBlock callblock, int callindex) {
314 Delta basedelta=null;
315 TempDescriptor tmpthis=fcall.getThis();
317 for(MethodDescriptor calledmd:targets) {
318 FlatMethod fm=state.getMethodFlat(calledmd);
319 boolean newmethod=false;
322 HashMap<TempDescriptor, TempDescriptor> tmpMap=new HashMap<TempDescriptor, TempDescriptor>();
325 tmpMap.put(tmpthis, fm.getParameter(offset++));
327 for(int i=0;i<fcall.numArgs();i++) {
328 TempDescriptor tmp=fcall.getArg(i);
329 tmpMap.put(tmp,fm.getParameter(i+offset));
332 //Get basicblock for the method
333 BasicBlock block=getBBlock(fm);
336 if (!callMap.containsKey(fcall)) {
337 callMap.put(fcall, new HashSet<BBlock>());
340 Delta returnDelta=null;
341 if (!callMap.get(fcall).contains(block.getStart())) {
342 callMap.get(fcall).add(block.getStart());
346 if (!returnMap.containsKey(block.getExit())) {
347 returnMap.put(block.getExit(), new HashSet<PPoint>());
349 returnMap.get(block.getExit()).add(new PPoint(callblock, callindex));
351 if (bbgraphMap.containsKey(block.getExit())) {
352 //Need to push existing results to current node
353 if (returnDelta==null) {
354 returnDelta=new Delta(null, false);
355 buildInitDelta(bbgraphMap.get(block.getExit()), returnDelta);
356 if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
357 returnDelta.setBlock(new PPoint(callblock, callindex));
358 toprocess.add(returnDelta);
361 if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
362 toprocess.add(returnDelta.diffBlock(new PPoint(callblock, callindex)));
368 if (oldedgeset==null) {
369 //First build of this graph
370 //Build and enqueue delta...safe to just use existing delta
371 Delta d=newDelta.changeParams(tmpMap, new PPoint(block.getStart()));
373 } else if (newmethod) {
374 if (basedelta==null) {
375 basedelta=newDelta.buildBase(oldedgeset);
377 //Build and enqueue delta
378 Delta d=basedelta.changeParams(tmpMap, new PPoint(block.getStart()));
381 //Build and enqueue delta
382 Delta d=newDelta.changeParams(tmpMap, new PPoint(block.getStart()));
389 /* This function computes all edges that start outside of the callee context and go into the callee context */
391 void computeExternalEdges(Graph graph, Delta delta, HashSet<AllocNode> nodeset, HashSet<AllocNode> deltaset, MySet<Edge> externaledgeset) {
392 //Do heap edges first
393 HashSet<AllocNode> externalnodes=new HashSet<AllocNode>();
394 externalnodes.addAll(delta.baseheapedge.keySet());
395 externalnodes.addAll(delta.heapedgeadd.keySet());
396 externalnodes.addAll(delta.heapedgeremove.keySet());
397 //remove allinternal nodes
398 externalnodes.removeAll(nodeset);
399 for(AllocNode extNode:externalnodes) {
400 //Compute set of edges from given node
401 MySet<Edge> edges=new MySet<Edge>(delta.baseheapedge.get(extNode));
402 edges.removeAll(delta.heapedgeremove.get(extNode));
403 edges.addAll(delta.heapedgeadd.get(extNode));
406 if (nodeset.contains(e.dst))
407 externaledgeset.add(e);
411 //Do heap edges first
412 HashSet<TempDescriptor> temps=new HashSet<TempDescriptor>();
413 temps.addAll(delta.basevaredge.keySet());
414 temps.addAll(delta.varedgeadd.keySet());
415 temps.addAll(delta.varedgeremove.keySet());
416 //remove allinternal nodes
417 temps.removeAll(nodeset);
419 for(TempDescriptor tmp:temps) {
420 //Compute set of edges from given node
421 MySet<Edge> edges=new MySet<Edge>(delta.basevaredge.get(tmp));
422 edges.removeAll(delta.varedgeremove.get(tmp));
423 edges.addAll(delta.varedgeadd.get(tmp));
426 if (nodeset.contains(e.dst))
427 externaledgeset.add(e);
432 void removeEdges(Delta delta, HashSet<AllocNode> nodeset, MySet<Edge> edgeset, MySet<Edge> externaledgeset) {
433 //Want to remove the set of internal edges
434 for(Edge e:edgeset) {
436 if (delta.heapedgeadd.containsKey(e.src)&&delta.heapedgeadd.get(e.src).contains(e)) {
437 //remove edge if it is in the add set
438 delta.heapedgeadd.get(e.src).remove(e);
440 //add edge to to the remove set
441 if (!delta.heapedgeremove.containsKey(e.src))
442 delta.heapedgeremove.put(e.src, new MySet<Edge>());
443 delta.heapedgeremove.get(e.src).add(e);
448 //Want to remove the set of external edges
449 for(Edge e:externaledgeset) {
450 //want to remove the set of internal edges
452 if (delta.heapedgeadd.containsKey(e.src)&&delta.heapedgeadd.get(e.src).contains(e)) {
453 //remove edge if it is in the add set
454 delta.heapedgeadd.get(e.src).remove(e);
456 //add edge to to the remove set
457 if (!delta.heapedgeremove.containsKey(e.src))
458 delta.heapedgeremove.put(e.src, new MySet<Edge>());
459 delta.heapedgeremove.get(e.src).add(e);
462 if (delta.varedgeadd.containsKey(e.srcvar)&&delta.varedgeadd.get(e.srcvar).contains(e)) {
463 //remove edge if it is in the add set
464 delta.varedgeadd.get(e.srcvar).remove(e);
466 //add edge to to the remove set
467 if (!delta.varedgeremove.containsKey(e.srcvar))
468 delta.varedgeremove.put(e.srcvar,new MySet<Edge>());
469 delta.varedgeremove.get(e.srcvar).add(e);
476 Delta processFlatCall(BBlock callblock, int callindex, FlatCall fcall, Delta delta, Graph graph) {
477 Delta newDelta=new Delta(null, false);
479 if (delta.getInit()) {
480 MySet<Edge> edgeset=new MySet<Edge>();
481 MySet<Edge> externaledgeset=new MySet<Edge>();
482 HashSet<AllocNode> nodeset=new HashSet<AllocNode>();
483 HashSet<ClassDescriptor> targetSet=new HashSet<ClassDescriptor>();
484 Stack<AllocNode> tovisit=new Stack<AllocNode>();
485 TempDescriptor tmpthis=fcall.getThis();
487 //Handle the this temp
488 processThisTargets(targetSet, graph, delta, newDelta, nodeset, tovisit, edgeset, tmpthis, null);
490 //Go through each temp
491 processParams(graph, delta, newDelta, nodeset, tovisit, edgeset, fcall, false);
493 //Traverse all reachable nodes
494 computeReachableNodes(graph, delta, newDelta, nodeset, tovisit, edgeset, null);
496 //Compute call targets
497 HashSet<MethodDescriptor> targets=computeTargets(fcall, newDelta);
500 fixMapping(fcall, targets, null, newDelta, callblock, callindex);
502 //Compute edges into region to splice out
503 computeExternalEdges(graph, delta, nodeset, null, externaledgeset);
505 //Splice out internal edges
506 removeEdges(delta, nodeset, edgeset, externaledgeset);
508 graph.reachNode=nodeset;
509 graph.reachEdge=edgeset;
511 //Apply diffs to graph
512 applyDiffs(graph, delta, true);
514 MySet<Edge> edgeset=new MySet<Edge>();
515 MySet<Edge> externaledgeset=new MySet<Edge>();
516 HashSet<AllocNode> nodeset=new HashSet<AllocNode>();
517 MySet<Edge> oldedgeset=graph.reachEdge;
518 HashSet<AllocNode> oldnodeset=graph.reachNode;
520 HashSet<ClassDescriptor> targetSet=new HashSet<ClassDescriptor>();
521 Stack<AllocNode> tovisit=new Stack<AllocNode>();
522 TempDescriptor tmpthis=fcall.getThis();
524 //Handle the this temp
525 processThisTargets(targetSet, graph, delta, newDelta, nodeset, tovisit, edgeset, tmpthis, oldnodeset);
527 //Go through each temp
528 processParams(graph, delta, newDelta, nodeset, tovisit, edgeset, fcall, true);
530 //Go through each new heap edge that starts from old node
531 MySet<Edge> newedges=GraphManip.getDiffEdges(delta, oldnodeset);
532 edgeset.addAll(newedges);
533 for(Edge e:newedges) {
534 if (!nodeset.contains(e.dst)&&!oldnodeset.contains(e.dst)) {
540 //Traverse all reachable nodes
541 computeReachableNodes(graph, delta, newDelta, nodeset, tovisit, edgeset, oldnodeset);
543 //Compute call targets
544 HashSet<MethodDescriptor> targets=computeTargets(fcall, newDelta);
546 //add in new nodeset and edgeset
547 oldnodeset.addAll(nodeset);
548 oldedgeset.addAll(edgeset);
551 fixMapping(fcall, targets, oldedgeset, newDelta, callblock, callindex);
553 //Compute edges into region to splice out
554 computeExternalEdges(graph, delta, oldnodeset, nodeset, externaledgeset);
556 //Splice out internal edges
557 removeEdges(delta, nodeset, edgeset, externaledgeset);
559 //Apply diffs to graph
560 applyDiffs(graph, delta);
565 void applyDiffs(Graph graph, Delta delta) {
566 applyDiffs(graph, delta, false);
569 void applyDiffs(Graph graph, Delta delta, boolean genbackwards) {
570 //build backwards map if requested
571 if (genbackwards&&graph.backMap==null) {
572 graph.backMap=new HashMap<AllocNode, MySet<Edge>>();
573 if (graph.parent.backMap==null) {
574 graph.parent.backMap=new HashMap<AllocNode, MySet<Edge>>();
575 for(Map.Entry<AllocNode, MySet<Edge>> entry:graph.nodeMap.entrySet()) {
576 for(Edge e:entry.getValue()) {
577 if (!graph.parent.backMap.containsKey(e.dst))
578 graph.parent.backMap.put(e.dst, new MySet<Edge>());
579 graph.parent.backMap.get(e.dst).add(e);
582 for(Map.Entry<TempDescriptor, MySet<Edge>> entry:graph.varMap.entrySet()) {
583 for(Edge e:entry.getValue()) {
584 if (!graph.parent.backMap.containsKey(e.dst))
585 graph.parent.backMap.put(e.dst, new MySet<Edge>());
586 graph.parent.backMap.get(e.dst).add(e);
592 //Add hidden base edges
593 for(Map.Entry<AllocNode, MySet<Edge>> e: delta.baseheapedge.entrySet()) {
594 AllocNode node=e.getKey();
595 MySet<Edge> edges=e.getValue();
596 if (graph.nodeMap.containsKey(node)) {
597 MySet<Edge> nodeEdges=graph.nodeMap.get(node);
598 nodeEdges.addAll(edges);
603 for(Map.Entry<AllocNode, MySet<Edge>> e: delta.heapedgeremove.entrySet()) {
604 AllocNode node=e.getKey();
605 MySet<Edge> edgestoremove=e.getValue();
606 if (graph.nodeMap.containsKey(node)) {
607 //Just apply diff to current map
608 graph.nodeMap.get(node).removeAll(edgestoremove);
610 //Generate diff from parent graph
611 MySet<Edge> parentedges=graph.parent.nodeMap.get(node);
612 MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
613 graph.nodeMap.put(node, newedgeset);
618 for(Map.Entry<AllocNode, MySet<Edge>> e: delta.heapedgeadd.entrySet()) {
619 AllocNode node=e.getKey();
620 MySet<Edge> edgestoadd=e.getValue();
621 //If we have not done a subtract, then
622 if (!graph.nodeMap.containsKey(node)) {
623 //Copy the parent entry
624 graph.nodeMap.put(node, (MySet<Edge>)graph.parent.nodeMap.get(node).clone());
626 graph.nodeMap.get(node).addAll(edgestoadd);
628 for(Edge eadd:edgestoadd) {
629 if (!graph.backMap.containsKey(eadd.dst))
630 graph.backMap.put(eadd.dst, new MySet<Edge>());
631 graph.backMap.get(eadd.dst).add(eadd);
637 for(Map.Entry<TempDescriptor, MySet<Edge>> e: delta.varedgeremove.entrySet()) {
638 TempDescriptor tmp=e.getKey();
639 MySet<Edge> edgestoremove=e.getValue();
641 if (graph.varMap.containsKey(tmp)) {
642 //Just apply diff to current map
643 graph.varMap.get(tmp).removeAll(edgestoremove);
645 //Generate diff from parent graph
646 MySet<Edge> parentedges=graph.parent.varMap.get(tmp);
647 MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
648 graph.varMap.put(tmp, newedgeset);
653 for(Map.Entry<TempDescriptor, MySet<Edge>> e: delta.varedgeadd.entrySet()) {
654 TempDescriptor tmp=e.getKey();
655 MySet<Edge> edgestoadd=e.getValue();
656 graph.varMap.put(tmp, (MySet<Edge>) edgestoadd.clone());
658 for(Edge eadd:edgestoadd) {
659 if (!graph.backMap.containsKey(eadd.dst))
660 graph.backMap.put(eadd.dst, new MySet<Edge>());
661 graph.backMap.get(eadd.dst).add(eadd);
667 for(AllocNode node:delta.addNodeAges) {
668 graph.nodeAges.add(node);
671 for(Map.Entry<AllocNode, Boolean> nodeentry:delta.addOldNodes.entrySet()) {
672 AllocNode node=nodeentry.getKey();
673 Boolean ispresent=nodeentry.getValue();
674 graph.oldNodes.put(node, ispresent);
678 Delta processSetFieldElementNode(FlatNode node, Delta delta, Graph graph) {
682 if (node.kind()==FKind.FlatSetElementNode) {
683 FlatSetElementNode fen=(FlatSetElementNode) node;
688 FlatSetFieldNode ffn=(FlatSetFieldNode) node;
693 if (delta.getInit()) {
694 HashSet<AllocNode> srcNodes=GraphManip.getNodes(graph, delta, src);
695 HashSet<AllocNode> dstNodes=GraphManip.getNodes(graph, delta, dst);
696 MySet<Edge> edgesToAdd=GraphManip.genEdges(srcNodes, fd, dstNodes);
697 MySet<Edge> edgesToRemove=null;
698 if (dstNodes.size()==1&&!dstNodes.iterator().next().isSummary()) {
699 /* Can do a strong update */
700 edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
703 updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
704 applyDiffs(graph, delta);
706 /* First look at new sources */
707 MySet<Edge> edgesToAdd=new MySet<Edge>();
708 HashSet<AllocNode> newSrcNodes=GraphManip.getDiffNodes(delta, src);
709 HashSet<AllocNode> dstNodes=GraphManip.getDiffNodes(delta, dst);
710 edgesToAdd.addAll(GraphManip.genEdges(newSrcNodes, fd, dstNodes));
711 HashSet<AllocNode> newDstNodes=GraphManip.getDiffNodes(delta, dst);
712 MySet<Edge> edgesToRemove=null;
713 if (newDstNodes.size()!=0) {
714 if (dstNodes.size()==1&&!dstNodes.iterator().next().isSummary()) {
715 /* Need to undo strong update */
716 if (graph.strongUpdateSet!=null) {
717 edgesToAdd.addAll(graph.strongUpdateSet);
718 graph.strongUpdateSet.clear();
720 } else if (dstNodes.size()==0&&newDstNodes.size()==1&&!newDstNodes.iterator().next().isSummary()&&graph.strongUpdateSet==null) {
721 edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
723 HashSet<AllocNode> srcNodes=GraphManip.getDiffNodes(delta, src);
724 edgesToAdd.addAll(GraphManip.genEdges(srcNodes, fd, newDstNodes));
727 updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
728 applyDiffs(graph, delta);
733 Delta processCopyNode(FlatNode node, Delta delta, Graph graph) {
736 if (node.kind()==FKind.FlatOpNode) {
737 FlatOpNode fon=(FlatOpNode) node;
740 } else if (node.kind()==FKind.FlatReturnNode) {
741 FlatReturnNode frn=(FlatReturnNode)node;
742 src=frn.getReturnTemp();
745 FlatCastNode fcn=(FlatCastNode) node;
749 if (delta.getInit()) {
750 HashSet<AllocNode> srcnodes=GraphManip.getNodes(graph, delta, src);
751 MySet<Edge> edgesToAdd=GraphManip.genEdges(src, srcnodes);
752 MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
753 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
754 applyDiffs(graph, delta);
756 /* First compute new src nodes */
757 HashSet<AllocNode> newSrcNodes=GraphManip.getDiffNodes(delta, src);
759 /* Compute the union, and then the set of edges */
760 MySet<Edge> edgesToAdd=GraphManip.genEdges(src, newSrcNodes);
762 /* Compute set of edges to remove */
763 MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);
766 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
767 applyDiffs(graph, delta);
772 Delta processFieldElementNode(FlatNode node, Delta delta, Graph graph) {
776 if (node.kind()==FKind.FlatElementNode) {
777 FlatElementNode fen=(FlatElementNode) node;
782 FlatFieldNode ffn=(FlatFieldNode) node;
787 if (delta.getInit()) {
788 HashSet<AllocNode> srcnodes=GraphManip.getNodes(graph, delta, src);
789 HashSet<AllocNode> fdnodes=GraphManip.getNodes(graph, delta, srcnodes, fd);
790 MySet<Edge> edgesToAdd=GraphManip.genEdges(src, fdnodes);
791 MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
792 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
793 applyDiffs(graph, delta);
795 /* First compute new objects we read fields of */
796 HashSet<AllocNode> allsrcnodes=GraphManip.getNodes(graph, delta, src);
797 HashSet<AllocNode> difffdnodes=GraphManip.getDiffNodes(delta, allsrcnodes, fd);
798 /* Next compute new targets of fields */
799 HashSet<AllocNode> newsrcnodes=GraphManip.getDiffNodes(delta, src);
800 HashSet<AllocNode> newfdnodes=GraphManip.getNodes(graph, delta, newsrcnodes, fd);
801 /* Compute the union, and then the set of edges */
802 HashSet<AllocNode> newTargets=new HashSet<AllocNode>();
803 newTargets.addAll(newfdnodes);
804 newTargets.addAll(difffdnodes);
805 MySet<Edge> edgesToAdd=GraphManip.genEdges(src, newTargets);
807 /* Compute set of edges to remove */
808 MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);
811 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
812 applyDiffs(graph, delta);
817 static void updateVarDelta(Graph graph, Delta delta, TempDescriptor tmp, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
818 MySet<Edge> edgeAdd=delta.varedgeadd.get(tmp);
819 MySet<Edge> edgeRemove=delta.varedgeremove.get(tmp);
820 MySet<Edge> existingEdges=graph.getEdges(tmp);
821 for(Edge e: edgestoRemove) {
822 //remove edge from delta
824 //if the edge is already in the graph, add an explicit remove to the delta
825 if (existingEdges.contains(e))
828 for(Edge e: edgestoAdd) {
829 //Remove the edge from the remove set
830 edgeRemove.remove(e);
831 //Explicitly add it to the add set unless it is already in the graph
832 if (!existingEdges.contains(e))
837 static void updateHeapDelta(Graph graph, Delta delta, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
838 for(Edge e: edgestoRemove) {
840 MySet<Edge> edgeAdd=delta.heapedgeadd.get(src);
841 MySet<Edge> existingEdges=graph.getEdges(src);
842 //remove edge from delta
844 //if the edge is already in the graph, add an explicit remove to the delta
845 if (existingEdges.contains(e)) {
846 MySet<Edge> edgeRemove=delta.heapedgeremove.get(src);
850 for(Edge e: edgestoAdd) {
852 MySet<Edge> edgeRemove=delta.heapedgeremove.get(src);
853 MySet<Edge> existingEdges=graph.getEdges(src);
854 //Remove the edge from the remove set
855 edgeRemove.remove(e);
856 //Explicitly add it to the add set unless it is already in the graph
857 if (!existingEdges.contains(e)) {
858 MySet<Edge> edgeAdd=delta.heapedgeadd.get(src);
864 Delta processFlatNop(FlatNode node, Delta delta, Graph graph) {
865 applyDiffs(graph, delta);
869 Delta processNewNode(FlatNew node, Delta delta, Graph graph) {
870 AllocNode summary=allocFactory.getAllocNode(node, true);
871 AllocNode single=allocFactory.getAllocNode(node, false);
872 TempDescriptor tmp=node.getDst();
874 if (delta.getInit()) {
875 /* We don't have to deal with summarization here... The
876 * intuition is that this is the only place where we generate
877 * nodes for this allocation site and this is the first time
878 * we've analyzed this site */
881 Edge e=new Edge(tmp, single);
883 MySet<Edge> newedges=new MySet<Edge>();
885 //Add it into the diffs
886 delta.varedgeadd.put(tmp, newedges);
887 //Remove the old edges
888 delta.varedgeremove.put(tmp, graph.getEdges(tmp));
889 //Apply incoming diffs to graph
890 applyDiffs(graph, delta);
891 //Note that we create a single node
892 delta.addNodeAges.add(single);
894 if (delta.addOldNodes.containsKey(single)||delta.baseOldNodes.containsKey(single)) {
895 delta.addOldNodes.put(single, Boolean.FALSE);
898 /* 1. Fix up the variable edge additions */
900 for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.varedgeadd.entrySet().iterator();entryIt.hasNext();) {
901 Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
903 if (entry.getKey()==tmp) {
904 /* Check if this is the tmp we overwrite */
907 /* Otherwise, check if the target of the edge is changed... */
908 summarizeSet(entry.getValue(), graph.varMap.get(entry.getKey()), single, summary);
912 /* 2. Fix up the base variable edges */
914 for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.basevaredge.entrySet().iterator();entryIt.hasNext();) {
915 Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
916 TempDescriptor entrytmp=entry.getKey();
918 /* Check is this is the tmp we overwrite, if so add to remove set */
919 Util.relationUpdate(delta.varedgeremove, tmp, null, entry.getValue());
921 /* Check if the target of the edge is changed */
922 MySet<Edge> newset=(MySet<Edge>)entry.getValue().clone();
923 MySet<Edge> removeset=shrinkSet(newset, graph.varMap.get(entrytmp), single, summary);
924 Util.relationUpdate(delta.varedgeremove, entrytmp, newset, removeset);
925 Util.relationUpdate(delta.varedgeadd, entrytmp, null, newset);
930 /* 3. Fix up heap edge additions */
932 HashMap<AllocNode, MySet<Edge>> addheapedge=new HashMap<AllocNode, MySet<Edge>>();
933 for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.heapedgeadd.entrySet().iterator();entryIt.hasNext();) {
934 Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
935 MySet<Edge> edgeset=entry.getValue();
936 AllocNode allocnode=entry.getKey();
937 if (allocnode==single) {
939 summarizeSet(edgeset, graph.nodeMap.get(summary), single, summary);
940 addheapedge.put(summary, edgeset);
942 summarizeSet(edgeset, graph.nodeMap.get(allocnode), single, summary);
948 for(Map.Entry<AllocNode, MySet<Edge>> entry:addheapedge.entrySet()) {
949 AllocNode allocnode=entry.getKey();
950 Util.relationUpdate(delta.heapedgeadd, allocnode, null, entry.getValue());
953 /* 4. Fix up the base heap edges */
955 for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.baseheapedge.entrySet().iterator();entryIt.hasNext();) {
956 Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
957 MySet<Edge> edgeset=entry.getValue();
958 AllocNode allocnode=entry.getKey();
959 if (allocnode==single) {
962 AllocNode addnode=(allocnode==single)?summary:allocnode;
964 MySet<Edge> newset=(MySet<Edge>) edgeset.clone();
965 MySet<Edge> removeset=shrinkSet(newset, graph.nodeMap.get(addnode), single, summary);
966 Util.relationUpdate(delta.heapedgeadd, addnode, null, newset);
967 Util.relationUpdate(delta.heapedgeremove, allocnode, null, removeset);
970 /* Update Node Ages...If the base or addNodeAges set contains a
971 * single node, it now should also contain a summary node... No
972 * need to generate a single node as that has already been
974 if (delta.baseNodeAges.contains(single)||delta.addNodeAges.contains(single)) {
975 delta.addNodeAges.add(summary);
978 //Kill the old node if someone tries to add it
979 if (delta.addOldNodes.containsKey(single)||delta.baseOldNodes.containsKey(single)) {
980 delta.addOldNodes.put(single, Boolean.FALSE);
983 //Apply incoming diffs to graph
984 applyDiffs(graph, delta);
989 /* This function builds a new edge set where oldnode is summarized into new node */
991 void summarizeSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode sumnode) {
992 MySet<Edge> newSet=null;
993 for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
994 Edge e=edgeit.next();
995 if (e.dst==oldnode||e.src==oldnode) {
997 newSet=new MySet<Edge>();
1002 if (e.dst==oldnode) {
1005 if (e.src==oldnode) {
1008 if (oldedgeset==null||!oldedgeset.contains(e))
1013 edgeset.addAll(newSet);
1016 /* Shrinks the incoming set to just include rewritten values.
1017 * Returns a set of the original rewritten values */
1019 MySet<Edge> shrinkSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode newnode) {
1020 MySet<Edge> newSet=null;
1021 MySet<Edge> removeSet=null;
1022 for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
1023 Edge e=edgeit.next();
1025 if (e.dst==oldnode||e.src==oldnode) {
1027 newSet=new MySet<Edge>();
1028 removeSet=new MySet<Edge>();
1031 removeSet.add(e.copy());
1036 if (oldedgeset==null||!oldedgeset.contains(e))
1041 edgeset.addAll(newSet);
1045 /* This function returns a completely new Delta... It is safe to
1048 Delta applyInitDelta(Delta delta, BBlock block) {
1049 //Apply delta to graph
1050 boolean newGraph=false;
1051 if (!bbgraphMap.containsKey(block)) {
1052 bbgraphMap.put(block, new Graph(null));
1056 Graph graph=bbgraphMap.get(block);
1059 newdelta=new Delta(null, true);
1060 //Add in heap edges and throw away original diff
1061 graph.nodeMap.putAll(delta.heapedgeadd);
1062 //Add in var edges and throw away original diff
1063 graph.varMap.putAll(delta.varedgeadd);
1064 //Record that this is initial set...
1065 graph.nodeAges.addAll(delta.addNodeAges);
1067 for(Map.Entry<AllocNode, Boolean> oldentry:delta.addOldNodes.entrySet()) {
1068 if (oldentry.getValue().booleanValue()) {
1069 graph.oldNodes.put(oldentry.getKey(), Boolean.TRUE);
1073 newdelta=new Delta(null, false);
1074 //merge in heap edges and variables
1075 mergeHeapEdges(graph, delta, newdelta);
1076 mergeVarEdges(graph, delta, newdelta);
1077 mergeAges(graph, delta, newdelta);
1082 /* This function merges in the heap edges. It updates delta to be
1085 void mergeHeapEdges(Graph graph, Delta delta, Delta newdelta) {
1087 for(Map.Entry<AllocNode, MySet<Edge>> heapedge:delta.heapedgeadd.entrySet()) {
1088 AllocNode nsrc=heapedge.getKey();
1089 MySet<Edge> edges=heapedge.getValue();
1091 if (graph.backMap!=null) {
1093 if (!graph.backMap.containsKey(e.dst))
1094 graph.backMap.put(e.dst, new MySet<Edge>());
1095 graph.backMap.get(e.dst).add(e);
1099 if (!graph.nodeMap.containsKey(nsrc)) {
1100 graph.nodeMap.put(nsrc, new MySet<Edge>());
1102 MySet<Edge> dstedges=graph.nodeMap.get(nsrc);
1103 MySet<Edge> diffedges=new MySet<Edge>();
1105 if (!dstedges.contains(e)) {
1106 //We have a new edge
1111 //Done with edge set...
1112 if (diffedges.size()>0) {
1114 newdelta.baseheapedge.put(nsrc, diffedges);
1119 /* This function merges in the var edges. It updates delta to be
1122 void mergeVarEdges(Graph graph, Delta delta, Delta newdelta) {
1124 for(Map.Entry<TempDescriptor, MySet<Edge>> varedge:delta.varedgeadd.entrySet()) {
1125 TempDescriptor tmpsrc=varedge.getKey();
1126 MySet<Edge> edges=varedge.getValue();
1127 if (graph.backMap!=null) {
1129 if (!graph.backMap.containsKey(e.dst))
1130 graph.backMap.put(e.dst, new MySet<Edge>());
1131 graph.backMap.get(e.dst).add(e);
1135 if (!graph.varMap.containsKey(tmpsrc)) {
1136 graph.varMap.put(tmpsrc, new MySet<Edge>());
1138 MySet<Edge> dstedges=graph.varMap.get(tmpsrc);
1139 MySet<Edge> diffedges=new MySet<Edge>();
1141 if (!dstedges.contains(e)) {
1142 //We have a new edge
1147 //Done with edge set...
1148 if (diffedges.size()>=0) {
1150 newdelta.basevaredge.put(tmpsrc,diffedges);
1155 void mergeAges(Graph graph, Delta delta, Delta newDelta) {
1157 for(AllocNode node:delta.addNodeAges) {
1158 if (!graph.nodeAges.contains(node)) {
1159 graph.nodeAges.add(node);
1160 newDelta.baseNodeAges.add(node);
1163 for(Map.Entry<AllocNode, Boolean> oldentry:delta.addOldNodes.entrySet()) {
1164 AllocNode node=oldentry.getKey();
1165 boolean ispresent=oldentry.getValue().booleanValue();
1166 if (ispresent&&!graph.oldNodes.containsKey(node)) {
1167 graph.oldNodes.put(node, Boolean.TRUE);
1168 newDelta.baseOldNodes.put(node, Boolean.TRUE);