Skip to content

Commit 15221c6

Browse files
committed
optimizing removal of useless load/pop pairs
1 parent b6b4c45 commit 15221c6

File tree

5 files changed

+125
-77
lines changed

5 files changed

+125
-77
lines changed

bin/JasminOptimizer.class

1.57 KB
Binary file not shown.

bin/output.txt

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +0,0 @@
1-
;;;;;;;;; Begin Block 0 ;;;;;;;;;
2-
.class public simple
3-
.super java/lang/Object
4-
;;;;;;;;; End Block 0 ;;;;;;;;;
5-
;;;;;;;;; Begin Block 1 ;;;;;;;;;
6-
.method public <init>()V
7-
aload_0
8-
invokenonvirtual java/lang/Object/<init>()V
9-
return
10-
;;;;;;;;; End Block 1 ;;;;;;;;;
11-
;;;;;;;;; Begin Block 2 ;;;;;;;;;
12-
.end method
13-
;;;;;;;;; End Block 2 ;;;;;;;;;
14-
;;;;;;;;; Begin Block 3 ;;;;;;;;;
15-
.method public static println(I)I
16-
.limit stack 5
17-
.limit locals 2
18-
getstatic java/lang/System/out Ljava/io/PrintStream;
19-
iload 0
20-
ifeq false_label
21-
; Block 3 exits to blocks 4 5
22-
;;;;;;;;; End Block 3 ;;;;;;;;;
23-
;;;;;;;;; Begin Block 4 ;;;;;;;;;
24-
ldc "true"
25-
ifne dummy_label
26-
; Block 4 exits to blocks 5 7
27-
;;;;;;;;; End Block 4 ;;;;;;;;;
28-
;;;;;;;;; Begin Block 5 ;;;;;;;;;
29-
false_label: ;label (target of jump)
30-
ldc "false"
31-
ldc 0
32-
ifne dummy_label
33-
; Block 5 exits to blocks 6 7
34-
;;;;;;;;; End Block 5 ;;;;;;;;;
35-
;;;;;;;;; Begin Block 6 ;;;;;;;;;
36-
ldc 6
37-
dup
38-
fadd
39-
ldc 2
40-
ldc 2
41-
ldc 2
42-
fmul
43-
ldc "table"
44-
astore
45-
;;;;;;;;; End Block 6 ;;;;;;;;;
46-
;;;;;;;;; Begin Block 7 ;;;;;;;;;
47-
dummy_label: ;label (target of jump)
48-
pop
49-
ldc 0
50-
fload 2.0
51-
ldc 3
52-
fmul
53-
.end method
54-
;;;;;;;;; End Block 7 ;;;;;;;;;
55-
;;;;;;;;; Begin Block 8 ;;;;;;;;;
56-
.method public static main()F
57-
.limit stack 50
58-
.limit locals 50
59-
ldc 0.0
60-
fstore 0
61-
ldc "test"
62-
pop
63-
fload 0
64-
freturn
65-
;;;;;;;;; End Block 8 ;;;;;;;;;
66-
;;;;;;;;; Begin Block 9 ;;;;;;;;;
67-
.end method
68-
;;;;;;;;; End Block 9 ;;;;;;;;;

