use of org.graalvm.compiler.lir.LIRInsertionBuffer in project graal by oracle.
the class LinearScanOptimizeSpillPositionPhase method optimizeInterval.
@SuppressWarnings("try")
private void optimizeInterval(LIRInsertionBuffer[] insertionBuffers, Interval interval, LIRGenerationResult res) {
if (interval == null || !interval.isSplitParent() || interval.spillState() != SpillState.SpillInDominator) {
return;
}
AbstractBlockBase<?> defBlock = allocator.blockForId(interval.spillDefinitionPos());
AbstractBlockBase<?> spillBlock = null;
Interval firstSpillChild = null;
try (Indent indent = debug.logAndIndent("interval %s (%s)", interval, defBlock)) {
for (Interval splitChild : interval.getSplitChildren()) {
if (isStackSlotValue(splitChild.location())) {
if (firstSpillChild == null || splitChild.from() < firstSpillChild.from()) {
firstSpillChild = splitChild;
} else {
assert firstSpillChild.from() < splitChild.from();
}
// iterate all blocks where the interval has use positions
for (AbstractBlockBase<?> splitBlock : blocksForInterval(splitChild)) {
if (dominates(defBlock, splitBlock)) {
debug.log("Split interval %s, block %s", splitChild, splitBlock);
if (spillBlock == null) {
spillBlock = splitBlock;
} else {
spillBlock = commonDominator(spillBlock, splitBlock);
assert spillBlock != null;
}
}
}
}
}
if (spillBlock == null) {
debug.log("not spill interval found");
// no spill interval
interval.setSpillState(SpillState.StoreAtDefinition);
return;
}
debug.log(DebugContext.VERBOSE_LEVEL, "Spill block candidate (initial): %s", spillBlock);
// move out of loops
if (defBlock.getLoopDepth() < spillBlock.getLoopDepth()) {
spillBlock = moveSpillOutOfLoop(defBlock, spillBlock);
}
debug.log(DebugContext.VERBOSE_LEVEL, "Spill block candidate (after loop optimizaton): %s", spillBlock);
/*
* The spill block is the begin of the first split child (aka the value is on the
* stack).
*
* The problem is that if spill block has more than one predecessor, the values at the
* end of the predecessors might differ. Therefore, we would need a spill move in all
* predecessors. To avoid this we spill in the dominator.
*/
assert firstSpillChild != null;
if (!defBlock.equals(spillBlock) && spillBlock.equals(allocator.blockForId(firstSpillChild.from()))) {
AbstractBlockBase<?> dom = spillBlock.getDominator();
if (debug.isLogEnabled()) {
debug.log("Spill block (%s) is the beginning of a spill child -> use dominator (%s)", spillBlock, dom);
}
spillBlock = dom;
}
if (defBlock.equals(spillBlock)) {
debug.log(DebugContext.VERBOSE_LEVEL, "Definition is the best choice: %s", defBlock);
// definition is the best choice
interval.setSpillState(SpillState.StoreAtDefinition);
return;
}
assert dominates(defBlock, spillBlock);
betterSpillPos.increment(debug);
if (debug.isLogEnabled()) {
debug.log("Better spill position found (Block %s)", spillBlock);
}
if (defBlock.probability() <= spillBlock.probability()) {
debug.log(DebugContext.VERBOSE_LEVEL, "Definition has lower probability %s (%f) is lower than spill block %s (%f)", defBlock, defBlock.probability(), spillBlock, spillBlock.probability());
// better spill block has the same probability -> do nothing
interval.setSpillState(SpillState.StoreAtDefinition);
return;
}
LIRInsertionBuffer insertionBuffer = insertionBuffers[spillBlock.getId()];
if (insertionBuffer == null) {
insertionBuffer = new LIRInsertionBuffer();
insertionBuffers[spillBlock.getId()] = insertionBuffer;
insertionBuffer.init(allocator.getLIR().getLIRforBlock(spillBlock));
}
int spillOpId = allocator.getFirstLirInstructionId(spillBlock);
// insert spill move
AllocatableValue fromLocation = interval.getSplitChildAtOpId(spillOpId, OperandMode.DEF, allocator).location();
AllocatableValue toLocation = LinearScan.canonicalSpillOpr(interval);
LIRInstruction move = allocator.getSpillMoveFactory().createMove(toLocation, fromLocation);
move.setComment(res, "LSRAOptimizeSpillPos: optimize spill pos");
debug.log(DebugContext.VERBOSE_LEVEL, "Insert spill move %s", move);
move.setId(LinearScan.DOMINATOR_SPILL_MOVE_ID);
/*
* We can use the insertion buffer directly because we always insert at position 1.
*/
insertionBuffer.append(1, move);
betterSpillPosWithLowerProbability.increment(debug);
interval.setSpillDefinitionPos(spillOpId);
}
}
use of org.graalvm.compiler.lir.LIRInsertionBuffer in project graal by oracle.
the class SaveCalleeSaveRegisters method restoreAtExit.
private static void restoreAtExit(LIR lir, LIRGeneratorTool.MoveFactory moveFactory, LIRGenerationResult lirGenRes, RegisterMap<Variable> calleeSaveRegisters, AbstractBlockBase<?> block) {
ArrayList<LIRInstruction> instructions = lir.getLIRforBlock(block);
int insertionIndex = instructions.size() - 1;
LIRInsertionBuffer buffer = new LIRInsertionBuffer();
buffer.init(instructions);
assert instructions.get(insertionIndex) instanceof StandardOp.BlockEndOp;
calleeSaveRegisters.forEach((Register register, Variable saved) -> {
LIRInstruction restore = moveFactory.createMove(register.asValue(saved.getValueKind()), saved);
buffer.append(insertionIndex, restore);
restore.setComment(lirGenRes, "SaveCalleeSavedRegisters: restoreAtExit");
});
buffer.finish();
}
use of org.graalvm.compiler.lir.LIRInsertionBuffer in project graal by oracle.
the class SaveCalleeSaveRegisters method saveAtEntry.
private static RegisterMap<Variable> saveAtEntry(LIR lir, LIRGeneratorTool lirGen, LIRGenerationResult lirGenRes, RegisterArray calleeSaveRegisters, Architecture arch) {
AbstractBlockBase<?> startBlock = lir.getControlFlowGraph().getStartBlock();
ArrayList<LIRInstruction> instructions = lir.getLIRforBlock(startBlock);
int insertionIndex = 1;
LIRInsertionBuffer buffer = new LIRInsertionBuffer();
buffer.init(instructions);
StandardOp.LabelOp entry = (StandardOp.LabelOp) instructions.get(insertionIndex - 1);
RegisterValue[] savedRegisterValues = new RegisterValue[calleeSaveRegisters.size()];
int savedRegisterValueIndex = 0;
RegisterMap<Variable> saveMap = new RegisterMap<>(arch);
for (Register register : calleeSaveRegisters) {
PlatformKind registerPlatformKind = arch.getLargestStorableKind(register.getRegisterCategory());
LIRKind lirKind = LIRKind.value(registerPlatformKind);
RegisterValue registerValue = register.asValue(lirKind);
Variable saveVariable = lirGen.newVariable(lirKind);
LIRInstruction save = lirGen.getSpillMoveFactory().createMove(saveVariable, registerValue);
buffer.append(insertionIndex, save);
save.setComment(lirGenRes, "SaveCalleeSavedRegisters: saveAtEntry");
saveMap.put(register, saveVariable);
savedRegisterValues[savedRegisterValueIndex++] = registerValue;
}
entry.addIncomingValues(savedRegisterValues);
buffer.finish();
return saveMap;
}
use of org.graalvm.compiler.lir.LIRInsertionBuffer in project graal by oracle.
the class LinearScanEliminateSpillMovePhase method eliminateSpillMoves.
// called once before assignment of register numbers
@SuppressWarnings("try")
void eliminateSpillMoves(LIRGenerationResult res) {
DebugContext debug = allocator.getDebug();
try (Indent indent = debug.logAndIndent("Eliminating unnecessary spill moves")) {
/*
* collect all intervals that must be stored after their definition. The list is sorted
* by Interval.spillDefinitionPos.
*/
Interval interval;
interval = allocator.createUnhandledLists(mustStoreAtDefinition, null).getLeft();
if (Assertions.detailedAssertionsEnabled(allocator.getOptions())) {
checkIntervals(debug, interval);
}
LIRInsertionBuffer insertionBuffer = new LIRInsertionBuffer();
for (AbstractBlockBase<?> block : allocator.sortedBlocks()) {
try (Indent indent1 = debug.logAndIndent("Handle %s", block)) {
ArrayList<LIRInstruction> instructions = allocator.getLIR().getLIRforBlock(block);
int numInst = instructions.size();
// iterate all instructions of the block.
for (int j = firstInstructionOfInterest(); j < numInst; j++) {
LIRInstruction op = instructions.get(j);
int opId = op.id();
if (opId == -1) {
MoveOp move = MoveOp.asMoveOp(op);
/*
* Remove move from register to stack if the stack slot is guaranteed to
* be correct. Only moves that have been inserted by LinearScan can be
* removed.
*/
if (Options.LIROptLSRAEliminateSpillMoves.getValue(allocator.getOptions()) && canEliminateSpillMove(block, move)) {
/*
* Move target is a stack slot that is always correct, so eliminate
* instruction.
*/
if (debug.isLogEnabled()) {
if (ValueMoveOp.isValueMoveOp(op)) {
ValueMoveOp vmove = ValueMoveOp.asValueMoveOp(op);
debug.log("eliminating move from interval %d (%s) to %d (%s) in block %s", allocator.operandNumber(vmove.getInput()), vmove.getInput(), allocator.operandNumber(vmove.getResult()), vmove.getResult(), block);
} else {
LoadConstantOp load = LoadConstantOp.asLoadConstantOp(op);
debug.log("eliminating constant load from %s to %d (%s) in block %s", load.getConstant(), allocator.operandNumber(load.getResult()), load.getResult(), block);
}
}
// null-instructions are deleted by assignRegNum
instructions.set(j, null);
}
} else {
/*
* Insert move from register to stack just after the beginning of the
* interval.
*/
assert interval.isEndMarker() || interval.spillDefinitionPos() >= opId : "invalid order";
assert interval.isEndMarker() || (interval.isSplitParent() && interval.spillState() == SpillState.StoreAtDefinition) : "invalid interval";
while (!interval.isEndMarker() && interval.spillDefinitionPos() == opId) {
if (!interval.canMaterialize()) {
if (!insertionBuffer.initialized()) {
/*
* prepare insertion buffer (appended when all instructions
* in the block are processed)
*/
insertionBuffer.init(instructions);
}
AllocatableValue fromLocation = interval.location();
AllocatableValue toLocation = LinearScan.canonicalSpillOpr(interval);
if (!fromLocation.equals(toLocation)) {
assert isRegister(fromLocation) : "from operand must be a register but is: " + fromLocation + " toLocation=" + toLocation + " spillState=" + interval.spillState();
assert isStackSlotValue(toLocation) : "to operand must be a stack slot";
LIRInstruction move = allocator.getSpillMoveFactory().createMove(toLocation, fromLocation);
insertionBuffer.append(j + 1, move);
move.setComment(res, "LSRAEliminateSpillMove: store at definition");
if (debug.isLogEnabled()) {
debug.log("inserting move after definition of interval %d to stack slot %s at opId %d", interval.operandNumber, interval.spillSlot(), opId);
}
}
}
interval = interval.next;
}
}
}
if (insertionBuffer.initialized()) {
insertionBuffer.finish();
}
}
}
assert interval.isEndMarker() : "missed an interval";
}
}
use of org.graalvm.compiler.lir.LIRInsertionBuffer in project graal by oracle.
the class TraceLinearScanEliminateSpillMovePhase method eliminateSpillMoves.
// called once before assignment of register numbers
@SuppressWarnings("try")
private static void eliminateSpillMoves(TraceLinearScan allocator, boolean shouldEliminateSpillMoves, TraceBuilderResult traceBuilderResult, LIRGenerationResult res) {
DebugContext debug = allocator.getDebug();
try (Indent indent = debug.logAndIndent("Eliminating unnecessary spill moves: Trace%d", traceBuilderResult.getTraceForBlock(allocator.blockAt(0)).getId())) {
allocator.sortIntervalsBySpillPos();
/*
* collect all intervals that must be stored after their definition. The list is sorted
* by Interval.spillDefinitionPos.
*/
TraceInterval interval = allocator.createUnhandledListBySpillPos(spilledIntervals);
if (Assertions.detailedAssertionsEnabled(allocator.getOptions())) {
checkIntervals(debug, interval);
}
if (debug.isLogEnabled()) {
try (Indent indent2 = debug.logAndIndent("Sorted intervals")) {
for (TraceInterval i = interval; i != null; i = i.next) {
debug.log("%5d: %s", i.spillDefinitionPos(), i);
}
}
}
LIRInsertionBuffer insertionBuffer = new LIRInsertionBuffer();
for (AbstractBlockBase<?> block : allocator.sortedBlocks()) {
try (Indent indent1 = debug.logAndIndent("Handle %s", block)) {
ArrayList<LIRInstruction> instructions = allocator.getLIR().getLIRforBlock(block);
int numInst = instructions.size();
int lastOpId = -1;
// iterate all instructions of the block.
for (int j = 0; j < numInst; j++) {
LIRInstruction op = instructions.get(j);
int opId = op.id();
try (Indent indent2 = debug.logAndIndent("%5d %s", opId, op)) {
if (opId == -1) {
MoveOp move = MoveOp.asMoveOp(op);
/*
* Remove move from register to stack if the stack slot is
* guaranteed to be correct. Only moves that have been inserted by
* LinearScan can be removed.
*/
if (shouldEliminateSpillMoves && canEliminateSpillMove(allocator, block, move, lastOpId)) {
/*
* Move target is a stack slot that is always correct, so
* eliminate instruction.
*/
if (debug.isLogEnabled()) {
if (ValueMoveOp.isValueMoveOp(op)) {
ValueMoveOp vmove = ValueMoveOp.asValueMoveOp(op);
debug.log("eliminating move from interval %s to %s in block %s", vmove.getInput(), vmove.getResult(), block);
} else {
LoadConstantOp load = LoadConstantOp.asLoadConstantOp(op);
debug.log("eliminating constant load from %s to %s in block %s", load.getConstant(), load.getResult(), block);
}
}
// null-instructions are deleted by assignRegNum
instructions.set(j, null);
}
} else {
lastOpId = opId;
// interval.spillDefinitionPos() >= opId : "invalid order";
assert interval == TraceInterval.EndMarker || (interval.isSplitParent() && SpillState.IN_MEMORY.contains(interval.spillState())) : "invalid interval";
while (interval != TraceInterval.EndMarker && interval.spillDefinitionPos() == opId) {
debug.log("handle %s", interval);
if (!interval.canMaterialize() && interval.spillState() != SpillState.StartInMemory) {
AllocatableValue fromLocation = interval.getSplitChildAtOpId(opId, OperandMode.DEF).location();
AllocatableValue toLocation = allocator.canonicalSpillOpr(interval);
if (!fromLocation.equals(toLocation)) {
if (!insertionBuffer.initialized()) {
/*
* prepare insertion buffer (appended when all
* instructions in the block are processed)
*/
insertionBuffer.init(instructions);
}
assert isRegister(fromLocation) : "from operand must be a register but is: " + fromLocation + " toLocation=" + toLocation + " spillState=" + interval.spillState();
assert isStackSlotValue(toLocation) : "to operand must be a stack slot";
LIRInstruction move = allocator.getSpillMoveFactory().createMove(toLocation, fromLocation);
insertionBuffer.append(j + 1, move);
move.setComment(res, "TraceLSRAEliminateSpillMove: spill def pos");
if (debug.isLogEnabled()) {
debug.log("inserting move after definition of interval %d to stack slot %s at opId %d", interval.operandNumber, interval.spillSlot(), opId);
}
}
}
interval = interval.next;
}
}
}
}
if (insertionBuffer.initialized()) {
insertionBuffer.finish();
}
}
}
assert interval == TraceInterval.EndMarker : "missed an interval";
}
}
Aggregations