001/*
002 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.nodes.memory;
024
025import static jdk.internal.jvmci.meta.LocationIdentity.*;
026
027import java.util.*;
028
029import jdk.internal.jvmci.meta.*;
030
031import com.oracle.graal.compiler.common.*;
032import com.oracle.graal.compiler.common.type.*;
033import com.oracle.graal.graph.*;
034import com.oracle.graal.nodeinfo.*;
035import com.oracle.graal.nodes.*;
036import com.oracle.graal.nodes.calc.*;
037import com.oracle.graal.nodes.spi.*;
038
039@NodeInfo(allowedUsageTypes = {InputType.Extension, InputType.Memory})
040public final class MemoryMapNode extends FloatingNode implements MemoryMap, MemoryNode, LIRLowerable {
041
042    public static final NodeClass<MemoryMapNode> TYPE = NodeClass.create(MemoryMapNode.class);
043    protected final List<LocationIdentity> locationIdentities;
044    @Input(InputType.Memory) NodeInputList<ValueNode> nodes;
045
046    private boolean checkOrder(Map<LocationIdentity, MemoryNode> mmap) {
047        for (int i = 0; i < locationIdentities.size(); i++) {
048            LocationIdentity locationIdentity = locationIdentities.get(i);
049            ValueNode n = nodes.get(i);
050            assertTrue(mmap.get(locationIdentity) == n, "iteration order of keys differs from values in input map");
051        }
052        return true;
053    }
054
055    public MemoryMapNode(Map<LocationIdentity, MemoryNode> mmap) {
056        super(TYPE, StampFactory.forVoid());
057        locationIdentities = new ArrayList<>(mmap.keySet());
058        nodes = new NodeInputList<>(this, mmap.values());
059        assert checkOrder(mmap);
060    }
061
062    public boolean isEmpty() {
063        if (locationIdentities.isEmpty()) {
064            return true;
065        }
066        if (locationIdentities.size() == 1) {
067            if (nodes.get(0) instanceof StartNode) {
068                return true;
069            }
070        }
071        return false;
072    }
073
074    public MemoryNode getLastLocationAccess(LocationIdentity locationIdentity) {
075        if (locationIdentity.isImmutable()) {
076            return null;
077        } else {
078            int index = locationIdentities.indexOf(locationIdentity);
079            if (index == -1) {
080                index = locationIdentities.indexOf(any());
081            }
082            assert index != -1;
083            return (MemoryNode) nodes.get(index);
084        }
085    }
086
087    public Collection<LocationIdentity> getLocations() {
088        return locationIdentities;
089    }
090
091    public Map<LocationIdentity, MemoryNode> toMap() {
092        HashMap<LocationIdentity, MemoryNode> res = CollectionsFactory.newMap(locationIdentities.size());
093        for (int i = 0; i < nodes.size(); i++) {
094            res.put(locationIdentities.get(i), (MemoryNode) nodes.get(i));
095        }
096        return res;
097    }
098
099    public void generate(NodeLIRBuilderTool generator) {
100        // nothing to do...
101    }
102}