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