1/*
2 * Copyright (c) 2017, 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 */
23package org.graalvm.compiler.hotspot.nodes;
24
25import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_2;
26import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_2;
27
28import org.graalvm.compiler.core.common.CompressEncoding;
29import org.graalvm.compiler.core.common.type.Stamp;
30import org.graalvm.compiler.debug.GraalError;
31import org.graalvm.compiler.graph.NodeClass;
32import org.graalvm.compiler.hotspot.nodes.type.HotSpotNarrowOopStamp;
33import org.graalvm.compiler.nodeinfo.NodeInfo;
34import org.graalvm.compiler.nodes.CompressionNode;
35import org.graalvm.compiler.nodes.ValueNode;
36
37import jdk.vm.ci.hotspot.HotSpotCompressedNullConstant;
38import jdk.vm.ci.hotspot.HotSpotConstant;
39import jdk.vm.ci.meta.Constant;
40import jdk.vm.ci.meta.JavaConstant;
41
42@NodeInfo(nameTemplate = "{p#op/s}", cycles = CYCLES_2, size = SIZE_2)
43public final class HotSpotCompressionNode extends CompressionNode {
44
45    public static final NodeClass<HotSpotCompressionNode> TYPE = NodeClass.create(HotSpotCompressionNode.class);
46
47    public HotSpotCompressionNode(CompressionOp op, ValueNode input, CompressEncoding encoding) {
48        super(TYPE, op, input, HotSpotNarrowOopStamp.mkStamp(op, input.stamp(), encoding), encoding);
49    }
50
51    public static HotSpotCompressionNode compress(ValueNode input, CompressEncoding encoding) {
52        return input.graph().unique(new HotSpotCompressionNode(CompressionOp.Compress, input, encoding));
53    }
54
55    public static CompressionNode uncompress(ValueNode input, CompressEncoding encoding) {
56        return input.graph().unique(new HotSpotCompressionNode(CompressionOp.Uncompress, input, encoding));
57    }
58
59    @Override
60    protected Constant compress(Constant c) {
61        if (JavaConstant.NULL_POINTER.equals(c)) {
62            return HotSpotCompressedNullConstant.COMPRESSED_NULL;
63        } else if (c instanceof HotSpotConstant) {
64            return ((HotSpotConstant) c).compress();
65        } else {
66            throw GraalError.shouldNotReachHere("invalid constant input for compress op: " + c);
67        }
68    }
69
70    @Override
71    protected Constant uncompress(Constant c) {
72        if (c instanceof HotSpotConstant) {
73            return ((HotSpotConstant) c).uncompress();
74        } else {
75            throw GraalError.shouldNotReachHere("invalid constant input for uncompress op: " + c);
76        }
77    }
78
79    @Override
80    protected Stamp mkStamp(Stamp input) {
81        return HotSpotNarrowOopStamp.mkStamp(op, input, encoding);
82    }
83}
84