1/*
2 * Copyright (c) 2012, 2015, 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.replacements.nodes;
24
25import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_0;
26import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_0;
27
28import org.graalvm.compiler.core.common.LIRKind;
29import org.graalvm.compiler.core.common.type.Stamp;
30import org.graalvm.compiler.core.common.type.StampFactory;
31import org.graalvm.compiler.graph.NodeClass;
32import org.graalvm.compiler.nodeinfo.NodeInfo;
33import org.graalvm.compiler.nodeinfo.Verbosity;
34import org.graalvm.compiler.nodes.FixedWithNextNode;
35import org.graalvm.compiler.nodes.spi.LIRLowerable;
36import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
37
38import jdk.vm.ci.code.Register;
39import jdk.vm.ci.meta.JavaKind;
40import jdk.vm.ci.meta.Value;
41
42/**
43 * Access the value of a specific register.
44 */
45@NodeInfo(nameTemplate = "ReadRegister %{p#register}", cycles = CYCLES_0, size = SIZE_0)
46public final class ReadRegisterNode extends FixedWithNextNode implements LIRLowerable {
47
48    public static final NodeClass<ReadRegisterNode> TYPE = NodeClass.create(ReadRegisterNode.class);
49    /**
50     * The fixed register to access.
51     */
52    protected final Register register;
53
54    /**
55     * When true, subsequent uses of this node use the fixed register; when false, the value is
56     * moved into a new virtual register so that the fixed register is not seen by uses.
57     */
58    protected final boolean directUse;
59
60    /**
61     * When true, this node is also an implicit definition of the value for the register allocator,
62     * i.e., the register is an implicit incoming value; when false, the register must be defined in
63     * the same method or must be an register excluded from register allocation.
64     */
65    protected final boolean incoming;
66
67    public ReadRegisterNode(Register register, JavaKind kind, boolean directUse, boolean incoming) {
68        super(TYPE, StampFactory.forKind(kind));
69        assert register != null;
70        this.register = register;
71        this.directUse = directUse;
72        this.incoming = incoming;
73    }
74
75    public ReadRegisterNode(@InjectedNodeParameter Stamp stamp, Register register, boolean directUse, boolean incoming) {
76        super(TYPE, stamp);
77        assert register != null;
78        this.register = register;
79        this.directUse = directUse;
80        this.incoming = incoming;
81    }
82
83    @Override
84    public void generate(NodeLIRBuilderTool generator) {
85        LIRKind kind = generator.getLIRGeneratorTool().getLIRKind(stamp());
86        Value result = register.asValue(kind);
87        if (incoming) {
88            generator.getLIRGeneratorTool().emitIncomingValues(new Value[]{result});
89        }
90        if (!directUse) {
91            result = generator.getLIRGeneratorTool().emitMove(result);
92        }
93        generator.setResult(this, result);
94    }
95
96    @Override
97    public String toString(Verbosity verbosity) {
98        if (verbosity == Verbosity.Name) {
99            return super.toString(Verbosity.Name) + "%" + register;
100        } else {
101            return super.toString(verbosity);
102        }
103    }
104}
105