bin/simple.j

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
; Begin standard header
2+
.class public simple
3+
.super java/lang/Object
4+
.method public <init>()V
5+
aload_0
6+
7+
invokenonvirtual java/lang/Object/<init>()V
8+
return
9+
.end method
10+
.method public static main([Ljava/lang/String;)V
11+
invokestatic simple/main()F
12+
return
13+
.end method
14+
; End standard header
15+
16+
.method public static main()F
17+
.limit stack 50
18+
.limit locals 50
19+
ldc 0.0
20+
fstore 0 ; i
21+
ldc 5.0
22+
fstore 0 ; i
23+
fload 0 ;i
24+
ldc 2.0
25+
fmul
26+
ldc 2.0
27+
fmul
28+
ldc 2.0
29+
fmul
30+
pop
31+
ldc 0.0
32+
fstore 1 ; x
33+
ldc 8.0
34+
fstore 1 ; x
35+
ldc 2.0
36+
ldc 2.0
37+
;fmul
38+
fload 1 ;x
39+
fmul
40+
pop
41+
ldc 0.0
42+
freturn
43+
.end method
44+
; Begin standard trailer

bin/simple22.j

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,21 @@ false_label:
3939
;ldc 2
4040
fdiv
4141

42+
fload 7
43+
ldc 8.0
44+
fmul
45+
fload 2
46+
ldc 5.0
47+
fadd
48+
fmul
49+
pop
50+
4251
cipher_label:
4352
ldc "table"
4453
astore
4554
dummy_label:
46-
pop
55+
56+
;pop
4757
ldc 0
4858
fload 2.0
4959
ldc 3

src/JasminOptimizer.java

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,14 @@ else if (isReturn(first)) {
171171
*/
172172
public void optimize() {
173173
for (int i = 0; i < codeBlocks.size(); i++) {
174-
CodeBlock b = codeBlocks.elementAt(i);
174+
175+
CodeBlock b = codeBlocks.elementAt(i);
176+
optimizeBlock(b);
177+
optimizeBLOCK_LoadPopPairs(b);
175178

176-
optimizeBlock(b);
177179
optimize_PrecalculateArithmeticConstants(b);
178180

181+
179182
}
180183

181184
optimizeJumpsToUnconditionalJumps();
@@ -186,15 +189,29 @@ public void optimize() {
186189

187190
}
188191

192+
193+
protected void optimizeBLOCK_LoadPopPairs(CodeBlock b) {
194+
LinkedList<Vector<String>> lines = b.getLines();
195+
ListIterator<Vector<String>> i = lines.listIterator();
196+
while (i.hasNext()) {
197+
Vector line = (Vector) i.next();
198+
String ins = (String) line.elementAt(0);
199+
200+
if (isPopInstruction(ins)) {
201+
202+
deletePrevious2(i); // the pop and the popped = 2
203+
Vector<String> v = new Vector<String>(1);
204+
205+
v.add("; useless load/pop pair removed (including useless instructions in between)" + line);
206+
i.add(v);
207+
}
208+
}
209+
}
210+
189211
protected void optimize_PrecalculateArithmeticConstants(CodeBlock b) {
190212
LinkedList<Vector<String>> lines = b.getLines();
191213
ListIterator<Vector<String>> i = lines.listIterator();
192-
193-
// since each mul or add or any other operation
194-
// of the sort takes two paramters, we go two up
195-
// which is the closest to 0 that we can stumble
196-
// upon an arithmetic operation
197-
214+
198215
if(i.hasNext()) i.next();
199216
if(i.hasNext()) i.next();
200217

@@ -779,5 +796,50 @@ protected static boolean isMultiplication(String s) {
779796
return false;
780797
}
781798

799+
protected boolean isRemovableONEaboveString(String s){
800+
801+
String instructions[] = { "fload", "ldc" , "pop"};
802+
for (int i = 0; i < instructions.length; i++) {
803+
if (s.compareTo(instructions[i]) == 0) {
804+
return true;
805+
}
806+
}
807+
return false;
808+
}
809+
protected boolean isRemovableTWOaboveString(String s){
810+
811+
String instructions[] = { "fmul", "dmul", "imul", "lmul", "fadd", "dadd", "iadd", "ladd", "fdiv", "ddiv", "idiv", "ldiv", "fsub", "dsub", "isub", "lsub" };
812+
for (int i = 0; i < instructions.length; i++) {
813+
if (s.compareTo(instructions[i]) == 0) {
814+
return true;
815+
}
816+
}
817+
return false;
818+
}
819+
820+
821+
protected void deletePrevious2(ListIterator<Vector<String>> i) {
822+
delete_previous(i);
823+
delete_previous(i);
824+
}
825+
protected void delete_previous(ListIterator<Vector<String>> i) {
826+
827+
i.remove();
828+
if(!i.hasPrevious()) return;
829+
i.previous();
830+
Vector line = (Vector) i.next();
831+
String ins = (String) line.elementAt(0);
832+
833+
if(isRemovableONEaboveString(ins)){
834+
delete_previous(i);
835+
}else if(isRemovableTWOaboveString(ins)){
836+
deletePrevious2(i);
837+
}else{
838+
return;
839+
}
840+
}
841+
842+
843+
782844

783845
}

0 commit comments

Comments
 (0)