KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > toolkits > scalar > ConstantPropagatorAndFolder


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1999 Phong Co
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27
28 package soot.jimple.toolkits.scalar;
29 import soot.options.*;
30
31 import soot.util.*;
32 import soot.*;
33 import soot.toolkits.scalar.*;
34 import soot.jimple.*;
35 import java.io.*;
36 import java.util.*;
37 import soot.toolkits.graph.*;
38
39 /** Does constant propagation and folding.
40  * Constant folding is the compile-time evaluation of constant
41  * expressions (i.e. 2 * 3). */

42 public class ConstantPropagatorAndFolder extends BodyTransformer
43 {
44     public ConstantPropagatorAndFolder( Singletons.Global g ) {}
45     public static ConstantPropagatorAndFolder v() { return G.v().soot_jimple_toolkits_scalar_ConstantPropagatorAndFolder(); }
46
47     protected void internalTransform(Body b, String JavaDoc phaseName, Map options)
48     {
49         StmtBody stmtBody = (StmtBody)b;
50         int numFolded = 0;
51         int numPropagated = 0;
52
53         if (Options.v().verbose())
54             G.v().out.println("[" + stmtBody.getMethod().getName() +
55                                "] Propagating and folding constants...");
56
57         Chain units = stmtBody.getUnits();
58         ExceptionalUnitGraph unitGraph = new ExceptionalUnitGraph(stmtBody);
59         LocalDefs localDefs;
60         
61         localDefs = new SmartLocalDefs(unitGraph, new SimpleLiveLocals(unitGraph));
62
63         // Perform a constant/local propagation pass.
64
Iterator stmtIt = (new PseudoTopologicalOrderer()).newList(unitGraph).iterator();
65
66         // go through each use box in each statement
67
while (stmtIt.hasNext()) {
68             Stmt stmt = (Stmt) stmtIt.next();
69
70             // propagation pass
71
Iterator useBoxIt = stmt.getUseBoxes().iterator();
72             ValueBox useBox;
73
74             while (useBoxIt.hasNext()) {
75                 useBox = (ValueBox) useBoxIt.next();
76                 if (useBox.getValue() instanceof Local) {
77                     Local local = (Local) useBox.getValue();
78                     List defsOfUse = localDefs.getDefsOfAt(local, stmt);
79                     if (defsOfUse.size() == 1) {
80                         DefinitionStmt defStmt =
81                             (DefinitionStmt) defsOfUse.get(0);
82                         if (defStmt.getRightOp() instanceof NumericConstant) {
83                             if (useBox.canContainValue(defStmt.getRightOp())) {
84                                 useBox.setValue(defStmt.getRightOp());
85                                 numPropagated++;
86                             }
87                         }
88                     }
89                 }
90             }
91                 
92             // folding pass
93
useBoxIt = stmt.getUseBoxes().iterator();
94
95             while (useBoxIt.hasNext()) {
96                 useBox = (ValueBox) useBoxIt.next();
97                 Value value = useBox.getValue();
98                 if (!(value instanceof Constant)) {
99                     if (Evaluator.isValueConstantValued(value)) {
100                         Value constValue =
101                             Evaluator.getConstantValueOf(value);
102                         if (useBox.canContainValue(constValue)) {
103                             useBox.setValue(constValue);
104                             numFolded++;
105                         }
106                     }
107                 }
108             }
109         }
110
111        if (Options.v().verbose())
112             G.v().out.println("[" + stmtBody.getMethod().getName() +
113                 "] Propagated: " + numPropagated + ", Folded: " + numFolded);
114
115     } // optimizeConstants
116

117 }
118     
119
120
121
122
123
Popular Tags