MemoryMapNode.java revision 12651:6ef01bd40ce2
1187517Sgonzo/*
2187517Sgonzo * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
3187517Sgonzo * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4187517Sgonzo *
5187517Sgonzo * This code is free software; you can redistribute it and/or modify it
6187517Sgonzo * under the terms of the GNU General Public License version 2 only, as
7187517Sgonzo * published by the Free Software Foundation.
8187517Sgonzo *
9187517Sgonzo * This code is distributed in the hope that it will be useful, but WITHOUT
10187517Sgonzo * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11187517Sgonzo * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12187517Sgonzo * version 2 for more details (a copy is included in the LICENSE file that
13187517Sgonzo * accompanied this code).
14187517Sgonzo *
15187517Sgonzo * You should have received a copy of the GNU General Public License version
16187517Sgonzo * 2 along with this work; if not, write to the Free Software Foundation,
17187517Sgonzo * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18187517Sgonzo *
19187517Sgonzo * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20187517Sgonzo * or visit www.oracle.com if you need additional information or have any
21187517Sgonzo * questions.
22187517Sgonzo */
23187517Sgonzopackage org.graalvm.compiler.nodes.memory;
24187517Sgonzo
25187517Sgonzoimport static org.graalvm.compiler.core.common.LocationIdentity.any;
26187517Sgonzoimport static org.graalvm.compiler.nodeinfo.InputType.Extension;
27187517Sgonzoimport static org.graalvm.compiler.nodeinfo.InputType.Memory;
28187517Sgonzoimport static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_0;
29187517Sgonzoimport static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_0;
30187517Sgonzo
31187517Sgonzoimport java.util.ArrayList;
32187517Sgonzoimport java.util.Collection;
33187517Sgonzoimport java.util.HashMap;
34187517Sgonzoimport java.util.List;
35187517Sgonzoimport java.util.Map;
36187517Sgonzo
37187517Sgonzoimport org.graalvm.compiler.core.common.CollectionsFactory;
38187517Sgonzoimport org.graalvm.compiler.core.common.LocationIdentity;
39187517Sgonzoimport org.graalvm.compiler.core.common.type.StampFactory;
40187517Sgonzoimport org.graalvm.compiler.graph.NodeClass;
41199497Sgonzoimport org.graalvm.compiler.graph.NodeInputList;
42187517Sgonzoimport org.graalvm.compiler.nodeinfo.NodeInfo;
43187517Sgonzoimport org.graalvm.compiler.nodes.StartNode;
44187517Sgonzoimport org.graalvm.compiler.nodes.ValueNode;
45221257Sadrianimport org.graalvm.compiler.nodes.calc.FloatingNode;
46187517Sgonzoimport org.graalvm.compiler.nodes.spi.LIRLowerable;
47188883Sgonzoimport org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
48188883Sgonzo
49188883Sgonzo@NodeInfo(allowedUsageTypes = {Extension, Memory}, cycles = CYCLES_0, size = SIZE_0)
50188883Sgonzopublic final class MemoryMapNode extends FloatingNode implements MemoryMap, MemoryNode, LIRLowerable {
51188883Sgonzo
52188883Sgonzo    public static final NodeClass<MemoryMapNode> TYPE = NodeClass.create(MemoryMapNode.class);
53188883Sgonzo    protected final List<LocationIdentity> locationIdentities;
54187517Sgonzo    @Input(Memory) NodeInputList<ValueNode> nodes;
55187517Sgonzo
56212413Savg    private boolean checkOrder(Map<LocationIdentity, MemoryNode> mmap) {
57187517Sgonzo        for (int i = 0; i < locationIdentities.size(); i++) {
58187517Sgonzo            LocationIdentity locationIdentity = locationIdentities.get(i);
59187517Sgonzo            ValueNode n = nodes.get(i);
60187517Sgonzo            assertTrue(mmap.get(locationIdentity) == n, "iteration order of keys differs from values in input map");
61187517Sgonzo        }
62187517Sgonzo        return true;
63187517Sgonzo    }
64187517Sgonzo
65187517Sgonzo    public MemoryMapNode(Map<LocationIdentity, MemoryNode> mmap) {
66187517Sgonzo        super(TYPE, StampFactory.forVoid());
67187517Sgonzo        locationIdentities = new ArrayList<>(mmap.keySet());
68187517Sgonzo        nodes = new NodeInputList<>(this, mmap.values());
69187517Sgonzo        assert checkOrder(mmap);
70187517Sgonzo    }
71187517Sgonzo
72187517Sgonzo    public boolean isEmpty() {
73187517Sgonzo        if (locationIdentities.isEmpty()) {
74187517Sgonzo            return true;
75192822Sgonzo        }
76192822Sgonzo        if (locationIdentities.size() == 1) {
77187517Sgonzo            if (nodes.get(0) instanceof StartNode) {
78192822Sgonzo                return true;
79187517Sgonzo            }
80187517Sgonzo        }
81187517Sgonzo        return false;
82187517Sgonzo    }
83187517Sgonzo
84187517Sgonzo    @Override
85187517Sgonzo    public MemoryNode getLastLocationAccess(LocationIdentity locationIdentity) {
86192822Sgonzo        if (locationIdentity.isImmutable()) {
87192822Sgonzo            return null;
88187517Sgonzo        } else {
89187517Sgonzo            int index = locationIdentities.indexOf(locationIdentity);
90192822Sgonzo            if (index == -1) {
91187517Sgonzo                index = locationIdentities.indexOf(any());
92187517Sgonzo            }
93187517Sgonzo            assert index != -1;
94187517Sgonzo            return (MemoryNode) nodes.get(index);
95187517Sgonzo        }
96187517Sgonzo    }
97187517Sgonzo
98187517Sgonzo    @Override
99187517Sgonzo    public Collection<LocationIdentity> getLocations() {
100187517Sgonzo        return locationIdentities;
101187517Sgonzo    }
102187517Sgonzo
103187517Sgonzo    public Map<LocationIdentity, MemoryNode> toMap() {
104187517Sgonzo        HashMap<LocationIdentity, MemoryNode> res = CollectionsFactory.newMap(locationIdentities.size());
105187517Sgonzo        for (int i = 0; i < nodes.size(); i++) {
106187517Sgonzo            res.put(locationIdentities.get(i), (MemoryNode) nodes.get(i));
107187517Sgonzo        }
108187517Sgonzo        return res;
109187517Sgonzo    }
110191837Sgonzo
111191837Sgonzo    @Override
112191837Sgonzo    public void generate(NodeLIRBuilderTool generator) {
113191837Sgonzo        // nothing to do...
114191837Sgonzo    }
115191837Sgonzo}
116191837Sgonzo