|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, 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.junit.Assert.assertEquals; |
| 30 | +import static org.junit.Assert.assertTrue; |
| 31 | + |
| 32 | +import org.graalvm.compiler.core.common.NumUtil; |
| 33 | +import org.graalvm.compiler.core.common.calc.CanonicalCondition; |
| 34 | +import org.graalvm.compiler.core.common.type.IntegerStamp; |
| 35 | +import org.graalvm.compiler.core.common.type.Stamp; |
| 36 | +import org.graalvm.compiler.core.common.type.StampFactory; |
| 37 | +import org.graalvm.compiler.core.common.type.StampPair; |
| 38 | +import org.graalvm.compiler.graph.test.GraphTest; |
| 39 | +import org.graalvm.compiler.nodes.ParameterNode; |
| 40 | +import org.graalvm.compiler.nodes.calc.NarrowNode; |
| 41 | +import org.junit.Test; |
| 42 | + |
| 43 | +import jdk.vm.ci.code.CodeUtil; |
| 44 | +import jdk.vm.ci.meta.JavaKind; |
| 45 | + |
| 46 | +/** |
| 47 | + * This class tests that {@link NarrowNode#preservesOrder(CanonicalCondition)} returns correct |
| 48 | + * value. |
| 49 | + */ |
| 50 | +public class NarrowPreservesOrderTest extends GraphTest { |
| 51 | + |
| 52 | + private static IntegerStamp signExtend(Stamp stamp, int bits) { |
| 53 | + assertTrue(stamp instanceof IntegerStamp); |
| 54 | + IntegerStamp integerStamp = (IntegerStamp) stamp; |
| 55 | + return IntegerStamp.create(bits, integerStamp.lowerBound(), integerStamp.upperBound()); |
| 56 | + } |
| 57 | + |
| 58 | + private static IntegerStamp zeroExtend(Stamp stamp, int bits) { |
| 59 | + assertTrue(stamp instanceof IntegerStamp); |
| 60 | + IntegerStamp integerStamp = (IntegerStamp) stamp; |
| 61 | + return IntegerStamp.create(bits, integerStamp.unsignedLowerBound(), integerStamp.unsignedUpperBound()); |
| 62 | + } |
| 63 | + |
| 64 | + private static IntegerStamp forConstantInt(long cst) { |
| 65 | + return IntegerStamp.create(32, cst, cst); |
| 66 | + } |
| 67 | + |
| 68 | + private static IntegerStamp forConstantLong(long cst) { |
| 69 | + return IntegerStamp.create(64, cst, cst); |
| 70 | + } |
| 71 | + |
| 72 | + private static void testPreserveOrder(Stamp inputStamp, int resultBits, CanonicalCondition cond, boolean expected) { |
| 73 | + ParameterNode input = new ParameterNode(0, StampPair.createSingle(inputStamp)); |
| 74 | + NarrowNode narrow = new NarrowNode(input, resultBits); |
| 75 | + assertEquals(expected, narrow.preservesOrder(cond)); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testBoolean() { |
| 80 | + testPreserveOrder(forConstantInt(0), 1, LT, true); |
| 81 | + testPreserveOrder(forConstantInt(0), 1, BT, true); |
| 82 | + testPreserveOrder(forConstantInt(1), 1, LT, false); |
| 83 | + testPreserveOrder(forConstantInt(1), 1, BT, true); |
| 84 | + testPreserveOrder(signExtend(StampFactory.forKind(JavaKind.Boolean), 32), 1, LT, false); |
| 85 | + testPreserveOrder(signExtend(StampFactory.forKind(JavaKind.Boolean), 32), 1, BT, true); |
| 86 | + |
| 87 | + testPreserveOrder(StampFactory.forKind(JavaKind.Byte), 1, LT, false); |
| 88 | + testPreserveOrder(StampFactory.forKind(JavaKind.Byte), 1, BT, false); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testByte() { |
| 93 | + testPreserveOrder(forConstantInt(0), 8, LT, true); |
| 94 | + testPreserveOrder(forConstantInt(0), 8, BT, true); |
| 95 | + testPreserveOrder(forConstantInt(CodeUtil.maxValue(8)), 8, LT, true); |
| 96 | + testPreserveOrder(forConstantInt(CodeUtil.maxValue(8)), 8, BT, true); |
| 97 | + testPreserveOrder(forConstantInt(NumUtil.maxValueUnsigned(8)), 8, LT, false); |
| 98 | + testPreserveOrder(forConstantInt(NumUtil.maxValueUnsigned(8)), 8, BT, true); |
| 99 | + testPreserveOrder(signExtend(StampFactory.forKind(JavaKind.Byte), 32), 8, LT, true); |
| 100 | + testPreserveOrder(signExtend(StampFactory.forKind(JavaKind.Byte), 32), 8, BT, true); |
| 101 | + testPreserveOrder(zeroExtend(StampFactory.forUnsignedInteger(8), 32), 8, LT, false); |
| 102 | + testPreserveOrder(zeroExtend(StampFactory.forUnsignedInteger(8), 32), 8, BT, true); |
| 103 | + |
| 104 | + testPreserveOrder(StampFactory.forKind(JavaKind.Short), 8, LT, false); |
| 105 | + testPreserveOrder(StampFactory.forKind(JavaKind.Short), 8, BT, false); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testShort() { |
| 110 | + testPreserveOrder(forConstantInt(0), 16, LT, true); |
| 111 | + testPreserveOrder(forConstantInt(0), 16, BT, true); |
| 112 | + testPreserveOrder(forConstantInt(CodeUtil.maxValue(16)), 16, LT, true); |
| 113 | + testPreserveOrder(forConstantInt(CodeUtil.maxValue(16)), 16, BT, true); |
| 114 | + testPreserveOrder(forConstantInt(NumUtil.maxValueUnsigned(16)), 16, LT, false); |
| 115 | + testPreserveOrder(forConstantInt(NumUtil.maxValueUnsigned(16)), 16, BT, true); |
| 116 | + testPreserveOrder(signExtend(StampFactory.forKind(JavaKind.Short), 32), 16, LT, true); |
| 117 | + testPreserveOrder(signExtend(StampFactory.forKind(JavaKind.Short), 32), 16, BT, true); |
| 118 | + testPreserveOrder(zeroExtend(StampFactory.forUnsignedInteger(16), 32), 16, LT, false); |
| 119 | + testPreserveOrder(zeroExtend(StampFactory.forUnsignedInteger(16), 32), 16, BT, true); |
| 120 | + |
| 121 | + testPreserveOrder(StampFactory.forKind(JavaKind.Int), 16, LT, false); |
| 122 | + testPreserveOrder(StampFactory.forKind(JavaKind.Int), 16, BT, false); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testInt() { |
| 127 | + testPreserveOrder(forConstantLong(0), 32, LT, true); |
| 128 | + testPreserveOrder(forConstantLong(0), 32, BT, true); |
| 129 | + testPreserveOrder(forConstantLong(CodeUtil.maxValue(32)), 32, LT, true); |
| 130 | + testPreserveOrder(forConstantLong(CodeUtil.maxValue(32)), 32, BT, true); |
| 131 | + testPreserveOrder(forConstantLong(NumUtil.maxValueUnsigned(32)), 32, LT, false); |
| 132 | + testPreserveOrder(forConstantLong(NumUtil.maxValueUnsigned(32)), 32, BT, true); |
| 133 | + testPreserveOrder(signExtend(StampFactory.forKind(JavaKind.Int), 64), 32, LT, true); |
| 134 | + testPreserveOrder(signExtend(StampFactory.forKind(JavaKind.Int), 64), 32, BT, true); |
| 135 | + testPreserveOrder(zeroExtend(StampFactory.forUnsignedInteger(32), 64), 32, LT, false); |
| 136 | + testPreserveOrder(zeroExtend(StampFactory.forUnsignedInteger(32), 64), 32, BT, true); |
| 137 | + |
| 138 | + testPreserveOrder(StampFactory.forKind(JavaKind.Long), 32, LT, false); |
| 139 | + testPreserveOrder(StampFactory.forKind(JavaKind.Long), 32, BT, false); |
| 140 | + } |
| 141 | +} |
0 commit comments