# HG changeset patch # User Josef Eisl # Date 1409064947 -7200 # Node ID 85020469ed2b7d334972f3e5dce9efffaeab9564 # Parent 2451521ed26fc7b9d1b610071e78d7ae9b0aed3b ConstantLoadOptimization: minor cleanups. diff -r 2451521ed26f -r 85020469ed2b graal/com.oracle.graal.lir/src/com/oracle/graal/lir/constopt/ConstantLoadOptimization.java --- a/graal/com.oracle.graal.lir/src/com/oracle/graal/lir/constopt/ConstantLoadOptimization.java Mon Aug 25 17:23:14 2014 +0200 +++ b/graal/com.oracle.graal.lir/src/com/oracle/graal/lir/constopt/ConstantLoadOptimization.java Tue Aug 26 16:55:47 2014 +0200 @@ -26,7 +26,6 @@ import static com.oracle.graal.lir.LIRValueUtil.*; import java.util.*; -import java.util.stream.*; import com.oracle.graal.api.meta.*; import com.oracle.graal.compiler.common.cfg.*; @@ -86,7 +85,7 @@ try (Indent indent = Debug.logAndIndent("ConstantLoadOptimization")) { try (Scope s = Debug.scope("BuildDefUseTree")) { // build DefUseTree - lir.getControlFlowGraph().getBlocks().forEach(this::processBlock); + lir.getControlFlowGraph().getBlocks().forEach(this::analyzeBlock); // remove all with only one use map.filter(t -> { if (t.usageCount() > 1) { @@ -114,10 +113,6 @@ } } - public void print() { - map.forEach(t -> Debug.log(1, "%s", t)); - } - private static boolean isConstantLoad(LIRInstruction inst) { if (!(inst instanceof MoveOp)) { return false; @@ -136,7 +131,10 @@ list.add(entry); } - private void processBlock(AbstractBlock block) { + /** + * Collects def-use information for a {@code block}. + */ + private void analyzeBlock(AbstractBlock block) { try (Indent indent = Debug.logAndIndent("Block: %s", block)) { InstructionValueConsumer loadConsumer = new InstructionValueConsumer() { @@ -202,11 +200,9 @@ } } - public List getUsages(AbstractBlock block, Variable variable) { - List list = blockMap.get(block); - return list == null ? Collections.emptyList() : list.stream().filter(u -> u.getValue().equals(variable)).collect(Collectors.toList()); - } - + /** + * Creates the dominator tree and searches for an solution. + */ private void createConstantTree(DefUseTree tree) { ConstantTree constTree = new ConstantTree(lir.getControlFlowGraph(), tree); constTree.set(Flags.SUBTREE, tree.getBlock()); @@ -224,9 +220,9 @@ int usageCount = cost.getUsages().size(); assert usageCount == tree.usageCount() : "Usage count differs: " + usageCount + " vs. " + tree.usageCount(); - if (Debug.isLogEnabled(1)) { - try (Indent i = Debug.logAndIndent(1, "Variable: %s, Block: %s, prob.: %f", tree.getVariable(), tree.getBlock(), tree.getBlock().probability())) { - Debug.log(1, "Usages result: %s", cost); + if (Debug.isLogEnabled()) { + try (Indent i = Debug.logAndIndent("Variable: %s, Block: %s, prob.: %f", tree.getVariable(), tree.getBlock(), tree.getBlock().probability())) { + Debug.log("Usages result: %s", cost); } } @@ -269,6 +265,26 @@ } } + private void insertLoad(Constant constant, LIRKind kind, AbstractBlock block, List usages) { + assert usages != null && usages.size() > 0 : String.format("No usages %s %s %s", constant, block, usages); + // create variable + Variable variable = lirGen.newVariable(kind); + // create move + LIRInstruction move = lir.getSpillMoveFactory().createMove(variable, constant); + // insert instruction + getInsertionBuffer(block).append(1, move); + Debug.log("new move (%s) and inserted in block %s", move, block); + // update usages + for (UseEntry u : usages) { + u.getPosition().set(u.getInstruction(), variable); + Debug.log("patched instruction %s", u.getInstruction()); + } + } + + /** + * Inserts the constant loads created in {@link #createConstantTree} and deletes the original + * definition. + */ private void rewriteBlock(AbstractBlock block) { // insert moves LIRInsertionBuffer buffer = insertionBuffers.get(block); @@ -300,22 +316,6 @@ lir.getLIRforBlock(block).set(instruction.id(), null); } - private void insertLoad(Constant constant, LIRKind kind, AbstractBlock block, List usages) { - assert usages != null && usages.size() > 0 : String.format("No usages %s %s %s", constant, block, usages); - // create variable - Variable variable = lirGen.newVariable(kind); - // create move - LIRInstruction move = lir.getSpillMoveFactory().createMove(variable, constant); - // insert instruction - getInsertionBuffer(block).append(1, move); - Debug.log("new move (%s) and inserted in block %s", move, block); - // update usages - for (UseEntry u : usages) { - u.getPosition().set(u.getInstruction(), variable); - Debug.log("patched instruction %s", u.getInstruction()); - } - } - private LIRInsertionBuffer getInsertionBuffer(AbstractBlock block) { LIRInsertionBuffer insertionBuffer = insertionBuffers.get(block); if (insertionBuffer == null) {