1 /*
   2  * Copyright (c) 2013, 2016, 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  */
  23 package org.graalvm.compiler.core.test;
  24 
  25 import static org.graalvm.compiler.core.GraalCompiler.compileGraph;
  26 import static org.graalvm.compiler.core.common.GraalOptions.OptAssumptions;
  27 import static org.junit.Assert.assertNotNull;
  28 
  29 import org.junit.Test;
  30 
  31 import org.graalvm.compiler.code.CompilationResult;
  32 import org.graalvm.compiler.lir.asm.CompilationResultBuilderFactory;
  33 import org.graalvm.compiler.nodes.FullInfopointNode;
  34 import org.graalvm.compiler.nodes.StructuredGraph;
  35 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  36 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
  37 import org.graalvm.compiler.phases.OptimisticOptimizations;
  38 import org.graalvm.compiler.phases.PhaseSuite;
  39 import org.graalvm.compiler.phases.tiers.HighTierContext;
  40 
  41 import jdk.vm.ci.code.site.Call;
  42 import jdk.vm.ci.code.site.Infopoint;
  43 import jdk.vm.ci.code.site.InfopointReason;
  44 import jdk.vm.ci.meta.ResolvedJavaMethod;
  45 
  46 /**
  47  * Test that infopoints in {@link CompilationResult}s have correctly assigned reasons.
  48  */
  49 public class InfopointReasonTest extends GraalCompilerTest {
  50 
  51     public static final String[] STRINGS = new String[]{"world", "everyone", "you"};
  52 
  53     public String testMethod() {
  54         StringBuilder sb = new StringBuilder("Hello ");
  55         for (String s : STRINGS) {
  56             sb.append(s).append(", ");
  57         }
  58         sb.replace(sb.length() - 2, sb.length(), "!");
  59         return sb.toString();
  60     }
  61 
  62     @Test
  63     public void callInfopoints() {
  64         final ResolvedJavaMethod method = getResolvedJavaMethod("testMethod");
  65         final StructuredGraph graph = parseEager(method, AllowAssumptions.YES);
  66         final CompilationResult cr = compileGraph(graph, graph.method(), getProviders(), getBackend(), getDefaultGraphBuilderSuite(), OptimisticOptimizations.ALL, graph.getProfilingInfo(),
  67                         createSuites(graph.getOptions()), createLIRSuites(graph.getOptions()), new CompilationResult(graph.compilationId()), CompilationResultBuilderFactory.Default);
  68         for (Infopoint sp : cr.getInfopoints()) {
  69             assertNotNull(sp.reason);
  70             if (sp instanceof Call) {
  71                 assertDeepEquals(InfopointReason.CALL, sp.reason);
  72             }
  73         }
  74     }
  75 
  76     @Test
  77     public void lineInfopoints() {
  78         final ResolvedJavaMethod method = getResolvedJavaMethod("testMethod");
  79         final StructuredGraph graph = parse(builder(method, AllowAssumptions.ifTrue(OptAssumptions.getValue(getInitialOptions()))), getDebugGraphBuilderSuite());
  80         int graphLineSPs = 0;
  81         for (FullInfopointNode ipn : graph.getNodes().filter(FullInfopointNode.class)) {
  82             if (ipn.getReason() == InfopointReason.BYTECODE_POSITION) {
  83                 ++graphLineSPs;
  84             }
  85         }
  86         assertTrue(graphLineSPs > 0);
  87         PhaseSuite<HighTierContext> graphBuilderSuite = getCustomGraphBuilderSuite(GraphBuilderConfiguration.getDefault(getDefaultGraphBuilderPlugins()).withFullInfopoints(true));
  88         final CompilationResult cr = compileGraph(graph, graph.method(), getProviders(), getBackend(), graphBuilderSuite, OptimisticOptimizations.ALL, graph.getProfilingInfo(),
  89                         createSuites(graph.getOptions()), createLIRSuites(graph.getOptions()), new CompilationResult(graph.compilationId()), CompilationResultBuilderFactory.Default);
  90         int lineSPs = 0;
  91         for (Infopoint sp : cr.getInfopoints()) {
  92             assertNotNull(sp.reason);
  93             if (sp.reason == InfopointReason.BYTECODE_POSITION) {
  94                 ++lineSPs;
  95             }
  96         }
  97         assertTrue(lineSPs > 0);
  98     }
  99 
 100 }