package org.graalvm.compiler.lir.alloc.trace;
import static org.graalvm.compiler.lir.LIRValueUtil.isStackSlotValue;
import static org.graalvm.compiler.lir.alloc.trace.TraceUtil.asShadowedRegisterValue;
import static org.graalvm.compiler.lir.alloc.trace.TraceUtil.isShadowedRegisterValue;
import static jdk.vm.ci.code.ValueUtil.asRegisterValue;
import static jdk.vm.ci.code.ValueUtil.isIllegal;
import static jdk.vm.ci.code.ValueUtil.isRegister;
import java.util.List;
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.debug.Debug;
import org.graalvm.compiler.debug.Indent;
import org.graalvm.compiler.lir.LIR;
import org.graalvm.compiler.lir.LIRInstruction;
import org.graalvm.compiler.lir.alloc.trace.TraceAllocationPhase.TraceAllocationContext;
import org.graalvm.compiler.lir.gen.LIRGenerationResult;
import org.graalvm.compiler.lir.gen.LIRGeneratorTool.MoveFactory;
import org.graalvm.compiler.lir.phases.LIRPhase;
import org.graalvm.compiler.lir.ssa.SSAUtil.PhiValueVisitor;
import org.graalvm.compiler.lir.ssi.SSIUtil;
import jdk.vm.ci.code.Architecture;
import jdk.vm.ci.code.RegisterValue;
import jdk.vm.ci.code.TargetDescription;
import jdk.vm.ci.meta.AllocatableValue;
import jdk.vm.ci.meta.Value;
public final class TraceGlobalMoveResolutionPhase extends LIRPhase<TraceAllocationPhase.TraceAllocationContext> {
public abstract static class MoveResolver {
public abstract void addMapping(Value src, AllocatableValue dst, Value fromStack);
}
@Override
protected void run(TargetDescription target, LIRGenerationResult lirGenRes, TraceAllocationContext context) {
MoveFactory spillMoveFactory = context.spillMoveFactory;
resolveGlobalDataFlow(context.resultTraces, lirGenRes, spillMoveFactory, target.arch);
}
@SuppressWarnings("try")
private static void resolveGlobalDataFlow(TraceBuilderResult resultTraces, LIRGenerationResult lirGenRes, MoveFactory spillMoveFactory, Architecture arch) {
LIR lir = lirGenRes.getLIR();
TraceGlobalMoveResolver moveResolver = new TraceGlobalMoveResolver(lirGenRes, spillMoveFactory, arch);
PhiValueVisitor visitor = (Value phiIn, Value phiOut) -> {
if (!isIllegal(phiIn)) {
addMapping(moveResolver, phiOut, phiIn);
}
};
try (Indent indent = Debug.logAndIndent("Trace global move resolution")) {
for (Trace trace : resultTraces.getTraces()) {
for (AbstractBlockBase<?> fromBlock : trace.getBlocks()) {
for (AbstractBlockBase<?> toBlock : fromBlock.getSuccessors()) {
if (resultTraces.getTraceForBlock(fromBlock) != resultTraces.getTraceForBlock(toBlock)) {
try (Indent indent0 = Debug.logAndIndent("Handle trace edge from %s (Trace%d) to %s (Trace%d)", fromBlock, resultTraces.getTraceForBlock(fromBlock).getId(), toBlock,
resultTraces.getTraceForBlock(toBlock).getId())) {
final List<LIRInstruction> instructions;
final int insertIdx;
if (fromBlock.getSuccessorCount() == 1) {
instructions = lir.getLIRforBlock(fromBlock);
insertIdx = instructions.size() - 1;
} else {
assert toBlock.getPredecessorCount() == 1;
instructions = lir.getLIRforBlock(toBlock);
insertIdx = 1;
}
moveResolver.setInsertPosition(instructions, insertIdx);
SSIUtil.forEachValuePair(lir, toBlock, fromBlock, visitor);
moveResolver.resolveAndAppendMoves();
}
}
}
}
}
}
}
public static void addMapping(MoveResolver moveResolver, Value from, Value to) {
assert !isIllegal(to);
if (isShadowedRegisterValue(to)) {
ShadowedRegisterValue toSh = asShadowedRegisterValue(to);
addMappingToRegister(moveResolver, from, toSh.getRegister());
addMappingToStackSlot(moveResolver, from, toSh.getStackSlot());
} else {
if (isRegister(to)) {
addMappingToRegister(moveResolver, from, asRegisterValue(to));
} else {
assert isStackSlotValue(to) : "Expected stack slot: " + to;
addMappingToStackSlot(moveResolver, from, (AllocatableValue) to);
}
}
}
private static void addMappingToRegister(MoveResolver moveResolver, Value from, RegisterValue register) {
if (isShadowedRegisterValue(from)) {
RegisterValue fromReg = asShadowedRegisterValue(from).getRegister();
AllocatableValue fromStack = asShadowedRegisterValue(from).getStackSlot();
checkAndAddMapping(moveResolver, fromReg, register, fromStack);
} else {
checkAndAddMapping(moveResolver, from, register, null);
}
}
private static void addMappingToStackSlot(MoveResolver moveResolver, Value from, AllocatableValue stack) {
if (isShadowedRegisterValue(from)) {
ShadowedRegisterValue shadowedFrom = asShadowedRegisterValue(from);
RegisterValue fromReg = shadowedFrom.getRegister();
AllocatableValue fromStack = shadowedFrom.getStackSlot();
if (!fromStack.equals(stack)) {
checkAndAddMapping(moveResolver, fromReg, stack, fromStack);
}
} else {
checkAndAddMapping(moveResolver, from, stack, null);
}
}
private static void checkAndAddMapping(MoveResolver moveResolver, Value from, AllocatableValue to, AllocatableValue fromStack) {
if (!from.equals(to) && (fromStack == null || !fromStack.equals(to))) {
moveResolver.addMapping(from, to, fromStack);
}
}
}