00001 package edu.ksu.cis.bandera.prog;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import ca.mcgill.sable.util.*;
00036 import ca.mcgill.sable.soot.*;
00037 import ca.mcgill.sable.soot.jimple.*;
00038
00039 import edu.ksu.cis.bandera.prog.Index;
00040 import edu.ksu.cis.bandera.util.GenericSet;
00041
00042 public class BackEdgeAnalysis
00043 {
00044 private List backEdgesList;
00045 private StmtList stmtList;
00046 private StmtGraph stmtGraph;
00047
00048 public BackEdgeAnalysis(StmtList statementsList, StmtGraph sg)
00049 {
00050 stmtList = statementsList;
00051 stmtGraph =sg;
00052
00053 backEdgeAnalysis();
00054 }
00055 private void backEdgeAnalysis()
00056 {
00057
00058 DomAnalysis da = new DomAnalysis(stmtList, stmtGraph);
00059 Map stmtToDoms = da.stmtToDominatorsMap();
00060
00061 backEdgesList = new ArrayList();
00062
00063 Iterator keyStmtIt = stmtToDoms.keySet().iterator();
00064 while (keyStmtIt.hasNext())
00065 {
00066 Stmt keyStmt = (Stmt) keyStmtIt.next();
00067 Index stmtIndex = new Index(stmtList.indexOf(keyStmt));
00068
00069 List succsOfKeyStmt = stmtGraph.getSuccsOf(keyStmt);
00070 GenericSet succSet = stmtListToIndexSet(succsOfKeyStmt);
00071
00072 GenericSet dominators = (GenericSet) stmtToDoms.get(keyStmt);
00073 for (int i = 0; i < dominators.size(); i++)
00074 {
00075 Index domIndex = (Index) dominators.setRef(i);
00076 if (succSet.contains(domIndex))
00077 {
00078 Edge backEdge = new Edge(stmtIndex, domIndex);
00079 List natLoop = naturalLoop(stmtIndex, domIndex);
00080 backEdge.setNaturalLoop(natLoop);
00081
00082
00083
00084
00085
00086 backEdgesList.add(backEdge);
00087 }
00088 }
00089 }
00090 }
00091 public List backEdgeList()
00092 {
00093 return backEdgesList;
00094 }
00095 private List naturalLoop(Index fromIndex, Index toIndex)
00096 {
00097 List loop = new ArrayList();
00098
00099 loop.add(fromIndex);
00100 loop.add(toIndex);
00101
00102 if (fromIndex.equals(toIndex)) return loop;
00103
00104 LinkedList stack = new LinkedList();
00105 stack.addFirst(fromIndex);
00106
00107 while (!stack.isEmpty())
00108 {
00109 Index currentIndex = (Index) stack.removeFirst();
00110 Stmt currentStmt = (Stmt) stmtList.get(currentIndex.index());
00111 List predsList = stmtGraph.getPredsOf(currentStmt);
00112 Iterator predsIt = predsList.iterator();
00113 while (predsIt.hasNext())
00114 {
00115 Index predIndex = new Index(stmtList.indexOf(predsIt.next()));
00116 if (!loop.contains(predIndex))
00117 {
00118 loop.add(predIndex);
00119 stack.addLast(predIndex);
00120 }
00121 }
00122 }
00123
00124 return loop;
00125 }
00126 private GenericSet stmtListToIndexSet(List succList)
00127 {
00128 GenericSet returnSet = new GenericSet();
00129
00130 for (int i = 0; i<succList.size(); i++)
00131 {
00132 Stmt s = (Stmt) succList.get(i);
00133 Index sIndex = new Index(stmtList.indexOf(s));
00134 returnSet.addElemToSet(sIndex);
00135 }
00136 return returnSet;
00137 }
00138 }