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

LocalDefsFlowAnalysis.java

00001 package ca.mcgill.sable.soot.jimple;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Jimple, a 3-address code Java(TM) bytecode representation.        *
00005  * Copyright (C) 1997, 1998 Raja Vallee-Rai (kor@sable.mcgill.ca)    *
00006  * All rights reserved.                                              *
00007  *                                                                   *
00008  * This work was done as a project of the Sable Research Group,      *
00009  * School of Computer Science, McGill University, Canada             *
00010  * (http://www.sable.mcgill.ca/).  It is understood that any         *
00011  * modification not identified as such is not covered by the         *
00012  * 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 library; 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 Sable Research Group projects, please      *
00033  * visit the web site: http://www.sable.mcgill.ca/                   *
00034  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00035 
00036 /*
00037  Reference Version
00038  -----------------
00039  This is the latest official version on which this file is based.
00040  The reference version is: $SootVersion: 1.beta.4 $
00041 
00042  Change History
00043  --------------
00044  A) Notes:
00045 
00046  Please use the following template.  Most recent changes should
00047  appear at the top of the list.
00048 
00049  - Modified on [date (March 1, 1900)] by [name]. [(*) if appropriate]
00050    [description of modification].
00051 
00052  Any Modification flagged with "(*)" was done as a project of the
00053  Sable Research Group, School of Computer Science,
00054  McGill University, Canada (http://www.sable.mcgill.ca/).
00055 
00056  You should add your copyright, using the following template, at
00057  the top of this file, along with other copyrights.
00058 
00059  *                                                                   *
00060  * Modifications by [name] are                                       *
00061  * Copyright (C) [year(s)] [your name (or company)].  All rights     *
00062  * reserved.                                                         *
00063  *                                                                   *
00064 
00065  B) Changes:
00066  
00067  - Modified on March 13, 1999 by Raja Vallee-Rai (rvalleerai@sable.mcgill.ca) (*)
00068    Re-organized the timers.
00069 
00070  - Modified on November 2, 1998 by Raja Vallee-Rai (kor@sable.mcgill.ca) (*)
00071    Repackaged all source files and performed extensive modifications.
00072    First initial release of Soot.
00073 
00074  - Modified on 23-Jul-1998 by Raja Vallee-Rai (kor@sable.mcgill.ca). (*)
00075    Renamed the uses of Hashtable to HashMap.
00076 
00077  - Modified on 15-Jun-1998 by Raja Vallee-Rai (kor@sable.mcgill.ca). (*)
00078    First internal release (Version 0.1).
00079 */
00080 
00081 import ca.mcgill.sable.soot.*;
00082 import ca.mcgill.sable.util.*;
00083 
00084 class LocalDefsFlowAnalysis extends ForwardFlowAnalysis
00085 {
00086     FlowSet emptySet;
00087     Map localToPreserveSet;
00088     Map localToIntPair;
00089 
00090     public LocalDefsFlowAnalysis(StmtGraph g)
00091     {
00092         super(g);
00093 
00094         Object[] defs;
00095         FlowUniverse defUniverse;
00096 
00097         if(Main.isProfilingOptimization)
00098                 Main.defsSetupTimer.start();
00099 
00100         // Create a list of all the definitions and group defs of the same local together
00101         {
00102             Map localToDefList = new HashMap(g.getBody().getLocalCount() * 2 + 1, 0.7f);
00103 
00104             // Initialize the set of defs for each local to empty
00105             {
00106                 Iterator localIt = g.getBody().getLocals().iterator();
00107 
00108                 while(localIt.hasNext())
00109                 {
00110                     Local l = (Local) localIt.next();
00111 
00112                     localToDefList.put(l, new ArrayList());
00113                 }
00114             }
00115 
00116             // Fill the sets up
00117             {
00118                 Iterator it = g.iterator();
00119 
00120                 while(it.hasNext())
00121                 {
00122                     Stmt s = (Stmt) it.next();
00123 
00124                     if(s instanceof DefinitionStmt)
00125                     {
00126                         DefinitionStmt d = (DefinitionStmt) s;
00127 
00128                         if(d.getLeftOp() instanceof Local)
00129                             ((List) localToDefList.get(d.getLeftOp())).add(d);
00130 
00131                     }
00132                 }
00133             }
00134 
00135             // Generate the list & localToIntPair
00136             {
00137                 Iterator it = localToDefList.keySet().iterator();
00138                 List defList = new LinkedList();
00139 
00140                 int startPos = 0;
00141 
00142                 localToIntPair = new HashMap(g.getBody().getLocalCount() * 2 + 1, 0.7f);
00143 
00144                 // For every local, add all its defs
00145                 {
00146                     while(it.hasNext())
00147                     {
00148                         Local l = (Local) it.next();
00149                         Iterator jt = ((List) localToDefList.get(l)).iterator();
00150 
00151                         int endPos = startPos - 1;
00152 
00153                         while(jt.hasNext())
00154                         {
00155                             defList.add(jt.next());
00156                             endPos++;
00157                         }
00158 
00159                         localToIntPair.put(l, new IntPair(startPos, endPos));
00160 
00161                         // System.out.println(startPos + ":" + endPos);
00162 
00163                         startPos = endPos + 1;
00164                     }
00165                 }
00166 
00167                 defs = defList.toArray();
00168                 defUniverse = new FlowUniverse(defs);
00169             }
00170         }
00171 
00172         emptySet = new ArrayPackedSet(defUniverse);
00173 
00174         // Create the preserve sets for each local.
00175         {
00176             Map localToKillSet = new HashMap(g.getBody().getLocalCount() * 2 + 1, 0.7f);
00177             localToPreserveSet = new HashMap(g.getBody().getLocalCount() * 2 + 1, 0.7f);
00178 
00179             List locals = g.getBody().getLocals();
00180 
00181             // Initialize to empty set
00182             {
00183                 Iterator localIt = locals.iterator();
00184 
00185                 while(localIt.hasNext())
00186                 {
00187                     Local l = (Local) localIt.next();
00188 
00189                     localToKillSet.put(l, emptySet.clone());
00190                 }
00191             }
00192 
00193             // Add every definition of this local
00194                 for(int i = 0; i < defs.length; i++)
00195                 {
00196                     DefinitionStmt d = (DefinitionStmt) defs[i];
00197 
00198                     if(d.getLeftOp() instanceof Local)
00199                     {
00200                         BoundedFlowSet killSet = (BoundedFlowSet) localToKillSet.get(d.getLeftOp());
00201 
00202                         killSet.add(d, killSet);
00203                     }
00204                 }
00205 
00206             // Store complement
00207             {
00208                 Iterator localIt = locals.iterator();
00209 
00210                 while(localIt.hasNext())
00211                 {
00212                     Local l = (Local) localIt.next();
00213 
00214                     BoundedFlowSet killSet = (BoundedFlowSet) localToKillSet.get(l);
00215 
00216                     killSet.complement(killSet);
00217 
00218                     localToPreserveSet.put(l, killSet);
00219                 }
00220             }
00221         }
00222 
00223         if(Main.isProfilingOptimization)
00224       Main.defsSetupTimer.end();
00225 
00226         if(Main.isProfilingOptimization)
00227       Main.defsAnalysisTimer.start();
00228 
00229         doAnalysis();
00230         
00231         if(Main.isProfilingOptimization)
00232       Main.defsAnalysisTimer.end();
00233     }
00234     protected void copy(Object source, Object dest)
00235     {
00236         FlowSet sourceSet = (FlowSet) source,
00237             destSet = (FlowSet) dest;
00238             
00239         sourceSet.copy(destSet);
00240     }
00241     protected void flowThrough(Object inValue, Stmt stmt, Object outValue)
00242     {
00243         FlowSet in = (FlowSet) inValue, out = (FlowSet) outValue;
00244 
00245         if(stmt instanceof DefinitionStmt)
00246         {
00247             DefinitionStmt d = (DefinitionStmt) stmt;
00248 
00249             if(!(d.getLeftOp() instanceof Local))
00250             {
00251                 in.copy(out);
00252                 return;
00253             }
00254 
00255 
00256             Local local = (Local) d.getLeftOp();
00257 
00258             // Perform kill on value
00259                 in.intersection((FlowSet) localToPreserveSet.get(local), out);
00260 
00261             // Perform generation
00262                 out.add(d, out);
00263 
00264         }
00265         else
00266             in.copy(out);
00267     }
00268     protected void merge(Object in1, Object in2, Object out)
00269     {
00270         FlowSet inSet1 = (FlowSet) in1,
00271             inSet2 = (FlowSet) in2;
00272 
00273         FlowSet outSet = (FlowSet) out;
00274 
00275         inSet1.union(inSet2, outSet);
00276     }
00277     protected Object newInitialFlow()
00278     {
00279         return emptySet.clone();
00280     }
00281 }

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