changeset 6940:954700ff3322

8043284: Optimize signed integer comparison Summary: Folding of BoolNode if input add/sub of CmpI overflows and we can prove that compared value is not in the two resulting ranges. Added test for CmpI and CmpU optimizations. Reviewed-by: kvn, rbackman, roland
author thartmann
date Mon, 11 Aug 2014 13:01:37 +0200
parents 6618133bfaf7
children db7d2f27bcb6
files src/share/vm/opto/subnode.cpp src/share/vm/opto/subnode.hpp test/compiler/IntegerArithmetic/TestIntegerComparison.java
diffstat 3 files changed, 147 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/vm/opto/subnode.cpp	Fri Aug 08 11:36:48 2014 -0700
+++ b/src/share/vm/opto/subnode.cpp	Mon Aug 11 13:01:37 2014 +0200
@@ -1201,6 +1201,54 @@
   return new BoolNode(in(1), _test.negate());
 }
 
+// Change "bool eq/ne (cmp (add/sub A B) C)" into false/true if add/sub
+// overflows and we can prove that C is not in the two resulting ranges.
+// This optimization is similar to the one performed by CmpUNode::Value().
+Node* BoolNode::fold_cmpI(PhaseGVN* phase, SubNode* cmp, Node* cmp1, int cmp_op,
+                          int cmp1_op, const TypeInt* cmp2_type) {
+  // Only optimize eq/ne integer comparison of add/sub
+  if((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
+     (cmp_op == Op_CmpI) && (cmp1_op == Op_AddI || cmp1_op == Op_SubI)) {
+    // Skip cases were inputs of add/sub are not integers or of bottom type
+    const TypeInt* r0 = phase->type(cmp1->in(1))->isa_int();
+    const TypeInt* r1 = phase->type(cmp1->in(2))->isa_int();
+    if ((r0 != NULL) && (r0 != TypeInt::INT) &&
+        (r1 != NULL) && (r1 != TypeInt::INT) &&
+        (cmp2_type != TypeInt::INT)) {
+      // Compute exact (long) type range of add/sub result
+      jlong lo_long = r0->_lo;
+      jlong hi_long = r0->_hi;
+      if (cmp1_op == Op_AddI) {
+        lo_long += r1->_lo;
+        hi_long += r1->_hi;
+      } else {
+        lo_long -= r1->_hi;
+        hi_long -= r1->_lo;
+      }
+      // Check for over-/underflow by casting to integer
+      int lo_int = (int)lo_long;
+      int hi_int = (int)hi_long;
+      bool underflow = lo_long != (jlong)lo_int;
+      bool overflow  = hi_long != (jlong)hi_int;
+      if ((underflow != overflow) && (hi_int < lo_int)) {
+        // Overflow on one boundary, compute resulting type ranges:
+        // tr1 [MIN_INT, hi_int] and tr2 [lo_int, MAX_INT]
+        int w = MAX2(r0->_widen, r1->_widen); // _widen does not matter here
+        const TypeInt* tr1 = TypeInt::make(min_jint, hi_int, w);
+        const TypeInt* tr2 = TypeInt::make(lo_int, max_jint, w);
+        // Compare second input of cmp to both type ranges
+        const Type* sub_tr1 = cmp->sub(tr1, cmp2_type);
+        const Type* sub_tr2 = cmp->sub(tr2, cmp2_type);
+        if (sub_tr1 == TypeInt::CC_LT && sub_tr2 == TypeInt::CC_GT) {
+          // The result of the add/sub will never equal cmp2. Replace BoolNode
+          // by false (0) if it tests for equality and by true (1) otherwise.
+          return ConINode::make((_test._test == BoolTest::eq) ? 0 : 1);
+        }
+      }
+    }
+  }
+  return NULL;
+}
 
 //------------------------------Ideal------------------------------------------
 Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) {
@@ -1294,6 +1342,9 @@
     return new BoolNode( ncmp, _test.commute() );
   }
 
+  // Try to optimize signed integer comparison
+  return fold_cmpI(phase, cmp->as_Sub(), cmp1, cop, cmp1_op, cmp2_type);
+
   //  The transformation below is not valid for either signed or unsigned
   //  comparisons due to wraparound concerns at MAX_VALUE and MIN_VALUE.
   //  This transformation can be resurrected when we are able to
