001/*
002 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.compiler.test;
024
025import org.junit.*;
026
027import com.oracle.graal.compiler.common.type.*;
028import com.oracle.graal.nodes.*;
029import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions;
030import com.oracle.graal.phases.common.*;
031import com.oracle.graal.phases.tiers.*;
032
033/**
034 * This class tests some specific patterns the stamp system should be able to canonicalize away
035 * using {@link IntegerStamp#upMask()}.
036 */
037public class StampCanonicalizerTest extends GraalCompilerTest {
038
039    public static int andStamp(int a, int b) {
040        int v = (a & 0xffff00) & (b & 0xff0000f);
041        return v & 1;
042    }
043
044    @Test
045    public void testAnd() {
046        testZeroReturn("andStamp");
047    }
048
049    public static int shiftLeftStamp1(int a) {
050        int v = a << 1;
051        return v & 1;
052    }
053
054    public static int shiftLeftStamp2(int a) {
055        int v = a << 1;
056        if (a == 17) {
057            v = a * 4;
058        }
059        return v & 1;
060    }
061
062    @Test
063    public void testShift() {
064        testZeroReturn("shiftLeftStamp1");
065        testZeroReturn("shiftLeftStamp2");
066    }
067
068    public static int upperBoundShiftStamp1(int a) {
069        int v = a & 0xffff;
070        return (v << 4) & 0xff00000;
071    }
072
073    public static int upperBoundShiftStamp2(int a) {
074        int v = a & 0xffff;
075        return (v >> 4) & 0xf000;
076    }
077
078    @Test
079    public void testUpperBoundShift() {
080        testZeroReturn("upperBoundShiftStamp1");
081        testZeroReturn("upperBoundShiftStamp2");
082    }
083
084    public static int divStamp1(int[] a) {
085        int v = a.length / 4;
086        return v & 0x80000000;
087    }
088
089    public static int divStamp2(int[] a) {
090        int v = a.length / 5;
091        return v & 0x80000000;
092    }
093
094    @Test
095    public void testDiv() {
096        testZeroReturn("divStamp1");
097        testZeroReturn("divStamp2");
098    }
099
100    public static int distinctMask(int a, int b) {
101        int x = a & 0xaaaa;
102        int y = (b & 0x5555) | 0x1;
103        return x == y ? 1 : 0;
104    }
105
106    @Test
107    public void testDistinctMask() {
108        testZeroReturn("distinctMask");
109    }
110
111    private void testZeroReturn(String methodName) {
112        StructuredGraph graph = parseEager(methodName, AllowAssumptions.YES);
113        new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders()));
114        new DeadCodeEliminationPhase().apply(graph);
115        assertConstantReturn(graph, 0);
116    }
117}