Index: include/llvm/IR/ModuleSummaryIndex.h =================================================================== --- include/llvm/IR/ModuleSummaryIndex.h +++ include/llvm/IR/ModuleSummaryIndex.h @@ -25,6 +25,7 @@ #include "llvm/ADT/StringRef.h" #include "llvm/IR/GlobalValue.h" #include "llvm/IR/Module.h" +#include "llvm/Transforms/IPO/FunctionAttrs.h" #include #include #include @@ -415,6 +416,8 @@ TIdInfo = llvm::make_unique(); TIdInfo->TypeTests.push_back(Guid); } + + friend struct GraphTraits; }; template <> struct DenseMapInfo { @@ -771,6 +774,56 @@ StringMap &ModuleToDefinedGVSummaries) const; }; +/// GraphTraits definition to build SCC for the index +template <> struct GraphTraits { + typedef FunctionSummary *NodeRef; + static NodeRef fsumFromEdge(FunctionSummary::EdgeTy &P) { + if (P.first.Ref && P.first.getSummaryList().size()) + return cast(P.first.getSummaryList().front().get()); + + // it's not in the index, so it's an external function + static auto ExternalFunction = llvm::make_unique( + FunctionSummary::GVFlags( + GlobalValue::LinkageTypes::AvailableExternallyLinkage, true, false), + 0, FunctionSummary::FFlags{}, std::vector(), + std::vector(), + std::vector(), + std::vector(), + std::vector(), + std::vector(), + std::vector()); + return ExternalFunction.get(); + } + using ChildIteratorType = + mapped_iterator::iterator, + decltype(&fsumFromEdge)>; + + // Use the first callee as the entry node + static NodeRef getEntryNode(FunctionSummary *F) { return F; } + + static ChildIteratorType child_begin(NodeRef N) { + return ChildIteratorType(N->CallGraphEdgeList.begin(), &fsumFromEdge); + } + + static ChildIteratorType child_end(NodeRef N) { + return ChildIteratorType(N->CallGraphEdgeList.end(), &fsumFromEdge); + } +}; + +template <> +struct GraphTraits + : public GraphTraits { + static NodeRef getEntryNode(ModuleSummaryIndex *I) { + for (auto &P : *I) { + if (P.second.SummaryList.size()) + if (FunctionSummary *FS = + dyn_cast(P.second.SummaryList.front().get())) + return FS; + } + assert(false && "ModuleSummary doesn't have any functions!"); + } +}; + } // end namespace llvm #endif // LLVM_IR_MODULESUMMARYINDEX_H