1 /* 2 * Copyright (c) 2015, 2017, 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 24 package org.graalvm.compiler.hotspot.amd64; 25 26 import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC; 27 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_0; 28 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_0; 29 30 import org.graalvm.compiler.asm.amd64.AMD64Address.Scale; 31 import org.graalvm.compiler.core.amd64.AMD64AddressLowering; 32 import org.graalvm.compiler.core.amd64.AMD64AddressNode; 33 import org.graalvm.compiler.core.common.CompressEncoding; 34 import org.graalvm.compiler.core.common.LIRKind; 35 import org.graalvm.compiler.core.common.NumUtil; 36 import org.graalvm.compiler.core.common.type.ObjectStamp; 37 import org.graalvm.compiler.core.common.type.StampFactory; 38 import org.graalvm.compiler.debug.CounterKey; 39 import org.graalvm.compiler.debug.DebugContext; 40 import org.graalvm.compiler.graph.NodeClass; 41 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig; 42 import org.graalvm.compiler.hotspot.nodes.GraalHotSpotVMConfigNode; 43 import org.graalvm.compiler.hotspot.nodes.type.KlassPointerStamp; 44 import org.graalvm.compiler.nodeinfo.NodeInfo; 45 import org.graalvm.compiler.nodes.CompressionNode; 46 import org.graalvm.compiler.nodes.CompressionNode.CompressionOp; 47 import org.graalvm.compiler.nodes.ValueNode; 48 import org.graalvm.compiler.nodes.calc.FloatingNode; 49 import org.graalvm.compiler.nodes.spi.LIRLowerable; 50 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool; 51 import org.graalvm.compiler.options.OptionValues; 52 53 import jdk.vm.ci.code.Register; 54 import jdk.vm.ci.meta.JavaKind; 55 56 public class AMD64HotSpotAddressLowering extends AMD64AddressLowering { 57 58 private static final CounterKey counterFoldedUncompressDuringAddressLowering = DebugContext.counter("FoldedUncompressDuringAddressLowering"); 59 60 private final long heapBase; 61 private final Register heapBaseRegister; 62 private final GraalHotSpotVMConfig config; 63 private final boolean generatePIC; 64 65 @NodeInfo(cycles = CYCLES_0, size = SIZE_0) 66 public static class HeapBaseNode extends FloatingNode implements LIRLowerable { 67 68 public static final NodeClass<HeapBaseNode> TYPE = NodeClass.create(HeapBaseNode.class); 69 70 private final Register heapBaseRegister; 71 72 public HeapBaseNode(Register heapBaseRegister) { 73 super(TYPE, StampFactory.pointer()); 74 this.heapBaseRegister = heapBaseRegister; 75 } 76 77 @Override 78 public void generate(NodeLIRBuilderTool generator) { 79 LIRKind kind = generator.getLIRGeneratorTool().getLIRKind(stamp()); 80 generator.setResult(this, heapBaseRegister.asValue(kind)); 81 } 82 } 83 84 public AMD64HotSpotAddressLowering(GraalHotSpotVMConfig config, Register heapBaseRegister, OptionValues options) { 85 this.heapBase = config.getOopEncoding().getBase(); 86 this.config = config; 87 this.generatePIC = GeneratePIC.getValue(options); 88 if (heapBase == 0 && !generatePIC) { 89 this.heapBaseRegister = null; 90 } else { 91 this.heapBaseRegister = heapBaseRegister; 92 } 93 } 94 95 @Override 96 protected boolean improve(DebugContext debug, AMD64AddressNode addr) { 97 98 boolean result = false; 99 100 while (super.improve(debug, addr)) { 101 result = true; 102 } 103 104 if (addr.getScale() == Scale.Times1) { 105 if (addr.getIndex() instanceof CompressionNode) { 106 if (improveUncompression(addr, (CompressionNode) addr.getIndex(), addr.getBase())) { 107 counterFoldedUncompressDuringAddressLowering.increment(debug); 108 return true; 109 } 110 } 111 112 if (addr.getBase() instanceof CompressionNode) { 113 if (improveUncompression(addr, (CompressionNode) addr.getBase(), addr.getIndex())) { 114 counterFoldedUncompressDuringAddressLowering.increment(debug); 115 return true; 116 } 117 } 118 } 119 120 return result; 121 } 122 123 private boolean improveUncompression(AMD64AddressNode addr, CompressionNode compression, ValueNode other) { 124 if (compression.getOp() == CompressionOp.Uncompress) { 125 CompressEncoding encoding = compression.getEncoding(); 126 Scale scale = Scale.fromShift(encoding.getShift()); 127 if (scale == null) { 128 return false; 129 } 130 131 if (heapBaseRegister != null && encoding.getBase() == heapBase) { 132 if ((!generatePIC || compression.stamp() instanceof ObjectStamp) && other == null) { 133 // With PIC it is only legal to do for oops since the base value may be 134 // different at runtime. 135 ValueNode base = compression.graph().unique(new HeapBaseNode(heapBaseRegister)); 136 addr.setBase(base); 137 } else { 138 return false; 139 } 140 } else if (encoding.getBase() != 0 || (generatePIC && compression.stamp() instanceof KlassPointerStamp)) { 141 if (generatePIC) { 142 if (other == null) { 143 ValueNode base = compression.graph().unique(new GraalHotSpotVMConfigNode(config, config.MARKID_NARROW_KLASS_BASE_ADDRESS, JavaKind.Long)); 144 addr.setBase(base); 145 } else { 146 return false; 147 } 148 } else { 149 long disp = addr.getDisplacement() + encoding.getBase(); 150 if (NumUtil.isInt(disp)) { 151 addr.setDisplacement((int) disp); 152 addr.setBase(other); 153 } else { 154 return false; 155 } 156 } 157 } else { 158 addr.setBase(other); 159 } 160 161 addr.setScale(scale); 162 addr.setIndex(compression.getValue()); 163 return true; 164 } else { 165 return false; 166 } 167 } 168 }