Reviewers: Lex, scottb, Description: Intended to be sandwiched between Flattener/UnFlattener, prunes duplicate clinit calls within a single control flow block. Currently prunes about 62 cliints from Showcase (probably saves 7 chars average per clinit, or about 420 bytes). Hopefully this'll get *a lot* better once inter-block information is propagated.
Please review this at http://gwt-code-reviews.appspot.com/80801 Affected files: dev/core/src/com/google/gwt/dev/jjs/impl/flow/ClInitPruner.java Index: dev/core/src/com/google/gwt/dev/jjs/impl/flow/ClInitPruner.java =================================================================== --- dev/core/src/com/google/gwt/dev/jjs/impl/flow/ClInitPruner.java Thu Oct 15 20:29:31 PDT 2009 +++ dev/core/src/com/google/gwt/dev/jjs/impl/flow/ClInitPruner.java Thu Oct 15 20:29:31 PDT 2009 @@ -0,0 +1,85 @@ +/* + * Copyright 2009 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.gwt.dev.jjs.impl.flow; + +import com.google.gwt.dev.jjs.ast.JDeclaredType; +import com.google.gwt.dev.jjs.ast.JExpression; +import com.google.gwt.dev.jjs.ast.JExpressionStatement; +import com.google.gwt.dev.jjs.ast.JMethod; +import com.google.gwt.dev.jjs.ast.JMethodCall; +import com.google.gwt.dev.jjs.ast.JProgram; +import com.google.gwt.dev.jjs.ast.JStatement; +import com.google.gwt.dev.jjs.impl.Tracing; +import com.google.gwt.dev.jjs.impl.WorklistBasedOptimizer; + +import java.util.HashSet; +import java.util.Iterator; + +/** + * Prune clinits that are invoked more than once per block. + */ +public class ClInitPruner extends WorklistBasedOptimizer { + + public static boolean exec(JProgram program) { + return new ClInitPruner(program).execImpl(); + } + + public ClInitPruner(JProgram program) { + super(program); + } + + @Override + protected void optimize(UseDefInfo useDefInfo, JMethod x, UseDef ud) { + } + + @Override + protected void postOptimizeMethod(ControlFlowGraph graph) { + Iterator<BasicBlock> it = graph.reversePostOrderIterator(); + while (it.hasNext()) { + BasicBlock bb = it.next(); + checkClinit(bb, graph.getMethod()); + } + } + + private void checkClinit(BasicBlock bb, JMethod method) { + HashSet<JMethod> clinitsSeen = new HashSet<JMethod>(); + for (JStatement stmt : bb.getStatements()) { + JMethod clinit = getClinit(stmt); + tracer.inc(Tracing.COUNT_CLINIT); + if (clinitsSeen.contains(clinit)) { + tracer.inc(Tracing.COUNT_PRUNABLE_CLINIT); + } else if (clinit != null) { + clinitsSeen.add(clinit); + } + } + } + + private JMethod getClinit(JStatement stmt) { + if (stmt instanceof JExpressionStatement) { + JExpression expr = ((JExpressionStatement) stmt).getExpr(); + if (expr instanceof JMethodCall) { + JMethodCall methCall = (JMethodCall) expr; + JMethod meth = methCall.getTarget(); + JDeclaredType type = meth.getEnclosingType(); + if (type != null && type.getMethods() != null + && type.getMethods().get(0) == meth) { + return meth; + } + } + } + return null; + } +} --~--~---------~--~----~------------~-------~--~----~ http://groups.google.com/group/Google-Web-Toolkit-Contributors -~----------~----~----~----~------~----~------~--~---
