Skip to content

Commit 6fbb929

Browse files
committed
added optimization helper functions
1 parent 15221c6 commit 6fbb929

File tree

2 files changed

+149
-5
lines changed

2 files changed

+149
-5
lines changed

bin/JasminOptimizer.class

2.46 KB
Binary file not shown.

src/JasminOptimizer.java

Lines changed: 149 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
public class JasminOptimizer{
1919

2020
Vector<CodeBlock> codeBlocks;
21+
Vector<CodeBlock> assessedBlocks;
2122

2223
public static void main(String args[]) {
2324

@@ -182,13 +183,43 @@ public void optimize() {
182183
}
183184

184185
optimizeJumpsToUnconditionalJumps();
185-
186-
187-
188-
189186

190187
}
191188

189+
protected void optimize_EliminateUselessStore() {
190+
191+
192+
for (int i = 0; i < codeBlocks.size() - 1; i++) {
193+
CodeBlock b = codeBlocks.elementAt(i);
194+
LinkedList<Vector<String>> lines = b.getLines();
195+
196+
for(int j = 0; j<lines.size();j++){
197+
if(isStore(lines.get(j).elementAt(0))){
198+
199+
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
200+
if(leadsStoreToDeadEnd(j, b)){
201+
System.out.println(lines.get(j).elementAt(0));
202+
//lines.remove(j);
203+
204+
lines.get(j).setElementAt("pop", 0);
205+
lines.get(j).add("; Removed store that leads to dead end --- QUESTION 7");
206+
207+
//System.out.println(lines.get(j).elementAt(0));
208+
//lines.get(j).add("; Removed store that leads to dead end --- QUESTION 7 \n");
209+
210+
211+
/*Vector<String> v = new Vector<String>();
212+
v.add("pop");
213+
v \n");
214+
lines.add(v);
215+
++j;*/
216+
}
217+
}
218+
}
219+
220+
}
221+
}
222+
192223

193224
protected void optimizeBLOCK_LoadPopPairs(CodeBlock b) {
194225
LinkedList<Vector<String>> lines = b.getLines();
@@ -205,8 +236,45 @@ protected void optimizeBLOCK_LoadPopPairs(CodeBlock b) {
205236
v.add("; useless load/pop pair removed (including useless instructions in between)" + line);
206237
i.add(v);
207238
}
208-
}
209239
}
240+
}
241+
protected void findRemovableBlocks(CodeBlock b){
242+
243+
if(assessedBlocks.contains(b)) return;
244+
245+
assessedBlocks.addElement(b);
246+
247+
248+
Vector<CodeBlock> successors = b.getSuccessors();
249+
250+
for(int i =0;i<successors.size();i++){
251+
findRemovableBlocks(successors.get(i));
252+
}
253+
}
254+
255+
protected void optimize_RemoveNonVisitedBlocks() {
256+
assessedBlocks = new Vector<CodeBlock>();
257+
258+
findRemovableBlocks(codeBlocks.get(0));
259+
260+
261+
for (int i = 0; i < codeBlocks.size(); i++) {
262+
CodeBlock b = codeBlocks.elementAt(i);
263+
LinkedList<Vector<String>> lines = b.getLines();
264+
265+
for(int j = 0; j<lines.size();j++){
266+
if(isDirective(lines.get(j).elementAt(0))){
267+
268+
if(!assessedBlocks.contains(b)){
269+
findRemovableBlocks(b);
270+
}
271+
}
272+
}
273+
274+
}
275+
276+
codeBlocks = assessedBlocks;
277+
}
210278

211279
protected void optimize_PrecalculateArithmeticConstants(CodeBlock b) {
212280
LinkedList<Vector<String>> lines = b.getLines();
@@ -839,6 +907,82 @@ protected void delete_previous(ListIterator<Vector<String>> i) {
839907
}
840908
}
841909

910+
protected boolean isStore(String s) {
911+
String loadInstructions[] = { "istore", "astore", "fstore"};
912+
for (int i = 0; i < loadInstructions.length; i++) {
913+
if (s.compareTo(loadInstructions[i]) == 0) {
914+
return true;
915+
}
916+
}
917+
return false;
918+
}
919+
920+
protected static boolean isLoad(String s) {
921+
// FIXME: incomplete list
922+
String loadInstructions[] = { "iload", "aload", "fload", "aaload",
923+
"baload", "caload", "daload", "faload", "fload", "iaload"};
924+
for (int i = 0; i < loadInstructions.length; i++) {
925+
if (s.compareTo(loadInstructions[i]) == 0) {
926+
return true;
927+
}
928+
}
929+
return false;
930+
}
931+
932+
protected boolean hasALoad(CodeBlock b, int currentStoreCount){
933+
LinkedList<Vector<String>> lines = b.getLines();
934+
935+
for(int j = 0; j<lines.size();++j){
936+
if(isLoad(lines.get(j).elementAt(0))){
937+
if(currentStoreCount == 0) // no other stores met so load found and no dead end has been reached
938+
return true;
939+
else
940+
--currentStoreCount;
941+
}else if(isStore(lines.get(j).elementAt(0))){
942+
++currentStoreCount;
943+
}
944+
}
945+
946+
Vector<CodeBlock> successors = b.getSuccessors();
947+
for(int i=0;i<successors.size();i++){
948+
if(hasALoad(successors.get(i), currentStoreCount)){
949+
return true;
950+
}
951+
}
952+
return false;
953+
954+
}
955+
956+
protected boolean leadsStoreToDeadEnd(int currentLineIndex, CodeBlock b){
957+
LinkedList<Vector<String>> lines = b.getLines();
958+
959+
int currentStoreCount = 0;
960+
for(int j = currentLineIndex; j<lines.size();++j){
961+
if(isLoad(lines.get(j).elementAt(0))){
962+
if(currentStoreCount == 0) {// no other stores met so load found and no dead end has been reached
963+
//System.out.println("RETURNED" + lines.get(j).elementAt(0));
964+
return false;
965+
}else
966+
--currentStoreCount;
967+
}else if(isStore(lines.get(j).elementAt(0))){
968+
++currentStoreCount;
969+
}
970+
}
971+
972+
// now go through all the reachable blocks. if at least one has a load then
973+
// we cannot assume that we always reach a dead end ! - return false
974+
Vector<CodeBlock> successors = b.getSuccessors();
975+
976+
for(int i=0;i<successors.size();i++){
977+
if(hasALoad(successors.get(i), currentStoreCount)){
978+
return false;
979+
}
980+
}
981+
982+
return true;
983+
984+
}
985+
842986

843987

844988

0 commit comments

Comments
 (0)