1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-09-08 17:54:40 +00:00

Added cached map for statement from index

This commit is contained in:
jespergravgaard 2017-11-11 08:46:56 +01:00
parent 15d6935601
commit af281612d2
11 changed files with 45 additions and 31 deletions

View File

@ -194,7 +194,7 @@ public class Compiler {
//program.getLog().setVerboseLiveRanges(true);
new Pass3StatementBlocks(program).generateStatementBlocks();
new Pass3StatementInfos(program).generateStatementInfos();
new Pass3VariableReferenceInfos(program).generateVariableReferenceInfos();
new Pass3LiveRangesAnalysis(program).findLiveRanges();
program.getLog().append("CONTROL FLOW GRAPH - LIVE RANGES FOUND");
@ -210,7 +210,7 @@ public class Compiler {
new Pass3AddNopBeforeCallOns(program).generate();
new Pass3StatementIndices(program).generateStatementIndices();
new Pass3CallGraphAnalysis(program).findCallGraph();
new Pass3StatementBlocks(program).generateStatementBlocks();
new Pass3StatementInfos(program).generateStatementInfos();
new Pass3VariableReferenceInfos(program).generateVariableReferenceInfos();
new Pass3LiveRangesAnalysis(program).findLiveRanges();
program.getLog().append("CONTROL FLOW GRAPH - BEFORE EFFECTIVE LIVE RANGES");

View File

@ -25,7 +25,7 @@ public class AsmFragmentSignature {
ControlFlowBlock block,
Program program,
ControlFlowGraph graph) {
this.codeScopeRef = program.getStatementBlocks().getBlock(conditionalJump).getScope();
this.codeScopeRef = program.getStatementInfos().getBlock(conditionalJump).getScope();
this.bindings = new LinkedHashMap<>();
this.program = program;
String conditionalJumpSignature = conditionalJumpSignature(conditionalJump, block, graph);
@ -33,7 +33,7 @@ public class AsmFragmentSignature {
}
public AsmFragmentSignature(StatementAssignment assignment, Program program) {
this.codeScopeRef = program.getStatementBlocks().getBlock(assignment).getScope();
this.codeScopeRef = program.getStatementInfos().getBlock(assignment).getScope();
this.bindings = new LinkedHashMap<>();
this.program = program;
setSignature(assignmentSignature(
@ -51,14 +51,14 @@ public class AsmFragmentSignature {
}
public AsmFragmentSignature(StatementAssignment assignment, StatementAssignment assignmentAlu, Program program) {
this.codeScopeRef = program.getStatementBlocks().getBlock(assignment).getScope();
this.codeScopeRef = program.getStatementInfos().getBlock(assignment).getScope();
this.bindings = new LinkedHashMap<>();
this.program = program;
setSignature(assignmentWithAluSignature(assignment, assignmentAlu));
}
private String assignmentWithAluSignature(StatementAssignment assignment, StatementAssignment assignmentAlu) {
this.codeScopeRef = program.getStatementBlocks().getBlock(assignment).getScope();
this.codeScopeRef = program.getStatementInfos().getBlock(assignment).getScope();
if (!(assignment.getrValue2() instanceof VariableRef)) {
throw new AsmFragment.AluNotApplicableException("Error! ALU register only allowed as rValue2. " + assignment);
}

View File

@ -169,7 +169,7 @@ public class LiveRangeVariablesEffective {
Collection<VariableRef> aliveAtStmt = statementsLiveVariables.get(statement.getIndex());
CallPaths callPaths;
Collection<VariableRef> referencedInProcedure;
ControlFlowBlock block = program.getStatementBlocks().getBlock(statement);
ControlFlowBlock block = program.getStatementInfos().getBlock(statement);
ScopeRef scopeRef = block.getScope();
Scope scope = program.getScope().getScope(scopeRef);
if (scope instanceof Procedure) {
@ -190,7 +190,7 @@ public class LiveRangeVariablesEffective {
List<CallGraph.CallBlock.Call> path = calledPath.getPath();
CallGraph.CallBlock.Call lastCall = path.get(path.size() - 1);
Integer lastCallStatementIdx = lastCall.getCallStatementIdx();
LabelRef lastCallBlockRef = program.getStatementBlocks().getBlockRef(lastCallStatementIdx);
LabelRef lastCallBlockRef = program.getStatementInfos().getBlockRef(lastCallStatementIdx);
if(lastCallBlockRef.equals(block.getLabel())) {
if (callAliases == null) {
// Found a matching call!

View File

@ -28,7 +28,7 @@ public class Program {
private NaturalLoopSet loopSet;
/** Which block is each statement a part of. */
private StatementBlocks statementBlocks;
private StatementInfos statementInfos;
/** The variables freferenced by blocks/statements. */
private VariableReferenceInfos variableReferenceInfos;
/** The live ranges of all variables. */
@ -123,12 +123,12 @@ public class Program {
this.variableReferenceInfos = variableReferenceInfos;
}
public StatementBlocks getStatementBlocks() {
return statementBlocks;
public StatementInfos getStatementInfos() {
return statementInfos;
}
public void setStatementBlocks(StatementBlocks statementBlocks) {
this.statementBlocks = statementBlocks;
public void setStatementInfos(StatementInfos statementInfos) {
this.statementInfos = statementInfos;
}
public void setLiveRangeVariables(LiveRangeVariables liveRangeVariables) {

View File

@ -2,8 +2,8 @@ package dk.camelot64.kickc.model;
import java.util.Map;
/** Cached information about which block each statement is a part of */
public class StatementBlocks {
/** Cached information about statements is a part of (which block they bloing to, statement from idx, ...) */
public class StatementInfos {
/** The control flow graph. */
private ControlFlowGraph graph;
@ -11,9 +11,13 @@ public class StatementBlocks {
/** Maps statement index to block label. */
private Map<Integer, LabelRef> stmtBlocks;
public StatementBlocks(Program program, Map<Integer, LabelRef> stmtBlocks) {
/** Maps statement index to statement. */
private Map<Integer, Statement> stmtIdx;
public StatementInfos(Program program, Map<Integer, LabelRef> stmtBlocks, Map<Integer, Statement> stmtIdx) {
this.graph = program.getGraph();
this.stmtBlocks = stmtBlocks;
this.stmtIdx = stmtIdx;
}
/**
@ -52,5 +56,13 @@ public class StatementBlocks {
return graph.getBlock(getBlockRef(stmtIdx));
}
/**
* Get statement from index
* @param Statement index
* @return The statement with the passed index
*/
public Statement getStatement(int index) {
return stmtIdx.get(index);
}
}

View File

@ -109,7 +109,7 @@ public class Pass3LiveRangesAnalysis extends Pass2Base {
// Add all used variables to the previous statement (taking into account phi from blocks)
modified |= initUsedVars(liveRanges, stmt, previousStmt);
// Add all alive variables to previous that are used inside the method
ControlFlowBlock procBlock = getProgram().getStatementBlocks().getBlock(stmt);
ControlFlowBlock procBlock = getProgram().getStatementInfos().getBlock(stmt);
Procedure procedure = (Procedure) getProgram().getScope().getSymbol(procBlock.getLabel());
Collection<VariableRef> procUsed = referenceInfo.getUsed(procedure.getRef().getLabelRef());
// The call statement has no used or defined by itself so only work with the alive vars
@ -153,7 +153,7 @@ public class Pass3LiveRangesAnalysis extends Pass2Base {
// If current statement is a phi add the used variables to previous based on the phi entries
StatementPhiBlock phi = (StatementPhiBlock) stmt;
ControlFlowBlock previousBlock =
getProgram().getStatementBlocks().getBlock(previousStmt.getStatement());
getProgram().getStatementInfos().getBlock(previousStmt.getStatement());
for (StatementPhiBlock.PhiVariable phiVariable : phi.getPhiVariables()) {
for (StatementPhiBlock.PhiRValue phiRValue : phiVariable.getValues()) {
if (phiRValue.getPredecessor().equals(previousBlock.getLabel())) {
@ -252,12 +252,12 @@ public class Pass3LiveRangesAnalysis extends Pass2Base {
}
} else {
// No preceding statements. Examine if this is the first statement in a call.
ControlFlowBlock block = getProgram().getStatementBlocks().getBlock(statement);
ControlFlowBlock block = getProgram().getStatementInfos().getBlock(statement);
if (block.isProcedureEntry(getProgram())) {
// Current is first statement of a call - add the statement preceding the call.
Collection<CallGraph.CallBlock.Call> callers = getProgram().getCallGraph().getCallers(block.getLabel());
for (CallGraph.CallBlock.Call call : callers) {
Statement callStmt = getProgram().getGraph().getStatementByIndex(call.getCallStatementIdx());
Statement callStmt = getProgram().getStatementInfos().getStatement(call.getCallStatementIdx());
Collection<Statement> precedingCallStmt = getPrecedingStatement(callStmt);
for (Statement precedingCall : precedingCallStmt) {
previousStatements.add(new PreviousStatement(precedingCall, PreviousStatement.Type.BEFORE_METHOD));
@ -299,7 +299,7 @@ public class Pass3LiveRangesAnalysis extends Pass2Base {
private Collection<Statement> getPrecedingStatement(Statement statement) {
Statement previousStmt = null;
Statement prev = null;
ControlFlowBlock block = getProgram().getStatementBlocks().getBlock(statement);
ControlFlowBlock block = getProgram().getStatementInfos().getBlock(statement);
List<Statement> statements = block.getStatements();
for (Statement stmt : statements) {
if (statement.getIndex().equals(stmt.getIndex())) {

View File

@ -57,8 +57,8 @@ public class Pass3LiveRangesEffectiveAnalysis extends Pass2Base {
for (CallGraph.CallBlock.Call caller : callers) {
// Each caller creates its own call-paths
StatementCall callStatement =
(StatementCall) getProgram().getGraph().getStatementByIndex(caller.getCallStatementIdx());
ControlFlowBlock callBlock = getProgram().getStatementBlocks().getBlock(callStatement);
(StatementCall) getProgram().getStatementInfos().getStatement(caller.getCallStatementIdx());
ControlFlowBlock callBlock = getProgram().getStatementInfos().getBlock(callStatement);
ScopeRef callScopeRef = callBlock.getScope();
Scope callScope = getProgram().getScope().getScope(callScopeRef);
if (callScope instanceof Procedure) {

View File

@ -44,7 +44,7 @@ public class Pass3LoopDepthAnalysis extends Pass2Base {
Collection<CallGraph.CallBlock.Call> calls = callingBlock.getCalls(currentScope);
for (CallGraph.CallBlock.Call call : calls) {
int callStatementIdx = call.getCallStatementIdx();
ControlFlowBlock callingControlBlock = getProgram().getStatementBlocks().getBlock(callStatementIdx);
ControlFlowBlock callingControlBlock = getProgram().getStatementInfos().getBlock(callStatementIdx);
Collection<NaturalLoop> callingLoops = loopSet.getLoopsContainingBlock(callingControlBlock.getLabel());
for (NaturalLoop callingLoop : callingLoops) {
int potentialDepth = callingLoop.getDepth()+1;

View File

@ -8,23 +8,25 @@ import java.util.LinkedHashMap;
/**
* Identify the block for each statement.
*/
public class Pass3StatementBlocks extends Pass2Base {
public class Pass3StatementInfos extends Pass2Base {
public Pass3StatementBlocks(Program program) {
public Pass3StatementInfos(Program program) {
super(program);
}
/**
* Create map from statement index to block
*/
public void generateStatementBlocks() {
public void generateStatementInfos() {
LinkedHashMap<Integer, LabelRef> stmtBlocks = new LinkedHashMap<>();
LinkedHashMap<Integer, Statement> stmtIdx = new LinkedHashMap<>();
for (ControlFlowBlock block : getProgram().getGraph().getAllBlocks()) {
for (Statement statement : block.getStatements()) {
stmtBlocks.put(statement.getIndex(), block.getLabel());
stmtIdx.put(statement.getIndex(), statement);
}
}
getProgram().setStatementBlocks(new StatementBlocks(getProgram(), stmtBlocks));
getProgram().setStatementInfos(new StatementInfos(getProgram(), stmtBlocks, stmtIdx));
}

View File

@ -35,7 +35,7 @@ public class Pass4AssertNoCpuClobber extends Pass2Base {
if (asmSegment.getStatementIdx() != null) {
// Find the ICL statement
int statementIdx = asmSegment.getStatementIdx();
Statement statement = getGraph().getStatementByIndex(statementIdx);
Statement statement = getProgram().getStatementInfos().getStatement(statementIdx);
// Find the registered clobbered by the ASM asmSegment
AsmClobber asmSegmentClobber = asmSegment.getClobber();
Collection<Registers.Register> clobberRegisters = getClobberRegisters(asmSegmentClobber);
@ -49,7 +49,7 @@ public class Pass4AssertNoCpuClobber extends Pass2Base {
if(asmSegment.getPhiTransitionId()!=null && asmSegment.getPhiTransitionAssignmentIdx()!=null) {
String phiTransitionId = asmSegment.getPhiTransitionId();
int transitionAssignmentIdx = asmSegment.getPhiTransitionAssignmentIdx();
ControlFlowBlock statementBlock = getProgram().getStatementBlocks().getBlock(statementIdx);
ControlFlowBlock statementBlock = getProgram().getStatementInfos().getBlock(statementIdx);
PhiTransitions phiTransitions = new PhiTransitions(getProgram(), statementBlock);
PhiTransitions.PhiTransition phiTransition = phiTransitions.getTransition(phiTransitionId);
for (PhiTransitions.PhiTransition.PhiAssignment phiAssignment : phiTransition.getAssignments()) {

View File

@ -182,7 +182,7 @@ public class Pass4RegisterUpliftCombinations extends Pass2Base {
Integer statementIdx = asmSegment.getStatementIdx();
int maxLoopDepth = 1;
if (statementIdx != null) {
ControlFlowBlock block = program.getStatementBlocks().getBlock(statementIdx);
ControlFlowBlock block = program.getStatementInfos().getBlock(statementIdx);
maxLoopDepth = loopSet.getMaxLoopDepth(block.getLabel());
}
score += asmSegmentCycles * Math.pow(10, maxLoopDepth);