ConditionalEliminationTest2.java revision 12651:6ef01bd40ce2
1/*
2 * Copyright (c) 2011, 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 */
23package org.graalvm.compiler.core.test;
24
25import org.junit.Test;
26
27import org.graalvm.compiler.nodes.GuardNode;
28import org.graalvm.compiler.nodes.StructuredGraph;
29import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
30import org.graalvm.compiler.nodes.spi.LoweringTool;
31import org.graalvm.compiler.phases.common.CanonicalizerPhase;
32import org.graalvm.compiler.phases.common.DominatorConditionalEliminationPhase;
33import org.graalvm.compiler.phases.common.FloatingReadPhase;
34import org.graalvm.compiler.phases.common.LoweringPhase;
35import org.graalvm.compiler.phases.tiers.PhaseContext;
36
37/**
38 * Collection of tests for
39 * {@link org.graalvm.compiler.phases.common.DominatorConditionalEliminationPhase} including those
40 * that triggered bugs in this phase.
41 */
42public class ConditionalEliminationTest2 extends ConditionalEliminationTestBase {
43
44    public static Object field;
45
46    static class Entry {
47
48        final String name;
49
50        Entry(String name) {
51            this.name = name;
52        }
53    }
54
55    static class EntryWithNext extends Entry {
56
57        EntryWithNext(String name, Entry next) {
58            super(name);
59            this.next = next;
60        }
61
62        final Entry next;
63    }
64
65    public static Entry search(Entry start, String name, Entry alternative) {
66        Entry current = start;
67        do {
68            while (current instanceof EntryWithNext) {
69                if (name != null && current.name == name) {
70                    current = null;
71                } else {
72                    Entry next = ((EntryWithNext) current).next;
73                    current = next;
74                }
75            }
76
77            if (current != null) {
78                if (current.name.equals(name)) {
79                    return current;
80                }
81            }
82            if (current == alternative) {
83                return null;
84            }
85            current = alternative;
86
87        } while (true);
88    }
89
90    public static int testRedundantComparesSnippet(int[] array) {
91        if (array == null) {
92            return 0;
93        }
94        return array[0] + array[1] + array[2] + array[3];
95    }
96
97    @Test
98    public void testRedundantCompares() {
99        StructuredGraph graph = parseEager("testRedundantComparesSnippet", AllowAssumptions.YES);
100        CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
101        PhaseContext context = new PhaseContext(getProviders());
102
103        new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
104        canonicalizer.apply(graph, context);
105        new FloatingReadPhase().apply(graph);
106        new DominatorConditionalEliminationPhase(true).apply(graph, context);
107        canonicalizer.apply(graph, context);
108
109        assertDeepEquals(1, graph.getNodes().filter(GuardNode.class).count());
110    }
111
112    public static String testInstanceOfCheckCastSnippet(Object e) {
113        if (e instanceof Entry) {
114            return ((Entry) e).name;
115        }
116        return null;
117    }
118
119    @Test
120    public void testInstanceOfCheckCastLowered() {
121        StructuredGraph graph = parseEager("testInstanceOfCheckCastSnippet", AllowAssumptions.YES);
122
123        CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
124        PhaseContext context = new PhaseContext(getProviders());
125
126        new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
127        canonicalizer.apply(graph, context);
128        new DominatorConditionalEliminationPhase(true).apply(graph, context);
129        canonicalizer.apply(graph, context);
130
131        assertDeepEquals(0, graph.getNodes().filter(GuardNode.class).count());
132    }
133
134}
135