1 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 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); 60 count += constInlining(mg, dataMap); 61 62 return count; 63 } 64 65 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 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 break; 98 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 |