changeset 1364:db8d14478e56

8079349: Eliminate dead code around Nashorn code generator Reviewed-by: hannesw, lagergren
author attila
date Wed, 06 May 2015 13:36:42 +0200
parents 28cae214dc6c
children 3905889a30af
files src/jdk/nashorn/internal/codegen/BranchOptimizer.java src/jdk/nashorn/internal/codegen/ClassEmitter.java src/jdk/nashorn/internal/codegen/CodeGenerator.java src/jdk/nashorn/internal/codegen/Emitter.java src/jdk/nashorn/internal/codegen/MethodEmitter.java
diffstat 5 files changed, 9 insertions(+), 101 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk/nashorn/internal/codegen/BranchOptimizer.java	Tue May 05 18:35:29 2015 +0200
+++ b/src/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/src/jdk/nashorn/internal/codegen/ClassEmitter.java	Tue May 05 18:35:29 2015 +0200
+++ b/src/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/src/jdk/nashorn/internal/codegen/CodeGenerator.java	Tue May 05 18:35:29 2015 +0200
+++ b/src/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/src/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/src/jdk/nashorn/internal/codegen/MethodEmitter.java	Tue May 05 18:35:29 2015 +0200
+++ b/src/jdk/nashorn/internal/codegen/MethodEmitter.java	Wed May 06 13:36:42 2015 +0200
@@ -124,7 +124,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;
 
@@ -137,9 +137,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;
 
     /**
@@ -208,9 +205,7 @@
 
     /**
      * Begin a method
-     * @see Emitter
      */
-    @Override
     public void begin() {
         classEmitter.beginMethod(this);
         newStack();
@@ -219,9 +214,7 @@
 
     /**
      * End a method
-     * @see Emitter
      */
-    @Override
     public void end() {
         method.visitMaxs(0, 0);
         method.visitEnd();
@@ -1616,15 +1609,6 @@
         }
     }
 
-    MethodEmitter registerReturn() {
-        setHasReturn();
-        return this;
-    }
-
-    void setHasReturn() {
-        this.hasReturn = true;
-    }
-
     /**
      * Perform a non void return, popping the type from the stack
      *
@@ -2716,10 +2700,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