Skip to content

Commit

Permalink
Adds caching for block graphs, renames getControlGraph() to getMethod…
Browse files Browse the repository at this point in the history
…Graph()

Issue: #17
Issue: #92
  • Loading branch information
0x7CFE committed Jul 5, 2016
1 parent ce9f0b7 commit d74c752
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
16 changes: 10 additions & 6 deletions include/inference.h
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,8 @@ class TypeSystem {
bool sendToSuper = false);

InferContext* inferBlock(Type& block, const Type& arguments, TContextStack* parent);
ControlGraph* getControlGraph(TMethod* method);
ControlGraph* getMethodGraph(TMethod* method);
ControlGraph* getBlockGraph(st::ParsedBlock* parsedBlock);

void dumpAllContexts() const;
void drawCallGraph() const;
Expand All @@ -391,14 +392,17 @@ class TypeSystem {
// [Block, Args] -> block context
typedef std::map<Type, InferContext*> TBlockCache;

typedef std::map<st::ParsedBlock*, ControlGraph*> TBlockGraphCache;

private:
SmalltalkVM& m_vm; // TODO Image must be enough
SmalltalkVM& m_vm; // TODO Image must be enough

TGraphCache m_graphCache;
TContextCache m_contextCache;
TBlockCache m_blockCache;
TGraphCache m_graphCache;
TContextCache m_contextCache;
TBlockCache m_blockCache;
TBlockGraphCache m_blockGraphCache;

std::size_t m_lastContextIndex;
std::size_t m_lastContextIndex;
};

class TypeAnalyzer {
Expand Down
31 changes: 21 additions & 10 deletions src/TypeAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ void TypeAnalyzer::doMarkArguments(const InstructionNode& instruction) {
InferContext* TypeAnalyzer::getMethodContext() {
assert(m_blockType);

for (TContextStack* stack = m_contextStack.parent; stack; stack = stack->parent) {
for (TContextStack* stack = &m_contextStack; stack; stack = stack->parent) {
const TInteger contextIndex((*m_blockType)[Type::bstContextIndex].getValue());

if (stack->context.getIndex() == static_cast<std::size_t>(contextIndex))
Expand Down Expand Up @@ -626,9 +626,8 @@ void TypeAnalyzer::doPushBlock(const InstructionNode& instruction) {
blockType.pushSubType(Type(TInteger(argIndex))); // [Type::bstArgIndex]
blockType.pushSubType(Type(TInteger(m_context.getIndex()))); // [Type::bstContextIndex]

// TODO Cache and reuse in TypeSystem::inferBlock()
ControlGraph* const blockGraph = new ControlGraph(pushBlock->getParsedBlock()->getContainer(), pushBlock->getParsedBlock());
blockGraph->buildGraph();
ControlGraph* const blockGraph = m_system.getBlockGraph(pushBlock->getParsedBlock());
assert(blockGraph);

typedef ControlGraph::TMetaInfo::TIndexList TIndexList;
const TIndexList& readsTemporaries = blockGraph->getMeta().readsTemporaries;
Expand Down Expand Up @@ -1158,7 +1157,7 @@ void TypeAnalyzer::walkComplete() {
// std::printf("walk complete\n");
}

ControlGraph* TypeSystem::getControlGraph(TMethod* method) {
ControlGraph* TypeSystem::getMethodGraph(TMethod* method) {
TGraphCache::iterator iGraph = m_graphCache.find(method);
if (iGraph != m_graphCache.end())
return iGraph->second.second;
Expand All @@ -1182,6 +1181,19 @@ ControlGraph* TypeSystem::getControlGraph(TMethod* method) {
return controlGraph;
}

ControlGraph* TypeSystem::getBlockGraph(st::ParsedBlock* parsedBlock) {
TBlockGraphCache::const_iterator iGraph = m_blockGraphCache.find(parsedBlock);
if (iGraph != m_blockGraphCache.end())
return iGraph->second;

ControlGraph* const graph = new ControlGraph(parsedBlock->getContainer(), parsedBlock);
graph->buildGraph();

m_blockGraphCache[parsedBlock] = graph;

return graph;
}

InferContext* TypeSystem::inferMessage(
TSelector selector,
const Type& arguments,
Expand Down Expand Up @@ -1278,7 +1290,7 @@ InferContext* TypeSystem::inferMessage(
selector->toString().c_str(),
method->text->toString().c_str());

ControlGraph* const methodGraph = getControlGraph(method);
ControlGraph* const methodGraph = getMethodGraph(method);
assert(methodGraph);

TContextStack contextStack(*inferContext, parent);
Expand Down Expand Up @@ -1320,7 +1332,7 @@ InferContext* TypeSystem::inferBlock(Type& block, const Type& arguments, TContex
std::printf("Cached block context %s -> %p (index %u), cache size %u\n",
key.toString().c_str(), inferContext, inferContext->getIndex(), m_blockCache.size());

ControlGraph* const methodGraph = getControlGraph(method);
ControlGraph* const methodGraph = getMethodGraph(method);
assert(methodGraph);

std::printf("(%u) Analyzing block %s::%s ...\n",
Expand All @@ -1331,9 +1343,8 @@ InferContext* TypeSystem::inferBlock(Type& block, const Type& arguments, TContex
st::ParsedMethod* const parsedMethod = methodGraph->getParsedMethod();
st::ParsedBlock* const parsedBlock = parsedMethod->getParsedBlockByOffset(offset);

// TODO Cache
ControlGraph* const blockGraph = new ControlGraph(parsedMethod, parsedBlock);
blockGraph->buildGraph();
ControlGraph* const blockGraph = getBlockGraph(parsedBlock);
assert(blockGraph);

{
std::ostringstream ss;
Expand Down

0 comments on commit d74c752

Please sign in to comment.