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