KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > optimize > peephole > LoadStoreRemover


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21 package proguard.optimize.peephole;
22
23 import proguard.classfile.*;
24 import proguard.classfile.attribute.CodeAttribute;
25 import proguard.classfile.editor.CodeAttributeEditor;
26 import proguard.classfile.instruction.*;
27 import proguard.classfile.instruction.visitor.InstructionVisitor;
28 import proguard.classfile.util.SimplifiedVisitor;
29
30 /**
31  * This InstructionVisitor deletes load/store instruction pairs.
32  *
33  * @author Eric Lafortune
34  */

35 public class LoadStoreRemover
36 extends SimplifiedVisitor
37 implements InstructionVisitor
38 {
39     private BranchTargetFinder branchTargetFinder;
40     private CodeAttributeEditor codeAttributeEditor;
41     private InstructionVisitor extraInstructionVisitor;
42
43
44     /**
45      * Creates a new LoadStoreRemover.
46      * @param branchTargetFinder a branch target finder that has been
47      * initialized to indicate branch targets
48      * in the visited code.
49      * @param codeAttributeEditor a code editor that can be used for
50      * accumulating changes to the code.
51      */

52     public LoadStoreRemover(BranchTargetFinder branchTargetFinder,
53                             CodeAttributeEditor codeAttributeEditor)
54     {
55         this(branchTargetFinder, codeAttributeEditor, null);
56     }
57
58
59     /**
60      * Creates a new LoadStoreRemover.
61      * @param branchTargetFinder a branch target finder that has been
62      * initialized to indicate branch targets
63      * in the visited code.
64      * @param codeAttributeEditor a code editor that can be used for
65      * accumulating changes to the code.
66      * @param extraInstructionVisitor an optional extra visitor for all deleted
67      * load instructions.
68      */

69     public LoadStoreRemover(BranchTargetFinder branchTargetFinder,
70                             CodeAttributeEditor codeAttributeEditor,
71                             InstructionVisitor extraInstructionVisitor)
72     {
73         this.branchTargetFinder = branchTargetFinder;
74         this.codeAttributeEditor = codeAttributeEditor;
75         this.extraInstructionVisitor = extraInstructionVisitor;
76     }
77
78
79     // Implementations for InstructionVisitor.
80

81     public void visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction) {}
82
83
84     public void visitVariableInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VariableInstruction variableInstruction)
85     {
86         // Is this instruction a load instruction?
87
if (variableInstruction.isLoad() &&
88             variableInstruction.opcode != InstructionConstants.OP_RET)
89         {
90             int variableIndex = variableInstruction.variableIndex;
91
92             int nextOffset = offset + variableInstruction.length(offset);
93
94             if (!codeAttributeEditor.isModified(offset) &&
95                 !codeAttributeEditor.isModified(nextOffset) &&
96                 !branchTargetFinder.isTarget(nextOffset))
97             {
98                 // Is the next instruction a corresponding store instruction?
99
Instruction nextInstruction = InstructionFactory.create(codeAttribute.code,
100                                                                         nextOffset);
101
102                 if (nextInstruction instanceof VariableInstruction)
103                 {
104                     variableInstruction = (VariableInstruction)nextInstruction;
105                     if (!variableInstruction.isLoad() &&
106                         variableInstruction.opcode != InstructionConstants.OP_IINC &&
107                         variableInstruction.variableIndex == variableIndex)
108                     {
109                         // Delete both instructions.
110
codeAttributeEditor.deleteInstruction(offset);
111                         codeAttributeEditor.deleteInstruction(nextOffset);
112
113                         // Visit the instruction, if required.
114
if (extraInstructionVisitor != null)
115                         {
116                             extraInstructionVisitor.visitVariableInstruction(clazz, method, codeAttribute, offset, variableInstruction);
117                         }
118                     }
119                 }
120             }
121         }
122     }
123 }
124
Popular Tags