< prev index next >

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.nodes/src/org/graalvm/compiler/nodes/calc/CompareNode.java

Print this page
rev 52509 : [mq]: graal2

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2018, 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.

@@ -134,20 +134,43 @@
                 ConvertNode convertY = (ConvertNode) forY;
                 if (convertX.preservesOrder(condition) && convertY.preservesOrder(condition) && convertX.getValue().stamp(view).isCompatible(convertY.getValue().stamp(view))) {
                     boolean supported = true;
                     if (convertX.getValue().stamp(view) instanceof IntegerStamp) {
                         IntegerStamp intStamp = (IntegerStamp) convertX.getValue().stamp(view);
-                        supported = smallestCompareWidth != null && intStamp.getBits() >= smallestCompareWidth;
+                        boolean isConversionCompatible = convertX.getClass() == convertY.getClass();
+                        supported = smallestCompareWidth != null && intStamp.getBits() >= smallestCompareWidth && isConversionCompatible;
                     }
 
                     if (supported) {
-                        boolean multiUsage = (convertX.asNode().hasMoreThanOneUsage() || convertY.asNode().hasMoreThanOneUsage());
-                        if ((forX instanceof ZeroExtendNode || forX instanceof SignExtendNode) && multiUsage) {
-                            // Do not perform for zero or sign extend if there are multiple usages
-                            // of the value.
+
+                        ValueNode xValue = convertX.getValue();
+                        ValueNode yValue = convertY.getValue();
+
+                        if (forX instanceof ZeroExtendNode || forX instanceof SignExtendNode) {
+
+                            int introducedUsages = 0;
+                            int eliminatedNodes = 0;
+
+                            if (convertX.asNode().hasExactlyOneUsage()) {
+                                eliminatedNodes++;
+                            } else if (xValue.hasExactlyOneUsage()) {
+                                introducedUsages++;
+                            }
+
+                            if (convertY.asNode().hasExactlyOneUsage()) {
+                                eliminatedNodes++;
+                            } else if (yValue.hasExactlyOneUsage()) {
+                                introducedUsages++;
+                            }
+
+                            if (introducedUsages > eliminatedNodes) {
+                                // Only perform the optimization if there is
+                                // a good trade-off between introduced new usages and
+                                // eliminated nodes.
                             return null;
                         }
+                        }
                         return duplicateModified(convertX.getValue(), convertY.getValue(), unorderedIsTrue, view);
                     }
                 }
             }
             return null;

@@ -173,11 +196,11 @@
                 }
 
                 boolean supported = true;
                 if (convert.getValue().stamp(view) instanceof IntegerStamp) {
                     IntegerStamp intStamp = (IntegerStamp) convert.getValue().stamp(view);
-                    supported = smallestCompareWidth != null && intStamp.getBits() > smallestCompareWidth;
+                    supported = smallestCompareWidth != null && intStamp.getBits() >= smallestCompareWidth;
                 }
 
                 if (supported) {
                     ConstantNode newConstant = canonicalConvertConstant(constantReflection, metaAccess, options, condition, convert, constant, view);
                     if (newConstant != null) {
< prev index next >