changeset 21183:565d7dbae77c

LIRGenerator: replace doBlockStart/doBlockEnd with an AutoCloseable.
author Josef Eisl <josef.eisl@jku.at>
date Mon, 20 Apr 2015 11:16:15 +0200
parents d15ee06d36d0
children dd6cc861f7e2
files graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/gen/NodeLIRBuilder.java graal/com.oracle.graal.lir/src/com/oracle/graal/lir/gen/LIRGenerator.java graal/com.oracle.graal.lir/src/com/oracle/graal/lir/gen/LIRGeneratorTool.java
diffstat 3 files changed, 106 insertions(+), 79 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/gen/NodeLIRBuilder.java	Fri Apr 17 13:53:44 2015 +0200
+++ b/graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/gen/NodeLIRBuilder.java	Mon Apr 20 11:16:15 2015 +0200
@@ -45,6 +45,7 @@
 import com.oracle.graal.lir.debug.*;
 import com.oracle.graal.lir.gen.*;
 import com.oracle.graal.lir.gen.LIRGenerator.Options;
+import com.oracle.graal.lir.gen.LIRGeneratorTool.BlockScope;
 import com.oracle.graal.nodes.*;
 import com.oracle.graal.nodes.calc.*;
 import com.oracle.graal.nodes.cfg.*;
