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

DeadCodeEliminator.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    First release.
00069 */
00070 
00071 import ca.mcgill.sable.soot.*;
00072 import ca.mcgill.sable.util.*;
00073 
00074 public class DeadCodeEliminator
00075 {
00076     /** Eliminates dead code in a linear fashion.  Complexity is linear 
00077         with respect to the statements.
00078     */
00079     public static void eliminateDeadCode(StmtBody body)
00080     {
00081         if(Main.isVerbose)
00082             System.out.println("[" + body.getMethod().getName() +
00083                 "] Eliminating dead code...");
00084         
00085         if(Main.isProfilingOptimization)
00086             Main.deadCodeTimer.start();
00087 
00088         Set essentialStmts = new HashSet();
00089         LinkedList toVisit = new LinkedList();
00090         StmtList stmtList = body.getStmtList();
00091         
00092         // Make a first pass through the statements, noting 
00093         // the statements we must absolutely keep. 
00094         {
00095             Iterator stmtIt = stmtList.iterator();
00096             
00097             while(stmtIt.hasNext()) 
00098             {
00099                 Stmt s = (Stmt) stmtIt.next();
00100                 boolean isEssential = true;
00101                  
00102                 if(s instanceof NopStmt)
00103                     isEssential = false;
00104                  
00105                 if(s instanceof AssignStmt)
00106                 {
00107                     AssignStmt as = (AssignStmt) s;
00108                     
00109                     if(as.getLeftOp() instanceof Local &&
00110                         !(as.getRightOp() instanceof InvokeExpr) && 
00111                         !(as.getRightOp() instanceof InstanceFieldRef) &&
00112                         !(as.getRightOp() instanceof ArrayRef)) 
00113                     {
00114                         // Note that InstanceFieldRef, ArrayRef, InvokeExpr all can
00115                         // have side effects (like throwing a null pointer exception)
00116                     
00117                         isEssential = false;
00118                     }
00119                 }
00120                 
00121                 if(isEssential)
00122                 {
00123                     essentialStmts.add(s);
00124                     toVisit.addLast(s);                    
00125                 }                     
00126             }
00127         }
00128         
00129         // Add all the statements which are used to compute values
00130         // for the essential statements, recursively
00131         {
00132             CompleteStmtGraph graph = new CompleteStmtGraph(stmtList);
00133             LocalDefs defs = new SimpleLocalDefs(graph);
00134             
00135             while(!toVisit.isEmpty())
00136             {
00137                 Stmt s = (Stmt) toVisit.removeFirst();
00138                 Iterator boxIt = s.getUseBoxes().iterator();
00139                                 
00140                 while(boxIt.hasNext())
00141                 {
00142                     ValueBox box = (ValueBox) boxIt.next();
00143                     
00144                     if(box.getValue() instanceof Local)
00145                     {
00146                         Iterator defIt = defs.getDefsOfAt(
00147                             (Local) box.getValue(), s).iterator();
00148                         
00149                         while(defIt.hasNext())
00150                         {
00151                             // Add all the definitions as essential stmts
00152                             
00153                             Stmt def = (Stmt) defIt.next();
00154                             
00155                             if(!essentialStmts.contains(def))
00156                             {
00157                                 essentialStmts.add(def);
00158                                 toVisit.addLast(def);
00159                             }    
00160                         }         
00161                     }
00162                 }
00163             }
00164         }
00165         
00166         // Remove all statements which are not essential
00167         {
00168             Iterator stmtIt = stmtList.iterator();
00169             
00170             while(stmtIt.hasNext())
00171             {
00172                 Stmt s = (Stmt) stmtIt.next();
00173                 
00174                 if(!essentialStmts.contains(s))
00175                     stmtIt.remove();
00176                 else if(s instanceof AssignStmt &&
00177                     ((AssignStmt) s).getLeftOp() == 
00178                     ((AssignStmt) s).getRightOp() &&
00179                     ((AssignStmt) s).getLeftOp() instanceof Local)
00180                 {
00181                     // Stmt is of the form a = a which is useless
00182                     
00183                     stmtIt.remove();
00184                 }   
00185             }
00186         }
00187         
00188         if(Main.isProfilingOptimization)
00189             Main.deadCodeTimer.end();
00190 
00191     }
00192 }

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