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