18#include <llvm/ADT/DepthFirstIterator.h>
19#include <llvm/ADT/SmallVector.h>
20#include <llvm/Support/ErrorHandling.h>
27 if (
auto modOp = mlir::dyn_cast<mlir::ModuleOp>(op)) {
28 cg = std::make_unique<llzk::CallGraph>(modOp);
30 auto error_message =
"CallGraphAnalysis expects provided op to be a ModuleOp!";
31 op->emitError(error_message);
32 llvm::report_fatal_error(error_message);
45 mlir::Operation *, mlir::AnalysisManager &am
51 if (isReachableCached(A, B)) {
57 auto msg =
"CallGraph contains no starting node!";
59 llvm::report_fatal_error(msg);
68 auto dfsIt = llvm::df_begin<const CallGraphNode *>(startNode);
69 auto dfsEnd = llvm::df_end<const CallGraphNode *>(startNode);
70 for (; dfsIt != dfsEnd; ++dfsIt) {
78 for (
unsigned i = 0; i < dfsIt.getPathLength(); i++) {
79 FuncDefOp ancestorFn = dfsIt.getPath(i)->getCalledFunction();
80 reachabilityMap[ancestorFn].insert(currFn);
83 if (isReachableCached(currFn, B)) {
An analysis wrapper to compute the CallGraph for a Module.
CallGraphAnalysis(mlir::Operation *op)
This is a simple port of the mlir::CallGraphNode with llzk::CallGraph as a friend class,...
bool isExternal() const
Returns true if this node is an external node.
llzk::function::FuncDefOp getCalledFunction() const
Returns the called function that the callable region represents.
const llzk::CallGraph & getCallGraph() const
CallGraphReachabilityAnalysis(mlir::Operation *, mlir::AnalysisManager &am)
NOTE: the need for the mlir::Operation argument is a requirement of the mlir::getAnalysis method,...
bool isReachable(function::FuncDefOp &A, function::FuncDefOp &B) const
Returns whether B is reachable from A.
::mlir::Region * getCallableRegion()
Returns the region on the current operation that is callable.