18
18
public class JasminOptimizer {
19
19
20
20
Vector <CodeBlock > codeBlocks ;
21
+ Vector <CodeBlock > assessedBlocks ;
21
22
22
23
public static void main (String args []) {
23
24
@@ -182,13 +183,43 @@ public void optimize() {
182
183
}
183
184
184
185
optimizeJumpsToUnconditionalJumps ();
185
-
186
-
187
-
188
-
189
186
190
187
}
191
188
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
+
192
223
193
224
protected void optimizeBLOCK_LoadPopPairs (CodeBlock b ) {
194
225
LinkedList <Vector <String >> lines = b .getLines ();
@@ -205,8 +236,45 @@ protected void optimizeBLOCK_LoadPopPairs(CodeBlock b) {
205
236
v .add ("; useless load/pop pair removed (including useless instructions in between)" + line );
206
237
i .add (v );
207
238
}
208
- }
209
239
}
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
+ }
210
278
211
279
protected void optimize_PrecalculateArithmeticConstants (CodeBlock b ) {
212
280
LinkedList <Vector <String >> lines = b .getLines ();
@@ -839,6 +907,82 @@ protected void delete_previous(ListIterator<Vector<String>> i) {
839
907
}
840
908
}
841
909
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
+
842
986
843
987
844
988
0 commit comments