8079349: Eliminate dead code around Nashorn code generator
authorattila
Wed, 06 May 2015 13:36:42 +0200
changeset 30391 62dcad329b26
parent 30390 357f9a3f9394
child 30392 dc4a419b2982
8079349: Eliminate dead code around Nashorn code generator Reviewed-by: hannesw, lagergren
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/BranchOptimizer.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/ClassEmitter.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/CodeGenerator.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/Emitter.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/MethodEmitter.java
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/BranchOptimizer.java	Tue May 05 18:35:29 2015 +0200
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/BranchOptimizer.java	Wed May 06 13:36:42 2015 +0200
@@ -31,6 +31,7 @@
 import static jdk.nashorn.internal.codegen.Condition.LE;
 import static jdk.nashorn.internal.codegen.Condition.LT;
 import static jdk.nashorn.internal.codegen.Condition.NE;
+import static jdk.nashorn.internal.parser.TokenType.NOT;
 
 import jdk.nashorn.internal.ir.BinaryNode;
 import jdk.nashorn.internal.ir.Expression;
@@ -57,21 +58,11 @@
     }
 
     private void branchOptimizer(final UnaryNode unaryNode, final Label label, final boolean state) {
-        final Expression rhs = unaryNode.getExpression();
-
-        switch (unaryNode.tokenType()) {
-        case NOT:
-            branchOptimizer(rhs, label, !state);
-            return;
-        default:
-            if (unaryNode.getType().isBoolean()) {
-                branchOptimizer(rhs, label, state);
-                return;
-            }
-            break;
+        if (unaryNode.isTokenType(NOT)) {
+            branchOptimizer(unaryNode.getExpression(), label, !state);
+        } else {
+            loadTestAndJump(unaryNode, label, state);
         }
-
-        loadTestAndJump(unaryNode, label, state);
     }
 
     private void branchOptimizer(final BinaryNode binaryNode, final Label label, final boolean state) {
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/ClassEmitter.java	Tue May 05 18:35:29 2015 +0200
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/ClassEmitter.java	Wed May 06 13:36:42 2015 +0200
@@ -101,13 +101,10 @@
  * bytecodes that have been written. This is enabled by setting the
  * environment "nashorn.codegen.debug" to true, or --log=codegen:{@literal <level>}
  * <p>
- * A ClassEmitter implements an Emitter - i.e. it needs to have
- * well defined start and end calls for whatever it is generating. Assertions
- * detect if this is not true
  *
  * @see Compiler
  */
-public class ClassEmitter implements Emitter {
+public class ClassEmitter {
     /** Default flags for class generation - public class */
     private static final EnumSet<Flag> DEFAULT_METHOD_FLAGS = EnumSet.of(Flag.PUBLIC);
 
@@ -397,18 +394,14 @@
 
     /**
      * Call at beginning of class emission
-     * @see Emitter
      */
-    @Override
     public void begin() {
         classStarted = true;
     }
 
     /**
      * Call at end of class emission
-     * @see Emitter
      */
-    @Override
     public void end() {
         assert classStarted : "class not started for " + unitClassName;
 
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/CodeGenerator.java	Tue May 05 18:35:29 2015 +0200
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/CodeGenerator.java	Wed May 06 13:36:42 2015 +0200
@@ -174,8 +174,7 @@
  * This quickly became apparent when the code generator was generalized to work
  * with all types, and not just numbers or objects.
  * <p>
- * The CodeGenerator visits nodes only once, tags them as resolved and emits
- * bytecode for them.
+ * The CodeGenerator visits nodes only once and emits bytecode for them.
  */
 @Logger(name="codegen")
 final class CodeGenerator extends NodeOperatorVisitor<CodeGeneratorLexicalContext> implements Loggable {
@@ -1714,11 +1713,7 @@
 
     @Override
     public boolean enterEmptyNode(final EmptyNode emptyNode) {
-        if(!method.isReachable()) {
-            return false;
-        }
-        enterStatement(emptyNode);
-
+        // Don't even record the line number, it's irrelevant as there's no code.
         return false;
     }
 
@@ -2647,8 +2642,6 @@
         }
         enterStatement(returnNode);
 
-        method.registerReturn();
-
         final Type returnType = lc.getCurrentFunction().getReturnType();
 
         final Expression expression = returnNode.getExpression();
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/Emitter.java	Tue May 05 18:35:29 2015 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,49 +0,0 @@
-/*
- * Copyright (c) 2010, 2013, 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.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * 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 jdk.nashorn.internal.codegen;
-
-/**
- * Interface for anything that interacts with a low level bytecode
- * generation module, for example ASM.
- * <p>
- * This is pretty generic, i.e. it can be a ClassEmitter, MethodEmitter
- * or potentially even more fine grained stuff.
- *
- */
-public interface Emitter {
-
-    /**
-     * Register the start of emission for this CodeEmitter
-     */
-    public void begin();
-
-    /**
-     * Register the end of emission for this CodeEmitter.
-     * This is typically required before generated code can
-     * be requested from it
-     */
-    public void end();
-}
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/MethodEmitter.java	Tue May 05 18:35:29 2015 +0200
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/MethodEmitter.java	Wed May 06 13:36:42 2015 +0200
@@ -123,7 +123,7 @@
  * all generated bytecode and labels to stderr, for easier debugging,
  * including bytecode stack contents
  */
-public class MethodEmitter implements Emitter {
+public class MethodEmitter {
     /** The ASM MethodVisitor we are plugged into */
     private final MethodVisitor method;
 
@@ -136,9 +136,6 @@
     /** Current type stack for current evaluation */
     private Label.Stack stack;
 
-    /** Check whether this emitter ever has a function return point */
-    private boolean hasReturn;
-
     private boolean preventUndefinedLoad;
 
     /**
@@ -207,9 +204,7 @@
 
     /**
      * Begin a method
-     * @see Emitter
      */
-    @Override
     public void begin() {
         classEmitter.beginMethod(this);
         newStack();
@@ -218,9 +213,7 @@
 
     /**
      * End a method
-     * @see Emitter
      */
-    @Override
     public void end() {
         method.visitMaxs(0, 0);
         method.visitEnd();
@@ -1615,15 +1608,6 @@
         }
     }
 
-    MethodEmitter registerReturn() {
-        setHasReturn();
-        return this;
-    }
-
-    void setHasReturn() {
-        this.hasReturn = true;
-    }
-
     /**
      * Perform a non void return, popping the type from the stack
      *
@@ -2724,10 +2708,6 @@
         this.functionNode = functionNode;
     }
 
-    boolean hasReturn() {
-        return hasReturn;
-    }
-
     /**
      * Invoke to enforce assertions preventing load from a local variable slot that's known to not have been written to.
      * Used by CodeGenerator, as it strictly enforces tracking of stores. Simpler uses of MethodEmitter, e.g. those