1 /* 2 * Copyright (c) 2014, 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.nodes; 26 27 import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC; 28 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_2; 29 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_2; 30 31 import org.graalvm.compiler.core.common.CompressEncoding; 32 import org.graalvm.compiler.core.common.type.AbstractObjectStamp; 33 import org.graalvm.compiler.core.common.type.Stamp; 34 import org.graalvm.compiler.debug.GraalError; 35 import org.graalvm.compiler.graph.NodeClass; 36 import org.graalvm.compiler.graph.spi.CanonicalizerTool; 37 import org.graalvm.compiler.lir.gen.LIRGeneratorTool; 38 import org.graalvm.compiler.nodeinfo.NodeInfo; 39 import org.graalvm.compiler.nodes.calc.ConvertNode; 40 import org.graalvm.compiler.nodes.calc.UnaryNode; 41 import org.graalvm.compiler.nodes.spi.LIRLowerable; 42 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool; 43 import org.graalvm.compiler.nodes.type.StampTool; 44 45 import jdk.vm.ci.meta.Constant; 46 import jdk.vm.ci.meta.JavaConstant; 47 import jdk.vm.ci.meta.ConstantReflectionProvider; 48 import jdk.vm.ci.meta.Value; 49 50 /** 51 * Compress or uncompress an oop or metaspace pointer. 52 */ 53 @NodeInfo(nameTemplate = "{p#op/s}", cycles = CYCLES_2, size = SIZE_2) 54 public abstract class CompressionNode extends UnaryNode implements ConvertNode, LIRLowerable { 55 56 public static final NodeClass<CompressionNode> TYPE = NodeClass.create(CompressionNode.class); 57 58 public enum CompressionOp { 59 Compress, 60 Uncompress 61 } 62 63 protected final CompressionOp op; 64 protected final CompressEncoding encoding; 65 CompressionNode(NodeClass<? extends UnaryNode> c, CompressionOp op, ValueNode input, Stamp stamp, CompressEncoding encoding)66 public CompressionNode(NodeClass<? extends UnaryNode> c, CompressionOp op, ValueNode input, Stamp stamp, CompressEncoding encoding) { 67 super(c, stamp, input); 68 this.op = op; 69 this.encoding = encoding; 70 } 71 72 @Override foldStamp(Stamp newStamp)73 public Stamp foldStamp(Stamp newStamp) { 74 assert newStamp.isCompatible(getValue().stamp(NodeView.DEFAULT)); 75 return mkStamp(newStamp); 76 } 77 compress(Constant c)78 protected abstract Constant compress(Constant c); 79 uncompress(Constant c)80 protected abstract Constant uncompress(Constant c); 81 nullConstant()82 public JavaConstant nullConstant() { 83 return JavaConstant.NULL_POINTER; 84 } 85 86 @Override convert(Constant c, ConstantReflectionProvider constantReflection)87 public Constant convert(Constant c, ConstantReflectionProvider constantReflection) { 88 switch (op) { 89 case Compress: 90 return compress(c); 91 case Uncompress: 92 return uncompress(c); 93 default: 94 throw GraalError.shouldNotReachHere(); 95 } 96 } 97 98 @Override reverse(Constant c, ConstantReflectionProvider constantReflection)99 public Constant reverse(Constant c, ConstantReflectionProvider constantReflection) { 100 switch (op) { 101 case Compress: 102 return uncompress(c); 103 case Uncompress: 104 return compress(c); 105 default: 106 throw GraalError.shouldNotReachHere(); 107 } 108 } 109 110 @Override isLossless()111 public boolean isLossless() { 112 return true; 113 } 114 mkStamp(Stamp input)115 protected abstract Stamp mkStamp(Stamp input); 116 getOp()117 public CompressionOp getOp() { 118 return op; 119 } 120 getEncoding()121 public CompressEncoding getEncoding() { 122 return encoding; 123 } 124 125 @Override canonical(CanonicalizerTool tool, ValueNode forValue)126 public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) { 127 if (forValue.isConstant()) { 128 if (GeneratePIC.getValue(tool.getOptions())) { 129 // We always want uncompressed constants 130 return this; 131 } 132 133 ConstantNode constant = (ConstantNode) forValue; 134 return ConstantNode.forConstant(stamp(NodeView.DEFAULT), convert(constant.getValue(), tool.getConstantReflection()), constant.getStableDimension(), constant.isDefaultStable(), 135 tool.getMetaAccess()); 136 } else if (forValue instanceof CompressionNode) { 137 CompressionNode other = (CompressionNode) forValue; 138 if (op != other.op && encoding.equals(other.encoding)) { 139 return other.getValue(); 140 } 141 } 142 return this; 143 } 144 145 @Override generate(NodeLIRBuilderTool gen)146 public void generate(NodeLIRBuilderTool gen) { 147 boolean nonNull; 148 if (value.stamp(NodeView.DEFAULT) instanceof AbstractObjectStamp) { 149 nonNull = StampTool.isPointerNonNull(value.stamp(NodeView.DEFAULT)); 150 } else { 151 // metaspace pointers are never null 152 nonNull = true; 153 } 154 155 LIRGeneratorTool tool = gen.getLIRGeneratorTool(); 156 Value result; 157 switch (op) { 158 case Compress: 159 result = tool.emitCompress(gen.operand(value), encoding, nonNull); 160 break; 161 case Uncompress: 162 result = tool.emitUncompress(gen.operand(value), encoding, nonNull); 163 break; 164 default: 165 throw GraalError.shouldNotReachHere(); 166 } 167 168 gen.setResult(this, result); 169 } 170 171 @Override mayNullCheckSkipConversion()172 public boolean mayNullCheckSkipConversion() { 173 return true; 174 } 175 } 176