001/*
002 * Copyright (c) 2011, 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 com.oracle.graal.debug.*;
026
027import org.junit.*;
028
029import com.oracle.graal.nodes.*;
030import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions;
031import com.oracle.graal.phases.common.*;
032import com.oracle.graal.phases.tiers.*;
033
034public class StraighteningTest extends GraalCompilerTest {
035
036    private static final String REFERENCE_SNIPPET = "ref";
037
038    public static boolean ref(int a, int b) {
039        return a == b;
040    }
041
042    public static boolean test1Snippet(int a, int b) {
043        int c = a;
044        if (c == b) {
045            c = 0x55;
046        }
047        if (c != 0x55) {
048            return false;
049        }
050        return true;
051    }
052
053    public static boolean test3Snippet(int a, int b) {
054        int val = (int) System.currentTimeMillis();
055        int c = val + 1;
056        if (a == b) {
057            c = val;
058        }
059        if (c != val) {
060            return false;
061        }
062        return true;
063    }
064
065    public static boolean test2Snippet(int a, int b) {
066        int c;
067        if (a == b) {
068            c = 1;
069        } else {
070            c = 0;
071        }
072        return c == 1;
073    }
074
075    @Test(expected = AssertionError.class)
076    public void test1() {
077        test("test1Snippet");
078    }
079
080    public void test2() {
081        test("test2Snippet");
082    }
083
084    @Test(expected = AssertionError.class)
085    public void test3() {
086        test("test3Snippet");
087    }
088
089    private void test(final String snippet) {
090        // No debug scope to reduce console noise for @Test(expected = ...) tests
091        StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
092        Debug.dump(graph, "Graph");
093        new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders()));
094        StructuredGraph referenceGraph = parseEager(REFERENCE_SNIPPET, AllowAssumptions.YES);
095        assertEquals(referenceGraph, graph);
096    }
097}