📄 optimizer.java
字号:
new InstructionVisitor[] { new WriteOnlyFieldMarker(), new VariableUsageMarker(), }))))); // Mark all fields that are write-only, and mark the used local variables. if (configuration.verbose) { programClassPool.classFilesAccept(new AllFieldVisitor( new WriteOnlyFieldFilter( writeOnlyFieldCounter))); } // Mark all methods that can not be made private. programClassPool.classFilesAccept(new NonPrivateMethodMarker()); libraryClassPool.classFilesAccept(new NonPrivateMethodMarker()); // Make all non-private and unmarked methods in final classes private. programClassPool.classFilesAccept(new ClassFileAccessFilter(ClassConstants.INTERNAL_ACC_FINAL, 0, new AllMethodVisitor( new MemberInfoAccessFilter(0, ClassConstants.INTERNAL_ACC_PRIVATE, new MethodPrivatizer(privateMethodCounter))))); // Mark all used parameters, including the 'this' parameters. programClassPool.classFilesAccept(new AllMethodVisitor( new ParameterUsageMarker())); libraryClassPool.classFilesAccept(new AllMethodVisitor( new ParameterUsageMarker())); if (configuration.assumeNoSideEffects != null) { // Create a visitor for marking methods that don't have any side effects. NoSideEffectMethodMarker noSideEffectMethodMarker = new NoSideEffectMethodMarker(); ClassPoolVisitor noClassPoolvisitor = ClassSpecificationVisitorFactory.createClassPoolVisitor(configuration.assumeNoSideEffects, null, noSideEffectMethodMarker); // Mark the seeds. programClassPool.accept(noClassPoolvisitor); libraryClassPool.accept(noClassPoolvisitor); } // Mark all methods that have side effects. programClassPool.accept(new SideEffectMethodMarker()); // Perform partial evaluation. programClassPool.classFilesAccept(new AllMethodVisitor( new EvaluationSimplifier(pushCounter, branchCounter, deletedCounter))); // Inline interfaces with single implementations. programClassPool.classFilesAccept(new SingleImplementationInliner()); // Restore the interface references from these single implementations. programClassPool.classFilesAccept(new SingleImplementationFixer()); // Shrink the method parameters and make methods static. programClassPool.classFilesAccept(new AllMethodVisitor( new ParameterShrinker(1024, 64, parameterShrinkCounter, staticMethodCounter))); // Fix all references to class files. programClassPool.classFilesAccept(new ClassFileReferenceFixer(true)); // Fix all references to class members. programClassPool.classFilesAccept(new MemberReferenceFixer(1024)); // Create a branch target marker and a code attribute editor that can // be reused for all code attributes. BranchTargetFinder branchTargetFinder = new BranchTargetFinder(1024); CodeAttrInfoEditor codeAttrInfoEditor = new CodeAttrInfoEditor(1024); // Visit all code attributes. // First let the branch marker mark all branch targets. // Then share common blocks of code at branches. // Finally apply all changes to the code. programClassPool.classFilesAccept( new AllMethodVisitor( new AllAttrInfoVisitor( new MultiAttrInfoVisitor( new AttrInfoVisitor[] { branchTargetFinder, new CodeAttrInfoEditorResetter(codeAttrInfoEditor), new AllInstructionVisitor( new GotoCommonCodeReplacer(branchTargetFinder, codeAttrInfoEditor, commonCodeCounter)), codeAttrInfoEditor })))); // Visit all code attributes. // First let the branch marker mark all branch targets. // Then perform peephole optimisations on the instructions: // - Fix invocations of methods that have become private, static,... // - Remove nop instructions. // - Remove push/pop instruction pairs. // - Remove load/store instruction pairs. // - Replace store/load instruction pairs by dup/store instructions. // - Simplify branches to branch instructions. // - Replace branches to return instructions by return instructions. // - Inline simple getters and setters. // Finally apply all changes to the code. programClassPool.classFilesAccept( new AllMethodVisitor( new AllAttrInfoVisitor( new MultiAttrInfoVisitor( new AttrInfoVisitor[] { branchTargetFinder, new CodeAttrInfoEditorResetter(codeAttrInfoEditor), new AllInstructionVisitor( new MultiInstructionVisitor( new InstructionVisitor[] { new MethodInvocationFixer( codeAttrInfoEditor),// new NopRemover( codeAttrInfoEditor, nopCounter), new PushPopRemover( branchTargetFinder, codeAttrInfoEditor, pushPopCounter), new LoadStoreRemover( branchTargetFinder, codeAttrInfoEditor, loadStoreCounter), new StoreLoadReplacer( branchTargetFinder, codeAttrInfoEditor, storeLoadCounter), new GotoGotoReplacer( codeAttrInfoEditor, gotoGotoCounter), new GotoReturnReplacer( codeAttrInfoEditor, gotoReturnCounter), new GetterSetterInliner(configuration.allowAccessModification, codeAttrInfoEditor, getterSetterCounter), })), codeAttrInfoEditor })))); if (configuration.verbose) { System.out.println(" Number of inlined interfaces: "+singleImplementationCounter.getCount()); System.out.println(" Number of finalized classes: "+finalClassCounter .getCount()); System.out.println(" Number of removed write-only fields: "+writeOnlyFieldCounter .getCount()); System.out.println(" Number of finalized methods: "+finalMethodCounter .getCount()); System.out.println(" Number of privatized methods: "+privateMethodCounter .getCount()); System.out.println(" Number of staticized methods: "+staticMethodCounter .getCount()); System.out.println(" Number of simplified method declarations: "+parameterShrinkCounter .getCount()); System.out.println(" Number of inlined getters/setter calls: "+getterSetterCounter .getCount()); System.out.println(" Number of merged code blocks: "+commonCodeCounter .getCount()); System.out.println(" Number of simplified push instructions: "+pushCounter .getCount()); System.out.println(" Number of simplified branches: "+branchCounter .getCount()); System.out.println(" Number of removed instructions: "+deletedCounter .getCount());// System.out.println(" Number of removed nop instructions: "+nopCounter .getCount()); System.out.println(" Number of removed push/pop pairs: "+pushPopCounter .getCount()); System.out.println(" Number of removed load/store pairs: "+loadStoreCounter .getCount()); System.out.println(" Number of simplified store/load pairs: "+storeLoadCounter .getCount()); System.out.println(" Number of simplified goto/goto pairs: "+gotoGotoCounter .getCount()); System.out.println(" Number of simplified goto/return pairs: "+gotoReturnCounter .getCount()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -