001/*
002 * Copyright (c) 2013, 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 jdk.internal.jvmci.code.*;
026import jdk.internal.jvmci.code.CallingConvention.*;
027import jdk.internal.jvmci.code.CompilationResult.*;
028import jdk.internal.jvmci.meta.*;
029import static com.oracle.graal.compiler.GraalCompiler.*;
030import static com.oracle.graal.compiler.common.GraalOptions.*;
031import static jdk.internal.jvmci.code.CodeUtil.*;
032import static org.junit.Assert.*;
033
034import org.junit.*;
035
036import com.oracle.graal.graphbuilderconf.*;
037import com.oracle.graal.lir.asm.*;
038import com.oracle.graal.nodes.*;
039import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions;
040import com.oracle.graal.phases.*;
041import com.oracle.graal.phases.tiers.*;
042
043/**
044 * Test that infopoints in {@link CompilationResult}s have correctly assigned reasons.
045 */
046public class InfopointReasonTest extends GraalCompilerTest {
047
048    public static final String[] STRINGS = new String[]{"world", "everyone", "you"};
049
050    public String testMethod() {
051        StringBuilder sb = new StringBuilder("Hello ");
052        for (String s : STRINGS) {
053            sb.append(s).append(", ");
054        }
055        sb.replace(sb.length() - 2, sb.length(), "!");
056        return sb.toString();
057    }
058
059    @Test
060    public void callInfopoints() {
061        final ResolvedJavaMethod method = getResolvedJavaMethod("testMethod");
062        final StructuredGraph graph = parseEager(method, AllowAssumptions.YES);
063        CallingConvention cc = getCallingConvention(getCodeCache(), Type.JavaCallee, graph.method(), false);
064        final CompilationResult cr = compileGraph(graph, cc, graph.method(), getProviders(), getBackend(), getCodeCache().getTarget(), getDefaultGraphBuilderSuite(), OptimisticOptimizations.ALL,
065                        getProfilingInfo(graph), getSuites(), getLIRSuites(), new CompilationResult(), CompilationResultBuilderFactory.Default);
066        for (Infopoint sp : cr.getInfopoints()) {
067            assertNotNull(sp.reason);
068            if (sp instanceof Call) {
069                assertDeepEquals(InfopointReason.CALL, sp.reason);
070            }
071        }
072    }
073
074    @Test
075    public void lineInfopoints() {
076        final ResolvedJavaMethod method = getResolvedJavaMethod("testMethod");
077        final StructuredGraph graph = parseDebug(method, AllowAssumptions.from(OptAssumptions.getValue()));
078        int graphLineSPs = 0;
079        for (FullInfopointNode ipn : graph.getNodes().filter(FullInfopointNode.class)) {
080            if (ipn.getReason() == InfopointReason.LINE_NUMBER) {
081                ++graphLineSPs;
082            }
083        }
084        assertTrue(graphLineSPs > 0);
085        CallingConvention cc = getCallingConvention(getCodeCache(), Type.JavaCallee, graph.method(), false);
086        PhaseSuite<HighTierContext> graphBuilderSuite = getCustomGraphBuilderSuite(GraphBuilderConfiguration.getFullDebugDefault(getDefaultGraphBuilderPlugins()));
087        final CompilationResult cr = compileGraph(graph, cc, graph.method(), getProviders(), getBackend(), getCodeCache().getTarget(), graphBuilderSuite, OptimisticOptimizations.ALL,
088                        getProfilingInfo(graph), getSuites(), getLIRSuites(), new CompilationResult(), CompilationResultBuilderFactory.Default);
089        int lineSPs = 0;
090        for (Infopoint sp : cr.getInfopoints()) {
091            assertNotNull(sp.reason);
092            if (sp.reason == InfopointReason.LINE_NUMBER) {
093                ++lineSPs;
094            }
095        }
096        assertTrue(lineSPs > 0);
097    }
098
099}