use of org.graalvm.compiler.debug.DebugContext in project graal by oracle.
the class LinearScanWalker method activateCurrent.
// allocate a physical register or memory location to an interval
@Override
@SuppressWarnings("try")
protected boolean activateCurrent(Interval interval) {
boolean result = true;
DebugContext debug = allocator.getDebug();
try (Indent indent = debug.logAndIndent("activating interval %s, splitParent: %d", interval, interval.splitParent().operandNumber)) {
final Value operand = interval.operand;
if (interval.location() != null && isStackSlotValue(interval.location())) {
// used for method parameters
if (debug.isLogEnabled()) {
debug.log("interval has spill slot assigned (method parameter) . split it before first use");
}
splitStackInterval(interval);
result = false;
} else {
if (interval.location() == null) {
// (this is the normal case for most intervals)
if (debug.isLogEnabled()) {
debug.log("normal allocation of register");
}
// assign same spill slot to non-intersecting intervals
combineSpilledIntervals(interval);
initVarsForAlloc(interval);
if (noAllocationPossible(interval) || !allocFreeRegister(interval)) {
// no empty register available.
// split and spill another interval so that this interval gets a register
allocLockedRegister(interval);
}
// spilled intervals need not be move to active-list
if (!isRegister(interval.location())) {
result = false;
}
}
}
// load spilled values that become active from stack slot to register
if (interval.insertMoveWhenActivated()) {
assert interval.isSplitChild();
assert interval.currentSplitChild() != null;
assert !interval.currentSplitChild().operand.equals(operand) : "cannot insert move between same interval";
if (debug.isLogEnabled()) {
debug.log("Inserting move from interval %d to %d because insertMoveWhenActivated is set", interval.currentSplitChild().operandNumber, interval.operandNumber);
}
insertMove(interval.from(), interval.currentSplitChild(), interval);
}
interval.makeCurrentSplitChild();
}
// true = interval is moved to active list
return result;
}
use of org.graalvm.compiler.debug.DebugContext in project graal by oracle.
the class LinearScanWalker method allocFreeRegister.
@SuppressWarnings("try")
boolean allocFreeRegister(Interval interval) {
DebugContext debug = allocator.getDebug();
try (Indent indent = debug.logAndIndent("trying to find free register for %s", interval)) {
initUseLists(true);
freeExcludeActiveFixed();
freeExcludeActiveAny();
freeCollectInactiveFixed(interval);
freeCollectInactiveAny(interval);
// freeCollectUnhandled(fixedKind, cur);
assert unhandledLists.get(RegisterBinding.Fixed).isEndMarker() : "must not have unhandled fixed intervals because all fixed intervals have a use at position 0";
// ignored safely
if (debug.isLogEnabled()) {
// Enable this logging to see all register states
try (Indent indent2 = debug.logAndIndent("state of registers:")) {
for (Register register : availableRegs) {
int i = register.number;
debug.log("reg %d: usePos: %d", register.number, usePos[i]);
}
}
}
Register hint = null;
Interval locationHint = interval.locationHint(true);
if (locationHint != null && locationHint.location() != null && isRegister(locationHint.location())) {
hint = asRegister(locationHint.location());
if (debug.isLogEnabled()) {
debug.log("hint register %d from interval %s", hint.number, locationHint);
}
}
assert interval.location() == null : "register already assigned to interval";
// the register must be free at least until this position
int regNeededUntil = interval.from() + 1;
int intervalTo = interval.to();
boolean needSplit = false;
int splitPos = -1;
Register reg = null;
Register minFullReg = null;
Register maxPartialReg = null;
for (Register availableReg : availableRegs) {
int number = availableReg.number;
if (usePos[number] >= intervalTo) {
// this register is free for the full interval
if (minFullReg == null || availableReg.equals(hint) || (usePos[number] < usePos[minFullReg.number] && !minFullReg.equals(hint))) {
minFullReg = availableReg;
}
} else if (usePos[number] > regNeededUntil) {
// this register is at least free until regNeededUntil
if (maxPartialReg == null || availableReg.equals(hint) || (usePos[number] > usePos[maxPartialReg.number] && !maxPartialReg.equals(hint))) {
maxPartialReg = availableReg;
}
}
}
if (minFullReg != null) {
reg = minFullReg;
} else if (maxPartialReg != null) {
needSplit = true;
reg = maxPartialReg;
} else {
return false;
}
splitPos = usePos[reg.number];
interval.assignLocation(reg.asValue(interval.kind()));
if (debug.isLogEnabled()) {
debug.log("selected register %d", reg.number);
}
assert splitPos > 0 : "invalid splitPos";
if (needSplit) {
// register not available for full interval, so split it
splitWhenPartialRegisterAvailable(interval, splitPos);
}
// only return true if interval is completely assigned
return true;
}
}
use of org.graalvm.compiler.debug.DebugContext in project graal by oracle.
the class LinearScanWalker method splitForSpilling.
// split an interval at the optimal position between minSplitPos and
// maxSplitPos in two parts:
// 1) the left part has already a location assigned
// 2) the right part is always on the stack and therefore ignored in further processing
@SuppressWarnings("try")
void splitForSpilling(Interval interval) {
DebugContext debug = allocator.getDebug();
// calculate allowed range of splitting position
int maxSplitPos = currentPosition;
int previousUsage = interval.previousUsage(RegisterPriority.ShouldHaveRegister, maxSplitPos);
if (previousUsage == currentPosition) {
/*
* If there is a usage with ShouldHaveRegister priority at the current position fall
* back to MustHaveRegister priority. This only happens if register priority was
* downgraded to MustHaveRegister in #allocLockedRegister.
*/
previousUsage = interval.previousUsage(RegisterPriority.MustHaveRegister, maxSplitPos);
}
int minSplitPos = Math.max(previousUsage + 1, interval.from());
try (Indent indent = debug.logAndIndent("splitting and spilling interval %s between %d and %d", interval, minSplitPos, maxSplitPos)) {
assert interval.state == State.Active : "why spill interval that is not active?";
assert interval.from() <= minSplitPos : "cannot split before start of interval";
assert minSplitPos <= maxSplitPos : "invalid order";
assert maxSplitPos < interval.to() : "cannot split at end end of interval";
assert currentPosition < interval.to() : "interval must not end before current position";
if (minSplitPos == interval.from()) {
try (Indent indent2 = debug.logAndIndent("spilling entire interval because split pos is at beginning of interval (use positions: %d)", interval.usePosList().size())) {
assert interval.firstUsage(RegisterPriority.MustHaveRegister) > currentPosition : String.format("interval %s must not have use position before currentPosition %d", interval, currentPosition);
allocator.assignSpillSlot(interval);
handleSpillSlot(interval);
changeSpillState(interval, minSplitPos);
// Also kick parent intervals out of register to memory when they have no use
// position. This avoids short interval in register surrounded by intervals in
// memory . avoid useless moves from memory to register and back
Interval parent = interval;
while (parent != null && parent.isSplitChild()) {
parent = parent.getSplitChildBeforeOpId(parent.from());
if (isRegister(parent.location())) {
if (parent.firstUsage(RegisterPriority.ShouldHaveRegister) == Integer.MAX_VALUE) {
// parent is never used, so kick it out of its assigned register
if (debug.isLogEnabled()) {
debug.log("kicking out interval %d out of its register because it is never used", parent.operandNumber);
}
allocator.assignSpillSlot(parent);
handleSpillSlot(parent);
} else {
// do not go further back because the register is actually used by
// the interval
parent = null;
}
}
}
}
} else {
// search optimal split pos, split interval and spill only the right hand part
int optimalSplitPos = findOptimalSplitPos(interval, minSplitPos, maxSplitPos, false);
assert minSplitPos <= optimalSplitPos && optimalSplitPos <= maxSplitPos : "out of range";
assert optimalSplitPos < interval.to() : "cannot split at end of interval";
assert optimalSplitPos >= interval.from() : "cannot split before start of interval";
if (!allocator.isBlockBegin(optimalSplitPos)) {
// move position before actual instruction (odd opId)
optimalSplitPos = (optimalSplitPos - 1) | 1;
}
try (Indent indent2 = debug.logAndIndent("splitting at position %d", optimalSplitPos)) {
assert allocator.isBlockBegin(optimalSplitPos) || ((optimalSplitPos & 1) == 1) : "split pos must be odd when not on block boundary";
assert !allocator.isBlockBegin(optimalSplitPos) || ((optimalSplitPos & 1) == 0) : "split pos must be even on block boundary";
Interval spilledPart = interval.split(optimalSplitPos, allocator);
allocator.assignSpillSlot(spilledPart);
handleSpillSlot(spilledPart);
changeSpillState(spilledPart, optimalSplitPos);
if (!allocator.isBlockBegin(optimalSplitPos)) {
if (debug.isLogEnabled()) {
debug.log("inserting move from interval %d to %d", interval.operandNumber, spilledPart.operandNumber);
}
insertMove(optimalSplitPos, interval, spilledPart);
}
// the currentSplitChild is needed later when moves are inserted for reloading
assert spilledPart.currentSplitChild() == interval : "overwriting wrong currentSplitChild";
spilledPart.makeCurrentSplitChild();
if (debug.isLogEnabled()) {
debug.log("left interval: %s", interval.logString(allocator));
debug.log("spilled interval : %s", spilledPart.logString(allocator));
}
}
}
}
}
use of org.graalvm.compiler.debug.DebugContext in project graal by oracle.
the class LinearScanWalker method allocLockedRegister.
// Split an Interval and spill it to memory so that cur can be placed in a register
@SuppressWarnings("try")
void allocLockedRegister(Interval interval) {
DebugContext debug = allocator.getDebug();
try (Indent indent = debug.logAndIndent("alloc locked register: need to split and spill to get register for %s", interval)) {
// the register must be free at least until this position
int firstUsage = interval.firstUsage(RegisterPriority.MustHaveRegister);
int firstShouldHaveUsage = interval.firstUsage(RegisterPriority.ShouldHaveRegister);
int regNeededUntil = Math.min(firstUsage, interval.from() + 1);
int intervalTo = interval.to();
assert regNeededUntil >= 0 && regNeededUntil < Integer.MAX_VALUE : "interval has no use";
Register reg;
Register ignore;
/*
* In the common case we don't spill registers that have _any_ use position that is
* closer than the next use of the current interval, but if we can't spill the current
* interval we weaken this strategy and also allow spilling of intervals that have a
* non-mandatory requirements (no MustHaveRegister use position).
*/
for (RegisterPriority registerPriority = RegisterPriority.LiveAtLoopEnd; true; registerPriority = RegisterPriority.MustHaveRegister) {
// collect current usage of registers
initUseLists(false);
spillExcludeActiveFixed();
// spillBlockUnhandledFixed(cur);
assert unhandledLists.get(RegisterBinding.Fixed).isEndMarker() : "must not have unhandled fixed intervals because all fixed intervals have a use at position 0";
spillBlockInactiveFixed(interval);
spillCollectActiveAny(registerPriority);
spillCollectInactiveAny(interval);
if (debug.isLogEnabled()) {
printRegisterState();
}
reg = null;
ignore = interval.location() != null && isRegister(interval.location()) ? asRegister(interval.location()) : null;
for (Register availableReg : availableRegs) {
int number = availableReg.number;
if (availableReg.equals(ignore)) {
// this register must be ignored
} else if (usePos[number] > regNeededUntil) {
if (reg == null || (usePos[number] > usePos[reg.number])) {
reg = availableReg;
}
}
}
int regUsePos = (reg == null ? 0 : usePos[reg.number]);
if (regUsePos <= firstShouldHaveUsage) {
if (debug.isLogEnabled()) {
debug.log("able to spill current interval. firstUsage(register): %d, usePos: %d", firstUsage, regUsePos);
}
if (firstUsage <= interval.from() + 1) {
if (registerPriority.equals(RegisterPriority.LiveAtLoopEnd)) {
/*
* Tool of last resort: we can not spill the current interval so we try
* to spill an active interval that has a usage but do not require a
* register.
*/
debug.log("retry with register priority must have register");
continue;
}
String description = generateOutOfRegErrorMsg(interval, firstUsage, availableRegs);
/*
* assign a reasonable register and do a bailout in product mode to avoid
* errors
*/
allocator.assignSpillSlot(interval);
debug.dump(DebugContext.INFO_LEVEL, allocator.getLIR(), description);
allocator.printIntervals(description);
throw new OutOfRegistersException("LinearScan: no register found", description);
}
splitAndSpillInterval(interval);
return;
}
break;
}
boolean needSplit = blockPos[reg.number] <= intervalTo;
int splitPos = blockPos[reg.number];
if (debug.isLogEnabled()) {
debug.log("decided to use register %d", reg.number);
}
assert splitPos > 0 : "invalid splitPos";
assert needSplit || splitPos > interval.from() : "splitting interval at from";
interval.assignLocation(reg.asValue(interval.kind()));
if (needSplit) {
// register not available for full interval : so split it
splitWhenPartialRegisterAvailable(interval, splitPos);
}
// perform splitting and spilling for all affected intervals
splitAndSpillIntersectingIntervals(reg);
return;
}
}
use of org.graalvm.compiler.debug.DebugContext in project graal by oracle.
the class MoveResolver method insertMove.
private LIRInstruction insertMove(Interval fromInterval, Interval toInterval) {
assert !fromInterval.operand.equals(toInterval.operand) : "from and to interval equal: " + fromInterval;
assert LIRKind.verifyMoveKinds(toInterval.kind(), fromInterval.kind(), allocator.getRegisterAllocationConfig()) : "move between different types";
assert insertIdx != -1 : "must setup insert position first";
LIRInstruction move = createMove(fromInterval.operand, toInterval.operand, fromInterval.location(), toInterval.location());
insertionBuffer.append(insertIdx, move);
DebugContext debug = allocator.getDebug();
if (debug.isLogEnabled()) {
debug.log("insert move from %s to %s at %d", fromInterval, toInterval, insertIdx);
}
return move;
}
Aggregations