1 /*
2  * Copyright (c) 2016, 2018, 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.nodes;
26 
27 import org.graalvm.compiler.core.common.type.Stamp;
28 import org.graalvm.compiler.core.common.type.StampFactory;
29 import org.graalvm.compiler.core.common.type.TypeReference;
30 import org.graalvm.compiler.graph.Node;
31 import org.graalvm.compiler.graph.NodeClass;
32 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
33 import org.graalvm.compiler.nodeinfo.NodeInfo;
34 import org.graalvm.compiler.nodes.extended.GuardingNode;
35 
36 import jdk.vm.ci.meta.Assumptions;
37 import jdk.vm.ci.meta.ConstantReflectionProvider;
38 import jdk.vm.ci.meta.ResolvedJavaType;
39 
40 /**
41  * A {@link PiNode} where the type is not yet known. If the type becomes known at a later point in
42  * the compilation, this can canonicalize to a regular {@link PiNode}.
43  */
44 @NodeInfo
45 public final class DynamicPiNode extends PiNode {
46 
47     public static final NodeClass<DynamicPiNode> TYPE = NodeClass.create(DynamicPiNode.class);
48     @Input ValueNode typeMirror;
49     private final boolean exact;
50 
DynamicPiNode(ValueNode object, GuardingNode guard, ValueNode typeMirror, boolean exact)51     protected DynamicPiNode(ValueNode object, GuardingNode guard, ValueNode typeMirror, boolean exact) {
52         super(TYPE, object, StampFactory.object(), guard);
53         this.typeMirror = typeMirror;
54         this.exact = exact;
55     }
56 
create(Assumptions assumptions, ConstantReflectionProvider constantReflection, ValueNode object, GuardingNode guard, ValueNode typeMirror, boolean exact)57     public static ValueNode create(Assumptions assumptions, ConstantReflectionProvider constantReflection, ValueNode object, GuardingNode guard, ValueNode typeMirror, boolean exact) {
58         ValueNode synonym = findSynonym(assumptions, constantReflection, object, guard, typeMirror, exact);
59         if (synonym != null) {
60             return synonym;
61         }
62         return new DynamicPiNode(object, guard, typeMirror, exact);
63     }
64 
create(Assumptions assumptions, ConstantReflectionProvider constantReflection, ValueNode object, GuardingNode guard, ValueNode typeMirror)65     public static ValueNode create(Assumptions assumptions, ConstantReflectionProvider constantReflection, ValueNode object, GuardingNode guard, ValueNode typeMirror) {
66         return create(assumptions, constantReflection, object, guard, typeMirror, false);
67     }
68 
isExact()69     public boolean isExact() {
70         return exact;
71     }
72 
findSynonym(Assumptions assumptions, ConstantReflectionProvider constantReflection, ValueNode object, GuardingNode guard, ValueNode typeMirror, boolean exact)73     private static ValueNode findSynonym(Assumptions assumptions, ConstantReflectionProvider constantReflection, ValueNode object, GuardingNode guard, ValueNode typeMirror, boolean exact) {
74         if (typeMirror.isConstant()) {
75             ResolvedJavaType t = constantReflection.asJavaType(typeMirror.asConstant());
76             if (t != null) {
77                 Stamp staticPiStamp;
78                 if (t.isPrimitive()) {
79                     staticPiStamp = StampFactory.alwaysNull();
80                 } else {
81                     TypeReference type = exact ? TypeReference.createExactTrusted(t) : TypeReference.createTrusted(assumptions, t);
82                     staticPiStamp = StampFactory.object(type);
83                 }
84 
85                 return PiNode.create(object, staticPiStamp, (ValueNode) guard);
86             }
87         }
88 
89         return null;
90     }
91 
92     @Override
canonical(CanonicalizerTool tool)93     public Node canonical(CanonicalizerTool tool) {
94         ValueNode synonym = findSynonym(tool.getAssumptions(), tool.getConstantReflection(), object, guard, typeMirror, exact);
95         if (synonym != null) {
96             return synonym;
97         }
98         return this;
99     }
100 }
101