1 /*
   2  * Copyright (c) 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 package org.graalvm.compiler.nodes;
  24 
  25 import org.graalvm.compiler.core.common.type.Stamp;
  26 import org.graalvm.compiler.core.common.type.StampFactory;
  27 import org.graalvm.compiler.core.common.type.TypeReference;
  28 import org.graalvm.compiler.graph.Node;
  29 import org.graalvm.compiler.graph.NodeClass;
  30 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  31 import org.graalvm.compiler.nodeinfo.NodeInfo;
  32 import org.graalvm.compiler.nodes.extended.GuardingNode;
  33 
  34 import jdk.vm.ci.meta.ResolvedJavaType;
  35 
  36 /**
  37  * A {@link PiNode} where the type is not yet known. If the type becomes known at a later point in
  38  * the compilation, this can canonicalize to a regular {@link PiNode}.
  39  */
  40 @NodeInfo
  41 public final class DynamicPiNode extends PiNode {
  42 
  43     public static final NodeClass<DynamicPiNode> TYPE = NodeClass.create(DynamicPiNode.class);
  44     @Input ValueNode typeMirror;
  45 
  46     public DynamicPiNode(ValueNode object, GuardingNode guard, ValueNode typeMirror) {
  47         super(TYPE, object, StampFactory.object(), guard);
  48         this.typeMirror = typeMirror;
  49     }
  50 
  51     @Override
  52     public Node canonical(CanonicalizerTool tool) {
  53         if (typeMirror.isConstant()) {
  54             ResolvedJavaType t = tool.getConstantReflection().asJavaType(typeMirror.asConstant());
  55             if (t != null) {
  56                 Stamp staticPiStamp;
  57                 if (t.isPrimitive()) {
  58                     staticPiStamp = StampFactory.alwaysNull();
  59                 } else {
  60                     TypeReference type = TypeReference.createTrusted(tool.getAssumptions(), t);
  61                     staticPiStamp = StampFactory.object(type);
  62                 }
  63 
  64                 return new PiNode(object(), staticPiStamp, (ValueNode) getGuard()).canonical(tool);
  65             }
  66         }
  67 
  68         return this;
  69     }
  70 }