8248445: Use of AbsI/AbsL nodes should be limited to supported platforms

Reviewed-by: kvn, vlivanov
diff --git a/src/hotspot/share/opto/cfgnode.cpp b/src/hotspot/share/opto/cfgnode.cpp
index ee3cd30..e90c24d 100644
--- a/src/hotspot/share/opto/cfgnode.cpp
+++ b/src/hotspot/share/opto/cfgnode.cpp
@@ -1605,40 +1605,33 @@
   // Check other phi input for subtract node
   Node *sub = phi_root->in(3 - phi_x_idx);
 
+  bool is_sub = sub->Opcode() == Op_SubF || sub->Opcode() == Op_SubD ||
+                sub->Opcode() == Op_SubI || sub->Opcode() == Op_SubL;
+
   // Allow only Sub(0,X) and fail out for all others; Neg is not OK
-  if( tzero == TypeF::ZERO ) {
-    if( sub->Opcode() != Op_SubF ||
-        sub->in(2) != x ||
-        phase->type(sub->in(1)) != tzero ) return NULL;
+  if (!is_sub || phase->type(sub->in(1)) != tzero || sub->in(2) != x) return NULL;
+
+  if (tzero == TypeF::ZERO) {
     x = new AbsFNode(x);
     if (flip) {
       x = new SubFNode(sub->in(1), phase->transform(x));
     }
   } else if (tzero == TypeD::ZERO) {
-    if( sub->Opcode() != Op_SubD ||
-        sub->in(2) != x ||
-        phase->type(sub->in(1)) != tzero ) return NULL;
     x = new AbsDNode(x);
     if (flip) {
       x = new SubDNode(sub->in(1), phase->transform(x));
     }
-  } else if (tzero == TypeInt::ZERO) {
-    if (sub->Opcode() != Op_SubI ||
-        sub->in(2) != x ||
-        phase->type(sub->in(1)) != tzero) return NULL;
+  } else if (tzero == TypeInt::ZERO && Matcher::match_rule_supported(Op_AbsI)) {
     x = new AbsINode(x);
     if (flip) {
       x = new SubINode(sub->in(1), phase->transform(x));
     }
-  } else {
-    if (sub->Opcode() != Op_SubL ||
-        sub->in(2) != x ||
-        phase->type(sub->in(1)) != tzero) return NULL;
+  } else if (tzero == TypeLong::ZERO && Matcher::match_rule_supported(Op_AbsL)) {
     x = new AbsLNode(x);
     if (flip) {
       x = new SubLNode(sub->in(1), phase->transform(x));
     }
-  }
+  } else return NULL;
 
   return x;
 }
diff --git a/test/hotspot/jtreg/compiler/c2/TestAbs.java b/test/hotspot/jtreg/compiler/c2/TestAbs.java
new file mode 100644
index 0000000..6dfbbbf
--- /dev/null
+++ b/test/hotspot/jtreg/compiler/c2/TestAbs.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2020, BELLSOFT. 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.
+ */
+
+package compiler.c2;
+
+/*
+ * @test
+ * @bug 8248445
+ * @summary Use of AbsI / AbsL nodes should be limited to supported platforms
+ * @requires vm.debug == true
+ *
+ * @run main/othervm -XX:-TieredCompilation -Xbatch -XX:CompileOnly=java.lang.Math::abs compiler.c2.TestAbs
+ */
+public class TestAbs {
+
+    public static void test() {
+        // java.lang.Math.abs() collapses into AbsI/AbsL nodes on platforms that support the correspondent nodes
+        // Using unsupported nodes triggers a console warning on a release build and gives an error on a debug build
+        Math.abs(1);
+        Math.abs(-1);
+        Math.abs(1L);
+        Math.abs(-1L);
+    }
+
+    public static void main(String args[]) {
+        for (int i = 0; i < 20_000; i++) {
+            test();
+        }
+    }
+}
+