1 /* 2 * Copyright (c) 2014, 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.nodes.memory; 24 25 import static org.graalvm.compiler.core.common.LocationIdentity.any; 26 import static org.graalvm.compiler.nodeinfo.InputType.Extension; 27 import static org.graalvm.compiler.nodeinfo.InputType.Memory; 28 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_0; 29 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_0; 30 31 import java.util.ArrayList; 32 import java.util.Collection; 33 import java.util.HashMap; 34 import java.util.List; 35 import java.util.Map; 36 37 import org.graalvm.compiler.core.common.CollectionsFactory; 38 import org.graalvm.compiler.core.common.LocationIdentity; 39 import org.graalvm.compiler.core.common.type.StampFactory; 40 import org.graalvm.compiler.graph.NodeClass; 41 import org.graalvm.compiler.graph.NodeInputList; 42 import org.graalvm.compiler.nodeinfo.NodeInfo; 43 import org.graalvm.compiler.nodes.StartNode; 44 import org.graalvm.compiler.nodes.ValueNode; 45 import org.graalvm.compiler.nodes.calc.FloatingNode; 46 import org.graalvm.compiler.nodes.spi.LIRLowerable; 47 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool; 48 49 @NodeInfo(allowedUsageTypes = {Extension, Memory}, cycles = CYCLES_0, size = SIZE_0) 50 public final class MemoryMapNode extends FloatingNode implements MemoryMap, MemoryNode, LIRLowerable { 51 52 public static final NodeClass<MemoryMapNode> TYPE = NodeClass.create(MemoryMapNode.class); 53 protected final List<LocationIdentity> locationIdentities; 54 @Input(Memory) NodeInputList<ValueNode> nodes; 55 56 private boolean checkOrder(Map<LocationIdentity, MemoryNode> mmap) { 57 for (int i = 0; i < locationIdentities.size(); i++) { 58 LocationIdentity locationIdentity = locationIdentities.get(i); 59 ValueNode n = nodes.get(i); 60 assertTrue(mmap.get(locationIdentity) == n, "iteration order of keys differs from values in input map"); 61 } 62 return true; 63 } 64 65 public MemoryMapNode(Map<LocationIdentity, MemoryNode> mmap) { 66 super(TYPE, StampFactory.forVoid()); 67 locationIdentities = new ArrayList<>(mmap.keySet()); 68 nodes = new NodeInputList<>(this, mmap.values()); 69 assert checkOrder(mmap); 70 } 71 72 public boolean isEmpty() { 73 if (locationIdentities.isEmpty()) { 74 return true; 75 } 76 if (locationIdentities.size() == 1) { 77 if (nodes.get(0) instanceof StartNode) { 78 return true; 79 } 80 } 81 return false; 82 } 83 84 @Override 85 public MemoryNode getLastLocationAccess(LocationIdentity locationIdentity) { 86 if (locationIdentity.isImmutable()) { 87 return null; 88 } else { 89 int index = locationIdentities.indexOf(locationIdentity); 90 if (index == -1) { 91 index = locationIdentities.indexOf(any()); 92 } 93 assert index != -1; 94 return (MemoryNode) nodes.get(index); 95 } 96 } 97 98 @Override 99 public Collection<LocationIdentity> getLocations() { 100 return locationIdentities; 101 } 102 103 public Map<LocationIdentity, MemoryNode> toMap() { 104 HashMap<LocationIdentity, MemoryNode> res = CollectionsFactory.newMap(locationIdentities.size()); 105 for (int i = 0; i < nodes.size(); i++) { 106 res.put(locationIdentities.get(i), (MemoryNode) nodes.get(i)); 107 } 108 return res; 109 } 110 111 @Override 112 public void generate(NodeLIRBuilderTool generator) { 113 // nothing to do... 114 } 115 }