[llvm] r207411 - [LCG] Add the most basic of edge insertion to the lazy call graph. This

Chandler Carruth chandlerc at gmail.com
Mon Apr 28 04:10:23 PDT 2014


Author: chandlerc
Date: Mon Apr 28 06:10:23 2014
New Revision: 207411

URL: http://llvm.org/viewvc/llvm-project?rev=207411&view=rev
Log:
[LCG] Add the most basic of edge insertion to the lazy call graph. This
just handles the pre-DFS case. Also add some test cases for this case to
make sure it works.

Modified:
    llvm/trunk/include/llvm/Analysis/LazyCallGraph.h
    llvm/trunk/lib/Analysis/LazyCallGraph.cpp
    llvm/trunk/unittests/Analysis/LazyCallGraphTest.cpp

Modified: llvm/trunk/include/llvm/Analysis/LazyCallGraph.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/LazyCallGraph.h?rev=207411&r1=207410&r2=207411&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/LazyCallGraph.h (original)
+++ llvm/trunk/include/llvm/Analysis/LazyCallGraph.h Mon Apr 28 06:10:23 2014
@@ -163,6 +163,9 @@ public:
     /// CalleeIndexMap.
     Node(LazyCallGraph &G, Function &F);
 
+    /// \brief Internal helper to insert a callee.
+    void insertEdgeInternal(Function &Callee);
+
     /// \brief Internal helper to remove a callee from this node.
     void removeEdgeInternal(Function &Callee);
 
@@ -371,6 +374,14 @@ public:
   /// Once you begin manipulating a call graph's SCCs, you must perform all
   /// mutation of the graph via the SCC methods.
 
+  /// \brief Update the call graph after inserting a new edge.
+  void insertEdge(Node &Caller, Function &Callee);
+
+  /// \brief Update the call graph after inserting a new edge.
+  void insertEdge(Function &Caller, Function &Callee) {
+    return insertEdge(get(Caller), Callee);
+  }
+
   /// \brief Update the call graph after deleting an edge.
   void removeEdge(Node &Caller, Function &Callee);
 

Modified: llvm/trunk/lib/Analysis/LazyCallGraph.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/LazyCallGraph.cpp?rev=207411&r1=207410&r2=207411&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/LazyCallGraph.cpp (original)
+++ llvm/trunk/lib/Analysis/LazyCallGraph.cpp Mon Apr 28 06:10:23 2014
@@ -75,6 +75,14 @@ LazyCallGraph::Node::Node(LazyCallGraph
   findCallees(Worklist, Visited, Callees, CalleeIndexMap);
 }
 
+void LazyCallGraph::Node::insertEdgeInternal(Function &Callee) {
+  CalleeIndexMap.insert(std::make_pair(&Callee, Callees.size()));
+  if (Node *N = G->lookup(Callee))
+    Callees.push_back(N);
+  else
+    Callees.push_back(&Callee);
+}
+
 void LazyCallGraph::Node::removeEdgeInternal(Function &Callee) {
   auto IndexMapI = CalleeIndexMap.find(&Callee);
   assert(IndexMapI != CalleeIndexMap.end() &&
@@ -353,6 +361,13 @@ LazyCallGraph::SCC::removeIntraSCCEdge(N
   return ResultSCCs;
 }
 
+void LazyCallGraph::insertEdge(Node &CallerN, Function &Callee) {
+  assert(SCCMap.empty() && DFSStack.empty() &&
+         "This method cannot be called after SCCs have been formed!");
+
+  return CallerN.insertEdgeInternal(Callee);
+}
+
 void LazyCallGraph::removeEdge(Node &CallerN, Function &Callee) {
   assert(SCCMap.empty() && DFSStack.empty() &&
          "This method cannot be called after SCCs have been formed!");

Modified: llvm/trunk/unittests/Analysis/LazyCallGraphTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Analysis/LazyCallGraphTest.cpp?rev=207411&r1=207410&r2=207411&view=diff
==============================================================================
--- llvm/trunk/unittests/Analysis/LazyCallGraphTest.cpp (original)
+++ llvm/trunk/unittests/Analysis/LazyCallGraphTest.cpp Mon Apr 28 06:10:23 2014
@@ -255,6 +255,44 @@ static Function &lookupFunction(Module &
   report_fatal_error("Couldn't find function!");
 }
 
+TEST(LazyCallGraphTest, BasicGraphMutation) {
+  std::unique_ptr<Module> M = parseAssembly(
+      "define void @a() {\n"
+      "entry:\n"
+      "  call void @b()\n"
+      "  call void @c()\n"
+      "  ret void\n"
+      "}\n"
+      "define void @b() {\n"
+      "entry:\n"
+      "  ret void\n"
+      "}\n"
+      "define void @c() {\n"
+      "entry:\n"
+      "  ret void\n"
+      "}\n");
+  LazyCallGraph CG(*M);
+
+  LazyCallGraph::Node &A = CG.get(lookupFunction(*M, "a"));
+  LazyCallGraph::Node &B = CG.get(lookupFunction(*M, "b"));
+  EXPECT_EQ(2, std::distance(A.begin(), A.end()));
+  EXPECT_EQ(0, std::distance(B.begin(), B.end()));
+
+  CG.insertEdge(B, lookupFunction(*M, "c"));
+  EXPECT_EQ(1, std::distance(B.begin(), B.end()));
+  LazyCallGraph::Node &C = *B.begin();
+  EXPECT_EQ(0, std::distance(C.begin(), C.end()));
+
+  CG.insertEdge(C, B.getFunction());
+  EXPECT_EQ(1, std::distance(C.begin(), C.end()));
+  EXPECT_EQ(&B, &*C.begin());
+
+  CG.insertEdge(C, C.getFunction());
+  EXPECT_EQ(2, std::distance(C.begin(), C.end()));
+  EXPECT_EQ(&B, &*C.begin());
+  EXPECT_EQ(&C, &*(C.begin() + 1));
+}
+
 TEST(LazyCallGraphTest, MultiArmSCC) {
   // Two interlocking cycles. The really useful thing about this SCC is that it
   // will require Tarjan's DFS to backtrack and finish processing all of the





More information about the llvm-commits mailing list