1 /*
   2  * Copyright (c) 2014, 2018, 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.lir.gen;
  26 
  27 import jdk.internal.vm.compiler.collections.EconomicMap;
  28 import jdk.internal.vm.compiler.collections.Equivalence;
  29 import org.graalvm.compiler.core.common.CompilationIdentifier;
  30 import org.graalvm.compiler.core.common.CompilationIdentifier.Verbosity;
  31 import org.graalvm.compiler.debug.DebugContext;
  32 import org.graalvm.compiler.lir.LIR;
  33 import org.graalvm.compiler.lir.LIRInstruction;
  34 import org.graalvm.compiler.lir.framemap.FrameMap;
  35 import org.graalvm.compiler.lir.framemap.FrameMapBuilder;
  36 
  37 import jdk.vm.ci.code.CallingConvention;
  38 import jdk.vm.ci.code.RegisterConfig;
  39 
  40 public class LIRGenerationResult {
  41 
  42     private final LIR lir;
  43     private final FrameMapBuilder frameMapBuilder;
  44     private FrameMap frameMap;
  45     private final CallingConvention callingConvention;
  46     /**
  47      * Records whether the code being generated makes at least one foreign call.
  48      */
  49     private boolean hasForeignCall;
  50     /**
  51      * Unique identifier of this compilation.
  52      */
  53     private CompilationIdentifier compilationId;
  54 
  55     /**
  56      * Stores comments about a {@link LIRInstruction} , e.g., which phase created it.
  57      */
  58     private EconomicMap<LIRInstruction, String> comments;
  59 
  60     public LIRGenerationResult(CompilationIdentifier compilationId, LIR lir, FrameMapBuilder frameMapBuilder, CallingConvention callingConvention) {
  61         this.lir = lir;
  62         this.frameMapBuilder = frameMapBuilder;
  63         this.callingConvention = callingConvention;
  64         this.compilationId = compilationId;
  65     }
  66 
  67     /**
  68      * Adds a comment to a {@link LIRInstruction}. Existing comments are replaced.
  69      */
  70     public final void setComment(LIRInstruction op, String comment) {
  71         DebugContext debug = lir.getDebug();
  72         if (debug.isDumpEnabled(DebugContext.BASIC_LEVEL)) {
  73             if (comments == null) {
  74                 comments = EconomicMap.create(Equivalence.IDENTITY);
  75             }
  76             comments.put(op, comment);
  77         }
  78     }
  79 
  80     /**
  81      * Gets the comment attached to a {@link LIRInstruction}.
  82      */
  83     public final String getComment(LIRInstruction op) {
  84         if (comments == null) {
  85             return null;
  86         }
  87         return comments.get(op);
  88     }
  89 
  90     /**
  91      * Returns the incoming calling convention for the parameters of the method that is compiled.
  92      */
  93     public CallingConvention getCallingConvention() {
  94         return callingConvention;
  95     }
  96 
  97     /**
  98      * Returns the {@link FrameMapBuilder} for collecting the information to build a
  99      * {@link FrameMap}.
 100      *
 101      * This method can only be used prior calling {@link #buildFrameMap}.
 102      */
 103     public final FrameMapBuilder getFrameMapBuilder() {
 104         assert frameMap == null : "getFrameMapBuilder() can only be used before calling buildFrameMap()!";
 105         return frameMapBuilder;
 106     }
 107 
 108     /**
 109      * Creates a {@link FrameMap} out of the {@link FrameMapBuilder}. This method should only be
 110      * called once. After calling it, {@link #getFrameMapBuilder()} can no longer be used.
 111      *
 112      * @see FrameMapBuilder#buildFrameMap
 113      */
 114     public void buildFrameMap() {
 115         assert frameMap == null : "buildFrameMap() can only be called once!";
 116         frameMap = frameMapBuilder.buildFrameMap(this);
 117     }
 118 
 119     /**
 120      * Returns the {@link FrameMap} associated with this {@link LIRGenerationResult}.
 121      *
 122      * This method can only be called after {@link #buildFrameMap}.
 123      */
 124     public FrameMap getFrameMap() {
 125         assert frameMap != null : "getFrameMap() can only be used after calling buildFrameMap()!";
 126         return frameMap;
 127     }
 128 
 129     public final RegisterConfig getRegisterConfig() {
 130         return frameMapBuilder.getRegisterConfig();
 131     }
 132 
 133     public LIR getLIR() {
 134         return lir;
 135     }
 136 
 137     /**
 138      * Determines whether the code being generated makes at least one foreign call.
 139      */
 140     public boolean hasForeignCall() {
 141         return hasForeignCall;
 142     }
 143 
 144     public final void setForeignCall(boolean hasForeignCall) {
 145         this.hasForeignCall = hasForeignCall;
 146     }
 147 
 148     public String getCompilationUnitName() {
 149         if (compilationId == null || compilationId == CompilationIdentifier.INVALID_COMPILATION_ID) {
 150             return "<unknown>";
 151         }
 152         return compilationId.toString(Verbosity.NAME);
 153     }
 154 
 155     /**
 156      * Returns a unique identifier of the current compilation.
 157      */
 158     public CompilationIdentifier getCompilationId() {
 159         return compilationId;
 160     }
 161 }