KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jarg > DataflowOptimizer


1 /* ====================================================================
2  * Copyright (c) 2002, Hidetoshi Ohuchi <hchacha@users.sourceforge.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * Neither the name of the hchacha nor the names of its contributors
17  * may be used to endorse or promote products derived from this
18  * software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  * ====================================================================
33  */

34 package jarg;
35
36 import java.util.*;
37 import org.apache.bcel.classfile.*;
38 import org.apache.bcel.generic.*;
39 import org.apache.bcel.Constants;
40
41 /**
42  * The class for optimizing the byte-code by analizing data-flow.
43  *
44  * @version $Id: DataflowOptimizer.java,v 1.5 2002/04/27 12:26:22 hchacha Exp $
45  * @author Hidetoshi Ohuchi &lt;hchacha@users.sourceforge.net&gt;
46  */

47 class DataflowOptimizer {
48    private Jarg app;
49    private DataflowAnalizer analizer;
50
51    DataflowOptimizer(Jarg app) {
52       this.app = app;
53       this.analizer = new DataflowAnalizer(app);
54    }
55
56    int optimizingByDataflow(MethodGen mg) {
57       int count = 0;
58       HashMap dataMap = analizer.analize(mg); // HashMap<InstructionHandle, InstructionDataset>
59

60       count += constInlining(mg, dataMap);
61
62       return count;
63    }
64
65    // const inlining
66
//
67
// ICONST_0 ...
68
// ISTORE %4 ...
69
// ... ...
70
// ILOAD %4 ICONST_0
71
// ... ...
72
// ILOAD %4 ICONST_0
73
// ... ...
74
private int constInlining(MethodGen mg, HashMap dataMap) {
75       int count = 0;
76       CodeExceptionGen[] ceg = mg.getExceptionHandlers();
77       InstructionList il = mg.getInstructionList();
78
79       count += constInlining0(il, il.getStart(), dataMap, new HashSet());
80 // for (int i=0; i<ceg.length; i++) {
81
// count += constInlining0(il, ceg[i].getHandlerPC());
82
// }
83

84       if (count > 0 && app.isVerboseBCO) {
85          System.out.println("Replaced " + count + " xCONSTn/xSTOREn//xLOADn --> xCONSTn : " + mg.getClassName() + '#' + mg.getName());
86       }
87       return count;
88    }
89
90    private int constInlining0(InstructionList il, InstructionHandle first, HashMap dataMap, HashSet hset) {
91       int count = 0;
92       for (; first != null; ) {
93          Instruction ins1 = first.getInstruction();
94
95          if (ins1 instanceof BranchInstruction) {
96             // buggy after branch.
97
break;
98 /*
99             if (ins1 instanceof GotoInstruction) {
100                break;
101             } else if (ins1 instanceof Select) {
102                // can not do after select.
103                break;
104 // InstructionHandle[] targets = ((Select)ins1).getTargets();
105 // for (int i=0; i<targets.length; i++) {
106 // InstructionHandle targetn = targets[i];
107 // if (!hset.contains(targetn)) {
108 // hset.add(targetn);
109 // count += constInlining0(il, target, dataMap, hset);
110 // }
111 // }
112             } else {
113                InstructionHandle target = ((BranchInstruction)ins1).getTarget();
114                if (!hset.contains(target)) {
115                   hset.add(target);
116                   count += constInlining0(il, target, dataMap, hset);
117                }
118             }
119 */

120          } else if (ins1 instanceof ATHROW) {
121             break;
122          } else if (ins1 instanceof RET) {
123             break;
124          } else if (ins1 instanceof ReturnInstruction) {
125             break;
126
127          } else if (ins1 instanceof ALOAD) {
128             int vn = ((ALOAD)ins1).getIndex();
129             DataflowAnalizer.InstructionDataset dset
130                   = (DataflowAnalizer.InstructionDataset)dataMap.get(first);
131             if (dset != null) {
132                Type t = dset.getLocalType(vn);
133                if (t instanceof CONSTNull) {
134                   CONSTNull tt = (CONSTNull)t;
135                   Instruction insN = tt.createInstruction();
136
137                   InstructionHandle next = first.getNext();
138                   count ++;
139                   InstructionHandle newIh = il.insert(first, insN);
140                   if (first.hasTargeters()) {
141                      updateTargets(first, newIh, newIh.getPrev());
142                   }
143                   deleteInstruction(il, first, next);
144                   first = next;
145                   continue;
146                }
147             }
148          } else if (ins1 instanceof ILOAD) {
149             int vn = ((ILOAD)ins1).getIndex();
150             DataflowAnalizer.InstructionDataset dset
151                   = (DataflowAnalizer.InstructionDataset)dataMap.get(first);
152             if (dset != null) {
153                Type t = dset.getLocalType(vn);
154                if (t instanceof ConstantIntType) {
155                   ConstantIntType tt = (ConstantIntType)t;
156                   Instruction insN = tt.createInstruction();
157
158                   InstructionHandle next = first.getNext();
159                   count ++;
160                   InstructionHandle newIh = il.insert(first, insN);
161                   if (first.hasTargeters()) {
162                      updateTargets(first, newIh, newIh.getPrev());
163                   }
164                   deleteInstruction(il, first, next);
165                   first = next;
166                   continue;
167                }
168             }
169          }
170          first = first.getNext();
171       }
172       return count;
173    }
174
175    private void deleteInstruction(InstructionList il, InstructionHandle ih, InstructionHandle next) {
176       InstructionHandle prev = ih.getPrev();
177       if (prev == null) {
178          prev = ih.getNext();
179       }
180
181       try {
182          il.delete(ih);
183       } catch (TargetLostException ex) {
184          InstructionHandle[] targets = ex.getTargets();
185          for (int i=0; i < targets.length; i++) {
186             updateTargets(targets[i], next, prev);
187          }
188       }
189    }
190
191    private void updateTargets(InstructionHandle oldTarget, InstructionHandle newTarget, InstructionHandle newTargetPrev) {
192       InstructionTargeter[] targeters = oldTarget.getTargeters();
193       for (int j=0; j < targeters.length; j++) {
194          InstructionHandle curTarget = newTarget;
195          InstructionTargeter targeter = targeters[j];
196          if (targeter instanceof CodeExceptionGen) {
197             if (((CodeExceptionGen)targeter).getEndPC() == oldTarget) {
198                curTarget = newTargetPrev;
199             }
200          }
201          targeter.updateTarget(oldTarget, curTarget);
202       }
203    }
204 }
205
Popular Tags