@@ -1338,8 +1389,6 @@
   //         phase->type( cmp2->in(2) ) == TypeInt::ONE )
   //        return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::le );
   //    }
-
-  return NULL;
 }
 
 //------------------------------Value------------------------------------------
--- a/src/share/vm/opto/subnode.hpp	Fri Aug 08 11:36:48 2014 -0700
+++ b/src/share/vm/opto/subnode.hpp	Mon Aug 11 13:01:37 2014 +0200
@@ -286,6 +286,10 @@
   virtual uint hash() const;
   virtual uint cmp( const Node &n ) const;
   virtual uint size_of() const;
+
+  // Try to optimize signed integer comparison
+  Node* fold_cmpI(PhaseGVN* phase, SubNode* cmp, Node* cmp1, int cmp_op,
+                  int cmp1_op, const TypeInt* cmp2_type);
 public:
   const BoolTest _test;
   BoolNode( Node *cc, BoolTest::mask t): _test(t), Node(0,cc) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/compiler/IntegerArithmetic/TestIntegerComparison.java	Mon Aug 11 13:01:37 2014 +0200
@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 2014, 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.
+ *
+ * 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.
+ */
+
+/*
+ * @test TestIntegerComparison
+ * @bug 8043284 8042786
+ * @summary "Tests optimizations of signed and unsigned integer comparison."
+ * @run main/othervm -server -Xcomp -XX:+IgnoreUnrecognizedVMOptions -XX:-TieredCompilation -XX:CompileOnly=TestIntegerComparison::testSigned,TestIntegerComparison::testUnsigned TestIntegerComparison
+ */
+public class TestIntegerComparison {
+  /**
+   * Tests optimization of signed integer comparison (see BoolNode::Ideal).
+   * The body of the if statement is unreachable and should not be compiled.
+   * @param c Character (value in the integer range [0, 65535])
+   */
+  public static void testSigned(char c) {
+    // The following addition may overflow. The result is in one
+    // of the two ranges [IntMax] and [IntMin, IntMin + CharMax - 1].
+    int result = c + Integer.MAX_VALUE;
+    // CmpINode has to consider both result ranges instead of only
+    // the general [IntMin, IntMax] range to be able to prove that
+    // result is always unequal to CharMax.
+    if (result == Character.MAX_VALUE) {
+      // Unreachable
+      throw new RuntimeException("Should not reach here!");
+    }
+  }
+
+  /**
+   * Tests optimization of unsigned integer comparison (see CmpUNode::Value).
+   * The body of the if statement is unreachable and should not be compiled.
+   * @param c Character (value in the integer range [0, 65535])
+   */
+  public static void testUnsigned(char c) {
+    /*
+     * The following if statement consisting of two CmpIs is replaced
+     * by a CmpU during optimization (see 'IfNode::fold_compares').
+     *
+     * The signed (lo < i) and (i < hi) are replaced by the unsigned
+     * (i - (lo+1) < hi - (lo+1)). In this case the unsigned comparison
+     * equals (result - 2) < 98 leading to the following CmpUNode:
+     *
+     * CmpU (AddI result, -2) 98
+     *
+     * With the value of result this is simplified to:
+     *
+     * CmpU (AddI c, -(CharMax - IntMin)) 98
+     *
+     * The subtraction may underflow. The result is in one of the two
+     * ranges [IntMin], [IntMax - CharMax + 1]. Both ranges have to be
+     * considered instead of only the general [IntMin, IntMax] to prove
+     * that due to the overflow the signed comparison result < 98 is
+     * always false.
+     */
+    int result = c - (Character.MAX_VALUE - Integer.MIN_VALUE) + 2;
+    if (1 < result && result < 100) {
+      // Unreachable
+      throw new RuntimeException("Should not reach here!");
+    }
+  }
+
+  /**
+   * Tests optimizations of signed and unsigned integer comparison.
+   */
+  public static void main(String[] args) {
+    // We use characters to get a limited integer range for free
+    for (int i = Character.MIN_VALUE; i <= Character.MAX_VALUE; ++i) {
+      testSigned((char) i);
+      testUnsigned((char) i);
+    }
+  }
+}