|
| 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.core.test; |
| 26 | + |
| 27 | +import org.graalvm.compiler.nodes.StructuredGraph; |
| 28 | +import org.graalvm.compiler.nodes.calc.OrNode; |
| 29 | +import org.graalvm.compiler.nodes.calc.ShiftNode; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +public class MaskingOptimizationTest extends GraalCompilerTest { |
| 33 | + |
| 34 | + int shiftAndMask1(int x) { |
| 35 | + return (x << 2) & 3; // reduces to return 0 |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void test1() { |
| 40 | + test("shiftAndMask1", 42); |
| 41 | + } |
| 42 | + |
| 43 | + long shiftAndMask2(long x) { |
| 44 | + return (x << 2) & 3; // reduces to return 0 |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void test2() { |
| 49 | + test("shiftAndMask2", 42L); |
| 50 | + } |
| 51 | + |
| 52 | + int shiftAndMask3(int x, int y) { |
| 53 | + return (y + (x << 2)) & 3; // reduces to return y & 3 |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void test3() { |
| 58 | + test("shiftAndMask3", 42, 35); |
| 59 | + } |
| 60 | + |
| 61 | + long shiftAndMask4(long x, long y) { |
| 62 | + return (y + (x << 2)) & 3; // reduces to return y & 3 |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void test4() { |
| 67 | + test("shiftAndMask4", 42L, 35L); |
| 68 | + } |
| 69 | + |
| 70 | + int shiftAndMask5(int x, int y) { |
| 71 | + return ((y << 2) + (x << 2)) & 3; // reduces to return 0 |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void test5() { |
| 76 | + test("shiftAndMask5", 42, 35); |
| 77 | + } |
| 78 | + |
| 79 | + long shiftAndMask6(long x, long y) { |
| 80 | + return ((y << 2) + (x << 2)) & 3; // reduces to return 0 |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void test6() { |
| 85 | + test("shiftAndMask6", 42L, 35L); |
| 86 | + } |
| 87 | + |
| 88 | + long shiftAndMask7(int x) { |
| 89 | + return ((long) (x << 2)) & 3; // reduces to return 0 |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void test7() { |
| 94 | + test("shiftAndMask7", 42); |
| 95 | + } |
| 96 | + |
| 97 | + long shiftAndMask8(int x, int y) { |
| 98 | + return (y + (x << 2)) & 3; // reduces to y & 3 |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void test8() { |
| 103 | + test("shiftAndMask8", 42, 35); |
| 104 | + } |
| 105 | + |
| 106 | + long shiftAndMask9(int x, int y) { |
| 107 | + return (((long) (y << 2)) + ((long) (x << 2))) & 3; // reduces to return 0 |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void test9() { |
| 112 | + test("shiftAndMask9", 42, 35); |
| 113 | + } |
| 114 | + |
| 115 | + boolean shiftAndMaskCompare1(int x) { |
| 116 | + return ((x << 2) & 3) == 0; // reduces to return true |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void test10() { |
| 121 | + test("shiftAndMaskCompare1", 42); |
| 122 | + } |
| 123 | + |
| 124 | + boolean shiftAndMaskCompare2(long x) { |
| 125 | + return ((x << 2) & 3) == 0; // reduces to return true |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void test11() { |
| 130 | + test("shiftAndMaskCompare2", 42L); |
| 131 | + } |
| 132 | + |
| 133 | + boolean shiftAndMaskCompare3(int x, int y) { |
| 134 | + return ((y + (x << 2)) & 3) == 0; // reduces to return y & 3 == 0 |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void test12() { |
| 139 | + test("shiftAndMaskCompare3", 42, 35); |
| 140 | + } |
| 141 | + |
| 142 | + boolean shiftAndMaskCompare4(long x, long y) { |
| 143 | + return ((y + (x << 2)) & 3) == 0; // reduces to return y & 3 == 0 |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void test13() { |
| 148 | + test("shiftAndMaskCompare4", 42L, 35L); |
| 149 | + } |
| 150 | + |
| 151 | + boolean shiftAndMaskCompare5(int x, int y) { |
| 152 | + return (((y << 2) + (x << 2)) & 3) == 0; // reduces to return true |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + public void test14() { |
| 157 | + test("shiftAndMaskCompare5", 42, 35); |
| 158 | + } |
| 159 | + |
| 160 | + boolean shiftAndMaskCompare6(long x, long y) { |
| 161 | + return (((y << 2) + (x << 2)) & 3) == 0; // reduces to return true |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void test15() { |
| 166 | + test("shiftAndMaskCompare6", 42L, 35L); |
| 167 | + } |
| 168 | + |
| 169 | + boolean shiftAndMaskCompare7(int x) { |
| 170 | + return (((long) (x << 2)) & 3) == 0; // reduces to return true |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + public void test16() { |
| 175 | + test("shiftAndMaskCompare7", 42); |
| 176 | + } |
| 177 | + |
| 178 | + boolean shiftAndMaskCompare8(int x, int y) { |
| 179 | + return ((y + (x << 2)) & 3) == 0; // reduces to y & 3 == 0 |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + public void test17() { |
| 184 | + test("shiftAndMaskCompare8", 42, 35); |
| 185 | + } |
| 186 | + |
| 187 | + boolean shiftAndMaskCompare9(int x, int y) { |
| 188 | + return ((((long) (y << 2)) + ((long) (x << 2))) & 3) == 0; // reduces to return true |
| 189 | + } |
| 190 | + |
| 191 | + @Test |
| 192 | + public void test18() { |
| 193 | + test("shiftAndMaskCompare9", 42, 35); |
| 194 | + } |
| 195 | + |
| 196 | + int orAndMask1(int x) { |
| 197 | + return (x | 4) & 3; // reduces to return x & 3 |
| 198 | + } |
| 199 | + |
| 200 | + @Test |
| 201 | + public void test19() { |
| 202 | + test("orAndMask1", 42); |
| 203 | + } |
| 204 | + |
| 205 | + long orAndMask2(long x) { |
| 206 | + return (x | 4L) & 3L; // reduces to return x & 3 |
| 207 | + } |
| 208 | + |
| 209 | + @Test |
| 210 | + public void test20() { |
| 211 | + test("orAndMask2", 42L); |
| 212 | + } |
| 213 | + |
| 214 | + @Override |
| 215 | + protected void checkMidTierGraph(StructuredGraph graph) { |
| 216 | + super.checkMidTierGraph(graph); |
| 217 | + String methodName = graph.asJavaMethod().getName(); |
| 218 | + if (methodName.startsWith("shift") && !graph.getNodes().filter(n -> n instanceof ShiftNode).isEmpty()) { |
| 219 | + throw new AssertionError("Expected shift nodes to be removed by canonicalization"); |
| 220 | + } else if (methodName.startsWith("or") && !graph.getNodes().filter(n -> n instanceof OrNode).isEmpty()) { |
| 221 | + throw new AssertionError("Expected or nodes to be removed by canonicalization"); |
| 222 | + } |
| 223 | + } |
| 224 | +} |
0 commit comments