1 package Analysis.Pointer;
5 import Analysis.Liveness;
6 import Analysis.Pointer.BasicBlock.BBlock;
7 import Analysis.Pointer.AllocFactory.AllocNode;
8 import Analysis.Disjoint.Alloc;
9 import Analysis.Disjoint.Taint;
10 import Analysis.Disjoint.TaintSet;
11 import Analysis.Disjoint.Canonical;
12 import Analysis.Disjoint.HeapAnalysis;
13 import Analysis.CallGraph.CallGraph;
14 import Analysis.OoOJava.RBlockRelationAnalysis;
15 import Analysis.OoOJava.Accessible;
16 import Analysis.Disjoint.ExistPred;
17 import Analysis.Disjoint.ReachGraph;
18 import Analysis.Disjoint.EffectsAnalysis;
19 import Analysis.Disjoint.BuildStateMachines;
23 public class Pointer implements HeapAnalysis{
24 HashMap<FlatMethod, BasicBlock> blockMap;
25 HashMap<BBlock, Graph> bbgraphMap;
26 HashMap<FlatNode, Graph> graphMap;
27 HashMap<FlatCall, Set<BBlock>> callMap;
28 HashMap<BBlock, Set<PPoint>> returnMap;
29 HashMap<BBlock, Set<TempDescriptor>> bblivetemps;
31 private boolean OoOJava=false;
35 AllocFactory allocFactory;
36 LinkedList<Delta> toprocess;
37 TempDescriptor returntmp;
38 RBlockRelationAnalysis taskAnalysis;
39 EffectsAnalysis effectsAnalysis;
40 Accessible accessible;
42 public Pointer(State state, TypeUtil typeUtil, CallGraph callGraph, RBlockRelationAnalysis taskAnalysis, Liveness liveness) {
43 this(state, typeUtil);
44 this.callGraph=callGraph;
46 this.taskAnalysis=taskAnalysis;
47 this.effectsAnalysis=new EffectsAnalysis();
48 effectsAnalysis.state=state;
49 effectsAnalysis.buildStateMachines=new BuildStateMachines();
50 accessible=new Accessible(state, callGraph, taskAnalysis, liveness);
51 accessible.doAnalysis();
52 State.logEvent("Done Writing Accessible Analysis");
55 public Pointer(State state, TypeUtil typeUtil) {
57 this.blockMap=new HashMap<FlatMethod, BasicBlock>();
58 this.bbgraphMap=new HashMap<BBlock, Graph>();
59 this.bblivetemps=new HashMap<BBlock, Set<TempDescriptor>>();
60 this.graphMap=new HashMap<FlatNode, Graph>();
61 this.callMap=new HashMap<FlatCall, Set<BBlock>>();
62 this.returnMap=new HashMap<BBlock, Set<PPoint>>();
63 this.typeUtil=typeUtil;
64 this.allocFactory=new AllocFactory(state, typeUtil);
65 this.toprocess=new LinkedList<Delta>();
66 ClassDescriptor stringcd=typeUtil.getClass(TypeUtil.ObjectClass);
67 this.returntmp=new TempDescriptor("RETURNVAL", stringcd);
70 public EffectsAnalysis getEffectsAnalysis() {
71 return effectsAnalysis;
74 public BasicBlock getBBlock(FlatMethod fm) {
75 if (!blockMap.containsKey(fm)) {
76 blockMap.put(fm, BasicBlock.getBBlock(fm));
77 Hashtable<FlatNode, Set<TempDescriptor>> livemap=Liveness.computeLiveTemps(fm);
78 for(BBlock bblock:blockMap.get(fm).getBlocks()) {
79 FlatNode fn=bblock.nodes.get(0);
81 HashSet<TempDescriptor> fmset=new HashSet<TempDescriptor>();
82 fmset.addAll((List<TempDescriptor>)Arrays.asList(fm.writesTemps()));
83 bblivetemps.put(bblock, fmset);
85 Set<TempDescriptor> livetemps=livemap.get(fn);
86 bblivetemps.put(bblock, livetemps);
87 livetemps.add(returntmp);
91 return blockMap.get(fm);
94 Delta buildInitialContext() {
95 MethodDescriptor md=typeUtil.getMain();
96 FlatMethod fm=state.getMethodFlat(md);
97 BasicBlock bb=getBBlock(fm);
98 BBlock start=bb.getStart();
99 Delta delta=new Delta(new PPoint(start), true);
100 MySet<Edge> arrayset=new MySet<Edge>();
101 MySet<Edge> varset=new MySet<Edge>();
102 Edge arrayedge=new Edge(allocFactory.StringArray, null, allocFactory.Strings);
103 Edge stringedge=new Edge(fm.getParameter(0), allocFactory.StringArray);
104 delta.addHeapEdge(arrayedge);
105 delta.addVarEdge(stringedge);
111 public Graph getGraph(FlatNode fn) {
112 return graphMap.get(fn);
115 public void doAnalysis() {
117 toprocess.add(buildInitialContext());
119 while(!toprocess.isEmpty()) {
120 Delta delta=toprocess.remove();
121 PPoint ppoint=delta.getBlock();
122 BBlock bblock=ppoint.getBBlock();
123 Vector<FlatNode> nodes=bblock.nodes();
126 if (ppoint.getIndex()==-1) {
127 //Build base graph for entrance to this basic block
128 //System.out.println("Processing "+bblock.nodes.get(0).toString().replace(' ','_'));
130 delta=applyInitDelta(delta, bblock);
131 //System.out.println("Generating:");
134 //System.out.println("Processing Call "+bblock.nodes.get(ppoint.getIndex()).toString().replace(' ','_'));
137 startindex=ppoint.getIndex()+1;
138 delta=applyCallDelta(delta, bblock);
139 //System.out.println("Generating:");
142 Graph graph=bbgraphMap.get(bblock);
143 Graph nodeGraph=null;
144 boolean init=delta.getInit();
145 if (!init&&delta.isEmpty())
148 //Compute delta at exit of each node
149 for(int i=startindex; i<nodes.size();i++) {
150 FlatNode currNode=nodes.get(i);
151 //System.out.println("Start Processing "+currNode);
152 if (!graphMap.containsKey(currNode)) {
153 if (isNEEDED(currNode))
154 graphMap.put(currNode, new Graph(graph));
157 //base graph works for us
158 graphMap.put(currNode, new Graph(graph));
160 //just use previous graph
161 graphMap.put(currNode, graphMap.get(nodes.get(i-1)));
165 nodeGraph=graphMap.get(currNode);
166 delta=processNode(bblock, i, currNode, delta, nodeGraph);
167 //System.out.println("Processing "+currNode+" and generating delta:");
170 generateFinalDelta(bblock, delta, nodeGraph);
176 for(Map.Entry<BBlock, Graph> e:bbgraphMap.entrySet()) {
177 Graph g=e.getValue();
178 plotGraph(g,"BB"+e.getKey().nodes.get(0).toString().replace(' ','_'));
182 for(FlatMethod fm:blockMap.keySet()) {
183 System.out.println(fm.printMethod());
185 for(Map.Entry<FlatNode, Graph> e:graphMap.entrySet()) {
186 FlatNode fn=e.getKey();
187 Graph g=e.getValue();
188 plotGraph(g,"FN"+fn.toString()+debugindex);
193 State.logEvent("Done With Pointer Analysis");
197 effectsAnalysis.buildStateMachines.writeStateMachines();
198 State.logEvent("Done Writing State Machines");
202 void plotGraph(Graph g, String name) {
204 PrintWriter pw=new PrintWriter(new FileWriter(name.toString().replace(' ','_')+".dot"));
205 g.printGraph(pw, name);
207 } catch (Exception ex) {
208 ex.printStackTrace();
213 /* This function builds the last delta for a basic block. It
214 * handles the case for the first time the basic block is
217 void buildInitDelta(Graph graph, Delta newDelta) {
218 //First compute the set of temps
219 HashSet<TempDescriptor> tmpSet=new HashSet<TempDescriptor>();
220 tmpSet.addAll(graph.varMap.keySet());
221 tmpSet.addAll(graph.parent.varMap.keySet());
223 //Next build the temp map part of the delta
224 for(TempDescriptor tmp:tmpSet) {
225 MySet<Edge> edgeSet=new MySet<Edge>();
227 if (graph.varMap.containsKey(tmp))
228 edgeSet.addAll(graph.varMap.get(tmp));
230 edgeSet.addAll(graph.parent.varMap.get(tmp));
231 newDelta.varedgeadd.put(tmp, edgeSet);
234 //Next compute the set of src allocnodes
235 HashSet<AllocNode> nodeSet=new HashSet<AllocNode>();
236 nodeSet.addAll(graph.nodeMap.keySet());
237 nodeSet.addAll(graph.parent.nodeMap.keySet());
239 for(AllocNode node:nodeSet) {
240 MySet<Edge> edgeSet=new MySet<Edge>();
242 if (graph.nodeMap.containsKey(node))
243 edgeSet.addAll(graph.nodeMap.get(node));
245 edgeSet.addAll(graph.parent.nodeMap.get(node));
246 newDelta.heapedgeadd.put(node, edgeSet);
249 if (graph.oldNodes.containsKey(node)) {
251 } else if (graph.parent.oldNodes.containsKey(node)) {
252 //parent graphs only contain true...no need to check
253 newDelta.addOldNodes.put(node, Boolean.TRUE);
257 for(AllocNode node:graph.oldNodes.keySet()) {
258 if (graph.oldNodes.get(node).booleanValue())
259 newDelta.addOldNodes.put(node, Boolean.TRUE);
262 for(AllocNode node:graph.parent.oldNodes.keySet()) {
263 //make sure child doesn't override
264 if (!graph.oldNodes.containsKey(node))
265 newDelta.addOldNodes.put(node, Boolean.TRUE);
268 newDelta.addNodeAges.addAll(graph.nodeAges);
269 newDelta.addNodeAges.addAll(graph.parent.nodeAges);
272 /* This function build the delta for the exit of a basic block. */
274 void generateFinalDelta(BBlock bblock, Delta delta, Graph graph) {
275 Delta newDelta=new Delta(null, false);
276 if (delta.getInit()) {
277 buildInitDelta(graph, newDelta);
279 /* We can break the old delta...it is done being used */
280 /* First we will build variable edges */
281 HashSet<TempDescriptor> tmpSet=new HashSet<TempDescriptor>();
282 tmpSet.addAll(delta.basevaredge.keySet());
283 tmpSet.addAll(delta.varedgeadd.keySet());
284 for(TempDescriptor tmp:tmpSet) {
285 /* Start with the new incoming edges */
286 MySet<Edge> newbaseedge=delta.basevaredge.get(tmp);
287 /* Remove the remove set */
288 if (newbaseedge==null)
289 newbaseedge=new MySet<Edge>();
290 newbaseedge.removeAll(delta.varedgeremove.get(tmp));
291 /* Add in the new set*/
292 newbaseedge.addAll(delta.varedgeadd.get(tmp));
293 /* Store the results */
294 newDelta.varedgeadd.put(tmp, newbaseedge);
296 delta.basevaredge.clear();
298 /* Next we build heap edges */
299 HashSet<AllocNode> nodeSet=new HashSet<AllocNode>();
300 nodeSet.addAll(delta.baseheapedge.keySet());
301 nodeSet.addAll(delta.heapedgeadd.keySet());
302 nodeSet.addAll(delta.heapedgeremove.keySet());
303 for(AllocNode node:nodeSet) {
304 /* Start with the new incoming edges */
305 MySet<Edge> newheapedge=new MySet<Edge>(delta.baseheapedge.get(node));
306 /* Remove the remove set */
307 MySet<Edge> removeset=delta.heapedgeremove.get(node);
310 newheapedge.removeAll(removeset);
312 /* Add in the add set */
313 MySet<Edge> settoadd=delta.heapedgeadd.get(node);
315 newheapedge.addAll(settoadd);
316 newDelta.heapedgeadd.put(node, newheapedge);
318 /* Remove the newly created edges..no need to propagate a diff for those */
319 if (removeset!=null) {
320 removeset.removeAll(delta.baseheapedge.get(node));
321 newDelta.heapedgeremove.put(node, removeset);
325 /* Compute new ages */
326 newDelta.addNodeAges.addAll(delta.baseNodeAges);
327 newDelta.addNodeAges.addAll(delta.addNodeAges);
328 HashSet<AllocNode> oldNodes=new HashSet<AllocNode>();
330 /* Compute whether old nodes survive */
331 oldNodes.addAll(delta.baseOldNodes.keySet());
332 oldNodes.addAll(delta.addOldNodes.keySet());
333 for(AllocNode node:oldNodes) {
334 if (delta.addOldNodes.containsKey(node)) {
335 if (delta.addOldNodes.get(node).booleanValue()) {
336 newDelta.addOldNodes.put(node, Boolean.TRUE);
339 if (delta.baseOldNodes.get(node).booleanValue()) {
340 newDelta.addOldNodes.put(node, Boolean.TRUE);
346 /* Now we need to propagate newdelta */
347 if (!newDelta.heapedgeadd.isEmpty()||!newDelta.heapedgeremove.isEmpty()||!newDelta.varedgeadd.isEmpty()||!newDelta.addNodeAges.isEmpty()||!newDelta.addOldNodes.isEmpty()) {
348 /* We have a delta to propagate */
349 if (returnMap.containsKey(bblock)) {
353 for(PPoint caller:returnMap.get(bblock)) {
354 //System.out.println("Sending Return BBlock to "+caller.getBBlock().nodes.get(caller.getIndex()).toString().replace(' ','_'));
357 newDelta.setBlock(caller);
358 toprocess.add(newDelta);
361 Delta d=newDelta.diffBlock(caller);
367 Vector<BBlock> blockvector=bblock.next();
368 for(int i=0;i<blockvector.size();i++) {
369 //System.out.println("Sending BBlock to "+blockvector.get(i).nodes.get(0).toString().replace(' ','_'));
372 newDelta.setBlock(new PPoint(blockvector.get(i)));
373 toprocess.add(newDelta);
375 Delta d=newDelta.diffBlock(new PPoint(blockvector.get(i)));
381 //System.out.println("EMPTY DELTA");
382 //System.out.println("delta");
384 //System.out.println("newDelta");
389 boolean isNEEDED(FlatNode node) {
390 switch(node.kind()) {
391 case FKind.FlatSetFieldNode: {
392 FlatSetFieldNode n=(FlatSetFieldNode)node;
393 return n.getSrc().getType().isPtr();
395 case FKind.FlatSetElementNode: {
396 FlatSetElementNode n=(FlatSetElementNode)node;
397 return n.getSrc().getType().isPtr();
399 case FKind.FlatFieldNode: {
400 FlatFieldNode n=(FlatFieldNode)node;
401 return n.getDst().getType().isPtr();
403 case FKind.FlatElementNode: {
404 FlatElementNode n=(FlatElementNode)node;
405 return n.getDst().getType().isPtr();
411 Delta processNode(BBlock bblock, int index, FlatNode node, Delta delta, Graph newgraph) {
412 switch(node.kind()) {
414 return processNewNode((FlatNew)node, delta, newgraph);
415 case FKind.FlatFieldNode:
416 case FKind.FlatElementNode:
417 return processFieldElementNode(node, delta, newgraph);
418 case FKind.FlatCastNode:
419 case FKind.FlatOpNode:
420 case FKind.FlatReturnNode:
421 return processCopyNode(node, delta, newgraph);
422 case FKind.FlatSetFieldNode:
423 case FKind.FlatSetElementNode:
424 return processSetFieldElementNode(node, delta, newgraph);
425 case FKind.FlatSESEEnterNode:
426 return processSESEEnterNode((FlatSESEEnterNode) node, delta, newgraph);
427 case FKind.FlatSESEExitNode:
428 return processSESEExitNode((FlatSESEExitNode) node, delta, newgraph);
429 case FKind.FlatMethod:
431 case FKind.FlatBackEdge:
432 case FKind.FlatGenReachNode:
433 return processFlatNop(node, delta, newgraph);
435 return processFlatCall(bblock, index, (FlatCall) node, delta, newgraph);
437 throw new Error("Unrecognized node:"+node);
441 Delta processSESEEnterNode(FlatSESEEnterNode sese, Delta delta, Graph graph) {
443 return processFlatNop(sese, delta, graph);
444 if (delta.getInit()) {
445 removeInitTaints(null, delta, graph);
446 for (TempDescriptor tmp:sese.getInVarSet()) {
447 Taint taint=Taint.factory(sese, null, tmp, AllocFactory.dummyNode, sese, ReachGraph.predsEmpty);
448 MySet<Edge> edges=GraphManip.getEdges(graph, delta, tmp);
450 Edge newe=e.addTaint(taint);
451 delta.addVarEdge(newe);
455 removeDiffTaints(null, delta);
456 for (TempDescriptor tmp:sese.getInVarSet()) {
457 Taint taint=Taint.factory(sese, null, tmp, AllocFactory.dummyNode, sese, ReachGraph.predsEmpty);
458 MySet<Edge> edges=GraphManip.getDiffEdges(delta, tmp);
460 Edge newe=e.addTaint(taint);
461 delta.addVarEdge(newe);
467 applyDiffs(graph, delta);
471 private boolean isRecursive(FlatSESEEnterNode sese) {
472 MethodDescriptor md=sese.getmdEnclosing();
473 boolean isrecursive=callGraph.getCalleeSet(md).contains(md);
477 Delta processSESEExitNode(FlatSESEExitNode seseexit, Delta delta, Graph graph) {
479 return processFlatNop(seseexit, delta, graph);
480 FlatSESEEnterNode sese=seseexit.getFlatEnter();
481 //Strip Taints from this SESE
482 if (delta.getInit()) {
483 removeInitTaints(isRecursive(sese)?null:sese, delta, graph);
485 removeDiffTaints(isRecursive(sese)?null:sese, delta);
487 applyDiffs(graph, delta);
491 void removeDiffTaints(FlatSESEEnterNode sese, Delta delta) {
492 //Start with variable edges
494 MySet<Edge> edgestoadd=new MySet<Edge>();
495 MySet<Edge> edgestoremove=new MySet<Edge>();
497 //Process base diff edges
498 processEdgeMap(sese, delta.basevaredge, null, delta.varedgeremove, edgestoremove, edgestoadd);
499 //Process delta edges
500 processEdgeMap(sese, delta.varedgeadd, null, null, edgestoremove, edgestoadd);
501 for(Edge e:edgestoremove) {
502 delta.removeVarEdge(e);
504 for(Edge e:edgestoadd) {
511 MySet<Edge> edgestoadd=new MySet<Edge>();
512 MySet<Edge> edgestoremove=new MySet<Edge>();
514 //Process base diff edges
515 processEdgeMap(sese, delta.baseheapedge, null, delta.heapedgeremove, edgestoremove, edgestoadd);
516 //Process delta edges
517 processEdgeMap(sese, delta.heapedgeadd, null, null, edgestoremove, edgestoadd);
518 for(Edge e:edgestoremove) {
519 delta.removeHeapEdge(e);
521 for(Edge e:edgestoadd) {
522 delta.addHeapEdge(e);
527 void removeInitTaints(FlatSESEEnterNode sese, Delta delta, Graph graph) {
528 //Start with variable edges
530 MySet<Edge> edgestoadd=new MySet<Edge>();
531 MySet<Edge> edgestoremove=new MySet<Edge>();
533 //Process parent edges
534 processEdgeMap(sese, graph.parent.varMap, graph.varMap, delta.varedgeremove, edgestoremove, edgestoadd);
535 //Process graph edges
536 processEdgeMap(sese, graph.varMap, null, delta.varedgeremove, edgestoremove, edgestoadd);
537 //Process delta edges
538 processEdgeMap(sese, delta.varedgeadd, null, null, edgestoremove, edgestoadd);
539 for(Edge e:edgestoremove) {
540 delta.removeVarEdge(e);
542 for(Edge e:edgestoadd) {
549 MySet<Edge> edgestoadd=new MySet<Edge>();
550 MySet<Edge> edgestoremove=new MySet<Edge>();
552 //Process parent edges
553 processEdgeMap(sese, graph.parent.nodeMap, graph.nodeMap, delta.heapedgeremove, edgestoremove, edgestoadd);
554 //Process graph edges
555 processEdgeMap(sese, graph.nodeMap, null, delta.heapedgeremove, edgestoremove, edgestoadd);
556 //Process delta edges
557 processEdgeMap(sese, delta.heapedgeadd, null, null, edgestoremove, edgestoadd);
558 for(Edge e:edgestoremove) {
559 delta.removeHeapEdge(e);
561 for(Edge e:edgestoadd) {
562 delta.addHeapEdge(e);
567 void processEdgeMap(FlatSESEEnterNode sese, HashMap<?, MySet<Edge>> edgemap, HashMap<?, MySet<Edge>> childmap, HashMap<?, MySet<Edge>> removemap, MySet<Edge> edgestoremove, MySet<Edge> edgestoadd) {
568 for(Map.Entry<?, MySet<Edge>> entry:edgemap.entrySet()) {
569 //If the parent map exists and overrides this entry, skip it
570 if (childmap!=null&&childmap.containsKey(entry.getKey()))
572 for(Edge e:entry.getValue()) {
573 //check whether this edge has been removed
574 if (removemap!=null&&removemap.containsKey(entry.getKey())&&
575 removemap.get(entry.getKey()).contains(e))
578 TaintSet ts=e.getTaints();
580 //update non-null taint set
582 newts=Canonical.removeInContextTaintsNP(ts, sese);
584 edgestoremove.add(e);
585 edgestoadd.add(e.changeTaintSet(newts));
591 /* This function compute the edges for the this variable for a
592 * callee if it exists. */
594 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) {
595 //Handle the this temp
597 MySet<Edge> edges=(oldnodeset!=null)?GraphManip.getDiffEdges(delta, tmpthis):GraphManip.getEdges(graph, delta, tmpthis);
598 newDelta.varedgeadd.put(tmpthis, (MySet<Edge>) edges.clone());
599 edgeset.addAll(edges);
601 AllocNode dstnode=e.dst;
602 if (!nodeset.contains(dstnode)&&(oldnodeset==null||!oldnodeset.contains(dstnode))) {
603 TypeDescriptor type=dstnode.getType();
604 if (!type.isArray()) {
605 targetSet.add(type.getClassDesc());
607 //arrays don't have code
608 targetSet.add(typeUtil.getClass(TypeUtil.ObjectClass));
610 nodeset.add(dstnode);
611 tovisit.add(dstnode);
617 /* This function compute the edges for a call's parameters. */
619 void processParams(Graph graph, Delta delta, Delta newDelta, HashSet<AllocNode> nodeset, Stack<AllocNode> tovisit, MySet<Edge> edgeset, FlatCall fcall, boolean diff) {
620 //Go through each temp
621 for(int i=0;i<fcall.numArgs();i++) {
622 TempDescriptor tmp=fcall.getArg(i);
623 MySet<Edge> edges=diff?GraphManip.getDiffEdges(delta, tmp):GraphManip.getEdges(graph, delta, tmp);
624 newDelta.varedgeadd.put(tmp, (MySet<Edge>) edges.clone());
625 edgeset.addAll(edges);
627 if (!nodeset.contains(e.dst)) {
635 /* This function computes the reachable nodes for a callee. */
637 void computeReachableNodes(Graph graph, Delta delta, Delta newDelta, HashSet<AllocNode> nodeset, Stack<AllocNode> tovisit, MySet<Edge> edgeset, HashSet<AllocNode> oldnodeset) {
638 while(!tovisit.isEmpty()) {
639 AllocNode node=tovisit.pop();
640 MySet<Edge> edges=GraphManip.getEdges(graph, delta, node);
641 if (!edges.isEmpty()) {
642 newDelta.heapedgeadd.put(node, edges);
643 edgeset.addAll(edges);
645 if (!nodeset.contains(e.dst)&&(oldnodeset==null||!oldnodeset.contains(e.dst))) {
654 HashSet<MethodDescriptor> computeTargets(FlatCall fcall, Delta newDelta) {
655 TempDescriptor tmpthis=fcall.getThis();
656 MethodDescriptor md=fcall.getMethod();
657 HashSet<MethodDescriptor> targets=new HashSet<MethodDescriptor>();
662 for(Edge e:newDelta.varedgeadd.get(tmpthis)) {
663 AllocNode node=e.dst;
664 ClassDescriptor cd=node.getType().getClassDesc();
665 //Figure out exact method called and add to set
666 MethodDescriptor calledmd=cd.getCalledMethod(md);
667 targets.add(calledmd);
673 void fixMapping(FlatCall fcall, HashSet<MethodDescriptor> targets, MySet<Edge> oldedgeset, Delta newDelta, BBlock callblock, int callindex) {
674 Delta basedelta=null;
675 TempDescriptor tmpthis=fcall.getThis();
677 for(MethodDescriptor calledmd:targets) {
678 FlatMethod fm=state.getMethodFlat(calledmd);
679 boolean newmethod=false;
682 HashMap<TempDescriptor, TempDescriptor> tmpMap=new HashMap<TempDescriptor, TempDescriptor>();
685 tmpMap.put(tmpthis, fm.getParameter(offset++));
687 for(int i=0;i<fcall.numArgs();i++) {
688 TempDescriptor tmp=fcall.getArg(i);
689 tmpMap.put(tmp,fm.getParameter(i+offset));
692 //Get basicblock for the method
693 BasicBlock block=getBBlock(fm);
696 if (!callMap.containsKey(fcall)) {
697 callMap.put(fcall, new HashSet<BBlock>());
700 Delta returnDelta=null;
701 if (!callMap.get(fcall).contains(block.getStart())) {
702 callMap.get(fcall).add(block.getStart());
706 if (!returnMap.containsKey(block.getExit())) {
707 returnMap.put(block.getExit(), new HashSet<PPoint>());
709 returnMap.get(block.getExit()).add(new PPoint(callblock, callindex));
711 if (bbgraphMap.containsKey(block.getExit())) {
712 //Need to push existing results to current node
713 if (returnDelta==null) {
714 returnDelta=new Delta(null, false);
715 Vector<FlatNode> exitblocknodes=block.getExit().nodes();
716 FlatExit fexit=(FlatExit)exitblocknodes.get(exitblocknodes.size()-1);
717 buildInitDelta(graphMap.get(fexit), returnDelta);
718 if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
719 returnDelta.setBlock(new PPoint(callblock, callindex));
720 toprocess.add(returnDelta);
723 if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
724 toprocess.add(returnDelta.diffBlock(new PPoint(callblock, callindex)));
730 if (oldedgeset==null) {
731 //First build of this graph
732 //Build and enqueue delta...safe to just use existing delta
733 Delta d=newDelta.changeParams(tmpMap, new PPoint(block.getStart()));
734 //System.out.println("AProcessing "+block.getStart().nodes.get(0).toString().replace(' ','_'));
737 } else if (newmethod) {
738 if (basedelta==null) {
739 basedelta=newDelta.buildBase(oldedgeset);
741 //Build and enqueue delta
742 Delta d=basedelta.changeParams(tmpMap, new PPoint(block.getStart()));
743 //System.out.println("BProcessing "+block.getStart().nodes.get(0).toString().replace(' ','_'));
747 //Build and enqueue delta
748 Delta d=newDelta.changeParams(tmpMap, new PPoint(block.getStart()));
749 //System.out.println("CProcessing "+block.getStart().nodes.get(0).toString().replace(' ','_'));
757 /* This function computes all edges that start outside of the callee
758 * context and go into the callee context */
760 void computeExternalEdges(Graph graph, Delta delta, HashSet<AllocNode> nodeset, HashSet<AllocNode> deltaset, MySet<Edge> externaledgeset) {
761 //Do heap edges first
762 HashSet<AllocNode> externalnodes=new HashSet<AllocNode>();
763 externalnodes.addAll(delta.baseheapedge.keySet());
764 externalnodes.addAll(delta.heapedgeadd.keySet());
765 externalnodes.addAll(delta.heapedgeremove.keySet());
766 //remove allinternal nodes
767 externalnodes.removeAll(nodeset);
768 for(AllocNode extNode:externalnodes) {
769 //Compute set of edges from given node
770 MySet<Edge> edges=new MySet<Edge>(delta.baseheapedge.get(extNode));
771 edges.removeAll(delta.heapedgeremove.get(extNode));
772 edges.addAll(delta.heapedgeadd.get(extNode));
775 if (nodeset.contains(e.dst))
776 externaledgeset.add(e);
781 HashSet<TempDescriptor> temps=new HashSet<TempDescriptor>();
782 temps.addAll(delta.basevaredge.keySet());
783 temps.addAll(delta.varedgeadd.keySet());
784 temps.addAll(delta.varedgeremove.keySet());
785 //remove allinternal nodes
786 temps.removeAll(nodeset);
788 for(TempDescriptor tmp:temps) {
789 //Compute set of edges from given node
790 MySet<Edge> edges=new MySet<Edge>(delta.basevaredge.get(tmp));
792 edges.removeAll(delta.varedgeremove.get(tmp));
793 edges.addAll(delta.varedgeadd.get(tmp));
796 if (nodeset.contains(e.dst))
797 externaledgeset.add(e);
802 /* This function removes the caller reachable edges from the
805 void removeEdges(Graph graph, Delta delta, HashSet<AllocNode> nodeset, MySet<Edge> edgeset, MySet<Edge> externaledgeset) {
806 //Want to remove the set of internal edges
807 for(Edge e:edgeset) {
808 if (e.src!=null&&!graph.callerEdges.contains(e)) {
809 delta.removeHeapEdge(e);
813 //Want to remove the set of external edges
814 for(Edge e:externaledgeset) {
815 //want to remove the set of internal edges
816 if (!graph.callerEdges.contains(e))
821 Delta processFlatCall(BBlock callblock, int callindex, FlatCall fcall, Delta delta, Graph graph) {
822 Delta newDelta=new Delta(null, false);
824 if (delta.getInit()) {
825 MySet<Edge> edgeset=new MySet<Edge>();
826 MySet<Edge> externaledgeset=new MySet<Edge>();
827 HashSet<AllocNode> nodeset=new HashSet<AllocNode>();
828 HashSet<ClassDescriptor> targetSet=new HashSet<ClassDescriptor>();
829 Stack<AllocNode> tovisit=new Stack<AllocNode>();
830 TempDescriptor tmpthis=fcall.getThis();
831 graph.callerEdges=new MySet<Edge>();
833 //Handle the this temp
834 processThisTargets(targetSet, graph, delta, newDelta, nodeset, tovisit, edgeset, tmpthis, null);
836 //Go through each temp
837 processParams(graph, delta, newDelta, nodeset, tovisit, edgeset, fcall, false);
839 //Traverse all reachable nodes
840 computeReachableNodes(graph, delta, newDelta, nodeset, tovisit, edgeset, null);
842 //Compute call targets
843 HashSet<MethodDescriptor> newtargets=computeTargets(fcall, newDelta);
846 fixMapping(fcall, newtargets, null, newDelta, callblock, callindex);
848 //Compute edges into region to splice out
849 computeExternalEdges(graph, delta, nodeset, null, externaledgeset);
851 //Splice out internal edges
852 removeEdges(graph, delta, nodeset, edgeset, externaledgeset);
854 //store data structures
855 graph.externalEdgeSet=externaledgeset;
856 graph.reachNode=nodeset;
857 graph.reachEdge=edgeset;
859 graph.callTargets=newtargets;
860 graph.callNodeAges=new HashSet<AllocNode>();
861 graph.callOldNodes=new HashSet<AllocNode>();
863 //Apply diffs to graph
864 applyDiffs(graph, delta, true);
866 MySet<Edge> edgeset=new MySet<Edge>();
867 MySet<Edge> externaledgeset=new MySet<Edge>();
868 HashSet<AllocNode> nodeset=new HashSet<AllocNode>();
869 MySet<Edge> oldedgeset=graph.reachEdge;
870 HashSet<AllocNode> oldnodeset=graph.reachNode;
872 HashSet<ClassDescriptor> targetSet=new HashSet<ClassDescriptor>();
873 Stack<AllocNode> tovisit=new Stack<AllocNode>();
874 TempDescriptor tmpthis=fcall.getThis();
875 //Fix up delta to get rid of unnecessary heap edge removals
876 for(Map.Entry<AllocNode, MySet<Edge>> entry:delta.heapedgeremove.entrySet()) {
877 for(Iterator<Edge> eit=entry.getValue().iterator();eit.hasNext();) {
879 if (graph.callerEdges.contains(e))
884 //Fix up delta to get rid of unnecessary var edge removals
885 for(Map.Entry<TempDescriptor, MySet<Edge>> entry:delta.varedgeremove.entrySet()) {
886 for(Iterator<Edge> eit=entry.getValue().iterator();eit.hasNext();) {
888 if (graph.callerEdges.contains(e))
893 //Handle the this temp
894 processThisTargets(targetSet, graph, delta, newDelta, nodeset, tovisit, edgeset, tmpthis, oldnodeset);
896 //Go through each temp
897 processParams(graph, delta, newDelta, nodeset, tovisit, edgeset, fcall, true);
898 //Go through each new heap edge that starts from old node
899 MySet<Edge> newedges=GraphManip.getDiffEdges(delta, oldnodeset);
900 edgeset.addAll(newedges);
901 for(Edge e:newedges) {
902 //Add new edges that start from old node to newDelta
904 if (!newDelta.heapedgeadd.containsKey(src)) {
905 newDelta.heapedgeadd.put(src, new MySet<Edge>());
907 newDelta.heapedgeadd.get(src).add(e);
908 if (!nodeset.contains(e.dst)&&!oldnodeset.contains(e.dst)) {
914 //Traverse all reachable nodes
915 computeReachableNodes(graph, delta, newDelta, nodeset, tovisit, edgeset, oldnodeset);
916 //Compute call targets
917 HashSet<MethodDescriptor> newtargets=computeTargets(fcall, newDelta);
918 graph.callTargets.addAll(newtargets);
919 //add in new nodeset and edgeset
920 oldnodeset.addAll(nodeset);
921 oldedgeset.addAll(edgeset);
923 fixMapping(fcall, graph.callTargets, oldedgeset, newDelta, callblock, callindex);
924 //Compute edges into region to splice out
925 computeExternalEdges(graph, delta, oldnodeset, nodeset, externaledgeset);
927 //Splice out internal edges
928 removeEdges(graph, delta, nodeset, edgeset, externaledgeset);
930 //Add external edges back in
931 processCallExternal(graph, delta, externaledgeset);
933 //Move new edges that should be summarized
934 processSummarization(graph, delta);
936 //Add in new external edges
937 graph.externalEdgeSet.addAll(externaledgeset);
938 //Apply diffs to graph
939 applyDiffs(graph, delta);
944 void processSummarization(Graph graph, Delta delta) {
945 processSumHeapEdgeSet(delta.heapedgeadd, delta, graph);
946 processSumHeapEdgeSet(delta.baseheapedge, delta, graph);
947 processSumVarEdgeSet(delta.varedgeadd, delta, graph);
948 processSumVarEdgeSet(delta.basevaredge, delta, graph);
951 void processSumVarEdgeSet(HashMap<TempDescriptor, MySet<Edge>> map, Delta delta, Graph graph) {
952 MySet<Edge> edgestoadd=new MySet<Edge>();
953 MySet<Edge> edgestoremove=new MySet<Edge>();
954 for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> eit=map.entrySet().iterator();eit.hasNext();) {
955 Map.Entry<TempDescriptor, MySet<Edge>> entry=eit.next();
956 MySet<Edge> edgeset=entry.getValue();
958 for(Edge e:edgeset) {
960 boolean rewrite=false;
961 if (copy.dst!=null&&graph.callNodeAges.contains(copy.dst)) {
962 copy.dst=allocFactory.getAllocNode(copy.dst, true);
966 edgestoremove.add(e);
967 edgestoadd.add(copy);
971 for(Edge e:edgestoremove) {
972 delta.removeVarEdge(e);
974 for(Edge e:edgestoadd) {
979 public Alloc getAllocationSiteFromFlatNew(FlatNew node) {
980 return allocFactory.getAllocNode(node, false);
983 void processSumHeapEdgeSet(HashMap<AllocNode, MySet<Edge>> map, Delta delta, Graph graph) {
984 MySet<Edge> edgestoadd=new MySet<Edge>();
985 MySet<Edge> edgestoremove=new MySet<Edge>();
986 for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> eit=map.entrySet().iterator();eit.hasNext();) {
987 Map.Entry<AllocNode, MySet<Edge>> entry=eit.next();
988 AllocNode node=entry.getKey();
989 MySet<Edge> edgeset=entry.getValue();
991 for(Edge e:edgeset) {
993 boolean rewrite=false;
994 if (copy.src!=null&&graph.callNodeAges.contains(copy.src)) {
995 copy.src=allocFactory.getAllocNode(copy.src, true);
998 if (copy.dst!=null&&graph.callNodeAges.contains(copy.dst)) {
999 copy.dst=allocFactory.getAllocNode(copy.dst, true);
1003 edgestoremove.add(e);
1004 edgestoadd.add(copy);
1008 for(Edge e:edgestoremove) {
1009 delta.removeHeapEdge(e);
1011 for(Edge e:edgestoadd) {
1012 delta.addHeapEdge(e);
1016 //Handle external edges
1017 void processCallExternal(Graph graph, Delta newDelta, MySet<Edge> externalEdgeSet) {
1018 //Add external edges in
1019 for(Edge e:externalEdgeSet) {
1020 //First did we age the source
1021 Edge newedge=e.copy();
1022 if (newedge.src!=null&&!e.src.isSummary()&&graph.callNodeAges.contains(e.src)) {
1023 AllocNode summaryNode=allocFactory.getAllocNode(newedge.src, true);
1024 newedge.src=summaryNode;
1027 if (graph.callNodeAges.contains(e.dst)&&!e.dst.isSummary()) {
1028 if (graph.callOldNodes.contains(e.dst)) {
1030 Edge copy=newedge.copy();
1031 mergeEdge(graph, newDelta, copy);
1033 //Now add summarized node
1034 newedge.dst=allocFactory.getAllocNode(newedge.dst, true);
1035 mergeCallEdge(graph, newDelta, newedge);
1037 //Add edge to single node
1038 mergeEdge(graph, newDelta, newedge);
1043 /* This function applies callee deltas to the caller heap. */
1045 Delta applyCallDelta(Delta delta, BBlock bblock) {
1046 Delta newDelta=new Delta(null, false);
1047 Vector<FlatNode> nodes=bblock.nodes();
1048 PPoint ppoint=delta.getBlock();
1049 FlatCall fcall=(FlatCall)nodes.get(ppoint.getIndex());
1050 Graph graph=graphMap.get(fcall);
1051 Graph oldgraph=(ppoint.getIndex()==0)?
1052 bbgraphMap.get(bblock):
1053 graphMap.get(nodes.get(ppoint.getIndex()-1));
1054 Set<FlatSESEEnterNode> seseCallers=OoOJava?taskAnalysis.getTransitiveExecutingRBlocks(fcall):null;
1056 //Age outside nodes if necessary
1057 for(Iterator<AllocNode> nodeit=delta.addNodeAges.iterator();nodeit.hasNext();) {
1058 AllocNode node=nodeit.next();
1059 if (!graph.callNodeAges.contains(node)) {
1060 graph.callNodeAges.add(node);
1061 newDelta.addNodeAges.add(node);
1063 if (!graph.reachNode.contains(node)&&!node.isSummary()) {
1064 /* Need to age node in existing graph*/
1065 summarizeInGraph(graph, newDelta, node);
1069 for(Map.Entry<AllocNode, MySet<Edge>> entry:delta.heapedgeadd.entrySet()) {
1070 for(Edge e:entry.getValue()) {
1071 boolean addedge=false;
1072 Edge edgetoadd=null;
1073 if (e.statuspredicate==Edge.NEW) {
1076 Edge origEdgeKey=e.makeStatus(allocFactory);
1077 if (oldgraph.nodeMap.containsKey(origEdgeKey.src)&&
1078 oldgraph.nodeMap.get(origEdgeKey.src).contains(origEdgeKey)) {
1079 Edge origEdge=oldgraph.nodeMap.get(origEdgeKey.src).get(origEdgeKey);
1080 //copy the predicate
1081 origEdgeKey.statuspredicate=origEdge.statuspredicate;
1082 edgetoadd=origEdgeKey;
1085 if (seseCallers!=null&&edgetoadd!=null)
1086 edgetoadd.taintModify(seseCallers);
1087 mergeCallEdge(graph, newDelta, edgetoadd);
1091 processCallExternal(graph, newDelta, graph.externalEdgeSet);
1093 //Add edge for return value
1094 if (fcall.getReturnTemp()!=null) {
1095 MySet<Edge> returnedge=delta.varedgeadd.get(returntmp);
1096 if (returnedge!=null)
1097 for(Edge e:returnedge) {
1098 Edge newedge=e.copy();
1099 newedge.srcvar=fcall.getReturnTemp();
1100 if (seseCallers!=null)
1101 newedge.taintModify(seseCallers);
1102 if (graph.getEdges(fcall.getReturnTemp())==null||!graph.getEdges(fcall.getReturnTemp()).contains(newedge))
1103 newDelta.addEdge(newedge);
1106 applyDiffs(graph, newDelta);
1110 public void mergeEdge(Graph graph, Delta newDelta, Edge edgetoadd) {
1111 if (edgetoadd!=null) {
1112 Edge match=graph.getMatch(edgetoadd);
1114 if (match==null||!match.subsumes(edgetoadd)) {
1115 Edge mergededge=edgetoadd.merge(match);
1116 newDelta.addEdge(mergededge);
1121 /* This is a call produced edge...need to remember this */
1123 public void mergeCallEdge(Graph graph, Delta newDelta, Edge edgetoadd) {
1124 if (edgetoadd!=null) {
1125 Edge match=graph.getMatch(edgetoadd);
1127 if (match==null||!match.subsumes(edgetoadd)) {
1128 Edge mergededge=edgetoadd.merge(match);
1129 newDelta.addEdge(mergededge);
1130 graph.callerEdges.add(mergededge);
1131 //System.out.println("ADDING: "+ mergededge);
1137 /* Summarizes out of context nodes in graph */
1138 void summarizeInGraph(Graph graph, Delta newDelta, AllocNode singleNode) {
1139 AllocNode summaryNode=allocFactory.getAllocNode(singleNode, true);
1141 //Handle outgoing heap edges
1142 MySet<Edge> edgeset=graph.getEdges(singleNode);
1144 for(Edge e:edgeset) {
1145 Edge rewrite=e.rewrite(singleNode, summaryNode);
1147 newDelta.removeHeapEdge(e);
1148 mergeCallEdge(graph, newDelta, rewrite);
1151 //Handle incoming edges
1152 MySet<Edge> backedges=graph.getBackEdges(singleNode);
1153 for(Edge e:backedges) {
1154 if (e.dst==singleNode) {
1155 //Need to get original edge so that predicate will be correct
1156 Edge match=graph.getMatch(e);
1158 Edge rewrite=match.rewrite(singleNode, summaryNode);
1159 newDelta.removeEdge(match);
1160 mergeCallEdge(graph, newDelta, rewrite);
1166 void applyDiffs(Graph graph, Delta delta) {
1167 applyDiffs(graph, delta, false);
1170 void applyDiffs(Graph graph, Delta delta, boolean genbackwards) {
1171 //build backwards map if requested
1172 if (genbackwards&&graph.backMap==null) {
1173 graph.backMap=new HashMap<AllocNode, MySet<Edge>>();
1174 if (graph.parent.backMap==null) {
1175 graph.parent.backMap=new HashMap<AllocNode, MySet<Edge>>();
1176 for(Map.Entry<AllocNode, MySet<Edge>> entry:graph.nodeMap.entrySet()) {
1177 for(Edge e:entry.getValue()) {
1178 if (!graph.parent.backMap.containsKey(e.dst))
1179 graph.parent.backMap.put(e.dst, new MySet<Edge>());
1180 graph.parent.backMap.get(e.dst).add(e);
1183 for(Map.Entry<TempDescriptor, MySet<Edge>> entry:graph.varMap.entrySet()) {
1184 for(Edge e:entry.getValue()) {
1185 if (!graph.parent.backMap.containsKey(e.dst))
1186 graph.parent.backMap.put(e.dst, new MySet<Edge>());
1187 graph.parent.backMap.get(e.dst).add(e);
1193 //Add hidden base edges
1194 for(Map.Entry<AllocNode, MySet<Edge>> e: delta.baseheapedge.entrySet()) {
1195 AllocNode node=e.getKey();
1196 MySet<Edge> edges=e.getValue();
1197 if (graph.nodeMap.containsKey(node)) {
1198 MySet<Edge> nodeEdges=graph.nodeMap.get(node);
1199 nodeEdges.addAll(edges);
1204 for(Map.Entry<AllocNode, MySet<Edge>> e: delta.heapedgeremove.entrySet()) {
1205 AllocNode node=e.getKey();
1206 MySet<Edge> edgestoremove=e.getValue();
1207 if (graph.nodeMap.containsKey(node)) {
1208 //Just apply diff to current map
1209 graph.nodeMap.get(node).removeAll(edgestoremove);
1211 //Generate diff from parent graph
1212 MySet<Edge> parentedges=graph.parent.nodeMap.get(node);
1213 if (parentedges!=null) {
1214 MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
1215 graph.nodeMap.put(node, newedgeset);
1221 for(Map.Entry<AllocNode, MySet<Edge>> e: delta.heapedgeadd.entrySet()) {
1222 AllocNode node=e.getKey();
1223 MySet<Edge> edgestoadd=e.getValue();
1224 //If we have not done a subtract, then
1225 if (!graph.nodeMap.containsKey(node)) {
1226 //Copy the parent entry
1227 if (graph.parent.nodeMap.containsKey(node))
1228 graph.nodeMap.put(node, (MySet<Edge>)graph.parent.nodeMap.get(node).clone());
1230 graph.nodeMap.put(node, new MySet<Edge>());
1232 Edge.mergeEdgesInto(graph.nodeMap.get(node),edgestoadd);
1234 for(Edge eadd:edgestoadd) {
1235 if (!graph.backMap.containsKey(eadd.dst))
1236 graph.backMap.put(eadd.dst, new MySet<Edge>());
1237 graph.backMap.get(eadd.dst).add(eadd);
1243 for(Map.Entry<TempDescriptor, MySet<Edge>> e: delta.varedgeremove.entrySet()) {
1244 TempDescriptor tmp=e.getKey();
1245 MySet<Edge> edgestoremove=e.getValue();
1247 if (graph.varMap.containsKey(tmp)) {
1248 //Just apply diff to current map
1249 graph.varMap.get(tmp).removeAll(edgestoremove);
1250 } else if (graph.parent.varMap.containsKey(tmp)) {
1251 //Generate diff from parent graph
1252 MySet<Edge> parentedges=graph.parent.varMap.get(tmp);
1253 MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
1254 graph.varMap.put(tmp, newedgeset);
1259 for(Map.Entry<TempDescriptor, MySet<Edge>> e: delta.varedgeadd.entrySet()) {
1260 TempDescriptor tmp=e.getKey();
1261 MySet<Edge> edgestoadd=e.getValue();
1262 if (graph.varMap.containsKey(tmp)) {
1263 Edge.mergeEdgesInto(graph.varMap.get(tmp), edgestoadd);
1265 graph.varMap.put(tmp, (MySet<Edge>) edgestoadd.clone());
1267 for(Edge eadd:edgestoadd) {
1268 if (!graph.backMap.containsKey(eadd.dst))
1269 graph.backMap.put(eadd.dst, new MySet<Edge>());
1270 graph.backMap.get(eadd.dst).add(eadd);
1275 //Add node additions
1276 for(AllocNode node:delta.addNodeAges) {
1277 graph.nodeAges.add(node);
1280 for(Map.Entry<AllocNode, Boolean> nodeentry:delta.addOldNodes.entrySet()) {
1281 AllocNode node=nodeentry.getKey();
1282 Boolean ispresent=nodeentry.getValue();
1283 graph.oldNodes.put(node, ispresent);
1287 Delta processSetFieldElementNode(FlatNode node, Delta delta, Graph graph) {
1291 if (node.kind()==FKind.FlatSetElementNode) {
1292 FlatSetElementNode fen=(FlatSetElementNode) node;
1297 FlatSetFieldNode ffn=(FlatSetFieldNode) node;
1302 //Do nothing for non pointers
1303 if (!src.getType().isPtr())
1306 if (delta.getInit()) {
1307 MySet<Edge> srcEdges=GraphManip.getEdges(graph, delta, src);
1308 MySet<Edge> dstEdges=GraphManip.getEdges(graph, delta, dst);
1310 if (OoOJava&&!accessible.isAccessible(node, src)) {
1311 Taint srcStallTaint=Taint.factory(node, src, AllocFactory.dummyNode, node, ReachGraph.predsEmpty);
1312 srcEdges=Edge.taintAll(srcEdges, srcStallTaint);
1313 updateVarDelta(graph, delta, src, srcEdges, null);
1316 if (OoOJava&&!accessible.isAccessible(node, dst)) {
1317 Taint dstStallTaint=Taint.factory(node, dst, AllocFactory.dummyNode, node, ReachGraph.predsEmpty);
1318 dstEdges=Edge.taintAll(dstEdges, dstStallTaint);
1319 updateVarDelta(graph, delta, dst, dstEdges, null);
1322 MySet<Edge> edgesToAdd=GraphManip.genEdges(dstEdges, fd, srcEdges);
1323 MySet<Edge> edgesToRemove=null;
1324 if (dstEdges.size()==1&&!dstEdges.iterator().next().dst.isSummary()&&fd!=null) {
1325 /* Can do a strong update */
1326 edgesToRemove=GraphManip.getEdges(graph, delta, dstEdges, fd);
1327 graph.strongUpdateSet=edgesToRemove;
1329 graph.strongUpdateSet=new MySet<Edge>();
1332 effectsAnalysis.analyzeFlatSetFieldNode(dstEdges, fd, node);
1336 updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
1337 applyDiffs(graph, delta);
1339 /* First look at new sources */
1340 MySet<Edge> edgesToAdd=new MySet<Edge>();
1341 MySet<Edge> newSrcEdges=GraphManip.getDiffEdges(delta, src);
1342 MySet<Edge> srcEdges=GraphManip.getEdges(graph, delta, src);
1343 HashSet<AllocNode> dstNodes=GraphManip.getNodes(graph, delta, dst);
1344 MySet<Edge> newDstEdges=GraphManip.getDiffEdges(delta, dst);
1346 if (OoOJava&&!accessible.isAccessible(node, src)) {
1347 Taint srcStallTaint=Taint.factory(node, src, AllocFactory.dummyNode, node, ReachGraph.predsEmpty);
1348 newSrcEdges=Edge.taintAll(newSrcEdges, srcStallTaint);
1349 updateVarDelta(graph, delta, src, newSrcEdges, null);
1352 if (OoOJava&&!accessible.isAccessible(node, dst)) {
1353 Taint dstStallTaint=Taint.factory(node, dst, AllocFactory.dummyNode, node, ReachGraph.predsEmpty);
1354 newDstEdges=Edge.taintAll(newDstEdges, dstStallTaint);
1355 updateVarDelta(graph, delta, dst, newDstEdges, null);
1359 effectsAnalysis.analyzeFlatSetFieldNode(newDstEdges, fd, node);
1362 MySet<Edge> edgesToRemove=null;
1363 if (newDstEdges.size()!=0) {
1364 if (dstNodes.size()>1&&!dstNodes.iterator().next().isSummary()&&fd!=null) {
1365 /* Need to undo strong update */
1366 if (graph.strongUpdateSet!=null) {
1367 edgesToAdd.addAll(graph.strongUpdateSet);
1368 graph.strongUpdateSet=null; //Prevent future strong updates
1370 } else if (dstNodes.size()==1&&newDstEdges.size()==1&&!newDstEdges.iterator().next().dst.isSummary()&&graph.strongUpdateSet!=null&&fd!=null) {
1371 edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
1372 graph.strongUpdateSet.addAll(edgesToRemove);
1374 Edge.mergeEdgesInto(edgesToAdd, GraphManip.genEdges(newDstEdges, fd, srcEdges));
1378 if (graph.strongUpdateSet!=null&&fd!=null) {
1379 MySet<Edge> otherEdgesToRemove=GraphManip.getDiffEdges(delta, dstNodes, fd);
1380 if (edgesToRemove!=null)
1381 edgesToRemove.addAll(otherEdgesToRemove);
1383 edgesToRemove=otherEdgesToRemove;
1384 graph.strongUpdateSet.addAll(otherEdgesToRemove);
1387 //Next look at new destinations
1388 Edge.mergeEdgesInto(edgesToAdd, GraphManip.genEdges(dstNodes, fd, newSrcEdges));
1391 updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
1392 applyDiffs(graph, delta);
1397 Delta processCopyNode(FlatNode node, Delta delta, Graph graph) {
1400 if (node.kind()==FKind.FlatOpNode) {
1401 FlatOpNode fon=(FlatOpNode) node;
1404 } else if (node.kind()==FKind.FlatReturnNode) {
1405 FlatReturnNode frn=(FlatReturnNode)node;
1406 src=frn.getReturnTemp();
1408 if (src==null||!src.getType().isPtr()) {
1410 applyDiffs(graph, delta);
1414 FlatCastNode fcn=(FlatCastNode) node;
1418 if (delta.getInit()) {
1419 MySet<Edge> srcedges=GraphManip.getEdges(graph, delta, src);
1420 MySet<Edge> edgesToAdd=GraphManip.genEdges(dst, srcedges);
1421 MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
1422 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1423 applyDiffs(graph, delta);
1425 /* First compute new src nodes */
1426 MySet<Edge> newSrcEdges=GraphManip.getDiffEdges(delta, src);
1428 /* Compute the union, and then the set of edges */
1429 MySet<Edge> edgesToAdd=GraphManip.genEdges(dst, newSrcEdges);
1431 /* Compute set of edges to remove */
1432 MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);
1435 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1436 applyDiffs(graph, delta);
1441 Delta processFieldElementNode(FlatNode node, Delta delta, Graph graph) {
1445 TaintSet taint=null;
1447 if (node.kind()==FKind.FlatElementNode) {
1448 FlatElementNode fen=(FlatElementNode) node;
1453 FlatFieldNode ffn=(FlatFieldNode) node;
1458 if (OoOJava&&!accessible.isAccessible(node, src)) {
1459 taint=TaintSet.factory(Taint.factory(node, src, AllocFactory.dummyNode, node, ReachGraph.predsEmpty));
1462 //Do nothing for non pointers
1463 if (!dst.getType().isPtr())
1465 if (delta.getInit()) {
1466 MySet<Edge> srcedges=GraphManip.getEdges(graph, delta, src);
1467 MySet<Edge> edgesToAdd=GraphManip.dereference(graph, delta, dst, srcedges, fd, node, taint);
1468 MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
1470 effectsAnalysis.analyzeFlatFieldNode(srcedges, fd, node);
1472 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1473 applyDiffs(graph, delta);
1475 /* First compute new objects we read fields of */
1476 MySet<Edge> allsrcedges=GraphManip.getEdges(graph, delta, src);
1477 MySet<Edge> edgesToAdd=GraphManip.diffDereference(delta, dst, allsrcedges, fd, node, taint);
1478 /* Next compute new targets of fields */
1479 MySet<Edge> newsrcedges=GraphManip.getDiffEdges(delta, src);
1480 MySet<Edge> newfdedges=GraphManip.dereference(graph, delta, dst, newsrcedges, fd, node, taint);
1482 /* Compute the union, and then the set of edges */
1483 Edge.mergeEdgesInto(edgesToAdd, newfdedges);
1485 /* Compute set of edges to remove */
1486 MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);
1489 effectsAnalysis.analyzeFlatFieldNode(newsrcedges, fd, node);
1492 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1493 applyDiffs(graph, delta);
1499 void updateVarDelta(Graph graph, Delta delta, TempDescriptor tmp, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
1500 MySet<Edge> edgeAdd=delta.varedgeadd.get(tmp);
1501 MySet<Edge> edgeRemove=delta.varedgeremove.get(tmp);
1502 MySet<Edge> existingEdges=graph.getEdges(tmp);
1503 if (edgestoRemove!=null)
1504 for(Edge e: edgestoRemove) {
1505 //remove edge from delta
1508 //if the edge is already in the graph, add an explicit remove to the delta
1509 if (existingEdges.contains(e))
1510 delta.removeVarEdge(e);
1512 for(Edge e: edgestoAdd) {
1513 //Remove the edge from the remove set
1514 if (edgeRemove!=null)
1515 edgeRemove.remove(e);
1516 //Explicitly add it to the add set unless it is already in the graph
1517 if (typeUtil.isSuperorType(tmp.getType(), e.dst.getType())) {
1518 if (!existingEdges.contains(e)) {
1519 delta.addVarEdge(e);
1521 //See if the old edge subsumes the new one
1522 Edge olde=existingEdges.get(e);
1523 if (!olde.subsumes(e)) {
1524 delta.addVarEdge(olde.merge(e));
1531 void updateHeapDelta(Graph graph, Delta delta, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
1532 if (edgestoRemove!=null)
1533 for(Edge e: edgestoRemove) {
1534 AllocNode src=e.src;
1535 MySet<Edge> edgeAdd=delta.heapedgeadd.get(src);
1536 MySet<Edge> existingEdges=graph.getEdges(src);
1537 //remove edge from delta
1540 //if the edge is already in the graph, add an explicit remove to the delta
1541 if (existingEdges.contains(e)) {
1542 delta.removeHeapEdge(e);
1545 if (edgestoAdd!=null)
1546 for(Edge e: edgestoAdd) {
1547 AllocNode src=e.src;
1548 MySet<Edge> edgeRemove=delta.heapedgeremove.get(src);
1549 MySet<Edge> existingEdges=graph.getEdges(src);
1550 //Remove the edge from the remove set
1551 if (edgeRemove!=null)
1552 edgeRemove.remove(e);
1553 //Explicitly add it to the add set unless it is already in the graph
1554 if (!existingEdges.contains(e)) {
1555 delta.addHeapEdge(e);
1557 //See if the old edge subsumes the new one
1558 Edge olde=existingEdges.get(e);
1559 if (!olde.subsumes(e)) {
1560 delta.addHeapEdge(olde.merge(e));
1566 Delta processFlatNop(FlatNode node, Delta delta, Graph graph) {
1567 applyDiffs(graph, delta);
1571 Delta processNewNode(FlatNew node, Delta delta, Graph graph) {
1572 AllocNode summary=allocFactory.getAllocNode(node, true);
1573 AllocNode single=allocFactory.getAllocNode(node, false);
1574 TempDescriptor tmp=node.getDst();
1576 if (delta.getInit()) {
1577 /* We don't have to deal with summarization here... The
1578 * intuition is that this is the only place where we generate
1579 * nodes for this allocation site and this is the first time
1580 * we've analyzed this site */
1583 Edge e=new Edge(tmp, single);
1584 //Build new Edge set
1585 MySet<Edge> newedges=new MySet<Edge>();
1587 //Add it into the diffs
1588 delta.varedgeadd.put(tmp, newedges);
1589 //Remove the old edges
1590 MySet<Edge> oldedges=graph.getEdges(tmp);
1591 if (!oldedges.isEmpty())
1592 delta.varedgeremove.put(tmp, (MySet<Edge>) oldedges);
1593 //Apply incoming diffs to graph
1594 applyDiffs(graph, delta);
1595 //Note that we create a single node
1596 delta.addNodeAges.add(single);
1598 if (delta.addOldNodes.containsKey(single)||delta.baseOldNodes.containsKey(single)) {
1599 delta.addOldNodes.put(single, Boolean.FALSE);
1602 /* 1. Fix up the variable edge additions */
1604 for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.varedgeadd.entrySet().iterator();entryIt.hasNext();) {
1605 Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
1607 if (entry.getKey()==tmp) {
1608 /* Check if this is the tmp we overwrite */
1611 /* Otherwise, check if the target of the edge is changed... */
1612 summarizeSet(entry.getValue(), graph.varMap.get(entry.getKey()), single, summary);
1616 /* 2. Fix up the base variable edges */
1618 for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.basevaredge.entrySet().iterator();entryIt.hasNext();) {
1619 Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
1620 TempDescriptor entrytmp=entry.getKey();
1621 if (entrytmp==tmp) {
1622 /* Check is this is the tmp we overwrite, if so add to remove set */
1623 Util.relationUpdate(delta.varedgeremove, tmp, null, entry.getValue());
1625 /* Check if the target of the edge is changed */
1626 MySet<Edge> newset=(MySet<Edge>)entry.getValue().clone();
1627 MySet<Edge> removeset=shrinkSet(newset, graph.varMap.get(entrytmp), single, summary);
1628 Util.relationUpdate(delta.varedgeremove, entrytmp, newset, removeset);
1629 Util.relationUpdate(delta.varedgeadd, entrytmp, null, newset);
1634 /* 3. Fix up heap edge additions */
1636 HashMap<AllocNode, MySet<Edge>> addheapedge=new HashMap<AllocNode, MySet<Edge>>();
1637 for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.heapedgeadd.entrySet().iterator();entryIt.hasNext();) {
1638 Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
1639 MySet<Edge> edgeset=entry.getValue();
1640 AllocNode allocnode=entry.getKey();
1641 if (allocnode==single) {
1643 summarizeSet(edgeset, graph.nodeMap.get(summary), single, summary);
1644 addheapedge.put(summary, edgeset);
1646 summarizeSet(edgeset, graph.nodeMap.get(allocnode), single, summary);
1650 /* Merge in diffs */
1652 for(Map.Entry<AllocNode, MySet<Edge>> entry:addheapedge.entrySet()) {
1653 AllocNode allocnode=entry.getKey();
1654 Util.relationUpdate(delta.heapedgeadd, allocnode, null, entry.getValue());
1657 /* 4. Fix up the base heap edges */
1659 for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.baseheapedge.entrySet().iterator();entryIt.hasNext();) {
1660 Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
1661 MySet<Edge> edgeset=entry.getValue();
1662 AllocNode allocnode=entry.getKey();
1663 if (allocnode==single) {
1666 AllocNode addnode=(allocnode==single)?summary:allocnode;
1668 MySet<Edge> newset=(MySet<Edge>) edgeset.clone();
1669 MySet<Edge> removeset=shrinkSet(newset, graph.nodeMap.get(addnode), single, summary);
1670 Util.relationUpdate(delta.heapedgeadd, addnode, null, newset);
1671 Util.relationUpdate(delta.heapedgeremove, allocnode, null, removeset);
1674 /* Update Node Ages...If the base or addNodeAges set contains a
1675 * single node, it now should also contain a summary node... No
1676 * need to generate a single node as that has already been
1678 if (delta.baseNodeAges.contains(single)||delta.addNodeAges.contains(single)) {
1679 delta.addNodeAges.add(summary);
1682 //Kill the old node if someone tries to add it
1683 if (delta.addOldNodes.containsKey(single)||delta.baseOldNodes.containsKey(single)) {
1684 delta.addOldNodes.put(single, Boolean.FALSE);
1687 //Apply incoming diffs to graph
1688 applyDiffs(graph, delta);
1693 /* This function builds a new edge set where oldnode is summarized into new node */
1695 void summarizeSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode sumnode) {
1696 MySet<Edge> newSet=null;
1697 for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
1698 Edge e=edgeit.next();
1699 if (e.dst==oldnode||e.src==oldnode) {
1701 newSet=new MySet<Edge>();
1706 if (e.dst==oldnode) {
1709 if (e.src==oldnode) {
1712 if (oldedgeset==null||!oldedgeset.contains(e))
1717 edgeset.addAll(newSet);
1720 /* Shrinks the incoming set to just include rewritten values.
1721 * Returns a set of the original rewritten values */
1723 MySet<Edge> shrinkSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode newnode) {
1724 MySet<Edge> newSet=null;
1725 MySet<Edge> removeSet=null;
1726 for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
1727 Edge e=edgeit.next();
1729 if (e.dst==oldnode||e.src==oldnode) {
1731 newSet=new MySet<Edge>();
1732 removeSet=new MySet<Edge>();
1741 if (oldedgeset==null||!oldedgeset.contains(e))
1746 edgeset.addAll(newSet);
1750 /* This function returns a completely new Delta... It is safe to
1753 Delta applyInitDelta(Delta delta, BBlock block) {
1754 //Apply delta to graph
1755 boolean newGraph=false;
1756 if (!bbgraphMap.containsKey(block)) {
1757 bbgraphMap.put(block, new Graph(null));
1760 Graph graph=bbgraphMap.get(block);
1763 Delta newdelta=new Delta(null, true);
1764 //Add in heap edges and throw away original diff
1766 for(Map.Entry<AllocNode, MySet<Edge>> entry:delta.heapedgeadd.entrySet()) {
1767 graph.nodeMap.put(entry.getKey(), new MySet<Edge>(entry.getValue()));
1769 //Add in var edges and throw away original diff
1770 Set<TempDescriptor> livetemps=bblivetemps.get(block);
1772 for(Map.Entry<TempDescriptor, MySet<Edge>> entry:delta.varedgeadd.entrySet()) {
1773 if (livetemps.contains(entry.getKey()))
1774 graph.varMap.put(entry.getKey(), new MySet<Edge>(entry.getValue()));
1776 //Record that this is initial set...
1777 graph.nodeAges.addAll(delta.addNodeAges);
1779 for(Map.Entry<AllocNode, Boolean> oldentry:delta.addOldNodes.entrySet()) {
1780 if (oldentry.getValue().booleanValue()) {
1781 graph.oldNodes.put(oldentry.getKey(), Boolean.TRUE);
1786 Delta newdelta=new Delta(null, false);
1787 //merge in heap edges and variables
1788 mergeHeapEdges(graph, delta, newdelta);
1789 mergeVarEdges(graph, delta, newdelta, block);
1790 mergeAges(graph, delta, newdelta);
1795 /* This function merges in the heap edges. It updates delta to be
1798 void mergeHeapEdges(Graph graph, Delta delta, Delta newdelta) {
1800 for(Map.Entry<AllocNode, MySet<Edge>> heapedge:delta.heapedgeadd.entrySet()) {
1801 AllocNode nsrc=heapedge.getKey();
1802 MySet<Edge> edges=heapedge.getValue();
1804 if (graph.backMap!=null) {
1806 if (!graph.backMap.containsKey(e.dst))
1807 graph.backMap.put(e.dst, new MySet<Edge>());
1808 graph.backMap.get(e.dst).add(e);
1812 if (!graph.nodeMap.containsKey(nsrc)) {
1813 graph.nodeMap.put(nsrc, new MySet<Edge>());
1815 MySet<Edge> dstedges=graph.nodeMap.get(nsrc);
1816 MySet<Edge> diffedges=new MySet<Edge>();
1818 if (!dstedges.contains(e)) {
1819 //We have a new edge
1823 Edge origedge=dstedges.get(e);
1824 if (!origedge.subsumes(e)) {
1825 Edge mergededge=origedge.merge(e);
1826 diffedges.add(mergededge);
1827 dstedges.add(mergededge);
1831 //Done with edge set...
1832 if (diffedges.size()>0) {
1834 newdelta.baseheapedge.put(nsrc, diffedges);
1839 /* This function merges in the var edges. It updates delta to be
1842 void mergeVarEdges(Graph graph, Delta delta, Delta newdelta, BBlock block) {
1844 Set<TempDescriptor> livetemps=bblivetemps.get(block);
1846 for(Map.Entry<TempDescriptor, MySet<Edge>> varedge:delta.varedgeadd.entrySet()) {
1847 TempDescriptor tmpsrc=varedge.getKey();
1848 if (livetemps.contains(tmpsrc)) {
1849 MySet<Edge> edges=varedge.getValue();
1850 if (graph.backMap!=null) {
1852 if (!graph.backMap.containsKey(e.dst))
1853 graph.backMap.put(e.dst, new MySet<Edge>());
1854 graph.backMap.get(e.dst).add(e);
1858 if (!graph.varMap.containsKey(tmpsrc)) {
1859 graph.varMap.put(tmpsrc, new MySet<Edge>());
1861 MySet<Edge> dstedges=graph.varMap.get(tmpsrc);
1862 MySet<Edge> diffedges=new MySet<Edge>();
1864 if (!dstedges.contains(e)) {
1865 //We have a new edge
1869 Edge origedge=dstedges.get(e);
1870 if (!origedge.subsumes(e)) {
1871 Edge mergededge=origedge.merge(e);
1872 diffedges.add(mergededge);
1873 dstedges.add(mergededge);
1877 //Done with edge set...
1878 if (diffedges.size()>0) {
1880 newdelta.basevaredge.put(tmpsrc,diffedges);
1886 void mergeAges(Graph graph, Delta delta, Delta newDelta) {
1888 for(AllocNode node:delta.addNodeAges) {
1889 if (!graph.nodeAges.contains(node)) {
1890 graph.nodeAges.add(node);
1891 newDelta.baseNodeAges.add(node);
1894 for(Map.Entry<AllocNode, Boolean> oldentry:delta.addOldNodes.entrySet()) {
1895 AllocNode node=oldentry.getKey();
1896 boolean ispresent=oldentry.getValue().booleanValue();
1897 if (ispresent&&!graph.oldNodes.containsKey(node)) {
1898 graph.oldNodes.put(node, Boolean.TRUE);
1899 newDelta.baseOldNodes.put(node, Boolean.TRUE);