|
58 | 58 | import jdk.graal.compiler.nodes.calc.CompareNode; |
59 | 59 | import jdk.graal.compiler.nodes.calc.FloatConvertNode; |
60 | 60 | import jdk.graal.compiler.nodes.calc.LeftShiftNode; |
| 61 | +import jdk.graal.compiler.nodes.extended.ValueAnchorNode; |
61 | 62 | import jdk.graal.compiler.nodes.graphbuilderconf.GraphBuilderContext; |
62 | 63 | import jdk.graal.compiler.nodes.graphbuilderconf.InvocationPlugin; |
63 | 64 | import jdk.graal.compiler.nodes.graphbuilderconf.InvocationPlugin.ConditionalInvocationPlugin; |
@@ -111,6 +112,7 @@ public static void register(Architecture architecture, InvocationPlugins plugins |
111 | 112 | registerFramePlugins(plugins); |
112 | 113 | registerBytecodePlugins(plugins); |
113 | 114 | registerCompilerDirectivesPlugins(plugins); |
| 115 | + registerDynamicObjectPlugins(plugins); |
114 | 116 | } |
115 | 117 |
|
116 | 118 | private static void registerFramePlugins(InvocationPlugins plugins) { |
@@ -830,4 +832,64 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec |
830 | 832 | } |
831 | 833 | }); |
832 | 834 | } |
| 835 | + |
| 836 | + private static void registerDynamicObjectPlugins(InvocationPlugins plugins) { |
| 837 | + plugins.registerIntrinsificationPredicate(t -> t.getName().equals("Lcom/oracle/truffle/api/object/UnsafeAccess;")); |
| 838 | + InvocationPlugins.Registration r = new InvocationPlugins.Registration(plugins, "com.oracle.truffle.api.object.UnsafeAccess"); |
| 839 | + r.register(new OptionalInlineOnlyInvocationPlugin("hostUnsafeCast", Object.class, Class.class, boolean.class, boolean.class, boolean.class) { |
| 840 | + @Override |
| 841 | + public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode object, ValueNode clazz, ValueNode condition, ValueNode nonNull, |
| 842 | + ValueNode isExactType) { |
| 843 | + if (!clazz.isConstant() || !nonNull.isConstant() || !isExactType.isConstant()) { |
| 844 | + b.push(JavaKind.Object, object); |
| 845 | + return true; |
| 846 | + } |
| 847 | + if (!Options.TruffleTrustedTypeCast.getValue(b.getOptions())) { |
| 848 | + b.push(JavaKind.Object, object); |
| 849 | + return true; |
| 850 | + } |
| 851 | + ConstantReflectionProvider constantReflection = b.getConstantReflection(); |
| 852 | + ResolvedJavaType javaType = constantReflection.asJavaType(clazz.asConstant()); |
| 853 | + if (javaType == null) { |
| 854 | + b.push(JavaKind.Object, object); |
| 855 | + return true; |
| 856 | + } |
| 857 | + |
| 858 | + TypeReference type; |
| 859 | + if (isExactType.asJavaConstant().asInt() != 0) { |
| 860 | + assert javaType.isConcrete() || javaType.isArray() : "exact type is not a concrete class: " + javaType; |
| 861 | + type = TypeReference.createExactTrusted(javaType); |
| 862 | + } else { |
| 863 | + type = TypeReference.createTrusted(b.getAssumptions(), javaType); |
| 864 | + } |
| 865 | + |
| 866 | + boolean trustedNonNull = nonNull.asJavaConstant().asInt() != 0 && Options.TruffleTrustedNonNullCast.getValue(b.getOptions()); |
| 867 | + Stamp piStamp = StampFactory.object(type, trustedNonNull); |
| 868 | + |
| 869 | + ValueNode valueAnchorNode; |
| 870 | + if (condition.isConstant()) { |
| 871 | + if (condition.asJavaConstant().asInt() == 1) { |
| 872 | + // Nothing to do. |
| 873 | + valueAnchorNode = null; |
| 874 | + } else { |
| 875 | + valueAnchorNode = b.add(new ValueAnchorNode()); |
| 876 | + } |
| 877 | + } else { |
| 878 | + LogicNode compareNode = CompareNode.createCompareNode(CanonicalCondition.EQ, condition, ConstantNode.forBoolean(true), constantReflection, NodeView.DEFAULT); |
| 879 | + if (compareNode instanceof LogicConstantNode logicConstantNode) { |
| 880 | + if (logicConstantNode.getValue()) { |
| 881 | + valueAnchorNode = null; |
| 882 | + } else { |
| 883 | + valueAnchorNode = b.add(new ValueAnchorNode()); |
| 884 | + } |
| 885 | + } else { |
| 886 | + valueAnchorNode = b.add(new ConditionAnchorNode(compareNode)); |
| 887 | + } |
| 888 | + } |
| 889 | + |
| 890 | + b.addPush(JavaKind.Object, PiNode.create(object, piStamp, valueAnchorNode)); |
| 891 | + return true; |
| 892 | + } |
| 893 | + }); |
| 894 | + } |
833 | 895 | } |
0 commit comments