AddressLoweringByUsePhase.java revision 13175:3df8ef613001
1/*
2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
3 * Copyright (c) 2017, Red Hat Inc. All rights reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 */
24package org.graalvm.compiler.phases.common;
25
26import org.graalvm.compiler.core.common.type.Stamp;
27import org.graalvm.compiler.graph.Node;
28import org.graalvm.compiler.nodes.StructuredGraph;
29import org.graalvm.compiler.nodes.ValueNode;
30import org.graalvm.compiler.nodes.extended.JavaReadNode;
31import org.graalvm.compiler.nodes.memory.AbstractWriteNode;
32import org.graalvm.compiler.nodes.memory.FloatingReadNode;
33import org.graalvm.compiler.nodes.memory.ReadNode;
34import org.graalvm.compiler.nodes.memory.address.AddressNode;
35import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode;
36import org.graalvm.compiler.nodes.memory.address.RawAddressNode;
37import org.graalvm.compiler.nodes.util.GraphUtil;
38import org.graalvm.compiler.phases.Phase;
39
40/**
41 * Created by adinn on 09/05/17.
42 */
43public class AddressLoweringByUsePhase extends Phase {
44    public abstract static class AddressLoweringByUse {
45
46        public abstract AddressNode lower(ValueNode use, Stamp stamp, AddressNode address);
47
48        public abstract AddressNode lower(AddressNode address);
49    }
50
51    private final AddressLoweringByUse lowering;
52
53    public AddressLoweringByUsePhase(AddressLoweringByUse lowering) {
54        this.lowering = lowering;
55        assert lowering != null;
56    }
57
58    @Override
59    protected void run(StructuredGraph graph) {
60        // first replace address nodes hanging off known usages
61        for (Node node : graph.getNodes()) {
62            AddressNode address;
63            AddressNode lowered;
64            if (node instanceof ReadNode) {
65                ReadNode readNode = (ReadNode) node;
66                Stamp stamp = readNode.stamp();
67                address = readNode.getAddress();
68                lowered = lowering.lower(readNode, stamp, address);
69            } else if (node instanceof JavaReadNode) {
70                JavaReadNode javaReadNode = (JavaReadNode) node;
71                Stamp stamp = javaReadNode.stamp();
72                address = javaReadNode.getAddress();
73                lowered = lowering.lower(javaReadNode, stamp, address);
74            } else if (node instanceof FloatingReadNode) {
75                FloatingReadNode floatingReadNode = (FloatingReadNode) node;
76                Stamp stamp = floatingReadNode.stamp();
77                address = floatingReadNode.getAddress();
78                lowered = lowering.lower(floatingReadNode, stamp, address);
79            } else if (node instanceof AbstractWriteNode) {
80                AbstractWriteNode abstractWriteNode = (AbstractWriteNode) node;
81                Stamp stamp = abstractWriteNode.value().stamp();
82                address = abstractWriteNode.getAddress();
83                lowered = lowering.lower(abstractWriteNode, stamp, address);
84                // TODO -- PrefetchAllocateNode is not yet implemented for AArch64
85                // } else if (node instanceof PrefetchAllocateNode) {
86                // PrefetchAllocateNode prefetchAllocateNode = (PrefetchAllocateNode) node;
87                // Stamp stamp = prefetchAllocateNode.value().stamp();
88                // n.b.this getter is not provided!
89                // address = prefetchAllocateNode.getAddress();
90                // lowered = lowering.lower(prefetchAllocateNode, stamp, address);
91            } else {
92                continue;
93            }
94            // the lowered address amy already be a replacement
95            // in which case we want to use it not delete it!
96            if (lowered != address) {
97                // replace original with lowered at this usage only
98                // n.b. lowered is added unique so repeat lowerings will elide
99                node.replaceFirstInput(address, lowered);
100                // if that was the last reference we can kill the old (dead) node
101                if (address.hasNoUsages()) {
102                    GraphUtil.killWithUnusedFloatingInputs(address);
103                }
104            }
105        }
106
107        // now replace any remaining unlowered address nodes
108        for (Node node : graph.getNodes()) {
109            AddressNode lowered;
110            if (node instanceof RawAddressNode || node instanceof OffsetAddressNode) {
111                AddressNode address = (AddressNode) node;
112                lowered = lowering.lower(address);
113            } else {
114                continue;
115            }
116            // will always be a new AddresNode
117            node.replaceAtUsages(lowered);
118            GraphUtil.killWithUnusedFloatingInputs(node);
119        }
120    }
121}
122