package org.graalvm.compiler.lir.ssa;
import java.util.ArrayList;
import java.util.Arrays;
import org.graalvm.compiler.core.common.LIRKind;
import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
import org.graalvm.compiler.lir.LIR;
import org.graalvm.compiler.lir.LIRInstruction;
import org.graalvm.compiler.lir.LIRInstruction.OperandMode;
import org.graalvm.compiler.lir.StandardOp.BlockEndOp;
import org.graalvm.compiler.lir.StandardOp.JumpOp;
import org.graalvm.compiler.lir.StandardOp.LabelOp;
import org.graalvm.compiler.lir.ValueConsumer;
import jdk.vm.ci.meta.Value;
public final class SSAUtil {
public interface PhiValueVisitor {
void visit(Value phiIn, Value phiOut);
}
public static void forEachPhiValuePair(LIR lir, AbstractBlockBase<?> merge, AbstractBlockBase<?> pred, PhiValueVisitor visitor) {
if (merge.getPredecessorCount() < 2) {
return;
}
assert Arrays.asList(merge.getPredecessors()).contains(pred) : String.format("%s not in predecessor list: %s", pred, Arrays.toString(merge.getPredecessors()));
assert pred.getSuccessorCount() == 1 : String.format("Merge predecessor block %s has more than one successor? %s", pred, Arrays.toString(pred.getSuccessors()));
assert pred.getSuccessors()[0] == merge : String.format("Predecessor block %s has wrong successor: %s, should be: %s", pred, pred.getSuccessors()[0], merge);
JumpOp jump = phiOut(lir, pred);
LabelOp label = phiIn(lir, merge);
assert label.getPhiSize() == jump.getPhiSize() : String.format("Phi In/Out size mismatch: in=%d vs. out=%d", label.getPhiSize(), jump.getPhiSize());
for (int i = 0; i < label.getPhiSize(); i++) {
visitor.visit(label.getIncomingValue(i), jump.getOutgoingValue(i));
}
}
public static JumpOp phiOut(LIR lir, AbstractBlockBase<?> block) {
assert block.getSuccessorCount() == 1;
ArrayList<LIRInstruction> instructions = lir.getLIRforBlock(block);
int index = instructions.size() - 1;
LIRInstruction op = instructions.get(index);
return (JumpOp) op;
}
public static JumpOp phiOutOrNull(LIR lir, AbstractBlockBase<?> block) {
if (block.getSuccessorCount() != 1) {
return null;
}
return phiOut(lir, block);
}
public static int phiOutIndex(LIR lir, AbstractBlockBase<?> block) {
assert block.getSuccessorCount() == 1;
ArrayList<LIRInstruction> instructions = lir.getLIRforBlock(block);
int index = instructions.size() - 1;
assert instructions.get(index) instanceof JumpOp;
return index;
}
public static LabelOp phiIn(LIR lir, AbstractBlockBase<?> block) {
assert block.getPredecessorCount() > 1;
LabelOp label = (LabelOp) lir.getLIRforBlock(block).get(0);
return label;
}
public static void removePhiOut(LIR lir, AbstractBlockBase<?> block) {
JumpOp jump = phiOut(lir, block);
jump.clearOutgoingValues();
}
public static void removePhiIn(LIR lir, AbstractBlockBase<?> block) {
LabelOp label = phiIn(lir, block);
label.clearIncomingValues();
}
public static boolean verifySSAForm(LIR lir) {
return new SSAVerifier(lir).verify();
}
public static boolean isMerge(AbstractBlockBase<?> block) {
return block.getPredecessorCount() > 1;
}
public static void verifyPhi(LIR lir, AbstractBlockBase<?> merge) {
assert merge.getPredecessorCount() > 1;
for (AbstractBlockBase<?> pred : merge.getPredecessors()) {
forEachPhiValuePair(lir, merge, pred, (phiIn, phiOut) -> {
assert phiIn.getValueKind().equals(phiOut.getValueKind()) ||
(phiIn.getPlatformKind().equals(phiOut.getPlatformKind()) && LIRKind.isUnknownReference(phiIn) && LIRKind.isValue(phiOut));
});
}
}
public static void forEachPhiRegisterHint(LIR lir, AbstractBlockBase<?> block, LabelOp label, Value targetValue, OperandMode mode, ValueConsumer valueConsumer) {
assert mode == OperandMode.DEF : "Wrong operand mode: " + mode;
assert lir.getLIRforBlock(block).get(0).equals(label) : String.format("Block %s and Label %s do not match!", block, label);
if (!label.isPhiIn()) {
return;
}
int idx = indexOfValue(label, targetValue);
assert idx >= 0 : String.format("Value %s not in label %s", targetValue, label);
for (AbstractBlockBase<?> pred : block.getPredecessors()) {
JumpOp jump = phiOut(lir, pred);
Value sourceValue = jump.getOutgoingValue(idx);
valueConsumer.visitValue(jump, sourceValue, null, null);
}
}
private static int indexOfValue(LabelOp label, Value value) {
for (int i = 0; i < label.getIncomingSize(); i++) {
if (label.getIncomingValue(i).equals(value)) {
return i;
}
}
return -1;
}
public static int numPhiOut(LIR lir, AbstractBlockBase<?> block) {
if (block.getSuccessorCount() != 1) {
return 0;
}
return numPhiIn(lir, block.getSuccessors()[0]);
}
private static int numPhiIn(LIR lir, AbstractBlockBase<?> block) {
if (!isMerge(block)) {
return 0;
}
return phiIn(lir, block).getPhiSize();
}
}