1 /*
   2  * Copyright (c) 2015, 2017, 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.hotspot.nodes;
  24 
  25 import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC;
  26 import static org.graalvm.compiler.hotspot.GraalHotSpotVMConfig.INJECTED_VMCONFIG;
  27 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_1;
  28 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_1;
  29 
  30 import org.graalvm.compiler.api.replacements.Fold;
  31 import org.graalvm.compiler.api.replacements.Fold.InjectedParameter;
  32 import org.graalvm.compiler.core.common.type.StampFactory;
  33 import org.graalvm.compiler.graph.Node;
  34 import org.graalvm.compiler.graph.NodeClass;
  35 import org.graalvm.compiler.graph.spi.Canonicalizable;
  36 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  37 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
  38 import org.graalvm.compiler.hotspot.HotSpotLIRGenerator;
  39 import org.graalvm.compiler.nodeinfo.NodeInfo;
  40 import org.graalvm.compiler.nodes.ConstantNode;
  41 import org.graalvm.compiler.nodes.calc.FloatingNode;
  42 import org.graalvm.compiler.nodes.spi.LIRLowerable;
  43 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  44 
  45 import jdk.vm.ci.meta.JavaKind;
  46 import jdk.vm.ci.meta.Value;
  47 
  48 /**
  49  * Represents {@link GraalHotSpotVMConfig} values that may change after compilation.
  50  */
  51 @NodeInfo(cycles = CYCLES_1, size = SIZE_1)
  52 public class GraalHotSpotVMConfigNode extends FloatingNode implements LIRLowerable, Canonicalizable {
  53     public static final NodeClass<GraalHotSpotVMConfigNode> TYPE = NodeClass.create(GraalHotSpotVMConfigNode.class);
  54 
  55     private final GraalHotSpotVMConfig config;
  56     protected final int markId;
  57 
  58     /**
  59      * Constructor for {@link #areConfigValuesConstant()}.
  60      *
  61      * @param config
  62      */
  63     public GraalHotSpotVMConfigNode(@InjectedNodeParameter GraalHotSpotVMConfig config) {
  64         super(TYPE, StampFactory.forKind(JavaKind.Boolean));
  65         this.config = config;
  66         this.markId = 0;
  67     }
  68 
  69     /**
  70      * Constructor for node intrinsics below.
  71      *
  72      * @param config
  73      * @param markId id of the config value
  74      */
  75     public GraalHotSpotVMConfigNode(@InjectedNodeParameter GraalHotSpotVMConfig config, int markId) {
  76         super(TYPE, StampFactory.forNodeIntrinsic());
  77         this.config = config;
  78         this.markId = markId;
  79     }
  80 
  81     /**
  82      * Constructor with explicit type specification.
  83      *
  84      * @param config
  85      * @param markId id of the config value
  86      * @param kind explicit type of the node
  87      */
  88     public GraalHotSpotVMConfigNode(@InjectedNodeParameter GraalHotSpotVMConfig config, int markId, JavaKind kind) {
  89         super(TYPE, StampFactory.forKind(kind));
  90         this.config = config;
  91         this.markId = markId;
  92     }
  93 
  94     @Override
  95     public void generate(NodeLIRBuilderTool generator) {
  96         Value res = ((HotSpotLIRGenerator) generator.getLIRGeneratorTool()).emitLoadConfigValue(markId, generator.getLIRGeneratorTool().getLIRKind(stamp));
  97         generator.setResult(this, res);
  98     }
  99 
 100     @NodeIntrinsic
 101     private static native boolean areConfigValuesConstant();
 102 
 103     @NodeIntrinsic(setStampFromReturnType = true)
 104     private static native long loadLongConfigValue(@ConstantNodeParameter int markId);
 105 
 106     @NodeIntrinsic(setStampFromReturnType = true)
 107     private static native int loadIntConfigValue(@ConstantNodeParameter int markId);
 108 
 109     @NodeIntrinsic(setStampFromReturnType = true)
 110     private static native byte loadByteConfigValue(@ConstantNodeParameter int markId);
 111 
 112     public static long cardTableAddress() {
 113         return loadLongConfigValue(cardTableAddressMark(INJECTED_VMCONFIG));
 114     }
 115 
 116     public static boolean isCardTableAddressConstant() {
 117         return areConfigValuesConstant();
 118     }
 119 
 120     public static long heapTopAddress() {
 121         return loadLongConfigValue(heapTopAddressMark(INJECTED_VMCONFIG));
 122     }
 123 
 124     public static long heapEndAddress() {
 125         return loadLongConfigValue(heapEndAddressMark(INJECTED_VMCONFIG));
 126     }
 127 
 128     public static long crcTableAddress() {
 129         return loadLongConfigValue(crcTableAddressMark(INJECTED_VMCONFIG));
 130     }
 131 
 132     public static int logOfHeapRegionGrainBytes() {
 133         return loadIntConfigValue(logOfHeapRegionGrainBytesMark(INJECTED_VMCONFIG));
 134     }
 135 
 136     public static boolean inlineContiguousAllocationSupported() {
 137         return loadByteConfigValue(inlineContiguousAllocationSupportedMark(INJECTED_VMCONFIG)) != 0;
 138     }
 139 
 140     @Fold
 141     public static int cardTableAddressMark(@InjectedParameter GraalHotSpotVMConfig config) {
 142         return config.MARKID_CARD_TABLE_ADDRESS;
 143     }
 144 
 145     @Fold
 146     public static int heapTopAddressMark(@InjectedParameter GraalHotSpotVMConfig config) {
 147         return config.MARKID_HEAP_TOP_ADDRESS;
 148     }
 149 
 150     @Fold
 151     public static int heapEndAddressMark(@InjectedParameter GraalHotSpotVMConfig config) {
 152         return config.MARKID_HEAP_END_ADDRESS;
 153     }
 154 
 155     @Fold
 156     public static int crcTableAddressMark(@InjectedParameter GraalHotSpotVMConfig config) {
 157         return config.MARKID_CRC_TABLE_ADDRESS;
 158     }
 159 
 160     @Fold
 161     public static int logOfHeapRegionGrainBytesMark(@InjectedParameter GraalHotSpotVMConfig config) {
 162         return config.MARKID_LOG_OF_HEAP_REGION_GRAIN_BYTES;
 163     }
 164 
 165     @Fold
 166     public static int inlineContiguousAllocationSupportedMark(@InjectedParameter GraalHotSpotVMConfig config) {
 167         return config.MARKID_INLINE_CONTIGUOUS_ALLOCATION_SUPPORTED;
 168     }
 169 
 170     @Override
 171     public Node canonical(CanonicalizerTool tool) {
 172         if (markId == 0) {
 173             return ConstantNode.forBoolean(!GeneratePIC.getValue(tool.getOptions()));
 174         }
 175         if (!GeneratePIC.getValue(tool.getOptions())) {
 176             if (markId == config.MARKID_CARD_TABLE_ADDRESS) {
 177                 return ConstantNode.forLong(config.cardtableStartAddress);
 178             } else if (markId == config.MARKID_HEAP_TOP_ADDRESS) {
 179                 return ConstantNode.forLong(config.heapTopAddress);
 180             } else if (markId == config.MARKID_HEAP_END_ADDRESS) {
 181                 return ConstantNode.forLong(config.heapEndAddress);
 182             } else if (markId == config.MARKID_CRC_TABLE_ADDRESS) {
 183                 return ConstantNode.forLong(config.crcTableAddress);
 184             } else if (markId == config.MARKID_LOG_OF_HEAP_REGION_GRAIN_BYTES) {
 185                 return ConstantNode.forInt(config.logOfHRGrainBytes);
 186             } else if (markId == config.MARKID_INLINE_CONTIGUOUS_ALLOCATION_SUPPORTED) {
 187                 return ConstantNode.forBoolean(config.inlineContiguousAllocationSupported);
 188             } else {
 189                 assert false;
 190             }
 191         }
 192         return this;
 193     }
 194 }