package org.graalvm.compiler.lir.alloc.lsra;
import static org.graalvm.compiler.core.common.GraalOptions.DetailedAsserts;
import static org.graalvm.compiler.lir.LIRValueUtil.isJavaConstant;
import static org.graalvm.compiler.lir.LIRValueUtil.isStackSlotValue;
import static org.graalvm.compiler.lir.LIRValueUtil.isVariable;
import static org.graalvm.compiler.lir.LIRValueUtil.isVirtualStackSlot;
import static jdk.vm.ci.code.ValueUtil.isIllegal;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
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.ConstantValue;
import org.graalvm.compiler.lir.InstructionValueProcedure;
import org.graalvm.compiler.lir.LIRFrameState;
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;
import org.graalvm.compiler.lir.StandardOp.MoveOp;
import org.graalvm.compiler.lir.StandardOp.ValueMoveOp;
import org.graalvm.compiler.lir.Variable;
import org.graalvm.compiler.lir.gen.LIRGenerationResult;
import org.graalvm.compiler.lir.phases.AllocationPhase;
import jdk.vm.ci.code.TargetDescription;
import jdk.vm.ci.meta.AllocatableValue;
import jdk.vm.ci.meta.Value;
public class LinearScanAssignLocationsPhase extends AllocationPhase {
protected final LinearScan allocator;
public LinearScanAssignLocationsPhase(LinearScan allocator) {
this.allocator = allocator;
}
@Override
protected void run(TargetDescription target, LIRGenerationResult lirGenRes, AllocationContext context) {
assignLocations();
}
protected Value colorLirOperand(LIRInstruction op, Variable operand, OperandMode mode) {
int opId = op.id();
Interval interval = allocator.intervalFor(operand);
assert interval != null : "interval must exist";
if (opId != -1) {
if (DetailedAsserts.getValue()) {
AbstractBlockBase<?> block = allocator.blockForId(opId);
if (block.getSuccessorCount() <= 1 && opId == allocator.getLastLirInstructionId(block)) {
LIRInstruction instr = allocator.getLIR().getLIRforBlock(block).get(allocator.getLIR().getLIRforBlock(block).size() - 1);
if (instr instanceof StandardOp.JumpOp) {
if (allocator.getBlockData(block).liveOut.get(allocator.operandNumber(operand))) {
assert false : String.format(
"can't get split child for the last branch of a block because the information would be incorrect (moves are inserted before the branch in resolveDataFlow) block=%s, instruction=%s, operand=%s",
block, instr, operand);
}
}
}
}
interval = allocator.splitChildAtOpId(interval, opId, mode);
}
if (isIllegal(interval.location()) && interval.canMaterialize()) {
assert mode != OperandMode.DEF;
return new ConstantValue(interval.kind(), interval.getMaterializedValue());
}
return interval.location();
}
private Value debugInfoProcedure(LIRInstruction op, Value operand, OperandMode valueMode, EnumSet<OperandFlag> flags) {
if (isVirtualStackSlot(operand)) {
return operand;
}
int tempOpId = op.id();
OperandMode mode = OperandMode.USE;
AbstractBlockBase<?> block = allocator.blockForId(tempOpId);
if (block.getSuccessorCount() == 1 && tempOpId == allocator.getLastLirInstructionId(block)) {
final LIRInstruction instr = allocator.getLIR().getLIRforBlock(block).get(allocator.getLIR().getLIRforBlock(block).size() - 1);
if (instr instanceof StandardOp.JumpOp) {
if (allocator.getBlockData(block).liveOut.get(allocator.operandNumber(operand))) {
tempOpId = allocator.getFirstLirInstructionId(block.getSuccessors()[0]);
mode = OperandMode.DEF;
}
}
}
Value result = colorLirOperand(op, (Variable) operand, mode);
assert !allocator.hasCall(tempOpId) || isStackSlotValue(result) || isJavaConstant(result) || !allocator.isCallerSave(result) : "cannot have caller-save register operands at calls";
return result;
}
private void computeDebugInfo(final LIRInstruction op, LIRFrameState info) {
info.forEachState(op, this::debugInfoProcedure);
}
private void assignLocations(List<LIRInstruction> instructions) {
int numInst = instructions.size();
boolean hasDead = false;
for (int j = 0; j < numInst; j++) {
final LIRInstruction op = instructions.get(j);
if (op == null) {
hasDead = true;
} else if (assignLocations(op)) {
instructions.set(j, null);
hasDead = true;
}
}
if (hasDead) {
instructions.removeAll(Collections.singleton(null));
}
}
protected boolean assignLocations(LIRInstruction op) {
assert op != null;
InstructionValueProcedure assignProc = (inst, operand, mode, flags) -> isVariable(operand) ? colorLirOperand(inst, (Variable) operand, mode) : operand;
if (op instanceof MoveOp) {
AllocatableValue result = ((MoveOp) op).getResult();
if (isVariable(result) && allocator.isMaterialized(result, op.id(), OperandMode.DEF)) {
return true;
}
}
op.forEachInput(assignProc);
op.forEachAlive(assignProc);
op.forEachTemp(assignProc);
op.forEachOutput(assignProc);
op.forEachState((inst, state) -> computeDebugInfo(inst, state));
if (op instanceof ValueMoveOp) {
ValueMoveOp move = (ValueMoveOp) op;
if (move.getInput().equals(move.getResult())) {
return true;
}
}
return false;
}
@SuppressWarnings("try")
private void assignLocations() {
try (Indent indent = Debug.logAndIndent("assign locations")) {
for (AbstractBlockBase<?> block : allocator.sortedBlocks()) {
try (Indent indent2 = Debug.logAndIndent("assign locations in block B%d", block.getId())) {
assignLocations(allocator.getLIR().getLIRforBlock(block));
}
}
}
}
}