001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 1999 Phong Co
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.jimple.toolkits.scalar;
027:
028: import soot.options.*;
029:
030: import soot.*;
031: import soot.toolkits.scalar.*;
032: import soot.jimple.*;
033: import java.util.*;
034: import soot.toolkits.graph.*;
035:
036: /** Does constant propagation and folding.
037: * Constant folding is the compile-time evaluation of constant
038: * expressions (i.e. 2 * 3). */
039: public class ConstantPropagatorAndFolder extends BodyTransformer {
040: public ConstantPropagatorAndFolder(Singletons.Global g) {
041: }
042:
043: public static ConstantPropagatorAndFolder v() {
044: return G
045: .v()
046: .soot_jimple_toolkits_scalar_ConstantPropagatorAndFolder();
047: }
048:
049: protected void internalTransform(Body b, String phaseName,
050: Map options) {
051: StmtBody stmtBody = (StmtBody) b;
052: int numFolded = 0;
053: int numPropagated = 0;
054:
055: if (Options.v().verbose())
056: G.v().out.println("[" + stmtBody.getMethod().getName()
057: + "] Propagating and folding constants...");
058:
059: ExceptionalUnitGraph unitGraph = new ExceptionalUnitGraph(
060: stmtBody);
061: LocalDefs localDefs;
062:
063: localDefs = new SmartLocalDefs(unitGraph, new SimpleLiveLocals(
064: unitGraph));
065:
066: // Perform a constant/local propagation pass.
067: Iterator stmtIt = (new PseudoTopologicalOrderer()).newList(
068: unitGraph, false).iterator();
069:
070: // go through each use box in each statement
071: while (stmtIt.hasNext()) {
072: Stmt stmt = (Stmt) stmtIt.next();
073:
074: // propagation pass
075: Iterator useBoxIt = stmt.getUseBoxes().iterator();
076: ValueBox useBox;
077:
078: while (useBoxIt.hasNext()) {
079: useBox = (ValueBox) useBoxIt.next();
080: if (useBox.getValue() instanceof Local) {
081: Local local = (Local) useBox.getValue();
082: List<Unit> defsOfUse = localDefs.getDefsOfAt(local,
083: stmt);
084: if (defsOfUse.size() == 1) {
085: DefinitionStmt defStmt = (DefinitionStmt) defsOfUse
086: .get(0);
087: if (defStmt.getRightOp() instanceof NumericConstant) {
088: if (useBox.canContainValue(defStmt
089: .getRightOp())) {
090: useBox.setValue(defStmt.getRightOp());
091: numPropagated++;
092: }
093: }
094: }
095: }
096: }
097:
098: // folding pass
099: useBoxIt = stmt.getUseBoxes().iterator();
100:
101: while (useBoxIt.hasNext()) {
102: useBox = (ValueBox) useBoxIt.next();
103: Value value = useBox.getValue();
104: if (!(value instanceof Constant)) {
105: if (Evaluator.isValueConstantValued(value)) {
106: Value constValue = Evaluator
107: .getConstantValueOf(value);
108: if (useBox.canContainValue(constValue)) {
109: useBox.setValue(constValue);
110: numFolded++;
111: }
112: }
113: }
114: }
115: }
116:
117: if (Options.v().verbose())
118: G.v().out.println("[" + stmtBody.getMethod().getName()
119: + "] Propagated: " + numPropagated
120: + ", Folded: " + numFolded);
121:
122: } // optimizeConstants
123:
124: }
|