|
| 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.nodes.test; |
| 26 | + |
| 27 | +import static org.graalvm.compiler.core.common.calc.CanonicalCondition.BT; |
| 28 | +import static org.graalvm.compiler.core.common.calc.CanonicalCondition.LT; |
| 29 | +import static org.graalvm.compiler.core.test.GraalCompilerTest.getInitialOptions; |
| 30 | +import static org.junit.Assert.assertEquals; |
| 31 | + |
| 32 | +import org.graalvm.compiler.core.common.calc.CanonicalCondition; |
| 33 | +import org.graalvm.compiler.core.common.type.IntegerStamp; |
| 34 | +import org.graalvm.compiler.core.common.type.Stamp; |
| 35 | +import org.graalvm.compiler.core.common.type.StampFactory; |
| 36 | +import org.graalvm.compiler.core.common.type.StampPair; |
| 37 | +import org.graalvm.compiler.debug.DebugContext; |
| 38 | +import org.graalvm.compiler.graph.test.GraphTest; |
| 39 | +import org.graalvm.compiler.nodes.ParameterNode; |
| 40 | +import org.graalvm.compiler.nodes.StructuredGraph; |
| 41 | +import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; |
| 42 | +import org.graalvm.compiler.nodes.calc.NarrowNode; |
| 43 | +import org.graalvm.compiler.options.OptionValues; |
| 44 | +import org.junit.Before; |
| 45 | +import org.junit.Test; |
| 46 | + |
| 47 | +import jdk.vm.ci.meta.JavaKind; |
| 48 | + |
| 49 | +/** |
| 50 | + * This class tests that {@link NarrowNode#preservesOrder(CanonicalCondition)} returns correct |
| 51 | + * value. |
| 52 | + */ |
| 53 | +public class NarrowPreservesOrderTest extends GraphTest { |
| 54 | + |
| 55 | + private StructuredGraph graph; |
| 56 | + |
| 57 | + @Before |
| 58 | + public void before() { |
| 59 | + OptionValues options = getInitialOptions(); |
| 60 | + DebugContext debug = getDebug(options); |
| 61 | + graph = new StructuredGraph.Builder(options, debug, AllowAssumptions.YES).build(); |
| 62 | + } |
| 63 | + |
| 64 | + private void testPreserveOrder(Stamp inputStamp, int resultBits, CanonicalCondition cond, boolean expected) { |
| 65 | + ParameterNode input = new ParameterNode(0, StampPair.createSingle(inputStamp)); |
| 66 | + NarrowNode narrow = new NarrowNode(input, resultBits); |
| 67 | + assertEquals(expected, narrow.preservesOrder(cond)); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testBoolean() { |
| 72 | + testPreserveOrder(IntegerStamp.create(32, 0, 0), 1, LT, true); |
| 73 | + testPreserveOrder(IntegerStamp.create(32, 0, 0), 1, BT, true); |
| 74 | + testPreserveOrder(IntegerStamp.create(32, 1, 1), 1, LT, false); |
| 75 | + testPreserveOrder(IntegerStamp.create(32, 1, 1), 1, BT, true); |
| 76 | + testPreserveOrder(IntegerStamp.create(32, 0, 1), 1, LT, false); |
| 77 | + testPreserveOrder(IntegerStamp.create(32, 0, 1), 1, BT, true); |
| 78 | + |
| 79 | + testPreserveOrder(IntegerStamp.create(32, 0xFFFFFF80, 0x7F), 1, LT, false); |
| 80 | + testPreserveOrder(IntegerStamp.create(32, 0xFFFFFF80, 0x7F), 1, BT, false); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testByte() { |
| 85 | + testPreserveOrder(IntegerStamp.create(32, 0, 0), 8, LT, true); |
| 86 | + testPreserveOrder(IntegerStamp.create(32, 0, 0), 8, BT, true); |
| 87 | + testPreserveOrder(IntegerStamp.create(32, 0x7F, 0x7F), 8, LT, true); |
| 88 | + testPreserveOrder(IntegerStamp.create(32, 0x7F, 0x7F), 8, BT, true); |
| 89 | + testPreserveOrder(IntegerStamp.create(32, 0xFF, 0xFF), 8, LT, false); |
| 90 | + testPreserveOrder(IntegerStamp.create(32, 0xFF, 0xFF), 8, BT, true); |
| 91 | + testPreserveOrder(IntegerStamp.create(32, 0xFFFFFF80, 0x7F), 8, LT, true); |
| 92 | + testPreserveOrder(IntegerStamp.create(32, 0xFFFFFF80, 0x7F), 8, BT, true); |
| 93 | + testPreserveOrder(IntegerStamp.create(32, 0, 0xFF), 8, LT, false); |
| 94 | + testPreserveOrder(IntegerStamp.create(32, 0, 0xFF), 8, BT, true); |
| 95 | + |
| 96 | + testPreserveOrder(IntegerStamp.create(32, 0xFFFF8000, 0x7FFF), 8, LT, false); |
| 97 | + testPreserveOrder(IntegerStamp.create(32, 0xFFFF8000, 0x7FFF), 8, BT, false); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testShort() { |
| 102 | + testPreserveOrder(IntegerStamp.create(32, 0, 0), 16, LT, true); |
| 103 | + testPreserveOrder(IntegerStamp.create(32, 0, 0), 16, BT, true); |
| 104 | + testPreserveOrder(IntegerStamp.create(32, 0x7FFF, 0x7FFF), 16, LT, true); |
| 105 | + testPreserveOrder(IntegerStamp.create(32, 0x7FFF, 0x7FFF), 16, BT, true); |
| 106 | + testPreserveOrder(IntegerStamp.create(32, 0xFFFF, 0xFFFF), 16, LT, false); |
| 107 | + testPreserveOrder(IntegerStamp.create(32, 0xFFFF, 0xFFFF), 16, BT, true); |
| 108 | + testPreserveOrder(IntegerStamp.create(32, 0xFFFF8000, 0x7FFF), 16, LT, true); |
| 109 | + testPreserveOrder(IntegerStamp.create(32, 0xFFFF8000, 0x7FFF), 16, BT, true); |
| 110 | + testPreserveOrder(IntegerStamp.create(32, 0, 0xFFFF), 16, LT, false); |
| 111 | + testPreserveOrder(IntegerStamp.create(32, 0, 0xFFFF), 16, BT, true); |
| 112 | + |
| 113 | + testPreserveOrder(StampFactory.intValue(), 16, LT, false); |
| 114 | + testPreserveOrder(StampFactory.intValue(), 16, BT, false); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void testInt() { |
| 119 | + testPreserveOrder(IntegerStamp.create(64, 0, 0), 32, LT, true); |
| 120 | + testPreserveOrder(IntegerStamp.create(64, 0, 0), 32, BT, true); |
| 121 | + testPreserveOrder(IntegerStamp.create(64, 0x7FFFFFFF, 0x7FFFFFFF), 32, LT, true); |
| 122 | + testPreserveOrder(IntegerStamp.create(64, 0x7FFFFFFF, 0x7FFFFFFF), 32, BT, true); |
| 123 | + testPreserveOrder(IntegerStamp.create(64, 0x00000000FFFFFFFFL, 0x00000000FFFFFFFFL), 32, LT, false); |
| 124 | + testPreserveOrder(IntegerStamp.create(64, 0x00000000FFFFFFFFL, 0x00000000FFFFFFFFL), 32, BT, true); |
| 125 | + testPreserveOrder(IntegerStamp.create(64, 0x80000000, 0x7FFFFFFF), 32, LT, true); |
| 126 | + testPreserveOrder(IntegerStamp.create(64, 0x80000000, 0x7FFFFFFF), 32, BT, true); |
| 127 | + testPreserveOrder(IntegerStamp.create(64, 0, 0x00000000FFFFFFFFL), 32, LT, false); |
| 128 | + testPreserveOrder(IntegerStamp.create(64, 0, 0x00000000FFFFFFFFL), 32, BT, true); |
| 129 | + |
| 130 | + testPreserveOrder(StampFactory.forKind(JavaKind.Long), 32, LT, false); |
| 131 | + testPreserveOrder(StampFactory.forKind(JavaKind.Long), 32, BT, false); |
| 132 | + } |
| 133 | +} |
0 commit comments