package org.graalvm.compiler.lir.alloc.trace;
import java.util.ArrayList;
import org.graalvm.compiler.core.common.alloc.Trace;
import org.graalvm.compiler.core.common.alloc.TraceBuilderResult;
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.StandardOp.JumpOp;
import org.graalvm.compiler.lir.StandardOp.LabelOp;
import jdk.vm.ci.meta.Value;
public class TraceUtil {
public static AbstractBlockBase<?> getBestTraceInterPredecessor(TraceBuilderResult traceResult, AbstractBlockBase<?> block) {
AbstractBlockBase<?> bestPred = null;
int bestTraceId = traceResult.getTraceForBlock(block).getId();
for (AbstractBlockBase<?> pred : block.getPredecessors()) {
int predTraceId = traceResult.getTraceForBlock(pred).getId();
if (predTraceId < bestTraceId) {
bestPred = pred;
bestTraceId = predTraceId;
}
}
return bestPred;
}
public static boolean isShadowedRegisterValue(Value value) {
assert value != null;
return value instanceof ShadowedRegisterValue;
}
public static ShadowedRegisterValue asShadowedRegisterValue(Value value) {
assert isShadowedRegisterValue(value);
return (ShadowedRegisterValue) value;
}
public static boolean isTrivialTrace(LIR lir, Trace trace) {
if (trace.size() != 1) {
return false;
}
ArrayList<LIRInstruction> instructions = lir.getLIRforBlock(trace.getBlocks()[0]);
if (instructions.size() != 2) {
return false;
}
assert instructions.get(0) instanceof LabelOp : "First instruction not a LabelOp: " + instructions.get(0);
if (((LabelOp) instructions.get(0)).isPhiIn()) {
return false;
}
return instructions.get(1) instanceof JumpOp;
}
public static boolean hasInterTracePredecessor(TraceBuilderResult result, Trace trace, AbstractBlockBase<?> block) {
assert result.getTraceForBlock(block).equals(trace);
if (block.getPredecessorCount() == 0) {
return false;
}
if (block.getPredecessorCount() == 1) {
return !result.getTraceForBlock(block.getPredecessors()[0]).equals(trace);
}
return true;
}
public static boolean hasInterTraceSuccessor(TraceBuilderResult result, Trace trace, AbstractBlockBase<?> block) {
assert result.getTraceForBlock(block).equals(trace);
if (block.getSuccessorCount() == 0) {
return false;
}
if (block.getSuccessorCount() == 1) {
return !result.getTraceForBlock(block.getSuccessors()[0]).equals(trace);
}
return true;
}
}