001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 1997-1999 Raja Vallee-Rai
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: */
019:
020: /*
021: * Modified by the Sable Research Group and others 1997-1999.
022: * See the 'credits' file distributed with Soot for the complete list of
023: * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
024: */
025:
026: package soot.toolkits.scalar;
027:
028: import soot.options.*;
029:
030: import soot.*;
031:
032: import java.util.*;
033:
034: /**
035: * A BodyTransformer that removes all unused local variables from a given Body.
036: * Implemented as a singleton.
037: * @see BodyTransformer
038: * @see Body
039: */
040: public class UnusedLocalEliminator extends BodyTransformer {
041: public UnusedLocalEliminator(Singletons.Global g) {
042: }
043:
044: public static UnusedLocalEliminator v() {
045: return G.v().soot_toolkits_scalar_UnusedLocalEliminator();
046: }
047:
048: protected void internalTransform(Body body, String phaseName,
049: Map options) {
050: if (Options.v().verbose())
051: G.v().out.println("[" + body.getMethod().getName()
052: + "] Eliminating unused locals...");
053:
054: Set<Value> usedLocals = new HashSet<Value>();
055:
056: // Traverse statements noting all the uses and defs
057: {
058: Iterator unitIt = body.getUnits().iterator();
059:
060: while (unitIt.hasNext()) {
061: Unit s = (Unit) unitIt.next();
062:
063: {
064: Iterator boxIt;
065: boxIt = s.getUseBoxes().iterator();
066: while (boxIt.hasNext()) {
067: Value value = ((ValueBox) boxIt.next())
068: .getValue();
069:
070: if (value instanceof Local
071: && !usedLocals.contains(value))
072: usedLocals.add(value);
073: }
074: boxIt = s.getDefBoxes().iterator();
075: while (boxIt.hasNext()) {
076: Value value = ((ValueBox) boxIt.next())
077: .getValue();
078:
079: if (value instanceof Local
080: && !usedLocals.contains(value))
081: usedLocals.add(value);
082: }
083: }
084: }
085:
086: }
087:
088: // Remove all locals that are unused.
089: {
090: Iterator it = body.getLocals().iterator();
091:
092: while (it.hasNext()) {
093: Local local = (Local) it.next();
094:
095: if (!usedLocals.contains(local))
096: it.remove();
097: }
098: }
099: }
100: }
|