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