1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-11-27 19:50:10 +00:00

Removed 1GB of memory usage. #306

This commit is contained in:
jespergravgaard 2019-09-03 23:11:41 +02:00
parent 036ff9259c
commit 9c5de66830
2 changed files with 33 additions and 45 deletions

View File

@ -17,9 +17,6 @@ import java.util.stream.Collectors;
*/
public class VariableReferenceInfos {
/** Variables referenced in each block. */
private Map<LabelRef, Collection<VariableRef>> blockReferencedVars;
/** Variables used in each block. */
private Map<LabelRef, Collection<VariableRef>> blockUsedVars;
@ -78,7 +75,7 @@ public class VariableReferenceInfos {
this.referencedSymbol = referencedSymbol;
}
public Integer getStatementIdx() {
Integer getStatementIdx() {
return statementIdx;
}
@ -111,7 +108,7 @@ public class VariableReferenceInfos {
this.referencedSymbol = referencedSymbol;
}
public SymbolVariableRef getReferencingSymbol() {
SymbolVariableRef getReferencingSymbol() {
return referencingSymbol;
}
@ -127,14 +124,12 @@ public class VariableReferenceInfos {
}
public VariableReferenceInfos(
Map<LabelRef, Collection<VariableRef>> blockReferencedVars,
Map<LabelRef, Collection<VariableRef>> blockUsedVars,
Map<LabelRef, Collection<LabelRef>> blockSuccessorClosure,
Map<SymbolVariableRef, Collection<ReferenceToSymbolVar>> symbolVarReferences,
Map<LabelRef, Collection<VariableReferenceInfos.ReferenceToSymbolVar>> blockVarReferences,
Map<Integer, Collection<VariableReferenceInfos.ReferenceToSymbolVar>> statementVarReferences
) {
this.blockReferencedVars = blockReferencedVars;
this.blockUsedVars = blockUsedVars;
this.blockSuccessorClosure = blockSuccessorClosure;
this.symbolVarReferences = symbolVarReferences;
@ -145,52 +140,44 @@ public class VariableReferenceInfos {
public String getSizeInfo() {
StringBuilder sizeInfo = new StringBuilder();
if(blockSuccessorClosure != null) {
sizeInfo.append("blockSuccessorClosure " + blockSuccessorClosure.size() + " labels ");
sizeInfo.append("blockSuccessorClosure ").append(blockSuccessorClosure.size()).append(" labels ");
int sub = 0;
for(Collection<LabelRef> labelRefs : blockSuccessorClosure.values()) {
sub += labelRefs.size();
}
sizeInfo.append(" " + sub + " labels" + "\n");
}
if(blockReferencedVars != null) {
sizeInfo.append("blockReferencedVars " + blockReferencedVars.size() + " labels ");
int sub = 0;
for(Collection<VariableRef> variableRefs : blockReferencedVars.values()) {
sub += variableRefs.size();
}
sizeInfo.append(" " + sub + " varrefs" + "\n");
sizeInfo.append(" ").append(sub).append(" labels").append("\n");
}
if(blockUsedVars != null) {
sizeInfo.append("blockUsedVars " + blockUsedVars.size() + " labels ");
sizeInfo.append("blockUsedVars ").append(blockUsedVars.size()).append(" labels ");
int sub = 0;
for(Collection<VariableRef> variableRefs : blockUsedVars.values()) {
sub += variableRefs.size();
}
sizeInfo.append(" " + sub + " varrefs" + "\n");
sizeInfo.append(" ").append(sub).append(" varrefs").append("\n");
}
{
sizeInfo.append("symbolVarReferences " + symbolVarReferences.size() + " SymbolVariableRefs ");
sizeInfo.append("symbolVarReferences ").append(symbolVarReferences.size()).append(" SymbolVariableRefs ");
int sub = 0;
for(Collection<ReferenceToSymbolVar> value : symbolVarReferences.values()) {
sub += value.size();
}
sizeInfo.append(" " + sub + " ReferenceToSymbolVars" + "\n");
sizeInfo.append(" ").append(sub).append(" ReferenceToSymbolVars").append("\n");
}
{
sizeInfo.append("statementVarReferences " + statementVarReferences.size() + " statements ");
sizeInfo.append("statementVarReferences ").append(statementVarReferences.size()).append(" statements ");
int sub = 0;
for(Collection<ReferenceToSymbolVar> value : statementVarReferences.values()) {
sub += value.size();
}
sizeInfo.append(" " + sub + " ReferenceToSymbolVars" + "\n");
sizeInfo.append(" ").append(sub).append(" ReferenceToSymbolVars").append("\n");
}
{
sizeInfo.append("blockVarReferences " + blockVarReferences.size() + " blocks ");
sizeInfo.append("blockVarReferences ").append(blockVarReferences.size()).append(" blocks ");
int sub = 0;
for(Collection<ReferenceToSymbolVar> value : blockVarReferences.values()) {
sub += value.size();
}
sizeInfo.append(" " + sub + " ReferenceToSymbolVars" + "\n");
sizeInfo.append(" ").append(sub).append(" ReferenceToSymbolVars").append("\n");
}
return sizeInfo.toString();
}
@ -212,7 +199,19 @@ public class VariableReferenceInfos {
* @return All used variables
*/
public Collection<VariableRef> getReferencedVars(LabelRef labelRef) {
return blockReferencedVars.get(labelRef);
ArrayList<VariableRef> variableRefs = new ArrayList<>();
blockSuccessorClosure.get(labelRef)
.forEach(labelRef1 -> blockVarReferences
.get(labelRef1)
.stream()
.filter(referenceToSymbolVar -> referenceToSymbolVar.getReferenced() instanceof VariableRef)
.forEach(referenceToSymbolVar -> {
if(!variableRefs.contains(referenceToSymbolVar.getReferenced()))
variableRefs.add((VariableRef) referenceToSymbolVar.getReferenced());
}
)
);
return variableRefs;
}
/**
@ -233,14 +232,13 @@ public class VariableReferenceInfos {
*/
public Collection<VariableRef> getDefinedVars(Statement stmt) {
Collection<ReferenceToSymbolVar> referenceToSymbolVars = statementVarReferences.get(stmt.getIndex());
List<VariableRef> variableRefs = referenceToSymbolVars
return referenceToSymbolVars
.stream()
.filter(referenceToSymbolVar -> referenceToSymbolVar.getReferenced() instanceof VariableRef)
.filter(referenceToSymbolVar -> ReferenceToSymbolVar.ReferenceType.DEFINE.equals(referenceToSymbolVar.getReferenceType()))
.map(ReferenceToSymbolVar::getReferenced)
.map(symbolVariableRef -> (VariableRef) symbolVariableRef)
.collect(Collectors.toList());
return variableRefs;
}
/**
@ -257,7 +255,7 @@ public class VariableReferenceInfos {
.stream()
.filter(referenceToSymbolVar -> referenceToSymbolVar.getReferenced() instanceof VariableRef)
.map(ReferenceToSymbolVar::getReferenced)
.map(symbolVariableRef -> (VariableRef)symbolVariableRef)
.map(symbolVariableRef -> (VariableRef) symbolVariableRef)
.collect(Collectors.toList());
return variableRefs;
}

View File

@ -22,12 +22,10 @@ public class PassNCalcVariableReferenceInfos extends PassNCalcBase<VariableRefer
super(program);
}
private LinkedHashMap<LabelRef, Collection<VariableRef>> blockDirectVarRefsMap = null;
private LinkedHashMap<LabelRef, Collection<VariableRef>> blockDirectUsedVarsMap = null;
@Override
public VariableReferenceInfos calculate() {
LinkedHashMap<LabelRef, Collection<VariableRef>> blockReferencedVars = new LinkedHashMap<>();
LinkedHashMap<LabelRef, Collection<VariableRef>> blockUsedVars = new LinkedHashMap<>();
LinkedHashMap<LabelRef, Collection<LabelRef>> blockSuccessors = new LinkedHashMap<>();
@ -35,10 +33,8 @@ public class PassNCalcVariableReferenceInfos extends PassNCalcBase<VariableRefer
Map<LabelRef, Collection<VariableReferenceInfos.ReferenceToSymbolVar>> blockVarReferences = new LinkedHashMap<>();
Map<Integer, Collection<VariableReferenceInfos.ReferenceToSymbolVar>> statementVarReferences = new LinkedHashMap<>();
blockDirectVarRefsMap = new LinkedHashMap<>();
blockDirectUsedVarsMap = new LinkedHashMap<>();
for(ControlFlowBlock block : getProgram().getGraph().getAllBlocks()) {
LinkedHashSet<VariableRef> blockDirectVarRefs = new LinkedHashSet<>();
LinkedHashSet<VariableRef> blockDirectUsedVars = new LinkedHashSet<>();
blockVarReferences.putIfAbsent(block.getLabel(), new ArrayList<>());
Collection<VariableReferenceInfos.ReferenceToSymbolVar> blockSymbols = blockVarReferences.get(block.getLabel());
@ -56,7 +52,6 @@ public class PassNCalcVariableReferenceInfos extends PassNCalcBase<VariableRefer
Collection<VariableRef> stmtDefinedVars = getDefinedVars(statement);
LinkedHashSet<VariableRef> stmtUsedVars = new LinkedHashSet<>(stmtVarRefs);
stmtUsedVars.removeAll(stmtDefinedVars);
blockDirectVarRefs.addAll(stmtVarRefs);
blockDirectUsedVars.addAll(stmtUsedVars);
// Add variable definitions to the statement
statementVarReferences.putIfAbsent(statement.getIndex(), new ArrayList<>());
@ -83,15 +78,12 @@ public class PassNCalcVariableReferenceInfos extends PassNCalcBase<VariableRefer
}
}
LabelRef blockLabel = block.getLabel();
blockDirectVarRefsMap.put(blockLabel, blockDirectVarRefs);
blockDirectUsedVarsMap.put(blockLabel, blockDirectUsedVars);
}
for(ControlFlowBlock block : getProgram().getGraph().getAllBlocks()) {
LabelRef blockLabel = block.getLabel();
LinkedHashSet<VariableRef> blockRecursiveVarRefs = new LinkedHashSet<>();
LinkedHashSet<VariableRef> blockRecursiveUsedVars = new LinkedHashSet<>();
findReferencedVars(block.getLabel(), block, blockRecursiveVarRefs, blockRecursiveUsedVars, new ArrayList<>());
blockReferencedVars.put(blockLabel, blockRecursiveVarRefs);
findReferencedVars(block.getLabel(), block, blockRecursiveUsedVars, new ArrayList<>());
blockUsedVars.put(blockLabel, blockRecursiveUsedVars);
LinkedHashSet<LabelRef> successorClosure = new LinkedHashSet<>();
findSuccessorClosure(block.getLabel(), successorClosure, new ArrayList<>());
@ -111,7 +103,7 @@ public class PassNCalcVariableReferenceInfos extends PassNCalcBase<VariableRefer
}
});
}
VariableReferenceInfos variableReferenceInfos = new VariableReferenceInfos(blockReferencedVars, blockUsedVars, blockSuccessors, symbolVarReferences, blockVarReferences, statementVarReferences);
VariableReferenceInfos variableReferenceInfos = new VariableReferenceInfos(blockUsedVars, blockSuccessors, symbolVarReferences, blockVarReferences, statementVarReferences);
if(getLog().isVerboseSSAOptimize()) {
getLog().append(variableReferenceInfos.getSizeInfo());
}
@ -171,11 +163,10 @@ public class PassNCalcVariableReferenceInfos extends PassNCalcBase<VariableRefer
*
* @param labelRef The block to examine
* @param block The block to examine (optional, saves lookup)
* @param referencedVars the set of referenced variables
* @param usedVars the set of referenced variables
* @param visited The blocks already visited during the search. Used to stop infinite recursion
*/
private void findReferencedVars(LabelRef labelRef, ControlFlowBlock block, LinkedHashSet<VariableRef> referencedVars, LinkedHashSet<VariableRef> usedVars, Collection<LabelRef> visited) {
private void findReferencedVars(LabelRef labelRef, ControlFlowBlock block, LinkedHashSet<VariableRef> usedVars, Collection<LabelRef> visited) {
if(labelRef == null || visited.contains(labelRef))
return;
visited.add(labelRef);
@ -184,11 +175,10 @@ public class PassNCalcVariableReferenceInfos extends PassNCalcBase<VariableRefer
if(block == null)
return;
}
referencedVars.addAll(blockDirectVarRefsMap.get(labelRef));
usedVars.addAll(blockDirectUsedVarsMap.get(labelRef));
findReferencedVars(block.getDefaultSuccessor(), null, referencedVars, usedVars, visited);
findReferencedVars(block.getConditionalSuccessor(), null, referencedVars, usedVars, visited);
findReferencedVars(block.getCallSuccessor(), null, referencedVars, usedVars, visited);
findReferencedVars(block.getDefaultSuccessor(), null, usedVars, visited);
findReferencedVars(block.getConditionalSuccessor(), null, usedVars, visited);
findReferencedVars(block.getCallSuccessor(), null, usedVars, visited);
}
/**