/* * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code 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 * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package org.graalvm.compiler.lir; import java.util.ArrayList; import java.util.List; import org.graalvm.compiler.core.common.cfg.AbstractBlockBase; import org.graalvm.compiler.lir.StandardOp.LoadConstantOp; import org.graalvm.compiler.lir.StandardOp.MoveOp; import org.graalvm.compiler.lir.StandardOp.ValueMoveOp; import org.graalvm.compiler.lir.gen.LIRGenerationResult; import org.graalvm.compiler.lir.phases.PostAllocationOptimizationPhase; import jdk.vm.ci.code.TargetDescription; /** * This class optimizes moves, particularly those that result from eliminating SSA form. *
* When a block has more than one predecessor, and all predecessors end with the * {@linkplain Optimizer#same(LIRInstruction, LIRInstruction) same} sequence of {@linkplain MoveOp * move} instructions, then these sequences can be replaced with a single copy of the sequence at * the beginning of the block. *
* Similarly, when a block has more than one successor, then same sequences of moves at the * beginning of the successors can be placed once at the end of the block. But because the moves * must be inserted before all branch instructions, this works only when there is exactly one * conditional branch at the end of the block (because the moves must be inserted before all * branches, but after all compares). *
* This optimization affects all kind of moves (reg->reg, reg->stack and stack->reg).
* Because this optimization works best when a block contains only a few moves, it has a huge impact
* on the number of blocks that are totally empty.
*/
public final class EdgeMoveOptimizer extends PostAllocationOptimizationPhase {
@Override
protected void run(TargetDescription target, LIRGenerationResult lirGenRes, PostAllocationOptimizationContext context) {
LIR ir = lirGenRes.getLIR();
Optimizer optimizer = new Optimizer(ir);
AbstractBlockBase>[] blockList = ir.linearScanOrder();
// ignore the first block in the list (index 0 is not processed)
for (int i = blockList.length - 1; i >= 1; i--) {
AbstractBlockBase> block = blockList[i];
if (block.getPredecessorCount() > 1) {
optimizer.optimizeMovesAtBlockEnd(block);
}
if (block.getSuccessorCount() == 2) {
optimizer.optimizeMovesAtBlockBegin(block);
}
}
}
private static final class Optimizer {
private final List> edgeInstructionSeqences;
private LIR ir;
Optimizer(LIR ir) {
this.ir = ir;
edgeInstructionSeqences = new ArrayList<>(4);
}
/**
* Determines if two operations are both {@linkplain MoveOp moves} that have the same source
* and {@linkplain MoveOp#getResult() destination} operands.
*
* @param op1 the first instruction to compare
* @param op2 the second instruction to compare
* @return {@code true} if {@code op1} and {@code op2} are the same by the above algorithm
*/
private static boolean same(LIRInstruction op1, LIRInstruction op2) {
assert op1 != null;
assert op2 != null;
if (op1 instanceof ValueMoveOp && op2 instanceof ValueMoveOp) {
ValueMoveOp move1 = (ValueMoveOp) op1;
ValueMoveOp move2 = (ValueMoveOp) op2;
if (move1.getInput().equals(move2.getInput()) && move1.getResult().equals(move2.getResult())) {
// these moves are exactly equal and can be optimized
return true;
}
} else if (op1 instanceof LoadConstantOp && op2 instanceof LoadConstantOp) {
LoadConstantOp move1 = (LoadConstantOp) op1;
LoadConstantOp move2 = (LoadConstantOp) op2;
if (move1.getConstant().equals(move2.getConstant()) && move1.getResult().equals(move2.getResult())) {
// these moves are exactly equal and can be optimized
return true;
}
}
return false;
}
/**
* Moves the longest {@linkplain #same common} subsequence at the end all predecessors of
* {@code block} to the start of {@code block}.
*/
private void optimizeMovesAtBlockEnd(AbstractBlockBase> block) {
for (AbstractBlockBase> pred : block.getPredecessors()) {
if (pred == block) {
// currently we can't handle this correctly.
return;
}
}
// clear all internal data structures
edgeInstructionSeqences.clear();
int numPreds = block.getPredecessorCount();
assert numPreds > 1 : "do not call otherwise";
// setup a list with the LIR instructions of all predecessors
for (AbstractBlockBase> pred : block.getPredecessors()) {
assert pred != null;
assert ir.getLIRforBlock(pred) != null;
List