Files
swift-mirror/lib/SILAnalysis/CallGraphAnalysis.cpp
Mark Lacey 34a1050222 Add data structures for a new, more detailed, call graph.
This is not yet hooked up. I want to add a dumper and some tests, and
need to implement code to do invocation- and reverse-invocation-ordering
over the SCCs of the call graph before it can fully replace the existing
code.

This call graph has edges for each call site, so it's really a
multigraph. Each call site tracks the set of functions that can be
called from that point. Currently that set is always a singleton since
we only handle applies of direct function_refs (like the existing call
graph). In time it will be expanded to handle other types of function
application.

Swift SVN r21407
2014-08-22 07:46:53 +00:00

62 lines
2.0 KiB
C++

//===----- CallGraphAnalysis.cpp - Call graph construction ----*- C++ -*---===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/SILAnalysis/CallGraphAnalysis.h"
using namespace swift;
CallGraph::CallGraph(SILModule *M, bool completeModule) {
// Build the initial call graph by creating a node for each
// function, and an edge for each direct call to a free function.
// TODO: Handle other kinds of applies.
unsigned NodeOrdinal = 0;
for (auto &F : *M)
if (F.isDefinition())
addCallGraphNode(&F, NodeOrdinal++);
for (auto &F : *M)
if (F.isDefinition())
addEdges(&F);
}
void CallGraph::addCallGraphNode(SILFunction *F, unsigned NodeOrdinal) {
auto *Node = new CallGraphNode(F, NodeOrdinal);
assert(!FunctionToNodeMap.count(F) &&
"Added function already has a call graph node!");
FunctionToNodeMap[F] = Node;
// TODO: Only add functions clearly visible from outside our
// compilation scope as roots.
CallGraphRoots.insert(Node);
}
void CallGraph::addEdges(SILFunction *F) {
auto *CallerNode = getCallGraphNode(F);
assert(CallerNode && "Expected call graph node for function!");
for (auto &BB : *F)
for (auto &I : BB)
if (auto *AI = dyn_cast<ApplyInst>(&I))
if (auto *FRI = dyn_cast<FunctionRefInst>(AI->getCallee())) {
auto *Callee = FRI->getReferencedFunction();
auto *CallSite = new CallGraphEdge(AI, Callee);
CallerNode->addCallSite(CallSite);
auto *CalleeNode = getCallGraphNode(Callee);
if (CalleeNode)
CalleeNode->addCaller(CallSite);
}
}