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 org.junit.*;
026
027import com.oracle.graal.nodes.*;
028import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions;
029import com.oracle.graal.nodes.spi.*;
030import com.oracle.graal.phases.common.*;
031import com.oracle.graal.phases.tiers.*;
032
033/**
034 * Collection of tests for
035 * {@link com.oracle.graal.phases.common.DominatorConditionalEliminationPhase} including those that
036 * triggered bugs in this phase.
037 */
038public class ConditionalEliminationTest2 extends ConditionalEliminationTestBase {
039
040    public static Object field;
041
042    static class Entry {
043
044        final String name;
045
046        public Entry(String name) {
047            this.name = name;
048        }
049    }
050
051    static class EntryWithNext extends Entry {
052
053        public EntryWithNext(String name, Entry next) {
054            super(name);
055            this.next = next;
056        }
057
058        final Entry next;
059    }
060
061    public static Entry search(Entry start, String name, Entry alternative) {
062        Entry current = start;
063        do {
064            while (current instanceof EntryWithNext) {
065                if (name != null && current.name == name) {
066                    current = null;
067                } else {
068                    Entry next = ((EntryWithNext) current).next;
069                    current = next;
070                }
071            }
072
073            if (current != null) {
074                if (current.name.equals(name)) {
075                    return current;
076                }
077            }
078            if (current == alternative) {
079                return null;
080            }
081            current = alternative;
082
083        } while (true);
084    }
085
086    public static int testRedundantComparesSnippet(int[] array) {
087        if (array == null) {
088            return 0;
089        }
090        return array[0] + array[1] + array[2] + array[3];
091    }
092
093    @Test
094    public void testRedundantCompares() {
095        StructuredGraph graph = parseEager("testRedundantComparesSnippet", AllowAssumptions.YES);
096        CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
097        PhaseContext context = new PhaseContext(getProviders());
098
099        new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
100        canonicalizer.apply(graph, context);
101        new FloatingReadPhase().apply(graph);
102        new DominatorConditionalEliminationPhase(true).apply(graph, context);
103        canonicalizer.apply(graph, context);
104
105        assertDeepEquals(1, graph.getNodes().filter(GuardNode.class).count());
106    }
107
108    public static String testInstanceOfCheckCastSnippet(Object e) {
109        if (e instanceof Entry) {
110            return ((Entry) e).name;
111        }
112        return null;
113    }
114
115    @Test
116    public void testInstanceOfCheckCastLowered() {
117        StructuredGraph graph = parseEager("testInstanceOfCheckCastSnippet", AllowAssumptions.YES);
118
119        CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
120        PhaseContext context = new PhaseContext(getProviders());
121
122        new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
123        canonicalizer.apply(graph, context);
124        new DominatorConditionalEliminationPhase(true).apply(graph, context);
125        canonicalizer.apply(graph, context);
126
127        assertDeepEquals(0, graph.getNodes().filter(GuardNode.class).count());
128    }
129
130}