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

LocalSplitter.java

00001 package ca.mcgill.sable.soot.jimple;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Jimple, a 3-address code Java(TM) bytecode representation.        *
00005  * Copyright (C) 1999 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    Split off from Transformations.java
00069 */
00070 
00071 import ca.mcgill.sable.soot.*;
00072 import ca.mcgill.sable.util.*;
00073 
00074 public class LocalSplitter
00075 {
00076 
00077     public static void splitLocals(JimpleBody listBody)
00078     {
00079         StmtList stmtList = listBody.getStmtList();
00080         List webs = new ArrayList();
00081         
00082         if(Main.isVerbose)
00083             System.out.println("[" + listBody.getMethod().getName() + "] Splitting locals...");
00084 
00085         Map boxToSet = new HashMap(stmtList.size() * 2 + 1, 0.7f);
00086 
00087         if(Main.isProfilingOptimization)
00088                 Main.splitPhase1Timer.start();
00089 
00090         // Go through the definitions, building the webs
00091         {
00092             List code = stmtList;
00093 
00094             CompleteStmtGraph graph = new CompleteStmtGraph(stmtList);
00095 
00096             LocalDefs localDefs;
00097             
00098             if(Main.usePackedDefs) 
00099             {
00100                 localDefs = new SimpleLocalDefs(graph);
00101             }
00102             else {
00103                 LiveLocals liveLocals;
00104             
00105                 if(Main.usePackedLive) 
00106                     liveLocals = new SimpleLiveLocals(graph);
00107                 else
00108                     liveLocals = new SparseLiveLocals(graph);
00109 
00110                 localDefs = new SparseLocalDefs(graph, liveLocals);                
00111             }            
00112 
00113             LocalUses localUses = new SimpleLocalUses(graph, localDefs);
00114             
00115             if(Main.isProfilingOptimization)
00116                 Main.splitPhase1Timer.end();
00117     
00118             if(Main.isProfilingOptimization)
00119                 Main.splitPhase2Timer.start();
00120 
00121             Set markedBoxes = new HashSet();
00122             Map boxToStmt = new HashMap(stmtList.size() * 2 + 1, 0.7f);
00123             
00124             Iterator codeIt = stmtList.iterator();
00125 
00126             while(codeIt.hasNext())
00127             {
00128                 Stmt s = (Stmt) codeIt.next();
00129 
00130                 if(!(s instanceof DefinitionStmt))
00131                     continue;
00132 
00133                 DefinitionStmt def = (DefinitionStmt) s;
00134 
00135                 if(def.getLeftOp() instanceof Local && !markedBoxes.contains(def.getLeftOpBox()))
00136                 {
00137                     LinkedList defsToVisit = new LinkedList();
00138                     LinkedList boxesToVisit = new LinkedList();
00139 
00140                     List web = new ArrayList();
00141                     webs.add(web);
00142                                         
00143                     defsToVisit.add(def);
00144                     markedBoxes.add(def.getLeftOpBox());
00145                     
00146                     while(!boxesToVisit.isEmpty() || !defsToVisit.isEmpty())
00147                     {
00148                         if(!defsToVisit.isEmpty())
00149                         {
00150                             DefinitionStmt d = (DefinitionStmt) defsToVisit.removeFirst();
00151 
00152                             web.add(d.getLeftOpBox());
00153 
00154                             // Add all the uses of this definition to the queue
00155                             {
00156                                 List uses = localUses.getUsesOf(d);
00157                                 Iterator useIt = uses.iterator();
00158     
00159                                 while(useIt.hasNext())
00160                                 {
00161                                     StmtValueBoxPair use = (StmtValueBoxPair) useIt.next();
00162     
00163                                     if(!markedBoxes.contains(use.valueBox))
00164                                     {
00165                                         markedBoxes.add(use.valueBox);
00166                                         boxesToVisit.addLast(use.valueBox);
00167                                         boxToStmt.put(use.valueBox, use.stmt);
00168                                     }
00169                                 }
00170                             }
00171                         }
00172                         else {
00173                             ValueBox box = (ValueBox) boxesToVisit.removeFirst();
00174 
00175                             web.add(box);
00176 
00177                             // Add all the definitions of this use to the queue.
00178                             {               
00179                                 List defs = localDefs.getDefsOfAt((Local) box.getValue(),
00180                                     (Stmt) boxToStmt.get(box));
00181                                 Iterator defIt = defs.iterator();
00182     
00183                                 while(defIt.hasNext())
00184                                 {
00185                                     DefinitionStmt d = (DefinitionStmt) defIt.next();
00186     
00187                                     if(!markedBoxes.contains(d.getLeftOpBox()))
00188                                     {
00189                                         markedBoxes.add(d.getLeftOpBox());
00190                                         defsToVisit.addLast(d);
00191                                     }    
00192                                 }
00193                             }
00194                         }
00195                     }
00196                 }
00197             }
00198         }
00199 
00200         // Assign locals appropriately.
00201         {
00202             Map localToUseCount = new HashMap(listBody.getLocalCount() * 2 + 1, 0.7f);
00203             Iterator webIt = webs.iterator();
00204 
00205             while(webIt.hasNext())
00206             {
00207                 List web = (List) webIt.next();
00208 
00209                 ValueBox rep = (ValueBox) web.get(0);
00210                 Local desiredLocal = (Local) rep.getValue();
00211 
00212                 if(!localToUseCount.containsKey(desiredLocal))
00213                 {
00214                     // claim this local for this set
00215 
00216                     localToUseCount.put(desiredLocal, new Integer(1));
00217                 }
00218                 else {
00219                     // generate a new local
00220 
00221                     int useCount = ((Integer) localToUseCount.get(desiredLocal)).intValue() + 1;
00222                     localToUseCount.put(desiredLocal, new Integer(useCount));
00223 
00224                     Local local = new Local(desiredLocal.getName() + "$" + useCount, desiredLocal.getType());
00225 
00226                     listBody.addLocal(local);
00227 
00228                     // Change all boxes to point to this new local
00229                     {
00230                         Iterator j = web.iterator();
00231 
00232                         while(j.hasNext())
00233                         {
00234                             ValueBox box = (ValueBox) j.next();
00235 
00236                             box.setValue(local);
00237                         }
00238                     }
00239                 }
00240             }
00241         }
00242         
00243         if(Main.isProfilingOptimization)
00244             Main.splitPhase2Timer.end();
00245 
00246     }
00247 }

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