1 /* 2 * Copyright (c) 2012, 2018, 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.core.common.type.IntegerStamp; 28 import org.graalvm.compiler.nodes.StructuredGraph; 29 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; 30 import org.graalvm.compiler.phases.common.CanonicalizerPhase; 31 import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase; 32 import org.junit.Test; 33 34 /** 35 * This class tests some specific patterns the stamp system should be able to canonicalize away 36 * using {@link IntegerStamp#upMask()}. 37 */ 38 public class StampCanonicalizerTest extends GraalCompilerTest { 39 40 public static int andStamp(int a, int b) { 41 int v = (a & 0xffff00) & (b & 0xff0000f); 42 return v & 1; 43 } 44 45 @Test 46 public void testAnd() { 47 testZeroReturn("andStamp"); 48 } 49 50 public static int shiftLeftStamp1(int a) { 51 int v = a << 1; 52 return v & 1; 53 } 54 55 public static int shiftLeftStamp2(int a) { 56 int v = a << 1; 57 if (a == 17) { 58 v = a * 4; 59 } 60 return v & 1; 61 } 62 63 @Test 64 public void testShift() { 65 testZeroReturn("shiftLeftStamp1"); 66 testZeroReturn("shiftLeftStamp2"); 67 } 68 69 public static int upperBoundShiftStamp1(int a) { 70 int v = a & 0xffff; 71 return (v << 4) & 0xff00000; 72 } 73 74 public static int upperBoundShiftStamp2(int a) { 75 int v = a & 0xffff; 76 return (v >> 4) & 0xf000; 77 } 78 79 @Test 80 public void testUpperBoundShift() { 81 testZeroReturn("upperBoundShiftStamp1"); 82 testZeroReturn("upperBoundShiftStamp2"); 83 } 84 85 public static int divStamp1(int[] a) { 86 int v = a.length / 4; 87 return v & 0x80000000; 88 } 89 90 public static int divStamp2(int[] a) { 91 int v = a.length / 5; 92 return v & 0x80000000; 93 } 94 95 @Test 96 public void testDiv() { 97 testZeroReturn("divStamp1"); 98 testZeroReturn("divStamp2"); 99 } 100 101 public static int distinctMask(int a, int b) { 102 int x = a & 0xaaaa; 103 int y = (b & 0x5555) | 0x1; 104 return x == y ? 1 : 0; 105 } 106 107 @Test 108 public void testDistinctMask() { 109 testZeroReturn("distinctMask"); 110 } 111 112 private void testZeroReturn(String methodName) { 113 StructuredGraph graph = parseEager(methodName, AllowAssumptions.YES); 114 new CanonicalizerPhase().apply(graph, getProviders()); 115 new DeadCodeEliminationPhase().apply(graph); 116 assertConstantReturn(graph, 0); 117 } 118 }