changeset 47381:b6f19ebe3c10 condy-folding

moving isTrue and isFalse to ConstFold and propagating constants for conditional and typecast
author vromero
date Thu, 05 Oct 2017 15:35:10 -0400
parents 7e9a554ddaf3
children 6a0c6a7c6a00
files src/jdk.compiler/share/classes/com/sun/tools/javac/code/Type.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/ConstFold.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/ConstablesVisitor.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java test/jdk/java/lang/invoke/IntrinsicsTest.java
diffstat 7 files changed, 39 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Type.java	Thu Oct 05 13:09:58 2017 -0400
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Type.java	Thu Oct 05 15:35:10 2017 -0400
@@ -177,18 +177,6 @@
         return null;
     }
 
-    /** Is this a constant type whose value is false?
-     */
-    public boolean isFalse() {
-        return false;
-    }
-
-    /** Is this a constant type whose value is true?
-     */
-    public boolean isTrue() {
-        return false;
-    }
-
     /**
      * Get the representation of this type used for modelling purposes.
      * By default, this is itself. For ErrorType, a different value
@@ -735,16 +723,6 @@
                 };
         }
 
-        /** Is this a constant type whose value is true?
-         */
-        @Override
-        public boolean isTrue() {
-            return
-                tag == BOOLEAN &&
-                constValue() != null &&
-                ((Integer)constValue()).intValue() != 0;
-        }
-
         @Override @DefinedBy(Api.LANGUAGE_MODEL)
         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
             return v.visitPrimitive(this, p);
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Thu Oct 05 13:09:58 2017 -0400
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Thu Oct 05 15:35:10 2017 -0400
@@ -1606,7 +1606,7 @@
                 falsetype.constValue() != null &&
                 !owntype.hasTag(NONE)) {
             //constant folding
-            owntype = cfolder.coerce(condtype.isTrue() ? truetype : falsetype, owntype);
+            owntype = cfolder.coerce(ConstFold.isTrue(condtype.getTag(), condtype.constValue()) ? truetype : falsetype, owntype);
         }
         result = check(tree, owntype, KindSelector.VAL, resultInfo);
     }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/ConstFold.java	Thu Oct 05 13:09:58 2017 -0400
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/ConstFold.java	Thu Oct 05 15:35:10 2017 -0400
@@ -88,6 +88,16 @@
             return x.toString();
         }
     }
