1 /*
   2  * Copyright (c) 2014, 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 
  25 package org.graalvm.compiler.hotspot.nodes.type;
  26 
  27 import org.graalvm.compiler.core.common.CompressEncoding;
  28 import org.graalvm.compiler.core.common.type.AbstractObjectStamp;
  29 import org.graalvm.compiler.core.common.type.ObjectStamp;
  30 import org.graalvm.compiler.core.common.type.Stamp;
  31 import org.graalvm.compiler.debug.GraalError;
  32 import org.graalvm.compiler.nodes.CompressionNode.CompressionOp;
  33 import org.graalvm.compiler.nodes.type.NarrowOopStamp;
  34 
  35 import jdk.vm.ci.hotspot.HotSpotCompressedNullConstant;
  36 import jdk.vm.ci.hotspot.HotSpotMemoryAccessProvider;
  37 import jdk.vm.ci.hotspot.HotSpotObjectConstant;
  38 import jdk.vm.ci.meta.Constant;
  39 import jdk.vm.ci.meta.JavaConstant;
  40 import jdk.vm.ci.meta.MemoryAccessProvider;
  41 import jdk.vm.ci.meta.ResolvedJavaType;
  42 
  43 public final class HotSpotNarrowOopStamp extends NarrowOopStamp {
  44     private HotSpotNarrowOopStamp(ResolvedJavaType type, boolean exactType, boolean nonNull, boolean alwaysNull, CompressEncoding encoding) {
  45         super(type, exactType, nonNull, alwaysNull, encoding);
  46     }
  47 
  48     @Override
  49     protected AbstractObjectStamp copyWith(ResolvedJavaType type, boolean exactType, boolean nonNull, boolean alwaysNull) {
  50         return new HotSpotNarrowOopStamp(type, exactType, nonNull, alwaysNull, getEncoding());
  51     }
  52 
  53     public static Stamp compressed(AbstractObjectStamp stamp, CompressEncoding encoding) {
  54         return new HotSpotNarrowOopStamp(stamp.type(), stamp.isExactType(), stamp.nonNull(), stamp.alwaysNull(), encoding);
  55     }
  56 
  57     @Override
  58     public Constant readConstant(MemoryAccessProvider provider, Constant base, long displacement) {
  59         try {
  60             HotSpotMemoryAccessProvider hsProvider = (HotSpotMemoryAccessProvider) provider;
  61             return hsProvider.readNarrowOopConstant(base, displacement);
  62         } catch (IllegalArgumentException e) {
  63             return null;
  64         }
  65     }
  66 
  67     @Override
  68     public JavaConstant nullConstant() {
  69         return HotSpotCompressedNullConstant.COMPRESSED_NULL;
  70     }
  71 
  72     @Override
  73     public boolean isCompatible(Constant other) {
  74         if (other instanceof HotSpotObjectConstant) {
  75             return ((HotSpotObjectConstant) other).isCompressed();
  76         }
  77         return true;
  78     }
  79 
  80     public static Stamp mkStamp(CompressionOp op, Stamp input, CompressEncoding encoding) {
  81         switch (op) {
  82             case Compress:
  83                 if (input instanceof ObjectStamp) {
  84                     // compressed oop
  85                     return HotSpotNarrowOopStamp.compressed((ObjectStamp) input, encoding);
  86                 } else if (input instanceof KlassPointerStamp) {
  87                     // compressed klass pointer
  88                     return ((KlassPointerStamp) input).compressed(encoding);
  89                 }
  90                 break;
  91             case Uncompress:
  92                 if (input instanceof NarrowOopStamp) {
  93                     // oop
  94                     assert encoding.equals(((NarrowOopStamp) input).getEncoding());
  95                     return ((NarrowOopStamp) input).uncompressed();
  96                 } else if (input instanceof KlassPointerStamp) {
  97                     // metaspace pointer
  98                     assert encoding.equals(((KlassPointerStamp) input).getEncoding());
  99                     return ((KlassPointerStamp) input).uncompressed();
 100                 }
 101                 break;
 102         }
 103         throw GraalError.shouldNotReachHere(String.format("Unexpected input stamp %s", input));
 104     }
 105 
 106 }