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

soot.baf.toolkits.base.StoreChainOptimizer Maven / Gradle / Ivy

package soot.baf.toolkits.base;

/*-
 * #%L
 * Soot - a J*va Optimization Framework
 * %%
 * Copyright (C) 1997 - 2018 Raja Vallée-Rai and others
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2.1 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import soot.Body;
import soot.BodyTransformer;
import soot.G;
import soot.Local;
import soot.Singletons;
import soot.Unit;
import soot.baf.PushInst;
import soot.baf.StoreInst;
import soot.toolkits.scalar.Pair;

/**
 * Due to local packing, we may have chains of assignments to the same local.
 * 
 * push null; store.r $r2;
 *
 * push null; store.r $r2;
 * 
 * This transformer eliminates the redundant push/store instructions.
 * 
 * @author Steven Arzt
 *
 */
public class StoreChainOptimizer extends BodyTransformer {

  public StoreChainOptimizer(Singletons.Global g) {
  }

  public static StoreChainOptimizer v() {
    return G.v().soot_baf_toolkits_base_StoreChainOptimizer();
  }

  @Override
  protected void internalTransform(Body b, String phaseName, Map options) {
    // We keep track of all the stored values
    Map> stores = new HashMap>();
    Set toRemove = new HashSet();

    Unit lastPush = null;
    for (Unit u : b.getUnits()) {
      // If we can jump here from somewhere, do not modify this code
      if (!u.getBoxesPointingToThis().isEmpty()) {
        stores.clear();
        lastPush = null;
      }
      // Emulate pushing stuff on the stack
      else if (u instanceof PushInst) {
        lastPush = u;
      } else if (u instanceof StoreInst && lastPush != null) {
        StoreInst si = (StoreInst) u;
        Pair pushStorePair = stores.get(si.getLocal());
        if (pushStorePair != null) {
          // We can remove the push and the store
          toRemove.add(pushStorePair.getO1());
          toRemove.add(pushStorePair.getO2());
        }

        stores.put(si.getLocal(), new Pair(lastPush, u));
      } else {
        // We're outside of the trivial initialization chain
        stores.clear();
        lastPush = null;
      }
    }

    b.getUnits().removeAll(toRemove);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy