1 /*
   2  * Copyright (c) 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 package org.graalvm.compiler.replacements.amd64;
  24 
  25 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_256;
  26 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_64;
  27 
  28 import org.graalvm.compiler.core.common.type.StampFactory;
  29 import org.graalvm.compiler.graph.NodeClass;
  30 import org.graalvm.compiler.graph.NodeInputList;
  31 import org.graalvm.compiler.nodeinfo.InputType;
  32 import org.graalvm.compiler.nodeinfo.NodeInfo;
  33 import org.graalvm.compiler.nodes.FixedWithNextNode;
  34 import org.graalvm.compiler.nodes.NamedLocationIdentity;
  35 import org.graalvm.compiler.nodes.ValueNode;
  36 import org.graalvm.compiler.nodes.ValueNodeUtil;
  37 import org.graalvm.compiler.nodes.memory.MemoryAccess;
  38 import org.graalvm.compiler.nodes.memory.MemoryNode;
  39 import org.graalvm.compiler.nodes.spi.LIRLowerable;
  40 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  41 import org.graalvm.word.LocationIdentity;
  42 import org.graalvm.word.Pointer;
  43 
  44 import jdk.vm.ci.meta.JavaKind;
  45 import jdk.vm.ci.meta.Value;
  46 
  47 @NodeInfo(size = SIZE_64, cycles = CYCLES_256)
  48 public class AMD64StringIndexOfNode extends FixedWithNextNode implements LIRLowerable, MemoryAccess {
  49     public static final NodeClass<AMD64StringIndexOfNode> TYPE = NodeClass.create(AMD64StringIndexOfNode.class);
  50 
  51     @OptionalInput(InputType.Memory) protected MemoryNode lastLocationAccess;
  52 
  53     @Input protected NodeInputList<ValueNode> arguments;
  54 
  55     public AMD64StringIndexOfNode(ValueNode sourcePointer, ValueNode sourceCount, ValueNode targetPointer, ValueNode targetCount) {
  56         super(TYPE, StampFactory.forInteger(32));
  57         this.arguments = new NodeInputList<>(this, new ValueNode[]{sourcePointer, sourceCount, targetPointer, targetCount});
  58     }
  59 
  60     @Override
  61     public LocationIdentity getLocationIdentity() {
  62         return NamedLocationIdentity.getArrayLocation(JavaKind.Char);
  63     }
  64 
  65     ValueNode sourcePointer() {
  66         return arguments.get(0);
  67     }
  68 
  69     ValueNode sourceCount() {
  70         return arguments.get(1);
  71     }
  72 
  73     ValueNode targetPointer() {
  74         return arguments.get(2);
  75     }
  76 
  77     ValueNode targetCount() {
  78         return arguments.get(3);
  79     }
  80 
  81     @Override
  82     public void generate(NodeLIRBuilderTool gen) {
  83         int constantTargetCount = -1;
  84         if (targetCount().isConstant()) {
  85             constantTargetCount = targetCount().asJavaConstant().asInt();
  86         }
  87         Value result = gen.getLIRGeneratorTool().emitStringIndexOf(gen.operand(sourcePointer()), gen.operand(sourceCount()), gen.operand(targetPointer()), gen.operand(targetCount()),
  88                         constantTargetCount);
  89         gen.setResult(this, result);
  90     }
  91 
  92     @Override
  93     public MemoryNode getLastLocationAccess() {
  94         return lastLocationAccess;
  95     }
  96 
  97     @Override
  98     public void setLastLocationAccess(MemoryNode lla) {
  99         updateUsages(ValueNodeUtil.asNode(lastLocationAccess), ValueNodeUtil.asNode(lla));
 100         lastLocationAccess = lla;
 101     }
 102 
 103     @NodeIntrinsic
 104     public static native int optimizedStringIndexPointer(Pointer sourcePointer, int sourceCount, Pointer targetPointer, int targetCount);
 105 }