< prev index next >

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.replacements/src/org/graalvm/compiler/replacements/IntrinsicGraphBuilder.java

Print this page




  34 import org.graalvm.compiler.debug.DebugCloseable;
  35 import org.graalvm.compiler.debug.DebugContext;
  36 import org.graalvm.compiler.debug.GraalError;
  37 import org.graalvm.compiler.graph.NodeSourcePosition;
  38 import org.graalvm.compiler.nodes.CallTargetNode;
  39 import org.graalvm.compiler.nodes.CallTargetNode.InvokeKind;
  40 import org.graalvm.compiler.nodes.FixedNode;
  41 import org.graalvm.compiler.nodes.FixedWithNextNode;
  42 import org.graalvm.compiler.nodes.FrameState;
  43 import org.graalvm.compiler.nodes.Invoke;
  44 import org.graalvm.compiler.nodes.ParameterNode;
  45 import org.graalvm.compiler.nodes.ReturnNode;
  46 import org.graalvm.compiler.nodes.StateSplit;
  47 import org.graalvm.compiler.nodes.StructuredGraph;
  48 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  49 import org.graalvm.compiler.nodes.ValueNode;
  50 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
  51 import org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext;
  52 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
  53 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver;


  54 import org.graalvm.compiler.nodes.spi.StampProvider;
  55 import org.graalvm.compiler.options.OptionValues;
  56 
  57 import jdk.vm.ci.code.BailoutException;
  58 import jdk.vm.ci.code.BytecodeFrame;
  59 import jdk.vm.ci.meta.ConstantReflectionProvider;
  60 import jdk.vm.ci.meta.JavaKind;
  61 import jdk.vm.ci.meta.JavaType;
  62 import jdk.vm.ci.meta.MetaAccessProvider;
  63 import jdk.vm.ci.meta.ResolvedJavaMethod;
  64 import jdk.vm.ci.meta.ResolvedJavaType;
  65 import jdk.vm.ci.meta.Signature;
  66 
  67 /**
  68  * Implementation of {@link GraphBuilderContext} used to produce a graph for a method based on an
  69  * {@link InvocationPlugin} for the method.
  70  */
  71 public class IntrinsicGraphBuilder implements GraphBuilderContext, Receiver {
  72 
  73     protected final MetaAccessProvider metaAccess;
  74     protected final ConstantReflectionProvider constantReflection;
  75     protected final ConstantFieldProvider constantFieldProvider;
  76     protected final StampProvider stampProvider;
  77     protected final StructuredGraph graph;
  78     protected final Bytecode code;
  79     protected final ResolvedJavaMethod method;
  80     protected final int invokeBci;
  81     protected FixedWithNextNode lastInstr;
  82     protected ValueNode[] arguments;
  83     protected ValueNode returnValue;
  84 
  85     public IntrinsicGraphBuilder(OptionValues options, DebugContext debug, MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection, ConstantFieldProvider constantFieldProvider,
  86                     StampProvider stampProvider, Bytecode code, int invokeBci) {
  87         this(options, debug, metaAccess, constantReflection, constantFieldProvider, stampProvider, code, invokeBci, AllowAssumptions.YES);
  88     }
  89 
  90     protected IntrinsicGraphBuilder(OptionValues options, DebugContext debug, MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection, ConstantFieldProvider constantFieldProvider,
  91                     StampProvider stampProvider, Bytecode code, int invokeBci, AllowAssumptions allowAssumptions) {
  92         this.metaAccess = metaAccess;
  93         this.constantReflection = constantReflection;
  94         this.constantFieldProvider = constantFieldProvider;
  95         this.stampProvider = stampProvider;
  96         this.code = code;
  97         this.method = code.getMethod();
  98         this.graph = new StructuredGraph.Builder(options, debug, allowAssumptions).method(method).setIsSubstitution(true).trackNodeSourcePosition(true).build();
  99         this.invokeBci = invokeBci;
 100         this.lastInstr = graph.start();
 101 
 102         Signature sig = method.getSignature();
 103         int max = sig.getParameterCount(false);
 104         this.arguments = new ValueNode[max + (method.isStatic() ? 0 : 1)];
 105 
 106         int javaIndex = 0;
 107         int index = 0;
 108         if (!method.isStatic()) {
 109             // add the receiver
 110             Stamp receiverStamp = StampFactory.objectNonNull(TypeReference.createWithoutAssumptions(method.getDeclaringClass()));
 111             ValueNode receiver = graph.addWithoutUnique(new ParameterNode(javaIndex, StampPair.createSingle(receiverStamp)));
 112             arguments[index] = receiver;
 113             javaIndex = 1;
 114             index = 1;
 115         }
 116         ResolvedJavaType accessingClass = method.getDeclaringClass();
 117         for (int i = 0; i < max; i++) {
 118             JavaType type = sig.getParameterType(i, accessingClass).resolve(accessingClass);
 119             JavaKind kind = type.getJavaKind();
 120             Stamp stamp;
 121             if (kind == JavaKind.Object && type instanceof ResolvedJavaType) {
 122                 stamp = StampFactory.object(TypeReference.createWithoutAssumptions((ResolvedJavaType) type));
 123             } else {
 124                 stamp = StampFactory.forKind(kind);
 125             }
 126             ValueNode param = graph.addWithoutUnique(new ParameterNode(index, StampPair.createSingle(stamp)));
 127             arguments[index] = param;
 128             javaIndex += kind.getSlotCount();
 129             index++;
 130         }
 131     }
 132 
 133     private <T extends ValueNode> void updateLastInstruction(T v) {
 134         if (v instanceof FixedNode) {
 135             FixedNode fixedNode = (FixedNode) v;
 136             lastInstr.setNext(fixedNode);


 137             if (fixedNode instanceof FixedWithNextNode) {
 138                 FixedWithNextNode fixedWithNextNode = (FixedWithNextNode) fixedNode;
 139                 assert fixedWithNextNode.next() == null : "cannot append instruction to instruction which isn't end";
 140                 lastInstr = fixedWithNextNode;
 141             } else {
 142                 lastInstr = null;
 143             }
 144         }
 145     }
 146 
 147     @Override
 148     public <T extends ValueNode> T append(T v) {
 149         if (v.graph() != null) {
 150             return v;
 151         }
 152         T added = graph.addOrUniqueWithInputs(v);
 153         if (added == v) {
 154             updateLastInstruction(v);
 155         }
 156         return added;


 158 
 159     @Override
 160     public void push(JavaKind kind, ValueNode value) {
 161         assert kind != JavaKind.Void;
 162         assert returnValue == null;
 163         returnValue = value;
 164     }
 165 
 166     @Override
 167     public Invoke handleReplacedInvoke(InvokeKind invokeKind, ResolvedJavaMethod targetMethod, ValueNode[] args, boolean forceInlineEverything) {
 168         throw GraalError.shouldNotReachHere();
 169     }
 170 
 171     @Override
 172     public void handleReplacedInvoke(CallTargetNode callTarget, JavaKind resultType) {
 173         throw GraalError.shouldNotReachHere();
 174     }
 175 
 176     @Override
 177     public StampProvider getStampProvider() {
 178         return stampProvider;
 179     }
 180 
 181     @Override
 182     public MetaAccessProvider getMetaAccess() {
 183         return metaAccess;
 184     }
 185 
 186     @Override
 187     public ConstantReflectionProvider getConstantReflection() {
 188         return constantReflection;
 189     }
 190 
 191     @Override
 192     public ConstantFieldProvider getConstantFieldProvider() {
 193         return constantFieldProvider;





 194     }
 195 
 196     @Override
 197     public StructuredGraph getGraph() {
 198         return graph;
 199     }
 200 
 201     @Override
 202     public void setStateAfter(StateSplit sideEffect) {
 203         assert sideEffect.hasSideEffect();
 204         FrameState stateAfter = getGraph().add(new FrameState(BytecodeFrame.BEFORE_BCI));
 205         sideEffect.setStateAfter(stateAfter);
 206     }
 207 
 208     @Override
 209     public GraphBuilderContext getParent() {
 210         return null;
 211     }
 212 
 213     @Override




  34 import org.graalvm.compiler.debug.DebugCloseable;
  35 import org.graalvm.compiler.debug.DebugContext;
  36 import org.graalvm.compiler.debug.GraalError;
  37 import org.graalvm.compiler.graph.NodeSourcePosition;
  38 import org.graalvm.compiler.nodes.CallTargetNode;
  39 import org.graalvm.compiler.nodes.CallTargetNode.InvokeKind;
  40 import org.graalvm.compiler.nodes.FixedNode;
  41 import org.graalvm.compiler.nodes.FixedWithNextNode;
  42 import org.graalvm.compiler.nodes.FrameState;
  43 import org.graalvm.compiler.nodes.Invoke;
  44 import org.graalvm.compiler.nodes.ParameterNode;
  45 import org.graalvm.compiler.nodes.ReturnNode;
  46 import org.graalvm.compiler.nodes.StateSplit;
  47 import org.graalvm.compiler.nodes.StructuredGraph;
  48 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  49 import org.graalvm.compiler.nodes.ValueNode;
  50 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
  51 import org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext;
  52 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
  53 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver;
  54 import org.graalvm.compiler.nodes.spi.CoreProviders;
  55 import org.graalvm.compiler.nodes.spi.Replacements;
  56 import org.graalvm.compiler.nodes.spi.StampProvider;
  57 import org.graalvm.compiler.options.OptionValues;
  58 
  59 import jdk.vm.ci.code.BailoutException;
  60 import jdk.vm.ci.code.BytecodeFrame;
  61 import jdk.vm.ci.meta.ConstantReflectionProvider;
  62 import jdk.vm.ci.meta.JavaKind;
  63 import jdk.vm.ci.meta.JavaType;
  64 import jdk.vm.ci.meta.MetaAccessProvider;
  65 import jdk.vm.ci.meta.ResolvedJavaMethod;
  66 import jdk.vm.ci.meta.ResolvedJavaType;
  67 import jdk.vm.ci.meta.Signature;
  68 
  69 /**
  70  * Implementation of {@link GraphBuilderContext} used to produce a graph for a method based on an
  71  * {@link InvocationPlugin} for the method.
  72  */
  73 public class IntrinsicGraphBuilder implements GraphBuilderContext, Receiver {
  74 
  75     protected final CoreProviders providers;



  76     protected final StructuredGraph graph;
  77     protected final Bytecode code;
  78     protected final ResolvedJavaMethod method;
  79     protected final int invokeBci;
  80     protected FixedWithNextNode lastInstr;
  81     protected ValueNode[] arguments;
  82     protected ValueNode returnValue;
  83 
  84     public IntrinsicGraphBuilder(OptionValues options, DebugContext debug, CoreProviders providers, Bytecode code, int invokeBci) {
  85         this(options, debug, providers, code, invokeBci, AllowAssumptions.YES);

  86     }
  87 
  88     protected IntrinsicGraphBuilder(OptionValues options, DebugContext debug, CoreProviders providers, Bytecode code, int invokeBci, AllowAssumptions allowAssumptions) {
  89         this.providers = providers;




  90         this.code = code;
  91         this.method = code.getMethod();
  92         this.graph = new StructuredGraph.Builder(options, debug, allowAssumptions).method(method).setIsSubstitution(true).trackNodeSourcePosition(true).build();
  93         this.invokeBci = invokeBci;
  94         this.lastInstr = graph.start();
  95 
  96         Signature sig = method.getSignature();
  97         int max = sig.getParameterCount(false);
  98         this.arguments = new ValueNode[max + (method.isStatic() ? 0 : 1)];
  99 
 100         int javaIndex = 0;
 101         int index = 0;
 102         if (!method.isStatic()) {
 103             // add the receiver
 104             Stamp receiverStamp = StampFactory.objectNonNull(TypeReference.createWithoutAssumptions(method.getDeclaringClass()));
 105             ValueNode receiver = graph.addWithoutUnique(new ParameterNode(javaIndex, StampPair.createSingle(receiverStamp)));
 106             arguments[index] = receiver;
 107             javaIndex = 1;
 108             index = 1;
 109         }
 110         ResolvedJavaType accessingClass = method.getDeclaringClass();
 111         for (int i = 0; i < max; i++) {
 112             JavaType type = sig.getParameterType(i, accessingClass).resolve(accessingClass);
 113             JavaKind kind = type.getJavaKind();
 114             Stamp stamp;
 115             if (kind == JavaKind.Object && type instanceof ResolvedJavaType) {
 116                 stamp = StampFactory.object(TypeReference.createWithoutAssumptions((ResolvedJavaType) type));
 117             } else {
 118                 stamp = StampFactory.forKind(kind);
 119             }
 120             ValueNode param = graph.addWithoutUnique(new ParameterNode(index, StampPair.createSingle(stamp)));
 121             arguments[index] = param;
 122             javaIndex += kind.getSlotCount();
 123             index++;
 124         }
 125     }
 126 
 127     private <T extends ValueNode> void updateLastInstruction(T v) {
 128         if (v instanceof FixedNode) {
 129             FixedNode fixedNode = (FixedNode) v;
 130             if (lastInstr != null) {
 131                 lastInstr.setNext(fixedNode);
 132             }
 133             if (fixedNode instanceof FixedWithNextNode) {
 134                 FixedWithNextNode fixedWithNextNode = (FixedWithNextNode) fixedNode;
 135                 assert fixedWithNextNode.next() == null : "cannot append instruction to instruction which isn't end";
 136                 lastInstr = fixedWithNextNode;
 137             } else {
 138                 lastInstr = null;
 139             }
 140         }
 141     }
 142 
 143     @Override
 144     public <T extends ValueNode> T append(T v) {
 145         if (v.graph() != null) {
 146             return v;
 147         }
 148         T added = graph.addOrUniqueWithInputs(v);
 149         if (added == v) {
 150             updateLastInstruction(v);
 151         }
 152         return added;


 154 
 155     @Override
 156     public void push(JavaKind kind, ValueNode value) {
 157         assert kind != JavaKind.Void;
 158         assert returnValue == null;
 159         returnValue = value;
 160     }
 161 
 162     @Override
 163     public Invoke handleReplacedInvoke(InvokeKind invokeKind, ResolvedJavaMethod targetMethod, ValueNode[] args, boolean forceInlineEverything) {
 164         throw GraalError.shouldNotReachHere();
 165     }
 166 
 167     @Override
 168     public void handleReplacedInvoke(CallTargetNode callTarget, JavaKind resultType) {
 169         throw GraalError.shouldNotReachHere();
 170     }
 171 
 172     @Override
 173     public StampProvider getStampProvider() {
 174         return providers.getStampProvider();
 175     }
 176 
 177     @Override
 178     public MetaAccessProvider getMetaAccess() {
 179         return providers.getMetaAccess();
 180     }
 181 
 182     @Override
 183     public ConstantReflectionProvider getConstantReflection() {
 184         return providers.getConstantReflection();
 185     }
 186 
 187     @Override
 188     public ConstantFieldProvider getConstantFieldProvider() {
 189         return providers.getConstantFieldProvider();
 190     }
 191 
 192     @Override
 193     public Replacements getReplacements() {
 194         return providers.getReplacements();
 195     }
 196 
 197     @Override
 198     public StructuredGraph getGraph() {
 199         return graph;
 200     }
 201 
 202     @Override
 203     public void setStateAfter(StateSplit sideEffect) {
 204         assert sideEffect.hasSideEffect();
 205         FrameState stateAfter = getGraph().add(new FrameState(BytecodeFrame.BEFORE_BCI));
 206         sideEffect.setStateAfter(stateAfter);
 207     }
 208 
 209     @Override
 210     public GraphBuilderContext getParent() {
 211         return null;
 212     }
 213 
 214     @Override


< prev index next >