Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

PostdomAnalysis.java

00001 package edu.ksu.cis.bandera.prog;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Bandera, a Java(TM) analysis and transformation toolkit           *
00005  * Copyright (C) 1998, 1999   Hongjun Zheng (zheng@cis.ksu.edu)      *
00006  * All rights reserved.                                              *
00007  *                                                                   *
00008  * This work was done as a project in the SAnToS Laboratory,         *
00009  * Department of Computing and Information Sciences, Kansas State    *
00010  * University, USA (http://www.cis.ksu.edu/santos).                  *
00011  * It is understood that any modification not identified as such is  *
00012  * not covered by the preceding statement.                           *
00013  *                                                                   *
00014  * This work is free software; you can redistribute it and/or        *
00015  * modify it under the terms of the GNU Library General Public       *
00016  * License as published by the Free Software Foundation; either      *
00017  * version 2 of the License, or (at your option) any later version.  *
00018  *                                                                   *
00019  * This work is distributed in the hope that it will be useful,      *
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00022  * Library General Public License for more details.                  *
00023  *                                                                   *
00024  * You should have received a copy of the GNU Library General Public *
00025  * License along with this toolkit; if not, write to the             *
00026  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00027  * Boston, MA  02111-1307, USA.                                      *
00028  *                                                                   *
00029  * Java is a trademark of Sun Microsystems, Inc.                     *
00030  *                                                                   *
00031  * To submit a bug report, send a comment, or get the latest news on *
00032  * this project and other SAnToS projects, please visit the web-site *
00033  *                http://www.cis.ksu.edu/santos                      *
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.util.GenericSet;
00040 import edu.ksu.cis.bandera.prog.Index;
00041 
00042 public class PostdomAnalysis extends AbstractDom
00043 {
00044   private Map stmtToPostdoms;
00045   private Map stmtToImmediatePostdom;
00046 
00047   public PostdomAnalysis(StmtList statementsList, StmtGraph sg)
00048     {
00049       super(statementsList, sg);
00050       stmtToPostdoms = new HashMap(stmtList.size() *2 +1, 0.7f);
00051 
00052       postdominatorsMap();
00053       stmtToImmediatePostdom = computeImmedDom(stmtToPostdoms);
00054     }
00055   private GenericSet exitNodes()
00056     {
00057       GenericSet exitNodeSet = new GenericSet();
00058       List tails = stmtGraph.getTails();
00059       Iterator tailIt = tails.iterator();
00060 
00061       while (tailIt.hasNext())
00062     {
00063       Stmt tail = (Stmt) tailIt.next();
00064       Index tailIndex = new Index(stmtList.indexOf(tail));
00065       exitNodeSet.addElemToSet(tailIndex);
00066     }
00067 
00068       //element of tails are those successor is 0
00069       //add return stmt to exitNode, whose successor is not 0
00070       //this is caused from nextnextstmtAddress, 
00071       //the successor of these returns must be an exception catch
00072 
00073       Iterator stmtIt = stmtList.iterator();
00074       while (stmtIt.hasNext())
00075     {
00076       Stmt retStmt = (Stmt) stmtIt.next();
00077       if ((retStmt instanceof ReturnStmt) || (retStmt instanceof ReturnVoidStmt))
00078         {
00079           List succs = stmtGraph.getSuccsOf(retStmt);
00080           if (succs.size() != 0) 
00081         //maybe should limit to succ.size() == 1, and the succ
00082         //is an exeception handler
00083         {
00084           Index retStmtIndex = new Index(stmtList.indexOf(retStmt));
00085           exitNodeSet.addElemToSet(retStmtIndex);
00086         }
00087         }
00088     }
00089 
00090       return exitNodeSet;
00091     }
00092   private GenericSet exitNodesWithoutThrow(GenericSet exitNodeSet)
00093     {
00094       if (exitNodeSet == null) 
00095     {
00096       System.out.println("exitNodeSet should not be null in exitNodesWithoutThrow method in IndexMaps.java");
00097       System.exit(0);
00098     }
00099      
00100       GenericSet nodeSet = new GenericSet();
00101 
00102       for (int i = 0; i < exitNodeSet.size(); i++)
00103     {
00104       Index exitIndex = (Index) exitNodeSet.setRef(i);
00105       Stmt exitStmt = (Stmt) stmtList.get(exitIndex.index());
00106       if (!(exitStmt instanceof ThrowStmt))
00107         nodeSet.addElemToSet(exitIndex);
00108     }
00109 
00110       return nodeSet;
00111     }
00112   public Index immediatePostdomOf(Stmt s)
00113     {
00114       return (Index) stmtToImmediatePostdom.get(s);
00115     }
00116   private Index indefiniteFrom()
00117     {
00118       BackEdgeAnalysis bea = new BackEdgeAnalysis(stmtList, stmtGraph);
00119       List backEdgesList = bea.backEdgeList();
00120 
00121       Iterator edgeIt = backEdgesList.iterator();
00122       while (edgeIt.hasNext())
00123     {
00124       Edge edge = (Edge) edgeIt.next();
00125       if (edge.isPossibleIndefiniteLoop()) 
00126           return edge.fromNode;
00127     }
00128 
00129       return AbstractDom.specialExitNode;
00130     }
00131   private void postdominatorsMap()
00132     {
00133       boolean change = true;
00134       GenericSet postDominators;
00135       GenericSet tempPostdoms;
00136       Stmt keyStmt;
00137       Index stmtIndex;
00138       LinkedList workList = new LinkedList();
00139       GenericSet visitedNode = new GenericSet();
00140 
00141       //initialize the exit statement's postdom as itself
00142       //if there is no exit node, that means there must be an
00143       //indefinite loop
00144 
00145       GenericSet exitNodeList = exitNodes();
00146       GenericSet exitNodesNoThrow = exitNodesWithoutThrow(exitNodeList);
00147 
00148       if (exitNodesNoThrow.size() == 0)
00149     {
00150       Index indefiniteNode = indefiniteFrom();
00151       if (indefiniteNode.equals(AbstractDom.specialExitNode))
00152         {
00153           System.out.println("Can not find out the exit node or the infinite loop");
00154           System.exit(0);
00155         }
00156       workList.addFirst(indefiniteNode);
00157       
00158       keyStmt = (Stmt) stmtList.get(indefiniteNode.index());
00159       postDominators = new GenericSet(indefiniteNode);
00160       stmtToPostdoms.put(keyStmt, postDominators);
00161     }
00162       else
00163     {
00164       for (int i=0; i< exitNodesNoThrow.size(); i++)
00165         {
00166           stmtIndex = (Index) exitNodesNoThrow.setRef(i);
00167           workList.addFirst(stmtIndex);
00168           
00169           keyStmt = (Stmt) stmtList.get(stmtIndex.index());
00170           postDominators = new GenericSet(stmtIndex);
00171           stmtToPostdoms.put(keyStmt, postDominators);
00172         }
00173     }
00174 
00175       // Initialize postdoms for other statement as all index set
00176       // but not including exception handling statement
00177 
00178       GenericSet initialWorkList = new GenericSet();
00179       for (int i=0; i<workList.size(); i++)
00180     {
00181       initialWorkList.addElemToSet((Index) workList.get(i));
00182     }
00183 
00184       GenericSet startNodes = getStartNodes();
00185 
00186       //add start nodes into the worklist 
00187       for (int i = 0; i< startNodes.size(); i++)
00188     {
00189       stmtIndex = (Index) startNodes.setRef(i);
00190       workList.addFirst(stmtIndex);
00191     }
00192 
00193       postDominators = indexSetWithoutExceptionHandling();
00194 
00195       while (workList.size() != 0)
00196     {
00197       Index nodeIndex = (Index) workList.removeFirst();
00198       visitedNode.addElemToSet(nodeIndex);
00199       Stmt nodeStmt = (Stmt) stmtList.get(nodeIndex.index());
00200 
00201       if (! initialWorkList.contains(nodeIndex))
00202         stmtToPostdoms.put(nodeStmt, postDominators);
00203 
00204       List nodeStmtSuccs = stmtGraph.getSuccsOf(nodeStmt);
00205       List newSuccs = removeExceptionCaught(nodeStmtSuccs);
00206       Iterator succIt = newSuccs.iterator();
00207       while (succIt.hasNext())
00208         {
00209           Stmt succStmt = (Stmt) succIt.next();
00210           Index succIndex = new Index(stmtList.indexOf(succStmt));
00211           if (workList.contains(succIndex) || 
00212           visitedNode.contains(succIndex))
00213         {
00214         }
00215           else workList.addLast(succIndex);
00216         }
00217     }
00218 
00219       while (change)
00220     {     
00221       change = false;
00222       workList = new LinkedList();
00223       visitedNode = new GenericSet();
00224 
00225       //initializing worklist everytime
00226       for (int i=0; i<initialWorkList.size(); i++)
00227         {
00228           workList.addFirst((Index) initialWorkList.setRef(i));
00229         }
00230       for (int i = 0; i< startNodes.size(); i++)
00231         {
00232           stmtIndex = (Index) startNodes.setRef(i);
00233           workList.addFirst(stmtIndex);
00234         }
00235 
00236       //System.out.println("workList: " + workList);
00237 
00238       while (workList.size() != 0)
00239         {
00240           stmtIndex = (Index) workList.removeFirst();
00241           visitedNode.addElemToSet(stmtIndex);
00242 
00243           keyStmt = (Stmt) stmtList.get(stmtIndex.index());
00244           postDominators = (GenericSet) stmtToPostdoms.get(keyStmt);
00245           tempPostdoms = new GenericSet();
00246           tempPostdoms = tempPostdoms.union(postDominators);
00247           
00248           List realSuccs = stmtGraph.getSuccsOf(keyStmt);
00249                   
00250           List succs = removeExceptionCaught(realSuccs);
00251           
00252           for (int i = 0; i < succs.size(); i++)
00253         {
00254           Stmt succStmt = (Stmt) succs.get(i);
00255           GenericSet succDom = (GenericSet) stmtToPostdoms.get(succStmt);
00256           // System.out.println("succStmt: " + succStmt + " ( " + (Index) stmtToInd.get(succStmt) + " ) " + " postdom: " + succDom);
00257           //if (predDom != null)
00258           tempPostdoms = tempPostdoms.intersect(succDom);
00259         }
00260 
00261           tempPostdoms.addElemToSet(stmtIndex);
00262           
00263           if (! tempPostdoms.equals(postDominators))
00264         {
00265           change = true;
00266           stmtToPostdoms.remove(keyStmt);
00267           stmtToPostdoms.put(keyStmt, tempPostdoms);
00268         }
00269 
00270           //begin to add nodes into workList
00271 
00272           for (int i=0; i<succs.size(); i++)
00273         {
00274           Stmt succStmt = (Stmt) succs.get(i);
00275           Index succIndex = new Index(stmtList.indexOf(succStmt));
00276           if (workList.contains(succIndex) || 
00277               visitedNode.contains(succIndex))
00278             {
00279             }
00280           else workList.addLast(succIndex);
00281         }   
00282         }
00283     }
00284 
00285     }
00286   public GenericSet postdomsOf(Stmt stmt)
00287     {
00288       return (GenericSet) stmtToPostdoms.get(stmt);
00289     }
00290   public Map stmtToImmediatePostdom()
00291     {
00292       return stmtToImmediatePostdom;
00293     }
00294   public Map stmtToPostdominatorsMap()
00295     {
00296       return stmtToPostdoms;
00297     }
00298 }

Generated at Thu Feb 7 06:52:12 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001