1 package Analysis.OwnershipAnalysis;
9 public class OwnershipAnalysis {
12 private HashSet<FlatNode> visited;
13 private HashSet<FlatNode> toVisit;
15 private int labelindex;
16 private Hashtable<FlatNode, Integer> flatnodetolabel;
18 private Hashtable<FlatNode, OwnershipGraph> flatNodeToOwnershipGraph;
21 // this analysis generates an ownership graph for every task
23 public OwnershipAnalysis(State state) throws java.io.IOException {
29 public void analyzeTasks() throws java.io.IOException {
30 for( Iterator it_tasks=state.getTaskSymbolTable().getDescriptorsIterator();
34 // initialize the mapping of flat nodes to ownership graphs
35 // every flat node in the IR graph has its own ownership graph
36 flatNodeToOwnershipGraph = new Hashtable<FlatNode, OwnershipGraph>();
38 TaskDescriptor td = (TaskDescriptor)it_tasks.next();
39 FlatMethod fm = state.getMethodFlat( td );
41 // give every node in the flat IR graph a unique label
42 // so a human being can inspect the graph and verify
44 flatnodetolabel = new Hashtable<FlatNode, Integer>();
45 visited = new HashSet<FlatNode>();
49 // add method parameters to the list of heap regions
50 // and remember names for analysis
51 OwnershipGraph og = getGraphFromFlat( fm );
52 for( int i = 0; i < fm.numParameters(); ++i ) {
53 TempDescriptor tdParam = fm.getParameter( i );
54 og.newHeapRegion( tdParam );
55 og.addAnalysisRegion( tdParam );
58 String taskname = td.getSymbol();
59 analyzeFlatIRGraph( fm, taskname );
63 private void labelFlatNodes(FlatNode fn) {
65 flatnodetolabel.put(fn,new Integer(labelindex++));
66 for(int i=0;i<fn.numNext();i++) {
67 FlatNode nn=fn.getNext(i);
68 if(!visited.contains(nn)) {
74 private OwnershipGraph getGraphFromFlat( FlatNode fn ) {
75 if( !flatNodeToOwnershipGraph.containsKey(fn) ) {
76 flatNodeToOwnershipGraph.put( fn, new OwnershipGraph() );
79 return flatNodeToOwnershipGraph.get(fn);
82 private void analyzeFlatIRGraph( FlatMethod fm, String taskname ) throws java.io.IOException {
83 visited=new HashSet<FlatNode>();
84 toVisit=new HashSet<FlatNode>();
87 while( !toVisit.isEmpty() ) {
88 FlatNode fn=(FlatNode)toVisit.iterator().next();
92 // get this node's ownership graph, or create a new one
93 OwnershipGraph og = getGraphFromFlat( fn );
101 case FKind.FlatMethod:
102 og.writeGraph( makeNodeName( taskname, flatnodetolabel.get(fn), "Method" ) );
105 case FKind.FlatOpNode:
106 FlatOpNode fon = (FlatOpNode) fn;
107 if(fon.getOp().getOp()==Operation.ASSIGN) {
110 og.assignTempToTemp( src, dst );
111 og.writeGraph( makeNodeName( taskname, flatnodetolabel.get(fn), "Op" ) );
115 case FKind.FlatFieldNode:
116 FlatFieldNode ffn = (FlatFieldNode) fn;
119 fld = ffn.getField();
120 og.assignTempToField( src, dst, fld );
121 og.writeGraph( makeNodeName( taskname, flatnodetolabel.get(fn), "Field" ) );
124 case FKind.FlatSetFieldNode:
125 FlatSetFieldNode fsfn = (FlatSetFieldNode) fn;
128 fld = fsfn.getField();
129 og.assignFieldToTemp( src, dst, fld );
130 og.writeGraph( makeNodeName( taskname, flatnodetolabel.get(fn), "SetField" ) );
133 case FKind.FlatReturnNode:
134 og.writeGraph( makeNodeName( taskname, flatnodetolabel.get(fn), "Return" ) );
135 og.writeCondensedAnalysis( makeCondensedAnalysisName( taskname, flatnodetolabel.get(fn) ) );
139 // send this flat node's ownership graph along the out edges
140 // to be taken by the next flat edges in the flow, or if they
141 // already have a graph, to be merged
142 for(int i=0;i<fn.numNext();i++) {
143 FlatNode nn=fn.getNext(i);
145 if( !visited.contains( nn ) ) {
146 // FIX THE COPY!!!!!!!!!!!!!!!
147 //flatNodeToOwnershipGraph.put( nn, og.copy() );
148 flatNodeToOwnershipGraph.put( nn, og );
155 private String makeNodeName( String taskname, Integer id, String type ) {
156 String s = String.format( "%05d", id );
157 return "task"+taskname+"_FN"+s+"_"+type;
160 private String makeCondensedAnalysisName( String taskname, Integer id ) {
161 return "task"+taskname+"_Ownership_from"+id;