package org.graalvm.compiler.lir.ssi;
import static org.graalvm.compiler.lir.LIRValueUtil.isConstantValue;
import static org.graalvm.compiler.lir.LIRValueUtil.isStackSlotValue;
import static org.graalvm.compiler.lir.LIRValueUtil.isVirtualStackSlot;
import static jdk.vm.ci.code.ValueUtil.isRegister;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import org.graalvm.compiler.core.common.LIRKind;
import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
import org.graalvm.compiler.debug.Debug;
import org.graalvm.compiler.debug.Debug.Scope;
import org.graalvm.compiler.lir.InstructionValueConsumer;
import org.graalvm.compiler.lir.LIR;
import org.graalvm.compiler.lir.LIRInstruction;
import org.graalvm.compiler.lir.LIRInstruction.OperandFlag;
import org.graalvm.compiler.lir.LIRInstruction.OperandMode;
import org.graalvm.compiler.lir.StandardOp.BlockEndOp;
import org.graalvm.compiler.lir.StandardOp.LabelOp;
import jdk.vm.ci.meta.Value;
import jdk.vm.ci.meta.ValueKind;
public final class SSIVerifier {
public static boolean verify(LIR lir) {
return new SSIVerifier(lir).verify();
}
private final LIR lir;
private SSIVerifier(LIR lir) {
this.lir = lir;
}
@SuppressWarnings("try")
private boolean verify() {
try (Scope s = Debug.scope("SSIVerifier", lir)) {
for (AbstractBlockBase<?> block : lir.getControlFlowGraph().getBlocks()) {
doBlock(block);
}
} catch (Throwable e) {
throw Debug.handle(e);
}
return true;
}
private void doBlock(AbstractBlockBase<?> block) {
for (AbstractBlockBase<?> succ : block.getSuccessors()) {
verifyEdge(block, succ);
}
verifyInstructions(block);
}
private void verifyEdge(AbstractBlockBase<?> from, AbstractBlockBase<?> to) {
BlockEndOp out = SSIUtil.outgoing(lir, from);
LabelOp in = SSIUtil.incoming(lir, to);
int outgoingSize = out.getOutgoingSize();
int incomingSize = in.getIncomingSize();
assert outgoingSize == incomingSize : String.format("Outgoing size %d and incoming size %d do not match", outgoingSize, incomingSize);
for (int i = 0; i < outgoingSize; i++) {
Value incomingValue = in.getIncomingValue(i);
Value outgoingValue = out.getOutgoingValue(i);
ValueKind<?> inLIRKind = incomingValue.getValueKind();
ValueKind<?> outLIRKind = outgoingValue.getValueKind();
assert LIRKind.verifyMoveKinds(inLIRKind, outLIRKind) || incomingValue.equals(Value.ILLEGAL) : String.format("Outgoing LIRKind %s (%s) an and incoming LIRKind %s (%s) do not match",
outgoingValue, outLIRKind, incomingValue, inLIRKind);
}
}
private void verifyInstructions(AbstractBlockBase<?> block) {
List<LIRInstruction> instructions = lir.getLIRforBlock(block);
HashMap<Value, LIRInstruction> defined = new HashMap<>();
InstructionValueConsumer useConsumer = new InstructionValueConsumer() {
@Override
public void visitValue(LIRInstruction instruction, Value value, OperandMode mode, EnumSet<OperandFlag> flags) {
if (checkUsage(value)) {
assert defined.containsKey(value) || flags.contains(OperandFlag.UNINITIALIZED) : String.format("Value %s is used by instruction %s in block %s but not defined.", value,
instruction, block);
}
}
};
InstructionValueConsumer stateConsumer = new InstructionValueConsumer() {
@Override
public void visitValue(LIRInstruction instruction, Value value, OperandMode mode, EnumSet<OperandFlag> flags) {
if (checkUsage(value)) {
assert defined.containsKey(value) || isVirtualStackSlot(value) : String.format("Value %s is used in state of instruction %s in block %s but not defined.", value, instruction,
block);
}
}
};
InstructionValueConsumer defConsumer = new InstructionValueConsumer() {
@Override
public void visitValue(LIRInstruction instruction, Value value, OperandMode mode, EnumSet<OperandFlag> flags) {
if (trackDefinition(value)) {
assert !defined.containsKey(value) : String.format("Value %s is redefined by instruction %s in block %s but already defined by %s.", value, instruction, block, defined.get(value));
defined.put(value, instruction);
}
}
};
for (LIRInstruction op : instructions) {
op.visitEachAlive(useConsumer);
op.visitEachInput(useConsumer);
op.visitEachState(stateConsumer);
op.visitEachTemp(defConsumer);
op.visitEachOutput(defConsumer);
}
}
private static boolean trackDefinition(Value value) {
if (isRegister(value)) {
return false;
}
if (isStackSlotValue(value) && !isVirtualStackSlot(value)) {
return false;
}
if (value.equals(Value.ILLEGAL)) {
return false;
}
return true;
}
private static boolean checkUsage(Value value) {
if (isConstantValue(value)) {
return false;
}
if (isRegister(value)) {
return false;
}
if (isStackSlotValue(value)) {
return false;
}
if (value.equals(Value.ILLEGAL)) {
return false;
}
return true;
}
}