@@ -192,71 +193,71 @@
     }
 
     public void doBlock(Block block, StructuredGraph graph, BlockMap<List<Node>> blockMap) {
-        gen.doBlockStart(block);
+        try (BlockScope blockScope = gen.getBlockScope(block)) {
 
-        if (block == gen.getResult().getLIR().getControlFlowGraph().getStartBlock()) {
-            assert block.getPredecessorCount() == 0;
-            emitPrologue(graph);
-        } else {
-            assert block.getPredecessorCount() > 0;
-        }
+            if (block == gen.getResult().getLIR().getControlFlowGraph().getStartBlock()) {
+                assert block.getPredecessorCount() == 0;
+                emitPrologue(graph);
+            } else {
+                assert block.getPredecessorCount() > 0;
+            }
 
-        List<Node> nodes = blockMap.get(block);
+            List<Node> nodes = blockMap.get(block);
 
-        // Allow NodeLIRBuilder subclass to specialize code generation of any interesting groups
-        // of instructions
-        matchComplexExpressions(nodes);
+            // Allow NodeLIRBuilder subclass to specialize code generation of any interesting groups
+            // of instructions
+            matchComplexExpressions(nodes);
 
-        for (int i = 0; i < nodes.size(); i++) {
-            Node node = nodes.get(i);
-            if (node instanceof ValueNode) {
-                ValueNode valueNode = (ValueNode) node;
-                if (Options.TraceLIRGeneratorLevel.getValue() >= 3) {
-                    TTY.println("LIRGen for " + valueNode);
-                }
-                Value operand = getOperand(valueNode);
-                if (operand == null) {
-                    if (!peephole(valueNode)) {
-                        try {
-                            doRoot(valueNode);
-                        } catch (GraalInternalError e) {
-                            throw GraalGraphInternalError.transformAndAddContext(e, valueNode);
-                        } catch (Throwable e) {
-                            throw new GraalGraphInternalError(e).addContext(valueNode);
+            for (int i = 0; i < nodes.size(); i++) {
+                Node node = nodes.get(i);
+                if (node instanceof ValueNode) {
+                    ValueNode valueNode = (ValueNode) node;
+                    if (Options.TraceLIRGeneratorLevel.getValue() >= 3) {
+                        TTY.println("LIRGen for " + valueNode);
+                    }
+                    Value operand = getOperand(valueNode);
+                    if (operand == null) {
+                        if (!peephole(valueNode)) {
+                            try {
+                                doRoot(valueNode);
+                            } catch (GraalInternalError e) {
+                                throw GraalGraphInternalError.transformAndAddContext(e, valueNode);
+                            } catch (Throwable e) {
+                                throw new GraalGraphInternalError(e).addContext(valueNode);
+                            }
                         }
+                    } else if (ComplexMatchValue.INTERIOR_MATCH.equals(operand)) {
+                        // Doesn't need to be evaluated
+                        Debug.log("interior match for %s", valueNode);
+                    } else if (operand instanceof ComplexMatchValue) {
+                        Debug.log("complex match for %s", valueNode);
+                        ComplexMatchValue match = (ComplexMatchValue) operand;
+                        operand = match.evaluate(this);
+                        if (operand != null) {
+                            setResult(valueNode, operand);
+                        }
+                    } else {
+                        // There can be cases in which the result of an instruction is already set
+                        // before by other instructions.
                     }
-                } else if (ComplexMatchValue.INTERIOR_MATCH.equals(operand)) {
-                    // Doesn't need to be evaluated
-                    Debug.log("interior match for %s", valueNode);
-                } else if (operand instanceof ComplexMatchValue) {
-                    Debug.log("complex match for %s", valueNode);
-                    ComplexMatchValue match = (ComplexMatchValue) operand;
-                    operand = match.evaluate(this);
-                    if (operand != null) {
-                        setResult(valueNode, operand);
-                    }
-                } else {
-                    // There can be cases in which the result of an instruction is already set
-                    // before by other instructions.
                 }
             }
-        }
 
-        if (!gen.hasBlockEnd(block)) {
-            NodeClassIterable successors = block.getEndNode().successors();
-            assert successors.count() == block.getSuccessorCount();
-            if (block.getSuccessorCount() != 1) {
-                /*
-                 * If we have more than one successor, we cannot just use the first one. Since
-                 * successors are unordered, this would be a random choice.
-                 */
-                throw new GraalInternalError("Block without BlockEndOp: " + block.getEndNode());
+            if (!gen.hasBlockEnd(block)) {
+                NodeClassIterable successors = block.getEndNode().successors();
+                assert successors.count() == block.getSuccessorCount();
+                if (block.getSuccessorCount() != 1) {
+                    /*
+                     * If we have more than one successor, we cannot just use the first one. Since
+                     * successors are unordered, this would be a random choice.
+                     */
+                    throw new GraalInternalError("Block without BlockEndOp: " + block.getEndNode());
+                }
+                gen.emitJump(getLIRBlock((FixedNode) successors.first()));
             }
-            gen.emitJump(getLIRBlock((FixedNode) successors.first()));
+
+            assert verifyBlock(gen.getResult().getLIR(), block);
         }
-
-        assert verifyBlock(gen.getResult().getLIR(), block);
-        gen.doBlockEnd(block);
     }
 
     protected void matchComplexExpressions(List<Node> nodes) {
--- a/graal/com.oracle.graal.lir/src/com/oracle/graal/lir/gen/LIRGenerator.java	Fri Apr 17 13:53:44 2015 +0200
+++ b/graal/com.oracle.graal.lir/src/com/oracle/graal/lir/gen/LIRGenerator.java	Mon Apr 20 11:16:15 2015 +0200
@@ -183,7 +183,7 @@
             TTY.println();
         }
         assert LIRVerifier.verify(op);
-        res.getLIR().getLIRforBlock(currentBlock).add(op);
+        res.getLIR().getLIRforBlock(getCurrentBlock()).add(op);
         return op;
     }
 
@@ -195,35 +195,55 @@
         return ops.get(ops.size() - 1) instanceof BlockEndOp;
     }
 
-    public final void doBlockStart(AbstractBlockBase<?> block) {
-        if (Options.PrintIRWithLIR.getValue()) {
-            TTY.print(block.toString());
+    private final class BlockScopeImpl extends BlockScope {
+
+        private BlockScopeImpl(AbstractBlockBase<?> block) {
+            currentBlock = block;
+        }
+
+        private void doBlockStart() {
+            if (Options.PrintIRWithLIR.getValue()) {
+                TTY.print(currentBlock.toString());
+            }
+
+            // set up the list of LIR instructions
+            assert res.getLIR().getLIRforBlock(currentBlock) == null : "LIR list already computed for this block";
+            res.getLIR().setLIRforBlock(currentBlock, new ArrayList<LIRInstruction>());
+
+            append(new LabelOp(new Label(currentBlock.getId()), currentBlock.isAligned()));
+
+            if (Options.TraceLIRGeneratorLevel.getValue() >= 1) {
+                TTY.println("BEGIN Generating LIR for block B" + currentBlock.getId());
+            }
         }
 
-        currentBlock = block;
+        private void doBlockEnd() {
+            if (Options.TraceLIRGeneratorLevel.getValue() >= 1) {
+                TTY.println("END Generating LIR for block B" + currentBlock.getId());
+            }
 
-        // set up the list of LIR instructions
-        assert res.getLIR().getLIRforBlock(block) == null : "LIR list already computed for this block";
-        res.getLIR().setLIRforBlock(block, new ArrayList<LIRInstruction>());
+            if (Options.PrintIRWithLIR.getValue()) {
+                TTY.println();
+            }
+            currentBlock = null;
+        }
 
-        append(new LabelOp(new Label(block.getId()), block.isAligned()));
+        @Override
+        public AbstractBlockBase<?> getCurrentBlock() {
+            return currentBlock;
+        }
 
-        if (Options.TraceLIRGeneratorLevel.getValue() >= 1) {
-            TTY.println("BEGIN Generating LIR for block B" + block.getId());
+        @Override
+        public void close() {
+            doBlockEnd();
         }
+
     }
 
-    public final void doBlockEnd(AbstractBlockBase<?> block) {
-
-        if (Options.TraceLIRGeneratorLevel.getValue() >= 1) {
-            TTY.println("END Generating LIR for block B" + block.getId());
-        }
-
-        currentBlock = null;
-
-        if (Options.PrintIRWithLIR.getValue()) {
-            TTY.println();
-        }
+    public final BlockScope getBlockScope(AbstractBlockBase<?> block) {
+        BlockScopeImpl blockScope = new BlockScopeImpl(block);
+        blockScope.doBlockStart();
+        return blockScope;
     }
 
     public void emitIncomingValues(Value[] params) {
--- a/graal/com.oracle.graal.lir/src/com/oracle/graal/lir/gen/LIRGeneratorTool.java	Fri Apr 17 13:53:44 2015 +0200
+++ b/graal/com.oracle.graal.lir/src/com/oracle/graal/lir/gen/LIRGeneratorTool.java	Mon Apr 20 11:16:15 2015 +0200
@@ -37,6 +37,14 @@
         LIRInstruction createMove(AllocatableValue result, Value input);
     }
 
+    public abstract class BlockScope implements AutoCloseable {
+
+        public abstract AbstractBlockBase<?> getCurrentBlock();
+
+        public abstract void close();
+
+    }
+
     CodeGenProviders getProviders();
 
     TargetDescription target();
@@ -55,9 +63,7 @@
 
     SpillMoveFactory getSpillMoveFactory();
 
-    void doBlockStart(AbstractBlockBase<?> block);
-
-    void doBlockEnd(AbstractBlockBase<?> block);
+    BlockScope getBlockScope(AbstractBlockBase<?> block);
 
     Value emitLoadConstant(LIRKind kind, Constant constant);