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.replacements.test;
024
025import jdk.internal.jvmci.meta.*;
026
027import org.junit.*;
028
029import com.oracle.graal.compiler.test.*;
030import com.oracle.graal.nodes.*;
031import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions;
032import com.oracle.graal.nodes.java.*;
033import com.oracle.graal.phases.common.*;
034
035/**
036 * Tests compilation of a hot exception handler.
037 */
038public class CompiledExceptionHandlerTest extends GraalCompilerTest {
039
040    public CompiledExceptionHandlerTest() {
041        getSuites().getHighTier().findPhase(AbstractInliningPhase.class).remove();
042    }
043
044    @Override
045    protected StructuredGraph parseEager(ResolvedJavaMethod m, AllowAssumptions allowAssumptions) {
046        StructuredGraph graph = super.parseEager(m, allowAssumptions);
047        int handlers = graph.getNodes().filter(ExceptionObjectNode.class).count();
048        Assert.assertEquals(1, handlers);
049        return graph;
050    }
051
052    private static void raiseExceptionSimple(String s) {
053        throw new RuntimeException("Raising exception with message \"" + s + "\"");
054    }
055
056    @Test
057    public void test1() {
058        // Ensure the profile shows a hot exception
059        for (int i = 0; i < 10000; i++) {
060            test1Snippet("");
061            test1Snippet(null);
062        }
063
064        test("test1Snippet", "a string");
065        test("test1Snippet", (String) null);
066    }
067
068    public static String test1Snippet(String message) {
069        if (message != null) {
070            try {
071                raiseExceptionSimple(message);
072            } catch (Exception e) {
073                return message + e.getMessage();
074            }
075        }
076        return null;
077    }
078
079    private static void raiseException(String m1, String m2, String m3, String m4, String m5) {
080        throw new RuntimeException(m1 + m2 + m3 + m4 + m5);
081    }
082
083    @Test
084    public void test2() {
085        // Ensure the profile shows a hot exception
086        for (int i = 0; i < 10000; i++) {
087            test2Snippet("m1", "m2", "m3", "m4", "m5");
088            test2Snippet(null, "m2", "m3", "m4", "m5");
089        }
090
091        test("test2Snippet", "m1", "m2", "m3", "m4", "m5");
092        test("test2Snippet", null, "m2", "m3", "m4", "m5");
093    }
094
095    public static String test2Snippet(String m1, String m2, String m3, String m4, String m5) {
096        if (m1 != null) {
097            try {
098                raiseException(m1, m2, m3, m4, m5);
099            } catch (Exception e) {
100                return m5 + m4 + m3 + m2 + m1;
101            }
102        }
103        return m4 + m3;
104    }
105
106    @Test
107    public void test3() {
108        // Ensure the profile shows a hot exception
109        for (int i = 0; i < 10000; i++) {
110            test3Snippet("object1", "object2");
111            test3Snippet(null, "object2");
112        }
113
114        test("test3Snippet", (Object) null, "object2");
115        test("test3Snippet", "object1", "object2");
116    }
117
118    public static String test3Snippet(Object o, Object o2) {
119        try {
120            return o.toString();
121        } catch (NullPointerException e) {
122            return String.valueOf(o2);
123        }
124    }
125}