All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.gwt.dev.jjs.impl.FullOptimizerContext Maven / Gradle / Ivy

There is a newer version: 2.8.2-v20191108
Show newest version
/*
 * Copyright 2014 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;

import com.google.gwt.dev.jjs.ast.JDeclaredType;
import com.google.gwt.dev.jjs.ast.JField;
import com.google.gwt.dev.jjs.ast.JMethod;
import com.google.gwt.dev.jjs.ast.JNode;
import com.google.gwt.dev.jjs.ast.JProgram;
import com.google.gwt.dev.jjs.ast.JVisitor;
import com.google.gwt.thirdparty.guava.common.collect.HashMultiset;
import com.google.gwt.thirdparty.guava.common.collect.Lists;
import com.google.gwt.thirdparty.guava.common.collect.Multiset;
import com.google.gwt.thirdparty.guava.common.collect.Sets;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * Maintains dependence and modification information for AST optimizers.
 * 

* Is updated incrementally. */ public class FullOptimizerContext implements OptimizerContext { private int optimizationStep = -1; private CallGraph callGraph = new CallGraph(); private FieldReferencesGraph fieldReferencesGraph = new FieldReferencesGraph(); /** * The deleted sub call graph and added sub call graph at each step. */ private List deletedSubCallGraphs = Lists.newArrayList(); private List addedSubCallGraphs = Lists.newArrayList(); // TODO(leafwang): add other dependencies here /** * A mapping from methods to the numbers of the most recent step in which they were modified. */ private Multiset modificationStepByMethod = HashMultiset.create(); /** * A list of modified methods in each step. */ private List> methodsByModificationStep = Lists.newArrayList(); /** * A mapping from methods to the numbers of the most recent step in which they were modified. */ private Multiset modificationStepByField = HashMultiset.create(); /** * A list of modified fields in each step. */ private List> fieldsByModificationStep = Lists.newArrayList(); /** * A mapping from optimizers to their last modification step. */ private Multiset lastStepForOptimizer = HashMultiset.create(); public FullOptimizerContext(JProgram program) { incOptimizationStep(); initializeModifications(program); buildCallGraph(program); buildFieldReferencesGraph(program); incOptimizationStep(); } @Override public Set getCallees(Collection callerMethods) { return callGraph.getCallees(callerMethods); } @Override public Set getCallers(Collection calleeMethods) { return callGraph.getCallers(calleeMethods); } @Override public int getLastStepFor(String optimizerName) { return lastStepForOptimizer.count(optimizerName); } @Override public Set getMethodsByReferencedFields(Collection fields) { return fieldReferencesGraph.getReferencingMethodsForFields(fields); } @Override public Set getModifiedFieldsSince(int stepSince) { Set result = Sets.newLinkedHashSet(); for (int i = stepSince; i < optimizationStep; i++) { result.addAll(fieldsByModificationStep.get(i)); } return result; } @Override public Set getModifiedMethodsSince(int stepSince) { Set result = Sets.newLinkedHashSet(); for (int i = stepSince; i < optimizationStep; i++) { result.addAll(methodsByModificationStep.get(i)); } return result; } @Override public int getOptimizationStep() { return optimizationStep; } @Override public Set getReferencedFieldsByMethods(Collection methods) { return fieldReferencesGraph.getReferencedFieldsByMethods(methods); } @Override public Set getRemovedCalleeMethodsSince(int stepSince) { Set removedCalleeMethods = Sets.newLinkedHashSet(); for (int i = stepSince; i < optimizationStep; i++) { removedCalleeMethods.addAll(deletedSubCallGraphs.get(i).getAllCallees()); } return removedCalleeMethods; } @Override public void incOptimizationStep() { methodsByModificationStep.add(new LinkedHashSet()); fieldsByModificationStep.add(new LinkedHashSet()); deletedSubCallGraphs.add(new CallGraph()); addedSubCallGraphs.add(new CallGraph()); optimizationStep++; } @Override public void markModified(JField modifiedField) { fieldsByModificationStep.get(modificationStepByField.count(modifiedField)).remove( modifiedField); fieldsByModificationStep.get(optimizationStep).add(modifiedField); modificationStepByField.setCount(modifiedField, optimizationStep); // TODO(leafwang): update related dependence information here. } @Override public void markModified(JMethod modifiedMethod) { methodsByModificationStep.get(modificationStepByMethod.count(modifiedMethod)).remove( modifiedMethod); methodsByModificationStep.get(optimizationStep).add(modifiedMethod); modificationStepByMethod.setCount(modifiedMethod, optimizationStep); callGraph.updateCallGraphOfMethod(modifiedMethod, deletedSubCallGraphs.get(optimizationStep), addedSubCallGraphs.get(optimizationStep)); fieldReferencesGraph.updateFieldReferencesOfMethod(modifiedMethod); } @Override public void remove(JField field) { fieldsByModificationStep.get(modificationStepByField.count(field)).remove(field); modificationStepByField.remove(field); fieldReferencesGraph.removeField(field); } @Override public void remove(JMethod method) { methodsByModificationStep.get(modificationStepByMethod.count(method)).remove(method); modificationStepByMethod.remove(method); Set calleeMethods = callGraph.removeCallerMethod(method); deletedSubCallGraphs.get(optimizationStep).addCallerMethod(method, Sets.difference(calleeMethods, callGraph.getCallees(Collections.singleton(method)))); addedSubCallGraphs.get(optimizationStep).addCallerMethod(method, Sets.difference(callGraph.getCallees(Collections.singleton(method)), calleeMethods)); fieldReferencesGraph.removeMethod(method); } @Override public void removeFields(Collection fields) { for (JField field : fields) { remove(field); } } @Override public void removeMethods(Collection methods) { for (JMethod method : methods) { remove(method); } } @Override public void setLastStepFor(String optimizerName, int step) { lastStepForOptimizer.setCount(optimizerName, step); } @Override public void syncDeletedSubCallGraphsSince(int step, Collection prunedMethods) { for (int i = step; i < optimizationStep; i++) { for (JMethod prunedMethod : prunedMethods) { deletedSubCallGraphs.get(i).removeCallerMethod(prunedMethod); deletedSubCallGraphs.get(i).removeCalleeMethod(prunedMethod); } } } @Override public void traverse(JVisitor visitor, Set nodes) { assert (nodes != null); for (JNode node : nodes) { visitor.accept(node); } } private void buildCallGraph(JProgram program) { callGraph.buildCallGraph(program); } private void buildFieldReferencesGraph(JProgram program) { fieldReferencesGraph.buildFieldReferencesGraph(program); } private void initializeModifications(JProgram program) { assert optimizationStep == 0; for (JDeclaredType type : program.getModuleDeclaredTypes()) { fieldsByModificationStep.get(0).addAll(type.getFields()); methodsByModificationStep.get(0).addAll(type.getMethods()); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy