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