|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 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. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | +package org.graalvm.compiler.core.test; |
| 26 | + |
| 27 | +import org.graalvm.compiler.nodes.StructuredGraph; |
| 28 | +import org.graalvm.compiler.nodes.calc.AbsNode; |
| 29 | +import org.graalvm.compiler.nodes.calc.NegateNode; |
| 30 | +import org.junit.Assert; |
| 31 | +import org.junit.Test; |
| 32 | + |
| 33 | +public class AbsCanonicalizationTest extends GraalCompilerTest { |
| 34 | + public static double absReference(double x) { |
| 35 | + return Math.abs(x); |
| 36 | + } |
| 37 | + |
| 38 | + public static double absAbs(double x) { |
| 39 | + return Math.abs(Math.abs(x)); |
| 40 | + } |
| 41 | + |
| 42 | + public static double absNegate(double x) { |
| 43 | + return Math.abs(-x); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testAbsNegate() { |
| 48 | + StructuredGraph graph = parseEager("absNegate", StructuredGraph.AllowAssumptions.YES); |
| 49 | + createInliningPhase().apply(graph, getDefaultHighTierContext()); |
| 50 | + createCanonicalizerPhase().apply(graph, getProviders()); |
| 51 | + |
| 52 | + StructuredGraph referenceGraph = parseEager("absReference", StructuredGraph.AllowAssumptions.YES); |
| 53 | + assertEquals(referenceGraph, graph); |
| 54 | + Assert.assertEquals(0, graph.getNodes().filter(NegateNode.class).count()); |
| 55 | + |
| 56 | + testAgainstExpected(graph.method(), new Result(absNegate(-Double.MAX_VALUE), null), (Object) null, Double.MAX_VALUE); |
| 57 | + testAgainstExpected(graph.method(), new Result(absNegate(0d), null), (Object) null, 0d); |
| 58 | + testAgainstExpected(graph.method(), new Result(absNegate(Double.MAX_VALUE), null), (Object) null, Double.MAX_VALUE); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testAbsAbs() { |
| 63 | + StructuredGraph graph = parseEager("absAbs", StructuredGraph.AllowAssumptions.YES); |
| 64 | + createInliningPhase().apply(graph, getDefaultHighTierContext()); |
| 65 | + createCanonicalizerPhase().apply(graph, getProviders()); |
| 66 | + |
| 67 | + StructuredGraph referenceGraph = parseEager("absReference", StructuredGraph.AllowAssumptions.YES); |
| 68 | + assertEquals(referenceGraph, graph); |
| 69 | + Assert.assertEquals(1, graph.getNodes().filter(AbsNode.class).count()); |
| 70 | + |
| 71 | + testAgainstExpected(graph.method(), new Result(absAbs(-Double.MAX_VALUE), null), (Object) null, Double.MAX_VALUE); |
| 72 | + testAgainstExpected(graph.method(), new Result(absAbs(0d), null), (Object) null, 0d); |
| 73 | + testAgainstExpected(graph.method(), new Result(absAbs(Double.MAX_VALUE), null), (Object) null, Double.MAX_VALUE); |
| 74 | + } |
| 75 | +} |
0 commit comments