1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2025-04-11 04:37:29 +00:00

Working on #371 simpler live range calculation

This commit is contained in:
jespergravgaard 2020-03-16 07:57:56 +01:00
parent 20b596d98b
commit 4f8609ea72
16 changed files with 3344 additions and 87 deletions

View File

@ -221,6 +221,20 @@ public class CallGraph {
}
}
public int getCallDepth(ProcedureRef procedureRef) {
final Collection<CallBlock.Call> callers = getCallers(procedureRef);
int maxCallDepth = 1;
for(CallBlock.Call caller : callers) {
final ScopeRef callStatementScope = caller.getCallStatementScope();
if(callStatementScope instanceof ProcedureRef) {
int callerDepth = getCallDepth((ProcedureRef) callStatementScope)+1;
if(callerDepth>maxCallDepth)
maxCallDepth = callerDepth;
}
}
return maxCallDepth;
}
/**
* A block in the call graph, matching a scope in the program.

View File

@ -99,7 +99,8 @@ public class Program {
/** The register weight of all variables describing how much the variable would theoretically gain from being in a register. PASS 3-5 (CACHED ON-DEMAND) */
private VariableRegisterWeights variableRegisterWeights;
/** Enable live ranges per call path optimization, which limits memory usage and code, but takes a lot of compile time. */
private boolean enableLiveRangeCallPath = true;
//private boolean enableLiveRangeCallPath = true;
private boolean enableLiveRangeCallPath = false;
public Program() {
this.scope = new ProgramScope();

View File

@ -22,11 +22,11 @@ public class PassNCalcLiveRangeVariables extends PassNCalcBase<LiveRangeVariable
@Override
public LiveRangeVariables calculate() {
calculateProcedureReferencedVars();
final Map<ProcedureRef, Collection<VariableRef>> procedureReferencedVars = calculateProcedureReferencedVars(getProgram());
LiveRangeVariables liveRanges = new LiveRangeVariables(getProgram());
boolean propagating;
do {
propagating = calculateLiveRanges(liveRanges);
propagating = calculateLiveRanges(liveRanges, procedureReferencedVars);
getProgram().setLiveRangeVariables(liveRanges);
if(getLog().isVerboseLiveRanges()) {
getLog().append("Propagating live ranges...");
@ -37,22 +37,18 @@ public class PassNCalcLiveRangeVariables extends PassNCalcBase<LiveRangeVariable
return liveRanges;
}
/** Variables referenced inside each procedure and all it's sub-calls. */
private Map<ProcedureRef, Collection<VariableRef>> procedureReferencedVars;
/**
* Calculate the variables referenced inside each procedure and all it's sub-calls.
*/
private void calculateProcedureReferencedVars() {
VariableReferenceInfos referenceInfo = getProgram().getVariableReferenceInfos();
Collection<Procedure> allProcedures = getScope().getAllProcedures(true);
public static Map<ProcedureRef, Collection<VariableRef>> calculateProcedureReferencedVars(Program program) {
VariableReferenceInfos referenceInfo = program.getVariableReferenceInfos();
Collection<Procedure> allProcedures = program.getScope().getAllProcedures(true);
Map<ProcedureRef, Collection<VariableRef>> procReferencedVars = new LinkedHashMap<>();
for(Procedure procedure : allProcedures) {
Collection<VariableRef> referencedVars = referenceInfo.getReferencedVars(procedure.getRef().getLabelRef());
procReferencedVars.put(procedure.getRef(), referencedVars);
}
this.procedureReferencedVars = procReferencedVars;
return procReferencedVars;
}
/**
@ -77,7 +73,7 @@ public class PassNCalcLiveRangeVariables extends PassNCalcBase<LiveRangeVariable
* @param liveRanges The live ranges to propagate.
* @return true if any live ranges was modified. false if no modification was performed (and the propagation is complete)
*/
private boolean calculateLiveRanges(LiveRangeVariables liveRanges) {
private boolean calculateLiveRanges(LiveRangeVariables liveRanges, Map<ProcedureRef, Collection<VariableRef>> procedureReferencedVars) {
VariableReferenceInfos referenceInfo = getProgram().getVariableReferenceInfos();
boolean modified = false;
LiveRangeVariables.LiveRangeVariablesByStatement liveRangeVariablesByStatement = liveRanges.getLiveRangeVariablesByStatement();
@ -110,25 +106,25 @@ public class PassNCalcLiveRangeVariables extends PassNCalcBase<LiveRangeVariable
for(VariableRef aliveVar : aliveNextStmt) {
// Add all variables to previous that are not used inside the method
if(procUsed.contains(aliveVar)) {
boolean addUsedVar = liveRanges.addAlive(aliveVar, previousStmt.getStatementIdx());
modified |= addUsedVar;
if(addUsedVar && getLog().isVerboseLiveRanges()) {
boolean added = liveRanges.addAlive(aliveVar, previousStmt.getStatementIdx());
modified |= added;
if(added && getLog().isVerboseLiveRanges()) {
getLog().append("Propagated alive var used in method into method " + aliveVar + " to " + previousStmt.getStatement());
}
}
}
} else if(PreviousStatement.Type.SKIP_METHOD.equals(previousStmt.getType())) {
// Add all vars that the method does not use
// Add all vars from next statement that the method does not use
StatementCalling call = (StatementCalling) nextStmt;
ProcedureRef procedure = call.getProcedure();
Collection<VariableRef> procUsed = procedureReferencedVars.get(procedure);
Collection<VariableRef> procReferenced = procedureReferencedVars.get(procedure);
// The call statement has no used or defined by itself so only work with the alive vars
for(VariableRef aliveVar : aliveNextStmt) {
// Add all variables to previous that are not used inside the method
if(!procUsed.contains(aliveVar) && !definedNextStmt.contains(aliveVar)) {
boolean addSkipVar = liveRanges.addAlive(aliveVar, previousStmt.getStatementIdx());
modified |= addSkipVar;
if(addSkipVar && getLog().isVerboseLiveRanges()) {
if(!procReferenced.contains(aliveVar) && !definedNextStmt.contains(aliveVar)) {
boolean added = liveRanges.addAlive(aliveVar, previousStmt.getStatementIdx());
modified |= added;
if(added && getLog().isVerboseLiveRanges()) {
getLog().append("Propagated alive var unused in method by skipping call " + aliveVar + " to " + previousStmt.getStatement());
}
}
@ -137,15 +133,15 @@ public class PassNCalcLiveRangeVariables extends PassNCalcBase<LiveRangeVariable
// Add all alive variables to previous that are used inside the method
ControlFlowBlock procBlock = getProgram().getStatementInfos().getBlock(nextStmt);
Procedure procedure = (Procedure) getProgram().getScope().getSymbol(procBlock.getLabel());
Collection<VariableRef> procUsed = procedureReferencedVars.get(procedure.getRef());
Collection<VariableRef> procReferenced = procedureReferencedVars.get(procedure.getRef());
// The call statement has no used or defined by itself so only work with the alive vars
for(VariableRef aliveVar : aliveNextStmt) {
// Add all variables to previous that are used inside the method
if(procUsed.contains(aliveVar)) {
if(procReferenced.contains(aliveVar)) {
if(!definedNextStmt.contains(aliveVar)) {
boolean usedVar = liveRanges.addAlive(aliveVar, previousStmt.getStatementIdx());
modified |= usedVar;
if(usedVar && getLog().isVerboseLiveRanges()) {
boolean added = liveRanges.addAlive(aliveVar, previousStmt.getStatementIdx());
modified |= added;
if(added && getLog().isVerboseLiveRanges()) {
getLog().append("Propagated alive used in method out of method " + aliveVar + " to " + previousStmt.getStatement());
}
}
@ -217,28 +213,28 @@ public class PassNCalcLiveRangeVariables extends PassNCalcBase<LiveRangeVariable
* as multiple blocks may jump to the same block resulting in multiple stmt/nextstmt pairs.
* Calls to methods are also given special consideration.
* The following illustrates how the different types of previous statements work.
<pre>
b1: {
[1] stmt; prev: b3[1]/NORMAL, b2[2]/NORMAL
[2] call b4; prev: b1[1]/SKIP_METHOD, b4[3]/LAST_IN_METHOD
[3] stmt; prev: b1[2]/NORMAL
goto b2;
}
b2: {
[1] stmt; prev: b1[3]/NORMAL
[2] if(x) b1; prev: b2[1]/NORMAL
goto b3;
}
b3: {
[1] stmt; prev: b2[2]/NORMAL
goto b1
}
b4: {
[1] stmt; prev: b1[1]/BEFORE_METHOD
[2] stmt; prev: b4[1]/NORMAL
[3] return; prev: b4[3]/NORMAL
}
</pre> *
* <pre>
* b1: {
* [1] stmt; prev: b3[1]/NORMAL, b2[2]/NORMAL
* [2] call b4; prev: b1[1]/SKIP_METHOD, b4[3]/LAST_IN_METHOD
* [3] stmt; prev: b1[2]/NORMAL
* goto b2;
* }
* b2: {
* [1] stmt; prev: b1[3]/NORMAL
* [2] if(x) b1; prev: b2[1]/NORMAL
* goto b3;
* }
* b3: {
* [1] stmt; prev: b2[2]/NORMAL
* goto b1
* }
* b4: {
* [1] stmt; prev: b1[1]/BEFORE_METHOD
* [2] stmt; prev: b4[1]/NORMAL
* [3] return; prev: b4[3]/NORMAL
* }
* </pre> *
*
* @param statement The statement to find previous for
* @return statement(s) executed just before the passed statement
@ -246,7 +242,7 @@ public class PassNCalcLiveRangeVariables extends PassNCalcBase<LiveRangeVariable
private Collection<PreviousStatement> getPreviousStatements(Statement statement) {
ArrayList<PreviousStatement> previousStatements = new ArrayList<>();
// Find the statement(s) just before the current statement (disregarding if the current statement is a call - this will be handled later)
Collection<Statement> precedingStatements = getPrecedingStatement(statement);
Collection<Statement> precedingStatements = getPrecedingStatement(statement, getGraph(), getProgram().getStatementInfos());
if(statement instanceof StatementCalling) {
// Add the statement(s) just before the call
for(Statement precedingStatement : precedingStatements) {
@ -257,8 +253,8 @@ public class PassNCalcLiveRangeVariables extends PassNCalcBase<LiveRangeVariable
ProcedureRef procedure = call.getProcedure();
LabelRef procedureReturnBlock = procedure.getReturnBlock();
ControlFlowBlock returnBlock = getProgram().getGraph().getBlock(procedureReturnBlock);
if(returnBlock!=null) {
Collection<Statement> lastStatements = getLastInBlock(returnBlock);
if(returnBlock != null) {
Collection<Statement> lastStatements = getLastInBlock(returnBlock, getGraph());
for(Statement lastStatement : lastStatements) {
previousStatements.add(new PreviousStatement(lastStatement, PreviousStatement.Type.LAST_IN_METHOD));
}
@ -276,7 +272,7 @@ public class PassNCalcLiveRangeVariables extends PassNCalcBase<LiveRangeVariable
Collection<CallGraph.CallBlock.Call> callers = getProgram().getCallGraph().getCallers((ProcedureRef) block.getScope());
for(CallGraph.CallBlock.Call call : callers) {
Statement callStmt = getProgram().getStatementInfos().getStatement(call.getCallStatementIdx());
Collection<Statement> precedingCallStmt = getPrecedingStatement(callStmt);
Collection<Statement> precedingCallStmt = getPrecedingStatement(callStmt, getGraph(), getProgram().getStatementInfos());
for(Statement precedingCall : precedingCallStmt) {
previousStatements.add(new PreviousStatement(precedingCall, PreviousStatement.Type.BEFORE_METHOD));
}
@ -315,10 +311,10 @@ public class PassNCalcLiveRangeVariables extends PassNCalcBase<LiveRangeVariable
* Multiple statements are returned if the current statement is the first in a block with multiple predecessor blocks.
* Zero statements are returned if the current statement is the first statement in the program or the first statement in a method.
*/
private Collection<Statement> getPrecedingStatement(Statement statement) {
static Collection<Statement> getPrecedingStatement(Statement statement, ControlFlowGraph graph, StatementInfos statementInfos) {
Statement previousStmt = null;
Statement prev = null;
ControlFlowBlock block = getProgram().getStatementInfos().getBlock(statement);
ControlFlowBlock block = statementInfos.getBlock(statement);
List<Statement> statements = block.getStatements();
for(Statement stmt : statements) {
if(statement.getIndex().equals(stmt.getIndex())) {
@ -332,7 +328,7 @@ public class PassNCalcLiveRangeVariables extends PassNCalcBase<LiveRangeVariable
previous = Arrays.asList(previousStmt);
} else {
// Current is first in a block - look in predecessor blocks
previous = getLastInPredecessors(block);
previous = getLastInPredecessors(block, graph);
}
return previous;
}
@ -344,13 +340,13 @@ public class PassNCalcLiveRangeVariables extends PassNCalcBase<LiveRangeVariable
* @return The last statement(s). May contain multiple statements if the block is empty and has multiple predecessors.
* This method never traces back through calls, so the result may also be empty (if the block is an empty method).
*/
private Collection<Statement> getLastInBlock(ControlFlowBlock block) {
private static Collection<Statement> getLastInBlock(ControlFlowBlock block, ControlFlowGraph graph) {
List<Statement> statements = block.getStatements();
if(statements.size() > 0) {
return Arrays.asList(statements.get(statements.size() - 1));
} else {
// Trace back through direct/conditional predecessors (not calls)
return getLastInPredecessors(block);
return getLastInPredecessors(block, graph);
}
}
@ -361,12 +357,12 @@ public class PassNCalcLiveRangeVariables extends PassNCalcBase<LiveRangeVariable
* @return The last statement(s). May contain multiple statements if the block is empty and has multiple predecessors.
* This method never traces back through calls, so the result may also be empty (if the block is an empty method).
*/
private Collection<Statement> getLastInPredecessors(ControlFlowBlock block) {
List<ControlFlowBlock> predecessors = getProgram().getGraph().getPredecessors(block);
private static Collection<Statement> getLastInPredecessors(ControlFlowBlock block, ControlFlowGraph graph) {
List<ControlFlowBlock> predecessors = graph.getPredecessors(block);
ArrayList<Statement> last = new ArrayList<>();
for(ControlFlowBlock predecessor : predecessors) {
if(block.getLabel().equals(predecessor.getDefaultSuccessor()) || block.getLabel().equals(predecessor.getConditionalSuccessor())) {
last.addAll(getLastInBlock(predecessor));
last.addAll(getLastInBlock(predecessor, graph));
}
}
return last;

View File

@ -22,30 +22,44 @@ public class PassNCalcLiveRangesEffectiveSimple extends PassNCalcBase<LiveRangeV
final LiveRangeVariables liveRangeVariables = getProgram().getLiveRangeVariables();
final CallGraph callGraph = getProgram().getCallGraph();
final LiveRangeVariables.LiveRangeVariablesByStatement liveRangeVariablesByStatement = liveRangeVariables.getLiveRangeVariablesByStatement();
final Map<ProcedureRef, Collection<VariableRef>> procedureReferencedVars = PassNCalcLiveRangeVariables.calculateProcedureReferencedVars(getProgram());
// Find all alive vars from the recursive callers of each procedure
Map<ProcedureRef, Collection<VariableRef>> callersAlive = new LinkedHashMap<>();
// All variables alive outside a call. This is variables that are alive across the call that are not touched by the procedure (or any sub-procedure).
Map<CallGraph.CallBlock.Call, Collection<VariableRef>> aliveOutsideCalls = new LinkedHashMap<>();
// All variables alive outside all calls to a procedure. This is variables that are alive across a call that are not touched by the procedure (or any sub-procedure).
Map<ProcedureRef, Collection<VariableRef>> aliveOutsideProcedures = new LinkedHashMap<>();
Collection<Procedure> procedures = getProgram().getScope().getAllProcedures(true);
for(Procedure procedure : procedures) {
Collection<VariableRef> procCallersAlive = new LinkedHashSet<>();
Collection<VariableRef> aliveOutsideProcedure = new LinkedHashSet<>();
final Collection<CallGraph.CallBlock.Call> callers = callGraph.getRecursiveCallers(procedure.getRef());
for(CallGraph.CallBlock.Call caller : callers) {
final Integer callStatementIdx = caller.getCallStatementIdx();
final List<VariableRef> callStatementAlive = liveRangeVariables.getAlive(callStatementIdx);
procCallersAlive.addAll(callStatementAlive);
final Statement callStatement = getGraph().getStatementByIndex(callStatementIdx);
final Collection<Statement> precedingStatements = PassNCalcLiveRangeVariables.getPrecedingStatement(callStatement, getGraph(), getProgram().getStatementInfos());
Collection<VariableRef> aliveOutsideCall = new LinkedHashSet<>();
for(Statement precedingStatement : precedingStatements) {
final List<VariableRef> precedingStatementAlive = liveRangeVariables.getAlive(precedingStatement.getIndex());
aliveOutsideCall.addAll(precedingStatementAlive);
}
final Collection<VariableRef> procedureReferenced = procedureReferencedVars.get(procedure.getRef());
aliveOutsideCall.removeAll(procedureReferenced);
aliveOutsideCalls.put(caller, aliveOutsideCall);
aliveOutsideProcedure.addAll(aliveOutsideCall);
}
callersAlive.put(procedure.getRef(), procCallersAlive);
aliveOutsideProcedures.put(procedure.getRef(), aliveOutsideProcedure);
}
// Find all alive variables for all statements by adding the alive from all recursive callers
// Find all alive variables for all statements by adding the everything alive outside each call
final LinkedHashMap<Integer, Collection<VariableRef>> statementAliveEffective = new LinkedHashMap<>();
for(ControlFlowBlock block : getGraph().getAllBlocks()) {
final Procedure procedure = block.getProcedure(getProgram());
for(Statement statement : block.getStatements()) {
final Collection<VariableRef> aliveStmt = new LinkedHashSet<>();
aliveStmt.addAll(liveRangeVariablesByStatement.getAlive(statement.getIndex()));
final List<VariableRef> aliveStatement = liveRangeVariablesByStatement.getAlive(statement.getIndex());
aliveStmt.addAll(aliveStatement);
if(procedure!=null) {
aliveStmt.addAll(callersAlive.get(procedure.getRef()));
final Collection<VariableRef> aliveOutsideProcedure = aliveOutsideProcedures.get(procedure.getRef());
aliveStmt.addAll(aliveOutsideProcedure);
}
statementAliveEffective.put(statement.getIndex(), aliveStmt);
}

View File

@ -7,6 +7,8 @@ import dk.camelot64.kickc.model.statements.StatementConditionalJump;
import dk.camelot64.kickc.model.statements.StatementPhiBlock;
import dk.camelot64.kickc.model.values.*;
import java.util.Collection;
/**
* Finds register weights for all variables.
* <p>
@ -28,8 +30,10 @@ public class PassNCalcVariableRegisterWeight extends PassNCalcBase<VariableRegis
@Override
public VariableRegisterWeights calculate() {
NaturalLoopSet loopSet = getProgram().getLoopSet();
final CallGraph callGraph = getProgram().getCallGraph();
LiveRangeVariables liveRangeVariables = getProgram().getLiveRangeVariables();
VariableRegisterWeights variableRegisterWeights = new VariableRegisterWeights();
final StatementInfos statementInfos = getProgram().getStatementInfos();
for(ControlFlowBlock block : getProgram().getGraph().getAllBlocks()) {
for(Statement statement : block.getStatements()) {
@ -39,48 +43,46 @@ public class PassNCalcVariableRegisterWeight extends PassNCalcBase<VariableRegis
VariableRef philVariable = phiVariable.getVariable();
for(StatementPhiBlock.PhiRValue phiRValue : phiVariable.getValues()) {
if(phiRValue.getrValue() instanceof VariableRef) {
double w = addWeight(philVariable, phiRValue.getPredecessor(), variableRegisterWeights, loopSet, liveRangeVariables);
//log.append("Definition of " + philVariable + " w+:" + w + " - [" + statement.getIndex()+"]");
addWeight(philVariable, phiRValue.getPredecessor(), liveRangeVariables, loopSet, callGraph, statementInfos, variableRegisterWeights);
}
}
// Add weights for each usage of a variable
for(StatementPhiBlock.PhiRValue phiRValue : phiVariable.getValues()) {
addUsageWeightRValue(phiRValue.getrValue(), statement, phiRValue.getPredecessor(), variableRegisterWeights, loopSet, liveRangeVariables);
addUsageWeightRValue(phiRValue.getrValue(), phiRValue.getPredecessor(), variableRegisterWeights, loopSet, callGraph, statementInfos, liveRangeVariables);
}
}
} else if(statement instanceof StatementAssignment) {
// Add weights for the definition of the variable
addUsageWeightRValue(((StatementAssignment) statement).getlValue(), statement, block.getLabel(), variableRegisterWeights, loopSet, liveRangeVariables);
addUsageWeightRValue(((StatementAssignment) statement).getlValue(), block.getLabel(), variableRegisterWeights, loopSet, callGraph, statementInfos, liveRangeVariables);
// Add weights for each usage of variables
addUsageWeightRValue(((StatementAssignment) statement).getrValue1(), statement, block.getLabel(), variableRegisterWeights, loopSet, liveRangeVariables);
addUsageWeightRValue(((StatementAssignment) statement).getrValue2(), statement, block.getLabel(), variableRegisterWeights, loopSet, liveRangeVariables);
addUsageWeightRValue(((StatementAssignment) statement).getrValue1(), block.getLabel(), variableRegisterWeights, loopSet, callGraph, statementInfos, liveRangeVariables);
addUsageWeightRValue(((StatementAssignment) statement).getrValue2(), block.getLabel(), variableRegisterWeights, loopSet, callGraph, statementInfos, liveRangeVariables);
} else if(statement instanceof StatementConditionalJump) {
// Add weights for each usage of variables
addUsageWeightRValue(((StatementConditionalJump) statement).getrValue1(), statement, block.getLabel(), variableRegisterWeights, loopSet, liveRangeVariables);
addUsageWeightRValue(((StatementConditionalJump) statement).getrValue2(), statement, block.getLabel(), variableRegisterWeights, loopSet, liveRangeVariables);
addUsageWeightRValue(((StatementConditionalJump) statement).getrValue1(), block.getLabel(), variableRegisterWeights, loopSet, callGraph, statementInfos, liveRangeVariables);
addUsageWeightRValue(((StatementConditionalJump) statement).getrValue2(), block.getLabel(), variableRegisterWeights, loopSet, callGraph, statementInfos, liveRangeVariables);
}
}
}
return variableRegisterWeights;
}
private static void addUsageWeightRValue(Value rValue, Statement statement, LabelRef block, VariableRegisterWeights variableRegisterWeights, NaturalLoopSet loopSet, LiveRangeVariables liveRangeVariables) {
private static void addUsageWeightRValue(Value rValue, LabelRef block, VariableRegisterWeights variableRegisterWeights, NaturalLoopSet loopSet, CallGraph callGraph, StatementInfos statementInfos, LiveRangeVariables liveRangeVariables) {
if(rValue instanceof VariableRef) {
double w = addWeight((VariableRef) rValue, block, variableRegisterWeights, loopSet, liveRangeVariables);
//log.append("Usage of " + rValue + " w+:" + w + " - [" + statement.getIndex()+"]");
addWeight((VariableRef) rValue, block, liveRangeVariables, loopSet, callGraph, statementInfos, variableRegisterWeights);
} else if(rValue instanceof PointerDereferenceSimple) {
addUsageWeightRValue(((PointerDereferenceSimple) rValue).getPointer(), statement, block, variableRegisterWeights, loopSet, liveRangeVariables);
addUsageWeightRValue(((PointerDereferenceSimple) rValue).getPointer(), block, variableRegisterWeights, loopSet, callGraph, statementInfos, liveRangeVariables);
} else if(rValue instanceof PointerDereferenceIndexed) {
addUsageWeightRValue(((PointerDereferenceIndexed) rValue).getPointer(), statement, block, variableRegisterWeights, loopSet, liveRangeVariables);
addUsageWeightRValue(((PointerDereferenceIndexed) rValue).getIndex(), statement, block, variableRegisterWeights, loopSet, liveRangeVariables);
addUsageWeightRValue(((PointerDereferenceIndexed) rValue).getPointer(), block, variableRegisterWeights, loopSet, callGraph, statementInfos, liveRangeVariables);
addUsageWeightRValue(((PointerDereferenceIndexed) rValue).getIndex(), block, variableRegisterWeights, loopSet, callGraph, statementInfos, liveRangeVariables);
}
}
private static double addWeight(VariableRef variable, LabelRef block, VariableRegisterWeights variableRegisterWeights, NaturalLoopSet loopSet, LiveRangeVariables liveRangeVariables) {
int depth = loopSet.getMaxLoopDepth(block);
double w = 1.0 + Math.pow(10.0, depth);
private static double addWeight(VariableRef variable, LabelRef block, LiveRangeVariables liveRangeVariables, NaturalLoopSet loopSet, CallGraph callGraph, StatementInfos statementInfos, VariableRegisterWeights variableRegisterWeights) {
int loopCallDepth = getLoopCallDepth(block, loopSet, callGraph,statementInfos);
double w = 1.0 + Math.pow(10.0, loopCallDepth);
LiveRange liveRange = liveRangeVariables.getLiveRange(variable);
double s = liveRange==null?0.0:liveRange.size();
double s = liveRange == null ? 0.0 : liveRange.size();
if(s < 0.01) {
s = 0.1;
}
@ -88,5 +90,29 @@ public class PassNCalcVariableRegisterWeight extends PassNCalcBase<VariableRegis
return w / s;
}
/**
* Get the loop/call depth of a block.
*
* @param block The block to examine
* @param loopSet The loop set
* @param callGraph The call graph
* @return The combined loop/call depth
*/
private static int getLoopCallDepth(LabelRef block, NaturalLoopSet loopSet, CallGraph callGraph, StatementInfos statementInfos) {
final String procedureName = block.getFullName().contains("::") ? block.getScopeNames() : block.getFullName();
final ProcedureRef procedureRef = new ProcedureRef(procedureName);
final Collection<CallGraph.CallBlock.Call> callers = callGraph.getCallers(procedureRef);
int maxCallDepth = 0;
for(CallGraph.CallBlock.Call caller : callers) {
final Integer callStatementIdx = caller.getCallStatementIdx();
final ControlFlowBlock callBlock = statementInfos.getBlock(callStatementIdx);
int callDepth = getLoopCallDepth(callBlock.getLabel(), loopSet, callGraph, statementInfos) + 1;
if(callDepth > maxCallDepth)
maxCallDepth = callDepth;
}
int loopDepth = loopSet.getMaxLoopDepth(block);
return maxCallDepth + loopDepth;
}
}

View File

@ -3385,6 +3385,16 @@ public class TestPrograms {
compileAndCompare("constantmin");
}
@Test
public void testLiveRange2() throws IOException, URISyntaxException {
compileAndCompare("liverange-2",log().verboseUplift().verboseLiveRanges().verboseLoopAnalysis());
}
@Test
public void testLiveRange1() throws IOException, URISyntaxException {
compileAndCompare("liverange-1",log().verboseUplift().verboseLiveRanges());
}
@Test
public void testLiveRange() throws IOException, URISyntaxException {
compileAndCompare("liverange");

View File

@ -0,0 +1,15 @@
// Test propagation of live ranges back over PHI-calls
// The idx-variable is alive between the two calls to out() - but not before the first call.
void main() {
out('c');
out('m');
}
const char* SCREEN = 0x0400;
char idx = 0;
void out(char c) {
SCREEN[idx++] = c;
}

View File

@ -0,0 +1,18 @@
// Test effective live range and register allocation
void main() {
for(char a: 0..100 ) {
for( char b: 0..100 ) {
for( char c: 0..100 ) {
char ca = c+a;
print(b, ca);
}
}
}
}
const char* SCREEN = 0x0400;
void print(char b, char ca) {
SCREEN[b] = ca;
}

View File

@ -0,0 +1,26 @@
// Test propagation of live ranges back over PHI-calls
// The idx-variable is alive between the two calls to out() - but not before the first call.
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
.label SCREEN = $400
main: {
// out('c')
ldx #0
lda #'c'
jsr out
// out('m')
lda #'m'
jsr out
// }
rts
}
// out(byte register(A) c)
out: {
// SCREEN[idx++] = c
sta SCREEN,x
// SCREEN[idx++] = c;
inx
// }
rts
}

View File

@ -0,0 +1,33 @@
@begin: scope:[] from
[0] phi()
to:@1
@1: scope:[] from @begin
[1] phi()
[2] call main
to:@end
@end: scope:[] from @1
[3] phi()
(void()) main()
main: scope:[main] from @1
[4] phi()
[5] call out
to:main::@1
main::@1: scope:[main] from main
[6] phi()
[7] call out
to:main::@return
main::@return: scope:[main] from main::@1
[8] return
to:@return
(void()) out((byte) out::c)
out: scope:[out] from main main::@1
[9] (byte) idx#10 ← phi( main/(byte) 0 main::@1/(byte) idx#11 )
[9] (byte) out::c#2 ← phi( main/(byte) 'c' main::@1/(byte) 'm' )
[10] *((const byte*) SCREEN + (byte) idx#10) ← (byte) out::c#2
[11] (byte) idx#11 ← ++ (byte) idx#10
to:out::@return
out::@return: scope:[out] from out
[12] return
to:@return

View File

@ -0,0 +1,846 @@
CONTROL FLOW GRAPH SSA
@begin: scope:[] from
to:@1
(void()) main()
main: scope:[main] from @2
(byte) idx#13 ← phi( @2/(byte) idx#14 )
(byte) out::c#0 ← (byte) 'c'
call out
to:main::@1
main::@1: scope:[main] from main
(byte) idx#7 ← phi( main/(byte) idx#5 )
(byte) idx#0 ← (byte) idx#7
(byte) out::c#1 ← (byte) 'm'
call out
to:main::@2
main::@2: scope:[main] from main::@1
(byte) idx#8 ← phi( main::@1/(byte) idx#5 )
(byte) idx#1 ← (byte) idx#8
to:main::@return
main::@return: scope:[main] from main::@2
(byte) idx#9 ← phi( main::@2/(byte) idx#1 )
(byte) idx#2 ← (byte) idx#9
return
to:@return
@1: scope:[] from @begin
(byte) idx#3 ← (byte) 0
to:@2
(void()) out((byte) out::c)
out: scope:[out] from main main::@1
(byte) idx#10 ← phi( main/(byte) idx#13 main::@1/(byte) idx#0 )
(byte) out::c#2 ← phi( main/(byte) out::c#0 main::@1/(byte) out::c#1 )
*((const byte*) SCREEN + (byte) idx#10) ← (byte) out::c#2
(byte) idx#4 ← ++ (byte) idx#10
to:out::@return
out::@return: scope:[out] from out
(byte) idx#11 ← phi( out/(byte) idx#4 )
(byte) idx#5 ← (byte) idx#11
return
to:@return
@2: scope:[] from @1
(byte) idx#14 ← phi( @1/(byte) idx#3 )
call main
to:@3
@3: scope:[] from @2
(byte) idx#12 ← phi( @2/(byte) idx#2 )
(byte) idx#6 ← (byte) idx#12
to:@end
@end: scope:[] from @3
SYMBOL TABLE SSA
(label) @1
(label) @2
(label) @3
(label) @begin
(label) @end
(const byte*) SCREEN = (byte*)(number) $400
(byte) idx
(byte) idx#0
(byte) idx#1
(byte) idx#10
(byte) idx#11
(byte) idx#12
(byte) idx#13
(byte) idx#14
(byte) idx#2
(byte) idx#3
(byte) idx#4
(byte) idx#5
(byte) idx#6
(byte) idx#7
(byte) idx#8
(byte) idx#9
(void()) main()
(label) main::@1
(label) main::@2
(label) main::@return
(void()) out((byte) out::c)
(label) out::@return
(byte) out::c
(byte) out::c#0
(byte) out::c#1
(byte) out::c#2
Simplifying constant pointer cast (byte*) 1024
Successful SSA optimization PassNCastSimplification
Alias (byte) idx#0 = (byte) idx#7
Alias (byte) idx#1 = (byte) idx#8 (byte) idx#9 (byte) idx#2
Alias (byte) idx#11 = (byte) idx#4 (byte) idx#5
Alias (byte) idx#14 = (byte) idx#3
Alias (byte) idx#12 = (byte) idx#6
Successful SSA optimization Pass2AliasElimination
Identical Phi Values (byte) idx#13 (byte) idx#14
Identical Phi Values (byte) idx#0 (byte) idx#11
Identical Phi Values (byte) idx#1 (byte) idx#11
Identical Phi Values (byte) idx#12 (byte) idx#1
Successful SSA optimization Pass2IdenticalPhiElimination
Constant (const byte) out::c#0 = 'c'
Constant (const byte) out::c#1 = 'm'
Constant (const byte) idx#14 = 0
Successful SSA optimization Pass2ConstantIdentification
Inlining constant with var siblings (const byte) out::c#0
Inlining constant with var siblings (const byte) out::c#1
Inlining constant with var siblings (const byte) idx#14
Constant inlined out::c#0 = (byte) 'c'
Constant inlined out::c#1 = (byte) 'm'
Constant inlined idx#14 = (byte) 0
Successful SSA optimization Pass2ConstantInlining
Adding NOP phi() at start of @begin
Adding NOP phi() at start of @1
Adding NOP phi() at start of @2
Adding NOP phi() at start of @3
Adding NOP phi() at start of @end
Adding NOP phi() at start of main
Adding NOP phi() at start of main::@2
CALL GRAPH
Calls in [] to main:3
Calls in [main] to out:7 out:9
Adding empty live range for unused variable idx#15
Adding used var idx#11 to [7] call out
Adding empty live range for unused variable out::c#2
Adding empty live range for unused variable idx#10
Adding used phi var idx#15 to [8] idx#15 ← idx#11
Adding used var idx#10 to [12] idx#10 ← phi( main/0 main::@1/idx#15 )
[12] out::c#2 ← phi( main/'c' main::@1/'m' )
Adding used var out::c#2 to [12] idx#10 ← phi( main/0 main::@1/idx#15 )
[12] out::c#2 ← phi( main/'c' main::@1/'m' )
Adding used var idx#10 to [13] *(SCREEN + idx#10) ← out::c#2
Propagating live ranges...
CONTROL FLOW GRAPH - LIVE RANGES IN PROGRESS
@begin: scope:[] from
[0] phi() [ ]
to:@1
@1: scope:[] from @begin
[1] phi() [ ]
to:@2
@2: scope:[] from @1
[2] phi() [ ]
[3] call main [ ]
to:@3
@3: scope:[] from @2
[4] phi() [ ]
to:@end
@end: scope:[] from @3
[5] phi() [ ]
(void()) main()
main: scope:[main] from @2
[6] phi() [ ]
[7] call out [ idx#11 ]
to:main::@1
main::@1: scope:[main] from main
[8] (byte) idx#15 ← (byte) idx#11 [ idx#15 ]
[9] call out [ ]
to:main::@2
main::@2: scope:[main] from main::@1
[10] phi() [ ]
to:main::@return
main::@return: scope:[main] from main::@2
[11] return [ ]
to:@return
(void()) out((byte) out::c)
out: scope:[out] from main main::@1
[12] (byte) idx#10 ← phi( main/(byte) 0 main::@1/(byte) idx#15 ) [ out::c#2 idx#10 ]
[12] (byte) out::c#2 ← phi( main/(byte) 'c' main::@1/(byte) 'm' ) [ out::c#2 idx#10 ]
[13] *((const byte*) SCREEN + (byte) idx#10) ← (byte) out::c#2 [ idx#10 ]
[14] (byte) idx#11 ← ++ (byte) idx#10 [ ]
to:out::@return
out::@return: scope:[out] from out
[15] return [ ]
to:@return
Propagated alive var used in method into method idx#11 to [15] return
Propagating live ranges...
CONTROL FLOW GRAPH - LIVE RANGES IN PROGRESS
@begin: scope:[] from
[0] phi() [ ]
to:@1
@1: scope:[] from @begin
[1] phi() [ ]
to:@2
@2: scope:[] from @1
[2] phi() [ ]
[3] call main [ ]
to:@3
@3: scope:[] from @2
[4] phi() [ ]
to:@end
@end: scope:[] from @3
[5] phi() [ ]
(void()) main()
main: scope:[main] from @2
[6] phi() [ ]
[7] call out [ idx#11 ]
to:main::@1
main::@1: scope:[main] from main
[8] (byte) idx#15 ← (byte) idx#11 [ idx#15 ]
[9] call out [ ]
to:main::@2
main::@2: scope:[main] from main::@1
[10] phi() [ ]
to:main::@return
main::@return: scope:[main] from main::@2
[11] return [ ]
to:@return
(void()) out((byte) out::c)
out: scope:[out] from main main::@1
[12] (byte) idx#10 ← phi( main/(byte) 0 main::@1/(byte) idx#15 ) [ out::c#2 idx#10 ]
[12] (byte) out::c#2 ← phi( main/(byte) 'c' main::@1/(byte) 'm' ) [ out::c#2 idx#10 ]
[13] *((const byte*) SCREEN + (byte) idx#10) ← (byte) out::c#2 [ idx#10 ]
[14] (byte) idx#11 ← ++ (byte) idx#10 [ ]
to:out::@return
out::@return: scope:[out] from out
[15] return [ idx#11 ]
to:@return
Propagated alive var idx#11 to [14] idx#11 ← ++ idx#10
Propagating live ranges...
CONTROL FLOW GRAPH - LIVE RANGES IN PROGRESS
@begin: scope:[] from
[0] phi() [ ]
to:@1
@1: scope:[] from @begin
[1] phi() [ ]
to:@2
@2: scope:[] from @1
[2] phi() [ ]
[3] call main [ ]
to:@3
@3: scope:[] from @2
[4] phi() [ ]
to:@end
@end: scope:[] from @3
[5] phi() [ ]
(void()) main()
main: scope:[main] from @2
[6] phi() [ ]
[7] call out [ idx#11 ]
to:main::@1
main::@1: scope:[main] from main
[8] (byte) idx#15 ← (byte) idx#11 [ idx#15 ]
[9] call out [ ]
to:main::@2
main::@2: scope:[main] from main::@1
[10] phi() [ ]
to:main::@return
main::@return: scope:[main] from main::@2
[11] return [ ]
to:@return
(void()) out((byte) out::c)
out: scope:[out] from main main::@1
[12] (byte) idx#10 ← phi( main/(byte) 0 main::@1/(byte) idx#15 ) [ out::c#2 idx#10 ]
[12] (byte) out::c#2 ← phi( main/(byte) 'c' main::@1/(byte) 'm' ) [ out::c#2 idx#10 ]
[13] *((const byte*) SCREEN + (byte) idx#10) ← (byte) out::c#2 [ idx#10 ]
[14] (byte) idx#11 ← ++ (byte) idx#10 [ idx#11 ]
to:out::@return
out::@return: scope:[out] from out
[15] return [ idx#11 ]
to:@return
Propagating live ranges...
CONTROL FLOW GRAPH - LIVE RANGES IN PROGRESS
@begin: scope:[] from
[0] phi() [ ]
to:@1
@1: scope:[] from @begin
[1] phi() [ ]
to:@2
@2: scope:[] from @1
[2] phi() [ ]
[3] call main [ ]
to:@3
@3: scope:[] from @2
[4] phi() [ ]
to:@end
@end: scope:[] from @3
[5] phi() [ ]
(void()) main()
main: scope:[main] from @2
[6] phi() [ ]
[7] call out [ idx#11 ]
to:main::@1
main::@1: scope:[main] from main
[8] (byte) idx#15 ← (byte) idx#11 [ idx#15 ]
[9] call out [ ]
to:main::@2
main::@2: scope:[main] from main::@1
[10] phi() [ ]
to:main::@return
main::@return: scope:[main] from main::@2
[11] return [ ]
to:@return
(void()) out((byte) out::c)
out: scope:[out] from main main::@1
[12] (byte) idx#10 ← phi( main/(byte) 0 main::@1/(byte) idx#15 ) [ out::c#2 idx#10 ]
[12] (byte) out::c#2 ← phi( main/(byte) 'c' main::@1/(byte) 'm' ) [ out::c#2 idx#10 ]
[13] *((const byte*) SCREEN + (byte) idx#10) ← (byte) out::c#2 [ idx#10 ]
[14] (byte) idx#11 ← ++ (byte) idx#10 [ idx#11 ]
to:out::@return
out::@return: scope:[out] from out
[15] return [ idx#11 ]
to:@return
Created 2 initial phi equivalence classes
Coalesced [8] idx#15 ← idx#11
Coalesced down to 2 phi equivalence classes
Culled Empty Block (label) @1
Culled Empty Block (label) @3
Culled Empty Block (label) main::@2
Renumbering block @2 to @1
Adding NOP phi() at start of @begin
Adding NOP phi() at start of @1
Adding NOP phi() at start of @end
Adding NOP phi() at start of main
Adding NOP phi() at start of main::@1
Adding empty live range for unused variable out::c#2
Adding empty live range for unused variable idx#10
Adding used phi var idx#11 to [6] phi()
Adding used var idx#10 to [9] idx#10 ← phi( main/0 main::@1/idx#11 )
[9] out::c#2 ← phi( main/'c' main::@1/'m' )
Adding used var out::c#2 to [9] idx#10 ← phi( main/0 main::@1/idx#11 )
[9] out::c#2 ← phi( main/'c' main::@1/'m' )
Adding used var idx#10 to [10] *(SCREEN + idx#10) ← out::c#2
Propagating live ranges...
CONTROL FLOW GRAPH - LIVE RANGES IN PROGRESS
@begin: scope:[] from
[0] phi() [ ]
to:@1
@1: scope:[] from @begin
[1] phi() [ ]
[2] call main [ ]
to:@end
@end: scope:[] from @1
[3] phi() [ ]
(void()) main()
main: scope:[main] from @1
[4] phi() [ ]
[5] call out [ ]
to:main::@1
main::@1: scope:[main] from main
[6] phi() [ idx#11 ]
[7] call out [ ]
to:main::@return
main::@return: scope:[main] from main::@1
[8] return [ ]
to:@return
(void()) out((byte) out::c)
out: scope:[out] from main main::@1
[9] (byte) idx#10 ← phi( main/(byte) 0 main::@1/(byte) idx#11 ) [ out::c#2 idx#10 ]
[9] (byte) out::c#2 ← phi( main/(byte) 'c' main::@1/(byte) 'm' ) [ out::c#2 idx#10 ]
[10] *((const byte*) SCREEN + (byte) idx#10) ← (byte) out::c#2 [ idx#10 ]
[11] (byte) idx#11 ← ++ (byte) idx#10 [ ]
to:out::@return
out::@return: scope:[out] from out
[12] return [ ]
to:@return
Propagated alive var idx#11 to [5] call out
Propagating live ranges...
CONTROL FLOW GRAPH - LIVE RANGES IN PROGRESS
@begin: scope:[] from
[0] phi() [ ]
to:@1
@1: scope:[] from @begin
[1] phi() [ ]
[2] call main [ ]
to:@end
@end: scope:[] from @1
[3] phi() [ ]
(void()) main()
main: scope:[main] from @1
[4] phi() [ ]
[5] call out [ idx#11 ]
to:main::@1
main::@1: scope:[main] from main
[6] phi() [ idx#11 ]
[7] call out [ ]
to:main::@return
main::@return: scope:[main] from main::@1
[8] return [ ]
to:@return
(void()) out((byte) out::c)
out: scope:[out] from main main::@1
[9] (byte) idx#10 ← phi( main/(byte) 0 main::@1/(byte) idx#11 ) [ out::c#2 idx#10 ]
[9] (byte) out::c#2 ← phi( main/(byte) 'c' main::@1/(byte) 'm' ) [ out::c#2 idx#10 ]
[10] *((const byte*) SCREEN + (byte) idx#10) ← (byte) out::c#2 [ idx#10 ]
[11] (byte) idx#11 ← ++ (byte) idx#10 [ ]
to:out::@return
out::@return: scope:[out] from out
[12] return [ ]
to:@return
Propagated alive var used in method into method idx#11 to [12] return
Propagating live ranges...
CONTROL FLOW GRAPH - LIVE RANGES IN PROGRESS
@begin: scope:[] from
[0] phi() [ ]
to:@1
@1: scope:[] from @begin
[1] phi() [ ]
[2] call main [ ]
to:@end
@end: scope:[] from @1
[3] phi() [ ]
(void()) main()
main: scope:[main] from @1
[4] phi() [ ]
[5] call out [ idx#11 ]
to:main::@1
main::@1: scope:[main] from main
[6] phi() [ idx#11 ]
[7] call out [ ]
to:main::@return
main::@return: scope:[main] from main::@1
[8] return [ ]
to:@return
(void()) out((byte) out::c)
out: scope:[out] from main main::@1
[9] (byte) idx#10 ← phi( main/(byte) 0 main::@1/(byte) idx#11 ) [ out::c#2 idx#10 ]
[9] (byte) out::c#2 ← phi( main/(byte) 'c' main::@1/(byte) 'm' ) [ out::c#2 idx#10 ]
[10] *((const byte*) SCREEN + (byte) idx#10) ← (byte) out::c#2 [ idx#10 ]
[11] (byte) idx#11 ← ++ (byte) idx#10 [ ]
to:out::@return
out::@return: scope:[out] from out
[12] return [ idx#11 ]
to:@return
Propagated alive var idx#11 to [11] idx#11 ← ++ idx#10
Propagating live ranges...
CONTROL FLOW GRAPH - LIVE RANGES IN PROGRESS
@begin: scope:[] from
[0] phi() [ ]
to:@1
@1: scope:[] from @begin
[1] phi() [ ]
[2] call main [ ]
to:@end
@end: scope:[] from @1
[3] phi() [ ]
(void()) main()
main: scope:[main] from @1
[4] phi() [ ]
[5] call out [ idx#11 ]
to:main::@1
main::@1: scope:[main] from main
[6] phi() [ idx#11 ]
[7] call out [ ]
to:main::@return
main::@return: scope:[main] from main::@1
[8] return [ ]
to:@return
(void()) out((byte) out::c)
out: scope:[out] from main main::@1
[9] (byte) idx#10 ← phi( main/(byte) 0 main::@1/(byte) idx#11 ) [ out::c#2 idx#10 ]
[9] (byte) out::c#2 ← phi( main/(byte) 'c' main::@1/(byte) 'm' ) [ out::c#2 idx#10 ]
[10] *((const byte*) SCREEN + (byte) idx#10) ← (byte) out::c#2 [ idx#10 ]
[11] (byte) idx#11 ← ++ (byte) idx#10 [ idx#11 ]
to:out::@return
out::@return: scope:[out] from out
[12] return [ idx#11 ]
to:@return
Propagating live ranges...
CONTROL FLOW GRAPH - LIVE RANGES IN PROGRESS
@begin: scope:[] from
[0] phi() [ ]
to:@1
@1: scope:[] from @begin
[1] phi() [ ]
[2] call main [ ]
to:@end
@end: scope:[] from @1
[3] phi() [ ]
(void()) main()
main: scope:[main] from @1
[4] phi() [ ]
[5] call out [ idx#11 ]
to:main::@1
main::@1: scope:[main] from main
[6] phi() [ idx#11 ]
[7] call out [ ]
to:main::@return
main::@return: scope:[main] from main::@1
[8] return [ ]
to:@return
(void()) out((byte) out::c)
out: scope:[out] from main main::@1
[9] (byte) idx#10 ← phi( main/(byte) 0 main::@1/(byte) idx#11 ) [ out::c#2 idx#10 ]
[9] (byte) out::c#2 ← phi( main/(byte) 'c' main::@1/(byte) 'm' ) [ out::c#2 idx#10 ]
[10] *((const byte*) SCREEN + (byte) idx#10) ← (byte) out::c#2 [ idx#10 ]
[11] (byte) idx#11 ← ++ (byte) idx#10 [ idx#11 ]
to:out::@return
out::@return: scope:[out] from out
[12] return [ idx#11 ]
to:@return
FINAL CONTROL FLOW GRAPH
@begin: scope:[] from
[0] phi() [ ] ( [ ] )
to:@1
@1: scope:[] from @begin
[1] phi() [ ] ( [ ] )
[2] call main [ ] ( [ ] )
to:@end
@end: scope:[] from @1
[3] phi() [ ] ( [ ] )
(void()) main()
main: scope:[main] from @1
[4] phi() [ ] ( [ ] )
[5] call out [ idx#11 ] ( [ idx#11 ] )
to:main::@1
main::@1: scope:[main] from main
[6] phi() [ idx#11 ] ( [ idx#11 ] )
[7] call out [ ] ( [ ] )
to:main::@return
main::@return: scope:[main] from main::@1
[8] return [ ] ( [ ] )
to:@return
(void()) out((byte) out::c)
out: scope:[out] from main main::@1
[9] (byte) idx#10 ← phi( main/(byte) 0 main::@1/(byte) idx#11 ) [ out::c#2 idx#10 ] ( [ out::c#2 idx#10 ] )
[9] (byte) out::c#2 ← phi( main/(byte) 'c' main::@1/(byte) 'm' ) [ out::c#2 idx#10 ] ( [ out::c#2 idx#10 ] )
[10] *((const byte*) SCREEN + (byte) idx#10) ← (byte) out::c#2 [ idx#10 ] ( [ idx#10 ] )
[11] (byte) idx#11 ← ++ (byte) idx#10 [ idx#11 ] ( [ idx#11 ] )
to:out::@return
out::@return: scope:[out] from out
[12] return [ idx#11 ] ( [ idx#11 ] )
to:@return
VARIABLE REGISTER WEIGHTS
(byte) idx
(byte) idx#10 106.5
(byte) idx#11 28.0
(void()) main()
(void()) out((byte) out::c)
(byte) out::c
(byte) out::c#2 101.0
Initial phi equivalence classes
[ out::c#2 ]
[ idx#10 idx#11 ]
Complete equivalence classes
[ out::c#2 ]
[ idx#10 idx#11 ]
Allocated zp[1]:2 [ out::c#2 ]
Allocated zp[1]:3 [ idx#10 idx#11 ]
INITIAL ASM
Target platform is c64basic / MOS6502X
// File Comments
// Test propagation of live ranges back over PHI-calls
// The idx-variable is alive between the two calls to out() - but not before the first call.
// Upstart
.pc = $801 "Basic"
:BasicUpstart(__bbegin)
.pc = $80d "Program"
// Global Constants & labels
.label SCREEN = $400
.label idx = 3
// @begin
__bbegin:
// [1] phi from @begin to @1 [phi:@begin->@1]
__b1_from___bbegin:
jmp __b1
// @1
__b1:
// [2] call main
// [4] phi from @1 to main [phi:@1->main]
main_from___b1:
jsr main
// [3] phi from @1 to @end [phi:@1->@end]
__bend_from___b1:
jmp __bend
// @end
__bend:
// main
main: {
// [5] call out
// [9] phi from main to out [phi:main->out]
out_from_main:
// [9] phi (byte) idx#10 = (byte) 0 [phi:main->out#0] -- vbuz1=vbuc1
lda #0
sta.z idx
// [9] phi (byte) out::c#2 = (byte) 'c' [phi:main->out#1] -- vbuz1=vbuc1
lda #'c'
sta.z out.c
jsr out
// [6] phi from main to main::@1 [phi:main->main::@1]
__b1_from_main:
jmp __b1
// main::@1
__b1:
// [7] call out
// [9] phi from main::@1 to out [phi:main::@1->out]
out_from___b1:
// [9] phi (byte) idx#10 = (byte) idx#11 [phi:main::@1->out#0] -- register_copy
// [9] phi (byte) out::c#2 = (byte) 'm' [phi:main::@1->out#1] -- vbuz1=vbuc1
lda #'m'
sta.z out.c
jsr out
jmp __breturn
// main::@return
__breturn:
// [8] return
rts
}
// out
// out(byte zp(2) c)
out: {
.label c = 2
// [10] *((const byte*) SCREEN + (byte) idx#10) ← (byte) out::c#2 -- pbuc1_derefidx_vbuz1=vbuz2
lda.z c
ldy.z idx
sta SCREEN,y
// [11] (byte) idx#11 ← ++ (byte) idx#10 -- vbuz1=_inc_vbuz1
inc.z idx
jmp __breturn
// out::@return
__breturn:
// [12] return
rts
}
// File Data
REGISTER UPLIFT POTENTIAL REGISTERS
Potential registers zp[1]:2 [ out::c#2 ] : zp[1]:2 , reg byte a , reg byte x , reg byte y ,
Potential registers zp[1]:3 [ idx#10 idx#11 ] : zp[1]:3 , reg byte a , reg byte x , reg byte y ,
REGISTER UPLIFT SCOPES
Uplift Scope [] 134.5: zp[1]:3 [ idx#10 idx#11 ]
Uplift Scope [out] 101: zp[1]:2 [ out::c#2 ]
Uplift Scope [main]
Uplift attempt [] 76 allocation: zp[1]:3 [ idx#10 idx#11 ]
Uplift attempt [] clobber allocation: reg byte a [ idx#10 idx#11 ]
Uplift attempt [] 67 allocation: reg byte x [ idx#10 idx#11 ]
Uplift attempt [] 67 allocation: reg byte y [ idx#10 idx#11 ]
Uplifting [] best 67 combination reg byte x [ idx#10 idx#11 ]
Uplift attempt [out] 67 allocation: zp[1]:2 [ out::c#2 ]
Uplift attempt [out] 58 allocation: reg byte a [ out::c#2 ]
Overlap register reg byte x in [9] (byte) idx#10 ← phi( main/(byte) 0 main::@1/(byte) idx#11 ) [ out::c#2 idx#10 ] ( [ out::c#2 idx#10 ] )
[9] (byte) out::c#2 ← phi( main/(byte) 'c' main::@1/(byte) 'm' ) [ out::c#2 idx#10 ] ( [ out::c#2 idx#10 ] )
Uplift attempt [out] overlapping allocation: reg byte x [ out::c#2 ]
Uplift attempt [out] 60 allocation: reg byte y [ out::c#2 ]
Uplifting [out] best 58 combination reg byte a [ out::c#2 ]
Uplift attempt [main] 58 allocation:
Uplifting [main] best 58 combination
ASSEMBLER BEFORE OPTIMIZATION
// File Comments
// Test propagation of live ranges back over PHI-calls
// The idx-variable is alive between the two calls to out() - but not before the first call.
// Upstart
.pc = $801 "Basic"
:BasicUpstart(__bbegin)
.pc = $80d "Program"
// Global Constants & labels
.label SCREEN = $400
// @begin
__bbegin:
// [1] phi from @begin to @1 [phi:@begin->@1]
__b1_from___bbegin:
jmp __b1
// @1
__b1:
// [2] call main
// [4] phi from @1 to main [phi:@1->main]
main_from___b1:
jsr main
// [3] phi from @1 to @end [phi:@1->@end]
__bend_from___b1:
jmp __bend
// @end
__bend:
// main
main: {
// [5] call out
// [9] phi from main to out [phi:main->out]
out_from_main:
// [9] phi (byte) idx#10 = (byte) 0 [phi:main->out#0] -- vbuxx=vbuc1
ldx #0
// [9] phi (byte) out::c#2 = (byte) 'c' [phi:main->out#1] -- vbuaa=vbuc1
lda #'c'
jsr out
// [6] phi from main to main::@1 [phi:main->main::@1]
__b1_from_main:
jmp __b1
// main::@1
__b1:
// [7] call out
// [9] phi from main::@1 to out [phi:main::@1->out]
out_from___b1:
// [9] phi (byte) idx#10 = (byte) idx#11 [phi:main::@1->out#0] -- register_copy
// [9] phi (byte) out::c#2 = (byte) 'm' [phi:main::@1->out#1] -- vbuaa=vbuc1
lda #'m'
jsr out
jmp __breturn
// main::@return
__breturn:
// [8] return
rts
}
// out
// out(byte register(A) c)
out: {
// [10] *((const byte*) SCREEN + (byte) idx#10) ← (byte) out::c#2 -- pbuc1_derefidx_vbuxx=vbuaa
sta SCREEN,x
// [11] (byte) idx#11 ← ++ (byte) idx#10 -- vbuxx=_inc_vbuxx
inx
jmp __breturn
// out::@return
__breturn:
// [12] return
rts
}
// File Data
ASSEMBLER OPTIMIZATIONS
Removing instruction jmp __b1
Removing instruction jmp __bend
Removing instruction jmp __b1
Removing instruction jmp __breturn
Removing instruction jmp __breturn
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction __b1_from___bbegin:
Removing instruction __b1:
Removing instruction main_from___b1:
Removing instruction __bend_from___b1:
Removing instruction __b1_from_main:
Removing instruction out_from___b1:
Succesful ASM optimization Pass5RedundantLabelElimination
Removing instruction __bend:
Removing instruction out_from_main:
Removing instruction __b1:
Removing instruction __breturn:
Removing instruction __breturn:
Succesful ASM optimization Pass5UnusedLabelElimination
Updating BasicUpstart to call main directly
Removing instruction jsr main
Succesful ASM optimization Pass5SkipBegin
Removing instruction __bbegin:
Succesful ASM optimization Pass5UnusedLabelElimination
FINAL SYMBOL TABLE
(label) @1
(label) @begin
(label) @end
(const byte*) SCREEN = (byte*) 1024
(byte) idx
(byte) idx#10 reg byte x 106.5
(byte) idx#11 reg byte x 28.0
(void()) main()
(label) main::@1
(label) main::@return
(void()) out((byte) out::c)
(label) out::@return
(byte) out::c
(byte) out::c#2 reg byte a 101.0
reg byte a [ out::c#2 ]
reg byte x [ idx#10 idx#11 ]
FINAL ASSEMBLER
Score: 37
// File Comments
// Test propagation of live ranges back over PHI-calls
// The idx-variable is alive between the two calls to out() - but not before the first call.
// Upstart
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
// Global Constants & labels
.label SCREEN = $400
// @begin
// [1] phi from @begin to @1 [phi:@begin->@1]
// @1
// [2] call main
// [4] phi from @1 to main [phi:@1->main]
// [3] phi from @1 to @end [phi:@1->@end]
// @end
// main
main: {
// out('c')
// [5] call out
// [9] phi from main to out [phi:main->out]
// [9] phi (byte) idx#10 = (byte) 0 [phi:main->out#0] -- vbuxx=vbuc1
ldx #0
// [9] phi (byte) out::c#2 = (byte) 'c' [phi:main->out#1] -- vbuaa=vbuc1
lda #'c'
jsr out
// [6] phi from main to main::@1 [phi:main->main::@1]
// main::@1
// out('m')
// [7] call out
// [9] phi from main::@1 to out [phi:main::@1->out]
// [9] phi (byte) idx#10 = (byte) idx#11 [phi:main::@1->out#0] -- register_copy
// [9] phi (byte) out::c#2 = (byte) 'm' [phi:main::@1->out#1] -- vbuaa=vbuc1
lda #'m'
jsr out
// main::@return
// }
// [8] return
rts
}
// out
// out(byte register(A) c)
out: {
// SCREEN[idx++] = c
// [10] *((const byte*) SCREEN + (byte) idx#10) ← (byte) out::c#2 -- pbuc1_derefidx_vbuxx=vbuaa
sta SCREEN,x
// SCREEN[idx++] = c;
// [11] (byte) idx#11 ← ++ (byte) idx#10 -- vbuxx=_inc_vbuxx
inx
// out::@return
// }
// [12] return
rts
}
// File Data

View File

@ -0,0 +1,17 @@
(label) @1
(label) @begin
(label) @end
(const byte*) SCREEN = (byte*) 1024
(byte) idx
(byte) idx#10 reg byte x 106.5
(byte) idx#11 reg byte x 28.0
(void()) main()
(label) main::@1
(label) main::@return
(void()) out((byte) out::c)
(label) out::@return
(byte) out::c
(byte) out::c#2 reg byte a 101.0
reg byte a [ out::c#2 ]
reg byte x [ idx#10 idx#11 ]

View File

@ -0,0 +1,46 @@
// Test effective live range and register allocation
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
.label SCREEN = $400
main: {
.label y = 3
.label x = 2
lda #0
sta.z x
__b1:
lda #0
sta.z y
__b2:
ldy #0
__b3:
// val1 = a+x
tya
clc
adc.z x
// print(y, val1)
ldx.z y
jsr print
// for( char a: 0..100 )
iny
cpy #$65
bne __b3
// for( char y: 0..100 )
inc.z y
lda #$65
cmp.z y
bne __b2
// for(char x: 0..100 )
inc.z x
cmp.z x
bne __b1
// }
rts
}
// print(byte register(X) idx, byte register(A) val)
print: {
// SCREEN[idx] = val
sta SCREEN,x
// }
rts
}

View File

@ -0,0 +1,50 @@
@begin: scope:[] from
[0] phi()
to:@1
@1: scope:[] from @begin
[1] phi()
[2] call main
to:@end
@end: scope:[] from @1
[3] phi()
(void()) main()
main: scope:[main] from @1
[4] phi()
to:main::@1
main::@1: scope:[main] from main main::@5
[5] (byte) main::x#7 ← phi( main/(byte) 0 main::@5/(byte) main::x#1 )
to:main::@2
main::@2: scope:[main] from main::@1 main::@4
[6] (byte) main::y#4 ← phi( main::@1/(byte) 0 main::@4/(byte) main::y#1 )
to:main::@3
main::@3: scope:[main] from main::@2 main::@6
[7] (byte) main::a#2 ← phi( main::@2/(byte) 0 main::@6/(byte) main::a#1 )
[8] (byte) main::val1#0 ← (byte) main::a#2 + (byte) main::x#7
[9] (byte) print::idx#0 ← (byte) main::y#4
[10] (byte) print::val#0 ← (byte) main::val1#0
[11] call print
to:main::@6
main::@6: scope:[main] from main::@3
[12] (byte) main::a#1 ← ++ (byte) main::a#2
[13] if((byte) main::a#1!=(byte) $65) goto main::@3
to:main::@4
main::@4: scope:[main] from main::@6
[14] (byte) main::y#1 ← ++ (byte) main::y#4
[15] if((byte) main::y#1!=(byte) $65) goto main::@2
to:main::@5
main::@5: scope:[main] from main::@4
[16] (byte) main::x#1 ← ++ (byte) main::x#7
[17] if((byte) main::x#1!=(byte) $65) goto main::@1
to:main::@return
main::@return: scope:[main] from main::@5
[18] return
to:@return
(void()) print((byte) print::idx , (byte) print::val)
print: scope:[print] from main::@3
[19] *((const byte*) SCREEN + (byte) print::idx#0) ← (byte) print::val#0
to:print::@return
print::@return: scope:[print] from print
[20] return
to:@return

2109
src/test/ref/liverange-2.log Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,36 @@
(label) @1
(label) @begin
(label) @end
(const byte*) SCREEN = (byte*) 1024
(void()) main()
(label) main::@1
(label) main::@2
(label) main::@3
(label) main::@4
(label) main::@5
(label) main::@6
(label) main::@return
(byte) main::a
(byte) main::a#1 reg byte y 15001.5
(byte) main::a#2 reg byte y 6000.6
(byte) main::val1
(byte) main::val1#0 reg byte a 10001.0
(byte) main::x
(byte) main::x#1 x zp[1]:2 151.5
(byte) main::x#7 x zp[1]:2 927.5454545454544
(byte) main::y
(byte) main::y#1 y zp[1]:3 1501.5
(byte) main::y#4 y zp[1]:3 1500.375
(void()) print((byte) print::idx , (byte) print::val)
(label) print::@return
(byte) print::idx
(byte) print::idx#0 reg byte x 55001.0
(byte) print::val
(byte) print::val#0 reg byte a 110002.0
zp[1]:2 [ main::x#7 main::x#1 ]
zp[1]:3 [ main::y#4 main::y#1 ]
reg byte y [ main::a#2 main::a#1 ]
reg byte a [ main::val1#0 ]
reg byte x [ print::idx#0 ]
reg byte a [ print::val#0 ]