head	1.5;
access;
symbols
	bg2_23:1.4
	bg2_22:1.4
	bg2_21:1.4
	bg2_20:1.4
	bg2_16:1.4
	bg2_15:1.4
	bg2_12:1.4
	bg2_07:1.4;
locks; strict;
comment	@# @;


1.5
date	2008.10.01.12.05.12;	author jeuneS2;	state Exp;
branches;
next	1.4;
commitid	46c348e3674b4567;

1.4
date	2008.02.18.02.04.45;	author stefant;	state Exp;
branches;
next	1.3;
commitid	59a247b8e7bb4567;

1.3
date	2008.02.18.01.59.04;	author stefant;	state Exp;
branches;
next	1.2;
commitid	56ee47b8e6664567;

1.2
date	2008.02.17.20.47.30;	author stefant;	state Exp;
branches;
next	1.1;
commitid	6be447b89d5a4567;

1.1
date	2008.01.13.20.43.55;	author stefant;	state Exp;
branches;
next	;
commitid	4e54478a77ec4567;


desc
@@


1.5
log
@Bugfixes and two new peephole optimizations.
@
text
@/*
 * Copyright (c) 2007,2008, Stefan Hepp
 *
 * This file is part of JOPtimizer.
 *
 * JOPtimizer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * JOPtimizer 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 Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package joptimizer.optimizer;

import com.jopdesign.libgraph.cfg.ControlFlowGraph;
import com.jopdesign.libgraph.cfg.GraphException;
import com.jopdesign.libgraph.cfg.block.CodeBlock;
import com.jopdesign.libgraph.cfg.statements.StmtHandle;
import com.jopdesign.libgraph.struct.MethodInfo;
import joptimizer.config.JopConfig;
import joptimizer.framework.JOPtimizer;
import joptimizer.framework.actions.AbstractGraphAction;
import joptimizer.framework.actions.ActionException;
import joptimizer.optimizer.peephole.PeepGetfield;
import joptimizer.optimizer.peephole.PeepGoto;
import joptimizer.optimizer.peephole.PeepInc;
import joptimizer.optimizer.peephole.PeepLoadPop;
import joptimizer.optimizer.peephole.PeepNop;
import joptimizer.optimizer.peephole.PeepOptimization;
import org.apache.log4j.Logger;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * A peephole bytecode optimizer.
 * 
 * @@author Stefan Hepp, e0026640@@student.tuwien.ac.at
 */
public class PeepholeOptimizer extends AbstractGraphAction {
    
    public static final String ACTION_NAME = "peephole";

    private List optimizer;
    private int[] matches;

    private static final Logger logger = Logger.getLogger(PeepholeOptimizer.class);

    public PeepholeOptimizer(String name, String id, JOPtimizer joptimizer) {
        super(name, id, joptimizer);
    }

    public void appendActionArguments(List options) {
    }

    public String getActionDescription() {
        return "Run some peephole optimizations.";
    }

    public boolean doModifyClasses() {
        return true;
    }

    public boolean configure(JopConfig config) {

        optimizer = new LinkedList();

        // TODO get list of used optimizers from config

        optimizer.add(new PeepNop());
        optimizer.add(new PeepGoto());
        optimizer.add(new PeepGetfield());
        optimizer.add(new PeepInc());
        optimizer.add(new PeepLoadPop());

        return true;
    }

    public int getGraphStage() {
        return STAGE_STACK_TO_BYTECODE;
    }

    public int getRequiredForm() {
        return 0;
    }

    public void startAction() throws ActionException {
        for (Iterator it = optimizer.iterator(); it.hasNext();) {
            PeepOptimization optimization = (PeepOptimization) it.next();
            optimization.startOptimizer();
        }
        matches = new int[optimizer.size()];
    }