+    public static boolean isTrue(TypeTag tag, Object x) {
+        return tag == BOOLEAN &&
+                x != null &&
+                ((Integer)x).intValue() != 0;
+    }
+    public static boolean isFalse(TypeTag tag, Object x) {
+        return tag == BOOLEAN &&
+                x != null &&
+                ((Integer)x).intValue() == 0;
+    }
 
     Type fold1(OperatorSymbol op, Type od) {
         if (op.opcode == nop) {
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/ConstablesVisitor.java	Thu Oct 05 13:09:58 2017 -0400
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/ConstablesVisitor.java	Thu Oct 05 15:35:10 2017 -0400
@@ -174,25 +174,25 @@
 
     @Override
     public void visitConditional(JCConditional tree) {
-        // missing
         super.visitConditional(tree);
+        Object condConstant = getConstant(tree.cond);
+        Object truePartConstant = getConstant(tree.truepart);
+        Object falsePartConstant = getConstant(tree.falsepart);
         if (tree.type.constValue() == null &&
-            tree.cond.type.constValue() != null &&
-            tree.truepart.type.constValue() != null &&
-            tree.falsepart.type.constValue() != null &&
+            condConstant != null &&
+            truePartConstant != null &&
+            falsePartConstant != null &&
             !tree.type.hasTag(NONE)) {
-            //constant folding
-            tree.type = cfolder.coerce(tree.cond.type.isTrue() ? tree.truepart.type : tree.falsepart.type, tree.type);
+            Object constant = ConstFold.isTrue(tree.cond.type.getTag(), condConstant) ? truePartConstant : falsePartConstant;
+            elementToConstantMap.put(tree, constant);
         }
     }
 
     @Override
     public void visitTypeCast(JCTypeCast tree) {
-        // missing
         super.visitTypeCast(tree);
-        if (tree.type.constValue() == null &&
-                tree.expr.type.constValue() != null) {
-            tree.type = coerce(tree.expr.type, tree.type);
+        if (tree.type.constValue() == null && getConstant(tree.expr) != null) {
+            elementToConstantMap.put(tree, getConstant(tree.expr));
         }
     }
 
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java	Thu Oct 05 13:09:58 2017 -0400
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java	Thu Oct 05 15:35:10 2017 -0400
@@ -541,7 +541,7 @@
             scanStat(tree.body);
             alive |= resolveContinues(tree);
             scan(tree.cond);
-            alive = alive && !tree.cond.type.isTrue();
+            alive = alive && !ConstFold.isTrue(tree.cond.type.getTag(), tree.cond.type.constValue());
             alive |= resolveBreaks(tree, prevPendingExits);
         }
 
@@ -549,11 +549,11 @@
             ListBuffer<PendingExit> prevPendingExits = pendingExits;
             pendingExits = new ListBuffer<>();
             scan(tree.cond);
-            alive = !tree.cond.type.isFalse();
+            alive = !ConstFold.isFalse(tree.cond.type.getTag(), tree.cond.type.constValue());
             scanStat(tree.body);
             alive |= resolveContinues(tree);
             alive = resolveBreaks(tree, prevPendingExits) ||
-                !tree.cond.type.isTrue();
+                !ConstFold.isTrue(tree.cond.type.getTag(), tree.cond.type.constValue());
         }
 
         public void visitForLoop(JCForLoop tree) {
@@ -562,7 +562,7 @@
             pendingExits = new ListBuffer<>();
             if (tree.cond != null) {
                 scan(tree.cond);
-                alive = !tree.cond.type.isFalse();
+                alive = !ConstFold.isFalse(tree.cond.type.getTag(), tree.cond.type.constValue());
             } else {
                 alive = true;
             }
@@ -570,7 +570,7 @@
             alive |= resolveContinues(tree);
             scan(tree.step);
             alive = resolveBreaks(tree, prevPendingExits) ||
-                tree.cond != null && !tree.cond.type.isTrue();
+                tree.cond != null && !ConstFold.isTrue(tree.cond.type.getTag(), tree.cond.type.constValue());
         }
 
         public void visitForeachLoop(JCEnhancedForLoop tree) {
@@ -1722,7 +1722,7 @@
          *  rather than (un)inits on exit.
          */
         void scanCond(JCTree tree) {
-            if (tree.type.isFalse()) {
+            if (ConstFold.isFalse(tree.type.getTag(), tree.type.constValue())) {
                 if (inits.isReset()) merge();
                 initsWhenTrue.assign(inits);
                 initsWhenTrue.inclRange(firstadr, nextadr);
@@ -1730,7 +1730,7 @@
                 uninitsWhenTrue.inclRange(firstadr, nextadr);
                 initsWhenFalse.assign(inits);
                 uninitsWhenFalse.assign(uninits);
-            } else if (tree.type.isTrue()) {
+            } else if (ConstFold.isTrue(tree.type.getTag(), tree.type.constValue())) {
                 if (inits.isReset()) merge();
                 initsWhenFalse.assign(inits);
                 initsWhenFalse.inclRange(firstadr, nextadr);
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java	Thu Oct 05 13:09:58 2017 -0400
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java	Thu Oct 05 15:35:10 2017 -0400
@@ -2661,13 +2661,13 @@
     // are safe to simplify as no side-effects can occur.
 
     private boolean isTrue(JCTree exp) {
-        if (exp.type.isTrue())
+        if (ConstFold.isTrue(exp.type.getTag(), exp.type.constValue()))
             return true;
         Boolean b = expValue(exp);
         return b == null ? false : b;
     }
     private boolean isFalse(JCTree exp) {
-        if (exp.type.isFalse())
+        if (ConstFold.isFalse(exp.type.getTag(), exp.type.constValue()))
             return true;
         Boolean b = expValue(exp);
         return b == null ? false : !b;
@@ -2756,11 +2756,11 @@
      */
     public void visitAssert(JCAssert tree) {
         tree.cond = translate(tree.cond, syms.booleanType);
-        if (!tree.cond.type.isTrue()) {
+        if (!ConstFold.isTrue(tree.cond.type.getTag(), tree.cond.type.constValue())) {
             JCExpression cond = assertFlagTest(tree.pos());
             List<JCExpression> exnArgs = (tree.detail == null) ?
                 List.nil() : List.of(translate(tree.detail));
-            if (!tree.cond.type.isFalse()) {
+            if (!ConstFold.isFalse(tree.cond.type.getTag(), tree.cond.type.constValue())) {
                 cond = makeBinary
                     (AND,
                      cond,
--- a/test/jdk/java/lang/invoke/IntrinsicsTest.java	Thu Oct 05 13:09:58 2017 -0400
+++ b/test/jdk/java/lang/invoke/IntrinsicsTest.java	Thu Oct 05 15:35:10 2017 -0400
@@ -59,9 +59,13 @@
 
 
     public void testPropagateThroughLocals() {
+        boolean condition = true;
         String descriptor = "Ljava/lang/String;";
+        String descriptorI = "Ljava/lang/Integer;";
         ClassRef cc1 = ClassRef.ofDescriptor(descriptor);
+        ClassRef ccI = ClassRef.ofDescriptor(descriptorI);
         assertEquals(String.class, Intrinsics.ldc(cc1));
+        assertEquals(String.class, condition ? Intrinsics.ldc(cc1) : Intrinsics.ldc(ccI));
 
         ClassRef cc2 = ClassRef.ofDescriptor("Ljava/lang/String;");
         assertEquals(String.class, Intrinsics.ldc(cc2));
@@ -70,8 +74,8 @@
         assertEquals("foo", Intrinsics.ldc(s));
 
         // @@@ Boxing should preserve IC-ness
-//        ConstantRef<Integer> i = (Integer) 3;
-//        assertEquals(3, (int) Intrinsics.ldc(i));
+        ConstantRef<Integer> i = (Integer) 3;
+        assertEquals(3, (int) Intrinsics.ldc(i));
     }
 
     public void testPropagateThroughField() {
@@ -112,7 +116,7 @@
         ClassRef cc = ClassRef.ofDescriptor("Ljava/lang/String;");
         assertEquals(String.class, Intrinsics.ldc(ClassRef.ofDescriptor("" + cc.descriptorString())));
 
-//        assertEquals(2, (int) Intrinsics.ldc(1 + 1));
+        assertEquals(2, (int) Intrinsics.ldc(1 + 1));
     }
 
     public void testPropagateThroughCombinator() {