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