1 /* 2 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 25 package org.graalvm.compiler.core.test; 26 27 import org.graalvm.compiler.graph.Node; 28 import org.graalvm.compiler.nodes.IfNode; 29 import org.graalvm.compiler.nodes.StructuredGraph; 30 import org.graalvm.compiler.nodes.calc.AddNode; 31 import org.graalvm.compiler.nodes.calc.FloatEqualsNode; 32 import org.graalvm.compiler.nodes.calc.ReinterpretNode; 33 import org.junit.Assert; 34 import org.junit.Test; 35 36 /** 37 * Tests that substitutions for {@link Double#doubleToLongBits(double)} and 38 * {@link Float#floatToIntBits(float)} produce graphs such that multiple calls to these methods with 39 * the same input are canonicalized. 40 */ 41 public class CanonicalizedConversionTest extends GraalCompilerTest { 42 43 @Override 44 protected void checkLowTierGraph(StructuredGraph graph) { 45 int reinterpretCount = 0; 46 int floatEqualsCount = 0; 47 int addCount = 0; 48 for (Node node : graph.getNodes()) { 49 if (node instanceof ReinterpretNode) { 50 reinterpretCount++; 51 } else if (node instanceof FloatEqualsNode) { 52 floatEqualsCount++; 53 } else if (node instanceof IfNode) { 54 Assert.fail("Unexpected node: " + node); 55 } else if (node instanceof AddNode) { 56 addCount++; 57 } 58 } 59 Assert.assertEquals(1, reinterpretCount); 60 Assert.assertEquals(1, floatEqualsCount); 61 Assert.assertEquals(2, addCount); 62 } 63 64 @Test 65 public void test4() { 66 test("snippet4", 567.890F); 67 test("snippet4", -567.890F); 68 test("snippet4", Float.NaN); 69 } 70 71 public static int snippet4(float value) { 72 return Float.floatToIntBits(value) + Float.floatToIntBits(value) + Float.floatToIntBits(value); 73 } 74 75 @Test 76 public void test5() { 77 test("snippet5", 567.890D); 78 test("snippet5", -567.890D); 79 test("snippet5", Double.NaN); 80 } 81 82 public static long snippet5(double value) { 83 return Double.doubleToLongBits(value) + Double.doubleToLongBits(value) + Double.doubleToLongBits(value); 84 } 85 }