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.dummySite, 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.dummySite, 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).getAllocSite();
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;
1303 if (delta.getInit()) {
1304 MySet<Edge> dstEdges=GraphManip.getEdges(graph, delta, dst);
1306 if (OoOJava&&!accessible.isAccessible(node, dst)) {
1307 Taint dstStallTaint=Taint.factory(node, dst, AllocFactory.dummySite, node, ReachGraph.predsEmpty);
1308 dstEdges=Edge.taintAll(dstEdges, dstStallTaint);
1309 updateVarDelta(graph, delta, dst, dstEdges, null);
1312 effectsAnalysis.analyzeFlatSetFieldNode(dstEdges, fd, node);
1315 //Do nothing for non pointers
1316 if (!src.getType().isPtr()) {
1320 MySet<Edge> srcEdges=GraphManip.getEdges(graph, delta, src);
1321 if (OoOJava&&!accessible.isAccessible(node, src)) {
1322 Taint srcStallTaint=Taint.factory(node, src, AllocFactory.dummySite, node, ReachGraph.predsEmpty);
1323 srcEdges=Edge.taintAll(srcEdges, srcStallTaint);
1324 updateVarDelta(graph, delta, src, srcEdges, null);
1327 MySet<Edge> edgesToAdd=GraphManip.genEdges(dstEdges, fd, srcEdges);
1328 MySet<Edge> edgesToRemove=null;
1329 if (dstEdges.size()==1&&!dstEdges.iterator().next().dst.isSummary()&&fd!=null) {
1330 /* Can do a strong update */
1331 edgesToRemove=GraphManip.getEdges(graph, delta, dstEdges, fd);
1332 graph.strongUpdateSet=edgesToRemove;
1334 graph.strongUpdateSet=new MySet<Edge>();
1337 updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
1338 applyDiffs(graph, delta);
1340 MySet<Edge> newDstEdges=GraphManip.getDiffEdges(delta, dst);
1342 if (OoOJava&&!accessible.isAccessible(node, dst)) {
1343 Taint dstStallTaint=Taint.factory(node, dst, AllocFactory.dummySite, node, ReachGraph.predsEmpty);
1344 newDstEdges=Edge.taintAll(newDstEdges, dstStallTaint);
1345 updateVarDelta(graph, delta, dst, newDstEdges, null);
1349 effectsAnalysis.analyzeFlatSetFieldNode(newDstEdges, fd, node);
1352 if (!src.getType().isPtr()) {
1356 /* Next look at new sources */
1358 MySet<Edge> edgesToAdd=new MySet<Edge>();
1359 MySet<Edge> newSrcEdges=GraphManip.getDiffEdges(delta, src);
1360 MySet<Edge> srcEdges=GraphManip.getEdges(graph, delta, src);
1361 HashSet<AllocNode> dstNodes=GraphManip.getNodes(graph, delta, dst);
1363 if (OoOJava&&!accessible.isAccessible(node, src)) {
1364 Taint srcStallTaint=Taint.factory(node, src, AllocFactory.dummySite, node, ReachGraph.predsEmpty);
1365 newSrcEdges=Edge.taintAll(newSrcEdges, srcStallTaint);
1366 updateVarDelta(graph, delta, src, newSrcEdges, null);
1369 MySet<Edge> edgesToRemove=null;
1370 if (newDstEdges.size()!=0) {
1371 if (dstNodes.size()>1&&!dstNodes.iterator().next().isSummary()&&fd!=null) {
1372 /* Need to undo strong update */
1373 if (graph.strongUpdateSet!=null) {
1374 edgesToAdd.addAll(graph.strongUpdateSet);
1375 graph.strongUpdateSet=null; //Prevent future strong updates
1377 } else if (dstNodes.size()==1&&newDstEdges.size()==1&&!newDstEdges.iterator().next().dst.isSummary()&&graph.strongUpdateSet!=null&&fd!=null) {
1378 edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
1379 graph.strongUpdateSet.addAll(edgesToRemove);
1381 Edge.mergeEdgesInto(edgesToAdd, GraphManip.genEdges(newDstEdges, fd, srcEdges));
1385 if (graph.strongUpdateSet!=null&&fd!=null) {
1386 MySet<Edge> otherEdgesToRemove=GraphManip.getDiffEdges(delta, dstNodes, fd);
1387 if (edgesToRemove!=null)
1388 edgesToRemove.addAll(otherEdgesToRemove);
1390 edgesToRemove=otherEdgesToRemove;
1391 graph.strongUpdateSet.addAll(otherEdgesToRemove);
1394 //Next look at new destinations
1395 Edge.mergeEdgesInto(edgesToAdd, GraphManip.genEdges(dstNodes, fd, newSrcEdges));
1398 updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
1399 applyDiffs(graph, delta);
1404 Delta processCopyNode(FlatNode node, Delta delta, Graph graph) {
1407 if (node.kind()==FKind.FlatOpNode) {
1408 FlatOpNode fon=(FlatOpNode) node;
1411 } else if (node.kind()==FKind.FlatReturnNode) {
1412 FlatReturnNode frn=(FlatReturnNode)node;
1413 src=frn.getReturnTemp();
1415 if (src==null||!src.getType().isPtr()) {
1417 applyDiffs(graph, delta);
1421 FlatCastNode fcn=(FlatCastNode) node;
1425 if (delta.getInit()) {
1426 MySet<Edge> srcedges=GraphManip.getEdges(graph, delta, src);
1427 MySet<Edge> edgesToAdd=GraphManip.genEdges(dst, srcedges);
1428 MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
1429 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1430 applyDiffs(graph, delta);
1432 /* First compute new src nodes */
1433 MySet<Edge> newSrcEdges=GraphManip.getDiffEdges(delta, src);
1435 /* Compute the union, and then the set of edges */
1436 MySet<Edge> edgesToAdd=GraphManip.genEdges(dst, newSrcEdges);
1438 /* Compute set of edges to remove */
1439 MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);
1442 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1443 applyDiffs(graph, delta);
1448 Delta processFieldElementNode(FlatNode node, Delta delta, Graph graph) {
1452 TaintSet taint=null;
1454 if (node.kind()==FKind.FlatElementNode) {
1455 FlatElementNode fen=(FlatElementNode) node;
1460 FlatFieldNode ffn=(FlatFieldNode) node;
1465 if (OoOJava&&!accessible.isAccessible(node, src)) {
1466 taint=TaintSet.factory(Taint.factory(node, src, AllocFactory.dummySite, node, ReachGraph.predsEmpty));
1469 //Do nothing for non pointers
1470 if (delta.getInit()) {
1471 MySet<Edge> srcedges=GraphManip.getEdges(graph, delta, src);
1474 srcedges=Edge.taintAll(srcedges, taint);
1475 updateVarDelta(graph, delta, src, srcedges, null);
1477 effectsAnalysis.analyzeFlatFieldNode(srcedges, fd, node);
1479 if (!dst.getType().isPtr())
1482 MySet<Edge> edgesToAdd=GraphManip.dereference(graph, delta, dst, srcedges, fd, node);
1483 MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
1485 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1486 applyDiffs(graph, delta);
1488 MySet<Edge> newsrcedges=GraphManip.getDiffEdges(delta, src);
1491 newsrcedges=Edge.taintAll(newsrcedges, taint);
1492 updateVarDelta(graph, delta, src, newsrcedges, null);
1494 effectsAnalysis.analyzeFlatFieldNode(newsrcedges, fd, node);
1496 if (!dst.getType().isPtr())
1499 /* First compute new objects we read fields of */
1500 MySet<Edge> allsrcedges=GraphManip.getEdges(graph, delta, src);
1501 MySet<Edge> edgesToAdd=GraphManip.diffDereference(delta, dst, allsrcedges, fd, node);
1502 /* Next compute new targets of fields */
1503 MySet<Edge> newfdedges=GraphManip.dereference(graph, delta, dst, newsrcedges, fd, node);
1505 /* Compute the union, and then the set of edges */
1506 Edge.mergeEdgesInto(edgesToAdd, newfdedges);
1508 /* Compute set of edges to remove */
1509 MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);
1513 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1514 applyDiffs(graph, delta);
1520 void updateVarDelta(Graph graph, Delta delta, TempDescriptor tmp, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
1521 MySet<Edge> edgeAdd=delta.varedgeadd.get(tmp);
1522 MySet<Edge> edgeRemove=delta.varedgeremove.get(tmp);
1523 MySet<Edge> existingEdges=graph.getEdges(tmp);
1524 if (edgestoRemove!=null)
1525 for(Edge e: edgestoRemove) {
1526 //remove edge from delta
1529 //if the edge is already in the graph, add an explicit remove to the delta
1530 if (existingEdges.contains(e))
1531 delta.removeVarEdge(e);
1533 for(Edge e: edgestoAdd) {
1534 //Remove the edge from the remove set
1535 if (edgeRemove!=null)
1536 edgeRemove.remove(e);
1537 //Explicitly add it to the add set unless it is already in the graph
1538 if (typeUtil.isSuperorType(tmp.getType(), e.dst.getType())) {
1539 if (!existingEdges.contains(e)) {
1540 delta.addVarEdge(e);
1542 //See if the old edge subsumes the new one
1543 Edge olde=existingEdges.get(e);
1544 if (!olde.subsumes(e)) {
1545 delta.addVarEdge(olde.merge(e));
1552 void updateHeapDelta(Graph graph, Delta delta, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
1553 if (edgestoRemove!=null)
1554 for(Edge e: edgestoRemove) {
1555 AllocNode src=e.src;
1556 MySet<Edge> edgeAdd=delta.heapedgeadd.get(src);
1557 MySet<Edge> existingEdges=graph.getEdges(src);
1558 //remove edge from delta
1561 //if the edge is already in the graph, add an explicit remove to the delta
1562 if (existingEdges.contains(e)) {
1563 delta.removeHeapEdge(e);
1566 if (edgestoAdd!=null)
1567 for(Edge e: edgestoAdd) {
1568 AllocNode src=e.src;
1569 MySet<Edge> edgeRemove=delta.heapedgeremove.get(src);
1570 MySet<Edge> existingEdges=graph.getEdges(src);
1571 //Remove the edge from the remove set
1572 if (edgeRemove!=null)
1573 edgeRemove.remove(e);
1574 //Explicitly add it to the add set unless it is already in the graph
1575 if (!existingEdges.contains(e)) {
1576 delta.addHeapEdge(e);
1578 //See if the old edge subsumes the new one
1579 Edge olde=existingEdges.get(e);
1580 if (!olde.subsumes(e)) {
1581 delta.addHeapEdge(olde.merge(e));
1587 Delta processFlatNop(FlatNode node, Delta delta, Graph graph) {
1588 applyDiffs(graph, delta);
1592 Delta processNewNode(FlatNew node, Delta delta, Graph graph) {
1593 AllocNode summary=allocFactory.getAllocNode(node, true);
1594 AllocNode single=allocFactory.getAllocNode(node, false);
1595 TempDescriptor tmp=node.getDst();
1597 if (delta.getInit()) {
1598 /* We don't have to deal with summarization here... The
1599 * intuition is that this is the only place where we generate
1600 * nodes for this allocation site and this is the first time
1601 * we've analyzed this site */
1604 Edge e=new Edge(tmp, single);
1605 //Build new Edge set
1606 MySet<Edge> newedges=new MySet<Edge>();
1608 //Add it into the diffs
1609 delta.varedgeadd.put(tmp, newedges);
1610 //Remove the old edges
1611 MySet<Edge> oldedges=graph.getEdges(tmp);
1612 if (!oldedges.isEmpty())
1613 delta.varedgeremove.put(tmp, (MySet<Edge>) oldedges);
1614 //Apply incoming diffs to graph
1615 applyDiffs(graph, delta);
1616 //Note that we create a single node
1617 delta.addNodeAges.add(single);
1619 if (delta.addOldNodes.containsKey(single)||delta.baseOldNodes.containsKey(single)) {
1620 delta.addOldNodes.put(single, Boolean.FALSE);
1623 /* 1. Fix up the variable edge additions */
1625 for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.varedgeadd.entrySet().iterator();entryIt.hasNext();) {
1626 Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
1628 if (entry.getKey()==tmp) {
1629 /* Check if this is the tmp we overwrite */
1632 /* Otherwise, check if the target of the edge is changed... */
1633 summarizeSet(entry.getValue(), graph.varMap.get(entry.getKey()), single, summary);
1637 /* 2. Fix up the base variable edges */
1639 for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.basevaredge.entrySet().iterator();entryIt.hasNext();) {
1640 Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
1641 TempDescriptor entrytmp=entry.getKey();
1642 if (entrytmp==tmp) {
1643 /* Check is this is the tmp we overwrite, if so add to remove set */
1644 Util.relationUpdate(delta.varedgeremove, tmp, null, entry.getValue());
1646 /* Check if the target of the edge is changed */
1647 MySet<Edge> newset=(MySet<Edge>)entry.getValue().clone();
1648 MySet<Edge> removeset=shrinkSet(newset, graph.varMap.get(entrytmp), single, summary);
1649 Util.relationUpdate(delta.varedgeremove, entrytmp, newset, removeset);
1650 Util.relationUpdate(delta.varedgeadd, entrytmp, null, newset);
1655 /* 3. Fix up heap edge additions */
1657 HashMap<AllocNode, MySet<Edge>> addheapedge=new HashMap<AllocNode, MySet<Edge>>();
1658 for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.heapedgeadd.entrySet().iterator();entryIt.hasNext();) {
1659 Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
1660 MySet<Edge> edgeset=entry.getValue();
1661 AllocNode allocnode=entry.getKey();
1662 if (allocnode==single) {
1664 summarizeSet(edgeset, graph.nodeMap.get(summary), single, summary);
1665 addheapedge.put(summary, edgeset);
1667 summarizeSet(edgeset, graph.nodeMap.get(allocnode), single, summary);
1671 /* Merge in diffs */
1673 for(Map.Entry<AllocNode, MySet<Edge>> entry:addheapedge.entrySet()) {
1674 AllocNode allocnode=entry.getKey();
1675 Util.relationUpdate(delta.heapedgeadd, allocnode, null, entry.getValue());
1678 /* 4. Fix up the base heap edges */
1680 for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.baseheapedge.entrySet().iterator();entryIt.hasNext();) {
1681 Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
1682 MySet<Edge> edgeset=entry.getValue();
1683 AllocNode allocnode=entry.getKey();
1684 if (allocnode==single) {
1687 AllocNode addnode=(allocnode==single)?summary:allocnode;
1689 MySet<Edge> newset=(MySet<Edge>) edgeset.clone();
1690 MySet<Edge> removeset=shrinkSet(newset, graph.nodeMap.get(addnode), single, summary);
1691 Util.relationUpdate(delta.heapedgeadd, addnode, null, newset);
1692 Util.relationUpdate(delta.heapedgeremove, allocnode, null, removeset);
1695 /* Update Node Ages...If the base or addNodeAges set contains a
1696 * single node, it now should also contain a summary node... No
1697 * need to generate a single node as that has already been
1699 if (delta.baseNodeAges.contains(single)||delta.addNodeAges.contains(single)) {
1700 delta.addNodeAges.add(summary);
1703 //Kill the old node if someone tries to add it
1704 if (delta.addOldNodes.containsKey(single)||delta.baseOldNodes.containsKey(single)) {
1705 delta.addOldNodes.put(single, Boolean.FALSE);
1708 //Apply incoming diffs to graph
1709 applyDiffs(graph, delta);
1714 /* This function builds a new edge set where oldnode is summarized into new node */
1716 void summarizeSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode sumnode) {
1717 MySet<Edge> newSet=null;
1718 for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
1719 Edge e=edgeit.next();
1720 if (e.dst==oldnode||e.src==oldnode) {
1722 newSet=new MySet<Edge>();
1727 if (e.dst==oldnode) {
1730 if (e.src==oldnode) {
1733 if (oldedgeset==null||!oldedgeset.contains(e))
1738 edgeset.addAll(newSet);
1741 /* Shrinks the incoming set to just include rewritten values.
1742 * Returns a set of the original rewritten values */
1744 MySet<Edge> shrinkSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode newnode) {
1745 MySet<Edge> newSet=null;
1746 MySet<Edge> removeSet=null;
1747 for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
1748 Edge e=edgeit.next();
1750 if (e.dst==oldnode||e.src==oldnode) {
1752 newSet=new MySet<Edge>();
1753 removeSet=new MySet<Edge>();
1762 if (oldedgeset==null||!oldedgeset.contains(e))
1767 edgeset.addAll(newSet);
1771 /* This function returns a completely new Delta... It is safe to
1774 Delta applyInitDelta(Delta delta, BBlock block) {
1775 //Apply delta to graph
1776 boolean newGraph=false;
1777 if (!bbgraphMap.containsKey(block)) {
1778 bbgraphMap.put(block, new Graph(null));
1781 Graph graph=bbgraphMap.get(block);
1784 Delta newdelta=new Delta(null, true);
1785 //Add in heap edges and throw away original diff
1787 for(Map.Entry<AllocNode, MySet<Edge>> entry:delta.heapedgeadd.entrySet()) {
1788 graph.nodeMap.put(entry.getKey(), new MySet<Edge>(entry.getValue()));
1790 //Add in var edges and throw away original diff
1791 Set<TempDescriptor> livetemps=bblivetemps.get(block);
1793 for(Map.Entry<TempDescriptor, MySet<Edge>> entry:delta.varedgeadd.entrySet()) {
1794 if (livetemps.contains(entry.getKey()))
1795 graph.varMap.put(entry.getKey(), new MySet<Edge>(entry.getValue()));
1797 //Record that this is initial set...
1798 graph.nodeAges.addAll(delta.addNodeAges);
1800 for(Map.Entry<AllocNode, Boolean> oldentry:delta.addOldNodes.entrySet()) {
1801 if (oldentry.getValue().booleanValue()) {
1802 graph.oldNodes.put(oldentry.getKey(), Boolean.TRUE);
1807 Delta newdelta=new Delta(null, false);
1808 //merge in heap edges and variables
1809 mergeHeapEdges(graph, delta, newdelta);
1810 mergeVarEdges(graph, delta, newdelta, block);
1811 mergeAges(graph, delta, newdelta);
1816 /* This function merges in the heap edges. It updates delta to be
1819 void mergeHeapEdges(Graph graph, Delta delta, Delta newdelta) {
1821 for(Map.Entry<AllocNode, MySet<Edge>> heapedge:delta.heapedgeadd.entrySet()) {
1822 AllocNode nsrc=heapedge.getKey();
1823 MySet<Edge> edges=heapedge.getValue();
1825 if (graph.backMap!=null) {
1827 if (!graph.backMap.containsKey(e.dst))
1828 graph.backMap.put(e.dst, new MySet<Edge>());
1829 graph.backMap.get(e.dst).add(e);
1833 if (!graph.nodeMap.containsKey(nsrc)) {
1834 graph.nodeMap.put(nsrc, new MySet<Edge>());
1836 MySet<Edge> dstedges=graph.nodeMap.get(nsrc);
1837 MySet<Edge> diffedges=new MySet<Edge>();
1839 if (!dstedges.contains(e)) {
1840 //We have a new edge
1844 Edge origedge=dstedges.get(e);
1845 if (!origedge.subsumes(e)) {
1846 Edge mergededge=origedge.merge(e);
1847 diffedges.add(mergededge);
1848 dstedges.add(mergededge);
1852 //Done with edge set...
1853 if (diffedges.size()>0) {
1855 newdelta.baseheapedge.put(nsrc, diffedges);
1860 /* This function merges in the var edges. It updates delta to be
1863 void mergeVarEdges(Graph graph, Delta delta, Delta newdelta, BBlock block) {
1865 Set<TempDescriptor> livetemps=bblivetemps.get(block);
1867 for(Map.Entry<TempDescriptor, MySet<Edge>> varedge:delta.varedgeadd.entrySet()) {
1868 TempDescriptor tmpsrc=varedge.getKey();
1869 if (livetemps.contains(tmpsrc)) {
1870 MySet<Edge> edges=varedge.getValue();
1871 if (graph.backMap!=null) {
1873 if (!graph.backMap.containsKey(e.dst))
1874 graph.backMap.put(e.dst, new MySet<Edge>());
1875 graph.backMap.get(e.dst).add(e);
1879 if (!graph.varMap.containsKey(tmpsrc)) {
1880 graph.varMap.put(tmpsrc, new MySet<Edge>());
1882 MySet<Edge> dstedges=graph.varMap.get(tmpsrc);
1883 MySet<Edge> diffedges=new MySet<Edge>();
1885 if (!dstedges.contains(e)) {
1886 //We have a new edge
1890 Edge origedge=dstedges.get(e);
1891 if (!origedge.subsumes(e)) {
1892 Edge mergededge=origedge.merge(e);
1893 diffedges.add(mergededge);
1894 dstedges.add(mergededge);
1898 //Done with edge set...
1899 if (diffedges.size()>0) {
1901 newdelta.basevaredge.put(tmpsrc,diffedges);
1907 void mergeAges(Graph graph, Delta delta, Delta newDelta) {
1909 for(AllocNode node:delta.addNodeAges) {
1910 if (!graph.nodeAges.contains(node)) {
1911 graph.nodeAges.add(node);
1912 newDelta.baseNodeAges.add(node);
1915 for(Map.Entry<AllocNode, Boolean> oldentry:delta.addOldNodes.entrySet()) {
1916 AllocNode node=oldentry.getKey();
1917 boolean ispresent=oldentry.getValue().booleanValue();
1918 if (ispresent&&!graph.oldNodes.containsKey(node)) {
1919 graph.oldNodes.put(node, Boolean.TRUE);
1920 newDelta.baseOldNodes.put(node, Boolean.TRUE);