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 */ 24 package org.graalvm.compiler.phases.common; 25 26 import jdk.vm.ci.meta.JavaKind; 27 import org.graalvm.compiler.core.common.type.Stamp; 28 import org.graalvm.compiler.core.common.type.StampFactory; 29 import org.graalvm.compiler.graph.Node; 30 import org.graalvm.compiler.nodes.PrefetchAllocateNode; 31 import org.graalvm.compiler.nodes.StructuredGraph; 32 import org.graalvm.compiler.nodes.ValueNode; 33 import org.graalvm.compiler.nodes.extended.JavaReadNode; 34 import org.graalvm.compiler.nodes.memory.AbstractWriteNode; 35 import org.graalvm.compiler.nodes.memory.FloatingReadNode; 36 import org.graalvm.compiler.nodes.memory.ReadNode; 37 import org.graalvm.compiler.nodes.memory.address.AddressNode; 38 import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode; 39 import org.graalvm.compiler.nodes.memory.address.RawAddressNode; 40 import org.graalvm.compiler.nodes.util.GraphUtil; 41 import org.graalvm.compiler.phases.Phase; 42 43 /** 44 * Created by adinn on 09/05/17. 45 */ 46 public class AddressLoweringByUsePhase extends Phase { 47 public abstract static class AddressLoweringByUse { 48 49 public abstract AddressNode lower(ValueNode use, Stamp stamp, AddressNode address); 50 51 public abstract AddressNode lower(AddressNode address); 52 } 53 54 private final AddressLoweringByUse lowering; 55 56 public AddressLoweringByUsePhase(AddressLoweringByUse lowering) { 57 this.lowering = lowering; 58 assert lowering != null; 59 } 60 61 @Override 62 protected void run(StructuredGraph graph) { 63 // first replace address nodes hanging off known usages 64 for (Node node : graph.getNodes()) { 65 AddressNode address; 66 AddressNode lowered; 67 if (node instanceof ReadNode) { 68 ReadNode readNode = (ReadNode) node; 69 Stamp stamp = readNode.stamp(); 70 address = readNode.getAddress(); 71 lowered = lowering.lower(readNode, stamp, address); 72 } else if (node instanceof JavaReadNode) { 73 JavaReadNode javaReadNode = (JavaReadNode) node; 74 Stamp stamp = javaReadNode.stamp(); 75 address = javaReadNode.getAddress(); 76 lowered = lowering.lower(javaReadNode, stamp, address); 77 } else if (node instanceof FloatingReadNode) { 78 FloatingReadNode floatingReadNode = (FloatingReadNode) node; 79 Stamp stamp = floatingReadNode.stamp(); 80 address = floatingReadNode.getAddress(); 81 lowered = lowering.lower(floatingReadNode, stamp, address); 82 } else if (node instanceof AbstractWriteNode) { 83 AbstractWriteNode abstractWriteNode = (AbstractWriteNode) node; 84 Stamp stamp = abstractWriteNode.value().stamp(); 85 address = abstractWriteNode.getAddress(); 86 lowered = lowering.lower(abstractWriteNode, stamp, address); 87 } else if (node instanceof PrefetchAllocateNode) { 88 PrefetchAllocateNode prefetchAllocateNode = (PrefetchAllocateNode) node; 89 Stamp stamp = StampFactory.forKind(JavaKind.Object); 90 address = (AddressNode) prefetchAllocateNode.inputs().first(); 91 lowered = lowering.lower(prefetchAllocateNode, stamp, address); 92 } else { 93 continue; 94 } 95 // the lowered address amy already be a replacement 96 // in which case we want to use it not delete it! 97 if (lowered != address) { 98 // replace original with lowered at this usage only 99 // n.b. lowered is added unique so repeat lowerings will elide 100 node.replaceFirstInput(address, lowered); 101 // if that was the last reference we can kill the old (dead) node 102 if (address.hasNoUsages()) { 103 GraphUtil.killWithUnusedFloatingInputs(address); 104 } 105 } 106 } 107 108 // now replace any remaining unlowered address nodes 109 for (Node node : graph.getNodes()) { 110 AddressNode lowered; 111 if (node instanceof RawAddressNode || node instanceof OffsetAddressNode) { 112 AddressNode address = (AddressNode) node; 113 lowered = lowering.lower(address); 114 } else { 115 continue; 116 } 117 // will always be a new AddresNode 118 node.replaceAtUsages(lowered); 119 GraphUtil.killWithUnusedFloatingInputs(node); 120 } 121 } 122 }