    public void finishAction() throws ActionException {
        int m = 0;
        for (Iterator it = optimizer.iterator(); it.hasNext();) {
            PeepOptimization optimization = (PeepOptimization) it.next();
            optimization.finishOptimizer();
            
            if (logger.isInfoEnabled()) {
                logger.info("Found {" + matches[m] + "} matches for {" + optimization.getClass().getName() + "}.");
            }
            m++;
        }
    }

    public void execute(MethodInfo methodInfo, ControlFlowGraph graph) throws ActionException {

        int m = 0;
        for (Iterator it = optimizer.iterator(); it.hasNext();) {
            PeepOptimization optimization = (PeepOptimization) it.next();

            if ( !optimization.startGraph(graph) ) {
                logger.warn("Could not start optimization {"+optimization.getClass().getName()+"}, skipped.");
                continue;
            }

            Class firstClass = optimization.getFirstStmtClass();

            for (int i = 0; i < graph.getBlockCount(); i++ ) {

                CodeBlock code = graph.getBlock(i).getCodeBlock();
				boolean match = false;

                for (int j = 0; j < code.size(); j++) {
                    StmtHandle handle = code.getStmtHandle(j);

                    if ( firstClass == null || firstClass.isAssignableFrom( handle.getStatement().getClass() ) ) {

                        StmtHandle next = optimization.processStatement(handle);

                        if ( next != null ) {
                            matches[m]++;
							match = true;
                            // TODO set loop to next
                            next.dispose();
                        }
                        
                    }

                    handle.dispose();
                }

				if (match) {
					graph.setModified(true);
				}
            }

            m++;
        }

		try {
			methodInfo.getMethodCode().compileGraph();
		} catch (GraphException e) {
			if ( getJopConfig().doIgnoreActionErrors() ) {
				logger.warn("Could peephole optimize {"+methodInfo.getFQMethodName()+"}, skipping.", e);
			} else {
				throw new ActionException("Could not get CFG for method.", e);
			}
		}

    }
}
@


1.4
log
@fixed stats output
@
text
@d22 1
d32 2
d80 2
d131 1
d137 1
d142 1
a142 1

d152 3
d160 10
@


1.3
log
@added first simple peephole optimizer
@
text
@d106 1
@


1.2
log
@initializing all actions with id, fallback for configuration to default name, new syntax for action-options.
@
text
@d22 2
d29 5
d35 2
d48 5
d69 10
a78 1
        return false;
d89 20
d110 39
@


1.1
log
@added joptimizer sources
@
text
@d1 68
a68 68
/*
 * Copyright (c) 2007,2008, Stefan Hepp
 *
 * This file is part of JOPtimizer.
 *
 * JOPtimizer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * JOPtimizer 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 Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package joptimizer.optimizer;

import joptimizer.config.JopConfig;
import joptimizer.framework.JOPtimizer;
import joptimizer.framework.actions.AbstractGraphAction;
import joptimizer.framework.actions.ActionException;
import com.jopdesign.libgraph.cfg.ControlFlowGraph;
import com.jopdesign.libgraph.struct.MethodInfo;

import java.util.List;

/**
 * A peephole bytecode optimizer.
 * 
 * @@author Stefan Hepp, e0026640@@student.tuwien.ac.at
 */
public class PeepholeOptimizer extends AbstractGraphAction {
    
    public static final String ACTION_NAME = "peephole";

    public PeepholeOptimizer(String name, JOPtimizer joptimizer) {
        super(name, joptimizer);
    }

    public void appendActionArguments(String prefix, List options) {
    }

    public String getActionDescription() {
        return "Run some peephole optimizations.";
    }

    public boolean doModifyClasses() {
        return true;
    }

    public boolean configure(String prefix, JopConfig config) {
        return false;
    }

    public int getDefaultStage() {
        return STAGE_STACK_TO_BYTECODE;
    }

    public int getRequiredForm() {
        return 0;
    }

    public void execute(MethodInfo methodInfo, ControlFlowGraph graph) throws ActionException {
    }
}
@

