1 package Analysis.Pointer;
5 import Analysis.Liveness;
6 import Analysis.Pointer.BasicBlock.BBlock;
7 import Analysis.Pointer.AllocFactory.AllocNode;
8 import Analysis.Disjoint.Taint;
9 import Analysis.Disjoint.TaintSet;
10 import Analysis.Disjoint.Canonical;
11 import Analysis.CallGraph.CallGraph;
12 import Analysis.OoOJava.RBlockRelationAnalysis;
13 import Analysis.Disjoint.EffectsAnalysis;
14 import Analysis.Disjoint.BuildStateMachines;
18 public class Pointer {
19 HashMap<FlatMethod, BasicBlock> blockMap;
20 HashMap<BBlock, Graph> bbgraphMap;
21 HashMap<FlatNode, Graph> graphMap;
22 HashMap<FlatCall, Set<BBlock>> callMap;
23 HashMap<BBlock, Set<PPoint>> returnMap;
24 HashMap<BBlock, Set<TempDescriptor>> bblivetemps;
26 boolean OoOJava=false;
30 AllocFactory allocFactory;
31 LinkedList<Delta> toprocess;
32 TempDescriptor returntmp;
33 RBlockRelationAnalysis taskAnalysis;
34 EffectsAnalysis effectsAnalysis;
35 BuildStateMachines buildStateMachines;
37 public Pointer(State state, TypeUtil typeUtil, CallGraph callGraph, RBlockRelationAnalysis taskAnalysis) {
38 this(state, typeUtil);
39 this.callGraph=callGraph;
41 this.taskAnalysis=taskAnalysis;
42 this.effectsAnalysis=new EffectsAnalysis();
43 effectsAnalysis.state=state;
44 effectsAnalysis.buildStateMachines=new BuildStateMachines();
47 public Pointer(State state, TypeUtil typeUtil) {
49 this.blockMap=new HashMap<FlatMethod, BasicBlock>();
50 this.bbgraphMap=new HashMap<BBlock, Graph>();
51 this.bblivetemps=new HashMap<BBlock, Set<TempDescriptor>>();
52 this.graphMap=new HashMap<FlatNode, Graph>();
53 this.callMap=new HashMap<FlatCall, Set<BBlock>>();
54 this.returnMap=new HashMap<BBlock, Set<PPoint>>();
55 this.typeUtil=typeUtil;
56 this.allocFactory=new AllocFactory(state, typeUtil);
57 this.toprocess=new LinkedList<Delta>();
58 ClassDescriptor stringcd=typeUtil.getClass(TypeUtil.ObjectClass);
59 this.returntmp=new TempDescriptor("RETURNVAL", stringcd);
62 public BasicBlock getBBlock(FlatMethod fm) {
63 if (!blockMap.containsKey(fm)) {
64 blockMap.put(fm, BasicBlock.getBBlock(fm));
65 Hashtable<FlatNode, Set<TempDescriptor>> livemap=Liveness.computeLiveTemps(fm);
66 for(BBlock bblock:blockMap.get(fm).getBlocks()) {
67 FlatNode fn=bblock.nodes.get(0);
69 HashSet<TempDescriptor> fmset=new HashSet<TempDescriptor>();
70 fmset.addAll((List<TempDescriptor>)Arrays.asList(fm.writesTemps()));
71 bblivetemps.put(bblock, fmset);
73 Set<TempDescriptor> livetemps=livemap.get(fn);
74 bblivetemps.put(bblock, livetemps);
75 livetemps.add(returntmp);
79 return blockMap.get(fm);
82 Delta buildInitialContext() {
83 MethodDescriptor md=typeUtil.getMain();
84 FlatMethod fm=state.getMethodFlat(md);
85 BasicBlock bb=getBBlock(fm);
86 BBlock start=bb.getStart();
87 Delta delta=new Delta(new PPoint(start), true);
88 MySet<Edge> arrayset=new MySet<Edge>();
89 MySet<Edge> varset=new MySet<Edge>();
90 Edge arrayedge=new Edge(allocFactory.StringArray, null, allocFactory.Strings);
91 Edge stringedge=new Edge(fm.getParameter(0), allocFactory.StringArray);
92 delta.addHeapEdge(arrayedge);
93 delta.addVarEdge(stringedge);
98 public void doAnalysis() {
99 toprocess.add(buildInitialContext());
101 while(!toprocess.isEmpty()) {
102 Delta delta=toprocess.remove();
103 PPoint ppoint=delta.getBlock();
104 BBlock bblock=ppoint.getBBlock();
105 Vector<FlatNode> nodes=bblock.nodes();
108 if (ppoint.getIndex()==-1) {
109 //Build base graph for entrance to this basic block
110 //System.out.println("Processing "+bblock.nodes.get(0).toString().replace(' ','_'));
112 delta=applyInitDelta(delta, bblock);
113 //System.out.println("Generating:");
116 //System.out.println("Processing Call "+bblock.nodes.get(ppoint.getIndex()).toString().replace(' ','_'));
119 startindex=ppoint.getIndex()+1;
120 delta=applyCallDelta(delta, bblock);
121 //System.out.println("Generating:");
124 Graph graph=bbgraphMap.get(bblock);
125 Graph nodeGraph=null;
126 boolean init=delta.getInit();
127 if (!init&&delta.isEmpty())
130 //Compute delta at exit of each node
131 for(int i=startindex; i<nodes.size();i++) {
132 FlatNode currNode=nodes.get(i);
133 //System.out.println("Start Processing "+currNode);
134 if (!graphMap.containsKey(currNode)) {
135 if (isNEEDED(currNode))
136 graphMap.put(currNode, new Graph(graph));
139 //base graph works for us
140 graphMap.put(currNode, new Graph(graph));
142 //just use previous graph
143 graphMap.put(currNode, graphMap.get(nodes.get(i-1)));
147 nodeGraph=graphMap.get(currNode);
148 delta=processNode(bblock, i, currNode, delta, nodeGraph);
149 //System.out.println("Processing "+currNode+" and generating delta:");
152 generateFinalDelta(bblock, delta, nodeGraph);
158 for(Map.Entry<BBlock, Graph> e:bbgraphMap.entrySet()) {
159 Graph g=e.getValue();
160 plotGraph(g,"BB"+e.getKey().nodes.get(0).toString().replace(' ','_'));
164 for(FlatMethod fm:blockMap.keySet()) {
165 System.out.println(fm.printMethod());
167 for(Map.Entry<FlatNode, Graph> e:graphMap.entrySet()) {
168 FlatNode fn=e.getKey();
169 Graph g=e.getValue();
170 plotGraph(g,"FN"+fn.toString()+debugindex);
176 void plotGraph(Graph g, String name) {
178 PrintWriter pw=new PrintWriter(new FileWriter(name.toString().replace(' ','_')+".dot"));
179 g.printGraph(pw, name);
181 } catch (Exception ex) {
182 ex.printStackTrace();
187 /* This function builds the last delta for a basic block. It
188 * handles the case for the first time the basic block is
191 void buildInitDelta(Graph graph, Delta newDelta) {
192 //First compute the set of temps
193 HashSet<TempDescriptor> tmpSet=new HashSet<TempDescriptor>();
194 tmpSet.addAll(graph.varMap.keySet());
195 tmpSet.addAll(graph.parent.varMap.keySet());
197 //Next build the temp map part of the delta
198 for(TempDescriptor tmp:tmpSet) {
199 MySet<Edge> edgeSet=new MySet<Edge>();
201 if (graph.varMap.containsKey(tmp))
202 edgeSet.addAll(graph.varMap.get(tmp));
204 edgeSet.addAll(graph.parent.varMap.get(tmp));
205 newDelta.varedgeadd.put(tmp, edgeSet);
208 //Next compute the set of src allocnodes
209 HashSet<AllocNode> nodeSet=new HashSet<AllocNode>();
210 nodeSet.addAll(graph.nodeMap.keySet());
211 nodeSet.addAll(graph.parent.nodeMap.keySet());
213 for(AllocNode node:nodeSet) {
214 MySet<Edge> edgeSet=new MySet<Edge>();
216 if (graph.nodeMap.containsKey(node))
217 edgeSet.addAll(graph.nodeMap.get(node));
219 edgeSet.addAll(graph.parent.nodeMap.get(node));
220 newDelta.heapedgeadd.put(node, edgeSet);
223 if (graph.oldNodes.containsKey(node)) {
225 } else if (graph.parent.oldNodes.containsKey(node)) {
226 //parent graphs only contain true...no need to check
227 newDelta.addOldNodes.put(node, Boolean.TRUE);
231 for(AllocNode node:graph.oldNodes.keySet()) {
232 if (graph.oldNodes.get(node).booleanValue())
233 newDelta.addOldNodes.put(node, Boolean.TRUE);
236 for(AllocNode node:graph.parent.oldNodes.keySet()) {
237 //make sure child doesn't override
238 if (!graph.oldNodes.containsKey(node))
239 newDelta.addOldNodes.put(node, Boolean.TRUE);
242 newDelta.addNodeAges.addAll(graph.nodeAges);
243 newDelta.addNodeAges.addAll(graph.parent.nodeAges);
246 /* This function build the delta for the exit of a basic block. */
248 void generateFinalDelta(BBlock bblock, Delta delta, Graph graph) {
249 Delta newDelta=new Delta(null, false);
250 if (delta.getInit()) {
251 buildInitDelta(graph, newDelta);
253 /* We can break the old delta...it is done being used */
254 /* First we will build variable edges */
255 HashSet<TempDescriptor> tmpSet=new HashSet<TempDescriptor>();
256 tmpSet.addAll(delta.basevaredge.keySet());
257 tmpSet.addAll(delta.varedgeadd.keySet());
258 for(TempDescriptor tmp:tmpSet) {
259 /* Start with the new incoming edges */
260 MySet<Edge> newbaseedge=delta.basevaredge.get(tmp);
261 /* Remove the remove set */
262 if (newbaseedge==null)
263 newbaseedge=new MySet<Edge>();
264 newbaseedge.removeAll(delta.varedgeremove.get(tmp));
265 /* Add in the new set*/
266 newbaseedge.addAll(delta.varedgeadd.get(tmp));
267 /* Store the results */
268 newDelta.varedgeadd.put(tmp, newbaseedge);
270 delta.basevaredge.clear();
272 /* Next we build heap edges */
273 HashSet<AllocNode> nodeSet=new HashSet<AllocNode>();
274 nodeSet.addAll(delta.baseheapedge.keySet());
275 nodeSet.addAll(delta.heapedgeadd.keySet());
276 nodeSet.addAll(delta.heapedgeremove.keySet());
277 for(AllocNode node:nodeSet) {
278 /* Start with the new incoming edges */
279 MySet<Edge> newheapedge=new MySet<Edge>(delta.baseheapedge.get(node));
280 /* Remove the remove set */
281 MySet<Edge> removeset=delta.heapedgeremove.get(node);
284 newheapedge.removeAll(removeset);
286 /* Add in the add set */
287 MySet<Edge> settoadd=delta.heapedgeadd.get(node);
289 newheapedge.addAll(settoadd);
290 newDelta.heapedgeadd.put(node, newheapedge);
292 /* Remove the newly created edges..no need to propagate a diff for those */
293 if (removeset!=null) {
294 removeset.removeAll(delta.baseheapedge.get(node));
295 newDelta.heapedgeremove.put(node, removeset);
299 /* Compute new ages */
300 newDelta.addNodeAges.addAll(delta.baseNodeAges);
301 newDelta.addNodeAges.addAll(delta.addNodeAges);
302 HashSet<AllocNode> oldNodes=new HashSet<AllocNode>();
304 /* Compute whether old nodes survive */
305 oldNodes.addAll(delta.baseOldNodes.keySet());
306 oldNodes.addAll(delta.addOldNodes.keySet());
307 for(AllocNode node:oldNodes) {
308 if (delta.addOldNodes.containsKey(node)) {
309 if (delta.addOldNodes.get(node).booleanValue()) {
310 newDelta.addOldNodes.put(node, Boolean.TRUE);
313 if (delta.baseOldNodes.get(node).booleanValue()) {
314 newDelta.addOldNodes.put(node, Boolean.TRUE);
320 /* Now we need to propagate newdelta */
321 if (!newDelta.heapedgeadd.isEmpty()||!newDelta.heapedgeremove.isEmpty()||!newDelta.varedgeadd.isEmpty()||!newDelta.addNodeAges.isEmpty()||!newDelta.addOldNodes.isEmpty()) {
322 /* We have a delta to propagate */
323 if (returnMap.containsKey(bblock)) {
327 for(PPoint caller:returnMap.get(bblock)) {
328 //System.out.println("Sending Return BBlock to "+caller.getBBlock().nodes.get(caller.getIndex()).toString().replace(' ','_'));
331 newDelta.setBlock(caller);
332 toprocess.add(newDelta);
335 Delta d=newDelta.diffBlock(caller);
341 Vector<BBlock> blockvector=bblock.next();
342 for(int i=0;i<blockvector.size();i++) {
343 //System.out.println("Sending BBlock to "+blockvector.get(i).nodes.get(0).toString().replace(' ','_'));
346 newDelta.setBlock(new PPoint(blockvector.get(i)));
347 toprocess.add(newDelta);
349 Delta d=newDelta.diffBlock(new PPoint(blockvector.get(i)));
355 //System.out.println("EMPTY DELTA");
356 //System.out.println("delta");
358 //System.out.println("newDelta");
363 boolean isNEEDED(FlatNode node) {
364 switch(node.kind()) {
365 case FKind.FlatSetFieldNode: {
366 FlatSetFieldNode n=(FlatSetFieldNode)node;
367 return n.getSrc().getType().isPtr();
369 case FKind.FlatSetElementNode: {
370 FlatSetElementNode n=(FlatSetElementNode)node;
371 return n.getSrc().getType().isPtr();
373 case FKind.FlatFieldNode: {
374 FlatFieldNode n=(FlatFieldNode)node;
375 return n.getDst().getType().isPtr();
377 case FKind.FlatElementNode: {
378 FlatElementNode n=(FlatElementNode)node;
379 return n.getDst().getType().isPtr();
385 Delta processNode(BBlock bblock, int index, FlatNode node, Delta delta, Graph newgraph) {
386 switch(node.kind()) {
388 return processNewNode((FlatNew)node, delta, newgraph);
389 case FKind.FlatFieldNode:
390 case FKind.FlatElementNode:
391 return processFieldElementNode(node, delta, newgraph);
392 case FKind.FlatCastNode:
393 case FKind.FlatOpNode:
394 case FKind.FlatReturnNode:
395 return processCopyNode(node, delta, newgraph);
396 case FKind.FlatSetFieldNode:
397 case FKind.FlatSetElementNode:
398 return processSetFieldElementNode(node, delta, newgraph);
399 case FKind.FlatSESEEnterNode:
400 return processSESEEnterNode((FlatSESEEnterNode) node, delta, newgraph);
401 case FKind.FlatSESEExitNode:
402 return processSESEExitNode((FlatSESEExitNode) node, delta, newgraph);
403 case FKind.FlatMethod:
405 case FKind.FlatBackEdge:
406 case FKind.FlatGenReachNode:
407 return processFlatNop(node, delta, newgraph);
409 return processFlatCall(bblock, index, (FlatCall) node, delta, newgraph);
411 throw new Error("Unrecognized node:"+node);
415 Delta processSESEEnterNode(FlatSESEEnterNode sese, Delta delta, Graph graph) {
417 return processFlatNop(sese, delta, graph);
418 if (delta.getInit()) {
419 removeInitTaints(null, delta, graph);
420 for (TempDescriptor tmp:sese.getInVarSet()) {
421 Taint taint=Taint.factory(sese, null, tmp, null, sese, null);
422 MySet<Edge> edges=GraphManip.getEdges(graph, delta, tmp);
424 Edge newe=e.addTaint(taint);
425 delta.addVarEdge(newe);
429 removeDiffTaints(null, delta, graph);
430 for (TempDescriptor tmp:sese.getInVarSet()) {
431 Taint taint=Taint.factory(sese, null, tmp, null, sese, null);
432 MySet<Edge> edges=GraphManip.getDiffEdges(delta, tmp);
434 Edge newe=e.addTaint(taint);
435 delta.addVarEdge(newe);
441 applyDiffs(graph, delta);
445 private boolean isRecursive(FlatSESEEnterNode sese) {
446 MethodDescriptor md=sese.getmdEnclosing();
447 return callGraph.getCalleeSet(md).contains(md);
450 Delta processSESEExitNode(FlatSESEExitNode seseexit, Delta delta, Graph graph) {
452 return processFlatNop(seseexit, delta, graph);
453 FlatSESEEnterNode sese=seseexit.getFlatEnter();
454 //Strip Taints from this SESE
455 if (delta.getInit()) {
456 removeInitTaints(isRecursive(sese)?sese:null, delta, graph);
458 removeDiffTaints(isRecursive(sese)?sese:null, delta);
460 applyDiffs(graph, delta);
464 void removeDiffTaints(FlatSESEEnterNode sese, Delta delta) {
465 //Start with variable edges
467 MySet<Edge> edgestoadd=new MySet<Edge>();
468 MySet<Edge> edgestoremove=new MySet<Edge>();
470 //Process base diff edges
471 processEdgeMap(sese, delta.basevaredge, null, delta.varedgeremove, edgestoremove, edgestoadd);
472 //Process delta edges
473 processEdgeMap(sese, delta.varedgeadd, null, null, edgestoremove, edgestoadd);
474 for(Edge e:edgestoremove) {
475 delta.removeVarEdge(e);
477 for(Edge e:edgestoadd) {
484 MySet<Edge> edgestoadd=new MySet<Edge>();
485 MySet<Edge> edgestoremove=new MySet<Edge>();
487 //Process base diff edges
488 processEdgeMap(sese, delta.baseheapedge, null, delta.heapedgeremove, edgestoremove, edgestoadd);
489 //Process delta edges
490 processEdgeMap(sese, delta.heapedgeadd, null, null, edgestoremove, edgestoadd);
491 for(Edge e:edgestoremove) {
492 delta.removeHeapEdge(e);
494 for(Edge e:edgestoadd) {
495 delta.addHeapEdge(e);
500 void removeInitTaints(FlatSESEEnterNode sese, Delta delta, Graph graph) {
501 //Start with variable edges
503 MySet<Edge> edgestoadd=new MySet<Edge>();
504 MySet<Edge> edgestoremove=new MySet<Edge>();
506 //Process parent edges
507 processEdgeMap(sese, graph.parent.varMap, graph.varMap, delta.varedgeremove, edgestoremove, edgestoadd);
508 //Process graph edges
509 processEdgeMap(sese, graph.varMap, null, delta.varedgeremove, edgestoremove, edgestoadd);
510 //Process delta edges
511 processEdgeMap(sese, delta.varedgeadd, null, null, edgestoremove, edgestoadd);
512 for(Edge e:edgestoremove) {
513 delta.removeVarEdge(e);
515 for(Edge e:edgestoadd) {
522 MySet<Edge> edgestoadd=new MySet<Edge>();
523 MySet<Edge> edgestoremove=new MySet<Edge>();
525 //Process parent edges
526 processEdgeMap(sese, graph.parent.nodeMap, graph.nodeMap, delta.heapedgeremove, edgestoremove, edgestoadd);
527 //Process graph edges
528 processEdgeMap(sese, graph.nodeMap, null, delta.heapedgeremove, edgestoremove, edgestoadd);
529 //Process delta edges
530 processEdgeMap(sese, delta.heapedgeadd, null, null, edgestoremove, edgestoadd);
531 for(Edge e:edgestoremove) {
532 delta.removeHeapEdge(e);
534 for(Edge e:edgestoadd) {
535 delta.addHeapEdge(e);
540 void processEdgeMap(FlatSESEEnterNode sese, HashMap<?, MySet<Edge>> edgemap, HashMap<?, MySet<Edge>> childmap, HashMap<?, MySet<Edge>> removemap, MySet<Edge> edgestoremove, MySet<Edge> edgestoadd) {
541 for(Map.Entry<?, MySet<Edge>> entry:edgemap.entrySet()) {
542 //If the parent map exists and overrides this entry, skip it
543 if (childmap!=null&&childmap.containsKey(entry.getKey()))
545 for(Edge e:entry.getValue()) {
546 //check whether this edge has been removed
547 if (removemap==null&&removemap.containsKey(entry.getKey())&&
548 removemap.get(entry.getKey()).contains(e))
551 TaintSet ts=e.getTaints();
553 //update non-null taint set
555 newts=Canonical.removeInContextTaintsNP(ts, sese);
557 edgestoremove.add(e);
558 edgestoadd.add(e.changeTaintSet(newts));
564 void removeDiffTaints(FlatSESEEnterNode sese, Delta delta, Graph graph) {
568 /* This function compute the edges for the this variable for a
569 * callee if it exists. */
571 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) {
572 //Handle the this temp
574 MySet<Edge> edges=(oldnodeset!=null)?GraphManip.getDiffEdges(delta, tmpthis):GraphManip.getEdges(graph, delta, tmpthis);
575 newDelta.varedgeadd.put(tmpthis, (MySet<Edge>) edges.clone());
576 edgeset.addAll(edges);
578 AllocNode dstnode=e.dst;
579 if (!nodeset.contains(dstnode)&&(oldnodeset==null||!oldnodeset.contains(dstnode))) {
580 TypeDescriptor type=dstnode.getType();
581 if (!type.isArray()) {
582 targetSet.add(type.getClassDesc());
584 //arrays don't have code
585 targetSet.add(typeUtil.getClass(TypeUtil.ObjectClass));
587 nodeset.add(dstnode);
588 tovisit.add(dstnode);
594 /* This function compute the edges for a call's parameters. */
596 void processParams(Graph graph, Delta delta, Delta newDelta, HashSet<AllocNode> nodeset, Stack<AllocNode> tovisit, MySet<Edge> edgeset, FlatCall fcall, boolean diff) {
597 //Go through each temp
598 for(int i=0;i<fcall.numArgs();i++) {
599 TempDescriptor tmp=fcall.getArg(i);
600 MySet<Edge> edges=diff?GraphManip.getDiffEdges(delta, tmp):GraphManip.getEdges(graph, delta, tmp);
601 newDelta.varedgeadd.put(tmp, (MySet<Edge>) edges.clone());
602 edgeset.addAll(edges);
604 if (!nodeset.contains(e.dst)) {
612 /* This function computes the reachable nodes for a callee. */
614 void computeReachableNodes(Graph graph, Delta delta, Delta newDelta, HashSet<AllocNode> nodeset, Stack<AllocNode> tovisit, MySet<Edge> edgeset, HashSet<AllocNode> oldnodeset) {
615 while(!tovisit.isEmpty()) {
616 AllocNode node=tovisit.pop();
617 MySet<Edge> edges=GraphManip.getEdges(graph, delta, node);
618 if (!edges.isEmpty()) {
619 newDelta.heapedgeadd.put(node, edges);
620 edgeset.addAll(edges);
622 if (!nodeset.contains(e.dst)&&(oldnodeset==null||!oldnodeset.contains(e.dst))) {
631 HashSet<MethodDescriptor> computeTargets(FlatCall fcall, Delta newDelta) {
632 TempDescriptor tmpthis=fcall.getThis();
633 MethodDescriptor md=fcall.getMethod();
634 HashSet<MethodDescriptor> targets=new HashSet<MethodDescriptor>();
639 for(Edge e:newDelta.varedgeadd.get(tmpthis)) {
640 AllocNode node=e.dst;
641 ClassDescriptor cd=node.getType().getClassDesc();
642 //Figure out exact method called and add to set
643 MethodDescriptor calledmd=cd.getCalledMethod(md);
644 targets.add(calledmd);
650 void fixMapping(FlatCall fcall, HashSet<MethodDescriptor> targets, MySet<Edge> oldedgeset, Delta newDelta, BBlock callblock, int callindex) {
651 Delta basedelta=null;
652 TempDescriptor tmpthis=fcall.getThis();
654 for(MethodDescriptor calledmd:targets) {
655 FlatMethod fm=state.getMethodFlat(calledmd);
656 boolean newmethod=false;
659 HashMap<TempDescriptor, TempDescriptor> tmpMap=new HashMap<TempDescriptor, TempDescriptor>();
662 tmpMap.put(tmpthis, fm.getParameter(offset++));
664 for(int i=0;i<fcall.numArgs();i++) {
665 TempDescriptor tmp=fcall.getArg(i);
666 tmpMap.put(tmp,fm.getParameter(i+offset));
669 //Get basicblock for the method
670 BasicBlock block=getBBlock(fm);
673 if (!callMap.containsKey(fcall)) {
674 callMap.put(fcall, new HashSet<BBlock>());
677 Delta returnDelta=null;
678 if (!callMap.get(fcall).contains(block.getStart())) {
679 callMap.get(fcall).add(block.getStart());
683 if (!returnMap.containsKey(block.getExit())) {
684 returnMap.put(block.getExit(), new HashSet<PPoint>());
686 returnMap.get(block.getExit()).add(new PPoint(callblock, callindex));
688 if (bbgraphMap.containsKey(block.getExit())) {
689 //Need to push existing results to current node
690 if (returnDelta==null) {
691 returnDelta=new Delta(null, false);
692 Vector<FlatNode> exitblocknodes=block.getExit().nodes();
693 FlatExit fexit=(FlatExit)exitblocknodes.get(exitblocknodes.size()-1);
694 buildInitDelta(graphMap.get(fexit), returnDelta);
695 if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
696 returnDelta.setBlock(new PPoint(callblock, callindex));
697 toprocess.add(returnDelta);
700 if (!returnDelta.heapedgeadd.isEmpty()||!returnDelta.heapedgeremove.isEmpty()||!returnDelta.varedgeadd.isEmpty()) {
701 toprocess.add(returnDelta.diffBlock(new PPoint(callblock, callindex)));
707 if (oldedgeset==null) {
708 //First build of this graph
709 //Build and enqueue delta...safe to just use existing delta
710 Delta d=newDelta.changeParams(tmpMap, new PPoint(block.getStart()));
711 //System.out.println("AProcessing "+block.getStart().nodes.get(0).toString().replace(' ','_'));
714 } else if (newmethod) {
715 if (basedelta==null) {
716 basedelta=newDelta.buildBase(oldedgeset);
718 //Build and enqueue delta
719 Delta d=basedelta.changeParams(tmpMap, new PPoint(block.getStart()));
720 //System.out.println("BProcessing "+block.getStart().nodes.get(0).toString().replace(' ','_'));
724 //Build and enqueue delta
725 Delta d=newDelta.changeParams(tmpMap, new PPoint(block.getStart()));
726 //System.out.println("CProcessing "+block.getStart().nodes.get(0).toString().replace(' ','_'));
734 /* This function computes all edges that start outside of the callee
735 * context and go into the callee context */
737 void computeExternalEdges(Graph graph, Delta delta, HashSet<AllocNode> nodeset, HashSet<AllocNode> deltaset, MySet<Edge> externaledgeset) {
738 //Do heap edges first
739 HashSet<AllocNode> externalnodes=new HashSet<AllocNode>();
740 externalnodes.addAll(delta.baseheapedge.keySet());
741 externalnodes.addAll(delta.heapedgeadd.keySet());
742 externalnodes.addAll(delta.heapedgeremove.keySet());
743 //remove allinternal nodes
744 externalnodes.removeAll(nodeset);
745 for(AllocNode extNode:externalnodes) {
746 //Compute set of edges from given node
747 MySet<Edge> edges=new MySet<Edge>(delta.baseheapedge.get(extNode));
748 edges.removeAll(delta.heapedgeremove.get(extNode));
749 edges.addAll(delta.heapedgeadd.get(extNode));
752 if (nodeset.contains(e.dst))
753 externaledgeset.add(e);
758 HashSet<TempDescriptor> temps=new HashSet<TempDescriptor>();
759 temps.addAll(delta.basevaredge.keySet());
760 temps.addAll(delta.varedgeadd.keySet());
761 temps.addAll(delta.varedgeremove.keySet());
762 //remove allinternal nodes
763 temps.removeAll(nodeset);
765 for(TempDescriptor tmp:temps) {
766 //Compute set of edges from given node
767 MySet<Edge> edges=new MySet<Edge>(delta.basevaredge.get(tmp));
769 edges.removeAll(delta.varedgeremove.get(tmp));
770 edges.addAll(delta.varedgeadd.get(tmp));
773 if (nodeset.contains(e.dst))
774 externaledgeset.add(e);
779 /* This function removes the caller reachable edges from the
782 void removeEdges(Graph graph, Delta delta, HashSet<AllocNode> nodeset, MySet<Edge> edgeset, MySet<Edge> externaledgeset) {
783 //Want to remove the set of internal edges
784 for(Edge e:edgeset) {
785 if (e.src!=null&&!graph.callerEdges.contains(e)) {
786 delta.removeHeapEdge(e);
790 //Want to remove the set of external edges
791 for(Edge e:externaledgeset) {
792 //want to remove the set of internal edges
793 if (!graph.callerEdges.contains(e))
798 Delta processFlatCall(BBlock callblock, int callindex, FlatCall fcall, Delta delta, Graph graph) {
799 Delta newDelta=new Delta(null, false);
801 if (delta.getInit()) {
802 MySet<Edge> edgeset=new MySet<Edge>();
803 MySet<Edge> externaledgeset=new MySet<Edge>();
804 HashSet<AllocNode> nodeset=new HashSet<AllocNode>();
805 HashSet<ClassDescriptor> targetSet=new HashSet<ClassDescriptor>();
806 Stack<AllocNode> tovisit=new Stack<AllocNode>();
807 TempDescriptor tmpthis=fcall.getThis();
808 graph.callerEdges=new MySet<Edge>();
810 //Handle the this temp
811 processThisTargets(targetSet, graph, delta, newDelta, nodeset, tovisit, edgeset, tmpthis, null);
813 //Go through each temp
814 processParams(graph, delta, newDelta, nodeset, tovisit, edgeset, fcall, false);
816 //Traverse all reachable nodes
817 computeReachableNodes(graph, delta, newDelta, nodeset, tovisit, edgeset, null);
819 //Compute call targets
820 HashSet<MethodDescriptor> newtargets=computeTargets(fcall, newDelta);
823 fixMapping(fcall, newtargets, null, newDelta, callblock, callindex);
825 //Compute edges into region to splice out
826 computeExternalEdges(graph, delta, nodeset, null, externaledgeset);
828 //Splice out internal edges
829 removeEdges(graph, delta, nodeset, edgeset, externaledgeset);
831 //store data structures
832 graph.externalEdgeSet=externaledgeset;
833 graph.reachNode=nodeset;
834 graph.reachEdge=edgeset;
836 graph.callTargets=newtargets;
837 graph.callNodeAges=new HashSet<AllocNode>();
838 graph.callOldNodes=new HashSet<AllocNode>();
840 //Apply diffs to graph
841 applyDiffs(graph, delta, true);
843 MySet<Edge> edgeset=new MySet<Edge>();
844 MySet<Edge> externaledgeset=new MySet<Edge>();
845 HashSet<AllocNode> nodeset=new HashSet<AllocNode>();
846 MySet<Edge> oldedgeset=graph.reachEdge;
847 HashSet<AllocNode> oldnodeset=graph.reachNode;
849 HashSet<ClassDescriptor> targetSet=new HashSet<ClassDescriptor>();
850 Stack<AllocNode> tovisit=new Stack<AllocNode>();
851 TempDescriptor tmpthis=fcall.getThis();
852 //Fix up delta to get rid of unnecessary heap edge removals
853 for(Map.Entry<AllocNode, MySet<Edge>> entry:delta.heapedgeremove.entrySet()) {
854 for(Iterator<Edge> eit=entry.getValue().iterator();eit.hasNext();) {
856 if (graph.callerEdges.contains(e))
861 //Fix up delta to get rid of unnecessary var edge removals
862 for(Map.Entry<TempDescriptor, MySet<Edge>> entry:delta.varedgeremove.entrySet()) {
863 for(Iterator<Edge> eit=entry.getValue().iterator();eit.hasNext();) {
865 if (graph.callerEdges.contains(e))
870 //Handle the this temp
871 processThisTargets(targetSet, graph, delta, newDelta, nodeset, tovisit, edgeset, tmpthis, oldnodeset);
873 //Go through each temp
874 processParams(graph, delta, newDelta, nodeset, tovisit, edgeset, fcall, true);
875 //Go through each new heap edge that starts from old node
876 MySet<Edge> newedges=GraphManip.getDiffEdges(delta, oldnodeset);
877 edgeset.addAll(newedges);
878 for(Edge e:newedges) {
879 //Add new edges that start from old node to newDelta
881 if (!newDelta.heapedgeadd.containsKey(src)) {
882 newDelta.heapedgeadd.put(src, new MySet<Edge>());
884 newDelta.heapedgeadd.get(src).add(e);
885 if (!nodeset.contains(e.dst)&&!oldnodeset.contains(e.dst)) {
891 //Traverse all reachable nodes
892 computeReachableNodes(graph, delta, newDelta, nodeset, tovisit, edgeset, oldnodeset);
893 //Compute call targets
894 HashSet<MethodDescriptor> newtargets=computeTargets(fcall, newDelta);
895 graph.callTargets.addAll(newtargets);
896 //add in new nodeset and edgeset
897 oldnodeset.addAll(nodeset);
898 oldedgeset.addAll(edgeset);
900 fixMapping(fcall, graph.callTargets, oldedgeset, newDelta, callblock, callindex);
901 //Compute edges into region to splice out
902 computeExternalEdges(graph, delta, oldnodeset, nodeset, externaledgeset);
904 //Splice out internal edges
905 removeEdges(graph, delta, nodeset, edgeset, externaledgeset);
907 //Add external edges back in
908 processCallExternal(graph, delta, externaledgeset);
910 //Move new edges that should be summarized
911 processSummarization(graph, delta);
913 //Add in new external edges
914 graph.externalEdgeSet.addAll(externaledgeset);
915 //Apply diffs to graph
916 applyDiffs(graph, delta);
921 void processSummarization(Graph graph, Delta delta) {
922 processSumHeapEdgeSet(delta.heapedgeadd, delta, graph);
923 processSumHeapEdgeSet(delta.baseheapedge, delta, graph);
924 processSumVarEdgeSet(delta.varedgeadd, delta, graph);
925 processSumVarEdgeSet(delta.basevaredge, delta, graph);
928 void processSumVarEdgeSet(HashMap<TempDescriptor, MySet<Edge>> map, Delta delta, Graph graph) {
929 MySet<Edge> edgestoadd=new MySet<Edge>();
930 MySet<Edge> edgestoremove=new MySet<Edge>();
931 for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> eit=map.entrySet().iterator();eit.hasNext();) {
932 Map.Entry<TempDescriptor, MySet<Edge>> entry=eit.next();
933 MySet<Edge> edgeset=entry.getValue();
935 for(Edge e:edgeset) {
937 boolean rewrite=false;
938 if (copy.dst!=null&&graph.callNodeAges.contains(copy.dst)) {
939 copy.dst=allocFactory.getAllocNode(copy.dst, true);
943 edgestoremove.add(e);
944 edgestoadd.add(copy);
948 for(Edge e:edgestoremove) {
949 delta.removeVarEdge(e);
951 for(Edge e:edgestoadd) {
956 void processSumHeapEdgeSet(HashMap<AllocNode, MySet<Edge>> map, Delta delta, Graph graph) {
957 MySet<Edge> edgestoadd=new MySet<Edge>();
958 MySet<Edge> edgestoremove=new MySet<Edge>();
959 for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> eit=map.entrySet().iterator();eit.hasNext();) {
960 Map.Entry<AllocNode, MySet<Edge>> entry=eit.next();
961 AllocNode node=entry.getKey();
962 MySet<Edge> edgeset=entry.getValue();
964 for(Edge e:edgeset) {
966 boolean rewrite=false;
967 if (copy.src!=null&&graph.callNodeAges.contains(copy.src)) {
968 copy.src=allocFactory.getAllocNode(copy.src, true);
971 if (copy.dst!=null&&graph.callNodeAges.contains(copy.dst)) {
972 copy.dst=allocFactory.getAllocNode(copy.dst, true);
976 edgestoremove.add(e);
977 edgestoadd.add(copy);
981 for(Edge e:edgestoremove) {
982 delta.removeHeapEdge(e);
984 for(Edge e:edgestoadd) {
985 delta.addHeapEdge(e);
989 //Handle external edges
990 void processCallExternal(Graph graph, Delta newDelta, MySet<Edge> externalEdgeSet) {
991 //Add external edges in
992 for(Edge e:externalEdgeSet) {
993 //First did we age the source
994 Edge newedge=e.copy();
995 if (newedge.src!=null&&!e.src.isSummary()&&graph.callNodeAges.contains(e.src)) {
996 AllocNode summaryNode=allocFactory.getAllocNode(newedge.src, true);
997 newedge.src=summaryNode;
1000 if (graph.callNodeAges.contains(e.dst)&&!e.dst.isSummary()) {
1001 if (graph.callOldNodes.contains(e.dst)) {
1003 Edge copy=newedge.copy();
1004 mergeEdge(graph, newDelta, copy);
1006 //Now add summarized node
1007 newedge.dst=allocFactory.getAllocNode(newedge.dst, true);
1008 mergeCallEdge(graph, newDelta, newedge);
1010 //Add edge to single node
1011 mergeEdge(graph, newDelta, newedge);
1016 /* This function applies callee deltas to the caller heap. */
1018 Delta applyCallDelta(Delta delta, BBlock bblock) {
1019 Delta newDelta=new Delta(null, false);
1020 Vector<FlatNode> nodes=bblock.nodes();
1021 PPoint ppoint=delta.getBlock();
1022 FlatCall fcall=(FlatCall)nodes.get(ppoint.getIndex());
1023 Graph graph=graphMap.get(fcall);
1024 Graph oldgraph=(ppoint.getIndex()==0)?
1025 bbgraphMap.get(bblock):
1026 graphMap.get(nodes.get(ppoint.getIndex()-1));
1027 Set<FlatSESEEnterNode> seseCallers=OoOJava?taskAnalysis.getTransitiveExecutingRBlocks(fcall):null;
1029 //Age outside nodes if necessary
1030 for(Iterator<AllocNode> nodeit=delta.addNodeAges.iterator();nodeit.hasNext();) {
1031 AllocNode node=nodeit.next();
1032 if (!graph.callNodeAges.contains(node)) {
1033 graph.callNodeAges.add(node);
1034 newDelta.addNodeAges.add(node);
1036 if (!graph.reachNode.contains(node)&&!node.isSummary()) {
1037 /* Need to age node in existing graph*/
1038 summarizeInGraph(graph, newDelta, node);
1042 for(Map.Entry<AllocNode, MySet<Edge>> entry:delta.heapedgeadd.entrySet()) {
1043 for(Edge e:entry.getValue()) {
1044 boolean addedge=false;
1045 Edge edgetoadd=null;
1046 if (e.statuspredicate==Edge.NEW) {
1049 Edge origEdgeKey=e.makeStatus(allocFactory);
1050 if (oldgraph.nodeMap.containsKey(origEdgeKey.src)&&
1051 oldgraph.nodeMap.get(origEdgeKey.src).contains(origEdgeKey)) {
1052 Edge origEdge=oldgraph.nodeMap.get(origEdgeKey.src).get(origEdgeKey);
1053 //copy the predicate
1054 origEdgeKey.statuspredicate=origEdge.statuspredicate;
1055 edgetoadd=origEdgeKey;
1058 if (seseCallers!=null)
1059 edgetoadd.taintModify(seseCallers);
1060 mergeCallEdge(graph, newDelta, edgetoadd);
1064 processCallExternal(graph, newDelta, graph.externalEdgeSet);
1066 //Add edge for return value
1067 if (fcall.getReturnTemp()!=null) {
1068 MySet<Edge> returnedge=delta.varedgeadd.get(returntmp);
1069 if (returnedge!=null)
1070 for(Edge e:returnedge) {
1071 Edge newedge=e.copy();
1072 newedge.srcvar=fcall.getReturnTemp();
1073 if (seseCallers!=null)
1074 newedge.taintModify(seseCallers);
1075 if (graph.getEdges(fcall.getReturnTemp())==null||!graph.getEdges(fcall.getReturnTemp()).contains(newedge))
1076 newDelta.addEdge(newedge);
1079 applyDiffs(graph, newDelta);
1083 public void mergeEdge(Graph graph, Delta newDelta, Edge edgetoadd) {
1084 if (edgetoadd!=null) {
1085 Edge match=graph.getMatch(edgetoadd);
1087 if (match==null||!match.subsumes(edgetoadd)) {
1088 Edge mergededge=edgetoadd.merge(match);
1089 newDelta.addEdge(mergededge);
1094 /* This is a call produced edge...need to remember this */
1096 public void mergeCallEdge(Graph graph, Delta newDelta, Edge edgetoadd) {
1097 if (edgetoadd!=null) {
1098 Edge match=graph.getMatch(edgetoadd);
1100 if (match==null||!match.subsumes(edgetoadd)) {
1101 Edge mergededge=edgetoadd.merge(match);
1102 newDelta.addEdge(mergededge);
1103 graph.callerEdges.add(mergededge);
1104 //System.out.println("ADDING: "+ mergededge);
1110 /* Summarizes out of context nodes in graph */
1111 void summarizeInGraph(Graph graph, Delta newDelta, AllocNode singleNode) {
1112 AllocNode summaryNode=allocFactory.getAllocNode(singleNode, true);
1114 //Handle outgoing heap edges
1115 MySet<Edge> edgeset=graph.getEdges(singleNode);
1117 for(Edge e:edgeset) {
1118 Edge rewrite=e.rewrite(singleNode, summaryNode);
1120 newDelta.removeHeapEdge(e);
1121 mergeCallEdge(graph, newDelta, rewrite);
1124 //Handle incoming edges
1125 MySet<Edge> backedges=graph.getBackEdges(singleNode);
1126 for(Edge e:backedges) {
1127 if (e.dst==singleNode) {
1128 //Need to get original edge so that predicate will be correct
1129 Edge match=graph.getMatch(e);
1131 Edge rewrite=match.rewrite(singleNode, summaryNode);
1132 newDelta.removeEdge(match);
1133 mergeCallEdge(graph, newDelta, rewrite);
1139 void applyDiffs(Graph graph, Delta delta) {
1140 applyDiffs(graph, delta, false);
1143 void applyDiffs(Graph graph, Delta delta, boolean genbackwards) {
1144 //build backwards map if requested
1145 if (genbackwards&&graph.backMap==null) {
1146 graph.backMap=new HashMap<AllocNode, MySet<Edge>>();
1147 if (graph.parent.backMap==null) {
1148 graph.parent.backMap=new HashMap<AllocNode, MySet<Edge>>();
1149 for(Map.Entry<AllocNode, MySet<Edge>> entry:graph.nodeMap.entrySet()) {
1150 for(Edge e:entry.getValue()) {
1151 if (!graph.parent.backMap.containsKey(e.dst))
1152 graph.parent.backMap.put(e.dst, new MySet<Edge>());
1153 graph.parent.backMap.get(e.dst).add(e);
1156 for(Map.Entry<TempDescriptor, MySet<Edge>> entry:graph.varMap.entrySet()) {
1157 for(Edge e:entry.getValue()) {
1158 if (!graph.parent.backMap.containsKey(e.dst))
1159 graph.parent.backMap.put(e.dst, new MySet<Edge>());
1160 graph.parent.backMap.get(e.dst).add(e);
1166 //Add hidden base edges
1167 for(Map.Entry<AllocNode, MySet<Edge>> e: delta.baseheapedge.entrySet()) {
1168 AllocNode node=e.getKey();
1169 MySet<Edge> edges=e.getValue();
1170 if (graph.nodeMap.containsKey(node)) {
1171 MySet<Edge> nodeEdges=graph.nodeMap.get(node);
1172 nodeEdges.addAll(edges);
1177 for(Map.Entry<AllocNode, MySet<Edge>> e: delta.heapedgeremove.entrySet()) {
1178 AllocNode node=e.getKey();
1179 MySet<Edge> edgestoremove=e.getValue();
1180 if (graph.nodeMap.containsKey(node)) {
1181 //Just apply diff to current map
1182 graph.nodeMap.get(node).removeAll(edgestoremove);
1184 //Generate diff from parent graph
1185 MySet<Edge> parentedges=graph.parent.nodeMap.get(node);
1186 if (parentedges!=null) {
1187 MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
1188 graph.nodeMap.put(node, newedgeset);
1194 for(Map.Entry<AllocNode, MySet<Edge>> e: delta.heapedgeadd.entrySet()) {
1195 AllocNode node=e.getKey();
1196 MySet<Edge> edgestoadd=e.getValue();
1197 //If we have not done a subtract, then
1198 if (!graph.nodeMap.containsKey(node)) {
1199 //Copy the parent entry
1200 if (graph.parent.nodeMap.containsKey(node))
1201 graph.nodeMap.put(node, (MySet<Edge>)graph.parent.nodeMap.get(node).clone());
1203 graph.nodeMap.put(node, new MySet<Edge>());
1205 Edge.mergeEdgesInto(graph.nodeMap.get(node),edgestoadd);
1207 for(Edge eadd:edgestoadd) {
1208 if (!graph.backMap.containsKey(eadd.dst))
1209 graph.backMap.put(eadd.dst, new MySet<Edge>());
1210 graph.backMap.get(eadd.dst).add(eadd);
1216 for(Map.Entry<TempDescriptor, MySet<Edge>> e: delta.varedgeremove.entrySet()) {
1217 TempDescriptor tmp=e.getKey();
1218 MySet<Edge> edgestoremove=e.getValue();
1220 if (graph.varMap.containsKey(tmp)) {
1221 //Just apply diff to current map
1222 graph.varMap.get(tmp).removeAll(edgestoremove);
1223 } else if (graph.parent.varMap.containsKey(tmp)) {
1224 //Generate diff from parent graph
1225 MySet<Edge> parentedges=graph.parent.varMap.get(tmp);
1226 MySet<Edge> newedgeset=Util.setSubtract(parentedges, edgestoremove);
1227 graph.varMap.put(tmp, newedgeset);
1232 for(Map.Entry<TempDescriptor, MySet<Edge>> e: delta.varedgeadd.entrySet()) {
1233 TempDescriptor tmp=e.getKey();
1234 MySet<Edge> edgestoadd=e.getValue();
1235 if (graph.varMap.containsKey(tmp)) {
1236 Edge.mergeEdgesInto(graph.varMap.get(tmp), edgestoadd);
1238 graph.varMap.put(tmp, (MySet<Edge>) edgestoadd.clone());
1240 for(Edge eadd:edgestoadd) {
1241 if (!graph.backMap.containsKey(eadd.dst))
1242 graph.backMap.put(eadd.dst, new MySet<Edge>());
1243 graph.backMap.get(eadd.dst).add(eadd);
1248 //Add node additions
1249 for(AllocNode node:delta.addNodeAges) {
1250 graph.nodeAges.add(node);
1253 for(Map.Entry<AllocNode, Boolean> nodeentry:delta.addOldNodes.entrySet()) {
1254 AllocNode node=nodeentry.getKey();
1255 Boolean ispresent=nodeentry.getValue();
1256 graph.oldNodes.put(node, ispresent);
1260 Delta processSetFieldElementNode(FlatNode node, Delta delta, Graph graph) {
1264 if (node.kind()==FKind.FlatSetElementNode) {
1265 FlatSetElementNode fen=(FlatSetElementNode) node;
1270 FlatSetFieldNode ffn=(FlatSetFieldNode) node;
1275 //Do nothing for non pointers
1276 if (!src.getType().isPtr())
1279 if (delta.getInit()) {
1280 MySet<Edge> srcEdges=GraphManip.getEdges(graph, delta, src);
1281 MySet<Edge> dstEdges=GraphManip.getEdges(graph, delta, dst);
1282 MySet<Edge> edgesToAdd=GraphManip.genEdges(dstEdges, fd, srcEdges);
1283 MySet<Edge> edgesToRemove=null;
1284 if (dstEdges.size()==1&&!dstEdges.iterator().next().dst.isSummary()&&fd!=null) {
1285 /* Can do a strong update */
1286 edgesToRemove=GraphManip.getEdges(graph, delta, dstEdges, fd);
1287 graph.strongUpdateSet=edgesToRemove;
1289 graph.strongUpdateSet=new MySet<Edge>();
1292 effectsAnalysis.analyzeFlatSetFieldNode(dstEdges, fd, node);
1296 updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
1297 applyDiffs(graph, delta);
1299 /* First look at new sources */
1300 MySet<Edge> edgesToAdd=new MySet<Edge>();
1301 MySet<Edge> newSrcEdges=GraphManip.getDiffEdges(delta, src);
1302 MySet<Edge> srcEdges=GraphManip.getEdges(graph, delta, src);
1303 HashSet<AllocNode> dstNodes=GraphManip.getNodes(graph, delta, dst);
1304 MySet<Edge> newDstEdges=GraphManip.getDiffEdges(delta, dst);
1307 effectsAnalysis.analyzeFlatSetFieldNode(newDstEdges, fd, node);
1310 MySet<Edge> edgesToRemove=null;
1311 if (newDstEdges.size()!=0) {
1312 if (dstNodes.size()>1&&!dstNodes.iterator().next().isSummary()&&fd!=null) {
1313 /* Need to undo strong update */
1314 if (graph.strongUpdateSet!=null) {
1315 edgesToAdd.addAll(graph.strongUpdateSet);
1316 graph.strongUpdateSet=null; //Prevent future strong updates
1318 } else if (dstNodes.size()==1&&newDstEdges.size()==1&&!newDstEdges.iterator().next().dst.isSummary()&&graph.strongUpdateSet!=null&&fd!=null) {
1319 edgesToRemove=GraphManip.getEdges(graph, delta, dstNodes, fd);
1320 graph.strongUpdateSet.addAll(edgesToRemove);
1322 Edge.mergeEdgesInto(edgesToAdd, GraphManip.genEdges(newDstEdges, fd, srcEdges));
1326 if (graph.strongUpdateSet!=null&&fd!=null) {
1327 MySet<Edge> otherEdgesToRemove=GraphManip.getDiffEdges(delta, dstNodes, fd);
1328 if (edgesToRemove!=null)
1329 edgesToRemove.addAll(otherEdgesToRemove);
1331 edgesToRemove=otherEdgesToRemove;
1332 graph.strongUpdateSet.addAll(otherEdgesToRemove);
1335 //Next look at new destinations
1336 Edge.mergeEdgesInto(edgesToAdd, GraphManip.genEdges(dstNodes, fd, newSrcEdges));
1339 updateHeapDelta(graph, delta, edgesToAdd, edgesToRemove);
1340 applyDiffs(graph, delta);
1345 Delta processCopyNode(FlatNode node, Delta delta, Graph graph) {
1348 if (node.kind()==FKind.FlatOpNode) {
1349 FlatOpNode fon=(FlatOpNode) node;
1352 } else if (node.kind()==FKind.FlatReturnNode) {
1353 FlatReturnNode frn=(FlatReturnNode)node;
1354 src=frn.getReturnTemp();
1356 if (src==null||!src.getType().isPtr()) {
1358 applyDiffs(graph, delta);
1362 FlatCastNode fcn=(FlatCastNode) node;
1366 if (delta.getInit()) {
1367 MySet<Edge> srcedges=GraphManip.getEdges(graph, delta, src);
1368 MySet<Edge> edgesToAdd=GraphManip.genEdges(dst, srcedges);
1369 MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
1370 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1371 applyDiffs(graph, delta);
1373 /* First compute new src nodes */
1374 MySet<Edge> newSrcEdges=GraphManip.getDiffEdges(delta, src);
1376 /* Compute the union, and then the set of edges */
1377 MySet<Edge> edgesToAdd=GraphManip.genEdges(dst, newSrcEdges);
1379 /* Compute set of edges to remove */
1380 MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);
1383 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1384 applyDiffs(graph, delta);
1389 Delta processFieldElementNode(FlatNode node, Delta delta, Graph graph) {
1393 TaintSet taint=null;
1395 if (node.kind()==FKind.FlatElementNode) {
1396 FlatElementNode fen=(FlatElementNode) node;
1401 FlatFieldNode ffn=(FlatFieldNode) node;
1406 if (OoOJava&&taskAnalysis.isPotentialStallSite(node)) {
1407 taint=TaintSet.factory(Taint.factory(node, src, null, null, null));
1410 //Do nothing for non pointers
1411 if (!dst.getType().isPtr())
1413 if (delta.getInit()) {
1414 MySet<Edge> srcedges=GraphManip.getEdges(graph, delta, src);
1415 MySet<Edge> edgesToAdd=GraphManip.dereference(graph, delta, dst, srcedges, fd, node, taint);
1416 MySet<Edge> edgesToRemove=GraphManip.getEdges(graph, delta, dst);
1418 effectsAnalysis.analyzeFlatFieldNode(srcedges, fd, node);
1420 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1421 applyDiffs(graph, delta);
1423 /* First compute new objects we read fields of */
1424 MySet<Edge> allsrcedges=GraphManip.getEdges(graph, delta, src);
1425 MySet<Edge> edgesToAdd=GraphManip.diffDereference(delta, dst, allsrcedges, fd, node, taint);
1426 /* Next compute new targets of fields */
1427 MySet<Edge> newsrcedges=GraphManip.getDiffEdges(delta, src);
1428 MySet<Edge> newfdedges=GraphManip.dereference(graph, delta, dst, newsrcedges, fd, node, taint);
1430 /* Compute the union, and then the set of edges */
1431 Edge.mergeEdgesInto(edgesToAdd, newfdedges);
1433 /* Compute set of edges to remove */
1434 MySet<Edge> edgesToRemove=GraphManip.getDiffEdges(delta, dst);
1437 effectsAnalysis.analyzeFlatFieldNode(newsrcedges, fd, node);
1440 updateVarDelta(graph, delta, dst, edgesToAdd, edgesToRemove);
1441 applyDiffs(graph, delta);
1447 void updateVarDelta(Graph graph, Delta delta, TempDescriptor tmp, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
1448 MySet<Edge> edgeAdd=delta.varedgeadd.get(tmp);
1449 MySet<Edge> edgeRemove=delta.varedgeremove.get(tmp);
1450 MySet<Edge> existingEdges=graph.getEdges(tmp);
1451 for(Edge e: edgestoRemove) {
1452 //remove edge from delta
1455 //if the edge is already in the graph, add an explicit remove to the delta
1456 if (existingEdges.contains(e))
1457 delta.removeVarEdge(e);
1459 for(Edge e: edgestoAdd) {
1460 //Remove the edge from the remove set
1461 if (edgeRemove!=null)
1462 edgeRemove.remove(e);
1463 //Explicitly add it to the add set unless it is already in the graph
1464 if (!existingEdges.contains(e)&&typeUtil.isSuperorType(tmp.getType(),e.dst.getType()))
1465 delta.addVarEdge(e);
1469 void updateHeapDelta(Graph graph, Delta delta, MySet<Edge> edgestoAdd, MySet<Edge> edgestoRemove) {
1470 if (edgestoRemove!=null)
1471 for(Edge e: edgestoRemove) {
1472 AllocNode src=e.src;
1473 MySet<Edge> edgeAdd=delta.heapedgeadd.get(src);
1474 MySet<Edge> existingEdges=graph.getEdges(src);
1475 //remove edge from delta
1478 //if the edge is already in the graph, add an explicit remove to the delta
1479 if (existingEdges.contains(e)) {
1480 delta.removeHeapEdge(e);
1483 if (edgestoAdd!=null)
1484 for(Edge e: edgestoAdd) {
1485 AllocNode src=e.src;
1486 MySet<Edge> edgeRemove=delta.heapedgeremove.get(src);
1487 MySet<Edge> existingEdges=graph.getEdges(src);
1488 //Remove the edge from the remove set
1489 if (edgeRemove!=null)
1490 edgeRemove.remove(e);
1491 //Explicitly add it to the add set unless it is already in the graph
1492 if (!existingEdges.contains(e)||!existingEdges.get(e).isNew()) {
1493 delta.addHeapEdge(e);
1498 Delta processFlatNop(FlatNode node, Delta delta, Graph graph) {
1499 applyDiffs(graph, delta);
1503 Delta processNewNode(FlatNew node, Delta delta, Graph graph) {
1504 AllocNode summary=allocFactory.getAllocNode(node, true);
1505 AllocNode single=allocFactory.getAllocNode(node, false);
1506 TempDescriptor tmp=node.getDst();
1508 if (delta.getInit()) {
1509 /* We don't have to deal with summarization here... The
1510 * intuition is that this is the only place where we generate
1511 * nodes for this allocation site and this is the first time
1512 * we've analyzed this site */
1515 Edge e=new Edge(tmp, single);
1516 //Build new Edge set
1517 MySet<Edge> newedges=new MySet<Edge>();
1519 //Add it into the diffs
1520 delta.varedgeadd.put(tmp, newedges);
1521 //Remove the old edges
1522 MySet<Edge> oldedges=graph.getEdges(tmp);
1523 if (!oldedges.isEmpty())
1524 delta.varedgeremove.put(tmp, (MySet<Edge>) oldedges);
1525 //Apply incoming diffs to graph
1526 applyDiffs(graph, delta);
1527 //Note that we create a single node
1528 delta.addNodeAges.add(single);
1530 if (delta.addOldNodes.containsKey(single)||delta.baseOldNodes.containsKey(single)) {
1531 delta.addOldNodes.put(single, Boolean.FALSE);
1534 /* 1. Fix up the variable edge additions */
1536 for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.varedgeadd.entrySet().iterator();entryIt.hasNext();) {
1537 Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
1539 if (entry.getKey()==tmp) {
1540 /* Check if this is the tmp we overwrite */
1543 /* Otherwise, check if the target of the edge is changed... */
1544 summarizeSet(entry.getValue(), graph.varMap.get(entry.getKey()), single, summary);
1548 /* 2. Fix up the base variable edges */
1550 for(Iterator<Map.Entry<TempDescriptor, MySet<Edge>>> entryIt=delta.basevaredge.entrySet().iterator();entryIt.hasNext();) {
1551 Map.Entry<TempDescriptor, MySet<Edge>> entry=entryIt.next();
1552 TempDescriptor entrytmp=entry.getKey();
1553 if (entrytmp==tmp) {
1554 /* Check is this is the tmp we overwrite, if so add to remove set */
1555 Util.relationUpdate(delta.varedgeremove, tmp, null, entry.getValue());
1557 /* Check if the target of the edge is changed */
1558 MySet<Edge> newset=(MySet<Edge>)entry.getValue().clone();
1559 MySet<Edge> removeset=shrinkSet(newset, graph.varMap.get(entrytmp), single, summary);
1560 Util.relationUpdate(delta.varedgeremove, entrytmp, newset, removeset);
1561 Util.relationUpdate(delta.varedgeadd, entrytmp, null, newset);
1566 /* 3. Fix up heap edge additions */
1568 HashMap<AllocNode, MySet<Edge>> addheapedge=new HashMap<AllocNode, MySet<Edge>>();
1569 for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.heapedgeadd.entrySet().iterator();entryIt.hasNext();) {
1570 Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
1571 MySet<Edge> edgeset=entry.getValue();
1572 AllocNode allocnode=entry.getKey();
1573 if (allocnode==single) {
1575 summarizeSet(edgeset, graph.nodeMap.get(summary), single, summary);
1576 addheapedge.put(summary, edgeset);
1578 summarizeSet(edgeset, graph.nodeMap.get(allocnode), single, summary);
1582 /* Merge in diffs */
1584 for(Map.Entry<AllocNode, MySet<Edge>> entry:addheapedge.entrySet()) {
1585 AllocNode allocnode=entry.getKey();
1586 Util.relationUpdate(delta.heapedgeadd, allocnode, null, entry.getValue());
1589 /* 4. Fix up the base heap edges */
1591 for(Iterator<Map.Entry<AllocNode, MySet<Edge>>> entryIt=delta.baseheapedge.entrySet().iterator();entryIt.hasNext();) {
1592 Map.Entry<AllocNode, MySet<Edge>> entry=entryIt.next();
1593 MySet<Edge> edgeset=entry.getValue();
1594 AllocNode allocnode=entry.getKey();
1595 if (allocnode==single) {
1598 AllocNode addnode=(allocnode==single)?summary:allocnode;
1600 MySet<Edge> newset=(MySet<Edge>) edgeset.clone();
1601 MySet<Edge> removeset=shrinkSet(newset, graph.nodeMap.get(addnode), single, summary);
1602 Util.relationUpdate(delta.heapedgeadd, addnode, null, newset);
1603 Util.relationUpdate(delta.heapedgeremove, allocnode, null, removeset);
1606 /* Update Node Ages...If the base or addNodeAges set contains a
1607 * single node, it now should also contain a summary node... No
1608 * need to generate a single node as that has already been
1610 if (delta.baseNodeAges.contains(single)||delta.addNodeAges.contains(single)) {
1611 delta.addNodeAges.add(summary);
1614 //Kill the old node if someone tries to add it
1615 if (delta.addOldNodes.containsKey(single)||delta.baseOldNodes.containsKey(single)) {
1616 delta.addOldNodes.put(single, Boolean.FALSE);
1619 //Apply incoming diffs to graph
1620 applyDiffs(graph, delta);
1625 /* This function builds a new edge set where oldnode is summarized into new node */
1627 void summarizeSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode sumnode) {
1628 MySet<Edge> newSet=null;
1629 for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
1630 Edge e=edgeit.next();
1631 if (e.dst==oldnode||e.src==oldnode) {
1633 newSet=new MySet<Edge>();
1638 if (e.dst==oldnode) {
1641 if (e.src==oldnode) {
1644 if (oldedgeset==null||!oldedgeset.contains(e))
1649 edgeset.addAll(newSet);
1652 /* Shrinks the incoming set to just include rewritten values.
1653 * Returns a set of the original rewritten values */
1655 MySet<Edge> shrinkSet(MySet<Edge> edgeset, MySet<Edge> oldedgeset, AllocNode oldnode, AllocNode newnode) {
1656 MySet<Edge> newSet=null;
1657 MySet<Edge> removeSet=null;
1658 for(Iterator<Edge> edgeit=edgeset.iterator();edgeit.hasNext();) {
1659 Edge e=edgeit.next();
1661 if (e.dst==oldnode||e.src==oldnode) {
1663 newSet=new MySet<Edge>();
1664 removeSet=new MySet<Edge>();
1673 if (oldedgeset==null||!oldedgeset.contains(e))
1678 edgeset.addAll(newSet);
1682 /* This function returns a completely new Delta... It is safe to
1685 Delta applyInitDelta(Delta delta, BBlock block) {
1686 //Apply delta to graph
1687 boolean newGraph=false;
1688 if (!bbgraphMap.containsKey(block)) {
1689 bbgraphMap.put(block, new Graph(null));
1692 Graph graph=bbgraphMap.get(block);
1695 Delta newdelta=new Delta(null, true);
1696 //Add in heap edges and throw away original diff
1698 for(Map.Entry<AllocNode, MySet<Edge>> entry:delta.heapedgeadd.entrySet()) {
1699 graph.nodeMap.put(entry.getKey(), new MySet<Edge>(entry.getValue()));
1701 //Add in var edges and throw away original diff
1702 Set<TempDescriptor> livetemps=bblivetemps.get(block);
1704 for(Map.Entry<TempDescriptor, MySet<Edge>> entry:delta.varedgeadd.entrySet()) {
1705 if (livetemps.contains(entry.getKey()))
1706 graph.varMap.put(entry.getKey(), new MySet<Edge>(entry.getValue()));
1708 //Record that this is initial set...
1709 graph.nodeAges.addAll(delta.addNodeAges);
1711 for(Map.Entry<AllocNode, Boolean> oldentry:delta.addOldNodes.entrySet()) {
1712 if (oldentry.getValue().booleanValue()) {
1713 graph.oldNodes.put(oldentry.getKey(), Boolean.TRUE);
1718 Delta newdelta=new Delta(null, false);
1719 //merge in heap edges and variables
1720 mergeHeapEdges(graph, delta, newdelta);
1721 mergeVarEdges(graph, delta, newdelta, block);
1722 mergeAges(graph, delta, newdelta);
1727 /* This function merges in the heap edges. It updates delta to be
1730 void mergeHeapEdges(Graph graph, Delta delta, Delta newdelta) {
1732 for(Map.Entry<AllocNode, MySet<Edge>> heapedge:delta.heapedgeadd.entrySet()) {
1733 AllocNode nsrc=heapedge.getKey();
1734 MySet<Edge> edges=heapedge.getValue();
1736 if (graph.backMap!=null) {
1738 if (!graph.backMap.containsKey(e.dst))
1739 graph.backMap.put(e.dst, new MySet<Edge>());
1740 graph.backMap.get(e.dst).add(e);
1744 if (!graph.nodeMap.containsKey(nsrc)) {
1745 graph.nodeMap.put(nsrc, new MySet<Edge>());
1747 MySet<Edge> dstedges=graph.nodeMap.get(nsrc);
1748 MySet<Edge> diffedges=new MySet<Edge>();
1750 if (!dstedges.contains(e)) {
1751 //We have a new edge
1755 Edge origedge=dstedges.get(e);
1756 if (!origedge.subsumes(e)) {
1757 Edge mergededge=origedge.merge(e);
1758 diffedges.add(mergededge);
1759 dstedges.add(mergededge);
1763 //Done with edge set...
1764 if (diffedges.size()>0) {
1766 newdelta.baseheapedge.put(nsrc, diffedges);
1771 /* This function merges in the var edges. It updates delta to be
1774 void mergeVarEdges(Graph graph, Delta delta, Delta newdelta, BBlock block) {
1776 Set<TempDescriptor> livetemps=bblivetemps.get(block);
1778 for(Map.Entry<TempDescriptor, MySet<Edge>> varedge:delta.varedgeadd.entrySet()) {
1779 TempDescriptor tmpsrc=varedge.getKey();
1780 if (livetemps.contains(tmpsrc)) {
1781 MySet<Edge> edges=varedge.getValue();
1782 if (graph.backMap!=null) {
1784 if (!graph.backMap.containsKey(e.dst))
1785 graph.backMap.put(e.dst, new MySet<Edge>());
1786 graph.backMap.get(e.dst).add(e);
1790 if (!graph.varMap.containsKey(tmpsrc)) {
1791 graph.varMap.put(tmpsrc, new MySet<Edge>());
1793 MySet<Edge> dstedges=graph.varMap.get(tmpsrc);
1794 MySet<Edge> diffedges=new MySet<Edge>();
1796 if (!dstedges.contains(e)) {
1797 //We have a new edge
1801 Edge origedge=dstedges.get(e);
1802 if (!origedge.subsumes(e)) {
1803 Edge mergededge=origedge.merge(e);
1804 diffedges.add(mergededge);
1805 dstedges.add(mergededge);
1809 //Done with edge set...
1810 if (diffedges.size()>0) {
1812 newdelta.basevaredge.put(tmpsrc,diffedges);
1818 void mergeAges(Graph graph, Delta delta, Delta newDelta) {
1820 for(AllocNode node:delta.addNodeAges) {
1821 if (!graph.nodeAges.contains(node)) {
1822 graph.nodeAges.add(node);
1823 newDelta.baseNodeAges.add(node);
1826 for(Map.Entry<AllocNode, Boolean> oldentry:delta.addOldNodes.entrySet()) {
1827 AllocNode node=oldentry.getKey();
1828 boolean ispresent=oldentry.getValue().booleanValue();
1829 if (ispresent&&!graph.oldNodes.containsKey(node)) {
1830 graph.oldNodes.put(node, Boolean.TRUE);
1831 newDelta.baseOldNodes.put(node, Boolean.TRUE);