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.type.ObjectStamp;
  36 import org.graalvm.compiler.core.common.type.StampFactory;
  37 import org.graalvm.compiler.debug.CounterKey;
  38 import org.graalvm.compiler.debug.DebugContext;
  39 import org.graalvm.compiler.graph.NodeClass;
  40 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
  41 import org.graalvm.compiler.hotspot.nodes.GraalHotSpotVMConfigNode;
  42 import org.graalvm.compiler.hotspot.nodes.type.KlassPointerStamp;
  43 import org.graalvm.compiler.nodeinfo.NodeInfo;
  44 import org.graalvm.compiler.nodes.CompressionNode;
  45 import org.graalvm.compiler.nodes.CompressionNode.CompressionOp;
  46 import org.graalvm.compiler.nodes.StructuredGraph;
  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(StructuredGraph graph, DebugContext debug, AMD64AddressNode addr, boolean isBaseNegated, boolean isIndexNegated) {
  97         if (super.improve(graph, debug, addr, isBaseNegated, isIndexNegated)) {
  98             return true;
  99         }
 100 
 101         if (addr.getScale() == Scale.Times1) {
 102             if (addr.getIndex() instanceof CompressionNode) {
 103                 if (improveUncompression(addr, (CompressionNode) addr.getIndex(), addr.getBase(), isBaseNegated, isIndexNegated)) {
 104                     counterFoldedUncompressDuringAddressLowering.increment(debug);
 105                     return true;
 106                 }
 107             }
 108 
 109             if (addr.getBase() instanceof CompressionNode) {
 110                 if (improveUncompression(addr, (CompressionNode) addr.getBase(), addr.getIndex(), isBaseNegated, isIndexNegated)) {
 111                     counterFoldedUncompressDuringAddressLowering.increment(debug);
 112                     return true;
 113                 }
 114             }
 115         }
 116 
 117         return false;
 118     }
 119 
 120     @Override
 121     protected boolean mightBeOptimized(ValueNode value) {
 122         return super.mightBeOptimized(value) || value instanceof CompressionNode;
 123     }
 124 
 125     private boolean improveUncompression(AMD64AddressNode addr, CompressionNode compression, ValueNode other, boolean isBaseNegated, boolean isIndexNegated) {
 126         if (isBaseNegated || isIndexNegated || compression.getOp() != CompressionOp.Uncompress) {
 127             return false;
 128         }
 129 
 130         CompressEncoding encoding = compression.getEncoding();
 131         Scale scale = Scale.fromShift(encoding.getShift());
 132         if (scale == null) {
 133             return false;
 134         }
 135 
 136         if (heapBaseRegister != null && encoding.getBase() == heapBase) {
 137             if ((!generatePIC || compression.stamp() instanceof ObjectStamp) && other == null) {
 138                 // With PIC it is only legal to do for oops since the base value may be
 139                 // different at runtime.
 140                 ValueNode base = compression.graph().unique(new HeapBaseNode(heapBaseRegister));
 141                 addr.setBase(base);
 142             } else {
 143                 return false;
 144             }
 145         } else if (encoding.getBase() != 0 || (generatePIC && compression.stamp() instanceof KlassPointerStamp)) {
 146             if (generatePIC) {
 147                 if (other == null) {
 148                     ValueNode base = compression.graph().unique(new GraalHotSpotVMConfigNode(config, config.MARKID_NARROW_KLASS_BASE_ADDRESS, JavaKind.Long));
 149                     addr.setBase(base);
 150                 } else {
 151                     return false;
 152                 }
 153             } else {
 154                 if (updateDisplacement(addr, encoding.getBase(), isBaseNegated)) {
 155                     addr.setBase(other);
 156                 } else {
 157                     return false;
 158                 }
 159             }
 160         } else {
 161             addr.setBase(other);
 162         }
 163 
 164         addr.setScale(scale);
 165         addr.setIndex(compression.getValue());
 166         return true;
 167     }
 168 }