use of jdk.vm.ci.meta.Constant in project graal by oracle.
the class HotSpotResolvedObjectTypeTest method testKlassLayoutHelper.
@Test
public void testKlassLayoutHelper() {
Constant klass = HotSpotResolvedObjectType.fromObjectClass(this.getClass()).klass();
MemoryAccessProvider memoryAccess = getProviders().getConstantReflection().getMemoryAccessProvider();
GraalHotSpotVMConfig config = runtime().getVMConfig();
Constant c = StampFactory.forKind(JavaKind.Int).readConstant(memoryAccess, klass, config.klassLayoutHelperOffset);
assertTrue(c.toString(), c.getClass() == PrimitiveConstant.class);
PrimitiveConstant pc = (PrimitiveConstant) c;
assertTrue(pc.toString(), pc.getJavaKind() == JavaKind.Int);
}
use of jdk.vm.ci.meta.Constant in project graal by oracle.
the class KlassLayoutHelperNode method canonical.
private static ValueNode canonical(KlassLayoutHelperNode klassLayoutHelperNode, GraalHotSpotVMConfig config, ValueNode klass, Stamp stamp, ConstantReflectionProvider constantReflection, MetaAccessProvider metaAccess) {
KlassLayoutHelperNode self = klassLayoutHelperNode;
if (klass.isConstant()) {
if (!klass.asConstant().isDefaultForKind()) {
Constant constant = stamp.readConstant(constantReflection.getMemoryAccessProvider(), klass.asConstant(), config.klassLayoutHelperOffset);
return ConstantNode.forConstant(stamp, constant, metaAccess);
}
}
if (klass instanceof LoadHubNode) {
LoadHubNode hub = (LoadHubNode) klass;
Stamp hubStamp = hub.getValue().stamp(NodeView.DEFAULT);
if (hubStamp instanceof ObjectStamp) {
ObjectStamp ostamp = (ObjectStamp) hubStamp;
HotSpotResolvedObjectType type = (HotSpotResolvedObjectType) ostamp.type();
if (type != null && type.isArray() && !type.getComponentType().isPrimitive()) {
// The layout for all object arrays is the same.
Constant constant = stamp.readConstant(constantReflection.getMemoryAccessProvider(), type.klass(), config.klassLayoutHelperOffset);
return ConstantNode.forConstant(stamp, constant, metaAccess);
}
}
}
if (self == null) {
self = new KlassLayoutHelperNode(config, klass);
}
return self;
}
use of jdk.vm.ci.meta.Constant in project graal by oracle.
the class TypeSwitchNode method simplify.
@Override
public void simplify(SimplifierTool tool) {
NodeView view = NodeView.from(tool);
if (value() instanceof ConstantNode) {
Constant constant = value().asConstant();
int survivingEdge = keySuccessorIndex(keyCount());
for (int i = 0; i < keyCount(); i++) {
Constant typeHub = keyAt(i);
Boolean equal = tool.getConstantReflection().constantEquals(constant, typeHub);
if (equal == null) {
/* We don't know if this key is a match or not, so we cannot simplify. */
return;
} else if (equal.booleanValue()) {
survivingEdge = keySuccessorIndex(i);
}
}
killOtherSuccessors(tool, survivingEdge);
}
if (value() instanceof LoadHubNode && ((LoadHubNode) value()).getValue().stamp(view) instanceof ObjectStamp) {
ObjectStamp objectStamp = (ObjectStamp) ((LoadHubNode) value()).getValue().stamp(view);
if (objectStamp.type() != null) {
int validKeys = 0;
for (int i = 0; i < keyCount(); i++) {
if (objectStamp.type().isAssignableFrom(keys[i])) {
validKeys++;
}
}
if (validKeys == 0) {
tool.addToWorkList(defaultSuccessor());
graph().removeSplitPropagate(this, defaultSuccessor());
} else if (validKeys != keys.length) {
ArrayList<AbstractBeginNode> newSuccessors = new ArrayList<>(blockSuccessorCount());
ResolvedJavaType[] newKeys = new ResolvedJavaType[validKeys];
int[] newKeySuccessors = new int[validKeys + 1];
double[] newKeyProbabilities = new double[validKeys + 1];
double totalProbability = 0;
int current = 0;
for (int i = 0; i < keyCount() + 1; i++) {
if (i == keyCount() || objectStamp.type().isAssignableFrom(keys[i])) {
int index = newSuccessors.indexOf(keySuccessor(i));
if (index == -1) {
index = newSuccessors.size();
newSuccessors.add(keySuccessor(i));
}
newKeySuccessors[current] = index;
if (i < keyCount()) {
newKeys[current] = keys[i];
}
newKeyProbabilities[current] = keyProbability(i);
totalProbability += keyProbability(i);
current++;
}
}
if (totalProbability > 0) {
for (int i = 0; i < current; i++) {
newKeyProbabilities[i] /= totalProbability;
}
} else {
for (int i = 0; i < current; i++) {
newKeyProbabilities[i] = 1.0 / current;
}
}
for (int i = 0; i < blockSuccessorCount(); i++) {
AbstractBeginNode successor = blockSuccessor(i);
if (!newSuccessors.contains(successor)) {
tool.deleteBranch(successor);
}
setBlockSuccessor(i, null);
}
AbstractBeginNode[] successorsArray = newSuccessors.toArray(new AbstractBeginNode[newSuccessors.size()]);
TypeSwitchNode newSwitch = graph().add(new TypeSwitchNode(value(), successorsArray, newKeys, newKeyProbabilities, newKeySuccessors, tool.getConstantReflection()));
((FixedWithNextNode) predecessor()).setNext(newSwitch);
GraphUtil.killWithUnusedFloatingInputs(this);
}
}
}
}
use of jdk.vm.ci.meta.Constant in project graal by oracle.
the class ClassCastBytecodeExceptionTest method registerInvocationPlugins.
@Override
protected void registerInvocationPlugins(InvocationPlugins invocationPlugins) {
invocationPlugins.register(new InvocationPlugin() {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode obj, ValueNode classNode) {
ResolvedJavaType type = b.getConstantReflection().asJavaType(classNode.asConstant());
Constant hub = b.getConstantReflection().asObjectHub(type);
Stamp hubStamp = b.getStampProvider().createHubStamp(StampFactory.object(TypeReference.createExactTrusted(type)));
ConstantNode hubConst = b.add(ConstantNode.forConstant(hubStamp, hub, b.getMetaAccess()));
return throwBytecodeException(b, ClassCastException.class, obj, hubConst);
}
}, Exceptions.class, "throwClassCast", Object.class, Class.class);
super.registerInvocationPlugins(invocationPlugins);
}
use of jdk.vm.ci.meta.Constant in project graal by oracle.
the class IfNode method checkForUnsignedCompare.
/**
* Recognize a couple patterns that can be merged into an unsigned compare.
*
* @param tool
* @return true if a replacement was done.
*/
private boolean checkForUnsignedCompare(SimplifierTool tool) {
assert trueSuccessor().hasNoUsages() && falseSuccessor().hasNoUsages();
if (condition() instanceof IntegerLessThanNode) {
NodeView view = NodeView.from(tool);
IntegerLessThanNode lessThan = (IntegerLessThanNode) condition();
Constant y = lessThan.getY().stamp(view).asConstant();
if (y instanceof PrimitiveConstant && ((PrimitiveConstant) y).asLong() == 0 && falseSuccessor().next() instanceof IfNode) {
IfNode ifNode2 = (IfNode) falseSuccessor().next();
if (ifNode2.condition() instanceof IntegerLessThanNode) {
IntegerLessThanNode lessThan2 = (IntegerLessThanNode) ifNode2.condition();
AbstractBeginNode falseSucc = ifNode2.falseSuccessor();
AbstractBeginNode trueSucc = ifNode2.trueSuccessor();
IntegerBelowNode below = null;
/*
* Convert x >= 0 && x < positive which is represented as !(x < 0) && x <
* <positive> into an unsigned compare.
*/
if (lessThan2.getX() == lessThan.getX() && lessThan2.getY().stamp(view) instanceof IntegerStamp && ((IntegerStamp) lessThan2.getY().stamp(view)).isPositive() && sameDestination(trueSuccessor(), ifNode2.falseSuccessor)) {
below = graph().unique(new IntegerBelowNode(lessThan2.getX(), lessThan2.getY()));
// swap direction
AbstractBeginNode tmp = falseSucc;
falseSucc = trueSucc;
trueSucc = tmp;
} else if (lessThan2.getY() == lessThan.getX() && sameDestination(trueSuccessor(), ifNode2.trueSuccessor)) {
/*
* Convert x >= 0 && x <= positive which is represented as !(x < 0) &&
* !(<positive> > x), into x <| positive + 1. This can only be done for
* constants since there isn't a IntegerBelowEqualThanNode but that doesn't
* appear to be interesting.
*/
JavaConstant positive = lessThan2.getX().asJavaConstant();
if (positive != null && positive.asLong() > 0 && positive.asLong() < positive.getJavaKind().getMaxValue()) {
ConstantNode newLimit = ConstantNode.forIntegerStamp(lessThan2.getX().stamp(view), positive.asLong() + 1, graph());
below = graph().unique(new IntegerBelowNode(lessThan.getX(), newLimit));
}
}
if (below != null) {
ifNode2.setTrueSuccessor(null);
ifNode2.setFalseSuccessor(null);
IfNode newIfNode = graph().add(new IfNode(below, falseSucc, trueSucc, 1 - trueSuccessorProbability));
// Remove the < 0 test.
tool.deleteBranch(trueSuccessor);
graph().removeSplit(this, falseSuccessor);
// Replace the second test with the new one.
ifNode2.predecessor().replaceFirstSuccessor(ifNode2, newIfNode);
ifNode2.safeDelete();
return true;
}
}
}
}
return false;
}
Aggregations