< prev index next >

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.hotspot.amd64/src/org/graalvm/compiler/hotspot/amd64/AMD64HotSpotAddressLowering.java

Print this page




  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 {


  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 }


  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 {


  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 }
< prev index next >